*/ public static function fields() { return array( 'afs_general_post_type' => array( 'section' => self::SECTION_GENERAL, 'type' => 'post_type', 'label' => __( 'Post type', 'ajax-filter-search' ), 'help' => __( 'Select a post type to pull the feed from.', 'ajax-filter-search' ), 'default' => 'post', ), 'afs_general_post_taxonomy' => array( 'section' => self::SECTION_GENERAL, 'type' => 'taxonomy', 'label' => __( 'Taxonomy', 'ajax-filter-search' ), 'help' => __( 'Select a taxonomy for the tabbed filters.', 'ajax-filter-search' ), 'default' => 'category', ), 'afs_general_posts_per_page' => array( 'section' => self::SECTION_GENERAL, 'type' => 'number', 'label' => __( 'Posts per page', 'ajax-filter-search' ), 'help' => __( 'How many posts to display per page (1 to 100).', 'ajax-filter-search' ), 'default' => 10, ), 'afs_general_show_filters' => array( 'section' => self::SECTION_GENERAL, 'type' => 'yesno', 'label' => __( 'Category tabs', 'ajax-filter-search' ), 'help' => __( 'Display tabbed filters.', 'ajax-filter-search' ), 'default' => 1, ), 'afs_general_views' => array( 'section' => self::SECTION_GENERAL, 'type' => 'yesno', 'label' => __( 'Layout switcher', 'ajax-filter-search' ), 'help' => __( 'Show the List View / Grid View switcher.', 'ajax-filter-search' ), 'default' => 1, ), 'afs_general_layout' => array( 'section' => self::SECTION_GENERAL, 'type' => 'layout', 'label' => __( 'Default layout', 'ajax-filter-search' ), 'help' => __( 'The layout used when a page first loads.', 'ajax-filter-search' ), 'default' => 'list', ), 'afs_general_show_sort' => array( 'section' => self::SECTION_GENERAL, 'type' => 'yesno', 'label' => __( 'Sort control', 'ajax-filter-search' ), 'help' => __( 'Let visitors sort by newest, oldest, A to Z, recently updated or most commented. Each feed can override this.', 'ajax-filter-search' ), 'default' => 0, ), 'afs_general_show_chips' => array( 'section' => self::SECTION_GENERAL, 'type' => 'yesno', 'label' => __( 'Active filter chips', 'ajax-filter-search' ), 'help' => __( 'Show the active filters as removable chips with a Clear all link, and offer a way out when nothing matches. Each feed can override this.', 'ajax-filter-search' ), 'default' => 0, ), 'afs_general_noindex_filtered' => array( 'section' => self::SECTION_GENERAL, 'type' => 'yesno', 'label' => __( 'Hide filtered views from search engines', 'ajax-filter-search' ), 'help' => __( 'Adds noindex to a page when it is shown with a filter, search or page number in the address, so only the main page is indexed.', 'ajax-filter-search' ), 'default' => 1, ), 'afs_appearance_design' => array( 'section' => self::SECTION_APPEARANCE, 'type' => 'design', 'label' => __( 'Design', 'ajax-filter-search' ), 'help' => __( 'How feeds look. Editorial Grid and Resource Library show image cards or compact rows; what they show is set under Cards. You can also choose a design for a single feed with the design attribute or the block setting.', 'ajax-filter-search' ), 'default' => AFS_Design::site_default(), ), 'afs_cards_show_image' => array( 'section' => self::SECTION_CARDS, 'type' => 'yesno', 'label' => __( 'Featured image', 'ajax-filter-search' ), 'help' => __( 'Show each post\'s featured image. A post without one gets a tinted tile with a letter.', 'ajax-filter-search' ), 'default' => 1, ), 'afs_cards_image_ratio' => array( 'section' => self::SECTION_CARDS, 'type' => 'ratio', 'label' => __( 'Image shape', 'ajax-filter-search' ), 'help' => __( 'Editorial Grid images only. Resource Library thumbnails are always square.', 'ajax-filter-search' ), 'default' => '16:10', 'needs' => 'afs_cards_show_image', ), 'afs_cards_columns' => array( 'section' => self::SECTION_CARDS, 'type' => 'columns', 'label' => __( 'Columns', 'ajax-filter-search' ), 'help' => __( 'The most columns the Editorial Grid uses. Fewer on narrow screens.', 'ajax-filter-search' ), 'default' => 3, ), 'afs_cards_show_excerpt' => array( 'section' => self::SECTION_CARDS, 'type' => 'yesno', 'label' => __( 'Excerpt', 'ajax-filter-search' ), 'help' => __( 'Show a short excerpt under each title.', 'ajax-filter-search' ), 'default' => 1, ), 'afs_cards_excerpt_length' => array( 'section' => self::SECTION_CARDS, 'type' => 'words', 'label' => __( 'Excerpt length', 'ajax-filter-search' ), 'help' => __( 'How many words of the excerpt to show (10 to 80).', 'ajax-filter-search' ), 'default' => 25, 'needs' => 'afs_cards_show_excerpt', ), 'afs_cards_show_date' => array( 'section' => self::SECTION_CARDS, 'type' => 'yesno', 'label' => __( 'Publication date', 'ajax-filter-search' ), 'help' => __( 'Show the date each post was published.', 'ajax-filter-search' ), 'default' => 1, ), 'afs_cards_show_time' => array( 'section' => self::SECTION_CARDS, 'type' => 'yesno', 'label' => __( 'Include the time', 'ajax-filter-search' ), 'help' => __( 'Show the time of day next to the date. It needs the publication date to be shown.', 'ajax-filter-search' ), 'default' => 0, 'needs' => 'afs_cards_show_date', ), 'afs_cards_show_category' => array( 'section' => self::SECTION_CARDS, 'type' => 'yesno', 'label' => __( 'Category badge', 'ajax-filter-search' ), 'help' => __( 'Show the post\'s category as a badge, using the taxonomy chosen above.', 'ajax-filter-search' ), 'default' => 1, ), 'afs_appearance_accent' => array( 'section' => self::SECTION_APPEARANCE, 'type' => 'color', 'label' => __( 'Accent color', 'ajax-filter-search' ), 'help' => __( 'Used for links, the active tab and the active page.', 'ajax-filter-search' ), 'default' => AFS_Design::default_accent(), ), ); } /** Defaults keyed by option key. */ public static function defaults() { $defaults = array(); foreach ( self::fields() as $key => $field ) { $defaults[ $key ] = $field['default']; } return $defaults; } /** * Retrieve a setting using the short 1.x key, e.g. '_general_post_type'. * * @param string $val Key suffix. * @return mixed */ public static function afs_retrieve( $val ) { return self::afs_get_option( AFS_SUB . $val ); } /** * Retrieve a setting by its full option key, falling back to the default. * * @param string $option_name Full key, e.g. 'afs_general_post_type'. * @return mixed False when the key is unknown. */ public static function afs_get_option( $option_name = '' ) { $defaults = self::defaults(); $options = get_option( AFS_SETTINGS, array() ); $options = wp_parse_args( is_array( $options ) ? $options : array(), $defaults ); return isset( $options[ $option_name ] ) ? $options[ $option_name ] : false; } /** Hook everything up. */ public static function init() { add_action( 'admin_menu', array( __CLASS__, 'add_admin_menu' ) ); add_action( 'admin_init', array( __CLASS__, 'register_settings' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_admin_scripts' ) ); add_filter( 'plugin_action_links_' . AFS_PLUGIN_FILE, array( __CLASS__, 'plugin_settings_link' ) ); } public static function add_admin_menu() { add_options_page( AFS_PAGE_TITLE, AFS_MENU_TAB_TITLE, AFS_CAPABILITIES, AFS_MENU_SLUG, array( __CLASS__, 'options_page' ) ); } public static function plugin_settings_link( $links ) { $url = admin_url( 'options-general.php?page=' . AFS_MENU_SLUG ); array_unshift( $links, '' . esc_html__( 'Settings', 'ajax-filter-search' ) . '' ); return $links; } public static function register_settings() { register_setting( 'afs_settings_group', AFS_SETTINGS, array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'default' => self::defaults(), ) ); add_settings_section( self::SECTION_GENERAL, __( 'General', 'ajax-filter-search' ), array( __CLASS__, 'general_section_intro' ), 'afs_settings_page' ); add_settings_section( self::SECTION_APPEARANCE, __( 'Appearance', 'ajax-filter-search' ), '__return_null', 'afs_settings_page' ); add_settings_section( self::SECTION_CARDS, __( 'Cards', 'ajax-filter-search' ), '__return_null', 'afs_settings_page' ); foreach ( self::fields() as $key => $field ) { add_settings_field( $key, $field['label'], array( __CLASS__, 'render_field' ), 'afs_settings_page', $field['section'], array_merge( $field, array( 'key' => $key, 'label_for' => 'afs-' . $key, ) ) ); } } public static function general_section_intro() { echo '

' . wp_kses( __( 'Customize your settings below, then add the shortcode [ajax_filter_search] (or the "Ajax Filter Search" block) to the page where you want the feed to appear.', 'ajax-filter-search' ), array( 'code' => array() ) ) . '

'; } /** * Validate everything on save. Unknown keys are dropped. * * @param mixed $input Raw submitted value. * @return array */ public static function sanitize( $input ) { $input = is_array( $input ) ? wp_unslash( $input ) : array(); $out = self::defaults(); $post_types = self::viewable_post_types(); $post_type = isset( $input['afs_general_post_type'] ) ? sanitize_key( $input['afs_general_post_type'] ) : 'post'; if ( ! isset( $post_types[ $post_type ] ) ) { $post_type = 'post'; } $out['afs_general_post_type'] = $post_type; $taxonomy = isset( $input['afs_general_post_taxonomy'] ) ? sanitize_key( $input['afs_general_post_taxonomy'] ) : 'none'; if ( 'none' !== $taxonomy && ! is_object_in_taxonomy( $post_type, $taxonomy ) ) { $taxonomy = 'none'; } $out['afs_general_post_taxonomy'] = $taxonomy; $per_page = isset( $input['afs_general_posts_per_page'] ) ? absint( $input['afs_general_posts_per_page'] ) : 10; $out['afs_general_posts_per_page'] = min( 100, max( 1, $per_page ) ); foreach ( array( 'afs_general_show_filters', 'afs_general_views', 'afs_general_noindex_filtered', 'afs_general_show_sort', 'afs_general_show_chips' ) as $key ) { $out[ $key ] = ( isset( $input[ $key ] ) && '0' !== (string) $input[ $key ] ) ? 1 : 0; } $layout = isset( $input['afs_general_layout'] ) ? sanitize_key( $input['afs_general_layout'] ) : 'list'; $out['afs_general_layout'] = in_array( $layout, array( 'list', 'grid' ), true ) ? $layout : 'list'; $design = isset( $input['afs_appearance_design'] ) ? sanitize_key( $input['afs_appearance_design'] ) : ''; $out['afs_appearance_design'] = AFS_Design::is_valid( $design ) ? $design : AFS_Design::site_default(); foreach ( array( 'afs_cards_show_image', 'afs_cards_show_excerpt', 'afs_cards_show_date', 'afs_cards_show_time', 'afs_cards_show_category' ) as $key ) { $out[ $key ] = ( isset( $input[ $key ] ) && '0' !== (string) $input[ $key ] ) ? 1 : 0; } $ratio = isset( $input['afs_cards_image_ratio'] ) ? AFS_Card::clean_ratio( $input['afs_cards_image_ratio'] ) : ''; $out['afs_cards_image_ratio'] = '' === $ratio ? '16:10' : $ratio; $columns = isset( $input['afs_cards_columns'] ) ? absint( $input['afs_cards_columns'] ) : 3; $out['afs_cards_columns'] = min( 4, max( 1, $columns ? $columns : 3 ) ); $words = isset( $input['afs_cards_excerpt_length'] ) ? absint( $input['afs_cards_excerpt_length'] ) : 25; $out['afs_cards_excerpt_length'] = min( 80, max( 10, $words ? $words : 25 ) ); $color = isset( $input['afs_appearance_accent'] ) ? sanitize_hex_color( $input['afs_appearance_accent'] ) : ''; $out['afs_appearance_accent'] = $color ? $color : AFS_Design::default_accent(); return $out; } /** Public, viewable post types as name => object. */ public static function viewable_post_types() { $types = get_post_types( array( 'public' => true ), 'objects' ); unset( $types['attachment'] ); return array_filter( $types, 'is_post_type_viewable' ); } /** Taxonomies usable for tabs on a post type, as name => label. */ public static function taxonomies_for( $post_type ) { $list = array(); foreach ( get_object_taxonomies( $post_type, 'objects' ) as $tax ) { if ( 'post_format' === $tax->name ) { continue; } $list[ $tax->name ] = $tax->label; } return $list; } /** * Output one settings field. * * @param array $args Field definition plus 'key'. */ public static function render_field( $args ) { $key = $args['key']; $id = 'afs-' . $key; $name = AFS_SETTINGS . '[' . $key . ']'; $value = self::afs_get_option( $key ); switch ( $args['type'] ) { case 'post_type': echo ''; break; case 'taxonomy': $post_type = self::afs_get_option( 'afs_general_post_type' ); $options = self::taxonomies_for( $post_type ); echo ''; break; case 'number': printf( '', esc_attr( $id ), esc_attr( $name ), (int) $value ); break; case 'ratio': echo ''; break; case 'columns': echo ''; break; case 'words': printf( '', esc_attr( $id ), esc_attr( $name ), (int) $value ); break; case 'yesno': echo '
' . esc_html( $args['label'] ) . ''; printf( '', esc_attr( $name ), checked( (int) $value, 1, false ), esc_html__( 'Yes', 'ajax-filter-search' ) ); printf( '', esc_attr( $name ), checked( (int) $value, 0, false ), esc_html__( 'No', 'ajax-filter-search' ) ); echo '
'; break; case 'design': echo '
' . esc_html( $args['label'] ) . ''; foreach ( AFS_Design::all() as $slug => $label ) { printf( '', esc_attr( $slug ), esc_attr( $name ), checked( $value, $slug, false ), esc_html( $label ), self::design_preview( $slug ) // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Static markup built in design_preview(). ); } echo '
'; break; case 'layout': echo ''; break; case 'color': printf( '', esc_attr( $id ), esc_attr( $name ), esc_attr( $value ), esc_attr( AFS_Design::default_accent() ) ); break; } if ( ! empty( $args['help'] ) && empty( $args['no_help'] ) ) { echo '

' . esc_html( $args['help'] ) . '

'; } } /** Which fields belong to which card, in order. */ private static function groups() { return array( 'appearance' => array( 'title' => __( 'Appearance', 'ajax-filter-search' ), 'intro' => __( 'How feeds look on your site.', 'ajax-filter-search' ), 'fields' => array( 'afs_appearance_design', 'afs_appearance_accent' ), ), 'cards' => array( 'title' => __( 'Cards', 'ajax-filter-search' ), 'intro' => __( 'What each result shows in Editorial Grid and Resource Library.', 'ajax-filter-search' ), 'callout' => array( __( 'Defaults, and how a feed overrides them.', 'ajax-filter-search' ), __( 'These apply to every feed that uses one of these designs. One feed can override any of them with a shortcode attribute (for example columns="2") or in the block\'s Cards panel ("Use plugin setting"). Classic and Modern ignore them.', 'ajax-filter-search' ), ), 'fold' => true, 'fields' => array( 'afs_cards_show_image', 'afs_cards_image_ratio', 'afs_cards_columns', 'afs_cards_show_excerpt', 'afs_cards_excerpt_length', 'afs_cards_show_date', 'afs_cards_show_time', 'afs_cards_show_category' ), ), 'feed' => array( 'title' => __( 'Feed', 'ajax-filter-search' ), 'intro' => __( 'What a feed lists by default. Every feed can override these with shortcode attributes or the block settings.', 'ajax-filter-search' ), 'fields' => array( 'afs_general_post_type', 'afs_general_post_taxonomy', 'afs_general_posts_per_page', 'afs_general_layout' ), ), 'filters' => array( 'title' => __( 'Filters and controls', 'ajax-filter-search' ), 'intro' => __( 'What visitors can use to narrow the results.', 'ajax-filter-search' ), 'fields' => array( 'afs_general_show_filters', 'afs_general_views', 'afs_general_show_sort', 'afs_general_show_chips' ), ), 'search' => array( 'title' => __( 'Search engines', 'ajax-filter-search' ), 'intro' => __( 'Keep filtered views out of search results so only your main page is indexed.', 'ajax-filter-search' ), 'fields' => array( 'afs_general_noindex_filtered' ), ), ); } /** Small illustrations for the design picker (decorative). */ private static function design_preview( $slug ) { if ( AFS_Design::EDITORIAL === $slug ) { return ''; } if ( AFS_Design::LIBRARY === $slug ) { return ''; } if ( AFS_Design::MODERN === $slug ) { return ''; } return ''; } public static function options_page() { if ( ! current_user_can( AFS_CAPABILITIES ) ) { return; } $fields = self::fields(); $icon = AFS_PLUGIN_URL . '/admin/images/icon.svg'; ?>

%1$s
', esc_attr( $example ), esc_html__( 'Copy', 'ajax-filter-search' ) ); }; $copy( '[ajax_filter_search]' ); ?>
$info ) : ?>

' . esc_html__( 'Card settings', 'ajax-filter-search' ); if ( ! $fold_open ) { echo ' ' . esc_html__( 'Used by feeds that choose Editorial Grid or Resource Library.', 'ajax-filter-search' ) . ''; } echo ''; } foreach ( $info['fields'] as $key ) { if ( ! isset( $fields[ $key ] ) ) { continue; } $field = $fields[ $key ]; // A control that depends on another (for example the image shape on the featured image) is hidden, never // disabled or removed, while its parent is off, so its saved value is kept and submitted. $needs = ! empty( $field['needs'] ) ? $field['needs'] : ''; $needs_attrs = $needs ? ' data-afs-needs="' . esc_attr( AFS_SETTINGS . '[' . $needs . ']' ) . '"' . ( (int) self::afs_get_option( $needs ) ? '' : ' hidden' ) : ''; ?>
>

$key, 'no_help' => true, ) ) ); ?>
' . esc_html__( 'Hidden options keep their saved values and apply again if you turn the option back on.', 'ajax-filter-search' ) . '

'; } ?>
$map, 'none' => __( 'None', 'ajax-filter-search' ), 'copied' => __( 'Copied', 'ajax-filter-search' ), 'unsaved' => __( 'You have unsaved changes.', 'ajax-filter-search' ), 'dismissed' => __( 'Announcement dismissed.', 'ajax-filter-search' ), ) ); } } endif;