Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Publicious.org
Plugins
Media Categories
Commits
39ca48f9
Commit
39ca48f9
authored
Jan 20, 2020
by
Ian D
Committed by
John James Jacoby
Jan 19, 2020
Browse files
Select attachment categories with checkboxes (#3)
* Add files via upload
parent
af4e437d
Changes
4
Hide whitespace changes
Inline
Side-by-side
wp-media-categories/assets/js/media-views.js
View file @
39ca48f9
...
...
@@ -66,3 +66,57 @@ window.wp = window.wp || { };
$
(
'
.media-toolbar.wp-filter .delete-selected-button
'
).
before
(
$
(
'
.media-toolbar.wp-filter .select-mode-toggle-button
'
)
);
}
);
}
)(
jQuery
);
/* (c) Ian Dickety */
/*
* save-attachment-compat doesn't do as save-attachment does, in that
* while it pings ajax upon updating, there is no feedback to tell
* the user the input has been saved. This snippet provides feedback (loading/succesful only)
*/
jQuery
(
function
(
$
)
{
$
(
'
#wpcontent
'
).
ajaxSend
(
function
(
e
,
r
,
s
)
{
var
input
=
QueryStringToHash
(
s
.
data
);
if
(
input
.
action
==
"
save-attachment-compat
"
)
{
$
(
'
.attachment-details
'
).
removeClass
(
'
save-ready
'
).
addClass
(
'
save-waiting
'
);
}
});
$
(
'
#wpcontent
'
).
ajaxComplete
(
function
(
e
,
r
,
s
)
{
var
input
=
QueryStringToHash
(
s
.
data
);
if
(
input
.
action
==
"
save-attachment-compat
"
)
{
$
(
'
.attachment-details
'
).
removeClass
(
'
save-waiting
'
);
if
(
r
.
responseJSON
.
success
===
true
)
{
$
(
'
.attachment-details
'
).
addClass
(
"
save-complete
"
).
delay
(
2000
).
queue
(
function
(
next
){
$
(
this
).
removeClass
(
"
save-complete
"
).
addClass
(
"
save-ready
"
);
next
();
});
}
else
{
$
(
'
.attachment-details
'
).
addClass
(
"
save-ready
"
);
}
}
});
var
QueryStringToHash
=
function
QueryStringToHash
(
query
)
{
var
query_string
=
{};
var
vars
=
query
.
split
(
"
&
"
);
for
(
var
i
=
0
;
i
<
vars
.
length
;
i
++
)
{
var
pair
=
vars
[
i
].
split
(
"
=
"
);
pair
[
0
]
=
decodeURIComponent
(
pair
[
0
]);
pair
[
1
]
=
decodeURIComponent
(
pair
[
1
]);
// If first entry with this name
if
(
typeof
query_string
[
pair
[
0
]]
===
"
undefined
"
)
{
query_string
[
pair
[
0
]]
=
pair
[
1
];
// If second entry with this name
}
else
if
(
typeof
query_string
[
pair
[
0
]]
===
"
string
"
)
{
var
arr
=
[
query_string
[
pair
[
0
]],
pair
[
1
]
];
query_string
[
pair
[
0
]]
=
arr
;
// If third or later entry with this name
}
else
{
query_string
[
pair
[
0
]].
push
(
pair
[
1
]);
}
}
return
query_string
;
}
});
wp-media-categories/includes/ajax.php
View file @
39ca48f9
...
...
@@ -80,3 +80,57 @@ function wp_media_categories_ajax_query_attachments() {
wp_send_json_success
(
$posts
);
}
/**
* Updating categories in a post
*
* @since 1.0.2
*/
function
wp_media_categories_ajax_update_attachment_taxonomies
()
{
if
(
!
isset
(
$_REQUEST
[
'id'
]
)
)
{
wp_send_json_error
();
}
if
(
!
$id
=
absint
(
$_REQUEST
[
'id'
]
)
)
{
wp_send_json_error
();
}
if
(
empty
(
$_REQUEST
[
'attachments'
]
)
||
empty
(
$_REQUEST
[
'attachments'
][
$id
]
)
)
{
wp_send_json_error
();
}
$attachment_data
=
$_REQUEST
[
'attachments'
][
$id
];
check_ajax_referer
(
'update-post_'
.
$id
,
'nonce'
);
if
(
!
current_user_can
(
'edit_post'
,
$id
)
)
{
wp_send_json_error
();
}
$post
=
get_post
(
$id
,
ARRAY_A
);
if
(
'attachment'
!=
$post
[
'post_type'
]
)
{
wp_send_json_error
();
}
$post
=
apply_filters
(
'attachment_fields_to_save'
,
$post
,
$attachment_data
);
wp_update_post
(
$post
);
$taxonomy
=
"media_category"
;
if
(
isset
(
$attachment_data
[
$taxonomy
]
)
)
{
wp_set_object_terms
(
$id
,
array_map
(
'trim'
,
preg_split
(
'/,+/'
,
$attachment_data
[
$taxonomy
]
)
),
$taxonomy
,
false
);
}
else
if
(
isset
(
$_REQUEST
[
'tax_input'
])
&&
isset
(
$_REQUEST
[
'tax_input'
][
$taxonomy
]
)
)
{
wp_set_object_terms
(
$id
,
$_REQUEST
[
'tax_input'
][
$taxonomy
],
$taxonomy
,
false
);
}
else
{
wp_set_object_terms
(
$id
,
''
,
$taxonomy
,
false
);
}
if
(
!
$attachment
=
wp_prepare_attachment_for_js
(
$id
)
)
{
wp_send_json_error
();
}
wp_send_json_success
(
$attachment
);
}
wp-media-categories/includes/functions.php
View file @
39ca48f9
...
...
@@ -177,3 +177,88 @@ function wp_media_categories_pre_get_posts( WP_Query $query ) {
}
}
}
/**
* Fired on attachment update
*
* @since 1.0.2
*
* @param $fields The existing fields
* @param WP_POST $post The post
*/
function
wp_media_attachment_fields
(
$fields
,
$post
)
{
$tx
=
"media_category"
;
$t
=
$fields
[
$tx
];
$taxonomy
=
$t
[
"name"
];
if
(
!
empty
(
$t
))
{
if
(
!
$t
[
'public'
]
||
!
$t
[
'show_ui'
]
)
{
continue
;
}
if
(
empty
(
$t
[
'args'
])
)
{
$t
[
'args'
]
=
array
();
}
$terms
=
wp_get_object_terms
(
$post
->
ID
,
$taxonomy
,
$t
[
'args'
]);
$values
=
array
();
foreach
(
$terms
as
$term
)
{
$values
[]
=
$term
->
slug
;
}
$t
[
'value'
]
=
join
(
', '
,
$values
);
if
(
$t
[
'hierarchical'
]
)
{
ob_start
();
wp_terms_checklist
(
$post
->
ID
,
array
(
'taxonomy'
=>
"media_category"
,
'checked_ontop'
=>
false
,
'walker'
=>
new
WP_Media_Categories_Checklist_Walker
()
)
);
if
(
ob_get_contents
()
!=
false
)
{
$html
=
'<ul class="term-list">'
.
ob_get_contents
()
.
'</ul>'
;
}
else
{
$html
=
'<ul class="term-list"><li>No '
.
$t
[
'label'
]
.
'</li></ul>'
;
}
ob_end_clean
();
$t
[
'input'
]
=
'html'
;
$t
[
'html'
]
=
$html
;
}
$form_fields
[
$taxonomy
]
=
$t
;
}
return
$form_fields
;
}
/**
* Get IDs of all attached to a media category and pass them through to the default gallery shortcode for rendering
*
* @since 1.0.2
*
* @param $args Arguments from shortcode
*/
function
wp_media_categories_register_gallery_shortcode
(
$args
)
{
//ensure there's a category attribute
if
(
isset
(
$args
[
"category"
]))
{
$category
=
$args
[
"category"
];
unset
(
$args
[
"category"
]);
//ensure the taxonomy exists
$taxon
=
get_term_by
(
'slug'
,
$category
,
"media_category"
);
if
(
$taxon
===
false
)
{
return
;
}
$query
=
array
(
'post_status'
=>
'inherit'
,
'posts_per_page'
=>
-
1
,
'post_type'
=>
'attachment'
,
'tax_query'
=>
array
(
array
(
'taxonomy'
=>
'media_category'
,
'terms'
=>
array
(
$category
),
'field'
=>
'slug'
,
)
)
);
$the_query
=
new
WP_Query
(
$query
);
$p
=
$the_query
->
get_posts
();
if
(
isset
(
$p
)
&&
!
empty
(
$p
))
{
$ids
=
array
();
foreach
(
$p
as
$post
)
{
$ids
[]
=
$post
->
ID
;
}
}
$args
[
"include"
]
=
implode
(
","
,
$ids
);
return
gallery_shortcode
(
$args
);
}
}
wp-media-categories/includes/hooks.php
View file @
39ca48f9
<?php
/**
* Media Categories Actions & Filters
*
* @package Media/Categories/Hooks
*/
// Exit if accessed directly
defined
(
'ABSPATH'
)
||
exit
;
// Init
add_action
(
'init'
,
'wp_media_categories_register_media_taxonomy'
);
add_action
(
'init'
,
'wp_media_categories_register_widgets'
);
// Admin
add_action
(
'admin_enqueue_scripts'
,
'wp_media_categories_enqueue_admin_scripts'
);
add_action
(
'admin_footer-upload.php'
,
'wp_media_categories_custom_bulk_admin_footer'
);
add_action
(
'admin_notices'
,
'wp_media_categories_custom_bulk_admin_notices'
);
add_action
(
'load-upload.php'
,
'wp_media_categories_custom_bulk_action'
);
// Save attachments
add_action
(
'add_attachment'
,
'wp_media_categories_set_attachment_category'
);
add_action
(
'edit_attachment'
,
'wp_media_categories_set_attachment_category'
);
// Ajax
add_action
(
'wp_ajax_query-attachments'
,
'wp_media_categories_ajax_query_attachments'
,
0
);
// Some filters and action to process categories
add_action
(
'restrict_manage_posts'
,
'wp_media_categories_restrict_manage_posts'
);
// Filter for `no_category` media category attachments list-table requests
add_filter
(
'request'
,
'wp_media_categories_no_category_request'
);
// Filter theme-side media category queries
add_action
(
'pre_get_posts'
,
'wp_media_categories_pre_get_posts'
);
<?php
/**
* Media Categories Actions & Filters
*
* @package Media/Categories/Hooks
*/
// Exit if accessed directly
defined
(
'ABSPATH'
)
||
exit
;
// Init
add_action
(
'init'
,
'wp_media_categories_register_media_taxonomy'
);
add_action
(
'init'
,
'wp_media_categories_register_widgets'
);
// Admin
add_action
(
'admin_enqueue_scripts'
,
'wp_media_categories_enqueue_admin_scripts'
);
add_action
(
'admin_footer-upload.php'
,
'wp_media_categories_custom_bulk_admin_footer'
);
add_action
(
'admin_notices'
,
'wp_media_categories_custom_bulk_admin_notices'
);
add_action
(
'load-upload.php'
,
'wp_media_categories_custom_bulk_action'
);
// Save attachments
add_action
(
'add_attachment'
,
'wp_media_categories_set_attachment_category'
);
add_action
(
'edit_attachment'
,
'wp_media_categories_set_attachment_category'
);
// Set custom selector in media window
add_filter
(
'attachment_fields_to_edit'
,
'wp_media_attachment_fields'
,
10
,
2
);
// filter the attachments from user dropdown
add_action
(
'wp_ajax_query-attachments'
,
'wp_media_categories_ajax_query_attachments'
,
0
);
// update the categories
add_action
(
'wp_ajax_save-attachment-compat'
,
'wp_media_categories_ajax_update_attachment_taxonomies'
,
0
);
// Some filters and action to process categories
add_action
(
'restrict_manage_posts'
,
'wp_media_categories_restrict_manage_posts'
);
// Filter for `no_category` media category attachments list-table requests
add_filter
(
'request'
,
'wp_media_categories_no_category_request'
);
// Filter theme-side media category queries
add_action
(
'pre_get_posts'
,
'wp_media_categories_pre_get_posts'
);
// Add a shortcode to enable media_categories to be loaded into the default gallery shortcode
add_shortcode
(
'mc-gallery'
,
'wp_media_categories_register_gallery_shortcode'
);
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment