diff --git a/src/QueryTemplate.php b/src/QueryTemplate.php index 7127dd8..59c5b45 100644 --- a/src/QueryTemplate.php +++ b/src/QueryTemplate.php @@ -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; @@ -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; @@ -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) { diff --git a/tests/src/Unit/QueryTemplateTest.php b/tests/src/Unit/QueryTemplateTest.php index 7a0ae00..7224501 100644 --- a/tests/src/Unit/QueryTemplateTest.php +++ b/tests/src/Unit/QueryTemplateTest.php @@ -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