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
+113
View File
@@ -0,0 +1,113 @@
<?php
/**
* Temporary CLI smoke test for 404 Enhancer. Boots WordPress and exercises the plugin.
* NOT part of the plugin. Delete after running.
*/
error_reporting( E_ALL );
ini_set( 'display_errors', '1' );
define( 'WP_USE_THEMES', false );
define( 'DOING_AJAX', true ); // Before wp-load so WPAjaxDieStopException exists.
require dirname( __DIR__, 3 ) . '/wp-load.php';
require_once ABSPATH . 'wp-admin/includes/plugin.php';
// Act as the admin user so capability checks pass in CLI.
$admin_id = 1;
if ( ! get_userdata( $admin_id ) ) {
$admin_id = (int) get_users( array( 'role' => 'administrator', 'number' => 1, 'fields' => 'ID' ) )[0];
}
wp_set_current_user( $admin_id );
$fail = 0;
function check( $label, $cond, $extra = '' ) {
global $fail;
if ( ! $cond ) {
$fail++;
}
printf( "%s %-52s %s\n", $cond ? 'PASS' : 'FAIL', $label, $extra );
}
// 1. Activation.
$already = is_plugin_active( '404-enhancer/404-enhancer.php' );
if ( ! $already ) {
$result = activate_plugin( '404-enhancer/404-enhancer.php' );
check( 'activate_plugin no WP_Error', ! is_wp_error( $result ), is_wp_error( $result ) ? $result->get_error_message() : '' );
}
check( 'plugin active', is_plugin_active( '404-enhancer/404-enhancer.php' ) );
check( 'post types option exists', is_array( get_option( 'fe_enabled_post_types' ) ), json_encode( get_option( 'fe_enabled_post_types' ) ) );
check( 'per page option exists', is_numeric( get_option( 'fe_suggestions_per_page' ) ) );
// 2. Classes loaded.
check( 'FE_Settings loaded', class_exists( 'FE_Settings' ) );
check( 'FE_Suggestions loaded', class_exists( 'FE_Suggestions' ) );
check( 'FE_Template loaded', class_exists( 'FE_Template' ) );
check( 'FE_Updater loaded', class_exists( 'FE_Updater' ) );
// 3. Options round-trip with sanitizers.
update_option( 'fe_custom_message', '<p>Hello <script>alert(1)</script>404</p>' );
check( 'message saved', get_option( 'fe_custom_message' ) !== false );
check( 'per page sanitize via register', FE_Settings::sanitize_per_page( 99 ) === 24 );
check( 'post types sanitize', FE_Settings::sanitize_post_types( array( 'post', 'evil<script>', '' ) ) === array( 'post' ) );
update_option( 'fe_enabled_post_types', array( 'post', 'page' ) );
update_option( 'fe_suggestions_per_page', 3 );
check( 'enabled types getter', FE_Settings::get_enabled_post_types() === array( 'post', 'page' ) );
check( 'per page getter', FE_Settings::get_per_page() === 3 );
// 4. Suggestions against the real DB.
$tokens = FE_Suggestions::tokenize( '/hello-world/' );
check( 'tokenize hello-world', $tokens === array( 'hello', 'world' ), json_encode( $tokens ) );
$related = FE_Suggestions::get_related( $tokens, array( 'post', 'page' ), 5 );
check( 'get_related returns array', is_array( $related ), 'count=' . count( $related ) );
foreach ( $related as $p ) {
echo ' related: [' . $p->post_type . '] ' . $p->post_title . ' -> ' . get_permalink( $p ) . "\n";
}
$random = FE_Suggestions::get_random( 'post', 3 );
check( 'get_random returns array', is_array( $random ), 'count=' . count( $random ) );
$ids = array_slice( wp_list_pluck( get_posts( array( 'post_type' => 'any', 'posts_per_page' => 5, 'fields' => 'ids' ) ), 'ID' ), 0, 3 );
$ids = array_map( 'intval', get_posts( array( 'post_type' => array( 'post', 'page' ), 'posts_per_page' => 3, 'fields' => 'ids' ) ) );
$featured = FE_Suggestions::get_featured( $ids, array( 'post', 'page' ) );
check( 'get_featured returns array', is_array( $featured ), 'ids=' . json_encode( $ids ) . ' count=' . count( $featured ) );
$did = FE_Suggestions::get_did_you_mean( 'hello-wrld', array( 'post', 'page' ) );
check( 'did_you_mean returns post|null', null === $did || $did instanceof WP_Post, null === $did ? 'null' : $did->post_name );
$cross = array();
if ( ! empty( $related ) ) {
$cross = FE_Suggestions::get_cross_links( $related[0]->ID, array( 'post', 'page' ), array( $related[0]->ID ), 3 );
check( 'get_cross_links returns array', is_array( $cross ), 'count=' . count( $cross ) );
} else {
check( 'get_cross_links skipped (no related)', true );
}
// 5. Updater no-op when unconfigured (must not fatal, must not inject).
$transient = new stdClass();
$transient->checked = array( '404-enhancer/404-enhancer.php' => '0.3' );
$transient->response = array();
$out = FE_Updater::check_for_updates( $transient );
check( 'updater no-op unconfigured', empty( $out->response ) );
// 6. Template include filter returns plugin template on 404.
$q = $GLOBALS['wp_query'];
$q->set_404();
$path = FE_Template::include_template( '/theme/404.php' );
check( 'template filter path', false !== strpos( $path, 'templates/404.php' ), $path );
$filtered = apply_filters( 'fe_404_template', '/custom/override.php' );
check( 'fe_404_template filter works', $filtered === '/custom/override.php' );
// 7. Settings page renders without fatal (admin_init never fires in CLI, so register manually;
// admin template functions are only loaded in admin context, so require them too).
require_once ABSPATH . 'wp-admin/includes/template.php';
FE_Settings::register_settings();
ob_start();
FE_Settings::render_settings_page();
$html = ob_get_clean();
check( 'settings page renders', false !== strpos( $html, 'fe_featured_image_id' ) && false !== strpos( $html, 'fe-post-search' ), 'len=' . strlen( $html ) );
// 8. AJAX handler is exercised by _cli_ajax.php in a separate process (wp_send_json* terminates).
echo $fail ? "\nRESULT: $fail FAILURES\n" : "\nRESULT: ALL PASS\n";
exit( $fail ? 1 : 0 );