From 40d5a2c23dc480cd4ad6821cad82b99c52ab58f2 Mon Sep 17 00:00:00 2001 From: Vladimir Bryzgalov Date: Tue, 21 Apr 2026 15:48:07 +0500 Subject: [PATCH] test: stateful WP shims (add_filter/get_option/is_email) for upcoming silent-drop tests --- tests/bootstrap.php | 121 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 7 deletions(-) diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6c517e0..5a90ffa 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,15 +1,12 @@ */ + public static array $options = []; + /** @var array> */ + public static array $filters = []; + + public static function reset(): void + { + self::$options = []; + self::$filters = []; + } } + +if (!function_exists('apply_filters')) { + function apply_filters($tag, $value, ...$args) { + if (!isset(Cf7stgWpShimState::$filters[$tag])) { + return $value; + } + $callbacks = Cf7stgWpShimState::$filters[$tag]; + usort($callbacks, static fn($a, $b) => $a['priority'] <=> $b['priority']); + 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); + } + return $value; + } +} + +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, + ]; + return true; + } +} + +if (!function_exists('remove_all_filters')) { + function remove_all_filters($tag, $priority = false) { + unset(Cf7stgWpShimState::$filters[$tag]); + return true; + } +} + +if (!function_exists('add_action')) { + function add_action($tag, $cb, $priority = 10, $accepted_args = 1) { + return add_filter($tag, $cb, $priority, $accepted_args); + } +} + +if (!function_exists('do_action')) { + function do_action($tag, ...$args) { + apply_filters($tag, null, ...$args); + } +} + +if (!function_exists('get_option')) { + function get_option($key, $default = false) { + return Cf7stgWpShimState::$options[$key] ?? $default; + } +} + +if (!function_exists('update_option')) { + function update_option($key, $value) { + Cf7stgWpShimState::$options[$key] = $value; + return true; + } +} + +if (!function_exists('add_option')) { + function add_option($key, $value) { + if (array_key_exists($key, Cf7stgWpShimState::$options)) { + return false; + } + Cf7stgWpShimState::$options[$key] = $value; + return true; + } +} + +if (!function_exists('delete_option')) { + function delete_option($key) { + unset(Cf7stgWpShimState::$options[$key]); + return true; + } +} + +if (!function_exists('is_email')) { + function is_email($email) { + $email = (string)$email; + if ($email === '' || !filter_var($email, FILTER_VALIDATE_EMAIL)) { + return false; + } + return $email; + } +} + +if (!function_exists('current_time')) { + function current_time($format) { + if ($format === 'mysql') return date('Y-m-d H:i:s'); + return date($format); + } +} + +if (!function_exists('wp_date')) { + function wp_date($format, $timestamp = null) { + return date($format, $timestamp ?? time()); + } +} + if (!function_exists('esc_html')) { function esc_html($s) { return htmlspecialchars((string)$s, ENT_QUOTES, 'UTF-8'); } } @@ -34,3 +138,6 @@ if (!function_exists('wp_strip_all_tags')) { if (!function_exists('__')) { function __($text) { return $text; } } +if (!function_exists('wp_json_encode')) { + function wp_json_encode($v) { return json_encode($v, JSON_UNESCAPED_UNICODE); } +}