2017-06-15 11:37:53 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Creates a dialog containing posts that can have a particular media attached
|
|
|
|
* to it.
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
2018-06-27 22:30:15 -04:00
|
|
|
* @output wp-admin/js/media.js
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
2017-12-15 08:42:46 -05:00
|
|
|
* @namespace findPosts
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
|
|
|
* @requires jQuery
|
|
|
|
*/
|
2018-06-27 22:30:15 -04:00
|
|
|
|
2022-03-10 13:43:02 -05:00
|
|
|
/* global ajaxurl, _wpMediaGridSettings, showNotice, findPosts, ClipboardJS */
|
2017-06-15 11:37:53 -04:00
|
|
|
|
2014-03-04 22:54:15 -05:00
|
|
|
( function( $ ){
|
2018-08-19 09:33:24 -04:00
|
|
|
window.findPosts = {
|
2017-06-15 11:37:53 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Opens a dialog to attach media to a post.
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
|
|
|
* Adds an overlay prior to retrieving a list of posts to attach the media to.
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
*
|
|
|
|
* @memberOf findPosts
|
|
|
|
*
|
|
|
|
* @param {string} af_name The name of the affected element.
|
|
|
|
* @param {string} af_val The value of the affected post element.
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {boolean} Always returns false.
|
2017-06-15 11:37:53 -04:00
|
|
|
*/
|
2014-03-04 22:54:15 -05:00
|
|
|
open: function( af_name, af_val ) {
|
|
|
|
var overlay = $( '.ui-find-overlay' );
|
2012-11-20 13:50:15 -05:00
|
|
|
|
2013-11-15 00:16:09 -05:00
|
|
|
if ( overlay.length === 0 ) {
|
2012-11-20 13:50:15 -05:00
|
|
|
$( 'body' ).append( '<div class="ui-find-overlay"></div>' );
|
|
|
|
findPosts.overlay();
|
|
|
|
}
|
|
|
|
|
|
|
|
overlay.show();
|
2012-08-22 20:04:18 -04:00
|
|
|
|
|
|
|
if ( af_name && af_val ) {
|
2017-06-15 11:37:53 -04:00
|
|
|
// #affected is a hidden input field in the dialog that keeps track of which media should be attached.
|
2014-03-04 22:54:15 -05:00
|
|
|
$( '#affected' ).attr( 'name', af_name ).val( af_val );
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
2014-03-04 18:11:13 -05:00
|
|
|
|
2014-03-04 22:54:15 -05:00
|
|
|
$( '#find-posts' ).show();
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
// Close the dialog when the escape key is pressed.
|
2021-02-23 14:45:04 -05:00
|
|
|
$('#find-posts-input').trigger( 'focus' ).on( 'keyup', function( event ){
|
2014-03-04 22:54:15 -05:00
|
|
|
if ( event.which == 27 ) {
|
|
|
|
findPosts.close();
|
2017-06-15 11:37:53 -04:00
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
// Retrieves a list of applicable posts for media attachment and shows them.
|
2012-11-20 13:50:15 -05:00
|
|
|
findPosts.send();
|
|
|
|
|
2012-08-22 20:04:18 -04:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Clears the found posts lists before hiding the attach media dialog.
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
*
|
|
|
|
* @memberOf findPosts
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-06-15 11:37:53 -04:00
|
|
|
*/
|
2014-03-04 22:54:15 -05:00
|
|
|
close: function() {
|
2015-03-09 15:44:26 -04:00
|
|
|
$('#find-posts-response').empty();
|
2014-03-04 22:54:15 -05:00
|
|
|
$('#find-posts').hide();
|
2012-11-20 13:50:15 -05:00
|
|
|
$( '.ui-find-overlay' ).hide();
|
|
|
|
},
|
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Binds a click event listener to the overlay which closes the attach media
|
|
|
|
* dialog.
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @memberOf findPosts
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-06-15 11:37:53 -04:00
|
|
|
*/
|
2014-03-04 22:54:15 -05:00
|
|
|
overlay: function() {
|
2014-03-04 18:11:13 -05:00
|
|
|
$( '.ui-find-overlay' ).on( 'click', function () {
|
2012-11-20 13:50:15 -05:00
|
|
|
findPosts.close();
|
|
|
|
});
|
2012-08-22 20:04:18 -04:00
|
|
|
},
|
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Retrieves and displays posts based on the search term.
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
2017-12-18 09:14:53 -05:00
|
|
|
* Sends a post request to the admin_ajax.php, requesting posts based on the
|
|
|
|
* search term provided by the user. Defaults to all posts if no search term is
|
|
|
|
* provided.
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
*
|
|
|
|
* @memberOf findPosts
|
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-06-15 11:37:53 -04:00
|
|
|
*/
|
2014-03-04 22:54:15 -05:00
|
|
|
send: function() {
|
2012-08-22 20:04:18 -04:00
|
|
|
var post = {
|
2014-03-04 22:54:15 -05:00
|
|
|
ps: $( '#find-posts-input' ).val(),
|
2012-11-20 13:50:15 -05:00
|
|
|
action: 'find_posts',
|
|
|
|
_ajax_nonce: $('#_ajax_nonce').val()
|
|
|
|
},
|
|
|
|
spinner = $( '.find-box-search .spinner' );
|
|
|
|
|
2015-04-03 00:52:27 -04:00
|
|
|
spinner.addClass( 'is-active' );
|
2012-08-22 20:04:18 -04:00
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Send a POST request to admin_ajax.php, hide the spinner and replace the list
|
|
|
|
* of posts with the response data. If an error occurs, display it.
|
2017-06-15 11:37:53 -04:00
|
|
|
*/
|
2014-03-04 22:54:15 -05:00
|
|
|
$.ajax( ajaxurl, {
|
|
|
|
type: 'POST',
|
|
|
|
data: post,
|
|
|
|
dataType: 'json'
|
|
|
|
}).always( function() {
|
2015-04-03 00:52:27 -04:00
|
|
|
spinner.removeClass( 'is-active' );
|
2014-03-04 22:54:15 -05:00
|
|
|
}).done( function( x ) {
|
|
|
|
if ( ! x.success ) {
|
2020-07-07 12:14:06 -04:00
|
|
|
$( '#find-posts-response' ).text( wp.i18n.__( 'An error has occurred. Please reload the page and try again.' ) );
|
2014-03-04 22:54:15 -05:00
|
|
|
}
|
2012-11-20 13:50:15 -05:00
|
|
|
|
2014-03-04 22:54:15 -05:00
|
|
|
$( '#find-posts-response' ).html( x.data );
|
|
|
|
}).fail( function() {
|
2020-07-07 12:14:06 -04:00
|
|
|
$( '#find-posts-response' ).text( wp.i18n.__( 'An error has occurred. Please reload the page and try again.' ) );
|
2012-11-20 13:50:15 -05:00
|
|
|
});
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Initializes the file once the DOM is fully loaded and attaches events to the
|
|
|
|
* various form elements.
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-06-15 11:37:53 -04:00
|
|
|
*/
|
2021-03-18 15:01:03 -04:00
|
|
|
$( function() {
|
2022-03-10 13:24:03 -05:00
|
|
|
var settings,
|
|
|
|
$mediaGridWrap = $( '#wp-media-grid' ),
|
|
|
|
copyAttachmentURLClipboard = new ClipboardJS( '.copy-attachment-url.media-library' ),
|
2024-10-06 22:49:17 -04:00
|
|
|
copyAttachmentURLSuccessTimeout,
|
|
|
|
previousSuccessElement = null;
|
2014-09-09 06:14:17 -04:00
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
// Opens a manage media frame into the grid.
|
2014-09-09 06:14:17 -04:00
|
|
|
if ( $mediaGridWrap.length && window.wp && window.wp.media ) {
|
2015-01-03 03:24:22 -05:00
|
|
|
settings = _wpMediaGridSettings;
|
|
|
|
|
2020-05-16 13:25:11 -04:00
|
|
|
var frame = window.wp.media({
|
2014-09-09 06:14:17 -04:00
|
|
|
frame: 'manage',
|
2015-01-03 03:24:22 -05:00
|
|
|
container: $mediaGridWrap,
|
|
|
|
library: settings.queryVars
|
2014-09-09 06:14:17 -04:00
|
|
|
}).open();
|
2020-05-16 13:25:11 -04:00
|
|
|
|
|
|
|
// Fire a global ready event.
|
|
|
|
$mediaGridWrap.trigger( 'wp-media-grid-ready', frame );
|
2014-09-09 06:14:17 -04:00
|
|
|
}
|
2014-06-05 18:01:18 -04:00
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
// Prevents form submission if no post has been selected.
|
2021-01-22 07:32:03 -05:00
|
|
|
$( '#find-posts-submit' ).on( 'click', function( event ) {
|
2014-03-04 22:54:15 -05:00
|
|
|
if ( ! $( '#find-posts-response input[type="radio"]:checked' ).length )
|
|
|
|
event.preventDefault();
|
2012-08-22 20:04:18 -04:00
|
|
|
});
|
2017-06-15 11:37:53 -04:00
|
|
|
|
|
|
|
// Submits the search query when hitting the enter key in the search input.
|
2021-01-22 07:32:03 -05:00
|
|
|
$( '#find-posts .find-box-search :input' ).on( 'keypress', function( event ) {
|
2012-08-22 20:04:18 -04:00
|
|
|
if ( 13 == event.which ) {
|
|
|
|
findPosts.send();
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-04 22:54:15 -05:00
|
|
|
});
|
2017-06-15 11:37:53 -04:00
|
|
|
|
|
|
|
// Binds the click event to the search button.
|
2021-01-22 07:32:03 -05:00
|
|
|
$( '#find-posts-search' ).on( 'click', findPosts.send );
|
2017-06-15 11:37:53 -04:00
|
|
|
|
|
|
|
// Binds the close dialog click event.
|
2021-01-22 07:32:03 -05:00
|
|
|
$( '#find-posts-close' ).on( 'click', findPosts.close );
|
2017-06-15 11:37:53 -04:00
|
|
|
|
|
|
|
// Binds the bulk action events to the submit buttons.
|
2021-01-22 07:32:03 -05:00
|
|
|
$( '#doaction' ).on( 'click', function( event ) {
|
2017-06-15 11:37:53 -04:00
|
|
|
|
|
|
|
/*
|
2021-01-07 11:23:07 -05:00
|
|
|
* Handle the bulk action based on its value.
|
2017-06-15 11:37:53 -04:00
|
|
|
*/
|
2021-01-07 11:23:07 -05:00
|
|
|
$( 'select[name="action"]' ).each( function() {
|
2016-06-26 10:26:29 -04:00
|
|
|
var optionValue = $( this ).val();
|
|
|
|
|
|
|
|
if ( 'attach' === optionValue ) {
|
2014-03-04 22:54:15 -05:00
|
|
|
event.preventDefault();
|
2012-08-22 20:04:18 -04:00
|
|
|
findPosts.open();
|
2016-06-26 10:26:29 -04:00
|
|
|
} else if ( 'delete' === optionValue ) {
|
|
|
|
if ( ! showNotice.warn() ) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2012-08-22 20:04:18 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-03-04 22:54:15 -05:00
|
|
|
|
2017-06-15 11:37:53 -04:00
|
|
|
/**
|
2017-12-18 09:14:53 -05:00
|
|
|
* Enables clicking on the entire table row.
|
2017-06-15 11:37:53 -04:00
|
|
|
*
|
2019-11-29 13:01:03 -05:00
|
|
|
* @return {void}
|
2017-06-15 11:37:53 -04:00
|
|
|
*/
|
2014-03-04 22:54:15 -05:00
|
|
|
$( '.find-box-inside' ).on( 'click', 'tr', function() {
|
|
|
|
$( this ).find( '.found-radio input' ).prop( 'checked', true );
|
2014-03-04 18:11:13 -05:00
|
|
|
});
|
2022-03-10 13:24:03 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles media list copy media URL button.
|
|
|
|
*
|
|
|
|
* @since 6.0.0
|
|
|
|
*
|
|
|
|
* @param {MouseEvent} event A click event.
|
|
|
|
* @return {void}
|
|
|
|
*/
|
|
|
|
copyAttachmentURLClipboard.on( 'success', function( event ) {
|
|
|
|
var triggerElement = $( event.trigger ),
|
|
|
|
successElement = $( '.success', triggerElement.closest( '.copy-to-clipboard-container' ) );
|
|
|
|
|
|
|
|
// Clear the selection and move focus back to the trigger.
|
|
|
|
event.clearSelection();
|
|
|
|
|
2024-10-06 22:49:17 -04:00
|
|
|
// Checking if the previousSuccessElement is present, adding the hidden class to it.
|
|
|
|
if ( previousSuccessElement ) {
|
|
|
|
previousSuccessElement.addClass( 'hidden' );
|
|
|
|
}
|
|
|
|
|
2022-03-10 13:24:03 -05:00
|
|
|
// Show success visual feedback.
|
|
|
|
clearTimeout( copyAttachmentURLSuccessTimeout );
|
|
|
|
successElement.removeClass( 'hidden' );
|
|
|
|
|
|
|
|
// Hide success visual feedback after 3 seconds since last success and unfocus the trigger.
|
|
|
|
copyAttachmentURLSuccessTimeout = setTimeout( function() {
|
|
|
|
successElement.addClass( 'hidden' );
|
2024-10-06 22:49:17 -04:00
|
|
|
// No need to store the previous success element further.
|
|
|
|
previousSuccessElement = null;
|
2022-03-10 13:24:03 -05:00
|
|
|
}, 3000 );
|
|
|
|
|
2024-10-06 22:49:17 -04:00
|
|
|
previousSuccessElement = successElement;
|
|
|
|
|
2022-03-10 13:24:03 -05:00
|
|
|
// Handle success audible feedback.
|
|
|
|
wp.a11y.speak( wp.i18n.__( 'The file URL has been copied to your clipboard' ) );
|
|
|
|
} );
|
2012-11-20 13:50:15 -05:00
|
|
|
});
|
2014-03-04 22:54:15 -05:00
|
|
|
})( jQuery );
|