wpGetObjectTerms.php 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. <?php
  2. /**
  3. * @group taxonomy
  4. * @covers ::wp_get_object_terms
  5. */
  6. class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase {
  7. private $taxonomy = 'wptests_tax';
  8. public function setUp() {
  9. parent::setUp();
  10. register_taxonomy( 'wptests_tax', 'post' );
  11. }
  12. public function test_get_object_terms_by_slug() {
  13. $post_id = self::factory()->post->create();
  14. $terms_1 = array( 'Foo', 'Bar', 'Baz' );
  15. $terms_1_slugs = array( 'foo', 'bar', 'baz' );
  16. // Set the initial terms.
  17. $tt_1 = wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  18. $this->assertSame( 3, count( $tt_1 ) );
  19. // Make sure they're correct.
  20. $terms = wp_get_object_terms(
  21. $post_id,
  22. $this->taxonomy,
  23. array(
  24. 'fields' => 'slugs',
  25. 'orderby' => 'term_id',
  26. )
  27. );
  28. $this->assertSame( $terms_1_slugs, $terms );
  29. }
  30. /**
  31. * @ticket 11003
  32. */
  33. public function test_should_not_filter_out_duplicate_terms_associated_with_different_objects() {
  34. $post_id1 = self::factory()->post->create();
  35. $post_id2 = self::factory()->post->create();
  36. $cat_id = self::factory()->category->create();
  37. $cat_id2 = self::factory()->category->create();
  38. wp_set_post_categories( $post_id1, array( $cat_id, $cat_id2 ) );
  39. wp_set_post_categories( $post_id2, $cat_id );
  40. $terms = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category' );
  41. $this->assertCount( 2, $terms );
  42. $this->assertSame( array( $cat_id, $cat_id2 ), wp_list_pluck( $terms, 'term_id' ) );
  43. $terms2 = wp_get_object_terms(
  44. array( $post_id1, $post_id2 ),
  45. 'category',
  46. array(
  47. 'fields' => 'all_with_object_id',
  48. )
  49. );
  50. $this->assertCount( 3, $terms2 );
  51. $this->assertSame( array( $cat_id, $cat_id, $cat_id2 ), wp_list_pluck( $terms2, 'term_id' ) );
  52. }
  53. /**
  54. * @ticket 17646
  55. */
  56. public function test_should_return_objects_with_int_properties() {
  57. $post_id = self::factory()->post->create();
  58. $term = wp_insert_term( 'one', $this->taxonomy );
  59. wp_set_object_terms( $post_id, $term, $this->taxonomy );
  60. $terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'all_with_object_id' ) );
  61. $term = array_shift( $terms );
  62. $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
  63. foreach ( $int_fields as $field ) {
  64. $this->assertInternalType( 'int', $term->$field, $field );
  65. }
  66. $terms = wp_get_object_terms( $post_id, $this->taxonomy, array( 'fields' => 'ids' ) );
  67. $term = array_shift( $terms );
  68. $this->assertInternalType( 'int', $term, 'term' );
  69. }
  70. /**
  71. * @ticket 26339
  72. */
  73. public function test_references_should_be_reset_after_wp_get_object_terms_filter() {
  74. $post_id = self::factory()->post->create();
  75. $terms_1 = array( 'foo', 'bar', 'baz' );
  76. wp_set_object_terms( $post_id, $terms_1, $this->taxonomy );
  77. add_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) );
  78. $terms = wp_get_object_terms( $post_id, $this->taxonomy );
  79. remove_filter( 'wp_get_object_terms', array( $this, 'filter_get_object_terms' ) );
  80. foreach ( $terms as $term ) {
  81. $this->assertInternalType( 'object', $term );
  82. }
  83. }
  84. /**
  85. * @ticket 40154
  86. */
  87. public function test_taxonomies_passed_to_wp_get_object_terms_filter_should_be_quoted() {
  88. register_taxonomy( 'wptests_tax', 'post' );
  89. register_taxonomy( 'wptests_tax_2', 'post' );
  90. add_filter( 'wp_get_object_terms', array( $this, 'wp_get_object_terms_callback' ), 10, 3 );
  91. $terms = wp_get_object_terms( 1, array( 'wptests_tax', 'wptests_tax_2' ) );
  92. remove_filter( 'wp_get_object_terms', array( $this, 'wp_get_object_terms_callback' ), 10, 3 );
  93. $this->assertSame( "'wptests_tax', 'wptests_tax_2'", $this->taxonomies );
  94. }
  95. public function wp_get_object_terms_callback( $terms, $object_ids, $taxonomies ) {
  96. $this->taxonomies = $taxonomies;
  97. return $terms;
  98. }
  99. public function test_orderby_name() {
  100. $p = self::factory()->post->create();
  101. $t1 = self::factory()->term->create(
  102. array(
  103. 'taxonomy' => $this->taxonomy,
  104. 'name' => 'AAA',
  105. )
  106. );
  107. $t2 = self::factory()->term->create(
  108. array(
  109. 'taxonomy' => $this->taxonomy,
  110. 'name' => 'ZZZ',
  111. )
  112. );
  113. $t3 = self::factory()->term->create(
  114. array(
  115. 'taxonomy' => $this->taxonomy,
  116. 'name' => 'JJJ',
  117. )
  118. );
  119. wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
  120. $found = wp_get_object_terms(
  121. $p,
  122. $this->taxonomy,
  123. array(
  124. 'orderby' => 'name',
  125. 'fields' => 'ids',
  126. )
  127. );
  128. $this->assertSame( array( $t1, $t3, $t2 ), $found );
  129. }
  130. public function test_orderby_count() {
  131. $posts = self::factory()->post->create_many( 3 );
  132. $t1 = self::factory()->term->create(
  133. array(
  134. 'taxonomy' => $this->taxonomy,
  135. 'name' => 'AAA',
  136. )
  137. );
  138. $t2 = self::factory()->term->create(
  139. array(
  140. 'taxonomy' => $this->taxonomy,
  141. 'name' => 'ZZZ',
  142. )
  143. );
  144. $t3 = self::factory()->term->create(
  145. array(
  146. 'taxonomy' => $this->taxonomy,
  147. 'name' => 'JJJ',
  148. )
  149. );
  150. wp_set_object_terms( $posts[0], array( $t3, $t2, $t1 ), $this->taxonomy );
  151. wp_set_object_terms( $posts[1], array( $t3, $t1 ), $this->taxonomy );
  152. wp_set_object_terms( $posts[2], array( $t3 ), $this->taxonomy );
  153. $found = wp_get_object_terms(
  154. $posts[0],
  155. $this->taxonomy,
  156. array(
  157. 'orderby' => 'count',
  158. 'fields' => 'ids',
  159. )
  160. );
  161. $this->assertSame( array( $t2, $t1, $t3 ), $found );
  162. }
  163. public function test_orderby_slug() {
  164. $p = self::factory()->post->create();
  165. $t1 = self::factory()->term->create(
  166. array(
  167. 'taxonomy' => $this->taxonomy,
  168. 'slug' => 'aaa',
  169. )
  170. );
  171. $t2 = self::factory()->term->create(
  172. array(
  173. 'taxonomy' => $this->taxonomy,
  174. 'slug' => 'zzz',
  175. )
  176. );
  177. $t3 = self::factory()->term->create(
  178. array(
  179. 'taxonomy' => $this->taxonomy,
  180. 'slug' => 'jjj',
  181. )
  182. );
  183. wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
  184. $found = wp_get_object_terms(
  185. $p,
  186. $this->taxonomy,
  187. array(
  188. 'orderby' => 'slug',
  189. 'fields' => 'ids',
  190. )
  191. );
  192. $this->assertSame( array( $t1, $t3, $t2 ), $found );
  193. }
  194. public function test_orderby_term_group() {
  195. $p = self::factory()->post->create();
  196. $t1 = self::factory()->term->create(
  197. array(
  198. 'taxonomy' => $this->taxonomy,
  199. )
  200. );
  201. $t2 = self::factory()->term->create(
  202. array(
  203. 'taxonomy' => $this->taxonomy,
  204. )
  205. );
  206. $t3 = self::factory()->term->create(
  207. array(
  208. 'taxonomy' => $this->taxonomy,
  209. )
  210. );
  211. // No great way to do this in the API.
  212. global $wpdb;
  213. $wpdb->update( $wpdb->terms, array( 'term_group' => 1 ), array( 'term_id' => $t1 ) );
  214. $wpdb->update( $wpdb->terms, array( 'term_group' => 3 ), array( 'term_id' => $t2 ) );
  215. $wpdb->update( $wpdb->terms, array( 'term_group' => 2 ), array( 'term_id' => $t3 ) );
  216. wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
  217. $found = wp_get_object_terms(
  218. $p,
  219. $this->taxonomy,
  220. array(
  221. 'orderby' => 'term_group',
  222. 'fields' => 'ids',
  223. )
  224. );
  225. $this->assertSame( array( $t1, $t3, $t2 ), $found );
  226. }
  227. public function test_orderby_term_order() {
  228. $p = self::factory()->post->create();
  229. $t1 = self::factory()->term->create(
  230. array(
  231. 'taxonomy' => $this->taxonomy,
  232. )
  233. );
  234. $t2 = self::factory()->term->create(
  235. array(
  236. 'taxonomy' => $this->taxonomy,
  237. )
  238. );
  239. $t3 = self::factory()->term->create(
  240. array(
  241. 'taxonomy' => $this->taxonomy,
  242. )
  243. );
  244. $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
  245. // No great way to do this in the API.
  246. $term_1 = get_term( $t1, $this->taxonomy );
  247. $term_2 = get_term( $t2, $this->taxonomy );
  248. $term_3 = get_term( $t3, $this->taxonomy );
  249. global $wpdb;
  250. $wpdb->update(
  251. $wpdb->term_relationships,
  252. array( 'term_order' => 1 ),
  253. array(
  254. 'term_taxonomy_id' => $term_1->term_taxonomy_id,
  255. 'object_id' => $p,
  256. )
  257. );
  258. $wpdb->update(
  259. $wpdb->term_relationships,
  260. array( 'term_order' => 3 ),
  261. array(
  262. 'term_taxonomy_id' => $term_2->term_taxonomy_id,
  263. 'object_id' => $p,
  264. )
  265. );
  266. $wpdb->update(
  267. $wpdb->term_relationships,
  268. array( 'term_order' => 2 ),
  269. array(
  270. 'term_taxonomy_id' => $term_3->term_taxonomy_id,
  271. 'object_id' => $p,
  272. )
  273. );
  274. $found = wp_get_object_terms(
  275. $p,
  276. $this->taxonomy,
  277. array(
  278. 'orderby' => 'term_order',
  279. 'fields' => 'ids',
  280. )
  281. );
  282. $this->assertSame( array( $t1, $t3, $t2 ), $found );
  283. }
  284. /**
  285. * @ticket 28688
  286. */
  287. public function test_orderby_parent() {
  288. $p = self::factory()->post->create();
  289. $t1 = self::factory()->term->create(
  290. array(
  291. 'taxonomy' => $this->taxonomy,
  292. )
  293. );
  294. $t2 = self::factory()->term->create(
  295. array(
  296. 'taxonomy' => $this->taxonomy,
  297. )
  298. );
  299. $t3 = self::factory()->term->create(
  300. array(
  301. 'taxonomy' => $this->taxonomy,
  302. )
  303. );
  304. $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
  305. $term_1 = get_term( $t1, $this->taxonomy );
  306. $term_2 = get_term( $t2, $this->taxonomy );
  307. $term_3 = get_term( $t3, $this->taxonomy );
  308. global $wpdb;
  309. $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 1 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id ) );
  310. $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 3 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) );
  311. $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 2 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) );
  312. $found = wp_get_object_terms(
  313. $p,
  314. $this->taxonomy,
  315. array(
  316. 'orderby' => 'parent',
  317. 'fields' => 'ids',
  318. )
  319. );
  320. $this->assertSame( array( $t1, $t3, $t2 ), $found );
  321. }
  322. /**
  323. * @ticket 28688
  324. */
  325. public function test_orderby_taxonomy() {
  326. register_taxonomy( 'wptests_tax_2', 'post' );
  327. register_taxonomy( 'wptests_tax_3', 'post' );
  328. $p = self::factory()->post->create();
  329. $t1 = self::factory()->term->create(
  330. array(
  331. 'taxonomy' => $this->taxonomy,
  332. )
  333. );
  334. $t2 = self::factory()->term->create(
  335. array(
  336. 'taxonomy' => 'wptests_tax_3',
  337. )
  338. );
  339. $t3 = self::factory()->term->create(
  340. array(
  341. 'taxonomy' => 'wptests_tax_2',
  342. )
  343. );
  344. wp_set_object_terms( $p, $t1, $this->taxonomy );
  345. wp_set_object_terms( $p, $t2, 'wptests_tax_3' );
  346. wp_set_object_terms( $p, $t3, 'wptests_tax_2' );
  347. $found = wp_get_object_terms(
  348. $p,
  349. array( $this->taxonomy, 'wptests_tax_2', 'wptests_tax_3' ),
  350. array(
  351. 'orderby' => 'taxonomy',
  352. 'fields' => 'ids',
  353. )
  354. );
  355. $this->assertSame( array( $t1, $t3, $t2 ), $found );
  356. }
  357. /**
  358. * @ticket 28688
  359. */
  360. public function test_orderby_tt_id() {
  361. $p = self::factory()->post->create();
  362. $t1 = self::factory()->term->create(
  363. array(
  364. 'taxonomy' => $this->taxonomy,
  365. )
  366. );
  367. $t2 = self::factory()->term->create(
  368. array(
  369. 'taxonomy' => $this->taxonomy,
  370. )
  371. );
  372. $t3 = self::factory()->term->create(
  373. array(
  374. 'taxonomy' => $this->taxonomy,
  375. )
  376. );
  377. // term_taxonomy_id will only have a different order from term_id in legacy situations.
  378. $term_1 = get_term( $t1, $this->taxonomy );
  379. $term_2 = get_term( $t2, $this->taxonomy );
  380. $term_3 = get_term( $t3, $this->taxonomy );
  381. global $wpdb;
  382. $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100004 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id ) );
  383. $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100006 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) );
  384. $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100005 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) );
  385. $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
  386. $found = wp_get_object_terms(
  387. $p,
  388. $this->taxonomy,
  389. array(
  390. 'orderby' => 'term_taxonomy_id',
  391. 'fields' => 'ids',
  392. )
  393. );
  394. $this->assertSame( array( $t1, $t3, $t2 ), $found );
  395. }
  396. public function test_order_desc() {
  397. $p = self::factory()->post->create();
  398. $t1 = self::factory()->term->create(
  399. array(
  400. 'taxonomy' => $this->taxonomy,
  401. 'name' => 'AAA',
  402. )
  403. );
  404. $t2 = self::factory()->term->create(
  405. array(
  406. 'taxonomy' => $this->taxonomy,
  407. 'name' => 'ZZZ',
  408. )
  409. );
  410. $t3 = self::factory()->term->create(
  411. array(
  412. 'taxonomy' => $this->taxonomy,
  413. 'name' => 'JJJ',
  414. )
  415. );
  416. wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
  417. $found = wp_get_object_terms(
  418. $p,
  419. $this->taxonomy,
  420. array(
  421. 'orderby' => 'name',
  422. 'order' => 'DESC',
  423. 'fields' => 'ids',
  424. )
  425. );
  426. $this->assertSame( array( $t2, $t3, $t1 ), $found );
  427. }
  428. /**
  429. * @ticket 15675
  430. */
  431. public function test_parent() {
  432. register_taxonomy(
  433. 'wptests_tax2',
  434. 'post',
  435. array(
  436. 'hierarchical' => true,
  437. )
  438. );
  439. $t1 = self::factory()->term->create(
  440. array(
  441. 'taxonomy' => 'wptests_tax2',
  442. )
  443. );
  444. $t2 = self::factory()->term->create(
  445. array(
  446. 'taxonomy' => 'wptests_tax2',
  447. )
  448. );
  449. $t3 = self::factory()->term->create(
  450. array(
  451. 'taxonomy' => 'wptests_tax2',
  452. 'parent' => $t1,
  453. )
  454. );
  455. $t4 = self::factory()->term->create(
  456. array(
  457. 'taxonomy' => 'wptests_tax2',
  458. 'parent' => $t2,
  459. )
  460. );
  461. $p = self::factory()->post->create();
  462. wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), 'wptests_tax2' );
  463. $found = wp_get_object_terms(
  464. $p,
  465. 'wptests_tax2',
  466. array(
  467. 'parent' => $t1,
  468. 'fields' => 'ids',
  469. )
  470. );
  471. $this->assertSame( array( $t3 ), $found );
  472. }
  473. /**
  474. * @ticket 15675
  475. */
  476. public function test_parent_0() {
  477. $t1 = self::factory()->term->create(
  478. array(
  479. 'taxonomy' => $this->taxonomy,
  480. )
  481. );
  482. $t2 = self::factory()->term->create(
  483. array(
  484. 'taxonomy' => $this->taxonomy,
  485. )
  486. );
  487. $t3 = self::factory()->term->create(
  488. array(
  489. 'taxonomy' => $this->taxonomy,
  490. 'parent' => $t1,
  491. )
  492. );
  493. $t4 = self::factory()->term->create(
  494. array(
  495. 'taxonomy' => $this->taxonomy,
  496. 'parent' => $t2,
  497. )
  498. );
  499. $p = self::factory()->post->create();
  500. wp_set_object_terms( $p, array( $t1, $t2, $t3, $t3 ), $this->taxonomy );
  501. $found = wp_get_object_terms(
  502. $p,
  503. $this->taxonomy,
  504. array(
  505. 'parent' => 0,
  506. 'fields' => 'ids',
  507. )
  508. );
  509. $this->assertSameSets( array( $t1, $t2 ), $found );
  510. }
  511. /**
  512. * @ticket 10142
  513. */
  514. public function test_termmeta_cache_should_be_primed_by_default() {
  515. global $wpdb;
  516. register_taxonomy( 'wptests_tax', 'post' );
  517. $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
  518. add_term_meta( $terms[0], 'foo', 'bar' );
  519. add_term_meta( $terms[1], 'foo', 'bar' );
  520. add_term_meta( $terms[2], 'foo', 'bar' );
  521. $p = self::factory()->post->create();
  522. wp_set_object_terms( $p, $terms, 'wptests_tax' );
  523. $found = wp_get_object_terms( $p, 'wptests_tax' );
  524. $num_queries = $wpdb->num_queries;
  525. foreach ( $terms as $t ) {
  526. $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
  527. }
  528. $this->assertSame( $num_queries, $wpdb->num_queries );
  529. }
  530. /**
  531. * @ticket 10142
  532. */
  533. public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_cache_is_false() {
  534. global $wpdb;
  535. register_taxonomy( 'wptests_tax', 'post' );
  536. $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
  537. add_term_meta( $terms[0], 'foo', 'bar' );
  538. add_term_meta( $terms[1], 'foo', 'bar' );
  539. add_term_meta( $terms[2], 'foo', 'bar' );
  540. $p = self::factory()->post->create();
  541. wp_set_object_terms( $p, $terms, 'wptests_tax' );
  542. $found = wp_get_object_terms(
  543. $p,
  544. 'wptests_tax',
  545. array(
  546. 'update_term_meta_cache' => false,
  547. )
  548. );
  549. $num_queries = $wpdb->num_queries;
  550. foreach ( $terms as $t ) {
  551. $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
  552. }
  553. $this->assertSame( $num_queries + 3, $wpdb->num_queries );
  554. }
  555. /**
  556. * @ticket 36932
  557. */
  558. public function test_termmeta_cache_should_be_primed_when_fields_is_all_with_object_id() {
  559. global $wpdb;
  560. register_taxonomy( 'wptests_tax', 'post' );
  561. $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
  562. add_term_meta( $terms[0], 'foo', 'bar' );
  563. add_term_meta( $terms[1], 'foo', 'bar' );
  564. add_term_meta( $terms[2], 'foo', 'bar' );
  565. $p = self::factory()->post->create();
  566. wp_set_object_terms( $p, $terms, 'wptests_tax' );
  567. $found = wp_get_object_terms(
  568. $p,
  569. 'wptests_tax',
  570. array(
  571. 'update_term_meta_cache' => true,
  572. 'fields' => 'all_with_object_id',
  573. )
  574. );
  575. $num_queries = $wpdb->num_queries;
  576. foreach ( $terms as $t ) {
  577. $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
  578. }
  579. $this->assertSame( $num_queries, $wpdb->num_queries );
  580. }
  581. /**
  582. * @ticket 36932
  583. */
  584. public function test_termmeta_cache_should_be_primed_when_fields_is_ids() {
  585. global $wpdb;
  586. register_taxonomy( 'wptests_tax', 'post' );
  587. $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
  588. add_term_meta( $terms[0], 'foo', 'bar' );
  589. add_term_meta( $terms[1], 'foo', 'bar' );
  590. add_term_meta( $terms[2], 'foo', 'bar' );
  591. $p = self::factory()->post->create();
  592. wp_set_object_terms( $p, $terms, 'wptests_tax' );
  593. $found = wp_get_object_terms(
  594. $p,
  595. 'wptests_tax',
  596. array(
  597. 'update_term_meta_cache' => true,
  598. 'fields' => 'ids',
  599. )
  600. );
  601. $num_queries = $wpdb->num_queries;
  602. foreach ( $terms as $t ) {
  603. $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
  604. }
  605. $this->assertSame( $num_queries, $wpdb->num_queries );
  606. }
  607. /**
  608. * @ticket 10142
  609. */
  610. public function test_meta_query() {
  611. register_taxonomy( 'wptests_tax', 'post' );
  612. $terms = self::factory()->term->create_many( 5, array( 'taxonomy' => 'wptests_tax' ) );
  613. add_term_meta( $terms[0], 'foo', 'bar' );
  614. add_term_meta( $terms[1], 'foo', 'bar' );
  615. add_term_meta( $terms[2], 'foo', 'baz' );
  616. add_term_meta( $terms[3], 'foob', 'ar' );
  617. $p = self::factory()->post->create();
  618. wp_set_object_terms( $p, $terms, 'wptests_tax' );
  619. $found = wp_get_object_terms(
  620. $p,
  621. 'wptests_tax',
  622. array(
  623. 'meta_query' => array(
  624. array(
  625. 'key' => 'foo',
  626. 'value' => 'bar',
  627. ),
  628. ),
  629. )
  630. );
  631. $this->assertSameSets( array( $terms[0], $terms[1] ), wp_list_pluck( $found, 'term_id' ) );
  632. }
  633. /**
  634. * @ticket 14162
  635. */
  636. public function test_should_return_wp_term_objects_for_fields_all() {
  637. register_taxonomy( 'wptests_tax', 'post' );
  638. $p = self::factory()->post->create();
  639. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  640. wp_set_object_terms( $p, $t, 'wptests_tax' );
  641. $found = wp_get_object_terms(
  642. $p,
  643. 'wptests_tax',
  644. array(
  645. 'fields' => 'all',
  646. )
  647. );
  648. $this->assertNotEmpty( $found );
  649. foreach ( $found as $f ) {
  650. $this->assertInstanceOf( 'WP_Term', $f );
  651. }
  652. }
  653. /**
  654. * @ticket 14162
  655. */
  656. public function test_should_return_wp_term_objects_for_fields_all_with_object_id() {
  657. register_taxonomy( 'wptests_tax', 'post' );
  658. $p = self::factory()->post->create();
  659. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  660. wp_set_object_terms( $p, $t, 'wptests_tax' );
  661. $found = wp_get_object_terms(
  662. $p,
  663. 'wptests_tax',
  664. array(
  665. 'fields' => 'all_with_object_id',
  666. )
  667. );
  668. $this->assertNotEmpty( $found );
  669. foreach ( $found as $f ) {
  670. $this->assertInstanceOf( 'WP_Term', $f );
  671. }
  672. }
  673. /**
  674. * @ticket 14162
  675. */
  676. public function test_should_prime_cache_for_found_terms() {
  677. global $wpdb;
  678. register_taxonomy( 'wptests_tax', 'post' );
  679. $p = self::factory()->post->create();
  680. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  681. wp_set_object_terms( $p, $t, 'wptests_tax' );
  682. $found = wp_get_object_terms(
  683. $p,
  684. 'wptests_tax',
  685. array(
  686. 'fields' => 'all_with_object_id',
  687. )
  688. );
  689. $num_queries = $wpdb->num_queries;
  690. $term = get_term( $t );
  691. $this->assertSame( $num_queries, $wpdb->num_queries );
  692. }
  693. /**
  694. * @ticket 14162
  695. */
  696. public function test_object_id_should_not_be_cached_with_term_object() {
  697. register_taxonomy( 'wptests_tax', 'post' );
  698. $p = self::factory()->post->create();
  699. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  700. wp_set_object_terms( $p, $t, 'wptests_tax' );
  701. $found = wp_get_object_terms(
  702. $p,
  703. 'wptests_tax',
  704. array(
  705. 'fields' => 'all_with_object_id',
  706. )
  707. );
  708. foreach ( $found as $f ) {
  709. $this->assertSame( $p, $f->object_id );
  710. }
  711. $term = get_term( $t );
  712. $this->assertFalse( isset( $term->object_id ) );
  713. }
  714. /**
  715. * @ticket 14162
  716. */
  717. public function test_term_cache_should_be_primed_for_all_taxonomies() {
  718. global $wpdb;
  719. register_taxonomy( 'wptests_tax1', 'post' );
  720. register_taxonomy( 'wptests_tax2', 'post' );
  721. $p = self::factory()->post->create();
  722. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
  723. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
  724. wp_set_object_terms( $p, $t1, 'wptests_tax1' );
  725. wp_set_object_terms( $p, $t2, 'wptests_tax2' );
  726. $found = wp_get_object_terms(
  727. $p,
  728. array(
  729. 'wptests_tax1',
  730. 'wptests_tax2',
  731. ),
  732. array(
  733. 'fields' => 'all_with_object_id',
  734. )
  735. );
  736. $this->assertSameSets( array( $t1, $t2 ), wp_list_pluck( $found, 'term_id' ) );
  737. $num_queries = $wpdb->num_queries;
  738. $term1 = get_term( $t1 );
  739. $term2 = get_term( $t2 );
  740. $this->assertSame( $num_queries, $wpdb->num_queries );
  741. }
  742. /**
  743. * @ticket 14162
  744. */
  745. public function test_object_id_should_be_set_on_objects_that_share_terms() {
  746. register_taxonomy( 'wptests_tax', 'post' );
  747. $posts = self::factory()->post->create_many( 2 );
  748. $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax' ) );
  749. wp_set_object_terms( $posts[0], $t, 'wptests_tax' );
  750. wp_set_object_terms( $posts[1], $t, 'wptests_tax' );
  751. $found = wp_get_object_terms(
  752. $posts,
  753. 'wptests_tax',
  754. array(
  755. 'fields' => 'all_with_object_id',
  756. )
  757. );
  758. $this->assertSameSets( $posts, wp_list_pluck( $found, 'object_id' ) );
  759. }
  760. public function filter_get_object_terms( $terms ) {
  761. $term_ids = wp_list_pluck( $terms, 'term_id' );
  762. // All terms should still be objects.
  763. return $terms;
  764. }
  765. public function test_verify_args_parameter_can_be_string() {
  766. $p = self::factory()->post->create();
  767. $t1 = self::factory()->term->create(
  768. array(
  769. 'taxonomy' => $this->taxonomy,
  770. 'name' => 'AAA',
  771. )
  772. );
  773. $t2 = self::factory()->term->create(
  774. array(
  775. 'taxonomy' => $this->taxonomy,
  776. 'name' => 'ZZZ',
  777. )
  778. );
  779. $t3 = self::factory()->term->create(
  780. array(
  781. 'taxonomy' => $this->taxonomy,
  782. 'name' => 'JJJ',
  783. )
  784. );
  785. wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
  786. $found = wp_get_object_terms( $p, $this->taxonomy, 'orderby=name&fields=ids' );
  787. $this->assertSame( array( $t1, $t3, $t2 ), $found );
  788. }
  789. /**
  790. * @ticket 35925
  791. */
  792. public function test_wp_get_object_terms_args_filter() {
  793. $taxonomy = 'wptests_tax_4';
  794. register_taxonomy( $taxonomy, 'post', array( 'sort' => 'true' ) );
  795. $post_id = self::factory()->post->create();
  796. $terms = array( 'foo', 'bar', 'baz' );
  797. $set = wp_set_object_terms( $post_id, $terms, $taxonomy );
  798. // Filter for maintaining term order.
  799. add_filter( 'wp_get_object_terms_args', array( $this, 'filter_wp_get_object_terms_args' ), 10, 3 );
  800. // Test directly.
  801. $get_object_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'names' ) );
  802. $this->assertSame( $terms, $get_object_terms );
  803. // Test metabox taxonomy (admin advanced edit).
  804. $terms_to_edit = get_terms_to_edit( $post_id, $taxonomy );
  805. $this->assertSame( implode( ',', $terms ), $terms_to_edit );
  806. }
  807. function filter_wp_get_object_terms_args( $args, $object_ids, $taxonomies ) {
  808. $args['orderby'] = 'term_order';
  809. return $args;
  810. }
  811. /**
  812. * @ticket 41010
  813. */
  814. public function test_duplicate_terms_should_not_be_returned_when_passed_multiple_taxonomies_registered_with_args_array() {
  815. $taxonomy1 = 'wptests_tax';
  816. $taxonomy2 = 'wptests_tax_2';
  817. // Any non-empty 'args' array triggers the bug.
  818. $taxonomy_arguments = array(
  819. 'args' => array( 0 ),
  820. );
  821. register_taxonomy( $taxonomy1, 'post', $taxonomy_arguments );
  822. register_taxonomy( $taxonomy2, 'post', $taxonomy_arguments );
  823. $post_id = self::factory()->post->create();
  824. $term_1_id = self::factory()->term->create(
  825. array(
  826. 'taxonomy' => $taxonomy1,
  827. )
  828. );
  829. $term_2_id = self::factory()->term->create(
  830. array(
  831. 'taxonomy' => $taxonomy2,
  832. )
  833. );
  834. wp_set_object_terms( $post_id, $term_1_id, $taxonomy1 );
  835. wp_set_object_terms( $post_id, $term_2_id, $taxonomy2 );
  836. $expected = array( $term_1_id, $term_2_id );
  837. $actual = wp_get_object_terms(
  838. $post_id,
  839. array( $taxonomy1, $taxonomy2 ),
  840. array(
  841. 'orderby' => 'term_id',
  842. 'fields' => 'ids',
  843. )
  844. );
  845. $this->assertSameSets( $expected, $actual );
  846. }
  847. }