2017-06-15 17:37:53 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Creates a dialog containing posts that can have a particular media attached
|
|
|
|
* to it.
|
2017-06-15 17:37:53 +02:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
2018-06-28 04:30:15 +02:00
|
|
|
* @output wp-admin/js/media.js
|
2017-06-15 17:37:53 +02:00
|
|
|
*
|
2017-12-15 14:42:46 +01:00
|
|
|
* @namespace findPosts
|
2017-06-15 17:37:53 +02:00
|
|
|
*
|
|
|
|
* @requires jQuery
|
|
|
|
*/
|
2018-06-28 04:30:15 +02:00
|
|
|
|
2018-08-19 15:33:24 +02:00
|
|
|
/* global ajaxurl, attachMediaBoxL10n, _wpMediaGridSettings, showNotice, findPosts */
|
2017-06-15 17:37:53 +02:00
|
|
|
|
2014-03-05 04:54:15 +01:00
|
|
|
( function( $ ){
|
2018-08-19 15:33:24 +02:00
|
|
|
window.findPosts = {
|
2017-06-15 17:37:53 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Opens a dialog to attach media to a post.
|
2017-06-15 17:37:53 +02: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.
|
|
|
|
*
|
|
|
|
* @returns {boolean} Always returns false.
|
|
|
|
*/
|
2014-03-05 04:54:15 +01:00
|
|
|
open: function( af_name, af_val ) {
|
|
|
|
var overlay = $( '.ui-find-overlay' );
|
2012-11-20 19:50:15 +01:00
|
|
|
|
2013-11-15 06:16:09 +01:00
|
|
|
if ( overlay.length === 0 ) {
|
2012-11-20 19:50:15 +01:00
|
|
|
$( 'body' ).append( '<div class="ui-find-overlay"></div>' );
|
|
|
|
findPosts.overlay();
|
|
|
|
}
|
|
|
|
|
|
|
|
overlay.show();
|
2012-08-23 02:04:18 +02:00
|
|
|
|
|
|
|
if ( af_name && af_val ) {
|
2017-06-15 17:37:53 +02:00
|
|
|
// #affected is a hidden input field in the dialog that keeps track of which media should be attached.
|
2014-03-05 04:54:15 +01:00
|
|
|
$( '#affected' ).attr( 'name', af_name ).val( af_val );
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
2014-03-05 00:11:13 +01:00
|
|
|
|
2014-03-05 04:54:15 +01:00
|
|
|
$( '#find-posts' ).show();
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
// Close the dialog when the escape key is pressed.
|
2014-03-05 04:54:15 +01:00
|
|
|
$('#find-posts-input').focus().keyup( function( event ){
|
|
|
|
if ( event.which == 27 ) {
|
|
|
|
findPosts.close();
|
2017-06-15 17:37:53 +02:00
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
// Retrieves a list of applicable posts for media attachment and shows them.
|
2012-11-20 19:50:15 +01:00
|
|
|
findPosts.send();
|
|
|
|
|
2012-08-23 02:04:18 +02:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Clears the found posts lists before hiding the attach media dialog.
|
2017-06-15 17:37:53 +02:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
*
|
|
|
|
* @memberOf findPosts
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2014-03-05 04:54:15 +01:00
|
|
|
close: function() {
|
2015-03-09 20:44:26 +01:00
|
|
|
$('#find-posts-response').empty();
|
2014-03-05 04:54:15 +01:00
|
|
|
$('#find-posts').hide();
|
2012-11-20 19:50:15 +01:00
|
|
|
$( '.ui-find-overlay' ).hide();
|
|
|
|
},
|
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Binds a click event listener to the overlay which closes the attach media
|
|
|
|
* dialog.
|
2017-06-15 17:37:53 +02:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
|
|
|
*
|
|
|
|
* @memberOf findPosts
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2014-03-05 04:54:15 +01:00
|
|
|
overlay: function() {
|
2014-03-05 00:11:13 +01:00
|
|
|
$( '.ui-find-overlay' ).on( 'click', function () {
|
2012-11-20 19:50:15 +01:00
|
|
|
findPosts.close();
|
|
|
|
});
|
2012-08-23 02:04:18 +02:00
|
|
|
},
|
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Retrieves and displays posts based on the search term.
|
2017-06-15 17:37:53 +02:00
|
|
|
*
|
2017-12-18 15:14:53 +01: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 17:37:53 +02:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
|
|
|
*
|
|
|
|
* @memberOf findPosts
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2014-03-05 04:54:15 +01:00
|
|
|
send: function() {
|
2012-08-23 02:04:18 +02:00
|
|
|
var post = {
|
2014-03-05 04:54:15 +01:00
|
|
|
ps: $( '#find-posts-input' ).val(),
|
2012-11-20 19:50:15 +01:00
|
|
|
action: 'find_posts',
|
|
|
|
_ajax_nonce: $('#_ajax_nonce').val()
|
|
|
|
},
|
|
|
|
spinner = $( '.find-box-search .spinner' );
|
|
|
|
|
2015-04-03 06:52:27 +02:00
|
|
|
spinner.addClass( 'is-active' );
|
2012-08-23 02:04:18 +02:00
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01: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 17:37:53 +02:00
|
|
|
*/
|
2014-03-05 04:54:15 +01:00
|
|
|
$.ajax( ajaxurl, {
|
|
|
|
type: 'POST',
|
|
|
|
data: post,
|
|
|
|
dataType: 'json'
|
|
|
|
}).always( function() {
|
2015-04-03 06:52:27 +02:00
|
|
|
spinner.removeClass( 'is-active' );
|
2014-03-05 04:54:15 +01:00
|
|
|
}).done( function( x ) {
|
|
|
|
if ( ! x.success ) {
|
|
|
|
$( '#find-posts-response' ).text( attachMediaBoxL10n.error );
|
|
|
|
}
|
2012-11-20 19:50:15 +01:00
|
|
|
|
2014-03-05 04:54:15 +01:00
|
|
|
$( '#find-posts-response' ).html( x.data );
|
|
|
|
}).fail( function() {
|
|
|
|
$( '#find-posts-response' ).text( attachMediaBoxL10n.error );
|
2012-11-20 19:50:15 +01:00
|
|
|
});
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Initializes the file once the DOM is fully loaded and attaches events to the
|
|
|
|
* various form elements.
|
2017-06-15 17:37:53 +02:00
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2014-03-05 04:54:15 +01:00
|
|
|
$( document ).ready( function() {
|
2015-03-09 22:17:29 +01:00
|
|
|
var settings, $mediaGridWrap = $( '#wp-media-grid' );
|
2014-09-09 12:14:17 +02:00
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
// Opens a manage media frame into the grid.
|
2014-09-09 12:14:17 +02:00
|
|
|
if ( $mediaGridWrap.length && window.wp && window.wp.media ) {
|
2015-01-03 09:24:22 +01:00
|
|
|
settings = _wpMediaGridSettings;
|
|
|
|
|
2014-09-09 12:14:17 +02:00
|
|
|
window.wp.media({
|
|
|
|
frame: 'manage',
|
2015-01-03 09:24:22 +01:00
|
|
|
container: $mediaGridWrap,
|
|
|
|
library: settings.queryVars
|
2014-09-09 12:14:17 +02:00
|
|
|
}).open();
|
|
|
|
}
|
2014-06-06 00:01:18 +02:00
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
// Prevents form submission if no post has been selected.
|
2014-03-05 04:54:15 +01:00
|
|
|
$( '#find-posts-submit' ).click( function( event ) {
|
|
|
|
if ( ! $( '#find-posts-response input[type="radio"]:checked' ).length )
|
|
|
|
event.preventDefault();
|
2012-08-23 02:04:18 +02:00
|
|
|
});
|
2017-06-15 17:37:53 +02:00
|
|
|
|
|
|
|
// Submits the search query when hitting the enter key in the search input.
|
2012-08-23 02:04:18 +02:00
|
|
|
$( '#find-posts .find-box-search :input' ).keypress( function( event ) {
|
|
|
|
if ( 13 == event.which ) {
|
|
|
|
findPosts.send();
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-05 04:54:15 +01:00
|
|
|
});
|
2017-06-15 17:37:53 +02:00
|
|
|
|
|
|
|
// Binds the click event to the search button.
|
2012-08-23 02:04:18 +02:00
|
|
|
$( '#find-posts-search' ).click( findPosts.send );
|
2017-06-15 17:37:53 +02:00
|
|
|
|
|
|
|
// Binds the close dialog click event.
|
2012-08-23 02:04:18 +02:00
|
|
|
$( '#find-posts-close' ).click( findPosts.close );
|
2017-06-15 17:37:53 +02:00
|
|
|
|
|
|
|
// Binds the bulk action events to the submit buttons.
|
2014-03-05 04:54:15 +01:00
|
|
|
$( '#doaction, #doaction2' ).click( function( event ) {
|
2017-06-15 17:37:53 +02: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-05 04:54:15 +01:00
|
|
|
$( 'select[name^="action"]' ).each( function() {
|
2016-06-26 16:26:29 +02:00
|
|
|
var optionValue = $( this ).val();
|
|
|
|
|
|
|
|
if ( 'attach' === optionValue ) {
|
2014-03-05 04:54:15 +01:00
|
|
|
event.preventDefault();
|
2012-08-23 02:04:18 +02:00
|
|
|
findPosts.open();
|
2016-06-26 16:26:29 +02:00
|
|
|
} else if ( 'delete' === optionValue ) {
|
|
|
|
if ( ! showNotice.warn() ) {
|
|
|
|
event.preventDefault();
|
|
|
|
}
|
2012-08-23 02:04:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2014-03-05 04:54:15 +01:00
|
|
|
|
2017-06-15 17:37:53 +02:00
|
|
|
/**
|
2017-12-18 15:14:53 +01:00
|
|
|
* Enables clicking on the entire table row.
|
2017-06-15 17:37:53 +02:00
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2014-03-05 04:54:15 +01:00
|
|
|
$( '.find-box-inside' ).on( 'click', 'tr', function() {
|
|
|
|
$( this ).find( '.found-radio input' ).prop( 'checked', true );
|
2014-03-05 00:11:13 +01:00
|
|
|
});
|
2012-11-20 19:50:15 +01:00
|
|
|
});
|
2014-03-05 04:54:15 +01:00
|
|
|
})( jQuery );
|