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
|
|
|
|
2018-08-19 09:33:24 -04:00
|
|
|
/* global ajaxurl, attachMediaBoxL10n, _wpMediaGridSettings, showNotice, findPosts */
|
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.
|
2014-03-04 22:54:15 -05:00
|
|
|
$('#find-posts-input').focus().keyup( function( event ){
|
|
|
|
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 ) {
|
|
|
|
$( '#find-posts-response' ).text( attachMediaBoxL10n.error );
|
|
|
|
}
|
2012-11-20 13:50:15 -05:00
|
|
|
|
2014-03-04 22:54:15 -05:00
|
|
|
$( '#find-posts-response' ).html( x.data );
|
|
|
|
}).fail( function() {
|
|
|
|
$( '#find-posts-response' ).text( attachMediaBoxL10n.error );
|
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
|
|
|
*/
|
2014-03-04 22:54:15 -05:00
|
|
|
$( document ).ready( function() {
|
2015-03-09 17:17:29 -04:00
|
|
|
var settings, $mediaGridWrap = $( '#wp-media-grid' );
|
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.
|
2014-03-04 22:54:15 -05:00
|
|
|
$( '#find-posts-submit' ).click( function( event ) {
|
|
|
|
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.
|
2012-08-22 20:04:18 -04:00
|
|
|
$( '#find-posts .find-box-search :input' ).keypress( function( event ) {
|
|
|
|
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.
|
2012-08-22 20:04:18 -04:00
|
|
|
$( '#find-posts-search' ).click( findPosts.send );
|
2017-06-15 11:37:53 -04:00
|
|
|
|
|
|
|
// Binds the close dialog click event.
|
2012-08-22 20:04:18 -04:00
|
|
|
$( '#find-posts-close' ).click( findPosts.close );
|
2017-06-15 11:37:53 -04:00
|
|
|
|
|
|
|
// Binds the bulk action events to the submit buttons.
|
2014-03-04 22:54:15 -05:00
|
|
|
$( '#doaction, #doaction2' ).click( function( event ) {
|
2017-06-15 11:37:53 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieves all select elements for bulk actions that have a name starting with `action`
|
|
|
|
* and handle its action based on its value.
|
|
|
|
*/
|
2014-03-04 22:54:15 -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
|
|
|
});
|
2012-11-20 13:50:15 -05:00
|
|
|
});
|
2014-03-04 22:54:15 -05:00
|
|
|
})( jQuery );
|