PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.2.8
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.2.8
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / modules / trust-badges / class-trust-badges.php

class-trust-badges.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.2.8, at inc/modules/trust-badges/class-trust-badges.php

422 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Trust Badges.
5 *
6 * @package Merchant
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Trust Badges Class.
15 *
16 */
17 class Merchant_Trust_Badges extends Merchant_Add_Module {
18
19 /**
20 * Module ID.
21 *
22 */
23 const MODULE_ID = 'trust-badges';
24
25 /**
26 * Is module preview.
27 *
28 */
29 public static $is_module_preview = false;
30
31 /**
32 * Whether the module has a shortcode or not.
33 *
34 * @var bool
35 */
36 public $has_shortcode = true;
37
38 /**
39 * Constructor.
40 *
41 */
42 public function __construct() {
43 // Module id.
44 $this->module_id = self::MODULE_ID;
45
46 // WooCommerce only.
47 $this->wc_only = true;
48
49 // Parent construct.
50 parent::__construct();
51
52 // Module section.
53 $this->module_section = 'build-trust';
54
55 // Module default settings.
56 $this->module_default_settings = array(
57 'badges' => '',
58 'align' => 'center',
59 'title' => __( 'Product Quality Guaranteed!', 'merchant' ),
60 );
61
62 // Module data.
63 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
64
65 // Module options path.
66 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
67
68 // Is module preview page.
69 if ( is_admin() && parent::is_module_settings_page() ) {
70 self::$is_module_preview = true;
71
72 // Enqueue admin styles.
73 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
74
75 // Admin preview box.
76 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
77
78 // Custom CSS.
79 // The custom CSS should be added here as well due to ensure preview box works properly.
80 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
81 }
82
83 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
84 // Init translations.
85 $this->init_translations();
86 }
87
88 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
89 return;
90 }
91
92 // Return early if it's on admin but not in the respective module settings page.
93 if ( is_admin() && ! parent::is_module_settings_page() ) {
94 return;
95 }
96
97 // Enqueue styles.
98 add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );
99
100 // Add trust badges after add to cart form on single product pages.
101 add_action( 'woocommerce_single_product_summary', array( $this, 'trust_badges_output' ), 30 );
102
103 // Custom CSS.
104 add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) );
105
106 $this->backward_compatibility();
107 }
108
109 /**
110 * Print shortcode content.
111 *
112 * @return string
113 */
114 public function shortcode_handler() {
115 // Check if module is active.
116 if ( ! Merchant_Modules::is_module_active( $this->module_id ) ) {
117 return '';
118 }
119
120 // Check if shortcode is enabled.
121 if ( ! $this->is_shortcode_enabled() ) {
122 return '';
123 }
124
125 // Check if we are on a single product page.
126 if ( ! is_singular( 'product' ) ) {
127 // If user is admin, show error message.
128 if ( current_user_can( 'manage_options' ) ) {
129 return $this->shortcode_placement_error();
130 }
131
132 return '';
133 }
134 ob_start();
135
136 $settings = $this->get_module_settings();
137 $default_badges = ! empty( $settings['default_badges'] ) ? $settings['default_badges'] : array();
138 $badges = $this->get_badges( $settings['badges'] );
139 if ( ! empty( $default_badges ) || ! empty( $badges ) ) {
140 ?>
141 <fieldset class="merchant-trust-badges">
142 <?php
143 if ( ! empty( $settings['title'] ) ) : ?>
144 <legend class="merchant-trust-badges-title"><?php
145 echo esc_html( Merchant_Translator::translate( $settings['title'] ) ); ?></legend>
146 <?php
147 endif; ?>
148 <div class="merchant-trust-badges-images">
149 <?php
150 foreach ( $default_badges as $badge_name ) {
151 $badge_src = MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/' . $badge_name . '.svg';
152 echo '<img src="' . esc_url( $badge_src ) . '" class="is-placeholder" alt="' . esc_attr( $badge_name ) . '"/>';
153 }
154 foreach ( $badges as $image_id ) {
155 echo wp_kses_post( wp_get_attachment_image( $image_id, 'full' ) );
156 }
157 ?>
158 </div>
159 </fieldset>
160 <?php
161 }
162 $shortcode_content = ob_get_clean();
163
164 /**
165 * Filter the shortcode html content.
166 *
167 * @param string $shortcode_content shortcode html content
168 * @param string $module_id module id
169 * @param int $post_id product id
170 *
171 * @since 1.8
172 */
173 return apply_filters( 'merchant_module_shortcode_content_html', $shortcode_content, $this->module_id, get_the_ID() );
174 }
175
176 /**
177 * Init translations.
178 *
179 * @return void
180 */
181 public function init_translations() {
182 $settings = $this->get_module_settings();
183 if ( ! empty( $settings['title'] ) ) {
184 Merchant_Translator::register_string( $settings['title'], esc_html__( 'Trust badges text above logos', 'merchant' ) );
185 }
186 }
187
188 /**
189 * Admin enqueue CSS.
190 *
191 * @return void
192 */
193 public function admin_enqueue_css() {
194 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
195 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
196
197 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
198 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/trust-badges.min.css', array(), MERCHANT_VERSION );
199 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
200 }
201 }
202
203 /**
204 * Enqueue CSS.
205 *
206 * @return void
207 */
208 public function enqueue_css() {
209 if ( ! is_singular( 'product' ) && ! Merchant_Modules::is_module_active( 'quick-view' ) ) {
210 return;
211 }
212
213 // Specific module styles.
214 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/trust-badges.min.css', array(), MERCHANT_VERSION );
215 }
216
217 /**
218 * Render admin preview
219 *
220 * @param Merchant_Admin_Preview $preview
221 * @param string $module
222 *
223 * @return Merchant_Admin_Preview
224 */
225 public function render_admin_preview( $preview, $module ) {
226 if ( self::MODULE_ID === $module ) {
227 ob_start();
228 self::admin_preview_content();
229 $content = ob_get_clean();
230
231 // HTML.
232 $preview->set_html( $content );
233
234 // Text Above the Logos.
235 $preview->set_text( 'title', '.merchant-trust-badges-title' );
236 }
237
238 return $preview;
239 }
240
241 /**
242 * Admin preview content.
243 *
244 * @return void
245 */
246 public function admin_preview_content() {
247 ?>
248 <div class="mrc-preview-single-product-elements">
249 <div class="mrc-preview-left-column">
250 <div class="mrc-preview-product-image-wrapper">
251 <div class="mrc-preview-product-image"></div>
252 <div class="mrc-preview-product-image-thumbs">
253 <div class="mrc-preview-product-image-thumb"></div>
254 <div class="mrc-preview-product-image-thumb"></div>
255 <div class="mrc-preview-product-image-thumb"></div>
256 </div>
257 </div>
258 </div>
259 <div class="mrc-preview-right-column">
260 <div class="mrc-preview-text-placeholder"></div>
261 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
262 <div class="mrc-preview-text-placeholder mrc-mw-30 mrc-hide-on-smaller-screens"></div>
263 <div class="mrc-preview-addtocart-placeholder mrc-hide-on-smaller-screens"></div>
264 <?php
265 $this->trust_badges_output(); ?>
266 </div>
267 </div>
268
269 <?php
270 }
271
272 /**
273 * Get trust badges.
274 *
275 * @param string $badges
276 *
277 * @return array $badges Array of logos.
278 */
279 public function get_badges( $badges ) {
280 return ! empty( $badges )
281 ? explode( ',', $badges )
282 : array();
283 }
284
285 /**
286 * Render trust badges.
287 * TODO: Render through template files.
288 *
289 * @return void
290 */
291 public function trust_badges_output() {
292 if ( $this->is_shortcode_enabled() || ! is_product() ) {
293 return;
294 }
295
296 $settings = $this->get_module_settings();
297 $default_badges = ! empty( $settings['default_badges'] ) ? $settings['default_badges'] : array();
298 $badges = $this->get_badges( $settings['badges'] );
299 if ( empty( $default_badges ) && empty( $badges ) ) {
300 return;
301 }
302 ?>
303 <fieldset class="merchant-trust-badges">
304 <?php
305 if ( ! empty( $settings['title'] ) ) : ?>
306 <legend class="merchant-trust-badges-title"><?php
307 echo esc_html( Merchant_Translator::translate( $settings['title'] ) ); ?></legend>
308 <?php
309 endif; ?>
310 <div class="merchant-trust-badges-images">
311 <?php
312 foreach ( $default_badges as $badge_name ) {
313 $badge_src = MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/' . $badge_name . '.svg';
314 echo '<img src="' . esc_url( $badge_src ) . '" alt="' . esc_attr( $badge_name ) . '"/>';
315 }
316 foreach ( $badges as $image_id ) {
317 echo wp_kses_post( wp_get_attachment_image( $image_id, 'full' ) );
318 }
319 ?>
320 </div>
321 </fieldset>
322 <?php
323 }
324
325 /**
326 * Custom CSS.
327 *
328 * @return string
329 */
330 public function get_module_custom_css() {
331 $css = '';
332
333 // Font Size.
334 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'font-size', 15, '.merchant-trust-badges', '--mrc-tb-font-size', 'px' );
335
336 // Text Color.
337 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'text-color', '#212121', '.merchant-trust-badges', '--mrc-tb-text-color' );
338
339 // Border Color.
340 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'border-color', '#e5e5e5', '.merchant-trust-badges', '--mrc-tb-border-color' );
341
342 // Margin Top.
343 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'margin-top', 20, '.merchant-trust-badges', '--mrc-tb-margin-top', 'px' );
344
345 // Margin Bottom.
346 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'margin-bottom', 20, '.merchant-trust-badges', '--mrc-tb-margin-bottom', 'px' );
347
348 // Align.
349 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'align', 'center', '.merchant-trust-badges', '--mrc-tb-align' );
350
351 // Image Max Width.
352 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'image-max-width', 70, '.merchant-trust-badges', '--mrc-tb-image-max-width', 'px' );
353
354 // Image Max Height.
355 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'image-max-height', 70, '.merchant-trust-badges', '--mrc-tb-image-max-height', 'px' );
356
357 return $css;
358 }
359
360 /**
361 * Admin custom CSS.
362 *
363 * @param string $css The custom CSS.
364 *
365 * @return string $css The custom CSS.
366 */
367 public function admin_custom_css( $css ) {
368 $css .= $this->get_module_custom_css();
369
370 return $css;
371 }
372
373 /**
374 * Frontend custom CSS.
375 *
376 * @param string $css The custom CSS.
377 *
378 * @return string $css The custom CSS.
379 */
380 public function frontend_custom_css( $css ) {
381 if ( ! is_singular( 'product' ) && ! Merchant_Modules::is_module_active( 'quick-view' ) ) {
382 return $css;
383 }
384
385 $css .= $this->get_module_custom_css();
386
387 return $css;
388 }
389
390 /**
391 * Backward compatibility.
392 *
393 * @return void
394 */
395 public function backward_compatibility() {
396 $option_key = 'merchant_trust_badges_default_badges';
397 $flag = get_option( $option_key, false );
398
399 if ( ! $flag ) {
400 $settings = $this->get_module_settings();
401 if ( empty( $settings['badges'] ) ) {
402 Merchant_Admin_Options::set(
403 $this->module_id,
404 'default_badges',
405 array(
406 'badge1',
407 'badge2',
408 'badge3',
409 )
410 );
411 }
412
413 update_option( $option_key, true );
414 }
415 }
416 }
417
418 // Initialize the module.
419 add_action( 'init', function () {
420 Merchant_Modules::create_module( new Merchant_Trust_Badges() );
421 } );
422