class-media-library-organizer-ajax.php
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<?php
/**
* AJAX class.
*
* @package Media_Library_Organizer
* @author WP Media Library
*/
/**
* Registers AJAX actions for Term management, Attachment editing and Search.
*
* @since 1.0.9
*/
class Media_Library_Organizer_AJAX {
/**
* Holds the base class object.
*
* @since 1.0.9
*
* @var object
*/
public $base;
/**
* Constructor
*
* @since 1.0.9
*
* @param object $base Base Plugin Class.
*/
public function __construct( $base ) {
// Store base class.
$this->base = $base;
add_action( 'wp_ajax_media_library_organizer_add_term', array( $this, 'add_term' ) );
add_action( 'wp_ajax_media_library_organizer_edit_term', array( $this, 'edit_term' ) );
add_action( 'wp_ajax_media_library_organizer_delete_term', array( $this, 'delete_term' ) );
add_action( 'wp_ajax_media_library_organizer_categorize_attachments', array( $this, 'categorize_attachments' ) );
add_action( 'wp_ajax_media_library_organizer_search_authors', array( $this, 'search_authors' ) );
add_action( 'wp_ajax_media_library_organizer_search_taxonomy_terms', array( $this, 'search_taxonomy_terms' ) );
add_action( 'wp_ajax_media_library_organizer_get_taxonomies_terms', array( $this, 'get_taxonomies_terms' ) );
add_action( 'wp_ajax_media_library_organizer_get_taxonomy_terms', array( $this, 'get_taxonomy_terms' ) );
}
/**
* Adds a Term
*
* @since 1.1.1
*/
public function add_term() {
// Check nonce.
check_ajax_referer( 'media_library_organizer_add_term', 'nonce' );
// Get vars.
$taxonomy_name = sanitize_text_field( $_REQUEST['taxonomy_name'] );
$term_name = sanitize_text_field( $_REQUEST['term_name'] );
$term_parent_id = sanitize_text_field( $_REQUEST['term_parent_id'] );
$term_id = $this->base->get_class( 'taxonomies' )->create_term( $taxonomy_name, $term_name, $term_parent_id );
// Bail if Term ID is a WP_Error.
if ( is_wp_error( $term_id ) ) {
wp_send_json_error( $term_id->get_error_message() );
}
// Get Taxonomy and Term.
$taxonomy = $this->base->get_class( 'taxonomies' )->get_taxonomy( $taxonomy_name );
$term = get_term_by( 'id', $term_id, $taxonomy_name );
// Return success with created Term, List View compatible dropdown filter and Grid View Edit Attachment checkbox reflecting changes.
wp_send_json_success(
array(
// The Created Term.
'term' => $term,
// The List View <select> dropdown filter, reflecting the changes i.e. the new Term.
'dropdown_filter' => $this->base->get_class( 'media' )->get_list_table_category_filter( $taxonomy_name, $taxonomy->label ),
// The Grid View Edit Attachment <li> checkbox, which can be injected into the Edit Attachment Backbone modal.
'checkbox' => $this->base->get_class( 'media' )->get_grid_edit_attachment_checkbox( $taxonomy_name, $term ),
// The Taxonomy.
'taxonomy' => $taxonomy,
// All Terms.
'terms' => $this->base->get_class( 'common' )->get_terms_hierarchical( $taxonomy_name ),
)
);
}
/**
* Edit a Term
*
* @since 1.1.1
*/
public function edit_term() {
// Check nonce.
check_ajax_referer( 'media_library_organizer_edit_term', 'nonce' );
// Get vars.
$taxonomy_name = sanitize_text_field( $_REQUEST['taxonomy_name'] );
$term_id = absint( $_REQUEST['term_id'] );
$term_name = sanitize_text_field( $_REQUEST['term_name'] );
// Get what will become the Old Term.
$old_term = get_term_by( 'id', $term_id, $taxonomy_name );
// Bail if the (Old) Term doesn't exist.
if ( ! $old_term ) {
wp_send_json_error( __( 'Category does not exist, so cannot be deleted', 'media-library-organizer' ) );
}
// Update Term.
$result = $this->base->get_class( 'taxonomies' )->update_term( $taxonomy_name, $term_id, $term_name );
if ( is_wp_error( $result ) ) {
wp_send_json_error( $result->get_error_message() );
}
// Get Taxonomy.
$taxonomy = $this->base->get_class( 'taxonomies' )->get_taxonomy( $taxonomy_name );
// Return success with old term, edited Term and List View compatible dropdown filter reflecting changes.
wp_send_json_success(
array(
// Old Term.
'old_term' => $old_term,
// New (Edited) Term.
'term' => get_term_by( 'id', $term_id, $taxonomy_name ),
// The List View <select> dropdown filter, reflecting the changes i.e. the edited Term.
'dropdown_filter' => $this->base->get_class( 'media' )->get_list_table_category_filter( $taxonomy_name, $taxonomy->label ),
// The Taxonomy.
'taxonomy' => $taxonomy,
// All Terms.
'terms' => $this->base->get_class( 'common' )->get_terms_hierarchical( $taxonomy_name ),
)
);
}
/**
* Delete a Term
*
* @since 1.1.1
*/
public function delete_term() {
// Check nonce.
check_ajax_referer( 'media_library_organizer_delete_term', 'nonce' );
// Get vars.
$taxonomy_name = sanitize_text_field( $_REQUEST['taxonomy_name'] );
$term_id = absint( $_REQUEST['term_id'] );
// Get Term.
$term = get_term_by( 'id', $term_id, $taxonomy_name );
// Bail if the Term doesn't exist.
if ( ! $term ) {
wp_send_json_error( __( 'Term does not exist, so cannot be deleted', 'media-library-organizer' ) );
}
// Delete Term.
$result = $this->base->get_class( 'taxonomies' )->delete_term( $taxonomy_name, $term_id );
if ( is_wp_error( $result ) ) {
wp_send_json_error( $result->get_error_message() );
}
// Get Taxonomy.
$taxonomy = $this->base->get_class( 'taxonomies' )->get_taxonomy( $taxonomy_name );
// Return success with deleted Term and List View compatible dropdown filter reflecting changes.
wp_send_json_success(
array(
// Deleted Term.
'term' => $term,
// The List View <select> dropdown filter, reflecting the changes i.e. the deleted Term.
'dropdown_filter' => $this->base->get_class( 'media' )->get_list_table_category_filter( $taxonomy_name, $taxonomy->label ),
// The Taxonomy.
'taxonomy' => $taxonomy,
// All Terms.
'terms' => $this->base->get_class( 'common' )->get_terms_hierarchical( $taxonomy_name ),
)
);
}
/**
* Categorizes the given Attachment IDs with the given Term ID
*
* @since 1.1.1
*/
public function categorize_attachments() {
// Check nonce.
check_ajax_referer( 'media_library_organizer_categorize_attachments', 'nonce' );
// Get vars.
$taxonomy_name = sanitize_text_field( $_REQUEST['taxonomy_name'] );
$term_id = (int) sanitize_text_field( $_REQUEST['term_id'] );
$attachment_ids = $_REQUEST['attachment_ids'];
$attachments = array();
foreach ( $attachment_ids as $attachment_id ) {
// Get attachment.
$attachment = new Media_Library_Organizer_Attachment( absint( $attachment_id ) );
// If the Term ID is -1, remove Terms.
// Otherwise append them.
if ( $term_id === -1 ) {
$attachment->remove_terms( $taxonomy_name );
} else {
$attachment->append_terms( $taxonomy_name, array( $term_id ) );
}
// Update the Attachment.
$result = $attachment->update();
// Bail if an error occured.
if ( is_wp_error( $result ) ) {
wp_send_json_error( $result->get_error_message() );
}
// Add to return data.
$attachments[] = array(
'id' => $attachment_id,
'terms' => wp_get_post_terms( $attachment_id, $taxonomy_name ),
);
// Destroy the class.
unset( $attachment );
}
// Get Taxonomy.
$taxonomy = $this->base->get_class( 'taxonomies' )->get_taxonomy( $taxonomy_name );
// Return the Attachment IDs and their Categories.
wp_send_json_success(
array(
// Attachments updated, with Terms.
'attachments' => $attachments,
// Term Assigned to Attachments.
'term' => get_term_by( 'id', $term_id, $taxonomy_name ),
// The List View <select> dropdown filter, reflecting the changes i.e. the edited Term.
'dropdown_filter' => $this->base->get_class( 'media' )->get_list_table_category_filter( $taxonomy_name, $taxonomy->label ),
// The Taxonomy.
'taxonomy' => $taxonomy,
// All Terms.
'terms' => $this->base->get_class( 'common' )->get_terms_hierarchical( $taxonomy_name ),
)
);
}
/**
* Searches for Authors for the given freeform text
*
* @since 1.0.9
*/
public function search_authors() {
// Check nonce.
check_ajax_referer( 'media_library_organizer_search_authors', 'nonce' );
// Get vars.
$query = sanitize_text_field( $_REQUEST['query'] );
// Get results.
$users = new WP_User_Query(
array(
'search' => '*' . $query . '*',
)
);
// If an error occured, bail.
if ( is_wp_error( $users ) ) {
return wp_send_json_error( $users->get_error_message() );
}
// Build array.
$users_array = array();
$results = $users->get_results();
if ( ! empty( $results ) ) {
foreach ( $results as $user ) {
$users_array[] = array(
'id' => $user->ID,
'user_login' => $user->user_login,
);
}
}
// Done.
wp_send_json_success( $users_array );
}
/**
* Searches Categories for the given freeform text
*
* @since 1.0.9
*/
public function search_taxonomy_terms() {
// Check nonce.
check_ajax_referer( 'media_library_organizer_search_taxonomy_terms', 'nonce' );
// Get vars.
$taxonomy_name = false;
if ( isset( $_REQUEST['taxonomy_name'] ) ) {
$taxonomy_name = sanitize_text_field( $_REQUEST['taxonomy_name'] );
} elseif ( isset( $_REQUEST['args'] ) && isset( $_REQUEST['args']['taxonomy_name'] ) ) {
$taxonomy_name = sanitize_text_field( $_REQUEST['args']['taxonomy_name'] );
}
$query = sanitize_text_field( $_REQUEST['query'] );
// Bail if no Taxonomy Name specified.
if ( ! $taxonomy_name ) {
return wp_send_json_error( __( 'The taxonomy_name or args[taxonomy_name] parameter must be included in the request.', 'media-library-organizer' ) );
}
// Get results.
$terms = new WP_Term_Query(
array(
'taxonomy' => $taxonomy_name,
'search' => $query,
'hide_empty' => false,
)
);
// If an error occured, bail.
if ( is_wp_error( $terms ) ) {
return wp_send_json_error( $terms->get_error_message() );
}
// Build array.
$terms_array = array();
if ( ! empty( $terms->terms ) ) {
foreach ( $terms->terms as $term ) {
$terms_array[] = array(
'id' => $term->term_id,
'term' => $term->name,
'slug' => $term->slug,
);
}
}
// Done.
wp_send_json_success( $terms_array );
}
/**
* Returns all Terms for all Taxonomies
*
* @since 1.3.3
*/
public function get_taxonomies_terms() {
// Check nonce.
check_ajax_referer( 'media_library_organizer_get_taxonomies_terms', 'nonce' );
// Iterate through Taxonomies.
$response = array();
foreach ( $this->base->get_class( 'taxonomies' )->get_taxonomies() as $taxonomy_name => $taxonomy ) {
$response[ $taxonomy_name ] = array(
'taxonomy' => $this->base->get_class( 'taxonomies' )->get_taxonomy( $taxonomy_name ),
'terms' => $this->base->get_class( 'common' )->get_terms_hierarchical( $taxonomy_name ),
);
}
// Return success with Taxonomies and Terms.
wp_send_json_success( $response );
}
/**
* Returns all Terms for the given Taxonomy
*
* @since 1.3.3
*/
public function get_taxonomy_terms() {
// Check nonce.
check_ajax_referer( 'media_library_organizer_get_taxonomy_terms', 'nonce' );
// Get vars.
$taxonomy_name = sanitize_text_field( $_REQUEST['taxonomy_name'] );
// Return success with Taxonomy and Terms.
wp_send_json_success(
array(
'taxonomy' => $this->base->get_class( 'taxonomies' )->get_taxonomy( $taxonomy_name ),
'terms' => $this->base->get_class( 'common' )->get_terms_hierarchical( $taxonomy_name ),
)
);
}
}