class-media-library-organizer-tree-view-media.php
14.1 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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
/**
* Tree View Media class.
*
* @package Media_Library_Organizer
* @author WP Media Library
*/
/**
* Outputs the Tree View in the Media Library Footer
*
* @version 1.1.1
*/
class Media_Library_Organizer_Tree_View_Media {
/**
* Holds the base class object.
*
* @since 1.1.1
*
* @var object
*/
public $base;
/**
* Constructor
*
* @since 1.1.1
*
* @param object $base Base Plugin Class.
*/
public function __construct( $base ) {
// Store base class.
$this->base = $base;
// Enqueue JS and CSS for Tree View.
add_action( 'media_library_organizer_admin_scripts_js_media', array( $this, 'enqueue_js' ), 10, 4 );
add_action( 'media_library_organizer_admin_scripts_css_media', array( $this, 'enqueue_css' ), 10, 0 );
// Output Move Column in List View.
add_filter( 'media_library_organizer_media_define_list_view_columns', array( $this, 'define_list_view_columns' ), 10, 1 );
add_filter( 'media_library_organizer_media_define_list_view_columns_output_tree-view-move', array( $this, 'define_list_view_columns_output_tree_view_move' ), 10, 1 );
// Output HTML in the Upload List and Grid Views.
add_action( 'media_library_organizer_media_media_library_footer', array( $this, 'media_library_footer' ) );
}
/**
* Enqueue JS for the WordPress Admin > Media screens
*
* @since 1.1.1
*
* @param WP_Screen $screen Current WordPress Screen.
* @param array $screens Plugin Registered Screens.
* @param string $mode View Mode (list|grid).
* @param string $ext If defined, loads minified JS.
*/
public function enqueue_js( $screen, $screens, $mode, $ext ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
// Bail if Tree View isn't enabled.
if ( ! Media_Library_Organizer()->get_class( 'settings' )->get_setting( 'tree-view', 'enabled' ) ) {
return;
}
// WP Zinc.
wp_enqueue_script( 'wpzinc-admin-notification' );
// jQuery UI.
wp_enqueue_script( 'jquery-ui-core' );
wp_enqueue_script( 'jquery-ui-draggable' );
wp_enqueue_script( 'jquery-ui-droppable' );
wp_enqueue_script( 'jquery-ui-menu' );
wp_enqueue_script( 'jquery-ui-widget' );
wp_enqueue_script( $this->base->plugin->name . '-jstree', $this->base->plugin->url . 'assets/js/' . ( $ext ? $ext . '/' : '' ) . 'jstree' . ( $ext ? '-' . $ext : '' ) . '.js', array( 'jquery' ), Media_Library_Organizer()->plugin->version, true );
wp_enqueue_script( $this->base->plugin->name . '-resize-sensor', $this->base->plugin->url . 'assets/js/' . ( $ext ? $ext . '/' : '' ) . 'resize-sensor' . ( $ext ? '-' . $ext : '' ) . '.js', false, Media_Library_Organizer()->plugin->version, true );
wp_enqueue_script( $this->base->plugin->name . '-sticky-sidebar', $this->base->plugin->url . 'assets/js/' . ( $ext ? $ext . '/' : '' ) . 'sticky-sidebar' . ( $ext ? '-' . $ext : '' ) . '.js', false, Media_Library_Organizer()->plugin->version, true );
wp_enqueue_script( $this->base->plugin->name . '-jquery-ui-contextmenu', $this->base->plugin->url . 'assets/js/' . ( $ext ? $ext . '/' : '' ) . 'jquery.ui-contextmenu' . ( $ext ? '-' . $ext : '' ) . '.js', array( 'jquery' ), Media_Library_Organizer()->plugin->version, true );
wp_enqueue_script( $this->base->plugin->name . '-media', $this->base->plugin->url . 'assets/js/' . ( $ext ? $ext . '/' : '' ) . 'media' . ( $ext ? '-' . $ext : '' ) . '.js', array( 'jquery' ), Media_Library_Organizer()->plugin->version, true );
// Get Tree View Taxonomy.
$taxonomy = $this->get_tree_view_taxonomy();
// Add Context Menu to Add, Edit and Delete Categories if the User's Role permits this.
$context_menu = false;
if ( current_user_can( 'manage_categories' ) ) {
$context_menu = array(
array(
'title' => __( 'Add Child', 'media-library-organizer' ),
'cmd' => 'create_term',
),
array(
'title' => __( 'Edit', 'media-library-organizer' ),
'cmd' => 'edit_term',
),
array(
'title' => __( 'Delete', 'media-library-organizer' ),
'cmd' => 'delete_term',
),
);
}
/**
* Defines the menu items for the Tree View's Context Menu, triggered when a user
* right clicks on a Category in the Tree View.
*
* @since 1.3.9
*
* @param mixed $context_menu Context Menu (false: none, array).
*/
$context_menu = apply_filters( 'media_library_organizer_tree_view_media_context_menu', $context_menu );
// Define the AJAX actions supported by Tree View.
$actions = array(
'create_term' => array(
'action' => 'media_library_organizer_add_term',
'nonce' => wp_create_nonce( 'media_library_organizer_add_term' ),
'prompt' => sprintf(
/* translators: Taxonomy Name, Singular */
__( 'Enter a %s Name', 'media-library-organizer' ),
$taxonomy->labels->singular_name
),
),
'edit_term' => array(
'action' => 'media_library_organizer_edit_term',
'nonce' => wp_create_nonce( 'media_library_organizer_edit_term' ),
'prompt' => sprintf(
/* translators: Taxonomy Name, Singular */
__( 'Edit %s Name', 'media-library-organizer' ),
$taxonomy->labels->singular_name
),
'no_selection' => sprintf(
/* translators: Taxonomy Name, Singular */
__( 'You must select a %s first', 'media-library-organizer' ),
$taxonomy->labels->singular_name
),
),
'delete_term' => array(
'action' => 'media_library_organizer_delete_term',
'nonce' => wp_create_nonce( 'media_library_organizer_delete_term' ),
'prompt' => sprintf(
/* translators: Taxonomy Name, Singular */
__( 'Delete %s?', 'media-library-organizer' ),
$taxonomy->labels->singular_name
),
'no_selection' => sprintf(
/* translators: Taxonomy Name, Singular */
__( 'You must select a %s first', 'media-library-organizer' ),
$taxonomy->labels->singular_name
),
),
'categorize_attachments' => array(
'action' => 'media_library_organizer_categorize_attachments',
'nonce' => wp_create_nonce( 'media_library_organizer_categorize_attachments' ),
),
'get_tree_view' => array(
'action' => 'media_library_organizer_tree_view_get_tree_view',
'nonce' => wp_create_nonce( 'media_library_organizer_tree_view_get_tree_view' ),
),
);
/**
* Defines the AJAX actions supported by the Tree View. Any context menu items should have
* a corresponding action defined here.
*
* @since 1.3.9
*
* @param array $actions Actions.
*/
$actions = apply_filters( 'media_library_organizer_tree_view_media_actions', $actions );
// Define Media Settings.
$media_settings = array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'actions' => $actions,
'context_menu' => $context_menu,
'taxonomy' => $taxonomy,
'selected_term' => Media_Library_Organizer()->get_class( 'media' )->get_selected_terms_slugs( $taxonomy->name ),
'selected_term_id' => Media_Library_Organizer()->get_class( 'media' )->get_selected_terms_ids( $taxonomy->name ),
'media_view' => Media_Library_Organizer()->get_class( 'common' )->get_media_view(),
'jstree' => Media_Library_Organizer()->get_class( 'settings' )->get_setting( 'tree-view', 'expand_collapse' ),
'labels' => array(
/* translators: Number of attachments */
'categorized_attachments' => __( 'Categorized %s items', 'media-library-organizer' ),
/* translators: Number of attachments */
'categorize_attachments' => __( 'Categorize %s items', 'media-library-organizer' ),
'categorize_attachment' => __( 'Categorize 1 item', 'media-library-organizer' ),
),
);
// Localize Media script.
wp_localize_script( $this->base->plugin->name . '-media', 'media_library_organizer_tree_view', $media_settings );
}
/**
* Enqueue JS for the WordPress Admin > Media screens
*
* @since 1.1.1
*/
public function enqueue_css() {
// Bail if Tree View isn't enabled.
if ( ! Media_Library_Organizer()->get_class( 'settings' )->get_setting( 'tree-view', 'enabled' ) ) {
return;
}
// CSS.
wp_enqueue_style( $this->base->plugin->name . '-media', $this->base->plugin->url . '/assets/css/media.css', false, Media_Library_Organizer()->plugin->version );
}
/**
* Defines the Columns to display in the List View WP_List_Table
*
* @since 1.1.4
*
* @param array $columns Columns.
* @return array Columns
*/
public function define_list_view_columns( $columns ) {
// Bail if Tree View isn't enabled.
if ( ! Media_Library_Organizer()->get_class( 'settings' )->get_setting( 'tree-view', 'enabled' ) ) {
return $columns;
}
// Inject Move Column after the checkbox.
return Media_Library_Organizer()->get_class( 'common' )->array_insert_after(
$columns,
'cb',
array(
'tree-view-move' => '<span class="dashicons dashicons-move"></span>',
)
);
}
/**
* Defines the data to display in the List View WP_List_Table Move Column
*
* @since 1.1.4
*
* @param string $output Output.
* @return string Output
*/
public function define_list_view_columns_output_tree_view_move( $output ) {
// Bail if Tree View isn't enabled.
if ( ! Media_Library_Organizer()->get_class( 'settings' )->get_setting( 'tree-view', 'enabled' ) ) {
return $output;
}
return '<span class="dashicons dashicons-move"></span>';
}
/**
* Outputs the Tree View markup on the Media Library screens
*
* @since 1.1.1
*/
public function media_library_footer() {
// Bail if Tree View isn't enabled.
if ( ! Media_Library_Organizer()->get_class( 'settings' )->get_setting( 'tree-view', 'enabled' ) ) {
return;
}
// Get Taxonomy.
$taxonomy = $this->get_tree_view_taxonomy();
// Get Tree View.
$output = $this->get_tree_view( $taxonomy->name, Media_Library_Organizer()->get_class( 'media' )->get_selected_terms_ids( $taxonomy->name ) );
// Check is JSTree enabled.
$jstree_enabled = Media_Library_Organizer()->get_class( 'settings' )->get_setting( 'tree-view', 'jstree_enabled' );
// Output.
require_once $this->base->plugin->folder . '/views/admin/media.php';
// Output Notification.
require_once Media_Library_Organizer()->plugin->folder . '/_modules/dashboard/views/notification.php';
}
/**
* Returns the Taxonomy object to be used in the Tree View.
*
* @since 1.3.2
*
* @return WP_Taxonomy Taxonomy
*/
public function get_tree_view_taxonomy() {
/**
* Defines the Taxonomy to display Terms for in the Tree View.
*
* @since 1.3.2
*
* @param string $taxonomy_name Taxonomy Name.
*/
$taxonomy_name = apply_filters( 'media_library_organizer_tree_view_media_get_tree_view_taxonomy', 'mlo-category' );
// Return taxonomy object.
return Media_Library_Organizer()->get_class( 'taxonomies' )->get_taxonomy( $taxonomy_name );
}
/**
* Gets the Tree View markup.
*
* @since 1.1.1
*
* @param string $taxonomy_name Taxonomy Name.
* @param array $selected_term_ids Selected Term ID(s) (false | int | array of integers).
*/
public function get_tree_view( $taxonomy_name, $selected_term_ids = false ) {
// Define walker class to use for this Tree View.
$walker = new Media_Library_Organizer_Tree_View_Taxonomy_Walker();
// Build args.
$args = array(
'echo' => 0,
'hide_empty' => 0,
'show_count' => 1,
'taxonomy' => $taxonomy_name,
'title_li' => 0,
'walker' => $walker,
);
// If logged in as an Administrator, prevent PublishPress Permissions from attempting to filter Term counts,
// otherwise they will display as zero for Administrators (other User Roles are unaffected).
if ( is_user_logged_in() && wp_get_current_user()->roles[0] === 'administrator' ) {
$args['pp_no_filter'] = true;
}
// If a current Term ID is specified, add it to the arguments now.
if ( $selected_term_ids !== false ) {
// Cast integers.
if ( is_array( $selected_term_ids ) ) {
foreach ( $selected_term_ids as $index => $selected_term_id ) {
$selected_term_ids[ $index ] = absint( $selected_term_id );
}
} else {
$selected_term_ids = absint( $selected_term_ids );
}
$args['current_category'] = $selected_term_ids;
}
// Output.
$output = '<ul>
<li class="cat-item-all">
<a href="' . $this->get_all_terms_link() . '">' . __( 'All', 'media-library-organizer' ) . '</a>
</li>
<li class="cat-item-unassigned">
<a href="' . $this->get_unassigned_term_link( $taxonomy_name ) . '">' . __( '(Unassigned)', 'media-library-organizer' ) . '</a>
</li>' .
wp_list_categories( $args ) . '
</ul>';
// Return.
return $output;
}
/**
* Returns the All Categories contextual link, depending on the screen
* we're on.
*
* @since 1.1.1
*
* @return string URL
*/
public function get_all_terms_link() {
return add_query_arg( $this->get_filters(), 'upload.php' );
}
/**
* Returns the Uncategorized contextual link, depending on the screen
* we're on.
*
* @since 1.1.1
*
* @param string $taxonomy_name Taxonomy Name.
* @return string URL
*/
public function get_unassigned_term_link( $taxonomy_name ) {
return add_query_arg(
array_merge(
$this->get_filters(),
array(
$taxonomy_name => -1,
)
),
'upload.php'
);
}
/**
* Returns an array of filters that the user might have applied to the Media Library view
*
* @since 1.1.1
*
* @return array Filters
*/
private function get_filters() {
$args = array(
'mode' => Media_Library_Organizer()->get_class( 'common' )->get_media_view(),
);
$conditions = array(
'attachment-filter',
'm',
'orderby',
'order',
'paged',
);
foreach ( $conditions as $condition ) {
if ( ! isset( $_REQUEST[ $condition ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
continue;
}
if ( empty( $_REQUEST[ $condition ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
continue;
}
$args[ $condition ] = sanitize_text_field( $_REQUEST[ $condition ] ); // phpcs:ignore WordPress.Security.NonceVerification
}
return $args;
}
}