From 7f6f84606f2ed591294e42d49eb4154865513089 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Tue, 1 May 2018 15:42:38 +0000 Subject: [PATCH] Privacy: add "Mine" filter for media similarly to posts and comments. Props audrasjb. See #43820. git-svn-id: https://develop.svn.wordpress.org/trunk@43056 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/ajax-actions.php | 1 + .../includes/class-wp-media-list-table.php | 7 +++ src/wp-admin/includes/post.php | 4 ++ src/wp-includes/js/media-models.js | 22 +++---- src/wp-includes/js/media-views.js | 57 +++++++++++++++---- src/wp-includes/js/media/models/query.js | 20 ++++--- .../js/media/views/attachment-filters/all.js | 33 +++++++++-- .../views/attachment-filters/uploaded.js | 22 ++++++- src/wp-includes/media.php | 1 + 9 files changed, 129 insertions(+), 38 deletions(-) diff --git a/src/wp-admin/includes/ajax-actions.php b/src/wp-admin/includes/ajax-actions.php index 4f30007f1a..9e1b8c5554 100644 --- a/src/wp-admin/includes/ajax-actions.php +++ b/src/wp-admin/includes/ajax-actions.php @@ -2627,6 +2627,7 @@ function wp_ajax_query_attachments() { 'paged', 'post_mime_type', 'post_parent', + 'author', 'post__in', 'post__not_in', 'year', diff --git a/src/wp-admin/includes/class-wp-media-list-table.php b/src/wp-admin/includes/class-wp-media-list-table.php index 4b29ba67b9..f022cd38e3 100644 --- a/src/wp-admin/includes/class-wp-media-list-table.php +++ b/src/wp-admin/includes/class-wp-media-list-table.php @@ -130,6 +130,13 @@ class WP_Media_List_Table extends WP_List_Table { _x( 'Trash', 'attachment filter' ) ); } + + $type_links['mine'] = sprintf( + '', + selected( 'mine' === $filter, true, false ), + __( 'Mine' ) + ); + return $type_links; } diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 67ce58c2d8..392fea93e0 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -1211,6 +1211,10 @@ function wp_edit_attachments_query_vars( $q = false ) { $q['post_parent'] = 0; } + if ( isset( $q['mine'] ) || ( isset( $q['attachment-filter'] ) && 'mine' == $q['attachment-filter'] ) ) { + $q['author'] = get_current_user_id(); + } + // Filter query clauses to include filenames. if ( isset( $q['s'] ) ) { add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); diff --git a/src/wp-includes/js/media-models.js b/src/wp-includes/js/media-models.js index 28caa1553a..3bbe7471cd 100644 --- a/src/wp-includes/js/media-models.js +++ b/src/wp-includes/js/media-models.js @@ -1121,7 +1121,7 @@ Query = Attachments.extend(/** @lends wp.media.model.Query.prototype */{ // Only observe when a limited number of query args are set. There // are no filters for other properties, so observing will result in // false positives in those queries. - allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent' ]; + allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent', 'author' ]; if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() ) { this.observe( wp.Uploader.queue ); } @@ -1238,14 +1238,15 @@ Query = Attachments.extend(/** @lends wp.media.model.Query.prototype */{ * @readonly */ propmap: { - 'search': 's', - 'type': 'post_mime_type', - 'perPage': 'posts_per_page', - 'menuOrder': 'menu_order', - 'uploadedTo': 'post_parent', - 'status': 'post_status', - 'include': 'post__in', - 'exclude': 'post__not_in' + 'search': 's', + 'type': 'post_mime_type', + 'perPage': 'posts_per_page', + 'menuOrder': 'menu_order', + 'uploadedTo': 'post_parent', + 'status': 'post_status', + 'include': 'post__in', + 'exclude': 'post__not_in', + 'author': 'author' }, /** * Creates and returns an Attachments Query collection given the properties. @@ -1267,6 +1268,7 @@ Query = Attachments.extend(/** @lends wp.media.model.Query.prototype */{ * @param {Object} [props.menu_order] * @param {Object} [props.post_parent] * @param {Object} [props.post_status] + * @param {Object} [props.author] * @param {Object} [options] * * @returns {wp.media.model.Query} A new Attachments Query collection. @@ -1623,4 +1625,4 @@ module.exports = Selection; /***/ }) -/******/ }); \ No newline at end of file +/******/ }); diff --git a/src/wp-includes/js/media-views.js b/src/wp-includes/js/media-views.js index 53f02cde68..80899267a1 100644 --- a/src/wp-includes/js/media-views.js +++ b/src/wp-includes/js/media-views.js @@ -7251,6 +7251,7 @@ Uploaded = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attac createFilters: function() { var type = this.model.get('type'), types = wp.media.view.settings.mimeTypes, + uid = window.userSettings ? parseInt( window.userSettings.uid, 10 ) : 0, text; if ( types && type ) { @@ -7263,7 +7264,8 @@ Uploaded = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attac props: { uploadedTo: null, orderby: 'date', - order: 'DESC' + order: 'DESC', + author: null }, priority: 10 }, @@ -7273,7 +7275,8 @@ Uploaded = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attac props: { uploadedTo: wp.media.view.settings.post.id, orderby: 'menuOrder', - order: 'ASC' + order: 'ASC', + author: null }, priority: 20 }, @@ -7283,11 +7286,24 @@ Uploaded = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attac props: { uploadedTo: 0, orderby: 'menuOrder', - order: 'ASC' + order: 'ASC', + author: null }, priority: 50 } }; + + if ( uid ) { + this.filters.mine = { + text: l10n.mine, + props: { + orderby: 'date', + order: 'DESC', + author: uid + }, + priority: 50 + }; + } } }); @@ -7314,7 +7330,8 @@ var l10n = wp.media.view.l10n, */ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.AttachmentFilters.All.prototype */{ createFilters: function() { - var filters = {}; + var filters = {}, + uid = window.userSettings ? parseInt( window.userSettings.uid, 10 ) : 0; _.each( wp.media.view.settings.mimeTypes || {}, function( text, key ) { filters[ key ] = { @@ -7324,7 +7341,8 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment type: key, uploadedTo: null, orderby: 'date', - order: 'DESC' + order: 'DESC', + author: null } }; }); @@ -7336,7 +7354,8 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment type: null, uploadedTo: null, orderby: 'date', - order: 'DESC' + order: 'DESC', + author: null }, priority: 10 }; @@ -7349,7 +7368,8 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment type: null, uploadedTo: wp.media.view.settings.post.id, orderby: 'menuOrder', - order: 'ASC' + order: 'ASC', + author: null }, priority: 20 }; @@ -7362,11 +7382,27 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment uploadedTo: 0, type: null, orderby: 'menuOrder', - order: 'ASC' + order: 'ASC', + author: null }, priority: 50 }; + if ( uid ) { + filters.mine = { + text: l10n.mine, + props: { + status: null, + type: null, + uploadedTo: null, + orderby: 'date', + order: 'DESC', + author: uid + }, + priority: 50 + }; + } + if ( wp.media.view.settings.mediaTrash && this.controller.isModeActive( 'grid' ) ) { @@ -7377,7 +7413,8 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment status: 'trash', type: null, orderby: 'date', - order: 'DESC' + order: 'DESC', + author: null }, priority: 50 }; @@ -9425,4 +9462,4 @@ module.exports = Spinner; /***/ }) -/******/ ])); \ No newline at end of file +/******/ ])); diff --git a/src/wp-includes/js/media/models/query.js b/src/wp-includes/js/media/models/query.js index 420e4bbeae..560e54944d 100644 --- a/src/wp-includes/js/media/models/query.js +++ b/src/wp-includes/js/media/models/query.js @@ -71,7 +71,7 @@ Query = Attachments.extend(/** @lends wp.media.model.Query.prototype */{ // Only observe when a limited number of query args are set. There // are no filters for other properties, so observing will result in // false positives in those queries. - allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent' ]; + allowed = [ 's', 'order', 'orderby', 'posts_per_page', 'post_mime_type', 'post_parent', 'author' ]; if ( wp.Uploader && _( this.args ).chain().keys().difference( allowed ).isEmpty().value() ) { this.observe( wp.Uploader.queue ); } @@ -188,14 +188,15 @@ Query = Attachments.extend(/** @lends wp.media.model.Query.prototype */{ * @readonly */ propmap: { - 'search': 's', - 'type': 'post_mime_type', - 'perPage': 'posts_per_page', - 'menuOrder': 'menu_order', - 'uploadedTo': 'post_parent', - 'status': 'post_status', - 'include': 'post__in', - 'exclude': 'post__not_in' + 'search': 's', + 'type': 'post_mime_type', + 'perPage': 'posts_per_page', + 'menuOrder': 'menu_order', + 'uploadedTo': 'post_parent', + 'status': 'post_status', + 'include': 'post__in', + 'exclude': 'post__not_in', + 'author': 'author' }, /** * Creates and returns an Attachments Query collection given the properties. @@ -217,6 +218,7 @@ Query = Attachments.extend(/** @lends wp.media.model.Query.prototype */{ * @param {Object} [props.menu_order] * @param {Object} [props.post_parent] * @param {Object} [props.post_status] + * @param {Object} [props.author] * @param {Object} [options] * * @returns {wp.media.model.Query} A new Attachments Query collection. diff --git a/src/wp-includes/js/media/views/attachment-filters/all.js b/src/wp-includes/js/media/views/attachment-filters/all.js index fa85cbdba2..16170180fa 100644 --- a/src/wp-includes/js/media/views/attachment-filters/all.js +++ b/src/wp-includes/js/media/views/attachment-filters/all.js @@ -14,7 +14,8 @@ var l10n = wp.media.view.l10n, */ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.AttachmentFilters.All.prototype */{ createFilters: function() { - var filters = {}; + var filters = {}, + uid = window.userSettings ? parseInt( window.userSettings.uid, 10 ) : 0; _.each( wp.media.view.settings.mimeTypes || {}, function( text, key ) { filters[ key ] = { @@ -24,7 +25,8 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment type: key, uploadedTo: null, orderby: 'date', - order: 'DESC' + order: 'DESC', + author: null } }; }); @@ -36,7 +38,8 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment type: null, uploadedTo: null, orderby: 'date', - order: 'DESC' + order: 'DESC', + author: null }, priority: 10 }; @@ -49,7 +52,8 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment type: null, uploadedTo: wp.media.view.settings.post.id, orderby: 'menuOrder', - order: 'ASC' + order: 'ASC', + author: null }, priority: 20 }; @@ -62,11 +66,27 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment uploadedTo: 0, type: null, orderby: 'menuOrder', - order: 'ASC' + order: 'ASC', + author: null }, priority: 50 }; + if ( uid ) { + filters.mine = { + text: l10n.mine, + props: { + status: null, + type: null, + uploadedTo: null, + orderby: 'date', + order: 'DESC', + author: uid + }, + priority: 50 + }; + } + if ( wp.media.view.settings.mediaTrash && this.controller.isModeActive( 'grid' ) ) { @@ -77,7 +97,8 @@ All = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attachment status: 'trash', type: null, orderby: 'date', - order: 'DESC' + order: 'DESC', + author: null }, priority: 50 }; diff --git a/src/wp-includes/js/media/views/attachment-filters/uploaded.js b/src/wp-includes/js/media/views/attachment-filters/uploaded.js index 167d8d47ca..705414d2b6 100644 --- a/src/wp-includes/js/media/views/attachment-filters/uploaded.js +++ b/src/wp-includes/js/media/views/attachment-filters/uploaded.js @@ -16,6 +16,7 @@ Uploaded = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attac createFilters: function() { var type = this.model.get('type'), types = wp.media.view.settings.mimeTypes, + uid = window.userSettings ? parseInt( window.userSettings.uid, 10 ) : 0, text; if ( types && type ) { @@ -28,7 +29,8 @@ Uploaded = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attac props: { uploadedTo: null, orderby: 'date', - order: 'DESC' + order: 'DESC', + author: null }, priority: 10 }, @@ -38,7 +40,8 @@ Uploaded = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attac props: { uploadedTo: wp.media.view.settings.post.id, orderby: 'menuOrder', - order: 'ASC' + order: 'ASC', + author: null }, priority: 20 }, @@ -48,11 +51,24 @@ Uploaded = wp.media.view.AttachmentFilters.extend(/** @lends wp.media.view.Attac props: { uploadedTo: 0, orderby: 'menuOrder', - order: 'ASC' + order: 'ASC', + author: null }, priority: 50 } }; + + if ( uid ) { + this.filters.mine = { + text: l10n.mine, + props: { + orderby: 'date', + order: 'DESC', + author: uid + }, + priority: 50 + }; + } } }); diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 87321fe945..2f3c0de83d 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -3657,6 +3657,7 @@ function wp_enqueue_media( $args = array() ) { 'noItemsFound' => __( 'No items found.' ), 'insertIntoPost' => $post_type_object->labels->insert_into_item, 'unattached' => __( 'Unattached' ), + 'mine' => __( 'Mine' ), 'trash' => _x( 'Trash', 'noun' ), 'uploadedToThisPost' => $post_type_object->labels->uploaded_to_this_item, 'warnDelete' => __( "You are about to permanently delete this item from your site.\nThis action cannot be undone.\n 'Cancel' to stop, 'OK' to delete." ),