<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[38747] trunk: Taxonomy: Introduce `WP_Taxonomy` and use it in `register_taxonomy()` and `unregister_taxonomy()`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/38747">38747</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/38747","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>swissspidy</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-10-07 17:11:02 +0000 (Fri, 07 Oct 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>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 <a href="https://core.trac.wordpress.org/ticket/36224">#36224</a>. See <a href="https://core.trac.wordpress.org/ticket/36217">#36217</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesajaxactionsphp">trunk/src/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunksrcwpincludesclasswpxmlrpcserverphp">trunk/src/wp-includes/class-wp-xmlrpc-server.php</a></li>
<li><a href="#trunksrcwpincludestaxonomyphp">trunk/src/wp-includes/taxonomy.php</a></li>
<li><a href="#trunksrcwpsettingsphp">trunk/src/wp-settings.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswptaxonomyphp">trunk/src/wp-includes/class-wp-taxonomy.php</a></li>
<li><a href="#trunktestsphpunitteststermwpTaxonomyphp">trunk/tests/phpunit/tests/term/wpTaxonomy.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesajaxactionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/ajax-actions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/ajax-actions.php 2016-10-07 16:59:54 UTC (rev 38746)
+++ trunk/src/wp-admin/includes/ajax-actions.php 2016-10-07 17:11:02 UTC (rev 38747)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -134,9 +134,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.0.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param int $characters The minimum number of characters required. Default 2.
- * @param object $tax The taxonomy object.
- * @param string $s The search term.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param int $characters The minimum number of characters required. Default 2.
+ * @param WP_Taxonomy $tax The taxonomy object.
+ * @param string $s The search term.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $tax, $s );
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunksrcwpincludesclasswptaxonomyphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/src/wp-includes/class-wp-taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-taxonomy.php (rev 0)
+++ trunk/src/wp-includes/class-wp-taxonomy.php 2016-10-07 17:11:02 UTC (rev 38747)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,416 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Taxonomy API: WP_Taxonomy class
+ *
+ * @package WordPress
+ * @subpackage Taxonomy
+ * @since 4.7.0
+ */
+
+/**
+ * Core class used for interacting with taxonomies.
+ *
+ * @since 4.7.0
+ */
+final class WP_Taxonomy {
+ /**
+ * Taxonomy key.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var string
+ */
+ public $name;
+
+ /**
+ * Name of the taxonomy shown in the menu. Usually plural.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var string
+ */
+ public $label;
+
+ /**
+ * An array of labels for this taxonomy.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var object
+ */
+ public $labels = array();
+
+ /**
+ * A short descriptive summary of what the taxonomy is for.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var string
+ */
+ public $description = '';
+
+ /**
+ * Whether a taxonomy is intended for use publicly either via the admin interface or by front-end users.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $public = true;
+
+ /**
+ * Whether the taxonomy is publicly queryable.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $publicly_queryable = true;
+
+ /**
+ * Whether the taxonomy is hierarchical.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $hierarchical = false;
+
+ /**
+ * Whether to generate and allow a UI for managing terms in this taxonomy in the admin.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $show_ui = true;
+
+ /**
+ * Whether to show the taxonomy in the admin menu.
+ *
+ * If true, the taxonomy is shown as a submenu of the object type menu. If false, no menu is shown.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $show_in_menu = true;
+
+ /**
+ * Whether the taxonomy is available for selection in navigation menus.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $show_in_nav_menus = true;
+
+ /**
+ * Whether to list the taxonomy in the tag cloud widget controls.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $show_tagcloud = true;
+
+ /**
+ * Whether to show the taxonomy in the quick/bulk edit panel.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $show_in_quick_edit = true;
+
+ /**
+ * Whether to display a column for the taxonomy on its post type listing screens.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $show_admin_column = false;
+
+ /**
+ * The callback function for the meta box display.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool|callable
+ */
+ public $meta_box_cb = null;
+
+ /**
+ * An array of object types this taxonomy is registered for.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var array
+ */
+ public $object_type = null;
+
+ /**
+ * Capabilities for this taxonomy.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var array
+ */
+ public $cap;
+
+ /**
+ * Rewrites information for this taxonomy.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var array|false
+ */
+ public $rewrite;
+
+ /**
+ * Query var string for this taxonomy.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var string|false
+ */
+ public $query_var;
+
+ /**
+ * Function that will be called when the count is updated.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var callable
+ */
+ public $update_count_callback;
+
+ /**
+ * Whether it is a built-in taxonomy.
+ *
+ * @since 4.7.0
+ * @access public
+ * @var bool
+ */
+ public $_builtin;
+
+ /**
+ * Constructor.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @global WP $wp WP instance.
+ *
+ * @param string $taxonomy Taxonomy key, must not exceed 32 characters.
+ * @param array|string $object_type Name of the object type for the taxonomy object.
+ * @param array|string $args Optional. Array or query string of arguments for registering a taxonomy.
+ * Default empty array.
+ */
+ public function __construct( $taxonomy, $object_type, $args = array() ) {
+ $this->name = $taxonomy;
+
+ $this->set_props( $object_type, $args );
+ }
+
+ /**
+ * Sets taxonomy properties.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @param array|string $object_type Name of the object type for the taxonomy object.
+ * @param array|string $args Array or query string of arguments for registering a taxonomy.
+ */
+ public function set_props( $object_type, $args ) {
+ $args = wp_parse_args( $args );
+
+ /**
+ * Filters the arguments for registering a taxonomy.
+ *
+ * @since 4.4.0
+ *
+ * @param array $args Array of arguments for registering a taxonomy.
+ * @param string $taxonomy Taxonomy key.
+ * @param array $object_type Array of names of object types for the taxonomy.
+ */
+ $args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type );
+
+ $defaults = array(
+ 'labels' => array(),
+ 'description' => '',
+ 'public' => true,
+ 'publicly_queryable' => null,
+ 'hierarchical' => false,
+ 'show_ui' => null,
+ 'show_in_menu' => null,
+ 'show_in_nav_menus' => null,
+ 'show_tagcloud' => null,
+ 'show_in_quick_edit' => null,
+ 'show_admin_column' => false,
+ 'meta_box_cb' => null,
+ 'capabilities' => array(),
+ 'rewrite' => true,
+ 'query_var' => $this->name,
+ 'update_count_callback' => '',
+ '_builtin' => false,
+ );
+
+ $args = array_merge( $defaults, $args );
+
+ // If not set, default to the setting for public.
+ if ( null === $args['publicly_queryable'] ) {
+ $args['publicly_queryable'] = $args['public'];
+ }
+
+ if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) ) {
+ if ( true === $args['query_var'] ) {
+ $args['query_var'] = $this->name;
+ } else {
+ $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
+ }
+ } else {
+ // Force query_var to false for non-public taxonomies.
+ $args['query_var'] = false;
+ }
+
+ if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
+ $args['rewrite'] = wp_parse_args( $args['rewrite'], array(
+ 'with_front' => true,
+ 'hierarchical' => false,
+ 'ep_mask' => EP_NONE,
+ ) );
+
+ if ( empty( $args['rewrite']['slug'] ) ) {
+ $args['rewrite']['slug'] = sanitize_title_with_dashes( $this->name );
+ }
+ }
+
+ // If not set, default to the setting for public.
+ if ( null === $args['show_ui'] ) {
+ $args['show_ui'] = $args['public'];
+ }
+
+ // If not set, default to the setting for show_ui.
+ if ( null === $args['show_in_menu'] || ! $args['show_ui'] ) {
+ $args['show_in_menu'] = $args['show_ui'];
+ }
+
+ // If not set, default to the setting for public.
+ if ( null === $args['show_in_nav_menus'] ) {
+ $args['show_in_nav_menus'] = $args['public'];
+ }
+
+ // If not set, default to the setting for show_ui.
+ if ( null === $args['show_tagcloud'] ) {
+ $args['show_tagcloud'] = $args['show_ui'];
+ }
+
+ // If not set, default to the setting for show_ui.
+ if ( null === $args['show_in_quick_edit'] ) {
+ $args['show_in_quick_edit'] = $args['show_ui'];
+ }
+
+ $default_caps = array(
+ 'manage_terms' => 'manage_categories',
+ 'edit_terms' => 'manage_categories',
+ 'delete_terms' => 'manage_categories',
+ 'assign_terms' => 'edit_posts',
+ );
+
+ $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
+ unset( $args['capabilities'] );
+
+ $args['object_type'] = array_unique( (array) $object_type );
+
+ // If not set, use the default meta box
+ if ( null === $args['meta_box_cb'] ) {
+ if ( $args['hierarchical'] ) {
+ $args['meta_box_cb'] = 'post_categories_meta_box';
+ } else {
+ $args['meta_box_cb'] = 'post_tags_meta_box';
+ }
+ }
+
+ foreach ( $args as $property_name => $property_value ) {
+ $this->$property_name = $property_value;
+ }
+
+ $this->labels = get_taxonomy_labels( $this );
+ $this->label = $this->labels->name;
+ }
+
+ /**
+ * Adds the necessary rewrite rules for the taxonomy.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @global WP $wp Current WordPress environment instance.
+ */
+ public function add_rewrite_rules() {
+ /* @var WP $wp */
+ global $wp;
+
+ // Non-publicly queryable taxonomies should not register query vars, except in the admin.
+ if ( false !== $this->query_var && $wp ) {
+ $wp->add_query_var( $this->query_var );
+ }
+
+ if ( false !== $this->rewrite && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
+ if ( $this->hierarchical && $this->rewrite['hierarchical'] ) {
+ $tag = '(.+?)';
+ } else {
+ $tag = '([^/]+)';
+ }
+
+ add_rewrite_tag( "%$this->name%", $tag, $this->query_var ? "{$this->query_var}=" : "taxonomy=$this->name&term=" );
+ add_permastruct( $this->name, "{$this->rewrite['slug']}/%$this->name%", $this->rewrite );
+ }
+ }
+
+ /**
+ * Removes any rewrite rules, permastructs, and rules for the taxonomy.
+ *
+ * @since 4.7.0
+ * @access public
+ *
+ * @global WP $wp Current WordPress environment instance.
+ */
+ public function remove_rewrite_rules() {
+ /* @var WP $wp */
+ global $wp;
+
+ // Remove query var.
+ if ( false !== $this->query_var ) {
+ $wp->remove_query_var( $this->query_var );
+ }
+
+ // Remove rewrite tags and permastructs.
+ if ( false !== $this->rewrite ) {
+ remove_rewrite_tag( "%$this->name%" );
+ remove_permastruct( $this->name );
+ }
+ }
+
+ /**
+ * Registers the ajax callback for the meta box.
+ *
+ * @since 4.7.0
+ * @access public
+ */
+ public function add_hooks() {
+ add_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
+ }
+
+ /**
+ * Removes the ajax callback for the meta box.
+ *
+ * @since 4.7.0
+ * @access public
+ */
+ public function remove_hooks() {
+ remove_filter( 'wp_ajax_add-' . $this->name, '_wp_ajax_add_hierarchical_term' );
+ }
+}
</ins></span></pre></div>
<a id="trunksrcwpincludesclasswpxmlrpcserverphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-xmlrpc-server.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-xmlrpc-server.php 2016-10-07 16:59:54 UTC (rev 38746)
+++ trunk/src/wp-includes/class-wp-xmlrpc-server.php 2016-10-07 17:11:02 UTC (rev 38747)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -717,9 +717,9 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 3.4.0
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param array $_taxonomy An array of taxonomy data.
- * @param object $taxonomy Taxonomy object.
- * @param array $fields The subset of taxonomy fields to return.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param array $_taxonomy An array of taxonomy data.
+ * @param WP_Taxonomy $taxonomy Taxonomy object.
+ * @param array $fields The subset of taxonomy fields to return.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> return apply_filters( 'xmlrpc_prepare_taxonomy', $_taxonomy, $taxonomy, $fields );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpincludestaxonomyphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/taxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/taxonomy.php 2016-10-07 16:59:54 UTC (rev 38746)
+++ trunk/src/wp-includes/taxonomy.php 2016-10-07 17:11:02 UTC (rev 38747)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -223,7 +223,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_taxonomies The registered taxonomies.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $taxonomy Name of taxonomy object to return.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return object|false The Taxonomy Object or false if $taxonomy doesn't exist.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @return WP_Taxonomy|false The Taxonomy Object or false if $taxonomy doesn't exist.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_taxonomy( $taxonomy ) {
</span><span class="cx" style="display: block; padding: 0 10px"> global $wp_taxonomies;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -290,7 +290,6 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.5.0 Introduced `publicly_queryable` argument.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @global array $wp_taxonomies Registered taxonomies.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @global WP $wp WP instance.
</del><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @param string $taxonomy Taxonomy key, must not exceed 32 characters.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param array|string $object_type Object type or array of object types with which the taxonomy should be associated.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -359,135 +358,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @return WP_Error|void WP_Error, if errors.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp_taxonomies, $wp;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_taxonomies;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> if ( ! is_array( $wp_taxonomies ) )
</span><span class="cx" style="display: block; padding: 0 10px"> $wp_taxonomies = array();
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $args = wp_parse_args( $args );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /**
- * Filters the arguments for registering a taxonomy.
- *
- * @since 4.4.0
- *
- * @param array $args Array of arguments for registering a taxonomy.
- * @param string $taxonomy Taxonomy key.
- * @param array $object_type Array of names of object types for the taxonomy.
- */
- $args = apply_filters( 'register_taxonomy_args', $args, $taxonomy, (array) $object_type );
-
- $defaults = array(
- 'labels' => array(),
- 'description' => '',
- 'public' => true,
- 'publicly_queryable' => null,
- 'hierarchical' => false,
- 'show_ui' => null,
- 'show_in_menu' => null,
- 'show_in_nav_menus' => null,
- 'show_tagcloud' => null,
- 'show_in_quick_edit' => null,
- 'show_admin_column' => false,
- 'meta_box_cb' => null,
- 'capabilities' => array(),
- 'rewrite' => true,
- 'query_var' => $taxonomy,
- 'update_count_callback' => '',
- '_builtin' => false,
- );
- $args = array_merge( $defaults, $args );
-
</del><span class="cx" style="display: block; padding: 0 10px"> if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) {
</span><span class="cx" style="display: block; padding: 0 10px"> _doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), '4.2.0' );
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // If not set, default to the setting for public.
- if ( null === $args['publicly_queryable'] ) {
- $args['publicly_queryable'] = $args['public'];
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $taxonomy_object = new WP_Taxonomy( $taxonomy, $object_type, $args );
+ $taxonomy_object->add_rewrite_rules();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Non-publicly queryable taxonomies should not register query vars, except in the admin.
- if ( false !== $args['query_var'] && ( is_admin() || false !== $args['publicly_queryable'] ) && ! empty( $wp ) ) {
- if ( true === $args['query_var'] )
- $args['query_var'] = $taxonomy;
- else
- $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
- $wp->add_query_var( $args['query_var'] );
- } else {
- // Force query_var to false for non-public taxonomies.
- $args['query_var'] = false;
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $wp_taxonomies[ $taxonomy ] = $taxonomy_object;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
- $args['rewrite'] = wp_parse_args( $args['rewrite'], array(
- 'with_front' => true,
- 'hierarchical' => false,
- 'ep_mask' => EP_NONE,
- ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $taxonomy_object->add_hooks();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( empty( $args['rewrite']['slug'] ) )
- $args['rewrite']['slug'] = sanitize_title_with_dashes( $taxonomy );
</del><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] )
- $tag = '(.+?)';
- else
- $tag = '([^/]+)';
-
- add_rewrite_tag( "%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=" );
- add_permastruct( $taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite'] );
- }
-
- // If not set, default to the setting for public.
- if ( null === $args['show_ui'] )
- $args['show_ui'] = $args['public'];
-
- // If not set, default to the setting for show_ui.
- if ( null === $args['show_in_menu' ] || ! $args['show_ui'] )
- $args['show_in_menu' ] = $args['show_ui'];
-
- // If not set, default to the setting for public.
- if ( null === $args['show_in_nav_menus'] )
- $args['show_in_nav_menus'] = $args['public'];
-
- // If not set, default to the setting for show_ui.
- if ( null === $args['show_tagcloud'] )
- $args['show_tagcloud'] = $args['show_ui'];
-
- // If not set, default to the setting for show_ui.
- if ( null === $args['show_in_quick_edit'] ) {
- $args['show_in_quick_edit'] = $args['show_ui'];
- }
-
- $default_caps = array(
- 'manage_terms' => 'manage_categories',
- 'edit_terms' => 'manage_categories',
- 'delete_terms' => 'manage_categories',
- 'assign_terms' => 'edit_posts',
- );
- $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
- unset( $args['capabilities'] );
-
- $args['name'] = $taxonomy;
- $args['object_type'] = array_unique( (array) $object_type );
-
- $args['labels'] = get_taxonomy_labels( (object) $args );
- $args['label'] = $args['labels']->name;
-
- // If not set, use the default meta box
- if ( null === $args['meta_box_cb'] ) {
- if ( $args['hierarchical'] )
- $args['meta_box_cb'] = 'post_categories_meta_box';
- else
- $args['meta_box_cb'] = 'post_tags_meta_box';
- }
-
- $wp_taxonomies[ $taxonomy ] = (object) $args;
-
- // Register callback handling for meta box.
- add_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
-
</del><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Fires after a taxonomy is registered.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -518,29 +408,18 @@
</span><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $taxonomy_args = get_taxonomy( $taxonomy );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $taxonomy_object = get_taxonomy( $taxonomy );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Do not allow unregistering internal taxonomies.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $taxonomy_args->_builtin ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $taxonomy_object->_builtin ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return new WP_Error( 'invalid_taxonomy', __( 'Unregistering a built-in taxonomy is not allowed' ) );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- global $wp, $wp_taxonomies;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ global $wp_taxonomies;
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Remove query var.
- if ( false !== $taxonomy_args->query_var ) {
- $wp->remove_query_var( $taxonomy_args->query_var );
- }
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $taxonomy_object->remove_rewrite_rules();
+ $taxonomy_object->remove_hooks();
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Remove rewrite tags and permastructs.
- if ( false !== $taxonomy_args->rewrite ) {
- remove_rewrite_tag( "%$taxonomy%" );
- remove_permastruct( $taxonomy );
- }
-
- // Unregister callback handling for meta box.
- remove_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
-
</del><span class="cx" style="display: block; padding: 0 10px"> // Remove the taxonomy.
</span><span class="cx" style="display: block; padding: 0 10px"> unset( $wp_taxonomies[ $taxonomy ] );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -589,7 +468,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.3.0 Added the `no_terms` label.
</span><span class="cx" style="display: block; padding: 0 10px"> * @since 4.4.0 Added the `items_list_navigation` and `items_list` labels.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param object $tax Taxonomy object.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @param WP_Taxonomy $tax Taxonomy object.
</ins><span class="cx" style="display: block; padding: 0 10px"> * @return object object with all the labels as member variables.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> function get_taxonomy_labels( $tax ) {
</span></span></pre></div>
<a id="trunksrcwpsettingsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-settings.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-settings.php 2016-10-07 16:59:54 UTC (rev 38746)
+++ trunk/src/wp-settings.php 2016-10-07 17:11:02 UTC (rev 38747)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -187,6 +187,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/deprecated.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/script-loader.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/taxonomy.php' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require( ABSPATH . WPINC . '/class-wp-taxonomy.php' );
</ins><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/class-wp-term.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/class-wp-term-query.php' );
</span><span class="cx" style="display: block; padding: 0 10px"> require( ABSPATH . WPINC . '/class-wp-tax-query.php' );
</span></span></pre></div>
<a id="trunktestsphpunitteststermwpTaxonomyphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/term/wpTaxonomy.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/term/wpTaxonomy.php (rev 0)
+++ trunk/tests/phpunit/tests/term/wpTaxonomy.php 2016-10-07 17:11:02 UTC (rev 38747)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,90 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group taxonomy
+ */
+class Tests_WP_Taxonomy extends WP_UnitTestCase {
+ public function test_instances() {
+ global $wp_taxonomies;
+
+ foreach ( $wp_taxonomies as $taxonomy ) {
+ $this->assertInstanceOf( 'WP_Taxonomy', $taxonomy );
+ }
+ }
+
+ public function test_does_not_add_query_var_if_not_public() {
+ $this->set_permalink_structure( '/%postname%' );
+
+ /* @var WP $wp */
+ global $wp;
+
+ $taxonomy = rand_str();
+ $taxonomy_object = new WP_Taxonomy( $taxonomy, 'post' );
+
+ $taxonomy_object->add_rewrite_rules();
+ $this->assertFalse( in_array( 'foobar', $wp->public_query_vars ) );
+ }
+
+ public function test_adds_query_var_if_public() {
+ $this->set_permalink_structure( '/%postname%' );
+
+ /* @var WP $wp */
+ global $wp;
+
+ $taxonomy = rand_str();
+ $taxonomy_object = new WP_Taxonomy( $taxonomy, 'post', array(
+ 'public' => true,
+ 'rewrite' => false,
+ 'query_var' => 'foobar',
+ ) );
+
+ $taxonomy_object->add_rewrite_rules();
+ $in_array = in_array( 'foobar', $wp->public_query_vars );
+
+ $taxonomy_object->remove_rewrite_rules();
+ $in_array_after = in_array( 'foobar', $wp->public_query_vars );
+
+ $this->assertTrue( $in_array );
+ $this->assertFalse( $in_array_after );
+ }
+
+ public function test_adds_rewrite_rules() {
+ $this->set_permalink_structure( '/%postname%' );
+
+ /* @var WP_Rewrite $wp_rewrite */
+ global $wp_rewrite;
+
+ $taxonomy = rand_str();
+ $taxonomy_object = new WP_Taxonomy( $taxonomy, 'post', array(
+ 'public' => true,
+ 'rewrite' => true,
+ ) );
+
+ $taxonomy_object->add_rewrite_rules();
+ $rewrite_tags = $wp_rewrite->rewritecode;
+
+ $taxonomy_object->remove_rewrite_rules();
+ $rewrite_tags_after = $wp_rewrite->rewritecode;
+
+ $this->assertTrue( false !== array_search( "%$taxonomy%", $rewrite_tags ) );
+ $this->assertFalse( array_search( "%$taxonomy%", $rewrite_tags_after ) );
+ }
+
+ public function test_adds_ajax_callback() {
+ $taxonomy = rand_str();
+ $taxonomy_object = new WP_Taxonomy( $taxonomy, 'post', array(
+ 'public' => true,
+ 'rewrite' => true,
+ ) );
+
+ $taxonomy_object->add_hooks();
+ $has_action = has_action( "wp_ajax_add-$taxonomy", '_wp_ajax_add_hierarchical_term' );
+
+ $taxonomy_object->remove_hooks();
+ $has_action_after = has_action( "wp_ajax_add-$taxonomy", '_wp_ajax_add_hierarchical_term' );
+
+ $this->assertSame( 10, $has_action );
+ $this->assertFalse( $has_action_after );
+
+ }
+}
</ins></span></pre>
</div>
</div>
</body>
</html>