| @@ -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 { |
| @@ -42,12 +44,24 @@ | ||
| 42 | 44 | wp_enqueue_style( 'evercompare-frontend' ); |
| 43 | 45 | wp_enqueue_script( 'evercompare-frontend' ); |
| 44 | 46 | |
| 45 | 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 | + } | |
| 46 | 57 | |
| 47 | 58 | // Fetch option data |
| 59 | + $remove_on_click = ever_compare_get_option( 'remove_on_click','ever_compare_settings_tabs', 'off' ); | |
| 48 | 60 | $button_text = ever_compare_get_option( 'button_text','ever_compare_settings_tabs', 'Compare' ); |
| 49 | 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; | |
| 50 | 64 | |
| 51 | 65 | $shop_page_btn_position = ever_compare_get_option( 'shop_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 52 | 66 | $product_page_btn_position = ever_compare_get_option( 'product_btn_position', 'ever_compare_settings_tabs', 'after_cart_btn' ); |
| 53 | 67 | $button_style = ever_compare_get_option( 'button_style', 'ever_compare_style_tabs', 'theme' ); |
| @@ -69,19 +83,20 @@ | ||
| 69 | 83 | if( !empty( $button_text ) ){ |
| 70 | 84 | $button_text = '<span class="evercompare-btn-text">'.$button_text.'</span>'; |
| 71 | 85 | } |
| 72 | 86 | |
| 73 | - if( !empty( $button_added_text ) ){ | |
| 74 | - $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>'; | |
| 75 | 93 | } |
| 76 | 94 | |
| 77 | - if(\EverCompare\Frontend\Manage_Compare::instance()->is_product_in_compare( $product->get_id() )) { | |
| 78 | - $button_class[] = 'added'; | |
| 79 | - } | |
| 80 | - | |
| 81 | 95 | // Shortcode atts |
| 82 | 96 | $default_atts = array( |
| 83 | - 'product_id' => $product->get_id(), | |
| 97 | + 'product_id' => $product_id, | |
| 98 | + 'product_title' => $product_title, | |
| 84 | 99 | 'button_url' => Manage_Compare::instance()->get_compare_page_url(), |
| 85 | 100 | 'button_class' => implode(' ', $button_class ), |
| 86 | 101 | 'button_text' => $button_icon.$button_text, |
| 87 | 102 | 'button_added_text' => $added_button_icon.$button_added_text, |
| @@ -99,34 +114,40 @@ | ||
| 99 | 114 | * @param string $content |
| 100 | 115 | * @return [HTML] |
| 101 | 116 | */ |
| 102 | 117 | public function table_shortcode( $atts, $content = '' ){ |
| 103 | - | |
| 118 | + | |
| 104 | 119 | wp_enqueue_style( 'evercompare-frontend' ); |
| 105 | 120 | wp_enqueue_script( 'evercompare-frontend' ); |
| 106 | 121 | |
| 107 | - /* Fetch From option data */ | |
| 108 | - $empty_compare_text = ever_compare_get_option('empty_table_text','ever_compare_table_settings_tabs'); | |
| 109 | - $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'); | |
| 110 | 127 | |
| 111 | - /* Product and Field */ | |
| 112 | - $products = Manage_Compare::instance()->get_compared_products_data(); | |
| 113 | - $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(); | |
| 114 | 131 | |
| 115 | - $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(); | |
| 116 | 133 | |
| 117 | - $default_atts = array( | |
| 118 | - 'evercompare' => Manage_Compare::instance(), | |
| 119 | - 'products' => $products, | |
| 120 | - 'fields' => $fields, | |
| 121 | - 'return_shop_button'=> $return_shop_button, | |
| 122 | - 'heading_txt' => $custom_heading, | |
| 123 | - 'empty_compare_text'=> $empty_compare_text, | |
| 124 | - ); | |
| 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 | + ); | |
| 125 | 142 | |
| 126 | - $atts = shortcode_atts( $default_atts, $atts, $content ); | |
| 127 | - return Manage_Compare::instance()->table_html( $atts ); | |
| 143 | + $atts = shortcode_atts( $default_atts, $atts, $content ); | |
| 144 | + return Manage_Compare::instance()->table_html( $atts ); | |
| 145 | + } | |
| 128 | 146 | |
| 147 | + // Cookie-based: render placeholder that JS will populate via AJAX | |
| 148 | + return '<div class="htcompare-table" data-ajax-load="true"></div>'; | |
| 149 | + | |
| 129 | 150 | } |
| 130 | 151 | |
| 131 | 152 | /** |
| 132 | 153 | * Evercompare Counter Shortcode |
| @@ -140,9 +161,9 @@ | ||
| 140 | 161 | wp_enqueue_style( 'evercompare-frontend' ); |
| 141 | 162 | wp_enqueue_script( 'evercompare-frontend' ); |
| 142 | 163 | |
| 143 | 164 | $default_atts = array( |
| 144 | - 'count' => count(Manage_Compare::instance()->get_compared_products()), | |
| 165 | + 'count' => 0, | |
| 145 | 166 | 'page_url' => Manage_Compare::instance()->get_compare_page_url(), |
| 146 | 167 | ); |
| 147 | 168 | |
| 148 | 169 | $atts = shortcode_atts( $default_atts, $atts, $content ); |