array( 'label' => __('Secure Checkout', 'ai-eshop-optimizer'), 'description' => __('Your payment information is processed securely.', 'ai-eshop-optimizer'), 'image_url' => $base . 'secure-checkout.svg', ), 'money-back' => array( 'label' => __('Money Back', 'ai-eshop-optimizer'), 'description' => __('Full refund if you are not satisfied with your purchase.', 'ai-eshop-optimizer'), 'image_url' => $base . 'money-back.svg', ), 'free-shipping' => array( 'label' => __('Free Shipping', 'ai-eshop-optimizer'), 'description' => __('Free shipping on qualifying orders.', 'ai-eshop-optimizer'), 'image_url' => $base . 'free-shipping.svg', ), 'quality' => array( 'label' => __('Quality', 'ai-eshop-optimizer'), 'description' => __('High quality products guaranteed.', 'ai-eshop-optimizer'), 'image_url' => $base . 'quality.svg', ), 'satisfaction' => array( 'label' => __('Satisfaction', 'ai-eshop-optimizer'), 'description' => __('Your satisfaction is our priority.', 'ai-eshop-optimizer'), 'image_url' => $base . 'satisfaction.svg', ), 'support' => array( 'label' => __('Support', 'ai-eshop-optimizer'), 'description' => __('Customer support available when you need it.', 'ai-eshop-optimizer'), 'image_url' => $base . 'support.svg', ), ); } public function register_hooks() { add_action('wp_ajax_aieo_dmm_trust_badges_save', array($this, 'ajax_save')); add_shortcode('aieo_trust_badges', array($this, 'shortcode_handler')); add_shortcode('om_ppe_trust_badges', array($this, 'shortcode_handler')); $s = $this->get_settings(); if (empty($s['enabled'])) { return; } add_action('wp_enqueue_scripts', array($this, 'enqueue_styles')); if (empty($s['use_shortcode'])) { add_action('woocommerce_single_product_summary', array($this, 'output'), 35); } } public function get_settings($raw = false) { $stored = get_option(self::OPTION_KEY, array()); if (!is_array($stored)) { $stored = array(); } $legacy = get_option('om_ppe_trust-badges', array()); if (!is_array($legacy)) { $legacy = array(); } $merged = array_merge(array( 'enabled' => false, 'title' => '', 'badges' => isset($legacy['badges']) ? (string) $legacy['badges'] : '', 'default_badges' => isset($legacy['default_badges']) ? (array) $legacy['default_badges'] : array('secure-checkout','money-back','free-shipping'), 'hidden_badges' => isset($legacy['hidden_badges']) ? (array) $legacy['hidden_badges'] : array(), 'badge_overrides' => isset($legacy['default_badges_data']) && is_array($legacy['default_badges_data']) ? $legacy['default_badges_data'] : array(), 'align' => 'center', 'font_size' => 15, 'text_color' => '#212121', 'border_color' => '#e5e5e5', 'margin_top' => 20, 'margin_bottom' => 20, 'image_max_width' => 70, 'image_max_height' => 70, 'mobile_columns' => '2', 'use_shortcode' => false, 'custom_class' => '', ), $stored); return $raw ? $merged : self::fill_i18n_defaults($merged); } private static function i18n_default() { return __('Product Quality Guaranteed!', 'ai-eshop-optimizer'); } private static function fill_i18n_defaults(array $s) { if (!did_action('init')) { return $s; } if (!isset($s['title']) || '' === trim((string) $s['title'])) { $s['title'] = self::i18n_default(); } return $s; } public function ajax_save() { AIEO_DMM::verify_ajax_request(); $valid_align = array('flex-start','center','flex-end'); $align = isset($_POST['align']) ? sanitize_key(wp_unslash($_POST['align'])) : 'center'; $defaults = isset($_POST['default_badges']) && is_array($_POST['default_badges']) ? array_values(array_filter(array_map('sanitize_key', wp_unslash($_POST['default_badges'])))) : array(); $hidden = isset($_POST['hidden_badges']) && is_array($_POST['hidden_badges']) ? array_values(array_filter(array_map('sanitize_key', wp_unslash($_POST['hidden_badges'])))) : array(); $overrides = array(); if (isset($_POST['badge_overrides']) && is_array($_POST['badge_overrides'])) { foreach (wp_unslash($_POST['badge_overrides']) as $key => $v) { $key = sanitize_key($key); if ($key === '') { continue; } $label = isset($v['label']) ? sanitize_text_field($v['label']) : ''; $desc = isset($v['description']) ? sanitize_text_field($v['description']) : ''; if ($label !== '' || $desc !== '') { $overrides[] = array('badge_key' => $key, 'label' => $label, 'description' => $desc); } } } update_option(self::OPTION_KEY, array( 'enabled' => !empty($_POST['enabled']), 'title' => isset($_POST['title']) ? sanitize_text_field(wp_unslash($_POST['title'])) : '', 'badges' => isset($_POST['badges']) ? sanitize_text_field(wp_unslash($_POST['badges'])) : '', 'default_badges' => $defaults, 'hidden_badges' => $hidden, 'badge_overrides' => $overrides, 'align' => in_array($align, $valid_align, true) ? $align : 'center', 'font_size' => max(10, min(30, isset($_POST['font_size']) ? (int) $_POST['font_size'] : 15)), 'text_color' => isset($_POST['text_color']) ? (sanitize_hex_color(wp_unslash($_POST['text_color'])) ?: '#212121') : '#212121', 'border_color' => isset($_POST['border_color']) ? (sanitize_hex_color(wp_unslash($_POST['border_color'])) ?: '#e5e5e5') : '#e5e5e5', 'margin_top' => max(0, min(100, isset($_POST['margin_top']) ? (int) $_POST['margin_top'] : 20)), 'margin_bottom' => max(0, min(100, isset($_POST['margin_bottom']) ? (int) $_POST['margin_bottom'] : 20)), 'image_max_width' => max(20, min(200, isset($_POST['image_max_width']) ? (int) $_POST['image_max_width'] : 70)), 'image_max_height' => max(20, min(200, isset($_POST['image_max_height']) ? (int) $_POST['image_max_height'] : 70)), 'mobile_columns' => isset($_POST['mobile_columns']) ? (string) (int) $_POST['mobile_columns'] : '2', 'use_shortcode' => !empty($_POST['use_shortcode']), 'custom_class' => isset($_POST['custom_class']) ? sanitize_html_class(wp_unslash($_POST['custom_class'])) : '', ), false); wp_send_json_success(array('message' => __('Trust Badges settings saved.', 'ai-eshop-optimizer'))); } public function enqueue_styles() { if (!function_exists('is_product') || !is_product()) { return; } $version = defined('AIEO_VERSION') ? AIEO_VERSION : '1.0'; wp_enqueue_style( 'aieo-trust-badges-strip', plugins_url('assets/css/product-display/trust-badges.css', AIEO_FILE), array(), $version ); $s = $this->get_settings(); $css = sprintf( '.aieo-trust-badges{' . '--aieo-tb-font-size:%dpx;--aieo-tb-text-color:%s;--aieo-tb-border-color:%s;' . '--aieo-tb-margin-top:%dpx;--aieo-tb-margin-bottom:%dpx;--aieo-tb-align:%s;' . '--aieo-tb-image-max-width:%dpx;--aieo-tb-image-max-height:%dpx;' . '}', (int) $s['font_size'], esc_attr($s['text_color']), esc_attr($s['border_color']), (int) $s['margin_top'], (int) $s['margin_bottom'], esc_attr($s['align']), (int) $s['image_max_width'], (int) $s['image_max_height'] ); wp_add_inline_style('aieo-trust-badges-strip', $css); } public function is_active() { $s = $this->get_settings(); return !empty($s['enabled']); } public function output() { if (!$this->is_active()) { return; } static $rendered = false; if ($rendered) { return; } $rendered = true; echo $this->get_html(); } public function shortcode_handler($atts = array()) { $s = $this->get_settings(); if (empty($s['enabled']) || empty($s['use_shortcode'])) { return ''; } if (!is_singular('product')) { return ''; } return $this->get_html(); } private function effective_badge($key) { $s = $this->get_settings(); $catalog = $this->builtin_badges(); $b = isset($catalog[$key]) ? $catalog[$key] : array(); $custom = array(); foreach ((array) $s['badge_overrides'] as $o) { if (isset($o['badge_key']) && $o['badge_key'] === $key) { $custom = $o; break; } } return array( 'label' => !empty($custom['label']) ? (string) $custom['label'] : (isset($b['label']) ? (string) $b['label'] : ucwords(str_replace('-', ' ', $key))), 'description' => !empty($custom['description']) ? (string) $custom['description'] : (isset($b['description']) ? (string) $b['description'] : ''), 'image_url' => isset($b['image_url']) ? (string) $b['image_url'] : '', ); } private function get_html() { $s = $this->get_settings(); $defaults = (array) $s['default_badges']; $hidden = (array) $s['hidden_badges']; $visible = array_values(array_diff($defaults, $hidden)); $customs = array_filter(array_map('absint', explode(',', (string) $s['badges']))); if (empty($visible) && empty($customs)) { return ''; } $cls = 'aieo-trust-badges om-ppe-trust-badges' . (!empty($s['custom_class']) ? ' ' . esc_attr((string) $s['custom_class']) : ''); $cols = (string) (int) $s['mobile_columns']; $visible_count_for_grid = 0; foreach ($visible as $key) { $b = $this->effective_badge($key); if (!empty($b['image_url'])) { $visible_count_for_grid++; } } $visible_count_for_grid += count($customs); $odd_attr = ($visible_count_for_grid % 2 === 1) ? ' data-total-odd="true"' : ''; ob_start(); ?>
get_settings(); $catalog = $this->builtin_badges(); $defaults = (array) $s['default_badges']; $hidden = (array) $s['hidden_badges']; $overrides_by_key = array(); foreach ((array) $s['badge_overrides'] as $o) { if (!empty($o['badge_key'])) { $overrides_by_key[$o['badge_key']] = $o; } } $id_of = static function ($name) { return 'aieo-tb-' . str_replace('_', '-', $name); }; $row = static function ($name, $label, $control, $help = '') use ($id_of) { $field = array('id' => $id_of($name), 'label' => $label, 'control' => $control); if ('' !== $help) { $field['help'] = $help; } return \Rooster\Admin\Ui::field_row($field); }; $switch = static function ($name, $text, $hidden_label = false) use ($s, $id_of) { return \Rooster\Admin\Ui::toggle($name, !empty($s[$name]), array('id' => $id_of($name), 'hidden' => false, 'label' => $text, 'label_hidden' => $hidden_label)); }; $px = static function ($name, $min, $max) use ($s, $id_of) { return \Rooster\Admin\Ui::input($name, (string) (int) $s[$name], array('id' => $id_of($name), 'type' => 'number', 'xs' => true, 'unit' => 'px', 'min' => $min, 'max' => $max)); }; $card = static function ($anchor, $icon, $title, $col, $fields, $extra = array()) { $form_id = 'aieo-card-' . $anchor . '-form'; list($open, $close) = AIEO_DMM_UI::card(array( 'anchor' => $anchor, 'icon' => $icon, 'title' => $title, 'col' => $col, 'footer' => class_exists('\Rooster\Admin\States') ? \Rooster\Admin\States::save_button($form_id) : '', ) + $extra); echo $open; echo AIEO_DMM_UI::card_form('aieo_dmm_trust_badges_save', 'trust_badges', $form_id, '