Initial import of 404 Enhacer
This commit is contained in:
@@ -0,0 +1,411 @@
|
||||
<?php
|
||||
/**
|
||||
* Admin settings page for 404 Enhancer.
|
||||
*
|
||||
* @package 404_Enhancer
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the settings page, settings fields, sanitizers and admin assets.
|
||||
*/
|
||||
final class FE_Settings {
|
||||
|
||||
const OPTION_MESSAGE = 'fe_custom_message';
|
||||
const OPTION_IMAGE = 'fe_featured_image_id';
|
||||
const OPTION_POSTTYPES = 'fe_enabled_post_types';
|
||||
const OPTION_FEATURED = 'fe_featured_post_ids';
|
||||
const OPTION_PER_PAGE = 'fe_suggestions_per_page';
|
||||
|
||||
/**
|
||||
* Registers all admin hooks.
|
||||
*/
|
||||
public static function init() {
|
||||
add_action( 'admin_menu', array( __CLASS__, 'add_menu_page' ) );
|
||||
add_action( 'admin_init', array( __CLASS__, 'register_settings' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
|
||||
add_action( 'wp_ajax_fe_search_posts', array( __CLASS__, 'ajax_search_posts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the settings page under the Settings menu.
|
||||
*/
|
||||
public static function add_menu_page() {
|
||||
add_options_page(
|
||||
__( '404 Enhancer Settings', '404-enhancer' ),
|
||||
__( '404 Enhancer', '404-enhancer' ),
|
||||
'manage_options',
|
||||
'fe-settings',
|
||||
array( __CLASS__, 'render_settings_page' )
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers settings, sections and fields with sanitize callbacks.
|
||||
*/
|
||||
public static function register_settings() {
|
||||
register_setting(
|
||||
'fe_settings_group',
|
||||
self::OPTION_MESSAGE,
|
||||
array(
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'wp_kses_post',
|
||||
'default' => '',
|
||||
)
|
||||
);
|
||||
register_setting(
|
||||
'fe_settings_group',
|
||||
self::OPTION_IMAGE,
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => 'absint',
|
||||
'default' => 0,
|
||||
)
|
||||
);
|
||||
register_setting(
|
||||
'fe_settings_group',
|
||||
self::OPTION_POSTTYPES,
|
||||
array(
|
||||
'type' => 'array',
|
||||
'sanitize_callback' => array( __CLASS__, 'sanitize_post_types' ),
|
||||
'default' => array( 'post' ),
|
||||
)
|
||||
);
|
||||
register_setting(
|
||||
'fe_settings_group',
|
||||
self::OPTION_FEATURED,
|
||||
array(
|
||||
'type' => 'array',
|
||||
'sanitize_callback' => array( __CLASS__, 'sanitize_post_ids' ),
|
||||
'default' => array(),
|
||||
)
|
||||
);
|
||||
register_setting(
|
||||
'fe_settings_group',
|
||||
self::OPTION_PER_PAGE,
|
||||
array(
|
||||
'type' => 'integer',
|
||||
'sanitize_callback' => array( __CLASS__, 'sanitize_per_page' ),
|
||||
'default' => 6,
|
||||
)
|
||||
);
|
||||
|
||||
add_settings_section(
|
||||
'fe_section_display',
|
||||
__( 'Display', '404-enhancer' ),
|
||||
'__return_null',
|
||||
'fe-settings'
|
||||
);
|
||||
add_settings_field(
|
||||
self::OPTION_MESSAGE,
|
||||
__( 'Custom message', '404-enhancer' ),
|
||||
array( __CLASS__, 'field_message' ),
|
||||
'fe-settings',
|
||||
'fe_section_display'
|
||||
);
|
||||
add_settings_field(
|
||||
self::OPTION_IMAGE,
|
||||
__( 'Featured image', '404-enhancer' ),
|
||||
array( __CLASS__, 'field_image' ),
|
||||
'fe-settings',
|
||||
'fe_section_display'
|
||||
);
|
||||
|
||||
add_settings_section(
|
||||
'fe_section_content',
|
||||
__( 'Content suggestions', '404-enhancer' ),
|
||||
'__return_null',
|
||||
'fe-settings'
|
||||
);
|
||||
add_settings_field(
|
||||
self::OPTION_POSTTYPES,
|
||||
__( 'Post types to suggest', '404-enhancer' ),
|
||||
array( __CLASS__, 'field_post_types' ),
|
||||
'fe-settings',
|
||||
'fe_section_content'
|
||||
);
|
||||
add_settings_field(
|
||||
self::OPTION_FEATURED,
|
||||
__( 'Featured content', '404-enhancer' ),
|
||||
array( __CLASS__, 'field_featured_posts' ),
|
||||
'fe-settings',
|
||||
'fe_section_content'
|
||||
);
|
||||
add_settings_field(
|
||||
self::OPTION_PER_PAGE,
|
||||
__( 'Suggestions per section', '404-enhancer' ),
|
||||
array( __CLASS__, 'field_per_page' ),
|
||||
'fe-settings',
|
||||
'fe_section_content'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the enabled post types list against existing public post types.
|
||||
*
|
||||
* @param mixed $value Raw submitted value.
|
||||
* @return string[] Sanitized post type names.
|
||||
*/
|
||||
public static function sanitize_post_types( $value ) {
|
||||
$value = is_array( $value ) ? $value : array();
|
||||
$valid = get_post_types( array( 'public' => true ), 'names' );
|
||||
$sanitized = array();
|
||||
foreach ( $value as $post_type ) {
|
||||
$post_type = sanitize_key( (string) $post_type );
|
||||
if ( '' !== $post_type && in_array( $post_type, $valid, true ) ) {
|
||||
$sanitized[] = $post_type;
|
||||
}
|
||||
}
|
||||
return array_values( array_unique( $sanitized ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the featured post ID list.
|
||||
*
|
||||
* @param mixed $value Raw submitted value.
|
||||
* @return int[] Attachment/post IDs greater than zero.
|
||||
*/
|
||||
public static function sanitize_post_ids( $value ) {
|
||||
$value = is_array( $value ) ? $value : array();
|
||||
$ids = array_map( 'absint', $value );
|
||||
return array_values( array_unique( array_filter( $ids ) ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the per-section suggestion count.
|
||||
*
|
||||
* @param mixed $value Raw submitted value.
|
||||
* @return int Clamped between 1 and 24.
|
||||
*/
|
||||
public static function sanitize_per_page( $value ) {
|
||||
$value = absint( $value );
|
||||
if ( $value < 1 ) {
|
||||
$value = 1;
|
||||
}
|
||||
if ( $value > 24 ) {
|
||||
$value = 24;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues media library and admin script only on the settings page.
|
||||
*
|
||||
* @param string $hook_suffix Current admin page hook.
|
||||
*/
|
||||
public static function enqueue_assets( $hook_suffix ) {
|
||||
if ( 'settings_page_fe-settings' !== $hook_suffix ) {
|
||||
return;
|
||||
}
|
||||
wp_enqueue_media();
|
||||
wp_enqueue_script(
|
||||
'fe-admin',
|
||||
FE_PLUGIN_URL . 'assets/js/fe-admin.js',
|
||||
array( 'jquery' ),
|
||||
FE_VERSION,
|
||||
true
|
||||
);
|
||||
wp_localize_script(
|
||||
'fe-admin',
|
||||
'FEAdmin',
|
||||
array(
|
||||
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
|
||||
'nonce' => wp_create_nonce( 'fe_admin_nonce' ),
|
||||
'action' => 'fe_search_posts',
|
||||
'i18n' => array(
|
||||
'chooseImage' => __( 'Select or upload an image', '404-enhancer' ),
|
||||
'useImage' => __( 'Use this image', '404-enhancer' ),
|
||||
'searchPh' => __( 'Search posts to feature…', '404-enhancer' ),
|
||||
'noResults' => __( 'No matching published content.', '404-enhancer' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the settings page.
|
||||
*/
|
||||
public static function render_settings_page() {
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
return;
|
||||
}
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
|
||||
<form method="post" action="options.php">
|
||||
<?php
|
||||
settings_fields( 'fe_settings_group' );
|
||||
do_settings_sections( 'fe-settings' );
|
||||
submit_button();
|
||||
?>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the custom message editor field.
|
||||
*/
|
||||
public static function field_message() {
|
||||
$message = get_option( self::OPTION_MESSAGE, '' );
|
||||
wp_editor(
|
||||
$message,
|
||||
self::OPTION_MESSAGE,
|
||||
array(
|
||||
'textarea_rows' => 5,
|
||||
'media_buttons' => false,
|
||||
'teeny' => true,
|
||||
)
|
||||
);
|
||||
echo '<p class="description">' . esc_html__( 'Shown near the top of the 404 page, above the search form.', '404-enhancer' ) . '</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the featured image picker field.
|
||||
*/
|
||||
public static function field_image() {
|
||||
$image_id = absint( get_option( self::OPTION_IMAGE, 0 ) );
|
||||
$image_url = $image_id ? wp_get_attachment_url( $image_id ) : '';
|
||||
?>
|
||||
<img id="fe-featured-preview" src="<?php echo esc_url( $image_url ? $image_url : '' ); ?>" alt="" style="max-width: 300px; display: <?php echo $image_id ? 'block' : 'none'; ?>; margin-bottom: 10px;">
|
||||
<input type="hidden" id="fe_featured_image_id" name="<?php echo esc_attr( self::OPTION_IMAGE ); ?>" value="<?php echo esc_attr( $image_id ); ?>">
|
||||
<button type="button" class="button" id="fe-upload-btn"><?php esc_html_e( 'Choose image', '404-enhancer' ); ?></button>
|
||||
<button type="button" class="button" id="fe-remove-btn" style="display: <?php echo $image_id ? 'inline-block' : 'none'; ?>;"><?php esc_html_e( 'Remove', '404-enhancer' ); ?></button>
|
||||
<p class="description"><?php esc_html_e( 'Optional image shown on the 404 page.', '404-enhancer' ); ?></p>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the post type checkbox field.
|
||||
*/
|
||||
public static function field_post_types() {
|
||||
$post_types = get_post_types( array( 'public' => true ), 'objects' );
|
||||
$enabled = self::get_enabled_post_types();
|
||||
// Hidden empty value guarantees the option is submitted even when nothing is checked.
|
||||
echo '<input type="hidden" name="' . esc_attr( self::OPTION_POSTTYPES ) . '[]" value="">';
|
||||
foreach ( $post_types as $pt ) {
|
||||
printf(
|
||||
'<label style="display: block; margin-bottom: 4px;"><input type="checkbox" name="%1$s[]" value="%2$s" %3$s> %4$s</label>',
|
||||
esc_attr( self::OPTION_POSTTYPES ),
|
||||
esc_attr( $pt->name ),
|
||||
checked( in_array( $pt->name, $enabled, true ), true, false ),
|
||||
esc_html( $pt->labels->name )
|
||||
);
|
||||
}
|
||||
echo '<p class="description">' . esc_html__( 'Content from these post types is used for URL-based suggestions and random picks.', '404-enhancer' ) . '</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the featured content AJAX picker field.
|
||||
*/
|
||||
public static function field_featured_posts() {
|
||||
$featured = self::get_featured_post_ids();
|
||||
// Hidden empty value so the option is submitted when no posts are selected.
|
||||
echo '<input type="hidden" name="' . esc_attr( self::OPTION_FEATURED ) . '[]" value="">';
|
||||
?>
|
||||
<div id="fe-featured-picker">
|
||||
<ul id="fe-featured-list" style="margin: 0 0 8px;">
|
||||
<?php foreach ( $featured as $post_id ) : ?>
|
||||
<?php $post = get_post( $post_id ); ?>
|
||||
<?php if ( $post ) : ?>
|
||||
<li class="fe-chip" data-id="<?php echo esc_attr( $post_id ); ?>">
|
||||
<?php echo esc_html( get_the_title( $post ) ); ?>
|
||||
<?php printf( '<span class="fe-chip-type">%s</span>', esc_html( get_post_type_object( $post->post_type )->labels->singular_name ) ); ?>
|
||||
<input type="hidden" name="<?php echo esc_attr( self::OPTION_FEATURED ); ?>[]" value="<?php echo esc_attr( $post_id ); ?>">
|
||||
<button type="button" class="fe-chip-remove" aria-label="<?php esc_attr_e( 'Remove', '404-enhancer' ); ?>">×</button>
|
||||
</li>
|
||||
<?php endif; ?>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<input type="search" id="fe-post-search" class="regular-text" placeholder="<?php esc_attr_e( 'Search posts to feature…', '404-enhancer' ); ?>" autocomplete="off">
|
||||
<ul id="fe-search-results" style="display: none;"></ul>
|
||||
</div>
|
||||
<p class="description"><?php esc_html_e( 'Hand-picked content shown first on the 404 page, in the order selected.', '404-enhancer' ); ?></p>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the suggestions-per-section number field.
|
||||
*/
|
||||
public static function field_per_page() {
|
||||
$value = self::get_per_page();
|
||||
printf(
|
||||
'<input type="number" min="1" max="24" name="%s" value="%d">',
|
||||
esc_attr( self::OPTION_PER_PAGE ),
|
||||
esc_attr( $value )
|
||||
);
|
||||
echo '<p class="description">' . esc_html__( 'How many items each suggestion section on the 404 page shows.', '404-enhancer' ) . '</p>';
|
||||
}
|
||||
|
||||
/**
|
||||
* AJAX endpoint returning published posts matching a search string.
|
||||
*/
|
||||
public static function ajax_search_posts() {
|
||||
check_ajax_referer( 'fe_admin_nonce', 'nonce' );
|
||||
if ( ! current_user_can( 'manage_options' ) ) {
|
||||
wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
|
||||
}
|
||||
$term = isset( $_POST['s'] ) ? sanitize_text_field( wp_unslash( $_POST['s'] ) ) : '';
|
||||
$post_types = self::get_enabled_post_types();
|
||||
if ( empty( $post_types ) ) {
|
||||
$post_types = array( 'post' );
|
||||
}
|
||||
$query = new WP_Query(
|
||||
array(
|
||||
's' => $term,
|
||||
'post_type' => $post_types,
|
||||
'post_status' => 'publish',
|
||||
'posts_per_page' => 10,
|
||||
'orderby' => 'title',
|
||||
'order' => 'ASC',
|
||||
'no_found_rows' => true,
|
||||
)
|
||||
);
|
||||
$results = array();
|
||||
foreach ( $query->posts as $post ) {
|
||||
$results[] = array(
|
||||
'id' => $post->ID,
|
||||
'title' => get_the_title( $post ),
|
||||
'type' => get_post_type_object( $post->post_type )->labels->singular_name,
|
||||
'url' => get_permalink( $post ),
|
||||
);
|
||||
}
|
||||
wp_send_json_success( array( 'posts' => $results ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the enabled post types with a sensible fallback.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public static function get_enabled_post_types() {
|
||||
$enabled = get_option( self::OPTION_POSTTYPES, array( 'post' ) );
|
||||
$enabled = is_array( $enabled ) ? $enabled : array( 'post' );
|
||||
if ( empty( $enabled ) ) {
|
||||
$enabled = array( 'post' );
|
||||
}
|
||||
return $enabled;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the featured post IDs.
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
public static function get_featured_post_ids() {
|
||||
$ids = get_option( self::OPTION_FEATURED, array() );
|
||||
return is_array( $ids ) ? array_map( 'absint', $ids ) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the suggestions-per-section setting.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public static function get_per_page() {
|
||||
return self::sanitize_per_page( get_option( self::OPTION_PER_PAGE, 6 ) );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user