__( 'Find Parts', 'ymm-hero' ), 'widget_title' => __( 'Search by Vehicle', 'ymm-hero' ), 'make_placeholder' => __( 'Select Make', 'ymm-hero' ), 'model_placeholder' => __( 'Select Model', 'ymm-hero' ), 'year_placeholder' => __( 'Select Year', 'ymm-hero' ), ); } public static function get_general( $key = null ) { return self::get_tab_settings( 'general', self::get_general_defaults(), $key ); } public static function get_display_defaults() { return array( 'search_widget_auto_inject' => false, 'fitment_widget_auto_inject' => true, 'fitment_widget_position' => 'before_cart', 'compatibility_tab_auto_inject' => true, 'active_filter_banner_enabled' => true, 'only_assigned_vehicles' => true, ); } public static function get_display( $key = null ) { return self::get_tab_settings( 'display', self::get_display_defaults(), $key ); } public static function get_styling_defaults() { return array( 'primary_color' => '#2271b1', 'button_text_color' => '#ffffff', 'border_style' => 'none', ); } public static function get_styling( $key = null ) { return self::get_tab_settings( 'styling', self::get_styling_defaults(), $key ); } private static function get_tab_settings( $tab, $defaults, $key ) { $settings = get_option( self::OPTION_NAME, array() ); $values = wp_parse_args( isset( $settings[ $tab ] ) && is_array( $settings[ $tab ] ) ? $settings[ $tab ] : array(), $defaults ); if ( null !== $key ) { return isset( $values[ $key ] ) ? $values[ $key ] : null; } return $values; } public function register_settings() { register_setting( self::OPTION_GROUP, self::OPTION_NAME, array( 'sanitize_callback' => array( $this, 'sanitize' ), ) ); $this->register_general_fields(); $this->register_display_fields(); $this->register_styling_fields(); } private function register_general_fields() { add_settings_section( 'ymmhero_general_section', '', '__return_false', 'ymmhero_settings_general' ); // Labels only. Each of these fields is a piece of storefront copy whose // label already says what it is; a sentence restating that is noise. $fields = array( 'search_button_label' => __( 'Search Button Label', 'ymm-hero' ), 'widget_title' => __( 'Widget Title', 'ymm-hero' ), 'make_placeholder' => __( 'Make Placeholder', 'ymm-hero' ), 'model_placeholder' => __( 'Model Placeholder', 'ymm-hero' ), 'year_placeholder' => __( 'Year Placeholder', 'ymm-hero' ), ); foreach ( $fields as $key => $field ) { add_settings_field( $key, $field, array( $this, 'field_text' ), 'ymmhero_settings_general', 'ymmhero_general_section', array( 'tab' => 'general', 'key' => $key ) ); } } private function register_display_fields() { add_settings_section( 'ymmhero_display_shop_section', __( 'Shop Page', 'ymm-hero' ), '__return_false', 'ymmhero_settings_display' ); add_settings_field( 'search_widget_auto_inject', __( 'Search Widget', 'ymm-hero' ), array( $this, 'field_checkbox' ), 'ymmhero_settings_display', 'ymmhero_display_shop_section', array( 'tab' => 'display', 'key' => 'search_widget_auto_inject', 'label' => __( 'Automatically show the search widget above the product grid', 'ymm-hero' ), ) ); add_settings_field( 'active_filter_banner_enabled', __( 'Active Filter Banner', 'ymm-hero' ), array( $this, 'field_checkbox' ), 'ymmhero_settings_display', 'ymmhero_display_shop_section', array( 'tab' => 'display', 'key' => 'active_filter_banner_enabled', 'label' => __( 'Show the "Showing results for" banner when a vehicle filter is active', 'ymm-hero' ), ) ); add_settings_field( 'only_assigned_vehicles', __( 'Vehicle Dropdowns', 'ymm-hero' ), array( $this, 'field_checkbox' ), 'ymmhero_settings_display', 'ymmhero_display_shop_section', array( 'tab' => 'display', 'key' => 'only_assigned_vehicles', 'label' => __( 'Only show vehicles with assigned products', 'ymm-hero' ), 'description' => __( 'Turn this off while setting up the store, so vehicles show up before any products are assigned to them.', 'ymm-hero' ), ) ); add_settings_section( 'ymmhero_display_product_section', __( 'Product Detail Page', 'ymm-hero' ), '__return_false', 'ymmhero_settings_display' ); add_settings_field( 'fitment_widget_auto_inject', __( 'Fitment Widget', 'ymm-hero' ), array( $this, 'field_checkbox' ), 'ymmhero_settings_display', 'ymmhero_display_product_section', array( 'tab' => 'display', 'key' => 'fitment_widget_auto_inject', 'label' => __( 'Automatically show the fitment widget on product pages', 'ymm-hero' ), ) ); add_settings_field( 'fitment_widget_position', __( 'Fitment Widget Position', 'ymm-hero' ), array( $this, 'field_select' ), 'ymmhero_settings_display', 'ymmhero_display_product_section', array( 'tab' => 'display', 'key' => 'fitment_widget_position', 'options' => array( 'before_cart' => __( 'Before Add to Cart', 'ymm-hero' ), 'after_cart' => __( 'After Add to Cart', 'ymm-hero' ), ), ) ); add_settings_field( 'compatibility_tab_auto_inject', __( 'Compatibility Tab', 'ymm-hero' ), array( $this, 'field_checkbox' ), 'ymmhero_settings_display', 'ymmhero_display_product_section', array( 'tab' => 'display', 'key' => 'compatibility_tab_auto_inject', 'label' => __( 'Automatically add a Compatibility tab to products with mapped vehicles', 'ymm-hero' ), ) ); } private function register_styling_fields() { add_settings_section( 'ymmhero_styling_section', '', '__return_false', 'ymmhero_settings_styling' ); add_settings_field( 'primary_color', __( 'Primary Color', 'ymm-hero' ), array( $this, 'field_color' ), 'ymmhero_settings_styling', 'ymmhero_styling_section', array( 'tab' => 'styling', 'key' => 'primary_color' ) ); add_settings_field( 'button_text_color', __( 'Button Text Color', 'ymm-hero' ), array( $this, 'field_color' ), 'ymmhero_settings_styling', 'ymmhero_styling_section', array( 'tab' => 'styling', 'key' => 'button_text_color' ) ); add_settings_field( 'border_style', __( 'Widget Border Style', 'ymm-hero' ), array( $this, 'field_radio' ), 'ymmhero_settings_styling', 'ymmhero_styling_section', array( 'tab' => 'styling', 'key' => 'border_style', 'options' => array( 'none' => __( 'None', 'ymm-hero' ), 'light' => __( 'Light', 'ymm-hero' ), 'card' => __( 'Card', 'ymm-hero' ), ), ) ); } public function field_text( $args ) { $value = self::get_tab_value( $args ); printf( '', esc_attr( self::OPTION_NAME ), esc_attr( $args['tab'] ), esc_attr( $args['key'] ), esc_attr( $value ) ); if ( ! empty( $args['description'] ) ) { printf( '

%s

', esc_html( $args['description'] ) ); } } public function field_checkbox( $args ) { $value = self::get_tab_value( $args ); printf( '', esc_attr( self::OPTION_NAME ), esc_attr( $args['tab'] ), esc_attr( $args['key'] ), checked( $value, true, false ), esc_html( $args['label'] ) ); if ( ! empty( $args['description'] ) ) { printf( '

%s

', esc_html( $args['description'] ) ); } } public function field_select( $args ) { $value = self::get_tab_value( $args ); printf( ''; } public function field_color( $args ) { $value = self::get_tab_value( $args ); printf( '', esc_attr( self::OPTION_NAME ), esc_attr( $args['tab'] ), esc_attr( $args['key'] ), esc_attr( $value ) ); } public function field_radio( $args ) { $value = self::get_tab_value( $args ); foreach ( $args['options'] as $option_value => $option_label ) { printf( '', esc_attr( self::OPTION_NAME ), esc_attr( $args['tab'] ), esc_attr( $args['key'] ), esc_attr( $option_value ), checked( $value, $option_value, false ), esc_html( $option_label ) ); } } private static function get_tab_value( $args ) { if ( 'display' === $args['tab'] ) { return self::get_display( $args['key'] ); } if ( 'styling' === $args['tab'] ) { return self::get_styling( $args['key'] ); } return self::get_general( $args['key'] ); } public function sanitize( $input ) { $existing = get_option( self::OPTION_NAME, array() ); if ( isset( $input['general'] ) && is_array( $input['general'] ) ) { $existing['general'] = $this->sanitize_general( $input['general'] ); } if ( isset( $input['display'] ) && is_array( $input['display'] ) ) { $existing['display'] = $this->sanitize_display( $input['display'] ); } if ( isset( $input['styling'] ) && is_array( $input['styling'] ) ) { $existing['styling'] = $this->sanitize_styling( $input['styling'] ); } return $existing; } private function sanitize_general( $input ) { $defaults = self::get_general_defaults(); $general = array(); foreach ( array( 'search_button_label', 'widget_title', 'make_placeholder', 'model_placeholder', 'year_placeholder' ) as $key ) { $value = isset( $input[ $key ] ) ? sanitize_text_field( $input[ $key ] ) : ''; $general[ $key ] = '' !== $value ? $value : $defaults[ $key ]; } return $general; } private function sanitize_display( $input ) { $defaults = self::get_display_defaults(); $fitment_position = isset( $input['fitment_widget_position'] ) ? sanitize_key( $input['fitment_widget_position'] ) : ''; if ( ! in_array( $fitment_position, array( 'before_cart', 'after_cart' ), true ) ) { $fitment_position = $defaults['fitment_widget_position']; } return array( 'search_widget_auto_inject' => ! empty( $input['search_widget_auto_inject'] ), 'fitment_widget_auto_inject' => ! empty( $input['fitment_widget_auto_inject'] ), 'fitment_widget_position' => $fitment_position, 'compatibility_tab_auto_inject' => ! empty( $input['compatibility_tab_auto_inject'] ), 'active_filter_banner_enabled' => ! empty( $input['active_filter_banner_enabled'] ), 'only_assigned_vehicles' => ! empty( $input['only_assigned_vehicles'] ), ); } private function sanitize_styling( $input ) { $defaults = self::get_styling_defaults(); $primary_color = isset( $input['primary_color'] ) ? sanitize_hex_color( $input['primary_color'] ) : ''; $button_text_color = isset( $input['button_text_color'] ) ? sanitize_hex_color( $input['button_text_color'] ) : ''; $border_style = isset( $input['border_style'] ) ? sanitize_key( $input['border_style'] ) : ''; if ( ! in_array( $border_style, array( 'none', 'light', 'card' ), true ) ) { $border_style = $defaults['border_style']; } return array( 'primary_color' => $primary_color ? $primary_color : $defaults['primary_color'], 'button_text_color' => $button_text_color ? $button_text_color : $defaults['button_text_color'], 'border_style' => $border_style, ); } }