diff --git a/tests/Cf7stgTestCase.php b/tests/Cf7stgTestCase.php new file mode 100644 index 0000000..e668d3f --- /dev/null +++ b/tests/Cf7stgTestCase.php @@ -0,0 +1,15 @@ + */ public static array $options = []; - /** @var array> */ + /** @var array> */ public static array $filters = []; + public static int $next_seq = 0; public static function reset(): void { self::$options = []; self::$filters = []; + self::$next_seq = 0; } } @@ -36,7 +38,7 @@ if (!function_exists('apply_filters')) { return $value; } $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) { $cb_args = array_slice(array_merge([$value], $args), 0, $f['accepted_args']); $value = call_user_func_array($f['cb'], $cb_args); @@ -48,7 +50,10 @@ if (!function_exists('apply_filters')) { if (!function_exists('add_filter')) { function add_filter($tag, $cb, $priority = 10, $accepted_args = 1) { 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; } @@ -56,7 +61,17 @@ if (!function_exists('add_filter')) { if (!function_exists('remove_all_filters')) { function remove_all_filters($tag, $priority = false) { - unset(Cf7stgWpShimState::$filters[$tag]); + if ($priority === false) { + unset(Cf7stgWpShimState::$filters[$tag]); + 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; } }