New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_terms won't return custom Taxonomy term when tax is defined with dashes #109

Closed
jmslbam opened this Issue Apr 9, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@jmslbam
Copy link
Contributor

jmslbam commented Apr 9, 2018

The problem

https://github.com/level-level/Clarkson-Core/blob/master/wordpress-objects/Clarkson_Object.php#L352

ns-cat-blog won't pass the Class exists check.

How to fix

Classname needs to be sanitized

$cc = Clarkson_Core::get_instance();
$class_name = $cc->autoloader->sanitize_object_name( $taxonomy );

How to test

1.Create a custom taxonomy name custom-tax and make it available to a (custom) post type.

<?php

function custom_tax_init() {
	register_taxonomy( 'custom-tax', array( 'cpt', 'post' ), array(
		'hierarchical'      => true,
		'public'            => true,
		'show_in_nav_menus' => true,
		'show_ui'           => true,
		'show_admin_column' => false,
		'query_var'         => true,
		'rewrite'           => true,
		'capabilities'      => array(
			'manage_terms'  => 'edit_posts',
			'edit_terms'    => 'edit_posts',
			'delete_terms'  => 'edit_posts',
			'assign_terms'  => 'edit_posts'
		),
		'labels'            => array(
			'name'                       => __( 'Taxis', 'test' ),
			'singular_name'              => _x( 'Taxi', 'taxonomy general name', 'test' ),
			'search_items'               => __( 'Search Taxis', 'test' ),
			'popular_items'              => __( 'Popular Taxis', 'test' ),
			'all_items'                  => __( 'All Taxis', 'test' ),
			'parent_item'                => __( 'Parent Taxi', 'test' ),
			'parent_item_colon'          => __( 'Parent Taxi:', 'test' ),
			'edit_item'                  => __( 'Edit Taxi', 'test' ),
			'update_item'                => __( 'Update Taxi', 'test' ),
			'add_new_item'               => __( 'New Taxi', 'test' ),
			'new_item_name'              => __( 'New Taxi', 'test' ),
			'separate_items_with_commas' => __( 'Separate Taxis with commas', 'test' ),
			'add_or_remove_items'        => __( 'Add or remove Taxis', 'test' ),
			'choose_from_most_used'      => __( 'Choose from the most used Taxis', 'test' ),
			'not_found'                  => __( 'No Taxis found.', 'test' ),
			'menu_name'                  => __( 'Taxis', 'test' ),
		),
		'show_in_rest'      => true,
		'rest_base'         => 'custom-tax',
		'rest_controller_class' => 'WP_REST_Terms_Controller',
	) );

}
add_action( 'init', 'custom_tax_init' );
  1. Create a wordpress-object for custom-tax.
<?php
class custom_tax extends Clarkson_Term {
    public static $taxonomy = 'custom-tax';
}

1.Then create a post and assign a custom term.

  1. Then from PHP, call $post->get_terms('custom-tax');

Result = an array of custom_tax.php object

Before a custom taxonomy named custom-tax would pass the class exists check because it would check on the class “custom-tax” instead of the correct sanitized “custom_tax” when using $post->get_terms(‘custom-tax’). We sanitize this name because you can’t call / name a PHP Class custom-tax ;)

@jmslbam jmslbam added this to the 0.3.0 milestone Apr 18, 2018

@jmslbam

This comment has been minimized.

Copy link
Contributor Author

jmslbam commented Jul 12, 2018

@jmslbam jmslbam closed this Jul 12, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment