| @@ -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 |
| @@ -41,12 +44,24 @@ | ||
| 41 | 44 | wp_enqueue_style( 'evercompare-frontend' ); |
| 42 | 45 | wp_enqueue_script( 'evercompare-frontend' ); |
| 43 | 46 | |
| 44 | 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 | + } | |
| 45 | 57 | |
| 46 | 58 | // Fetch option data |
| 59 | + $remove_on_click = ever_compare_get_option( 'remove_on_click','ever_compare_settings_tabs', 'off' ); | |
| 47 | 60 | $button_text = ever_compare_get_option( 'button_text','ever_compare_settings_tabs', 'Compare' ); |
| 48 | 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; | |
| 49 | 64 | |
| 50 | 65 | $shop_page_btn_position = ever_compare_get_option( 'shop_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 51 | 66 | $product_page_btn_position = ever_compare_get_option( 'product_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 52 | 67 | $button_style = ever_compare_get_option( 'button_style', 'ever_compare_style_tabs', 'theme' ); |
| @@ -68,15 +83,20 @@ | ||
| 68 | 83 | if( !empty( $button_text ) ){ |
| 69 | 84 | $button_text = '<span class="evercompare-btn-text">'.$button_text.'</span>'; |
| 70 | 85 | } |
| 71 | 86 | |
| 72 | - if( !empty( $button_added_text ) ){ | |
| 73 | - $button_added_text = '<span class="evercompare-btn-text">'.$button_added_text.'</span>'; | |
| 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>'; | |
| 74 | 93 | } |
| 75 | 94 | |
| 76 | 95 | // Shortcode atts |
| 77 | 96 | $default_atts = array( |
| 78 | - 'product_id' => $product->get_id(), | |
| 97 | + 'product_id' => $product_id, | |
| 98 | + 'product_title' => $product_title, | |
| 79 | 99 | 'button_url' => Manage_Compare::instance()->get_compare_page_url(), |
| 80 | 100 | 'button_class' => implode(' ', $button_class ), |
| 81 | 101 | 'button_text' => $button_icon.$button_text, |
| 82 | 102 | 'button_added_text' => $added_button_icon.$button_added_text, |
| @@ -94,33 +114,61 @@ | ||
| 94 | 114 | * @param string $content |
| 95 | 115 | * @return [HTML] |
| 96 | 116 | */ |
| 97 | 117 | public function table_shortcode( $atts, $content = '' ){ |
| 98 | - | |
| 118 | + | |
| 99 | 119 | wp_enqueue_style( 'evercompare-frontend' ); |
| 100 | 120 | wp_enqueue_script( 'evercompare-frontend' ); |
| 101 | 121 | |
| 102 | - /* Fetch From option data */ | |
| 103 | - $empty_compare_text = ever_compare_get_option('empty_table_text','ever_compare_table_settings_tabs'); | |
| 104 | - $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'); | |
| 105 | 127 | |
| 106 | - /* Product and Field */ | |
| 107 | - $products = Manage_Compare::instance()->get_compared_products_data(); | |
| 108 | - $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(); | |
| 109 | 131 | |
| 110 | - $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(); | |
| 111 | 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 | + | |
| 112 | 164 | $default_atts = array( |
| 113 | - 'evercompare' => Manage_Compare::instance(), | |
| 114 | - 'products' => $products, | |
| 115 | - 'fields' => $fields, | |
| 116 | - 'return_shop_button'=> $return_shop_button, | |
| 117 | - 'heading_txt' => $custom_heading, | |
| 118 | - 'empty_compare_text'=> $empty_compare_text, | |
| 165 | + 'count' => 0, | |
| 166 | + 'page_url' => Manage_Compare::instance()->get_compare_page_url(), | |
| 119 | 167 | ); |
| 120 | 168 | |
| 121 | 169 | $atts = shortcode_atts( $default_atts, $atts, $content ); |
| 122 | - return Manage_Compare::instance()->table_html( $atts ); | |
| 170 | + return Manage_Compare::instance()->count_html( $atts ); | |
| 123 | 171 | |
| 124 | 172 | } |
| 125 | 173 | |
| 126 | 174 | /** |