64, 'gap' => 18, 'border_width' => 3, 'border_color' => '#789afb', 'hover_color' => '#999999', 'animation' => 'grow', 'speed' => 200, 'badge_bg' => '#111111', 'badge_color' => '#ffffff', 'oos_mode' => 'cross', 'oos_text' => 'Coming soon', ); } public static function animations() { return array( 'none' => __( 'None', 'variantly' ), 'grow' => __( 'Grow on hover', 'variantly' ), 'lift' => __( 'Lift on hover', 'variantly' ), 'pop' => __( 'Pop when selected', 'variantly' ), ); } public static function get() { return wp_parse_args( get_option( self::OPTION, array() ), self::defaults() ); } public static function init() { add_action( 'admin_menu', array( __CLASS__, 'add_menu' ) ); add_action( 'admin_init', array( __CLASS__, 'register' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'admin_assets' ) ); add_action( 'wp_enqueue_scripts', array( __CLASS__, 'frontend_css' ), 20 ); add_filter( 'plugin_action_links_' . plugin_basename( VARIANTLY_FILE ), array( __CLASS__, 'action_links' ) ); } public static function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'variantly' ) . '' ); return $links; } public static function add_menu() { add_submenu_page( 'woocommerce', __( 'Variantly', 'variantly' ), __( 'Variantly', 'variantly' ), 'manage_options', self::PAGE, array( __CLASS__, 'render_page' ) ); } public static function register() { register_setting( 'variantly_settings', self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'default' => self::defaults(), ) ); } private static function clamp( $input, $key, $min, $max ) { $d = self::defaults(); return isset( $input[ $key ] ) ? max( $min, min( $max, absint( $input[ $key ] ) ) ) : $d[ $key ]; } private static function color( $input, $key ) { $d = self::defaults(); $color = isset( $input[ $key ] ) ? sanitize_hex_color( sanitize_text_field( $input[ $key ] ) ) : ''; return $color ? $color : $d[ $key ]; } public static function sanitize( $input ) { $d = self::defaults(); $input = is_array( $input ) ? $input : array(); $anim = isset( $input['animation'] ) && is_string( $input['animation'] ) ? sanitize_key( $input['animation'] ) : $d['animation']; // Only plain strings are accepted, so a crafted array value cannot cause an error. $oos_text = isset( $input['oos_text'] ) && is_string( $input['oos_text'] ) ? mb_substr( trim( sanitize_text_field( $input['oos_text'] ) ), 0, 20 ) : ''; return array( 'swatch_size' => self::clamp( $input, 'swatch_size', 20, 150 ), 'gap' => self::clamp( $input, 'gap', 0, 60 ), 'border_width' => self::clamp( $input, 'border_width', 0, 10 ), 'border_color' => self::color( $input, 'border_color' ), 'hover_color' => self::color( $input, 'hover_color' ), 'animation' => isset( self::animations()[ $anim ] ) ? $anim : $d['animation'], 'speed' => self::clamp( $input, 'speed', 0, 1000 ), 'badge_bg' => self::color( $input, 'badge_bg' ), 'badge_color' => self::color( $input, 'badge_color' ), 'oos_mode' => ( isset( $input['oos_mode'] ) && 'text' === $input['oos_mode'] ) ? 'text' : 'cross', 'oos_text' => '' !== $oos_text ? $oos_text : $d['oos_text'], ); } public static function admin_assets( $hook ) { if ( 'woocommerce_page_' . self::PAGE !== $hook ) { return; } wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_style( 'variantly-settings', VARIANTLY_URL . 'assets/settings.css', array( 'wp-color-picker' ), VARIANTLY_VERSION ); wp_enqueue_script( 'variantly-settings', VARIANTLY_URL . 'assets/settings.js', array( 'jquery', 'wp-color-picker' ), VARIANTLY_VERSION, true ); } /** One labelled setting: label and help on the left, control on the right. */ private static function row_open( $id, $label, $help ) { ?>

v


$label ) : ?>