Introduce `WP_Term`.
`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, `get_term_by()` is able to cast results of its query
to a `WP_Term` object by passing it through `get_term()`.
The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
`get_term()` detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.
The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.
Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.
Built from https://develop.svn.wordpress.org/trunk@34997
git-svn-id: http://core.svn.wordpress.org/trunk@34962 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-09 21:59:29 -04:00
< ? php
/**
* Taxonomy API : WP_Term class
*
* @ package WordPress
* @ subpackage Taxonomy
* @ since 4.4 . 0
*/
/**
* Core class used to implement the WP_Term object .
*
* @ since 4.4 . 0
*/
final class WP_Term {
/**
* Term ID .
*
* @ since 4.4 . 0
* @ access public
* @ var int
*/
public $term_id ;
/**
* The term ' s name .
*
* @ since 4.4 . 0
* @ access public
* @ var string
*/
public $name = '' ;
/**
* The term ' s slug .
*
* @ since 4.4 . 0
* @ access public
* @ var string
*/
public $slug = '' ;
/**
* The term ' s term_group .
*
* @ since 4.4 . 0
* @ access public
* @ var string
*/
public $term_group = '' ;
/**
* Term Taxonomy ID .
*
* @ since 4.4 . 0
* @ access public
* @ var int
*/
public $term_taxonomy_id = 0 ;
/**
* The term ' s taxonomy name .
*
* @ since 4.4 . 0
* @ access public
* @ var string
*/
public $taxonomy = '' ;
/**
* The term ' s description .
*
* @ since 4.4 . 0
* @ access public
* @ var string
*/
public $description = '' ;
/**
* ID of a term ' s parent term .
*
* @ since 4.4 . 0
* @ access public
* @ var int
*/
public $parent = 0 ;
/**
* Cached object count for this term .
*
* @ since 4.4 . 0
* @ access public
* @ var int
*/
public $count = 0 ;
/**
* Stores the term object ' s sanitization level .
*
* Does not correspond to a database field .
*
* @ since 4.4 . 0
* @ access public
* @ var string
*/
public $filter = 'raw' ;
/**
* Retrieve WP_Term instance .
*
* @ since 4.4 . 0
* @ access public
* @ static
*
* @ global wpdb $wpdb WordPress database abstraction object .
*
Make `get_term()` behave more consistently in the context of shared terms.
When `WP_Term` was introduced in [34997], the `$taxonomy` parameter for
`get_term()` was made optional. This meant that, when the optional param was
omitted, `get_term()` had no way of determining which term was intended when
the term_id was shared between multiple taxonomies. As a (somewhat sneaky) way
of fixing things, `get_term()` split any shared terms it found. But this could
cause problems with developer expectations: it's not clear why requesting a
term should result in a database update, much less a potential change in the
ID of a term.
In place of this technique, this changeset introduces a number of changes that
make the handling of shared terms a bit less insane:
* When a taxonomy is provided to `get_term()`, and a cached term is found matching the term_id, make sure the taxonomy also matches before returning it.
* When a taxonomy is not provided, ensure that the term is not shared before adding it to the cache.
* When a term is shared between taxonomies and no taxonomy is provided, return a `WP_Error` rather than splitting the term.
* When a term is shared between taxonomies, only one of which is valid, return the term from that taxonomy.
Props boonebgorges, dlh.
Fixes #34533.
Built from https://develop.svn.wordpress.org/trunk@35537
git-svn-id: http://core.svn.wordpress.org/trunk@35501 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-11-05 11:45:25 -05:00
* @ param int $term_id Term ID .
* @ param string $taxonomy Optional . Limit matched terms to those matching `$taxonomy` . Only used for
* disambiguating potentially shared terms .
* @ return WP_Term | WP_Error | false Term object , if found . WP_Error if `$term_id` is shared between taxonomies and
* there ' s insufficient data to distinguish which term is intended .
* False for other failures .
Introduce `WP_Term`.
`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, `get_term_by()` is able to cast results of its query
to a `WP_Term` object by passing it through `get_term()`.
The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
`get_term()` detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.
The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.
Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.
Built from https://develop.svn.wordpress.org/trunk@34997
git-svn-id: http://core.svn.wordpress.org/trunk@34962 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-09 21:59:29 -04:00
*/
Make `get_term()` behave more consistently in the context of shared terms.
When `WP_Term` was introduced in [34997], the `$taxonomy` parameter for
`get_term()` was made optional. This meant that, when the optional param was
omitted, `get_term()` had no way of determining which term was intended when
the term_id was shared between multiple taxonomies. As a (somewhat sneaky) way
of fixing things, `get_term()` split any shared terms it found. But this could
cause problems with developer expectations: it's not clear why requesting a
term should result in a database update, much less a potential change in the
ID of a term.
In place of this technique, this changeset introduces a number of changes that
make the handling of shared terms a bit less insane:
* When a taxonomy is provided to `get_term()`, and a cached term is found matching the term_id, make sure the taxonomy also matches before returning it.
* When a taxonomy is not provided, ensure that the term is not shared before adding it to the cache.
* When a term is shared between taxonomies and no taxonomy is provided, return a `WP_Error` rather than splitting the term.
* When a term is shared between taxonomies, only one of which is valid, return the term from that taxonomy.
Props boonebgorges, dlh.
Fixes #34533.
Built from https://develop.svn.wordpress.org/trunk@35537
git-svn-id: http://core.svn.wordpress.org/trunk@35501 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-11-05 11:45:25 -05:00
public static function get_instance ( $term_id , $taxonomy = null ) {
Introduce `WP_Term`.
`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, `get_term_by()` is able to cast results of its query
to a `WP_Term` object by passing it through `get_term()`.
The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
`get_term()` detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.
The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.
Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.
Built from https://develop.svn.wordpress.org/trunk@34997
git-svn-id: http://core.svn.wordpress.org/trunk@34962 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-09 21:59:29 -04:00
global $wpdb ;
$term_id = ( int ) $term_id ;
if ( ! $term_id ) {
return false ;
}
$_term = wp_cache_get ( $term_id , 'terms' );
// If there isn't a cached version, hit the database.
Make `get_term()` behave more consistently in the context of shared terms.
When `WP_Term` was introduced in [34997], the `$taxonomy` parameter for
`get_term()` was made optional. This meant that, when the optional param was
omitted, `get_term()` had no way of determining which term was intended when
the term_id was shared between multiple taxonomies. As a (somewhat sneaky) way
of fixing things, `get_term()` split any shared terms it found. But this could
cause problems with developer expectations: it's not clear why requesting a
term should result in a database update, much less a potential change in the
ID of a term.
In place of this technique, this changeset introduces a number of changes that
make the handling of shared terms a bit less insane:
* When a taxonomy is provided to `get_term()`, and a cached term is found matching the term_id, make sure the taxonomy also matches before returning it.
* When a taxonomy is not provided, ensure that the term is not shared before adding it to the cache.
* When a term is shared between taxonomies and no taxonomy is provided, return a `WP_Error` rather than splitting the term.
* When a term is shared between taxonomies, only one of which is valid, return the term from that taxonomy.
Props boonebgorges, dlh.
Fixes #34533.
Built from https://develop.svn.wordpress.org/trunk@35537
git-svn-id: http://core.svn.wordpress.org/trunk@35501 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-11-05 11:45:25 -05:00
if ( ! $_term || ( $taxonomy && $taxonomy !== $_term -> taxonomy ) ) {
// Grab all matching terms, in case any are shared between taxonomies.
$terms = $wpdb -> get_results ( $wpdb -> prepare ( " SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id = %d " , $term_id ) );
if ( ! $terms ) {
return false ;
}
// If a taxonomy was specified, find a match.
if ( $taxonomy ) {
foreach ( $terms as $match ) {
if ( $taxonomy === $match -> taxonomy ) {
$_term = $match ;
break ;
}
}
// If only one match was found, it's the one we want.
} elseif ( 1 === count ( $terms ) ) {
$_term = reset ( $terms );
// Otherwise, the term must be shared between taxonomies.
} else {
// If the term is shared only with invalid taxonomies, return the one valid term.
foreach ( $terms as $t ) {
if ( ! taxonomy_exists ( $t -> taxonomy ) ) {
continue ;
}
// Only hit if we've already identified a term in a valid taxonomy.
if ( $_term ) {
return new WP_Error ( 'ambiguous_term_id' , __ ( 'Term ID is shared between multiple taxonomies' ), $term_id );
}
$_term = $t ;
}
}
Introduce `WP_Term`.
`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, `get_term_by()` is able to cast results of its query
to a `WP_Term` object by passing it through `get_term()`.
The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
`get_term()` detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.
The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.
Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.
Built from https://develop.svn.wordpress.org/trunk@34997
git-svn-id: http://core.svn.wordpress.org/trunk@34962 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-09 21:59:29 -04:00
if ( ! $_term ) {
return false ;
}
Make `get_term()` behave more consistently in the context of shared terms.
When `WP_Term` was introduced in [34997], the `$taxonomy` parameter for
`get_term()` was made optional. This meant that, when the optional param was
omitted, `get_term()` had no way of determining which term was intended when
the term_id was shared between multiple taxonomies. As a (somewhat sneaky) way
of fixing things, `get_term()` split any shared terms it found. But this could
cause problems with developer expectations: it's not clear why requesting a
term should result in a database update, much less a potential change in the
ID of a term.
In place of this technique, this changeset introduces a number of changes that
make the handling of shared terms a bit less insane:
* When a taxonomy is provided to `get_term()`, and a cached term is found matching the term_id, make sure the taxonomy also matches before returning it.
* When a taxonomy is not provided, ensure that the term is not shared before adding it to the cache.
* When a term is shared between taxonomies and no taxonomy is provided, return a `WP_Error` rather than splitting the term.
* When a term is shared between taxonomies, only one of which is valid, return the term from that taxonomy.
Props boonebgorges, dlh.
Fixes #34533.
Built from https://develop.svn.wordpress.org/trunk@35537
git-svn-id: http://core.svn.wordpress.org/trunk@35501 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-11-05 11:45:25 -05:00
// Don't return terms from invalid taxonomies.
if ( ! taxonomy_exists ( $_term -> taxonomy ) ) {
return new WP_Error ( 'invalid_taxonomy' , __ ( 'Invalid taxonomy' ) );
}
Introduce `WP_Term`.
`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, `get_term_by()` is able to cast results of its query
to a `WP_Term` object by passing it through `get_term()`.
The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
`get_term()` detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.
The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.
Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.
Built from https://develop.svn.wordpress.org/trunk@34997
git-svn-id: http://core.svn.wordpress.org/trunk@34962 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-09 21:59:29 -04:00
$_term = sanitize_term ( $_term , $_term -> taxonomy , 'raw' );
Make `get_term()` behave more consistently in the context of shared terms.
When `WP_Term` was introduced in [34997], the `$taxonomy` parameter for
`get_term()` was made optional. This meant that, when the optional param was
omitted, `get_term()` had no way of determining which term was intended when
the term_id was shared between multiple taxonomies. As a (somewhat sneaky) way
of fixing things, `get_term()` split any shared terms it found. But this could
cause problems with developer expectations: it's not clear why requesting a
term should result in a database update, much less a potential change in the
ID of a term.
In place of this technique, this changeset introduces a number of changes that
make the handling of shared terms a bit less insane:
* When a taxonomy is provided to `get_term()`, and a cached term is found matching the term_id, make sure the taxonomy also matches before returning it.
* When a taxonomy is not provided, ensure that the term is not shared before adding it to the cache.
* When a term is shared between taxonomies and no taxonomy is provided, return a `WP_Error` rather than splitting the term.
* When a term is shared between taxonomies, only one of which is valid, return the term from that taxonomy.
Props boonebgorges, dlh.
Fixes #34533.
Built from https://develop.svn.wordpress.org/trunk@35537
git-svn-id: http://core.svn.wordpress.org/trunk@35501 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-11-05 11:45:25 -05:00
// Don't cache terms that are shared between taxonomies.
if ( 1 === count ( $terms ) ) {
wp_cache_add ( $term_id , $_term , 'terms' );
}
Introduce `WP_Term`.
`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, `get_term_by()` is able to cast results of its query
to a `WP_Term` object by passing it through `get_term()`.
The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
`get_term()` detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.
The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.
Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.
Built from https://develop.svn.wordpress.org/trunk@34997
git-svn-id: http://core.svn.wordpress.org/trunk@34962 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-09 21:59:29 -04:00
}
2015-10-12 11:13:24 -04:00
$term_obj = new WP_Term ( $_term );
$term_obj -> filter ( $term_obj -> filter );
return $term_obj ;
Introduce `WP_Term`.
`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, `get_term_by()` is able to cast results of its query
to a `WP_Term` object by passing it through `get_term()`.
The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
`get_term()` detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.
The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.
Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.
Built from https://develop.svn.wordpress.org/trunk@34997
git-svn-id: http://core.svn.wordpress.org/trunk@34962 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-09 21:59:29 -04:00
}
/**
* Constructor .
*
* @ since 4.4 . 0
* @ access public
*
* @ param WP_Term | object $term Term object .
*/
public function __construct ( $term ) {
foreach ( get_object_vars ( $term ) as $key => $value ) {
$this -> $key = $value ;
}
}
/**
* Sanitizes term fields , according to the filter type provided .
*
* @ since 4.4 . 0
* @ access public
*
* @ param string $filter Filter context . Accepts 'edit' , 'db' , 'display' , 'attribute' , 'js' , 'raw' .
*/
public function filter ( $filter ) {
sanitize_term ( $this , $this -> taxonomy , $filter );
}
/**
* Converts an object to array .
*
* @ since 4.4 . 0
* @ access public
*
* @ return array Object as array .
*/
public function to_array () {
return get_object_vars ( $this );
}
2015-10-12 11:13:48 -04:00
/**
* Getter .
*
* @ since 4.4 . 0
* @ access public
*
2016-01-09 20:26:25 -05:00
* @ param string $key Property to get .
* @ return mixed Property value .
2015-10-12 11:13:48 -04:00
*/
public function __get ( $key ) {
switch ( $key ) {
case 'data' :
2015-10-18 23:12:24 -04:00
$data = new stdClass ();
$columns = array ( 'term_id' , 'name' , 'slug' , 'term_group' , 'term_taxonomy_id' , 'taxonomy' , 'description' , 'parent' , 'count' );
foreach ( $columns as $column ) {
$data -> { $column } = isset ( $this -> { $column } ) ? $this -> { $column } : null ;
}
return sanitize_term ( $data , $data -> taxonomy , 'raw' );
2015-10-12 11:13:48 -04:00
break ;
}
}
Introduce `WP_Term`.
`get_term()` now returns a `WP_Term` object, instead of a `stdClass` object.
Cache support and sanitization filters for individual terms are now more
centralized. For example, `get_term_by()` is able to cast results of its query
to a `WP_Term` object by passing it through `get_term()`.
The `$taxonomy` parameter for `get_term()` is now optional, as terms ought to
be unique to a taxonomy (ie, shared terms no longer exist). In cases where
`get_term()` detects that the term matching the specified term_id is from the
wrong taxonomy, it checks to see if you've requested a shared term, and if so,
it splits the term. This is used only for fallback purposes.
The elimination of shared terms allows the caching strategy for terms to be
simplified. Individual terms are now cached in a single 'terms' bucket.
Props flixos90, boonebgorges, scribu, dipesh.kakadiya.
See #14162.
Built from https://develop.svn.wordpress.org/trunk@34997
git-svn-id: http://core.svn.wordpress.org/trunk@34962 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2015-10-09 21:59:29 -04:00
}