From c5f9a7c26a62cbd13d70cc6fd79359e4535c965e Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Sat, 3 Jan 2015 08:24:22 +0000 Subject: [PATCH] Respect query vars for taxonomies passed as URL parameters when in grid mode of Media Library. Fixes #30584. Built from https://develop.svn.wordpress.org/trunk@31037 git-svn-id: http://core.svn.wordpress.org/trunk@31018 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 10 +++++-- wp-admin/includes/post.php | 47 ++++++++++++++++++++---------- wp-admin/js/media.js | 9 ++++-- wp-admin/js/media.min.js | 2 +- wp-admin/upload.php | 5 ++++ wp-includes/version.php | 2 +- 6 files changed, 53 insertions(+), 22 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 86b453f8f9..b9f13807c6 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -2159,11 +2159,17 @@ function wp_ajax_query_attachments() { wp_send_json_error(); $query = isset( $_REQUEST['query'] ) ? (array) $_REQUEST['query'] : array(); - $query = array_intersect_key( $query, array_flip( array( + $keys = array( 's', 'order', 'orderby', 'posts_per_page', 'paged', 'post_mime_type', 'post_parent', 'post__in', 'post__not_in', 'year', 'monthnum' - ) ) ); + ); + foreach ( get_taxonomies_for_attachments( 'objects' ) as $t ) { + if ( $t->query_var && isset( $query[ $t->query_var ] ) ) { + $keys[] = $t->query_var; + } + } + $query = array_intersect_key( $query, array_flip( $keys ) ); $query['post_type'] = 'attachment'; if ( MEDIA_TRASH && ! empty( $_REQUEST['query']['post_status'] ) diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index 4906030922..757f22834b 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -1021,32 +1021,34 @@ function get_available_post_mime_types($type = 'attachment') { } /** - * Executes a query for attachments. An array of WP_Query arguments - * can be passed in, which will override the arguments set by this function. + * Get the query vars for the current attachments request * - * @since 2.5.0 + * @since 4.2.0 * - * @param array|bool $q Array of query variables to use to build the query or false to use $_GET superglobal. - * @return array + * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal. + * + * @return array The parsed query vars. */ -function wp_edit_attachments_query( $q = false ) { - if ( false === $q ) +function wp_edit_attachments_query_vars( $q = false ) { + if ( false === $q ) { $q = $_GET; - + } $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; $q['post_type'] = 'attachment'; $post_type = get_post_type_object( 'attachment' ); $states = 'inherit'; - if ( current_user_can( $post_type->cap->read_private_posts ) ) + if ( current_user_can( $post_type->cap->read_private_posts ) ) { $states .= ',private'; + } $q['post_status'] = isset( $q['status'] ) && 'trash' == $q['status'] ? 'trash' : $states; $q['post_status'] = isset( $q['attachment-filter'] ) && 'trash' == $q['attachment-filter'] ? 'trash' : $states; $media_per_page = (int) get_user_option( 'upload_per_page' ); - if ( empty( $media_per_page ) || $media_per_page < 1 ) + if ( empty( $media_per_page ) || $media_per_page < 1 ) { $media_per_page = 20; + } /** * Filter the number of items to list per page when listing media items. @@ -1058,10 +1060,9 @@ function wp_edit_attachments_query( $q = false ) { $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page ); $post_mime_types = get_post_mime_types(); - $avail_post_mime_types = get_available_post_mime_types('attachment'); - - if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) + if ( isset($q['post_mime_type']) && !array_intersect( (array) $q['post_mime_type'], array_keys($post_mime_types) ) ) { unset($q['post_mime_type']); + } foreach( array_keys( $post_mime_types ) as $type ) { if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" == $q['attachment-filter'] ) { @@ -1074,9 +1075,25 @@ function wp_edit_attachments_query( $q = false ) { $q['post_parent'] = 0; } - wp( $q ); + return $q; +} - return array($post_mime_types, $avail_post_mime_types); +/** + * Executes a query for attachments. An array of WP_Query arguments + * can be passed in, which will override the arguments set by this function. + * + * @since 2.5.0 + * + * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal. + * @return array + */ +function wp_edit_attachments_query( $q = false ) { + wp( wp_edit_attachments_query_vars( $q ) ); + + $post_mime_types = get_post_mime_types(); + $avail_post_mime_types = get_available_post_mime_types( 'attachment' ); + + return array( $post_mime_types, $avail_post_mime_types ); } /** diff --git a/wp-admin/js/media.js b/wp-admin/js/media.js index a071cd6001..8e4ab7dfe8 100644 --- a/wp-admin/js/media.js +++ b/wp-admin/js/media.js @@ -1,4 +1,4 @@ -/* global ajaxurl, attachMediaBoxL10n */ +/* global ajaxurl, attachMediaBoxL10n, _wpMediaGridSettings */ var findPosts; ( function( $ ){ @@ -72,13 +72,16 @@ var findPosts; }; $( document ).ready( function() { - var $mediaGridWrap = $( '#wp-media-grid' ); + var settings, $mediaGridWrap = $( '#wp-media-grid' ); // Open up a manage media frame into the grid. if ( $mediaGridWrap.length && window.wp && window.wp.media ) { + settings = _wpMediaGridSettings; + window.wp.media({ frame: 'manage', - container: $mediaGridWrap + container: $mediaGridWrap, + library: settings.queryVars }).open(); } diff --git a/wp-admin/js/media.min.js b/wp-admin/js/media.min.js index 0b040ab731..f86dd45f61 100644 --- a/wp-admin/js/media.min.js +++ b/wp-admin/js/media.min.js @@ -1 +1 @@ -var findPosts;!function(a){findPosts={open:function(b,c){var d=a(".ui-find-overlay");return 0===d.length&&(a("body").append('
'),findPosts.overlay()),d.show(),b&&c&&a("#affected").attr("name",b).val(c),a("#find-posts").show(),a("#find-posts-input").focus().keyup(function(a){27==a.which&&findPosts.close()}),findPosts.send(),!1},close:function(){a("#find-posts-response").html(""),a("#find-posts").hide(),a(".ui-find-overlay").hide()},overlay:function(){a(".ui-find-overlay").on("click",function(){findPosts.close()})},send:function(){var b={ps:a("#find-posts-input").val(),action:"find_posts",_ajax_nonce:a("#_ajax_nonce").val()},c=a(".find-box-search .spinner");c.show(),a.ajax(ajaxurl,{type:"POST",data:b,dataType:"json"}).always(function(){c.hide()}).done(function(b){b.success||a("#find-posts-response").text(attachMediaBoxL10n.error),a("#find-posts-response").html(b.data)}).fail(function(){a("#find-posts-response").text(attachMediaBoxL10n.error)})}},a(document).ready(function(){var b=a("#wp-media-grid");b.length&&window.wp&&window.wp.media&&window.wp.media({frame:"manage",container:b}).open(),a("#find-posts-submit").click(function(b){a('#find-posts-response input[type="radio"]:checked').length||b.preventDefault()}),a("#find-posts .find-box-search :input").keypress(function(a){return 13==a.which?(findPosts.send(),!1):void 0}),a("#find-posts-search").click(findPosts.send),a("#find-posts-close").click(findPosts.close),a("#doaction, #doaction2").click(function(b){a('select[name^="action"]').each(function(){"attach"===a(this).val()&&(b.preventDefault(),findPosts.open())})}),a(".find-box-inside").on("click","tr",function(){a(this).find(".found-radio input").prop("checked",!0)})})}(jQuery); \ No newline at end of file +var findPosts;!function(a){findPosts={open:function(b,c){var d=a(".ui-find-overlay");return 0===d.length&&(a("body").append('
'),findPosts.overlay()),d.show(),b&&c&&a("#affected").attr("name",b).val(c),a("#find-posts").show(),a("#find-posts-input").focus().keyup(function(a){27==a.which&&findPosts.close()}),findPosts.send(),!1},close:function(){a("#find-posts-response").html(""),a("#find-posts").hide(),a(".ui-find-overlay").hide()},overlay:function(){a(".ui-find-overlay").on("click",function(){findPosts.close()})},send:function(){var b={ps:a("#find-posts-input").val(),action:"find_posts",_ajax_nonce:a("#_ajax_nonce").val()},c=a(".find-box-search .spinner");c.show(),a.ajax(ajaxurl,{type:"POST",data:b,dataType:"json"}).always(function(){c.hide()}).done(function(b){b.success||a("#find-posts-response").text(attachMediaBoxL10n.error),a("#find-posts-response").html(b.data)}).fail(function(){a("#find-posts-response").text(attachMediaBoxL10n.error)})}},a(document).ready(function(){var b,c=a("#wp-media-grid");c.length&&window.wp&&window.wp.media&&(b=_wpMediaGridSettings,window.wp.media({frame:"manage",container:c,library:b.queryVars}).open()),a("#find-posts-submit").click(function(b){a('#find-posts-response input[type="radio"]:checked').length||b.preventDefault()}),a("#find-posts .find-box-search :input").keypress(function(a){return 13==a.which?(findPosts.send(),!1):void 0}),a("#find-posts-search").click(findPosts.send),a("#find-posts-close").click(findPosts.close),a("#doaction, #doaction2").click(function(b){a('select[name^="action"]').each(function(){"attach"===a(this).val()&&(b.preventDefault(),findPosts.open())})}),a(".find-box-inside").on("click","tr",function(){a(this).find(".found-radio input").prop("checked",!0)})})}(jQuery); \ No newline at end of file diff --git a/wp-admin/upload.php b/wp-admin/upload.php index dbdf3100b5..d0aa5e69c4 100644 --- a/wp-admin/upload.php +++ b/wp-admin/upload.php @@ -24,8 +24,13 @@ if ( 'grid' === $mode ) { wp_enqueue_media(); wp_enqueue_script( 'media-grid' ); wp_enqueue_script( 'media' ); + + $vars = wp_edit_attachments_query_vars(); + unset( $vars['mode'], $vars['post_type'], $vars['post_status'], $vars['posts_per_page'] ); + wp_localize_script( 'media-grid', '_wpMediaGridSettings', array( 'adminUrl' => parse_url( self_admin_url(), PHP_URL_PATH ), + 'queryVars' => $vars ) ); get_current_screen()->add_help_tab( array( diff --git a/wp-includes/version.php b/wp-includes/version.php index 78289c85b3..385027e4a2 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.2-alpha-31036'; +$wp_version = '4.2-alpha-31037'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.