author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1 |
/** |
9 | 2 |
* Creates a dialog containing posts that can have a particular media attached |
3 |
* to it. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5 |
* @since 2.7.0 |
9 | 6 |
* @output wp-admin/js/media.js |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* |
9 | 8 |
* @namespace findPosts |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
9 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
10 |
* @requires jQuery |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
*/ |
9 | 12 |
|
19 | 13 |
/* global ajaxurl, _wpMediaGridSettings, showNotice, findPosts, ClipboardJS */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
|
5 | 15 |
( function( $ ){ |
9 | 16 |
window.findPosts = { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
/** |
9 | 18 |
* Opens a dialog to attach media to a post. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
20 |
* Adds an overlay prior to retrieving a list of posts to attach the media to. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
24 |
* @memberOf findPosts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
* @param {string} af_name The name of the affected element. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
* @param {string} af_val The value of the affected post element. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
* |
16 | 29 |
* @return {boolean} Always returns false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
30 |
*/ |
5 | 31 |
open: function( af_name, af_val ) { |
32 |
var overlay = $( '.ui-find-overlay' ); |
|
0 | 33 |
|
5 | 34 |
if ( overlay.length === 0 ) { |
0 | 35 |
$( 'body' ).append( '<div class="ui-find-overlay"></div>' ); |
36 |
findPosts.overlay(); |
|
37 |
} |
|
38 |
||
39 |
overlay.show(); |
|
40 |
||
41 |
if ( af_name && af_val ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
// #affected is a hidden input field in the dialog that keeps track of which media should be attached. |
5 | 43 |
$( '#affected' ).attr( 'name', af_name ).val( af_val ); |
0 | 44 |
} |
5 | 45 |
|
46 |
$( '#find-posts' ).show(); |
|
0 | 47 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
// Close the dialog when the escape key is pressed. |
18 | 49 |
$('#find-posts-input').trigger( 'focus' ).on( 'keyup', function( event ){ |
5 | 50 |
if ( event.which == 27 ) { |
51 |
findPosts.close(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
} |
0 | 53 |
}); |
54 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
// Retrieves a list of applicable posts for media attachment and shows them. |
0 | 56 |
findPosts.send(); |
57 |
||
58 |
return false; |
|
59 |
}, |
|
60 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
/** |
9 | 62 |
* Clears the found posts lists before hiding the attach media dialog. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
63 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* @memberOf findPosts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
* |
16 | 68 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
*/ |
5 | 70 |
close: function() { |
71 |
$('#find-posts-response').empty(); |
|
72 |
$('#find-posts').hide(); |
|
0 | 73 |
$( '.ui-find-overlay' ).hide(); |
74 |
}, |
|
75 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
/** |
9 | 77 |
* Binds a click event listener to the overlay which closes the attach media |
78 |
* dialog. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
* @since 3.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
* @memberOf findPosts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
* |
16 | 84 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
*/ |
5 | 86 |
overlay: function() { |
87 |
$( '.ui-find-overlay' ).on( 'click', function () { |
|
0 | 88 |
findPosts.close(); |
89 |
}); |
|
90 |
}, |
|
91 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
/** |
9 | 93 |
* Retrieves and displays posts based on the search term. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
* |
9 | 95 |
* Sends a post request to the admin_ajax.php, requesting posts based on the |
96 |
* search term provided by the user. Defaults to all posts if no search term is |
|
97 |
* provided. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
98 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
* @since 2.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
100 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
101 |
* @memberOf findPosts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
102 |
* |
16 | 103 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
*/ |
5 | 105 |
send: function() { |
0 | 106 |
var post = { |
5 | 107 |
ps: $( '#find-posts-input' ).val(), |
0 | 108 |
action: 'find_posts', |
109 |
_ajax_nonce: $('#_ajax_nonce').val() |
|
110 |
}, |
|
111 |
spinner = $( '.find-box-search .spinner' ); |
|
112 |
||
5 | 113 |
spinner.addClass( 'is-active' ); |
0 | 114 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
/** |
9 | 116 |
* Send a POST request to admin_ajax.php, hide the spinner and replace the list |
117 |
* of posts with the response data. If an error occurs, display it. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
*/ |
5 | 119 |
$.ajax( ajaxurl, { |
120 |
type: 'POST', |
|
121 |
data: post, |
|
122 |
dataType: 'json' |
|
123 |
}).always( function() { |
|
124 |
spinner.removeClass( 'is-active' ); |
|
125 |
}).done( function( x ) { |
|
126 |
if ( ! x.success ) { |
|
16 | 127 |
$( '#find-posts-response' ).text( wp.i18n.__( 'An error has occurred. Please reload the page and try again.' ) ); |
5 | 128 |
} |
0 | 129 |
|
5 | 130 |
$( '#find-posts-response' ).html( x.data ); |
131 |
}).fail( function() { |
|
16 | 132 |
$( '#find-posts-response' ).text( wp.i18n.__( 'An error has occurred. Please reload the page and try again.' ) ); |
0 | 133 |
}); |
134 |
} |
|
135 |
}; |
|
136 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
/** |
9 | 138 |
* Initializes the file once the DOM is fully loaded and attaches events to the |
139 |
* various form elements. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
* |
16 | 141 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
*/ |
18 | 143 |
$( function() { |
19 | 144 |
var settings, |
145 |
$mediaGridWrap = $( '#wp-media-grid' ), |
|
146 |
copyAttachmentURLClipboard = new ClipboardJS( '.copy-attachment-url.media-library' ), |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
147 |
copyAttachmentURLSuccessTimeout, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
148 |
previousSuccessElement = null; |
5 | 149 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
150 |
// Opens a manage media frame into the grid. |
5 | 151 |
if ( $mediaGridWrap.length && window.wp && window.wp.media ) { |
152 |
settings = _wpMediaGridSettings; |
|
153 |
||
16 | 154 |
var frame = window.wp.media({ |
5 | 155 |
frame: 'manage', |
156 |
container: $mediaGridWrap, |
|
157 |
library: settings.queryVars |
|
158 |
}).open(); |
|
16 | 159 |
|
160 |
// Fire a global ready event. |
|
161 |
$mediaGridWrap.trigger( 'wp-media-grid-ready', frame ); |
|
5 | 162 |
} |
163 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
// Prevents form submission if no post has been selected. |
18 | 165 |
$( '#find-posts-submit' ).on( 'click', function( event ) { |
5 | 166 |
if ( ! $( '#find-posts-response input[type="radio"]:checked' ).length ) |
167 |
event.preventDefault(); |
|
0 | 168 |
}); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
169 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
// Submits the search query when hitting the enter key in the search input. |
18 | 171 |
$( '#find-posts .find-box-search :input' ).on( 'keypress', function( event ) { |
0 | 172 |
if ( 13 == event.which ) { |
173 |
findPosts.send(); |
|
174 |
return false; |
|
175 |
} |
|
5 | 176 |
}); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
178 |
// Binds the click event to the search button. |
18 | 179 |
$( '#find-posts-search' ).on( 'click', findPosts.send ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
180 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
181 |
// Binds the close dialog click event. |
18 | 182 |
$( '#find-posts-close' ).on( 'click', findPosts.close ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
184 |
// Binds the bulk action events to the submit buttons. |
18 | 185 |
$( '#doaction' ).on( 'click', function( event ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
186 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
187 |
/* |
18 | 188 |
* Handle the bulk action based on its value. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
*/ |
18 | 190 |
$( 'select[name="action"]' ).each( function() { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
191 |
var optionValue = $( this ).val(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
192 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
193 |
if ( 'attach' === optionValue ) { |
5 | 194 |
event.preventDefault(); |
0 | 195 |
findPosts.open(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
} else if ( 'delete' === optionValue ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
if ( ! showNotice.warn() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
198 |
event.preventDefault(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
199 |
} |
0 | 200 |
} |
201 |
}); |
|
202 |
}); |
|
5 | 203 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
204 |
/** |
9 | 205 |
* Enables clicking on the entire table row. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
* |
16 | 207 |
* @return {void} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
*/ |
5 | 209 |
$( '.find-box-inside' ).on( 'click', 'tr', function() { |
210 |
$( this ).find( '.found-radio input' ).prop( 'checked', true ); |
|
211 |
}); |
|
19 | 212 |
|
213 |
/** |
|
214 |
* Handles media list copy media URL button. |
|
215 |
* |
|
216 |
* @since 6.0.0 |
|
217 |
* |
|
218 |
* @param {MouseEvent} event A click event. |
|
219 |
* @return {void} |
|
220 |
*/ |
|
221 |
copyAttachmentURLClipboard.on( 'success', function( event ) { |
|
222 |
var triggerElement = $( event.trigger ), |
|
223 |
successElement = $( '.success', triggerElement.closest( '.copy-to-clipboard-container' ) ); |
|
224 |
||
225 |
// Clear the selection and move focus back to the trigger. |
|
226 |
event.clearSelection(); |
|
227 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
228 |
// Checking if the previousSuccessElement is present, adding the hidden class to it. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
229 |
if ( previousSuccessElement ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
230 |
previousSuccessElement.addClass( 'hidden' ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
231 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
232 |
|
19 | 233 |
// Show success visual feedback. |
234 |
clearTimeout( copyAttachmentURLSuccessTimeout ); |
|
235 |
successElement.removeClass( 'hidden' ); |
|
236 |
||
237 |
// Hide success visual feedback after 3 seconds since last success and unfocus the trigger. |
|
238 |
copyAttachmentURLSuccessTimeout = setTimeout( function() { |
|
239 |
successElement.addClass( 'hidden' ); |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
240 |
// No need to store the previous success element further. |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
241 |
previousSuccessElement = null; |
19 | 242 |
}, 3000 ); |
243 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
244 |
previousSuccessElement = successElement; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
245 |
|
19 | 246 |
// Handle success audible feedback. |
247 |
wp.a11y.speak( wp.i18n.__( 'The file URL has been copied to your clipboard' ) ); |
|
248 |
} ); |
|
0 | 249 |
}); |
5 | 250 |
})( jQuery ); |