| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace EverCompare\Frontend; |
| 3 | + | |
| 4 | +defined( 'ABSPATH' ) || exit; | |
| 3 | 5 | /** |
| 4 | 6 | * Shortcode handler class |
| 5 | 7 | */ |
| 6 | 8 | class Shortcode { |
| @@ -27,8 +29,9 @@ | ||
| 27 | 29 | */ |
| 28 | 30 | function __construct() { |
| 29 | 31 | add_shortcode( 'evercompare_button', [ $this, 'button_shortcode' ] ); |
| 30 | 32 | add_shortcode( 'evercompare_table', [ $this, 'table_shortcode' ] ); |
| 33 | + add_shortcode( 'evercompare_counter', [ $this, 'counter_shortcode' ] ); | |
| 31 | 34 | } |
| 32 | 35 | |
| 33 | 36 | /** |
| 34 | 37 | * [button_shortcode] Compare Button Shortcode callable function |
| @@ -36,29 +39,68 @@ | ||
| 36 | 39 | * @param string $content |
| 37 | 40 | * @return [HTML] |
| 38 | 41 | */ |
| 39 | 42 | public function button_shortcode( $atts, $content = '' ){ |
| 43 | + | |
| 40 | 44 | wp_enqueue_style( 'evercompare-frontend' ); |
| 41 | 45 | wp_enqueue_script( 'evercompare-frontend' ); |
| 42 | 46 | |
| 43 | 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 | + } | |
| 44 | 57 | |
| 45 | 58 | // Fetch option data |
| 46 | - $button_text = ever_compare_get_option( 'button_text','ever_compare_table_settings_tabs', 'Compare' ); | |
| 47 | - $button_added_text = ever_compare_get_option( 'added_button_text','ever_compare_table_settings_tabs', 'Added' ); | |
| 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; | |
| 48 | 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 | + | |
| 49 | 69 | $button_class = array( |
| 50 | 70 | 'htcompare-btn', |
| 51 | - 'button', | |
| 71 | + 'htcompare-btn-style-'.$button_style, | |
| 72 | + 'htcompare-shop-'.$shop_page_btn_position, | |
| 73 | + 'htcompare-product-'.$product_page_btn_position, | |
| 52 | 74 | ); |
| 53 | 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 | + | |
| 54 | 95 | // Shortcode atts |
| 55 | 96 | $default_atts = array( |
| 56 | - 'product_id' => $product->get_id(), | |
| 97 | + 'product_id' => $product_id, | |
| 98 | + 'product_title' => $product_title, | |
| 57 | 99 | 'button_url' => Manage_Compare::instance()->get_compare_page_url(), |
| 58 | 100 | 'button_class' => implode(' ', $button_class ), |
| 59 | - 'button_text' => $button_text, | |
| 60 | - 'button_added_text' => $button_added_text, | |
| 101 | + 'button_text' => $button_icon.$button_text, | |
| 102 | + 'button_added_text' => $added_button_icon.$button_added_text, | |
| 61 | 103 | 'template_name' => 'add', |
| 62 | 104 | ); |
| 63 | 105 | |
| 64 | 106 | $atts = shortcode_atts( $default_atts, $atts, $content ); |
| @@ -72,33 +114,92 @@ | ||
| 72 | 114 | * @param string $content |
| 73 | 115 | * @return [HTML] |
| 74 | 116 | */ |
| 75 | 117 | public function table_shortcode( $atts, $content = '' ){ |
| 118 | + | |
| 76 | 119 | wp_enqueue_style( 'evercompare-frontend' ); |
| 77 | 120 | wp_enqueue_script( 'evercompare-frontend' ); |
| 78 | 121 | |
| 79 | - /* Fetch From option data */ | |
| 80 | - $empty_compare_text = ever_compare_get_option('empty_table_text','ever_compare_table_settings_tabs'); | |
| 81 | - $return_shop_button = ever_compare_get_option('shop_button_text','ever_compare_table_settings_tabs','Return to shop'); | |
| 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'); | |
| 82 | 127 | |
| 83 | - /* Product and Field */ | |
| 84 | - $products = Manage_Compare::instance()->get_compared_products_data(); | |
| 85 | - $fields = Manage_Compare::instance()->get_compare_fields(); | |
| 128 | + /* Product and Field */ | |
| 129 | + $products = Manage_Compare::instance()->get_compared_products_data(); | |
| 130 | + $fields = Manage_Compare::instance()->get_compare_fields(); | |
| 86 | 131 | |
| 87 | - $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(); | |
| 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(); | |
| 88 | 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 | + | |
| 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 | + | |
| 89 | 164 | $default_atts = array( |
| 90 | - 'evercompare' => Manage_Compare::instance(), | |
| 91 | - 'products' => $products, | |
| 92 | - 'fields' => $fields, | |
| 93 | - 'return_shop_button'=> $return_shop_button, | |
| 94 | - 'heading_txt' => $custom_heading, | |
| 95 | - 'empty_compare_text'=> $empty_compare_text, | |
| 165 | + 'count' => 0, | |
| 166 | + 'page_url' => Manage_Compare::instance()->get_compare_page_url(), | |
| 96 | 167 | ); |
| 97 | 168 | |
| 98 | 169 | $atts = shortcode_atts( $default_atts, $atts, $content ); |
| 99 | - return Manage_Compare::instance()->table_html( $atts ); | |
| 170 | + return Manage_Compare::instance()->count_html( $atts ); | |
| 100 | 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 | + | |
| 101 | 202 | } |
| 102 | 203 | |
| 103 | 204 | |
| 104 | 205 | } |