├── .gitignore
├── admin-settings.php
├── admin.php
├── assets
├── images
│ ├── add.png
│ ├── attention.png
│ ├── close_advanced.png
│ ├── inactive.png
│ ├── information.png
│ └── working.png
├── js
│ ├── admin-edit.js
│ ├── admin-options-validate.js
│ ├── admin-options.js
│ ├── admin-post.js
│ └── jquery.validate.min.js
└── scss
│ ├── admin-options.scss
│ └── admin-post.scss
├── composer.json
├── composer.lock
├── cpt-onomies.php
├── cpt-onomy.php
├── extend
└── gravity-forms-custom-post-types.php
├── gulpfile.js
├── languages
└── cpt-onomies.pot
├── manager.php
├── package.json
├── readme.txt
├── uninstall.php
└── widgets.php
/.gitignore:
--------------------------------------------------------------------------------
1 | assets/css
2 | assets/js/*.min.js
3 | vendor
4 |
--------------------------------------------------------------------------------
/admin.php:
--------------------------------------------------------------------------------
1 | user_has_term_capabilities()
84 | * removes the ability access this page and throws up a 'Cheatin' uh?' message
85 | * but this function replaces that message with some helpful text on where to go
86 | * to edit the terms.
87 | *
88 | * @since 1.0
89 | * @uses $cpt_onomies_manager, $pagenow
90 | */
91 | public function deny_edit_tags() {
92 | global $cpt_onomies_manager, $pagenow;
93 |
94 | // Only on the edit tags page.
95 | if ( 'edit-tags.php' == $pagenow ) {
96 |
97 | // Make sure we have a taxonomy.
98 | if ( ! empty( $_REQUEST['taxonomy'] )
99 | && $cpt_onomies_manager->is_registered_cpt_onomy( $_REQUEST['taxonomy'] ) ) {
100 |
101 | // Get the taxonomy and post type info.
102 | $taxonomy = $_REQUEST['taxonomy'];
103 | $tax = get_taxonomy( $taxonomy );
104 | $custom_post_type = get_post_type_object( $taxonomy );
105 |
106 | /*
107 | * If the user is capable of editing the post to begin with.
108 | *
109 | * Otherwise, don't get their hopes up.
110 | */
111 | if ( current_user_can( $custom_post_type->cap->edit_posts ) ) {
112 | wp_die( sprintf( __( 'Since "%1$s" is a registered %2$s, you manage it\'s "terms" by managing the posts created under the custom post type "%3$s". So go ahead... %4$smanage the posts%5$s.', 'cpt-onomies' ), $tax->labels->name, 'CPT-onomy', $tax->labels->name, '', '' ) );
113 | } else {
114 | wp_die( sprintf( __( 'Since "%1$s" is a registered %2$s, you manage it\'s "terms" by managing the posts created under the custom post type "%3$s". Unfortunately, you don\'t have permission to edit these posts. Sorry. If this is a mistake, contact your administrator.', 'cpt-onomies' ), $tax->labels->name, 'CPT-onomy', $tax->labels->name ) . ' ' . __( 'Go to the dashboard', 'cpt-onomies' ) . '' );
115 | }
116 | }
117 | }
118 | }
119 |
120 | /**
121 | * Bringing all of the styles and scripts into one function helps to
122 | * keep everything more organized and allows for easier sharing.
123 | *
124 | * @since 1.1
125 | * @uses $current_screen
126 | * @param string $page - the current page
127 | */
128 | public function admin_register_styles_and_scripts( $page ) {
129 | global $current_screen;
130 |
131 | // Several pages in the admin need this script.
132 | wp_register_script( 'jquery-form-validation', plugins_url( 'assets/js/jquery.validate.min.js', __FILE__ ), array( 'jquery' ), null, true );
133 |
134 | // Enqueue scripts depending on page.
135 | switch ( $page ) {
136 |
137 | case 'edit.php':
138 | wp_enqueue_script( 'custom-post-type-onomies-admin-edit', plugins_url( 'assets/js/admin-edit.min.js', __FILE__ ), array( 'jquery', 'inline-edit-post' ), null, true );
139 | break;
140 |
141 | case 'post.php':
142 | case 'post-new.php':
143 | wp_enqueue_style( 'custom-post-type-onomies-admin-post', plugins_url( 'assets/css/admin-post.min.css', __FILE__ ), false, null );
144 | wp_enqueue_script( 'custom-post-type-onomies-admin-post', plugins_url( 'assets/js/admin-post.min.js', __FILE__ ), array( 'jquery', 'post', 'jquery-ui-autocomplete' ), null, true );
145 |
146 | // Our localized info.
147 | $cpt_onomies_admin_post_data = array();
148 | $cpt_onomies_admin_post_translation = array(
149 | 'term_does_not_exist' => sprintf( __( 'The term you are trying to add does not exist. %s terms, a.k.a posts, must already exist to be available for selection.', 'cpt-onomies' ), 'CPT-onomy' ),
150 | 'add_a_term' => __( 'Add a term', 'cpt-onomies' ),
151 | 'add_the_term' => __( 'Add the term', 'cpt-onomies' ),
152 | 'no_self_relationship' => __( 'Kind of silly to create a relationship between a post and itself, eh?', 'cpt-onomies' ),
153 | 'relationship_already_exists' => __( 'This relationship already exists.', 'cpt-onomies' ),
154 | 'close' => __( 'Close', 'cpt-onomies' ),
155 | );
156 |
157 | /*
158 | * We need to know if the user has permission to edit specific
159 | * taxonomies AND we'll get the label name while we're at it.
160 | */
161 | foreach ( get_object_taxonomies( $current_screen->post_type, 'objects' ) as $taxonomy => $tax ) {
162 |
163 | // Get the permission.
164 | $cpt_onomies_admin_post_data['can_assign_terms'][ $taxonomy ] = current_user_can( $tax->cap->assign_terms );
165 |
166 | // Get the label name.
167 | $cpt_onomies_admin_post_translation['no_terms_selected'][ $taxonomy ] = sprintf( __( 'There are no %s selected.', 'cpt-onomies' ), strtolower( $tax->labels->name ) );
168 |
169 | }
170 |
171 | // Add our info to the scripts.
172 | wp_localize_script( 'custom-post-type-onomies-admin-post', 'cpt_onomies_admin_post_data', $cpt_onomies_admin_post_data );
173 | wp_localize_script( 'custom-post-type-onomies-admin-post', 'cpt_onomies_admin_post_L10n', $cpt_onomies_admin_post_translation );
174 |
175 | break;
176 |
177 | }
178 | }
179 |
180 | /**
181 | * Allows ajax to invoke the get_cpt_onomy_terms_include_term_ids() function.
182 | *
183 | * Prints an array of term ids.
184 | *
185 | * @since 1.3
186 | */
187 | public function ajax_get_cpt_onomy_terms_include_term_ids() {
188 |
189 | // Get the taxonomy and post type info.
190 | $taxonomy = ( isset( $_POST['custom_post_type_onomies_taxonomy'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomy'] ) ) ? $_POST['custom_post_type_onomies_taxonomy'] : array();
191 | $post_type = ( isset( $_POST['custom_post_type_onomies_post_type'] ) && ! empty( $_POST['custom_post_type_onomies_post_type'] ) ) ? $_POST['custom_post_type_onomies_post_type'] : null;
192 | $post_id = ( isset( $_POST['custom_post_type_onomies_post_id'] ) && ! empty( $_POST['custom_post_type_onomies_post_id'] ) ) ? $_POST['custom_post_type_onomies_post_id'] : 0;
193 | $include_term_ids = array();
194 |
195 | // If we have a taxonomy...
196 | if ( ! empty( $taxonomy ) ) {
197 |
198 | // Get the include IDs.
199 | $taxonomy_include_term_ids = $this->get_cpt_onomy_terms_include_term_ids( $taxonomy, $post_type, $post_id );
200 | if ( ! empty( $taxonomy_include_term_ids ) ) {
201 | $include_term_ids = array_merge( $include_term_ids, $taxonomy_include_term_ids );
202 | }
203 | }
204 |
205 | // Print terms.
206 | echo json_encode( $include_term_ids );
207 |
208 | die();
209 | }
210 |
211 | /**
212 | * The 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids' filter
213 | * allows you to designate that you only want to include/print specific terms, and therefore
214 | * only want those specific terms to be able to be assigned, in the admin by returning their
215 | * term IDs. This function invokes that filter when needed, cleans up the data, stores the
216 | * data in a global class variable and returns the data.
217 | *
218 | * The data returned to the filter can be an array, space-separated or comma separated string.
219 | * The filter passes three parameters: the $taxonomy, the $post_type and the $post_id.
220 | *
221 | * @since 1.3
222 | * @param string $taxonomy - the name of the CPT-onomy
223 | * @param string $post_type - the name of the post type the CPT-onomy is being assigned to
224 | * @param int $post_id - the ID for the post the CPT-onomy is being assigned to
225 | * @return array - the ids for the included cpt_onomy terms
226 | * @filters 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids' - $taxonomy, $post_type, $post_id
227 | */
228 | public function get_cpt_onomy_terms_include_term_ids( $taxonomy = null, $post_type = null, $post_id = 0 ) {
229 |
230 | $include_term_ids = apply_filters( 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids', array(), $taxonomy, $post_type, $post_id );
231 |
232 | // Make sure its an array.
233 | if ( ! is_array( $include_term_ids ) ) {
234 | $include_term_ids = str_replace( ' ', ',', str_replace( ', ', ',', $include_term_ids ) );
235 | $include_term_ids = explode( ',', $include_term_ids );
236 | }
237 |
238 | // Make sure the 'include' does not include the current post ID.
239 | if ( in_array( $post_id, $include_term_ids ) ) {
240 | foreach ( $include_term_ids as $term_id_index => $term_id ) {
241 | if ( $post_id == $term_id ) {
242 | unset( $include_term_ids[ $term_id_index ] );
243 | }
244 | }
245 | }
246 |
247 | // Store and return the include term data.
248 | return $this->assigning_terms_include_term_ids[ $taxonomy ][ $post_type ][ $post_id ] = array_unique( $include_term_ids );
249 | }
250 |
251 | /**
252 | * Allows ajax to invoke the get_cpt_onomy_terms_exclude_term_ids() function.
253 | *
254 | * Prints an array of term ids.
255 | *
256 | * @since 1.2.1
257 | */
258 | public function ajax_get_cpt_onomy_terms_exclude_term_ids() {
259 |
260 | // Get taxonomy and post type info
261 | $taxonomies = ( isset( $_POST['custom_post_type_onomies_taxonomies'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomies'] ) ) ? $_POST['custom_post_type_onomies_taxonomies'] : array();
262 | $post_type = ( isset( $_POST['custom_post_type_onomies_post_type'] ) && ! empty( $_POST['custom_post_type_onomies_post_type'] ) ) ? $_POST['custom_post_type_onomies_post_type'] : null;
263 | $post_id = ( isset( $_POST['custom_post_type_onomies_post_id'] ) && ! empty( $_POST['custom_post_type_onomies_post_id'] ) ) ? $_POST['custom_post_type_onomies_post_id'] : 0;
264 | $exclude_term_ids = array();
265 |
266 | foreach ( $taxonomies as $taxonomy ) {
267 | $taxonomy_exclude_term_ids = $this->get_cpt_onomy_terms_exclude_term_ids( $taxonomy, $post_type, $post_id );
268 | if ( ! empty( $taxonomy_exclude_term_ids ) ) {
269 | $exclude_term_ids = array_merge( $exclude_term_ids, $taxonomy_exclude_term_ids );
270 | }
271 | }
272 |
273 | // Print terms
274 | echo json_encode( $exclude_term_ids );
275 |
276 | die();
277 | }
278 |
279 | /**
280 | * The 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids' filter
281 | * allows you to exclude specific terms from being printed, and therefore assigned,
282 | * in the admin by returning their term IDs. This function invokes that filter when
283 | * needed, cleans up the data, stores the data in a global class variable and returns the data.
284 | *
285 | * The data returned to the filter can be an array, space-separated or comma separated string.
286 | * The filter passes three parameters: the $taxonomy, the $post_type and the $post_id.
287 | *
288 | * @since 1.2.1
289 | * @param string $taxonomy - the name of the CPT-onomy
290 | * @param string $post_type - the name of the post type the CPT-onomy is being assigned to
291 | * @param int $post_id - the ID for the post the CPT-onomy is being assigned to
292 | * @return array - the ids for the excluded cpt_onomy terms
293 | * @filters 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids' - $taxonomy, $post_type, $post_id
294 | */
295 | public function get_cpt_onomy_terms_exclude_term_ids( $taxonomy = null, $post_type = null, $post_id = 0 ) {
296 |
297 | $exclude_term_ids = apply_filters( 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids', array(), $taxonomy, $post_type, $post_id );
298 |
299 | // Make sure its an array.
300 | if ( ! is_array( $exclude_term_ids ) ) {
301 | $exclude_term_ids = str_replace( ' ', ',', str_replace( ', ', ',', $exclude_term_ids ) );
302 | $exclude_term_ids = explode( ',', $exclude_term_ids );
303 | }
304 |
305 | // Make sure the 'excludes' includes the current post ID.
306 | if ( ! in_array( $post_id, $exclude_term_ids ) ) {
307 | $exclude_term_ids[] = $post_id;
308 | }
309 |
310 | // Store and return the excluded term data.
311 | return $this->assigning_terms_exclude_term_ids[ $taxonomy ][ $post_type ][ $post_id ] = array_unique( $exclude_term_ids );
312 | }
313 |
314 | /**
315 | * Used in collaboration with the CPT-onomy autocomplete term selection,
316 | * if the CPT-onomy is hierarchical, in order to display a term's parents
317 | * in the autocomplete dropdown and in the selected terms' checklist.
318 | *
319 | * @since 1.1
320 | * @param int $term_parent - the term's parent term id
321 | * @return string the complete parent title
322 | */
323 | public function build_term_parent_title_with_csv( $term_parent ) {
324 |
325 | if ( $term_parent > 0 ) {
326 |
327 | $post_parent_id = $term_parent;
328 | $term_parent = '';
329 |
330 | do {
331 |
332 | $post_parent = get_post( $post_parent_id );
333 | $post_parent_id = $post_parent->post_parent;
334 |
335 | if ( $term_parent ) {
336 | $term_parent .= ',';
337 | }
338 |
339 | $term_parent .= $post_parent->post_title;
340 |
341 | } while ( $post_parent_id > 0 );
342 |
343 | return $term_parent;
344 | } else {
345 | return null;
346 | }
347 | }
348 |
349 | /**
350 | * Returns an object's term info to an AJAX call.
351 | *
352 | * Allows you to designate if you want the parent titles
353 | * instead of term id. This comes in handy with the autcomplete
354 | * term selection for hierarchical CPT-onomies so we can show
355 | * the parent title along with the term name.
356 | *
357 | * This function replaced populate_bulk_quick_edit().
358 | *
359 | * @since 1.1
360 | */
361 | public function ajax_get_wp_object_terms() {
362 |
363 | $post_ids = ( isset( $_POST['custom_post_type_onomies_post_ids'] ) && ! empty( $_POST['custom_post_type_onomies_post_ids'] ) ) ? $_POST['custom_post_type_onomies_post_ids'] : array();
364 | $taxonomies = ( isset( $_POST['custom_post_type_onomies_taxonomies'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomies'] ) ) ? $_POST['custom_post_type_onomies_taxonomies'] : array();
365 | $get_parent_title = ( isset( $_POST['custom_post_type_onomies_get_parent_title'] ) && ! empty( $_POST['custom_post_type_onomies_get_parent_title'] ) ) ? true : false;
366 | $terms_fields = ( isset( $_POST['custom_post_type_onomies_wp_get_object_terms_fields'] ) && ! empty( $_POST['custom_post_type_onomies_wp_get_object_terms_fields'] ) && in_array( $_POST['custom_post_type_onomies_wp_get_object_terms_fields'], array( 'ids' ) ) ) ? $_POST['custom_post_type_onomies_wp_get_object_terms_fields'] : null;
367 |
368 | if ( ! empty( $post_ids ) && ! empty( $taxonomies ) ) {
369 |
370 | if ( ! is_array( $post_ids ) ) {
371 | $post_ids = array( $post_ids );
372 | }
373 |
374 | if ( ! is_array( $taxonomies ) ) {
375 | $taxonomies = array( $taxonomies );
376 | }
377 |
378 | // Set any arguments.
379 | $args = array();
380 |
381 | // If we want specific fields.
382 | if ( $terms_fields ) {
383 | $args = array( 'fields' => $terms_fields );
384 | }
385 |
386 | // Get terms.
387 | $terms = wp_get_object_terms( $post_ids, $taxonomies, $args );
388 |
389 | // Get parent title, if desired AND if post type is hierarchical.
390 | if ( $get_parent_title ) {
391 | foreach ( $terms as $term_index => $term ) {
392 | $terms[ $term_index ]->parent = ( is_post_type_hierarchical( $term->taxonomy ) ) ? $this->build_term_parent_title_with_csv( $term->parent ) : '';
393 | }
394 | }
395 |
396 | // Print terms.
397 | echo json_encode( $terms );
398 |
399 | }
400 |
401 | die();
402 | }
403 |
404 | /**
405 | * Allows us to check if a term exists via AJAX.
406 | * Returns an object of term info.
407 | *
408 | * Also allows you to designate that you want the parent's
409 | * name instead of the parent's term id.
410 | *
411 | * @since 1.1
412 | * @uses $cpt_onomy
413 | */
414 | public function ajax_check_if_term_exists() {
415 | global $cpt_onomy;
416 |
417 | $term = ( isset( $_POST['custom_post_type_onomies_term'] ) && ! empty( $_POST['custom_post_type_onomies_term'] ) ) ? $_POST['custom_post_type_onomies_term'] : '';
418 | $term_id = ( isset( $_POST['custom_post_type_onomies_term_id'] ) && ! empty( $_POST['custom_post_type_onomies_term_id'] ) && (int) $_POST['custom_post_type_onomies_term_id'] > 0 ) ? (int) $_POST['custom_post_type_onomies_term_id'] : 0;
419 | $taxonomy = ( isset( $_POST['custom_post_type_onomies_taxonomy'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomy'] ) ) ? $_POST['custom_post_type_onomies_taxonomy'] : '';
420 | $get_parent_title = ( isset( $_POST['custom_post_type_onomies_get_parent_title'] ) && ! empty( $_POST['custom_post_type_onomies_get_parent_title'] ) ) ? true : false;
421 |
422 | if ( ( $term || $term_id > 0 ) && $taxonomy ) {
423 | $term_exists = false;
424 |
425 | if ( $term_id > 0 ) {
426 | $term_exists = $cpt_onomy->term_exists( $term_id, $taxonomy );
427 | }
428 |
429 | if ( ! $term_exists && $term ) {
430 | $term_exists = $cpt_onomy->term_exists( $term, $taxonomy );
431 | }
432 |
433 | if ( ! $term_exists ) {
434 | echo json_encode( array() );
435 | } elseif ( is_numeric( $term_exists ) ) {
436 | echo json_encode( (object) array( 'term_id' => $term_exists ) );
437 | } elseif ( is_object( $term_exists ) || is_array( $term_exists ) ) {
438 |
439 | // Get parent title, if desired.
440 | if ( $get_parent_title ) {
441 | $term_exists->parent = $this->build_term_parent_title_with_csv( $term_exists->parent );
442 | }
443 |
444 | echo json_encode( $term_exists );
445 |
446 | } else {
447 | echo json_encode( array() );
448 | }
449 | } else {
450 | echo json_encode( array() );
451 | }
452 |
453 | die();
454 | }
455 |
456 | /**
457 | * The jQuery field autocomplete callback
458 | *
459 | * This function returns results for the CPT-onomy autocomplete term selection.
460 | *
461 | * You can designate that you only want to include specific terms in the results by returning
462 | * their term IDs using the 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids'
463 | * filter which passes three parameters: the $taxonomy, the $post_type and the $post_id.
464 | * The "include" filter overwrites the "exclude" filter.
465 | *
466 | * You can disable specific terms from being listed in the results by returning their
467 | * term IDs using the 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids'
468 | * filter which passes three parameters: the $taxonomy, the $post_type and the $post_id.
469 | * While the "include" filter overwrites the "exclude" filter, if exclude terms are in the
470 | * include terms, they will be removed.
471 | *
472 | * @since 1.1
473 | * @uses $wpdb
474 | */
475 | public function ajax_meta_box_autocomplete_callback() {
476 | global $wpdb;
477 |
478 | // Get the taxonomy and post type info.
479 | $taxonomy = ( isset( $_POST['custom_post_type_onomies_taxonomy'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomy'] ) ) ? $_POST['custom_post_type_onomies_taxonomy'] : null;
480 | $term = ( isset( $_POST['custom_post_type_onomies_term'] ) && ! empty( $_POST['custom_post_type_onomies_term'] ) ) ? $_POST['custom_post_type_onomies_term'] : null;
481 | $post_type = ( isset( $_POST['custom_post_type_onomies_post_type'] ) && ! empty( $_POST['custom_post_type_onomies_post_type'] ) ) ? $_POST['custom_post_type_onomies_post_type'] : 0;
482 | $post_id = ( isset( $_POST['custom_post_type_onomies_post_id'] ) && ! empty( $_POST['custom_post_type_onomies_post_id'] ) ) ? $_POST['custom_post_type_onomies_post_id'] : 0;
483 |
484 | if ( $taxonomy && $term ) {
485 |
486 | // Get available terms.
487 | $available_terms = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title AS label, post_parent AS parent FROM {$wpdb->posts} WHERE post_type = %s AND post_status = 'publish' ORDER BY post_title ASC", $taxonomy ) );
488 | if ( $available_terms ) {
489 |
490 | /*
491 | * Allows you to use the
492 | * 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids'
493 | * filter to designate that you only want specific terms to be
494 | * printed and therefore assigned. The term ids are stored
495 | * in an array that is used to customize each printed format.
496 | * 'Include' overwrites 'exclude'.
497 | */
498 | $include_term_ids = $this->get_cpt_onomy_terms_include_term_ids( $taxonomy, $post_type, $post_id );
499 |
500 | /*
501 | * Allows you to use the
502 | * 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids'
503 | * filter to exclude specific terms from being printed
504 | * and therefore assigned. The term ids are stored in an
505 | * array that is used to customize each printed format.
506 | * While 'include' overwrites 'exclude', if exclude terms
507 | * are in the include array, they will be removed.
508 | */
509 | $exclude_term_ids = $this->get_cpt_onomy_terms_exclude_term_ids( $taxonomy, $post_type, $post_id );
510 |
511 | $results = array();
512 | foreach ( $available_terms as $this_term ) {
513 |
514 | // Whether or not we want the element displayed.
515 | $add_term_to_results = true;
516 |
517 | // Test against 'include' and 'exclude'.
518 | if ( $include_term_ids && ! in_array( $this_term->ID, $include_term_ids ) ) {
519 | $add_term_to_results = false;
520 | }
521 |
522 | if ( $exclude_term_ids && in_array( $this_term->ID, $exclude_term_ids ) ) {
523 | $add_term_to_results = false;
524 | }
525 |
526 | // We don't want to display children of terms we filtered out.
527 | if ( $this_term->parent ) {
528 | foreach ( get_post_ancestors( $this_term->ID ) as $ancestor ) {
529 | if ( in_array( $ancestor, $exclude_term_ids ) ) {
530 | $add_term_to_results = false;
531 | break;
532 | }
533 | }
534 | }
535 |
536 | if ( $add_term_to_results ) {
537 |
538 | // Go ahead and apply the filter before it's "searched".
539 | $this_term->label = apply_filters( 'the_title', $this_term->label, $this_term->ID );
540 |
541 | /*
542 | * We don't want to display the current post.
543 | * If a match was found, add it to the suggestions.
544 | */
545 | if ( stripos( $this_term->label, $term ) !== false ) {
546 |
547 | $results[] = array(
548 | 'value' => $this_term->ID,
549 | 'label' => $this_term->label,
550 | 'parent' => ( is_post_type_hierarchical( $taxonomy ) ) ? $this->build_term_parent_title_with_csv( $this_term->parent ) : '',
551 | );
552 |
553 | }
554 | }
555 | }
556 |
557 | // Print terms.
558 | echo json_encode( $results );
559 |
560 | }
561 | }
562 |
563 | die();
564 | }
565 |
566 | /**
567 | * If a CPT-onomy is attached to a post type, the plugin adds a meta box to
568 | * the post edit screen so the user can assign/manage the taxonomy's terms.
569 | *
570 | * You can remove the box by returning false to the
571 | * 'custom_post_type_onomies_add_cpt_onomy_admin_meta_box' filter, which passes
572 | * two parameters: the $taxonomy and the $post_type.
573 | *
574 | * This function is invoked by the action 'add_meta_boxes'.
575 | *
576 | * @since 1.0
577 | * @uses $cpt_onomies_manager
578 | * @param string $post_type - the current post's post type
579 | * @param object $post - the current post's information
580 | * @filters 'custom_post_type_onomies_add_cpt_onomy_admin_meta_box' - $taxonomy, $post_type
581 | */
582 | public function add_cpt_onomy_meta_boxes( $post_type, $post ) {
583 | global $cpt_onomies_manager;
584 |
585 | // Loop through all the taxonomies tied to this post type.
586 | foreach ( get_object_taxonomies( $post_type, 'objects' ) as $taxonomy => $tax ) {
587 |
588 | // Make sure its a registered CPT-onomy.
589 | if ( $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
590 |
591 | /*
592 | * This filter allows you to remove the meta box by returning false.
593 | *
594 | * If 'show_ui' is false, do not add meta box.
595 | */
596 | if ( apply_filters( 'custom_post_type_onomies_add_cpt_onomy_admin_meta_box', ( post_type_exists( $taxonomy ) ? get_post_type_object( $taxonomy )->show_ui : true ), $taxonomy, $post_type ) ) {
597 |
598 | // What's the meta box title? - default is taxonomy label.
599 | $meta_box_title = isset( $tax->meta_box_title ) && ! empty( $tax->meta_box_title ) ? $tax->meta_box_title : $tax->label;
600 |
601 | // Add the meta box.
602 | add_meta_box( 'custom-post-type-onomies-' . $taxonomy, apply_filters( 'custom_post_type_onomies_meta_box_title', $meta_box_title, $taxonomy, $post_type ), array( $this, 'print_cpt_onomy_meta_box' ), $post_type, 'side', 'core', array( 'taxonomy' => $taxonomy ) );
603 |
604 | }
605 | }
606 | }
607 | }
608 |
609 | /**
610 | * This function is invoked when a CPT-onomy meta box is attached to a post type's edit post screen.
611 | * This 'callback' function prints the html for the meta box.
612 | *
613 | * The meta box consists of a checklist that allows the user to assign/manage the taxonomy's terms.
614 | * This function mimics a meta box for an ordinary custom taxonomy.
615 | *
616 | * Version 1.1 brought support for 'autocomplete' and 'dropdown' selection format,
617 | * on top of the already existent 'checklist'.
618 | *
619 | * CPT-onomies follows default WordPress behavior, providing a checklist for hierarchical
620 | * CPT-onomies and the autocomplete box for non-hierarchical CPT-onomies. You can change the
621 | * format by hooking into the 'custom_post_type_onomies_meta_box_format' filter, which passes
622 | * two parameters: the $taxonomy and the $post_type.
623 | *
624 | * You can designate that you only want specific terms listed in the results by returning their
625 | * term IDs using the 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids'
626 | * filter which passes three parameters: the $taxonomy, the $post_type and the $post_id.
627 | *
628 | * You can disable specific terms from being listed in the results by returning their
629 | * term IDs using the 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids'
630 | * filter which passes three parameters: the $taxonomy, the $post_type and the $post_id.
631 | *
632 | * This code mimics the WordPress function post_categories_meta_box().
633 | *
634 | * This function is invoked by the action 'add_meta_boxes'.
635 | *
636 | * @since 1.0
637 | * @param object $post - the current post's information
638 | * @param array $box - information about the metabox
639 | * @filters 'custom_post_type_onomies_meta_box_format' - $taxonomy, $post_type
640 | */
641 | public function print_cpt_onomy_meta_box( $post, $metabox ) {
642 |
643 | // Add nonce.
644 | wp_nonce_field( 'assigning_custom_post_type_onomies_taxonomy_relationships', 'custom_post_type_onomies_nonce' );
645 |
646 | // Define variables.
647 | $post_type = ( isset( $post->post_type ) && ! empty( $post->post_type ) && post_type_exists( $post->post_type ) ) ? $post->post_type : null;
648 | $taxonomy = ( isset( $metabox['args']['taxonomy'] ) && ! empty( $metabox['args']['taxonomy'] ) && taxonomy_exists( $metabox['args']['taxonomy'] ) ) ? $metabox['args']['taxonomy'] : null;
649 |
650 | if ( $post_type && $taxonomy ) {
651 |
652 | // Get taxonomy info
653 | $tax = get_taxonomy( $taxonomy );
654 |
655 | // If 'meta_box_format' is not defined, use default WordPress setting
656 | if ( ! ( $format = ( isset( $tax->meta_box_format ) && ! empty( $tax->meta_box_format ) ) ? $tax->meta_box_format : null ) ) {
657 | $format = is_post_type_hierarchical( $taxonomy ) ? 'checklist' : 'autocomplete';
658 | }
659 |
660 | // Allow the user to change the format - 'autocomplete', 'dropdown', 'checklist' - default
661 | $format = apply_filters( 'custom_post_type_onomies_meta_box_format', $format, $taxonomy, $post_type );
662 |
663 | // Does the user have permission to assign terms?
664 | $disabled = ! current_user_can( $tax->cap->assign_terms ) ? ' disabled="disabled"' : '';
665 |
666 | /*
667 | * Allows you to use the
668 | * 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids'
669 | * filter to designate that you only want specific terms
670 | * to be printed and therefore assigned. The term ids are
671 | * stored in an array that is used to customize each printed
672 | * format. 'Include' overwrites 'exclude'.
673 | */
674 | $include_term_ids = $this->get_cpt_onomy_terms_include_term_ids( $taxonomy, $post_type, $post->ID );
675 |
676 | /*
677 | * Allows you to use the
678 | * 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids'
679 | * filter to exclude specific terms from being printed
680 | * and therefore assigned. The term ids are stored in an
681 | * array that is used to customize each printed format.
682 | * While 'include' overwrites 'exclude', if exclude terms
683 | * are in the include array, they will be removed.
684 | */
685 | $exclude_term_ids = $this->get_cpt_onomy_terms_exclude_term_ids( $taxonomy, $post_type, $post->ID );
686 |
687 | // Add field for testing "editability" when we save the information.
688 | ?>
689 |
690 |
697 |
724 | cap->assign_terms ) ) :
727 |
728 | ?>
729 | labels->choose_from_most_used; ?>
730 | ID, $taxonomy );
740 |
741 | // We only need the first term for a dropdown.
742 | $selected_term = $selected_terms ? array_shift( $selected_terms )->term_id : 0;
743 |
744 | /*
745 | * Because the dropdown function only has 'exclude', if 'include' is set,
746 | * we have to get all of the terms and exclude everything but what's in 'include'
747 | */
748 | $dropdown_exclude_term_ids = array();
749 | if ( $include_term_ids ) {
750 |
751 | // Get all terms for this taxonomy that are not in 'include'.
752 | foreach ( get_terms( $taxonomy, array( 'hide_empty' => false, 'fields' => 'ids' ) ) as $term_id ) {
753 | if ( ! in_array( $term_id, $include_term_ids ) ) {
754 | $dropdown_exclude_term_ids[] = $term_id;
755 | }
756 | }
757 | }
758 |
759 | // Make sure 'exclude' term ids are included.
760 | if ( $exclude_term_ids ) {
761 | $dropdown_exclude_term_ids = array_unique( array_merge( $dropdown_exclude_term_ids, $exclude_term_ids ) );
762 | }
763 |
764 | $dropdown = wp_dropdown_categories( array(
765 | 'show_option_none' => sprintf( __( 'No %s are selected', 'cpt-onomies' ), $tax->labels->all_items ),
766 | 'orderby' => 'name',
767 | 'order' => 'ASC',
768 | 'show_count' => false,
769 | 'hide_empty' => false,
770 | 'exclude' => $dropdown_exclude_term_ids,
771 | 'echo' => false,
772 | 'selected' => $selected_term,
773 | 'hierarchical' => is_post_type_hierarchical( $taxonomy ),
774 | 'name' => CPT_ONOMIES_POSTMETA_KEY . '[' . $taxonomy . '][]',
775 | 'id' => 'taxonomy-' . $taxonomy,
776 | 'class' => 'category cpt_onomies',
777 | 'taxonomy' => $taxonomy,
778 | 'hide_if_empty' => false,
779 | ));
780 |
781 | /*
782 | * Need to add disabled to select element
783 | * as a backup, this attribute is also checked in admin-post.js
784 | */
785 | if ( $disabled ) {
786 | $dropdown = preg_replace( '/^\