| @@ -1,7 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace EverCompare\Frontend; |
| 3 | 3 | |
| 4 | +defined( 'ABSPATH' ) || exit; | |
| 4 | 5 | /** |
| 5 | 6 | * Shortcode handler class |
| 6 | 7 | */ |
| 7 | 8 | class Shortcode { |
| @@ -28,8 +29,9 @@ | ||
| 28 | 29 | */ |
| 29 | 30 | function __construct() { |
| 30 | 31 | add_shortcode( 'evercompare_button', [ $this, 'button_shortcode' ] ); |
| 31 | 32 | add_shortcode( 'evercompare_table', [ $this, 'table_shortcode' ] ); |
| 33 | + add_shortcode( 'evercompare_counter', [ $this, 'counter_shortcode' ] ); | |
| 32 | 34 | } |
| 33 | 35 | |
| 34 | 36 | /** |
| 35 | 37 | * [button_shortcode] Compare Button Shortcode callable function |
| @@ -37,11 +39,74 @@ | ||
| 37 | 39 | * @param string $content |
| 38 | 40 | * @return [HTML] |
| 39 | 41 | */ |
| 40 | 42 | public function button_shortcode( $atts, $content = '' ){ |
| 43 | + | |
| 41 | 44 | wp_enqueue_style( 'evercompare-frontend' ); |
| 42 | 45 | wp_enqueue_script( 'evercompare-frontend' ); |
| 43 | - return Manage_Compare::instance()->add_to_compare_btn(); | |
| 46 | + | |
| 47 | + global $product; | |
| 48 | + $product_id = ''; | |
| 49 | + $product_title = ''; | |
| 50 | + if ( $product && is_a( $product, 'WC_Product' ) ) { | |
| 51 | + $product_id = $product->get_id(); | |
| 52 | + $product_title = $product->get_name(); | |
| 53 | + } else if (get_post_type(get_the_ID()) === 'product') { | |
| 54 | + $product_id = get_the_ID(); | |
| 55 | + $product_title = get_the_title($product_id); | |
| 56 | + } | |
| 57 | + | |
| 58 | + // Fetch option data | |
| 59 | + $remove_on_click = ever_compare_get_option( 'remove_on_click','ever_compare_settings_tabs', 'off' ); | |
| 60 | + $button_text = ever_compare_get_option( 'button_text','ever_compare_settings_tabs', 'Compare' ); | |
| 61 | + $button_added_text = ever_compare_get_option( 'added_button_text','ever_compare_settings_tabs', 'Added' ); | |
| 62 | + $button_remove_text = ever_compare_get_option( 'remove_button_text','ever_compare_settings_tabs', 'Remove' ); | |
| 63 | + $button_remove_text = empty($button_remove_text) ? __('Remove', 'ever-compare') : $button_remove_text; | |
| 64 | + | |
| 65 | + $shop_page_btn_position = ever_compare_get_option( 'shop_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); | |
| 66 | + $product_page_btn_position = ever_compare_get_option( 'product_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); | |
| 67 | + $button_style = ever_compare_get_option( 'button_style', 'ever_compare_style_tabs', 'theme' ); | |
| 68 | + | |
| 69 | + $button_class = array( | |
| 70 | + 'htcompare-btn', | |
| 71 | + 'htcompare-btn-style-'.$button_style, | |
| 72 | + 'htcompare-shop-'.$shop_page_btn_position, | |
| 73 | + 'htcompare-product-'.$product_page_btn_position, | |
| 74 | + ); | |
| 75 | + | |
| 76 | + if( $button_style === 'theme' ){ | |
| 77 | + $button_class[] = 'button'; | |
| 78 | + } | |
| 79 | + | |
| 80 | + $button_icon = $this->get_icon(); | |
| 81 | + $added_button_icon = $this->get_icon('added_'); | |
| 82 | + | |
| 83 | + if( !empty( $button_text ) ){ | |
| 84 | + $button_text = '<span class="evercompare-btn-text">'.$button_text.'</span>'; | |
| 85 | + } | |
| 86 | + | |
| 87 | + if($remove_on_click === 'off'){ | |
| 88 | + if( !empty( $button_added_text ) ){ | |
| 89 | + $button_added_text = '<span class="evercompare-btn-text">'.$button_added_text.'</span>'; | |
| 90 | + } | |
| 91 | + } else { | |
| 92 | + $button_added_text = '<span class="evercompare-btn-text">'.$button_remove_text.'</span>'; | |
| 93 | + } | |
| 94 | + | |
| 95 | + // Shortcode atts | |
| 96 | + $default_atts = array( | |
| 97 | + 'product_id' => $product_id, | |
| 98 | + 'product_title' => $product_title, | |
| 99 | + 'button_url' => Manage_Compare::instance()->get_compare_page_url(), | |
| 100 | + 'button_class' => implode(' ', $button_class ), | |
| 101 | + 'button_text' => $button_icon.$button_text, | |
| 102 | + 'button_added_text' => $added_button_icon.$button_added_text, | |
| 103 | + 'template_name' => 'add', | |
| 104 | + ); | |
| 105 | + | |
| 106 | + $atts = shortcode_atts( $default_atts, $atts, $content ); | |
| 107 | + return Manage_Compare::instance()->button_html( $atts ); | |
| 108 | + | |
| 44 | 109 | } |
| 45 | 110 | |
| 46 | 111 | /** |
| 47 | 112 | * [table_shortcode] Compare table shortcode callable function |
| @@ -49,10 +114,92 @@ | ||
| 49 | 114 | * @param string $content |
| 50 | 115 | * @return [HTML] |
| 51 | 116 | */ |
| 52 | 117 | public function table_shortcode( $atts, $content = '' ){ |
| 118 | + | |
| 53 | 119 | wp_enqueue_style( 'evercompare-frontend' ); |
| 54 | 120 | wp_enqueue_script( 'evercompare-frontend' ); |
| 55 | - return Manage_Compare::instance()->compare_table_shortcode(); | |
| 121 | + | |
| 122 | + // Shareable link via query param — render server-side (not cached due to query param) | |
| 123 | + if ( ! empty( $_GET['evcompare'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Recommended | |
| 124 | + /* Fetch From option data */ | |
| 125 | + $empty_compare_text = ever_compare_get_option('empty_table_text','ever_compare_table_settings_tabs'); | |
| 126 | + $return_shop_button = ever_compare_get_option('shop_button_text','ever_compare_table_settings_tabs','Return to shop'); | |
| 127 | + | |
| 128 | + /* Product and Field */ | |
| 129 | + $products = Manage_Compare::instance()->get_compared_products_data(); | |
| 130 | + $fields = Manage_Compare::instance()->get_compare_fields(); | |
| 131 | + | |
| 132 | + $custom_heading = !empty( ever_compare_get_option( 'table_heading', 'ever_compare_table_settings_tabs' ) ) ? ever_compare_get_option( 'table_heading', 'ever_compare_table_settings_tabs' ) : array(); | |
| 133 | + | |
| 134 | + $default_atts = array( | |
| 135 | + 'evercompare' => Manage_Compare::instance(), | |
| 136 | + 'products' => $products, | |
| 137 | + 'fields' => $fields, | |
| 138 | + 'return_shop_button'=> $return_shop_button, | |
| 139 | + 'heading_txt' => $custom_heading, | |
| 140 | + 'empty_compare_text'=> $empty_compare_text, | |
| 141 | + ); | |
| 142 | + | |
| 143 | + $atts = shortcode_atts( $default_atts, $atts, $content ); | |
| 144 | + return Manage_Compare::instance()->table_html( $atts ); | |
| 145 | + } | |
| 146 | + | |
| 147 | + // Cookie-based: render placeholder that JS will populate via AJAX | |
| 148 | + return '<div class="htcompare-table" data-ajax-load="true"></div>'; | |
| 149 | + | |
| 56 | 150 | } |
| 151 | + | |
| 152 | + /** | |
| 153 | + * Evercompare Counter Shortcode | |
| 154 | + * | |
| 155 | + * @param [array] $atts | |
| 156 | + * @param string $content | |
| 157 | + * @return void | |
| 158 | + */ | |
| 159 | + public function counter_shortcode( $atts, $content = '' ){ | |
| 160 | + | |
| 161 | + wp_enqueue_style( 'evercompare-frontend' ); | |
| 162 | + wp_enqueue_script( 'evercompare-frontend' ); | |
| 163 | + | |
| 164 | + $default_atts = array( | |
| 165 | + 'count' => 0, | |
| 166 | + 'page_url' => Manage_Compare::instance()->get_compare_page_url(), | |
| 167 | + ); | |
| 168 | + | |
| 169 | + $atts = shortcode_atts( $default_atts, $atts, $content ); | |
| 170 | + return Manage_Compare::instance()->count_html( $atts ); | |
| 171 | + | |
| 172 | + } | |
| 173 | + | |
| 174 | + /** | |
| 175 | + * [get_icon] | |
| 176 | + * @param string $type | |
| 177 | + * @return [HTML] | |
| 178 | + */ | |
| 179 | + public function get_icon( $type = '' ){ | |
| 180 | + | |
| 181 | + $default_icon = ever_compare_icon_list('default'); | |
| 182 | + $default_loader = '<span class="ever-compare-loader">'.ever_compare_icon_list('loading').'</span>'; | |
| 183 | + | |
| 184 | + $button_text = ( $type === 'added' ) ? ever_compare_get_option( 'added_button_text','ever_compare_settings_tabs', 'Added' ) : ever_compare_get_option( 'button_text','ever_compare_settings_tabs', 'Compare' ); | |
| 185 | + | |
| 186 | + $button_icon_type = ever_compare_get_option( $type.'button_icon_type', 'ever_compare_settings_tabs', 'default' ); | |
| 187 | + | |
| 188 | + if( $button_icon_type === 'custom' ){ | |
| 189 | + $button_icon = ever_compare_get_option( $type.'button_custom_icon','ever_compare_settings_tabs', '' ); | |
| 190 | + }elseif( $button_icon_type === 'default' ){ | |
| 191 | + return $default_icon; | |
| 192 | + }else{ | |
| 193 | + $button_icon = ''; | |
| 194 | + } | |
| 195 | + | |
| 196 | + if( !empty( $button_icon ) ){ | |
| 197 | + $button_icon = '<span class="ever-compare-btn-image"><img src="'.esc_url( $button_icon ).'" alt="'.esc_attr( $button_text ).'"></span>'; | |
| 198 | + } | |
| 199 | + | |
| 200 | + return $default_loader.$button_icon; | |
| 201 | + | |
| 202 | + } | |
| 203 | + | |
| 57 | 204 | |
| 58 | 205 | } |