class-media-library-organizer-taxonomies.php
10.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
<?php
/**
* Taxonomies class.
*
* @package Media_Library_Organizer
* @author WP Media Library
*/
/**
* Registers Media Taxonomies and provides helper functions for creating or updating
* Terms.
*
* @since 1.0.0
*/
class Media_Library_Organizer_Taxonomies {
/**
* Holds the base class object.
*
* @since 1.3.2
*
* @var object
*/
public $base;
/**
* Holds the array of Taxonomies that are registered
* using the media_library_organizer_taxonomies_register filter
*
* @since 1.3.2
*
* @var array
*/
public $taxonomies = array();
/**
* Constructor
*
* @since 1.3.2
*
* @param object $base Base Plugin Class.
*/
public function __construct( $base ) {
// Store base class.
$this->base = $base;
// Actions.
add_action( 'init', array( $this, 'register' ), 20 );
}
/**
* Registers Taxonomies
*
* @since 1.3.2
*/
public function register() {
// Get all Taxonomies to Register.
$taxonomies = $this->get_taxonomies_to_register();
// Bail if no Taxonomies.
if ( empty( $taxonomies ) ) {
return;
}
if ( ! count( $taxonomies ) ) {
return;
}
// Iterate through Taxonomies, registering them.
foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
// Skip if not enabled.
if ( ! $taxonomy['enabled'] ) {
continue;
}
// Define taxonomy arguments.
$args = array(
'labels' => array(
'name' => $taxonomy['plural_name'],
'singular_name' => $taxonomy['singular_name'],
/* translators: Taxonomy Label, Plural */
'search_items' => sprintf( __( 'Search %s', 'media-library-organizer' ), $taxonomy['plural_name'] ),
/* translators: Taxonomy Label, Plural */
'all_items' => sprintf( __( 'All %s', 'media-library-organizer' ), $taxonomy['plural_name'] ),
/* translators: Taxonomy Label, Singular */
'parent_item' => sprintf( __( 'Parent %s', 'media-library-organizer' ), $taxonomy['singular_name'] ),
/* translators: Taxonomy Label, Singular */
'parent_item_colon' => sprintf( __( 'Parent %s:', 'media-library-organizer' ), $taxonomy['singular_name'] ),
/* translators: Taxonomy Label, Singular */
'edit_item' => sprintf( __( 'Edit %s', 'media-library-organizer' ), $taxonomy['singular_name'] ),
/* translators: Taxonomy Label, Singular */
'update_item' => sprintf( __( 'Update %s', 'media-library-organizer' ), $taxonomy['singular_name'] ),
/* translators: Taxonomy Label, Singular */
'add_new_item' => sprintf( __( 'Add New %s', 'media-library-organizer' ), $taxonomy['singular_name'] ),
/* translators: Taxonomy Label, Singular */
'new_item_name' => sprintf( __( 'New %s', 'media-library-organizer' ), $taxonomy['singular_name'] ),
'menu_name' => $taxonomy['plural_name'],
),
'public' => false,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_rest' => true,
'show_tagcloud' => false,
'show_in_quick_edit' => true,
'show_admin_column' => true,
'hierarchical' => $taxonomy['hierarchical'],
'show_ui' => true,
// Force counts on Terms.
'update_count_callback' => '_update_generic_term_count',
);
/**
* Defines the parameters for registering the Media Categories Taxonomy
*
* @since 1.1.0
*
* @param array $args Arguments.
* @param string $taxonomy_name Programmatic Taxonomy Name.
* @return array Arguments
*/
$args = apply_filters( 'media_library_organizer_taxonomy_register_taxonomy', $args, $taxonomy_name );
// Register taxonomy.
$result = register_taxonomy( $taxonomy_name, array( 'attachment' ), $args );
// If an error occured, continue.
if ( is_wp_error( $result ) ) {
continue;
}
// Add this Taxonomy to the array of registered Taxonomies.
$this->taxonomies[ $taxonomy_name ] = $taxonomy;
}
}
/**
* Returns all Taxonomies to be registered by this Plugin
*
* They may or may not have been registered using register_taxonomy()
*
* @since 1.3.2
*
* @return array Taxonomies
*/
public function get_taxonomies_to_register() {
// Define the Categories Taxonomy.
$taxonomies = array(
'mlo-category' => array(
'plural_name' => __( 'Media Categories', 'media-library-organizer' ),
'singular_name' => __( 'Media Category', 'media-library-organizer' ),
'hierarchical' => true,
'enabled' => true,
'public' => 0,
),
);
/**
* Defines Taxonomies to Register against Attachments
*
* @since 1.3.2
*
* @param array $taxonomies Taxonomies to Register
*/
$taxonomies = apply_filters( 'media_library_organizer_taxonomies_register', $taxonomies );
// Ensure expected keys are set.
foreach ( $taxonomies as $taxonomy_name => $taxonomy ) {
if ( ! array_key_exists( 'public', $taxonomy ) ) {
$taxonomies[ $taxonomy_name ]['public'] = 0;
}
}
// Return.
return $taxonomies;
}
/**
* Returns all Taxonomies that were successfully registered by this Plugin
*
* @since 1.3.2
*
* @return array Taxonomies
*/
public function get_taxonomies() {
return $this->taxonomies;
}
/**
* Returns the Taxonomy object
*
* @since 1.0.5
*
* @param string $taxonomy_name Taxonomy Name.
* @return WP_Taxonomy Taxonomy
*/
public function get_taxonomy( $taxonomy_name ) {
return get_taxonomy( $taxonomy_name );
}
/**
* Returns a wp_terms_checklist(), replacing each input name's tax_input[taxonomy-name]
* with the supplied Field Name.
*
* @since 1.2.3
*
* @param int $post_id Post ID.
* @param array $args wp_terms_checklist() compatible arguments.
* @param string $field_name Field Name to use in place of tax_input[taxonomy-name].
* @return string wp_terms_checklist() markup
*/
public function get_terms_checklist( $post_id, $args, $field_name = false ) {
// Get checklist HTML.
$checklist = wp_terms_checklist( $post_id, $args );
// Replace field name.
if ( $field_name !== false ) {
$checklist = str_replace( 'name="tax_input[' . $args['taxonomy'] . ']', 'name="' . $field_name, $checklist );
}
// Return.
return $checklist;
}
/**
* Creates or Updates a Term for this Taxonomy
*
* @since 1.0.5
*
* @param string $taxonomy_name Taxonomy Name.
* @param string $term Term Name.
* @param int $parent Parent Term.
* @return mixed WP_Error | Term ID
*/
public function create_or_update_term( $taxonomy_name, $term, $parent = 0 ) {
// Check to see if the Term already exists.
$existing_term_id = term_exists( $term, $taxonomy_name, $parent );
if ( $existing_term_id ) {
$result = wp_update_term(
$existing_term_id['term_id'],
$taxonomy_name,
array(
'name' => $term,
'parent' => (int) $parent,
)
);
} else {
$result = wp_insert_term(
$term,
$taxonomy_name,
array(
'parent' => (int) $parent,
)
);
}
// Bail if an error occured.
if ( is_wp_error( $result ) ) {
return $result;
}
// Return Term ID.
return $result['term_id'];
}
/**
* Creates a Term for this Taxonomy
*
* @since 1.1.1
*
* @param string $taxonomy_name Taxonomy Name.
* @param string $name Term Name.
* @param int $parent Parent Term.
* @return mixed WP_Error | integer
*/
public function create_term( $taxonomy_name, $name, $parent = 0 ) {
$result = wp_insert_term(
$name,
$taxonomy_name,
array(
'parent' => (int) $parent,
)
);
// Bail if an error occured.
if ( is_wp_error( $result ) ) {
return $result;
}
// Return Term ID.
return absint( $result['term_id'] );
}
/**
* Updates a Term for this Taxonomy
*
* @since 1.1.1
*
* @param string $taxonomy_name Taxonomy Name.
* @param int $term_id Term ID.
* @param string $name Term Name.
* @param int $parent Parent Term.
* @return mixed WP_Error | integer
*/
public function update_term( $taxonomy_name, $term_id, $name, $parent = 0 ) {
// Build args.
$args = array(
'name' => $name,
);
if ( $parent > 0 ) {
$args['parent'] = $parent;
}
// Update.
$result = wp_update_term( $term_id, $taxonomy_name, $args );
// Bail if an error occured.
if ( is_wp_error( $result ) ) {
return $result;
}
// Return Term ID.
return absint( $result['term_id'] );
}
/**
* Deletes Term for this Taxonomy
*
* @since 1.1.1
*
* @param string $taxonomy_name Taxonomy Name.
* @param int $term_id Term ID.
* @return mixed WP_Error | bool
*/
public function delete_term( $taxonomy_name, $term_id ) {
return wp_delete_term( $term_id, $taxonomy_name );
}
/**
* Wrapper for wp_set_object_terms(), which also sets the metadata on any newly created terms
* to denote which Addon created them
*
* @since 1.1.0
*
* @param string $taxonomy_name Taxonomy Name.
* @param int $attachment_id Attachment ID.
* @param array $terms Terms.
* @param string $meta_key Meta Key to store against each created Term (false = don't store Meta).
* @param mixed $meta_value Meta Value to store against the Meta Key for each created Term.
* @return mixed WP_Error | array
*/
public function append_attachment_terms( $taxonomy_name, $attachment_id, $terms, $meta_key = false, $meta_value = 1 ) {
// Set attachment terms.
$result = wp_set_object_terms( $attachment_id, $terms, $taxonomy_name, true );
// Bail if an error occured.
if ( is_wp_error( $result ) ) {
return $result;
}
// Return result if we're not defining metadata.
if ( ! $meta_key ) {
return $result;
}
// Define meta key/value pair for each taxonomy term.
foreach ( $result as $taxonomy_term_id ) {
update_term_meta( $taxonomy_term_id, $meta_key, $meta_value );
}
// Return original result.
return $result;
}
}