test: stateful WP shims (add_filter/get_option/is_email) for upcoming silent-drop tests
This commit is contained in:
+114
-7
@@ -1,15 +1,12 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
// Simple autoloader that mirrors the runtime plugin autoloader,
|
||||
// mapping Cf7stg\ClassName → includes/class-class-name.php
|
||||
// Autoloader (без изменений)
|
||||
spl_autoload_register(static function (string $class): void {
|
||||
if (strpos($class, 'Cf7stg\\') !== 0) {
|
||||
return;
|
||||
}
|
||||
$relative = substr($class, strlen('Cf7stg\\'));
|
||||
// Turn "PhoneParser" into "phone-parser"; also normalize underscores
|
||||
// ("Settings_Page" → "settings-page").
|
||||
$slug = strtolower(preg_replace('/([a-z0-9])([A-Z])/', '$1-$2', $relative));
|
||||
$slug = str_replace('_', '-', $slug);
|
||||
$path = __DIR__ . '/../includes/class-' . $slug . '.php';
|
||||
@@ -18,10 +15,117 @@ spl_autoload_register(static function (string $class): void {
|
||||
}
|
||||
});
|
||||
|
||||
// Minimal shims for functions used by pure-logic classes when WP is absent.
|
||||
if (!function_exists('apply_filters')) {
|
||||
function apply_filters($tag, $value) { return $value; }
|
||||
// Stateful WP shims
|
||||
final class Cf7stgWpShimState
|
||||
{
|
||||
/** @var array<string,mixed> */
|
||||
public static array $options = [];
|
||||
/** @var array<string,array<int,array{cb:callable,priority:int,accepted_args:int}>> */
|
||||
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); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user