Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consider 'fields' value when returning terms from query #2806

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Factory/TermFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ protected function from_id(int $id): ?Term
return $this->build($wp_term);
}

protected function from_wp_term_query(WP_Term_Query $query): iterable
protected function from_wp_term_query(WP_Term_Query $query)
{
return \array_map([$this, 'build'], $query->get_terms());
$terms = $query->get_terms();

$fields = $query->query_vars['fields'];
if ('all' === $fields || 'all_with_object_id' === $fields) {
return \array_map([$this, 'build'], $terms);
}

return $terms;
}

protected function from_term_object(object $obj): CoreInterface
Expand Down
129 changes: 129 additions & 0 deletions tests/test-term-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,135 @@ public function testFromTermQuery()
$this->assertTrue(MyTerm::class === get_class($res[1]));
}

public function testFromTermQueryWithFields()
{
$term_ids = $this->factory->term->create_many(4, [
'taxonomy' => 'post_tag',
]);

$post_id = $this->factory->post->create();
wp_set_object_terms(
$post_id,
$term_ids[0],
'post_tag'
);
wp_set_object_terms(
$this->factory->post->create(),
[$term_ids[1], $term_ids[2]],
'post_tag'
);

$termFactory = new TermFactory();

// all: array of used terms as Timber\Term object
$termQuery = new WP_Term_Query([
'taxonomy' => 'post_tag',
'fields' => 'all',
]);
$terms = $termFactory->from($termQuery);
$this->assertCount(3, $terms);
foreach ($terms as $term) {
$this->assertInstanceOf(Term::class, $term);
}

// all_with_object_id: all terms used in a specific object as Timber\Term object
$termQuery = new WP_Term_Query([
'taxonomy' => 'post_tag',
'fields' => 'all_with_object_id',
'object_ids' => $post_id,
]);
$terms = $termFactory->from($termQuery);
$this->assertCount(1, $terms);
$this->assertInstanceOf(Term::class, $terms[0]);
$this->assertSame($terms[0]->id, $term_ids[0]);

// register class map and repeat previous tests
$my_class_map = function (array $map) {
return array_merge($map, [
'post_tag' => MyTerm::class,
]);
};
$this->add_filter_temporarily('timber/term/classmap', $my_class_map);

// all_with_object_id: all terms used in a specific object as MyTerm object
$termQuery = new WP_Term_Query([
'taxonomy' => 'post_tag',
'fields' => 'all',
]);
$terms = $termFactory->from($termQuery);
$this->assertCount(3, $terms);
foreach ($terms as $term) {
$this->assertInstanceOf(MyTerm::class, $term);
}

// all_with_object_id: all terms used in a specific object as MyTerm object
$termQuery = new WP_Term_Query([
'taxonomy' => 'post_tag',
'fields' => 'all_with_object_id',
'object_ids' => $post_id,
]);
$terms = $termFactory->from($termQuery);
$this->assertCount(1, $terms);
$this->assertInstanceOf(MyTerm::class, $terms[0]);
$this->assertSame($terms[0]->id, $term_ids[0]);

// count: number of used terms as integer string value
$termQuery = new WP_Term_Query([
'taxonomy' => 'post_tag',
'fields' => 'count',
]);
$count = $termFactory->from($termQuery);
$this->assertTrue(is_string($count));
$this->assertTrue(is_numeric($count));
$this->assertSame(intval($count), 3);

// count: number of terms as integer string value
$termQuery = new WP_Term_Query([
'taxonomy' => 'post_tag',
'fields' => 'count',
'hide_empty' => false,
]);
$count = $termFactory->from($termQuery);
$this->assertTrue(is_string($count));
$this->assertTrue(is_numeric($count));
$this->assertSame(intval($count), 4);

// ids: array of integer ids of used terms
$termQuery = new WP_Term_Query([
'taxonomy' => 'post_tag',
'fields' => 'ids',
]);
$ids = $termFactory->from($termQuery);
$this->assertCount(3, $ids);
foreach ($ids as $id) {
$this->assertTrue(is_int($id));
$this->assertTrue(in_array($id, $term_ids));
}

// names: array of strings
$termQuery = new WP_Term_Query([
'taxonomy' => 'post_tag',
'fields' => 'names',
]);
$names = $termFactory->from($termQuery);
$this->assertCount(3, $names);
foreach ($names as $name) {
$this->assertTrue(is_string($name));
}

// id=>parent: array of numeric strings
$termQuery = new WP_Term_Query([
'taxonomy' => 'post_tag',
'fields' => 'id=>parent',
]);
$map = $termFactory->from($termQuery);
$this->assertCount(3, $map);
foreach ($map as $k => $v) {
$this->assertTrue(is_int($k));
$this->assertTrue(is_int(filter_var($v, FILTER_VALIDATE_INT)));
}
}

public function testFromAssortedArray()
{
register_taxonomy('make', 'post');
Expand Down