'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; } ?>
' . esc_html__( 'Shown near the top of the 404 page, above the search form.', '404-enhancer' ) . '
'; } /** * 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 ) : ''; ?>' . esc_html__( 'Content from these post types is used for URL-based suggestions and random picks.', '404-enhancer' ) . '
'; } /** * 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 ''; ?>' . esc_html__( 'How many items each suggestion section on the 404 page shows.', '404-enhancer' ) . '
'; } /** * 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 ) ); } }