'', ]; $GLOBALS['jio_test_set_permalink_calls'] = []; $GLOBALS['jio_test_flush_calls'] = []; $GLOBALS['jio_test_hooks'] = []; } namespace { function jio_test_reset(array $options): void { $GLOBALS['jio_test_options'] = $options; $GLOBALS['jio_test_set_permalink_calls'] = []; $GLOBALS['jio_test_flush_calls'] = []; } function get_option(string $key, $default = false) { return array_key_exists($key, $GLOBALS['jio_test_options']) ? $GLOBALS['jio_test_options'][$key] : $default; } function update_option(string $key, $value): bool { $GLOBALS['jio_test_options'][$key] = $value; return true; } function delete_option(string $key): bool { unset($GLOBALS['jio_test_options'][$key]); return true; } function add_action(string $hook, $callback, int $priority = 10, int $args = 1): void { $GLOBALS['jio_test_hooks'][] = [ 'hook' => $hook, 'priority' => $priority, 'method' => is_array($callback) ? (string) $callback[1] : (string) $callback, ]; } function flush_rewrite_rules(bool $hard = true): void { $GLOBALS['jio_test_flush_calls'][] = $hard; } } namespace { if (!class_exists('WP_Rewrite')) { final class WP_Rewrite { public array $structures = []; public function set_permalink_structure(string $structure): void { $this->structures[] = $structure; // Core persists the option and refreshes in-memory state; the stub mirrors the // persistence half so assertions on the option bag stay meaningful. $GLOBALS['jio_test_options']['permalink_structure'] = $structure; } } } } namespace { require_once dirname(__DIR__, 2) . '/src/Core/RewriteBootstrap.php'; $bootstrap = \Jio\Core\RewriteBootstrap::get_instance(); $bootstrap->init(); $result = [ 'hooks' => $GLOBALS['jio_test_hooks'], ]; // --- Case 1: fresh install, no permalink structure chosen yet. jio_test_reset(['permalink_structure' => '']); $wp_rewrite = new WP_Rewrite(); $GLOBALS['wp_rewrite'] = $wp_rewrite; $bootstrap->ensure_permalink_structure(); $result['fresh_install'] = [ 'option' => get_option('permalink_structure'), 'set_permalink' => $wp_rewrite->structures, ]; // --- Case 2: administrator already chose a structure — must not be overridden. jio_test_reset(['permalink_structure' => '/%category%/%postname%.html']); $wp_rewrite = new WP_Rewrite(); $GLOBALS['wp_rewrite'] = $wp_rewrite; $bootstrap->ensure_permalink_structure(); $result['chosen_structure'] = [ 'option' => get_option('permalink_structure'), 'set_permalink' => $wp_rewrite->structures, ]; // --- Case 3: version bump flushes once, then is a no-op. jio_test_reset(['permalink_structure' => '/%postname%/']); $bootstrap->flush_when_version_changes(); $first_flush = count($GLOBALS['jio_test_flush_calls']); $stamp = get_option('jio_rewrite_flushed_version'); $bootstrap->flush_when_version_changes(); $result['version_flush'] = [ 'flush_count' => $first_flush, 'stamp' => $stamp, 'repeat_flush' => count($GLOBALS['jio_test_flush_calls']) - $first_flush, ]; // --- Case 4: activation flush. $bootstrap->flush_on_activate(); $result['activate'] = [ 'flush_count' => count($GLOBALS['jio_test_flush_calls']), ]; echo json_encode($result, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT); echo PHP_EOL; }