PluginProbe
Ever Compare – Products Compare Plugin for WooCommerce / 1.3.5
Ever Compare – Products Compare Plugin for WooCommerce v1.3.5
1.3.7 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 trunk 1.0.0 1.0.1 1.0.2 All 31 releases
ever-compare / includes / classes / Frontend / Shortcode.php

Shortcode.php in Ever Compare – Products Compare Plugin for WooCommerce 1.3.5, at includes/classes/Frontend/Shortcode.php

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