2016-01-19 04:55:19 +01:00
|
|
|
<?php
|
|
|
|
|
2017-06-30 06:35:39 +02:00
|
|
|
/**
|
|
|
|
* Unit test factory for terms.
|
|
|
|
*
|
|
|
|
* Note: The below @method notations are defined solely for the benefit of IDEs,
|
|
|
|
* as a way to indicate expected return values from the given factory methods.
|
|
|
|
*
|
|
|
|
* @method int create( $args = array(), $generation_definitions = null )
|
|
|
|
* @method int[] create_many( $count, $args = array(), $generation_definitions = null )
|
|
|
|
*/
|
2016-01-19 04:55:19 +01:00
|
|
|
class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
|
|
|
|
|
|
|
|
private $taxonomy;
|
|
|
|
const DEFAULT_TAXONOMY = 'post_tag';
|
|
|
|
|
2019-03-15 13:15:08 +01:00
|
|
|
public function __construct( $factory = null, $taxonomy = null ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
parent::__construct( $factory );
|
2017-12-01 00:09:33 +01:00
|
|
|
$this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
|
2016-01-19 04:55:19 +01:00
|
|
|
$this->default_generation_definitions = array(
|
2017-12-01 00:09:33 +01:00
|
|
|
'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
|
|
|
|
'taxonomy' => $this->taxonomy,
|
2016-01-19 04:55:19 +01:00
|
|
|
'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-01-09 06:43:14 +01:00
|
|
|
/**
|
|
|
|
* Creates a term object.
|
|
|
|
*
|
|
|
|
* @param array $args Array or string of arguments for inserting a term.
|
|
|
|
*
|
|
|
|
* @return array|WP_Error
|
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function create_object( $args ) {
|
2017-12-01 00:09:33 +01:00
|
|
|
$args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
|
2016-01-19 04:55:19 +01:00
|
|
|
$term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
|
2017-12-01 00:09:33 +01:00
|
|
|
if ( is_wp_error( $term_id_pair ) ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
return $term_id_pair;
|
2017-12-01 00:09:33 +01:00
|
|
|
}
|
2016-01-19 04:55:19 +01:00
|
|
|
return $term_id_pair['term_id'];
|
|
|
|
}
|
|
|
|
|
2019-01-09 06:43:14 +01:00
|
|
|
/**
|
|
|
|
* Updates the term.
|
|
|
|
*
|
|
|
|
* @param int|object $term The term to update.
|
|
|
|
* @param array|string $fields The context in which to relate the term to the object.
|
|
|
|
*
|
|
|
|
* @return int The term id.
|
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function update_object( $term, $fields ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
$fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
|
2017-12-01 00:09:33 +01:00
|
|
|
if ( is_object( $term ) ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
$taxonomy = $term->taxonomy;
|
2017-12-01 00:09:33 +01:00
|
|
|
}
|
2016-01-19 04:55:19 +01:00
|
|
|
$term_id_pair = wp_update_term( $term, $taxonomy, $fields );
|
|
|
|
return $term_id_pair['term_id'];
|
|
|
|
}
|
|
|
|
|
2019-01-09 06:43:14 +01:00
|
|
|
/**
|
|
|
|
* Attach terms on the given post.
|
|
|
|
*
|
|
|
|
* @param int $post_id The Post ID.
|
|
|
|
* @param string|array $terms An array of terms to set for the post, or a string of terms
|
|
|
|
* separated by commas. Hierarchical taxonomies must always pass IDs rather
|
|
|
|
* than names so that children with the same names but different parents
|
|
|
|
* aren't confused.
|
|
|
|
* @param string $taxonomy Taxonomy name.
|
|
|
|
* @param bool $append Optional. If true, don't delete existing terms, just add on. If false,
|
|
|
|
* replace the terms with the new terms. Default true.
|
|
|
|
*
|
|
|
|
* @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
|
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
|
|
|
|
}
|
|
|
|
|
2017-06-30 06:35:39 +02:00
|
|
|
/**
|
2019-01-09 06:43:14 +01:00
|
|
|
* Create a term and returns it as a object.
|
|
|
|
*
|
|
|
|
* @param array $args Array or string of arguments for inserting a term.
|
|
|
|
* @param null $generation_definitions The default values.
|
|
|
|
*
|
|
|
|
* @return null|WP_Error|WP_Term WP_Term on success. WP_error if taxonomy does not exist. Null for miscellaneous failure.
|
2017-06-30 06:35:39 +02:00
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function create_and_get( $args = array(), $generation_definitions = null ) {
|
2017-12-01 00:09:33 +01:00
|
|
|
$term_id = $this->create( $args, $generation_definitions );
|
2016-01-19 04:55:19 +01:00
|
|
|
$taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
|
|
|
|
return get_term( $term_id, $taxonomy );
|
|
|
|
}
|
|
|
|
|
2019-01-09 06:43:14 +01:00
|
|
|
/**
|
|
|
|
* Retrieves the term by given term id.
|
|
|
|
*
|
|
|
|
* @param int $term_id The term id to retrieve.
|
|
|
|
*
|
|
|
|
* @return null|WP_Error|WP_Term WP_Term on success. WP_error if taxonomy does not exist. Null for miscellaneous failure.
|
|
|
|
*/
|
2019-03-15 13:15:08 +01:00
|
|
|
public function get_object_by_id( $term_id ) {
|
2016-01-19 04:55:19 +01:00
|
|
|
return get_term( $term_id, $this->taxonomy );
|
|
|
|
}
|
|
|
|
}
|