test: stable filter ordering, base TestCase reset, priority-aware remove_all_filters
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Cf7stg\Tests;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
abstract class Cf7stgTestCase extends TestCase
|
||||||
|
{
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
\Cf7stgWpShimState::reset();
|
||||||
|
}
|
||||||
|
}
|
||||||
+18
-3
@@ -20,13 +20,15 @@ final class Cf7stgWpShimState
|
|||||||
{
|
{
|
||||||
/** @var array<string,mixed> */
|
/** @var array<string,mixed> */
|
||||||
public static array $options = [];
|
public static array $options = [];
|
||||||
/** @var array<string,array<int,array{cb:callable,priority:int,accepted_args:int}>> */
|
/** @var array<string,array<int,array{cb:callable,priority:int,accepted_args:int,seq:int}>> */
|
||||||
public static array $filters = [];
|
public static array $filters = [];
|
||||||
|
public static int $next_seq = 0;
|
||||||
|
|
||||||
public static function reset(): void
|
public static function reset(): void
|
||||||
{
|
{
|
||||||
self::$options = [];
|
self::$options = [];
|
||||||
self::$filters = [];
|
self::$filters = [];
|
||||||
|
self::$next_seq = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +38,7 @@ if (!function_exists('apply_filters')) {
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
$callbacks = Cf7stgWpShimState::$filters[$tag];
|
$callbacks = Cf7stgWpShimState::$filters[$tag];
|
||||||
usort($callbacks, static fn($a, $b) => $a['priority'] <=> $b['priority']);
|
usort($callbacks, static fn($a, $b) => ($a['priority'] <=> $b['priority']) ?: ($a['seq'] <=> $b['seq']));
|
||||||
foreach ($callbacks as $f) {
|
foreach ($callbacks as $f) {
|
||||||
$cb_args = array_slice(array_merge([$value], $args), 0, $f['accepted_args']);
|
$cb_args = array_slice(array_merge([$value], $args), 0, $f['accepted_args']);
|
||||||
$value = call_user_func_array($f['cb'], $cb_args);
|
$value = call_user_func_array($f['cb'], $cb_args);
|
||||||
@@ -48,7 +50,10 @@ if (!function_exists('apply_filters')) {
|
|||||||
if (!function_exists('add_filter')) {
|
if (!function_exists('add_filter')) {
|
||||||
function add_filter($tag, $cb, $priority = 10, $accepted_args = 1) {
|
function add_filter($tag, $cb, $priority = 10, $accepted_args = 1) {
|
||||||
Cf7stgWpShimState::$filters[$tag][] = [
|
Cf7stgWpShimState::$filters[$tag][] = [
|
||||||
'cb' => $cb, 'priority' => (int)$priority, 'accepted_args' => (int)$accepted_args,
|
'cb' => $cb,
|
||||||
|
'priority' => (int)$priority,
|
||||||
|
'accepted_args' => (int)$accepted_args,
|
||||||
|
'seq' => Cf7stgWpShimState::$next_seq++,
|
||||||
];
|
];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -56,9 +61,19 @@ if (!function_exists('add_filter')) {
|
|||||||
|
|
||||||
if (!function_exists('remove_all_filters')) {
|
if (!function_exists('remove_all_filters')) {
|
||||||
function remove_all_filters($tag, $priority = false) {
|
function remove_all_filters($tag, $priority = false) {
|
||||||
|
if ($priority === false) {
|
||||||
unset(Cf7stgWpShimState::$filters[$tag]);
|
unset(Cf7stgWpShimState::$filters[$tag]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
if (!isset(Cf7stgWpShimState::$filters[$tag])) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
Cf7stgWpShimState::$filters[$tag] = array_values(array_filter(
|
||||||
|
Cf7stgWpShimState::$filters[$tag],
|
||||||
|
static fn($f) => $f['priority'] !== (int)$priority
|
||||||
|
));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!function_exists('add_action')) {
|
if (!function_exists('add_action')) {
|
||||||
|
|||||||
Reference in New Issue
Block a user