Initial import of 404 Enhacer

This commit is contained in:
2026-11-16 12:24:05 -05:00
commit cf03c388c3
15 changed files with 1724 additions and 0 deletions
+36
View File
@@ -0,0 +1,36 @@
<?php
define('ABSPATH', '/tmp/');
function absint($v){ return abs((int)$v); }
function sanitize_key($k){ return preg_replace('/[^a-z0-9_\-]/', '', strtolower($k)); }
function sanitize_title($t){ return strtolower(preg_replace('/[^a-z0-9]+/i', '-', trim($t))); }
function get_post_types($args=array(), $output='names'){ return array('post','page','attachment'); }
$dir = __DIR__ . '/includes/';
require $dir . 'class-fe-suggestions.php';
require $dir . 'class-fe-settings.php';
$fail = 0;
function check($label, $actual, $expected) {
global $fail;
$ok = $actual === $expected;
if (!$ok) $fail++;
printf("%s %-58s got=%s\n", $ok ? 'PASS' : 'FAIL', $label, json_encode($actual));
}
check('tokenize blog post', FE_Suggestions::tokenize('/blog/my-great-post-2024/'), array('blog','great','post','2024'));
check('tokenize strips ext+stopword', FE_Suggestions::tokenize('/products/sale-2024/index.html'), array('products','sale','2024'));
check('tokenize simple', FE_Suggestions::tokenize('/foo-bar/'), array('foo','bar'));
check('tokenize digit split', FE_Suggestions::tokenize('/top10tips/'), array('top','tips'));
check('tokenize empty', FE_Suggestions::tokenize('/'), array());
check('search term', FE_Suggestions::search_term_from_tokens(array('great','post')), 'great post');
check('slug from path', FE_Suggestions::slug_from_path('/a/b/My Page v2/'), 'my-page-v2');
check('slug from root', FE_Suggestions::slug_from_path('/'), '');
check('per_page clamp low', FE_Settings::sanitize_per_page(0), 1);
check('per_page clamp high', FE_Settings::sanitize_per_page(99), 24);
check('per_page normal', FE_Settings::sanitize_per_page('7'), 7);
check('post types filter', FE_Settings::sanitize_post_types(array('post','bogus','page','')), array('post','page'));
check('post ids sanitize', FE_Settings::sanitize_post_ids(array('5','0','abc','5','12')), array(5,12));
check('lev close', levenshtein('my-pge', 'my-page') <= max(2, (int)(strlen('my-pge')/3)), true);
check('lev far', levenshtein('totally-different', 'my-page') <= max(2, (int)(strlen('totally-different')/3)), false);
exit($fail ? 1 : 0);