array( __( 'Features', '23h-product-features' ), 'dashicons-star-filled' ), 'forms' => array( __( 'Product options', '23h-product-features' ), 'dashicons-forms' ), 'quantity' => array( __( 'Quantity & step', '23h-product-features' ), 'dashicons-plus-alt' ), 'messages' => array( __( 'Messages', '23h-product-features' ), 'dashicons-format-chat' ), 'general' => array( __( 'Settings', '23h-product-features' ), 'dashicons-admin-generic' ), ); } public static function url( $tab = 'features', $args = array() ) { if ( 'forms' === $tab ) { return add_query_arg( $args, admin_url( 'edit.php?post_type=' . H23PF_Forms::CPT ) ); } return add_query_arg( array_merge( array( 'post_type' => 'product', 'page' => self::PAGE, 'tab' => $tab, ), $args ), admin_url( 'edit.php' ) ); } /** A read-only value of the current admin screen URL, sanitized. */ public static function query_arg( $key ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen state (tab, notice), no action is taken. return isset( $_GET[ $key ] ) ? sanitize_key( wp_unslash( $_GET[ $key ] ) ) : ''; } /** HTML allowed in the settings fields built by this class. */ public static function form_html() { $common = array( 'class' => true, 'id' => true, 'name' => true, 'value' => true, 'type' => true, 'placeholder' => true, 'title' => true, 'style' => true, 'min' => true, 'max' => true, 'step' => true, 'maxlength' => true, 'checked' => true, 'selected' => true, 'multiple' => true, 'rows' => true, 'inputmode' => true, 'aria-hidden' => true, 'aria-label' => true, 'readonly' => true, 'autocomplete' => true, 'spellcheck' => true, 'for' => true, 'href' => true, 'target' => true, 'rel' => true, ); $data = array( 'data-*' => true ); $tags = array( 'input', 'select', 'option', 'label', 'span', 'em', 'code', 'textarea', 'a', 'b', 'strong', 'br', 'p', 'div', 'small', 'button' ); $out = array(); foreach ( $tags as $tag ) { $out[ $tag ] = array_merge( $common, $data ); } return $out; } public static function menu() { add_submenu_page( 'edit.php?post_type=product', __( '23h Product Features', '23h-product-features' ), __( 'Product Features', '23h-product-features' ), 'manage_woocommerce', self::PAGE, array( __CLASS__, 'render' ) ); } private static function is_cpt_screen() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; return $screen && H23PF_Forms::CPT === $screen->post_type; } public static function parent_file( $file ) { return self::is_cpt_screen() ? 'edit.php?post_type=product' : $file; } public static function submenu_file( $file ) { return self::is_cpt_screen() ? self::PAGE : $file; } public static function body_class( $classes ) { $screen = get_current_screen(); if ( self::is_cpt_screen() || ( $screen && false !== strpos( $screen->id, self::PAGE ) ) ) { $classes .= ' h23pf-screen'; } return $classes; } /** Top bar, the same on all the plugin's screens (like Brand Ambassador's). */ public static function topbar() { ?>
v
' . esc_html( isset( $tabs[ $active ] ) ? $tabs[ $active ][0] : __( 'Product Features', '23h-product-features' ) ) . ''; } ?> id, self::PAGE ) ) ) { self::topbar(); } } public static function cpt_header() { if ( self::is_cpt_screen() ) { echo '
'; self::header( 'forms', false ); echo '
'; } } public static function assets( $hook ) { $screen = get_current_screen(); if ( ! $screen ) { return; } $is_page = false !== strpos( $hook, self::PAGE ); $is_form = H23PF_Forms::CPT === $screen->post_type; $is_product = 'product' === $screen->post_type && 'post' === $screen->base; $is_cat = 'product_cat' === $screen->taxonomy; $is_orders = in_array( $screen->id, array( 'woocommerce_page_wc-orders', 'edit-shop_order', 'shop_order' ), true ); if ( ! $is_page && ! $is_form && ! $is_product && ! $is_cat && ! $is_orders ) { return; } wp_enqueue_style( 'h23pf-admin', H23PF_URL . 'assets/css/admin.css', array(), H23PF_Settings::asset_ver( 'assets/css/admin.css' ) ); if ( $is_orders || $is_cat ) { return; } $deps = array( 'jquery', 'jquery-ui-sortable' ); if ( $is_page || $is_product ) { wp_enqueue_media(); } if ( $is_page ) { wp_enqueue_style( 'h23pf-features', H23PF_URL . 'assets/css/features.css', array(), H23PF_Settings::asset_ver( 'assets/css/features.css' ) ); } if ( $is_page || $is_form ) { wp_enqueue_style( 'woocommerce_admin_styles' ); wp_enqueue_script( 'wc-enhanced-select' ); $deps[] = 'wc-enhanced-select'; } wp_enqueue_script( 'h23pf-admin', H23PF_URL . 'assets/js/admin.js', $deps, H23PF_Settings::asset_ver( 'assets/js/admin.js' ), true ); wp_localize_script( 'h23pf-admin', 'H23PF_ADMIN', array( 'types' => H23PF_Forms::types(), 'choice' => H23PF_Forms::choice_types(), 'layout' => H23PF_Forms::layout_types(), 'currency' => html_entity_decode( get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8' ), 'iconSize' => (int) H23PF_Settings::get( 'feat_icon_size' ), 'i18n' => self::js_strings(), ) ); } /** Texts used by admin.js. */ private static function js_strings() { return array( 'mediaTitle' => __( 'Choose the icon', '23h-product-features' ), 'mediaButton' => __( 'Use this icon', '23h-product-features' ), 'option' => __( 'Option', '23h-product-features' ), /* translators: %d: option number. */ 'optionN' => __( 'Option %d', '23h-product-features' ), 'noLabel' => __( '(no label)', '23h-product-features' ), 'copy' => __( '(copy)', '23h-product-features' ), /* translators: %d: number of options. */ 'nOptions' => __( '%d options', '23h-product-features' ), 'duplicate' => __( 'Duplicate', '23h-product-features' ), 'delete' => __( 'Delete', '23h-product-features' ), 'edit' => __( 'Edit', '23h-product-features' ), 'separator' => __( 'Separator line between fields.', '23h-product-features' ), 'title' => __( 'Title', '23h-product-features' ), 'text' => __( 'Text', '23h-product-features' ), 'label' => __( 'Label', '23h-product-features' ), 'width' => __( 'Width', '23h-product-features' ), 'full' => __( 'full', '23h-product-features' ), 'half' => __( 'half', '23h-product-features' ), 'hiddenValue' => __( 'Value (not shown on the site, saved in the order)', '23h-product-features' ), 'description' => __( 'Description (below the label)', '23h-product-features' ), 'selectText' => __( 'List placeholder', '23h-product-features' ), 'choose' => __( '— Choose —', '23h-product-features' ), 'default' => __( 'Default', '23h-product-features' ), 'defaultTitle' => __( 'Selected by default', '23h-product-features' ), 'optionExample' => __( 'e.g. Candle 30 cm × 6 cm diam.', '23h-product-features' ), 'addOption' => __( '+ Add option', '23h-product-features' ), 'placeholder' => __( 'Placeholder', '23h-product-features' ), 'defaultValue' => __( 'Default value', '23h-product-features' ), 'maxChars' => __( 'Maximum characters', '23h-product-features' ), 'min' => __( 'Minimum', '23h-product-features' ), 'max' => __( 'Maximum', '23h-product-features' ), 'step' => __( 'Step', '23h-product-features' ), 'dateMin' => __( 'Earliest (days from today)', '23h-product-features' ), 'dateMax' => __( 'Latest (days from today)', '23h-product-features' ), 'egDays2' => __( 'e.g. 2', '23h-product-features' ), 'egDays60' => __( 'e.g. 60', '23h-product-features' ), 'required' => __( 'Required', '23h-product-features' ), 'reqMsg' => __( 'Custom message when it is missing', '23h-product-features' ), 'reqMsgPh' => __( 'default from Messages', '23h-product-features' ), ); } /* ------------------------------------------------------------------ * Page * ---------------------------------------------------------------- */ public static function render() { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $tab = self::query_arg( 'tab' ); if ( 'forms' === $tab || ! isset( self::tabs()[ $tab ] ) ) { $tab = 'features'; } $s = H23PF_Settings::get(); ?>

' . esc_html( $title ) . '

'; if ( $desc ) { echo '

' . wp_kses( $desc, self::form_html() ) . '

'; } echo '
'; } private static function card_close() { echo '
'; } private static function field( $label, $html, $help = '' ) { echo '
' . esc_html( $label ) . '
' . wp_kses( $html, self::form_html() ); if ( $help ) { echo '

' . wp_kses( $help, self::form_html() ) . '

'; } echo '
'; } public static function toggle( $name, $checked, $text ) { return sprintf( '', esc_attr( $name ), checked( $checked, true, false ), esc_html( $text ) ); } private static function number( $name, $value, $min, $max, $unit = 'px' ) { return sprintf( '%5$s', esc_attr( $name ), esc_attr( $value ), (int) $min, (int) $max, esc_html( $unit ) ); } private static function color( $name, $value, $placeholder = '' ) { return sprintf( '', esc_attr( $value ? $value : ( $placeholder ? $placeholder : '#ffffff' ) ), esc_attr( $name ), esc_attr( $value ), esc_attr( $placeholder ? $placeholder : __( 'default', '23h-product-features' ) ) ); } private static function options( $choices, $current ) { $html = ''; foreach ( $choices as $value => $label ) { $html .= sprintf( '', esc_attr( $value ), selected( (string) $current, (string) $value, false ), esc_html( $label ) ); } return $html; } /* ---------------- Features ---------------- */ private static function tab_features( $s ) { $terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, ) ); $terms = is_wp_error( $terms ) ? array() : $terms; self::card_open( __( 'Features with icons', '23h-product-features' ), __( 'Rows with an icon and a text shown on the product page (e.g. "Money back in 30 min.", "Priority delivery"). The text accepts <b>, <i>, <a> and <br>. There are two boxes, each with its own position and look: choose the box on each row. On each product you can add its own features or hide the general ones (the 23h Features tab of the product page).', '23h-product-features' ) ); H23PF_Features::repeater( 'h23pf[features]', $s['features'], $terms ); self::card_close(); foreach ( H23PF_Features::boxes() as $box ) { self::box_card( $box, $s ); } self::card_open( __( 'Preview', '23h-product-features' ), __( 'Updated after saving.', '23h-product-features' ), 'h23pf-card--preview' ); $any = false; foreach ( H23PF_Features::boxes() as $box ) { $items = H23PF_Features::in_box( H23PF_Features::enabled_items( $s['features'] ), $box ); if ( $items ) { $any = true; /* translators: %d: box number (1 or 2). */ echo '

' . esc_html( sprintf( __( 'Box %d', '23h-product-features' ), $box ) ) . '

'; H23PF_Features::output( $items, H23PF_Features::box_settings( $box, $s ), $box ); } } if ( ! $any ) { echo '

' . esc_html__( 'No active feature yet.', '23h-product-features' ) . '

'; } self::card_close(); } /** The settings of one features box (box 1: feat_*, box 2: feat2_*). */ private static function box_card( $box, $all ) { $s = H23PF_Features::box_settings( $box, $all ); $p = 2 === (int) $box ? 'h23pf[feat2_' : 'h23pf[feat_'; $n = static function ( $key ) use ( $p ) { return $p . $key . ']'; }; self::card_open( /* translators: %d: box number (1 or 2). */ sprintf( __( 'Box %d', '23h-product-features' ), $box ), 2 === (int) $box ? __( 'A second, separate list, e.g. offers above the "Add to cart" button. Choose "Box 2" on the rows that belong here.', '23h-product-features' ) : __( 'The main list, e.g. guarantees and delivery. Rows go here by default.', '23h-product-features' ), 'h23pf-card--box' ); echo '
'; self::field( __( 'Active', '23h-product-features' ), self::toggle( $n( 'enabled' ), (bool) $s['feat_enabled'], __( 'Show this box on the product page', '23h-product-features' ) ) ); $positions = array(); foreach ( H23PF_Settings::feature_positions() as $k => $pos ) { $positions[ $k ] = $pos[0]; } self::field( __( 'Position', '23h-product-features' ), '', sprintf( /* translators: %s: shortcode. */ __( 'Shortcode for any other place: %s', '23h-product-features' ), '[h23pf_features box="' . (int) $box . '"]' ) ); self::field( __( 'Box title', '23h-product-features' ), '' ); self::field( __( 'Columns', '23h-product-features' ), '', __( 'Phones always show one column.', '23h-product-features' ) ); self::field( __( 'Icon alignment', '23h-product-features' ), '' ); self::field( __( 'Icon size', '23h-product-features' ), self::number( $n( 'icon_size' ), $s['feat_icon_size'], 8, 400 ), __( 'Default for the rows of this box. Each row can set its own size.', '23h-product-features' ) ); self::field( __( 'Text size', '23h-product-features' ), self::number( $n( 'text_size' ), $s['feat_text_size'], 10, 40 ) ); self::field( __( 'Space between rows', '23h-product-features' ), self::number( $n( 'gap' ), $s['feat_gap'], 0, 60 ) ); self::field( __( 'Space above', '23h-product-features' ), self::number( $n( 'space_top' ), $s['feat_space_top'], 0, 200 ), __( 'Distance from what is above the box, e.g. the price or the short description.', '23h-product-features' ) ); self::field( __( 'Space below', '23h-product-features' ), self::number( $n( 'space_bottom' ), $s['feat_space_bottom'], 0, 200 ), __( 'Distance to what follows, e.g. the quantity and the "Add to cart" button.', '23h-product-features' ) ); echo '
'; self::field( __( 'Box', '23h-product-features' ), self::toggle( $n( 'boxed' ), (bool) $s['feat_boxed'], __( 'Background and border around the list', '23h-product-features' ) ) ); self::field( __( 'Background', '23h-product-features' ), self::color( $n( 'bg' ), $s['feat_bg'] ) ); self::field( __( 'Border', '23h-product-features' ), self::color( $n( 'border' ), $s['feat_border'] ) ); self::field( __( 'Text color', '23h-product-features' ), self::color( $n( 'text_color' ), $s['feat_text_color'], '#333333' ), __( 'Empty = the theme color.', '23h-product-features' ) ); self::field( __( 'Rounded corners', '23h-product-features' ), self::number( $n( 'radius' ), $s['feat_radius'], 0, 40 ) ); self::field( __( 'Inner spacing', '23h-product-features' ), self::number( $n( 'padding' ), $s['feat_padding'], 0, 60 ) ); echo '
'; self::card_close(); } /* ---------------- Quantity ---------------- */ private static function tab_quantity() { self::card_open( __( 'Minimum quantity and order step', '23h-product-features' ), __( 'Set on each product (the 23h Features tab of the product page) or on a category (Products → Categories → edit the category). Example: minimum 10 pcs. and step 5 → the customer can order 10, 15, 20… The product rule takes priority over the category rule.', '23h-product-features' ) ); // One simple query on the meta table (a meta_query with OR would join the table three times and hang on big shops). global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- admin overview. $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT DISTINCT post_id FROM %i WHERE meta_key IN (%s, %s, %s) AND meta_value <> %s ORDER BY post_id DESC LIMIT 300', $wpdb->postmeta, '_h23pf_min_qty', '_h23pf_step', '_h23pf_max_qty', '' ) ); echo '

' . esc_html__( 'Products with their own rules', '23h-product-features' ) . '

'; if ( ! $ids ) { echo '

' . esc_html__( 'No product has quantity rules yet.', '23h-product-features' ) . '

'; } else { echo ''; foreach ( $ids as $id ) { $p = wc_get_product( $id ); if ( ! $p ) { continue; } printf( '', esc_url( get_edit_post_link( $id ) ), esc_html( $p->get_name() ), esc_html( $p->get_meta( '_h23pf_min_qty' ) ? $p->get_meta( '_h23pf_min_qty' ) : '—' ), esc_html( $p->get_meta( '_h23pf_step' ) ? $p->get_meta( '_h23pf_step' ) : '1' ), esc_html( $p->get_meta( '_h23pf_max_qty' ) ? $p->get_meta( '_h23pf_max_qty' ) : '∞' ), esc_html( 'yes' === $p->get_meta( '_h23pf_qty_cumulative' ) ? __( 'whole product', '23h-product-features' ) : __( 'per cart line', '23h-product-features' ) ) ); } echo '
' . esc_html__( 'Product', '23h-product-features' ) . '' . esc_html__( 'Minimum', '23h-product-features' ) . '' . esc_html__( 'Step', '23h-product-features' ) . '' . esc_html__( 'Maximum', '23h-product-features' ) . '' . esc_html__( 'Counted', '23h-product-features' ) . '
%2$s%3$s%4$s%5$s%6$s
'; } // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- admin overview. $term_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT DISTINCT term_id FROM %i WHERE meta_key IN (%s, %s, %s) AND meta_value <> %s', $wpdb->termmeta, '_h23pf_min_qty', '_h23pf_step', '_h23pf_max_qty', '' ) ); $terms = $term_ids ? get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'include' => array_map( 'intval', $term_ids ), ) ) : array(); echo '

' . esc_html__( 'Categories with rules', '23h-product-features' ) . '

'; if ( is_wp_error( $terms ) || ! $terms ) { echo '

' . esc_html__( 'No category has quantity rules.', '23h-product-features' ) . '

'; } else { echo ''; foreach ( $terms as $t ) { $min = get_term_meta( $t->term_id, '_h23pf_min_qty', true ); $step = get_term_meta( $t->term_id, '_h23pf_step', true ); $max = get_term_meta( $t->term_id, '_h23pf_max_qty', true ); printf( '', esc_url( get_edit_term_link( $t->term_id, 'product_cat', 'product' ) ), esc_html( $t->name ), esc_html( $min ? $min : '—' ), esc_html( $step ? $step : '1' ), esc_html( $max ? $max : '∞' ) ); } echo '
' . esc_html__( 'Category', '23h-product-features' ) . '' . esc_html__( 'Minimum', '23h-product-features' ) . '' . esc_html__( 'Step', '23h-product-features' ) . '' . esc_html__( 'Maximum', '23h-product-features' ) . '
%2$s%3$s%4$s%5$s
'; } self::card_close(); } /* ---------------- Messages ---------------- */ private static function tab_messages( $s ) { self::card_open( __( 'Validation messages', '23h-product-features' ), __( 'Shown below the wrong field on the product page and in the cart. Leave empty for the default text.', '23h-product-features' ) ); echo '
'; $defaults = H23PF_Settings::default_messages(); foreach ( H23PF_Settings::message_labels() as $key => $info ) { $value = $s['messages'][ $key ] === $defaults[ $key ] ? '' : $s['messages'][ $key ]; echo ''; } echo '
'; self::card_close(); } /* ---------------- Settings ---------------- */ private static function tab_general( $s ) { self::card_open( __( 'General settings', '23h-product-features' ) ); self::field( __( 'Accent color', '23h-product-features' ), self::color( 'h23pf[accent]', $s['accent'] ), __( 'Used on the site for options and the − / + buttons.', '23h-product-features' ) ); self::field( __( '− / + buttons', '23h-product-features' ), self::toggle( 'h23pf[stepper]', (bool) $s['stepper'], __( 'Show − / + buttons next to the quantity', '23h-product-features' ) ), __( 'Turn it off if your theme already has such buttons. The minimum / step rules work either way.', '23h-product-features' ) ); self::field( __( 'Shop button text', '23h-product-features' ), '', __( 'For simple products with options to choose: the button in the shop takes the customer to the product page.', '23h-product-features' ) ); self::card_close(); self::card_open( __( 'Data', '23h-product-features' ) ); self::field( __( 'Uninstall', '23h-product-features' ), self::toggle( 'h23pf[delete_on_uninstall]', (bool) $s['delete_on_uninstall'], __( 'Delete all data when the plugin is deleted', '23h-product-features' ) ), __( 'Off: the settings, product option forms and product rules are kept if you delete the plugin. Orders are never changed.', '23h-product-features' ) ); self::card_close(); } }