Skip to content

Commit

Permalink
Merge pull request #29 from nlemoine/fix/template-filter
Browse files Browse the repository at this point in the history
Fix `${type}_template` filter
  • Loading branch information
gmazzap committed Nov 30, 2022
2 parents 15564dc + 043b93c commit f4ec0ab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions src/QueryTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,14 @@ public function findTemplate(?\WP_Query $query = null, bool $filters = true): st
$found = '';
while ($types && !$found) {
$type = array_shift($types);
$found = $this->finder->findFirst($leaves[$type], (string)$type);
$filters and $found = $this->applyFilter("{$type}_template", $found, $query);
$templates = $leaves[$type];
$found = $this->finder->findFirst($templates, (string) $type);
$filters and $found = $this->applyFilter(
"{$type}_template",
$found,
$query,
[$type, $templates]
);
}

return $found;
Expand Down Expand Up @@ -121,9 +127,10 @@ public function loadTemplate(
* @param string $filter
* @param string $value
* @param \WP_Query|null $query
* @param array $moreArgs
* @return string
*/
protected function applyFilter(string $filter, string $value, ?\WP_Query $query = null): string
protected function applyFilter(string $filter, string $value, ?\WP_Query $query = null, array $moreArgs = []): string
{
/** @var array{\WP_Query, \WP_Query}|null $backup */
$backup = null;
Expand All @@ -141,7 +148,7 @@ protected function applyFilter(string $filter, string $value, ?\WP_Query $query
$wp_the_query = $query;
}

$filtered = apply_filters($filter, $value);
$filtered = apply_filters($filter, $value, ...$moreArgs);
is_string($filtered) and $value = $filtered;

if ($custom && $backup) {
Expand Down
2 changes: 1 addition & 1 deletion tests/src/Unit/QueryTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public function testApplyFilters(): void

Filters\expectApplied('foo_template')
->once()
->with('found!')
->with(...['found!', 'foo', ['foo', 'bar']])
->andReturnUsing(
static function () use ($customQuery): string {
// during filter, globals `$wp_query` and `$wp_the_query` are equal to custom
Expand Down

0 comments on commit f4ec0ab

Please sign in to comment.