WordPress.org

Make WordPress Core

Changeset 38747


Ignore:
Timestamp:
10/07/2016 05:11:02 PM (20 months ago)
Author:
swissspidy
Message:

Taxonomy: Introduce WP_Taxonomy and use it in register_taxonomy() and unregister_taxonomy().

This changes the global $wp_taxonomies to an array of WP_Taxonomy objects. WP_Taxonomy includes methods to handle rewrite rules and hooks.
Each taxonomy argument becomes a property of WP_Taxonomy. Introducing such a class makes further improvements in the future much more feasible.

Props boonebgorges for review.
Fixes #36224. See #36217.

Location:
trunk
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r38733 r38747  
    135135     * @since 4.0.0
    136136     *
    137      * @param int    $characters The minimum number of characters required. Default 2.
    138      * @param object $tax        The taxonomy object.
    139      * @param string $s          The search term.
     137     * @param int         $characters The minimum number of characters required. Default 2.
     138     * @param WP_Taxonomy $tax        The taxonomy object.
     139     * @param string      $s          The search term.
    140140     */
    141141    $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
  • trunk/src/wp-includes/class-wp-xmlrpc-server.php

    r38698 r38747  
    718718         * @since 3.4.0
    719719         *
    720          * @param array  $_taxonomy An array of taxonomy data.
    721          * @param object $taxonomy  Taxonomy object.
    722          * @param array  $fields    The subset of taxonomy fields to return.
     720         * @param array       $_taxonomy An array of taxonomy data.
     721         * @param WP_Taxonomy $taxonomy  Taxonomy object.
     722         * @param array       $fields    The subset of taxonomy fields to return.
    723723         */
    724724        return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
  • trunk/src/wp-includes/taxonomy.php

    r38737 r38747  
    224224 *
    225225 * @param string $taxonomy Name of taxonomy object to return.
    226  * @return object|false The Taxonomy Object or false if $taxonomy doesn't exist.
     226 * @return WP_Taxonomy|false The Taxonomy Object or false if $taxonomy doesn't exist.
    227227 */
    228228function get_taxonomy( $taxonomy ) {
     
    291291 *
    292292 * @global array $wp_taxonomies Registered taxonomies.
    293  * @global WP    $wp            WP instance.
    294293 *
    295294 * @param string       $taxonomy    Taxonomy key, must not exceed 32 characters.
     
    360359 */
    361360function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
    362     global $wp_taxonomies, $wp;
     361    global $wp_taxonomies;
    363362
    364363    if ( ! is_array( $wp_taxonomies ) )
     
    366365
    367366    $args = wp_parse_args( $args );
    368 
    369     /**
    370      * Filters the arguments for registering a taxonomy.
    371      *
    372      * @since 4.4.0
    373      *
    374      * @param array  $args        Array of arguments for registering a taxonomy.
    375      * @param string $taxonomy    Taxonomy key.
    376      * @param array  $object_type Array of names of object types for the taxonomy.
    377      */
    378     $args = apply_filters( 'register_taxonomy_args', $args, $taxonomy, (array) $object_type );
    379 
    380     $defaults = array(
    381         'labels'                => array(),
    382         'description'           => '',
    383         'public'                => true,
    384         'publicly_queryable'    => null,
    385         'hierarchical'          => false,
    386         'show_ui'               => null,
    387         'show_in_menu'          => null,
    388         'show_in_nav_menus'     => null,
    389         'show_tagcloud'         => null,
    390         'show_in_quick_edit'    => null,
    391         'show_admin_column'     => false,
    392         'meta_box_cb'           => null,
    393         'capabilities'          => array(),
    394         'rewrite'               => true,
    395         'query_var'             => $taxonomy,
    396         'update_count_callback' => '',
    397         '_builtin'              => false,
    398     );
    399     $args = array_merge( $defaults, $args );
    400367
    401368    if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) {
     
    404371    }
    405372
    406     // If not set, default to the setting for public.
    407     if ( null === $args['publicly_queryable'] ) {
    408         $args['publicly_queryable'] = $args['public'];
    409     }
    410 
    411     // Non-publicly queryable taxonomies should not register query vars, except in the admin.
    412     if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) && ! empty( $wp ) ) {
    413         if ( true === $args['query_var'] )
    414             $args['query_var'] = $taxonomy;
    415         else
    416             $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
    417         $wp->add_query_var( $args['query_var'] );
    418     } else {
    419         // Force query_var to false for non-public taxonomies.
    420         $args['query_var'] = false;
    421     }
    422 
    423     if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
    424         $args['rewrite'] = wp_parse_args( $args['rewrite'], array(
    425             'with_front' => true,
    426             'hierarchical' => false,
    427             'ep_mask' => EP_NONE,
    428         ) );
    429 
    430         if ( empty( $args['rewrite']['slug'] ) )
    431             $args['rewrite']['slug'] = sanitize_title_with_dashes( $taxonomy );
    432 
    433         if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] )
    434             $tag = '(.+?)';
    435         else
    436             $tag = '([^/]+)';
    437 
    438         add_rewrite_tag( "%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=" );
    439         add_permastruct( $taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite'] );
    440     }
    441 
    442     // If not set, default to the setting for public.
    443     if ( null === $args['show_ui'] )
    444         $args['show_ui'] = $args['public'];
    445 
    446     // If not set, default to the setting for show_ui.
    447     if ( null === $args['show_in_menu' ] || ! $args['show_ui'] )
    448         $args['show_in_menu' ] = $args['show_ui'];
    449 
    450     // If not set, default to the setting for public.
    451     if ( null === $args['show_in_nav_menus'] )
    452         $args['show_in_nav_menus'] = $args['public'];
    453 
    454     // If not set, default to the setting for show_ui.
    455     if ( null === $args['show_tagcloud'] )
    456         $args['show_tagcloud'] = $args['show_ui'];
    457 
    458     // If not set, default to the setting for show_ui.
    459     if ( null === $args['show_in_quick_edit'] ) {
    460         $args['show_in_quick_edit'] = $args['show_ui'];
    461     }
    462 
    463     $default_caps = array(
    464         'manage_terms' => 'manage_categories',
    465         'edit_terms'   => 'manage_categories',
    466         'delete_terms' => 'manage_categories',
    467         'assign_terms' => 'edit_posts',
    468     );
    469     $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
    470     unset( $args['capabilities'] );
    471 
    472     $args['name'] = $taxonomy;
    473     $args['object_type'] = array_unique( (array) $object_type );
    474 
    475     $args['labels'] = get_taxonomy_labels( (object) $args );
    476     $args['label'] = $args['labels']->name;
    477 
    478     // If not set, use the default meta box
    479     if ( null === $args['meta_box_cb'] ) {
    480         if ( $args['hierarchical'] )
    481             $args['meta_box_cb'] = 'post_categories_meta_box';
    482         else
    483             $args['meta_box_cb'] = 'post_tags_meta_box';
    484     }
    485 
    486     $wp_taxonomies[ $taxonomy ] = (object) $args;
    487 
    488     // Register callback handling for meta box.
    489     add_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
     373    $taxonomy_object = new WP_Taxonomy( $taxonomy, $object_type, $args );
     374    $taxonomy_object->add_rewrite_rules();
     375
     376    $wp_taxonomies[ $taxonomy ] = $taxonomy_object;
     377
     378    $taxonomy_object->add_hooks();
     379
    490380
    491381    /**
     
    519409    }
    520410
    521     $taxonomy_args = get_taxonomy( $taxonomy );
     411    $taxonomy_object = get_taxonomy( $taxonomy );
    522412
    523413    // Do not allow unregistering internal taxonomies.
    524     if ( $taxonomy_args->_builtin ) {
     414    if ( $taxonomy_object->_builtin ) {
    525415        return new WP_Error( 'invalid_taxonomy', __( 'Unregistering a built-in taxonomy is not allowed' ) );
    526416    }
    527417
    528     global $wp, $wp_taxonomies;
    529 
    530     // Remove query var.
    531     if ( false !== $taxonomy_args->query_var ) {
    532         $wp->remove_query_var( $taxonomy_args->query_var );
    533     }
    534 
    535     // Remove rewrite tags and permastructs.
    536     if ( false !== $taxonomy_args->rewrite ) {
    537         remove_rewrite_tag( "%$taxonomy%" );
    538         remove_permastruct( $taxonomy );
    539     }
    540 
    541     // Unregister callback handling for meta box.
    542     remove_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
     418    global $wp_taxonomies;
     419
     420    $taxonomy_object->remove_rewrite_rules();
     421    $taxonomy_object->remove_hooks();
    543422
    544423    // Remove the taxonomy.
     
    590469 * @since 4.4.0 Added the `items_list_navigation` and `items_list` labels.
    591470 *
    592  * @param object $tax Taxonomy object.
     471 * @param WP_Taxonomy $tax Taxonomy object.
    593472 * @return object object with all the labels as member variables.
    594473 */
  • trunk/src/wp-settings.php

    r38496 r38747  
    188188require( ABSPATH . WPINC . '/script-loader.php' );
    189189require( ABSPATH . WPINC . '/taxonomy.php' );
     190require( ABSPATH . WPINC . '/class-wp-taxonomy.php' );
    190191require( ABSPATH . WPINC . '/class-wp-term.php' );
    191192require( ABSPATH . WPINC . '/class-wp-term-query.php' );
Note: See TracChangeset for help on using the changeset viewer.