[title, help]). */ public static function message_labels() { return array( 'form_error' => array( __( 'Form error', '23h-product-features' ), __( 'Shown right above the "Add to cart" button when some fields are empty or wrong.', '23h-product-features' ) ), 'required' => array( __( 'Required field', '23h-product-features' ), __( 'Can be replaced on each field.', '23h-product-features' ) ), 'maxlength' => array( __( 'Too many characters', '23h-product-features' ), __( 'Variables: {max}', '23h-product-features' ) ), 'min_value' => array( __( 'Value too low', '23h-product-features' ), __( 'Variables: {min} (number or date)', '23h-product-features' ) ), 'max_value' => array( __( 'Value too high', '23h-product-features' ), __( 'Variables: {max} (number or date)', '23h-product-features' ) ), 'number' => array( __( 'Invalid number', '23h-product-features' ), '' ), 'email' => array( __( 'Invalid email', '23h-product-features' ), '' ), 'url' => array( __( 'Invalid URL', '23h-product-features' ), '' ), 'option' => array( __( 'Invalid option', '23h-product-features' ), __( 'Only shown when someone sends a value that is not in the list.', '23h-product-features' ) ), 'qty_min' => array( __( 'Quantity below the minimum', '23h-product-features' ), __( 'Variables: {product}, {min}', '23h-product-features' ) ), 'qty_max' => array( __( 'Quantity above the maximum', '23h-product-features' ), __( 'Variables: {product}, {max}', '23h-product-features' ) ), 'qty_step' => array( __( 'Quantity outside the step', '23h-product-features' ), __( 'Variables: {product}, {step}, {min}, {example}', '23h-product-features' ) ), ); } /** Where the features box can be shown on the product page: key => [label, hook priority]. */ public static function feature_positions() { return array( 'after_title' => array( __( 'Below the title', '23h-product-features' ), 6 ), 'after_price' => array( __( 'Below the price', '23h-product-features' ), 11 ), 'after_excerpt' => array( __( 'Below the short description', '23h-product-features' ), 21 ), 'before_cart' => array( __( 'Above the "Add to cart" button', '23h-product-features' ), 29 ), 'after_cart' => array( __( 'Below the "Add to cart" button', '23h-product-features' ), 31 ), 'after_meta' => array( __( 'Below the categories / SKU', '23h-product-features' ), 41 ), 'shortcode' => array( __( 'Only with the [h23pf_features] shortcode', '23h-product-features' ), 0 ), ); } /** Default validation messages. */ public static function default_messages() { return array( 'form_error' => __( 'Please fill in the fields marked above.', '23h-product-features' ), 'required' => __( 'This field is required.', '23h-product-features' ), 'maxlength' => __( 'At most {max} characters are allowed.', '23h-product-features' ), 'min_value' => __( 'The lowest allowed value is {min}.', '23h-product-features' ), 'max_value' => __( 'The highest allowed value is {max}.', '23h-product-features' ), 'number' => __( 'Enter a valid number.', '23h-product-features' ), 'email' => __( 'Enter a valid email address.', '23h-product-features' ), 'url' => __( 'Enter a valid web address (e.g. https://example.com).', '23h-product-features' ), 'option' => __( 'The chosen option is not valid.', '23h-product-features' ), 'qty_min' => __( 'The minimum quantity for "{product}" is {min} pcs.', '23h-product-features' ), 'qty_max' => __( 'The maximum quantity for "{product}" is {max} pcs.', '23h-product-features' ), 'qty_step' => __( '"{product}" is ordered in steps of {step} pcs. (e.g. {example}).', '23h-product-features' ), ); } public static function defaults() { return array( // General. 'accent' => '#0f766e', 'stepper' => 1, 'loop_button_text' => __( 'Choose options', '23h-product-features' ), 'delete_on_uninstall' => 0, // Features (icon + text). 'features' => array(), 'feat_enabled' => 1, 'feat_title' => '', 'feat_position' => 'after_excerpt', 'feat_icon_size' => 40, 'feat_text_size' => 15, 'feat_gap' => 10, 'feat_columns' => 1, 'feat_valign' => 'middle', 'feat_bg' => '#f7f7f5', 'feat_border' => '#e6e4df', 'feat_text_color' => '', 'feat_radius' => 10, 'feat_padding' => 14, 'feat_boxed' => 1, 'feat_space_top' => 0, 'feat_space_bottom' => 20, // Second features box (e.g. offers above the "Add to cart" button). 'feat2_enabled' => 1, 'feat2_title' => '', 'feat2_position' => 'before_cart', 'feat2_icon_size' => 32, 'feat2_text_size' => 15, 'feat2_gap' => 8, 'feat2_columns' => 1, 'feat2_valign' => 'middle', 'feat2_bg' => '', 'feat2_border' => '', 'feat2_text_color' => '', 'feat2_radius' => 10, 'feat2_padding' => 14, 'feat2_boxed' => 0, 'feat2_space_top' => 20, 'feat2_space_bottom' => 20, // Messages. 'messages' => self::default_messages(), ); } public static function get( $key = null ) { if ( null === self::$cache ) { $saved = get_option( self::OPTION, array() ); $saved = is_array( $saved ) ? $saved : array(); $defaults = self::defaults(); $merged = array_merge( $defaults, $saved ); $merged['messages'] = array_merge( $defaults['messages'], isset( $saved['messages'] ) && is_array( $saved['messages'] ) ? array_filter( $saved['messages'], 'strlen' ) : array() ); foreach ( array( 'features' ) as $list ) { $merged[ $list ] = isset( $saved[ $list ] ) && is_array( $saved[ $list ] ) ? $saved[ $list ] : array(); } self::$cache = $merged; } if ( null === $key ) { return self::$cache; } return isset( self::$cache[ $key ] ) ? self::$cache[ $key ] : null; } /** Version of an asset file: the plugin version plus the file's modification time, so browsers reload changed files. */ public static function asset_ver( $rel ) { $file = H23PF_DIR . $rel; return H23PF_VERSION . ( file_exists( $file ) ? '.' . filemtime( $file ) : '' ); } public static function flush() { self::$cache = null; } /** A validation message with its variables replaced. */ public static function msg( $key, $vars = array() ) { $messages = self::get( 'messages' ); $text = isset( $messages[ $key ] ) ? $messages[ $key ] : ''; foreach ( $vars as $k => $v ) { $text = str_replace( '{' . $k . '}', $v, $text ); } return $text; } /** HTML allowed in a feature's text. */ public static function allowed_html() { return array( 'b' => array(), 'strong' => array(), 'i' => array(), 'em' => array(), 'u' => array(), 'small' => array(), 'mark' => array(), 'br' => array(), 'span' => array( 'class' => true, 'style' => true, ), 'a' => array( 'href' => true, 'target' => true, 'rel' => true, 'title' => true, ), ); } /** * Sanitizes a list of features sent from a form. * * @param mixed $rows Raw rows (already unslashed). * @param bool $with_cats Whether rows may be limited to categories. * @return array */ public static function sanitize_features( $rows, $with_cats = true ) { $out = array(); $rows = is_array( $rows ) ? $rows : array(); foreach ( $rows as $row ) { if ( ! is_array( $row ) ) { continue; } $text = trim( wp_kses( isset( $row['text'] ) ? (string) $row['text'] : '', self::allowed_html() ) ); $icon = esc_url_raw( trim( isset( $row['icon'] ) ? (string) $row['icon'] : '' ) ); if ( '' === $text && '' === $icon ) { continue; } $id = sanitize_key( isset( $row['id'] ) ? $row['id'] : '' ); if ( '' === $id ) { $id = 'f' . strtolower( wp_generate_password( 8, false, false ) ); } $size = isset( $row['size'] ) && '' !== $row['size'] ? absint( $row['size'] ) : ''; $item = array( 'id' => $id, 'icon' => $icon, 'icon_id' => absint( isset( $row['icon_id'] ) ? $row['icon_id'] : 0 ), 'size' => $size ? min( 400, max( 8, $size ) ) : '', 'alt' => sanitize_text_field( isset( $row['alt'] ) ? $row['alt'] : '' ), 'text' => $text, 'enabled' => empty( $row['enabled'] ) ? 0 : 1, 'box' => isset( $row['box'] ) && 2 === (int) $row['box'] ? 2 : 1, ); if ( $with_cats ) { $item['cats'] = isset( $row['cats'] ) ? array_values( array_filter( array_map( 'absint', (array) $row['cats'] ) ) ) : array(); } $out[] = $item; } return $out; } /** * Reads the posted settings of one tab: unslashed, then every value sanitized for its use. * * @param string $tab Settings tab. * @return array */ private static function posted( $tab ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing -- the nonce is verified in maybe_save() before this runs. $raw = isset( $_POST['h23pf'] ) && is_array( $_POST['h23pf'] ) ? wp_unslash( $_POST['h23pf'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- each key is sanitized below. $in = array(); if ( 'features' === $tab ) { $in['features'] = self::sanitize_features( isset( $raw['features'] ) ? $raw['features'] : array() ); } if ( 'messages' === $tab && isset( $raw['messages'] ) && is_array( $raw['messages'] ) ) { $in['messages'] = array(); foreach ( array_keys( self::message_labels() ) as $key ) { $in['messages'][ $key ] = isset( $raw['messages'][ $key ] ) ? sanitize_text_field( $raw['messages'][ $key ] ) : ''; } } foreach ( $raw as $key => $value ) { if ( is_array( $value ) ) { continue; } $in[ sanitize_key( $key ) ] = sanitize_text_field( $value ); } return $in; } public static function maybe_save() { if ( empty( $_POST['h23pf_settings_nonce'] ) ) { return; } if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['h23pf_settings_nonce'] ) ), 'h23pf_save_settings' ) ) { wp_die( esc_html__( 'The link has expired. Go back, reload the page and try again.', '23h-product-features' ) ); } if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You are not allowed to change these settings.', '23h-product-features' ) ); } $tab = isset( $_POST['h23pf_tab'] ) ? sanitize_key( wp_unslash( $_POST['h23pf_tab'] ) ) : 'features'; $in = self::posted( $tab ); $current = get_option( self::OPTION, array() ); $current = is_array( $current ) ? $current : array(); $num = static function ( $key, $min, $max, $def ) use ( $in ) { $v = isset( $in[ $key ] ) && '' !== $in[ $key ] ? (int) $in[ $key ] : $def; return min( $max, max( $min, $v ) ); }; $color = static function ( $key ) use ( $in ) { $c = sanitize_hex_color( isset( $in[ $key ] ) ? $in[ $key ] : '' ); return $c ? $c : ''; }; $flag = static function ( $key ) use ( $in ) { return empty( $in[ $key ] ) ? 0 : 1; }; if ( 'features' === $tab ) { $current['features'] = $in['features']; foreach ( array( 'feat_' => array( 'after_excerpt', 40, 10, 0, 20 ), 'feat2_' => array( 'before_cart', 32, 8, 20, 20 ) ) as $p => $def ) { $current[ $p . 'space_top' ] = $num( $p . 'space_top', 0, 200, $def[3] ); $current[ $p . 'space_bottom' ] = $num( $p . 'space_bottom', 0, 200, $def[4] ); $pos = isset( $in[ $p . 'position' ] ) ? $in[ $p . 'position' ] : ''; $current[ $p . 'enabled' ] = $flag( $p . 'enabled' ); $current[ $p . 'title' ] = isset( $in[ $p . 'title' ] ) ? $in[ $p . 'title' ] : ''; $current[ $p . 'position' ] = isset( self::feature_positions()[ $pos ] ) ? $pos : $def[0]; $current[ $p . 'icon_size' ] = $num( $p . 'icon_size', 8, 400, $def[1] ); $current[ $p . 'text_size' ] = $num( $p . 'text_size', 10, 40, 15 ); $current[ $p . 'gap' ] = $num( $p . 'gap', 0, 60, $def[2] ); $current[ $p . 'columns' ] = $num( $p . 'columns', 1, 4, 1 ); $current[ $p . 'valign' ] = isset( $in[ $p . 'valign' ] ) && 'top' === $in[ $p . 'valign' ] ? 'top' : 'middle'; $current[ $p . 'radius' ] = $num( $p . 'radius', 0, 40, 10 ); $current[ $p . 'padding' ] = $num( $p . 'padding', 0, 60, 14 ); $current[ $p . 'boxed' ] = $flag( $p . 'boxed' ); $current[ $p . 'bg' ] = $color( $p . 'bg' ); $current[ $p . 'border' ] = $color( $p . 'border' ); $current[ $p . 'text_color' ] = $color( $p . 'text_color' ); } } elseif ( 'general' === $tab ) { $accent = $color( 'accent' ); $current['accent'] = $accent ? $accent : '#0f766e'; $current['stepper'] = $flag( 'stepper' ); $current['loop_button_text'] = isset( $in['loop_button_text'] ) ? $in['loop_button_text'] : ''; $current['delete_on_uninstall'] = $flag( 'delete_on_uninstall' ); } elseif ( 'messages' === $tab ) { $current['messages'] = isset( $in['messages'] ) ? $in['messages'] : array(); } update_option( self::OPTION, $current ); self::flush(); wp_safe_redirect( H23PF_Admin::url( $tab, array( 'saved' => 1 ) ) ); exit; } }