get_name() : ''; } wp_localize_script( 'h23pf-front', 'H23PF', array( 'stepper' => (int) $s['stepper'], 'product' => $product_name, 'messages' => $s['messages'], 'i18n' => array( 'minus' => __( 'Decrease', '23h-product-features' ), 'plus' => __( 'Increase', '23h-product-features' ), ), ) ); } /* ------------------------------------------------------------------ * Product page * ---------------------------------------------------------------- */ public static function render() { global $product; if ( ! $product instanceof WC_Product ) { return; } $pid = $product->is_type( 'variation' ) ? $product->get_parent_id() : $product->get_id(); $forms = H23PF_Forms::for_product( $pid ); if ( ! $forms ) { return; } $s = H23PF_Settings::get(); $posted = self::posted_fields(); // Refills the form after a failed add to cart. ?>
$form ) : ?>
'; return; } if ( 'heading' === $type ) { echo '
'; if ( $f['label'] ) { echo '' . esc_html( $f['label'] ) . ''; } if ( $f['desc'] ) { echo '

' . nl2br( esc_html( $f['desc'] ) ) . '

'; } echo '
'; return; } if ( 'hidden' === $type ) { return; } $name = 'h23pf[' . (int) $form_id . '][' . $f['id'] . ']'; $dom_id = 'h23pf-' . (int) $form_id . '-' . $f['id']; $is_group = in_array( $type, array( 'radio', 'checkbox' ), true ); $error = isset( self::$field_errors[ (int) $form_id ][ $f['id'] ] ) ? self::$field_errors[ (int) $form_id ][ $f['id'] ] : ''; $attrs = array( 'class' => 'h23pf-field h23pf-field--' . $type . ' h23pf-w-' . $f['width'] . ( $error ? ' is-invalid' : '' ), 'data-type' => $type, 'data-required' => $f['required'] ? '1' : '0', 'data-reqmsg' => $f['req_msg'], 'data-maxlength' => $f['maxlength'], 'data-min' => $f['min'], 'data-max' => $f['max'], ); if ( 'date' === $type ) { $attrs['data-min'] = H23PF_Submission::date_limit( $f['min'] ); $attrs['data-max'] = H23PF_Submission::date_limit( $f['max'] ); $attrs['data-min-text'] = $attrs['data-min'] ? H23PF_Submission::format_date( $attrs['data-min'] ) : ''; $attrs['data-max-text'] = $attrs['data-max'] ? H23PF_Submission::format_date( $attrs['data-max'] ) : ''; } echo ' $v ) { echo ' ' . esc_attr( $k ) . '="' . esc_attr( $v ) . '"'; } echo '>'; // Label. echo $is_group ? '' : '' : ''; if ( $f['desc'] ) { echo '

' . nl2br( esc_html( $f['desc'] ) ) . '

'; } switch ( $type ) { case 'select': $sel = null !== $value && ! is_array( $value ) ? (string) $value : self::default_option( $f ); echo '
'; break; case 'radio': case 'checkbox': if ( null !== $value ) { $checked = array_map( 'strval', (array) $value ); } else { $checked = array(); foreach ( $f['options'] as $o ) { if ( $o['selected'] ) { $checked[] = $o['id']; } } } $input_name = 'checkbox' === $type ? $name . '[]' : $name; echo '
'; foreach ( $f['options'] as $o ) { printf( ''; } echo '
'; break; case 'textarea': printf( '', esc_attr( $dom_id ), esc_attr( $name ), esc_attr( $f['placeholder'] ), '' !== $f['maxlength'] ? ' maxlength="' . esc_attr( (int) $f['maxlength'] ) . '"' : '', $f['required'] ? ' aria-required="true"' : '', esc_textarea( null !== $value && ! is_array( $value ) ? $value : $f['default'] ) ); break; default: $html_type = $type; $extra = array(); if ( 'number' === $type ) { if ( '' !== $f['min'] ) { $extra['min'] = $f['min']; } if ( '' !== $f['max'] ) { $extra['max'] = $f['max']; } $extra['step'] = '' !== $f['step'] ? $f['step'] : 'any'; $extra['inputmode'] = 'decimal'; } elseif ( 'date' === $type ) { if ( $attrs['data-min'] ) { $extra['min'] = $attrs['data-min']; } if ( $attrs['data-max'] ) { $extra['max'] = $attrs['data-max']; } } elseif ( in_array( $type, array( 'text', 'password' ), true ) && '' !== $f['maxlength'] ) { $extra['maxlength'] = (int) $f['maxlength']; } elseif ( 'url' === $type ) { $html_type = 'text'; $extra['inputmode'] = 'url'; $extra['autocomplete'] = 'url'; } elseif ( 'email' === $type ) { $html_type = 'text'; $extra['inputmode'] = 'email'; $extra['autocomplete'] = 'email'; } if ( $f['required'] ) { $extra['aria-required'] = 'true'; } $val = null !== $value && ! is_array( $value ) ? $value : $f['default']; if ( 'color' === $type && ! sanitize_hex_color( $val ) ) { $val = '#000000'; } echo ' $v ) { echo ' ' . esc_attr( $k ) . '="' . esc_attr( $v ) . '"'; } echo '>'; } echo ''; echo ''; } private static function default_option( $f ) { foreach ( $f['options'] as $o ) { if ( $o['selected'] ) { return $o['id']; } } return ''; } /* ------------------------------------------------------------------ * Validation + cart * ---------------------------------------------------------------- */ public static function validate( $passed, $product_id, $quantity, $variation_id = 0, $variations = array(), $cart_item_data = array() ) { if ( ! empty( $cart_item_data['h23pf'] ) ) { return $passed; // E.g. "Order again": the values come from the old order. } if ( ! H23PF_Forms::for_product( $product_id ) ) { return $passed; } $res = H23PF_Submission::parse( $product_id, self::posted_fields() ); if ( $res['errors'] ) { // Shown next to each field when the page is shown again, and listed in the notice for themes that add to cart with Ajax. self::$field_errors = $res['fields']; foreach ( $res['errors'] as $e ) { wc_add_notice( $e, 'error' ); } return false; } return $passed; } public static function cart_item_data( $data, $product_id, $variation_id ) { if ( ! empty( $data['h23pf'] ) || ! H23PF_Forms::for_product( $product_id ) ) { return $data; } $res = H23PF_Submission::parse( $product_id, self::posted_fields() ); if ( $res['items'] ) { $data['h23pf'] = array( 'items' => $res['items'] ); } return $data; } // A chosen value, as shown in the cart and the order. private static function display_value( $it ) { return $it['value']; } public static function item_data( $data, $item ) { if ( empty( $item['h23pf']['items'] ) ) { return $data; } foreach ( $item['h23pf']['items'] as $it ) { if ( ! empty( $it['hidden'] ) ) { continue; } $data[] = array( 'key' => $it['label'], 'value' => $it['value'], 'display' => esc_html( $it['value'] ), ); } return $data; } public static function order_item_meta( $order_item, $cart_item_key, $values, $order ) { if ( empty( $values['h23pf']['items'] ) ) { return; } foreach ( $values['h23pf']['items'] as $it ) { if ( empty( $it['hidden'] ) ) { $order_item->add_meta_data( $it['label'], self::display_value( $it, false ) ); } } $order_item->add_meta_data( '_h23pf_items', $values['h23pf']['items'], true ); } /** Option values saved on an order line. */ public static function item_options( $item ) { $items = $item->get_meta( '_h23pf_items', true ); return is_array( $items ) ? $items : array(); } public static function order_again( $data, $item, $order ) { $items = self::item_options( $item ); if ( $items ) { $data['h23pf'] = array( 'items' => $items ); } return $data; } public static function hidden_meta( $keys ) { $keys[] = '_h23pf_items'; return $keys; } /** In the admin, also shows the hidden fields (not visible to the customer). */ public static function admin_hidden_fields( $item_id, $item, $product ) { if ( ! is_callable( array( $item, 'get_meta' ) ) ) { return; } foreach ( self::item_options( $item ) as $it ) { if ( ! empty( $it['hidden'] ) ) { echo '
' . esc_html( $it['label'] ) . ' ' . esc_html( sprintf( /* translators: %s: value of a hidden field. */ __( '(hidden): %s', '23h-product-features' ), self::display_value( $it, false ) ) ) . '
'; } } } /* ------------------------------------------------------------------ * Shop and category pages * ---------------------------------------------------------------- */ private static function needs_options( $product ) { return $product instanceof WC_Product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && H23PF_Forms::product_has_inputs( $product->get_id() ); } public static function loop_url( $url, $product ) { return self::needs_options( $product ) ? $product->get_permalink() : $url; } public static function loop_text( $text, $product ) { $custom = H23PF_Settings::get( 'loop_button_text' ); return $custom && self::needs_options( $product ) ? $custom : $text; } public static function loop_ajax( $supports, $feature, $product ) { if ( 'ajax_add_to_cart' === $feature && self::needs_options( $product ) ) { return false; } return $supports; } }