class-wp-unittest-factory-for-term.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * Unit test factory for terms.
  4. *
  5. * Note: The below @method notations are defined solely for the benefit of IDEs,
  6. * as a way to indicate expected return values from the given factory methods.
  7. *
  8. * @method int create( $args = array(), $generation_definitions = null )
  9. * @method WP_Term create_and_get( $args = array(), $generation_definitions = null )
  10. * @method int[] create_many( $count, $args = array(), $generation_definitions = null )
  11. */
  12. class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
  13. private $taxonomy;
  14. const DEFAULT_TAXONOMY = 'post_tag';
  15. public function __construct( $factory = null, $taxonomy = null ) {
  16. parent::__construct( $factory );
  17. $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
  18. $this->default_generation_definitions = array(
  19. 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
  20. 'taxonomy' => $this->taxonomy,
  21. 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
  22. );
  23. }
  24. /**
  25. * Creates a term object.
  26. *
  27. * @param array $args Array or string of arguments for inserting a term.
  28. *
  29. * @return array|WP_Error
  30. */
  31. public function create_object( $args ) {
  32. $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
  33. $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
  34. if ( is_wp_error( $term_id_pair ) ) {
  35. return $term_id_pair;
  36. }
  37. return $term_id_pair['term_id'];
  38. }
  39. /**
  40. * Updates the term.
  41. *
  42. * @param int|object $term The term to update.
  43. * @param array|string $fields The context in which to relate the term to the object.
  44. *
  45. * @return int The term ID.
  46. */
  47. public function update_object( $term, $fields ) {
  48. $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
  49. if ( is_object( $term ) ) {
  50. $taxonomy = $term->taxonomy;
  51. }
  52. $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
  53. return $term_id_pair['term_id'];
  54. }
  55. /**
  56. * Attach terms to the given post.
  57. *
  58. * @param int $post_id The post ID.
  59. * @param string|array $terms An array of terms to set for the post, or a string of terms
  60. * separated by commas. Hierarchical taxonomies must always pass IDs rather
  61. * than names so that children with the same names but different parents
  62. * aren't confused.
  63. * @param string $taxonomy Taxonomy name.
  64. * @param bool $append Optional. If true, don't delete existing terms, just add on. If false,
  65. * replace the terms with the new terms. Default true.
  66. *
  67. * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
  68. */
  69. public function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
  70. return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
  71. }
  72. /**
  73. * Create a term and returns it as an object.
  74. *
  75. * @param array $args Array or string of arguments for inserting a term.
  76. * @param null $generation_definitions The default values.
  77. *
  78. * @return WP_Term|WP_Error|null WP_Term on success. WP_error if taxonomy does not exist. Null for miscellaneous failure.
  79. */
  80. public function create_and_get( $args = array(), $generation_definitions = null ) {
  81. $term_id = $this->create( $args, $generation_definitions );
  82. if ( is_wp_error( $term_id ) ) {
  83. return $term_id;
  84. }
  85. $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
  86. return get_term( $term_id, $taxonomy );
  87. }
  88. /**
  89. * Retrieves the term by a given ID.
  90. *
  91. * @param int $term_id ID of the term to retrieve.
  92. *
  93. * @return WP_Term|WP_Error|null WP_Term on success. WP_error if taxonomy does not exist. Null for miscellaneous failure.
  94. */
  95. public function get_object_by_id( $term_id ) {
  96. return get_term( $term_id, $this->taxonomy );
  97. }
  98. }