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
+148
View File
@@ -0,0 +1,148 @@
<?php
/**
* Custom 404 template for 404 Enhancer.
*
* All dynamic output is escaped. Sections render only when they have content.
*
* @package 404_Enhancer
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
get_header();
$path = FE_Suggestions::get_requested_path();
$tokens = FE_Suggestions::tokenize( $path );
$slug = FE_Suggestions::slug_from_path( $path );
$search = FE_Suggestions::search_term_from_tokens( $tokens );
if ( '' === $search && '' !== $slug ) {
$search = str_replace( '-', ' ', $slug );
}
$post_types = FE_Settings::get_enabled_post_types();
$per_page = FE_Settings::get_per_page();
$message = get_option( FE_Settings::OPTION_MESSAGE, '' );
$image_id = absint( get_option( FE_Settings::OPTION_IMAGE, 0 ) );
$image_url = $image_id ? wp_get_attachment_url( $image_id ) : '';
$featured = FE_Suggestions::get_featured( FE_Settings::get_featured_post_ids(), $post_types );
$related = FE_Suggestions::get_related( $tokens, $post_types, $per_page );
$related_ids = wp_list_pluck( $related, 'ID' );
$did_you_mean = FE_Suggestions::get_did_you_mean( $slug, $post_types );
$cross_links = array();
if ( ! empty( $related ) ) {
$cross_links = FE_Suggestions::get_cross_links( $related[0]->ID, $post_types, $related_ids, $per_page );
}
$random = array();
foreach ( $post_types as $pt ) {
$random = array_merge( $random, FE_Suggestions::get_random( $pt, 3 ) );
}
$random = array_slice( $random, 0, $per_page );
$render_card = function ( $post ) {
setup_postdata( $post );
?>
<li class="fe-card">
<a class="fe-card-link" href="<?php echo esc_url( get_permalink( $post ) ); ?>">
<?php if ( has_post_thumbnail( $post ) ) : ?>
<span class="fe-card-thumb"><?php echo get_the_post_thumbnail( $post, 'medium', array( 'alt' => '' ) ); ?></span>
<?php endif; ?>
<span class="fe-card-body">
<span class="fe-card-title"><?php echo esc_html( get_the_title( $post ) ); ?></span>
<span class="fe-card-excerpt"><?php echo esc_html( wp_trim_words( get_the_excerpt( $post ), 18 ) ); ?></span>
</span>
</a>
</li>
<?php
};
$render_section = function ( $title, $posts ) use ( $render_card ) {
if ( empty( $posts ) ) {
return;
}
?>
<section class="fe-section">
<h2 class="fe-section-title"><?php echo esc_html( $title ); ?></h2>
<ul class="fe-card-grid">
<?php foreach ( $posts as $post ) : ?>
<?php $render_card( $post ); ?>
<?php endforeach; ?>
</ul>
</section>
<?php
};
/**
* Filters the 404 heading.
*
* @param string $title Default heading.
*/
$heading = apply_filters( 'fe_404_heading', __( 'Oops! That page can’t be found.', '404-enhancer' ) );
?>
<div class="fe-404">
<div class="fe-404-hero">
<?php if ( $image_url ) : ?>
<div class="fe-404-hero-image">
<img src="<?php echo esc_url( $image_url ); ?>" alt="<?php esc_attr_e( 'Illustration for the missing page', '404-enhancer' ); ?>">
</div>
<?php endif; ?>
<div class="fe-404-hero-text">
<h1 class="fe-404-heading"><?php echo esc_html( $heading ); ?></h1>
<?php if ( '' !== $path && '/' !== $path ) : ?>
<p class="fe-404-requested">
<?php
printf(
/* translators: %s: requested URL path. */
esc_html__( 'You requested %s, which doesn’t exist (anymore).', '404-enhancer' ),
'<code>' . esc_html( $path ) . '</code>'
);
?>
</p>
<?php endif; ?>
<?php if ( '' !== trim( (string) $message ) ) : ?>
<div class="fe-404-message"><?php echo wp_kses_post( $message ); ?></div>
<?php endif; ?>
<form role="search" method="get" class="fe-search-form" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<label class="fe-sr-only" for="fe-search-input"><?php esc_html_e( 'Search the site', '404-enhancer' ); ?></label>
<input type="search" id="fe-search-input" name="s" value="<?php echo esc_attr( $search ); ?>" placeholder="<?php esc_attr_e( 'Search the site…', '404-enhancer' ); ?>">
<button type="submit"><?php esc_html_e( 'Search', '404-enhancer' ); ?></button>
</form>
<?php if ( $did_you_mean ) : ?>
<p class="fe-did-you-mean">
<?php
printf(
/* translators: %s: linked title of the suggested post. */
esc_html__( 'Did you mean %s?', '404-enhancer' ),
'<a href="' . esc_url( get_permalink( $did_you_mean ) ) . '">' . esc_html( get_the_title( $did_you_mean ) ) . '</a>'
);
?>
</p>
<?php endif; ?>
</div>
</div>
<?php
$render_section( __( 'Featured', '404-enhancer' ), $featured );
if ( '' !== $search ) {
$render_section(
sprintf(
/* translators: %s: search term derived from the requested URL. */
__( 'Related to “%s”', '404-enhancer' ),
$search
),
$related
);
}
$render_section( __( 'You might also like', '404-enhancer' ), $cross_links );
$render_section( __( 'While you’re here', '404-enhancer' ), $random );
?>
</div>
<?php
wp_reset_postdata();
get_footer();