PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.1
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.1
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.3.1, at inc/modules/trust-badges/class-trust-badges.php

455 lines 13.3 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 * Initialize the module's option group definitions.
40 *
41 * @return array<int, array<string, mixed>> Array of option group arrays.
42 */
43 protected function init_option_groups() {
44 return require MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
45 }
46
47 /**
48 * Constructor.
49 *
50 */
51 public function __construct() {
52 // Module id.
53 $this->module_id = self::MODULE_ID;
54
55 // WooCommerce only.
56 $this->wc_only = true;
57
58 // Parent construct.
59 parent::__construct();
60
61 // Module section.
62 $this->module_section = 'build-trust';
63
64 // Module default settings.
65 $this->module_default_settings = array(
66 'badges' => '',
67 'align' => 'center',
68 'title' => __( 'Product Quality Guaranteed!', 'merchant' ),
69 );
70
71 // Module data.
72 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
73
74 // AI prompt examples.
75 $this->ai_examples = array(
76 'blurb' => esc_html__( 'See what AI can do for your trust badges, from choosing which ones appear on your product pages to restyling the heading and how the badges line up.', 'merchant' ),
77 'prompts' => array(
78 esc_html__( 'Explore the abilities WPVibe gives you access to, then show all three built-in trust badges on my product pages', 'merchant' ),
79 esc_html__( "Check what abilities you have available through WPVibe, then change the text above the trust badges from 'Product Quality Guaranteed!' to 'Shop With Confidence'", 'merchant' ),
80 esc_html__( 'Look at your available WPVibe abilities, then left-align the trust badges and set the heading font size to 18px', 'merchant' ),
81 esc_html__( 'See what abilities WPVibe exposes, then cap the trust badge images at 60px wide and 60px tall', 'merchant' ),
82 ),
83 );
84
85 // Module options path.
86 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
87
88 // Is module preview page.
89 if ( is_admin() && parent::is_module_settings_page() ) {
90 self::$is_module_preview = true;
91
92 // Enqueue admin styles.
93 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
94
95 // Admin preview box.
96 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
97
98 // Custom CSS.
99 // The custom CSS should be added here as well due to ensure preview box works properly.
100 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
101 }
102
103 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
104 // Init translations.
105 $this->init_translations();
106 }
107
108 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
109 return;
110 }
111
112 // Return early if it's on admin but not in the respective module settings page.
113 if ( is_admin() && ! wp_doing_ajax() && ! parent::is_module_settings_page() ) {
114 return;
115 }
116
117 // Enqueue styles.
118 add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );
119
120 // Add trust badges after add to cart form on single product pages.
121 add_action( 'woocommerce_single_product_summary', array( $this, 'trust_badges_output' ), 30 );
122
123 /**
124 * Add trust badges inside the Quick View modal.
125 *
126 * @since 2.3.0
127 */
128 add_action( 'merchant_quick_view_product_summary', array( $this, 'trust_badges_output' ), 30 );
129
130 // Custom CSS.
131 add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) );
132
133 $this->backward_compatibility();
134 }
135
136 /**
137 * Print shortcode content.
138 *
139 * @return string
140 */
141 public function shortcode_handler() {
142 // Check if module is active.
143 if ( ! Merchant_Modules::is_module_active( $this->module_id ) ) {
144 return '';
145 }
146
147 // Check if shortcode is enabled.
148 if ( ! $this->is_shortcode_enabled() ) {
149 return '';
150 }
151
152 // Check if we are on a single product page.
153 if ( ! is_singular( 'product' ) ) {
154 // If user is admin, show error message.
155 if ( current_user_can( 'manage_options' ) ) {
156 return $this->shortcode_placement_error();
157 }
158
159 return '';
160 }
161 ob_start();
162
163 $settings = $this->get_module_settings();
164 $default_badges = ! empty( $settings['default_badges'] ) ? $settings['default_badges'] : array();
165 $badges = $this->get_badges( $settings['badges'] );
166 if ( ! empty( $default_badges ) || ! empty( $badges ) ) {
167 ?>
168 <fieldset class="merchant-trust-badges">
169 <?php
170 if ( ! empty( $settings['title'] ) ) : ?>
171 <legend class="merchant-trust-badges-title"><?php
172 echo esc_html( Merchant_Translator::translate( $settings['title'] ) ); ?></legend>
173 <?php
174 endif; ?>
175 <div class="merchant-trust-badges-images">
176 <?php
177 foreach ( $default_badges as $badge_name ) {
178 $badge_src = MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/' . $badge_name . '.svg';
179 echo '<img src="' . esc_url( $badge_src ) . '" class="is-placeholder" alt="' . esc_attr( $badge_name ) . '"/>';
180 }
181 foreach ( $badges as $image_id ) {
182 echo wp_kses_post( wp_get_attachment_image( $image_id, 'full' ) );
183 }
184 ?>
185 </div>
186 </fieldset>
187 <?php
188 }
189 $shortcode_content = ob_get_clean();
190
191 /**
192 * Filter the shortcode html content.
193 *
194 * @param string $shortcode_content shortcode html content
195 * @param string $module_id module id
196 * @param int $post_id product id
197 *
198 * @since 1.8
199 */
200 return apply_filters( 'merchant_module_shortcode_content_html', $shortcode_content, $this->module_id, get_the_ID() );
201 }
202
203 /**
204 * Init translations.
205 *
206 * @return void
207 */
208 public function init_translations() {
209 $settings = $this->get_module_settings();
210 if ( ! empty( $settings['title'] ) ) {
211 Merchant_Translator::register_string( $settings['title'], esc_html__( 'Trust badges text above logos', 'merchant' ) );
212 }
213 }
214
215 /**
216 * Admin enqueue CSS.
217 *
218 * @return void
219 */
220 public function admin_enqueue_css() {
221 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
222 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
223
224 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
225 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/trust-badges.min.css', array(), MERCHANT_VERSION );
226 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
227 }
228 }
229
230 /**
231 * Enqueue CSS.
232 *
233 * @return void
234 */
235 public function enqueue_css() {
236 if ( ! is_singular( 'product' ) && ! Merchant_Modules::is_module_active( 'quick-view' ) ) {
237 return;
238 }
239
240 // Specific module styles.
241 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/trust-badges.min.css', array(), MERCHANT_VERSION );
242 }
243
244 /**
245 * Render admin preview
246 *
247 * @param Merchant_Admin_Preview $preview
248 * @param string $module
249 *
250 * @return Merchant_Admin_Preview
251 */
252 public function render_admin_preview( $preview, $module ) {
253 if ( self::MODULE_ID === $module ) {
254 ob_start();
255 self::admin_preview_content();
256 $content = ob_get_clean();
257
258 // HTML.
259 $preview->set_html( $content );
260
261 // Text Above the Logos.
262 $preview->set_text( 'title', '.merchant-trust-badges-title' );
263 }
264
265 return $preview;
266 }
267
268 /**
269 * Admin preview content.
270 *
271 * @return void
272 */
273 public function admin_preview_content() {
274 ?>
275 <div class="mrc-preview-single-product-elements">
276 <div class="mrc-preview-left-column">
277 <div class="mrc-preview-product-image-wrapper">
278 <div class="mrc-preview-product-image"></div>
279 <div class="mrc-preview-product-image-thumbs">
280 <div class="mrc-preview-product-image-thumb"></div>
281 <div class="mrc-preview-product-image-thumb"></div>
282 <div class="mrc-preview-product-image-thumb"></div>
283 </div>
284 </div>
285 </div>
286 <div class="mrc-preview-right-column">
287 <div class="mrc-preview-text-placeholder"></div>
288 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
289 <div class="mrc-preview-text-placeholder mrc-mw-30 mrc-hide-on-smaller-screens"></div>
290 <div class="mrc-preview-addtocart-placeholder mrc-hide-on-smaller-screens"></div>
291 <?php
292 $this->trust_badges_output(); ?>
293 </div>
294 </div>
295
296 <?php
297 }
298
299 /**
300 * Get trust badges.
301 *
302 * @param string $badges
303 *
304 * @return array $badges Array of logos.
305 */
306 public function get_badges( $badges ) {
307 return ! empty( $badges )
308 ? explode( ',', $badges )
309 : array();
310 }
311
312 /**
313 * Render trust badges.
314 * TODO: Render through template files.
315 *
316 * @return void
317 */
318 public function trust_badges_output() {
319 if ( $this->is_shortcode_enabled() ) {
320 return;
321 }
322
323 // `is_product()` is false during the Quick View AJAX request, so also allow
324 // output when we're rendering inside the Quick View modal.
325 if ( ! is_product() && ! did_action( 'merchant_quick_view_before_add_to_cart' ) ) {
326 return;
327 }
328
329 $settings = $this->get_module_settings();
330 $default_badges = ! empty( $settings['default_badges'] ) ? $settings['default_badges'] : array();
331 $badges = $this->get_badges( $settings['badges'] );
332 if ( empty( $default_badges ) && empty( $badges ) ) {
333 return;
334 }
335 ?>
336 <fieldset class="merchant-trust-badges">
337 <?php
338 if ( ! empty( $settings['title'] ) ) : ?>
339 <legend class="merchant-trust-badges-title"><?php
340 echo esc_html( Merchant_Translator::translate( $settings['title'] ) ); ?></legend>
341 <?php
342 endif; ?>
343 <div class="merchant-trust-badges-images">
344 <?php
345 foreach ( $default_badges as $badge_name ) {
346 $badge_src = MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/' . $badge_name . '.svg';
347 echo '<img src="' . esc_url( $badge_src ) . '" alt="' . esc_attr( $badge_name ) . '"/>';
348 }
349 foreach ( $badges as $image_id ) {
350 echo wp_kses_post( wp_get_attachment_image( $image_id, 'full' ) );
351 }
352 ?>
353 </div>
354 </fieldset>
355 <?php
356 }
357
358 /**
359 * Custom CSS.
360 *
361 * @return string
362 */
363 public function get_module_custom_css() {
364 $css = '';
365
366 // Font Size.
367 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'font-size', 15, '.merchant-trust-badges', '--mrc-tb-font-size', 'px' );
368
369 // Text Color.
370 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'text-color', '#212121', '.merchant-trust-badges', '--mrc-tb-text-color' );
371
372 // Border Color.
373 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'border-color', '#e5e5e5', '.merchant-trust-badges', '--mrc-tb-border-color' );
374
375 // Margin Top.
376 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'margin-top', 20, '.merchant-trust-badges', '--mrc-tb-margin-top', 'px' );
377
378 // Margin Bottom.
379 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'margin-bottom', 20, '.merchant-trust-badges', '--mrc-tb-margin-bottom', 'px' );
380
381 // Align.
382 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'align', 'center', '.merchant-trust-badges', '--mrc-tb-align' );
383
384 // Image Max Width.
385 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'image-max-width', 70, '.merchant-trust-badges', '--mrc-tb-image-max-width', 'px' );
386
387 // Image Max Height.
388 $css .= Merchant_Custom_CSS::get_variable_css( 'trust-badges', 'image-max-height', 70, '.merchant-trust-badges', '--mrc-tb-image-max-height', 'px' );
389
390 return $css;
391 }
392
393 /**
394 * Admin custom CSS.
395 *
396 * @param string $css The custom CSS.
397 *
398 * @return string $css The custom CSS.
399 */
400 public function admin_custom_css( $css ) {
401 $css .= $this->get_module_custom_css();
402
403 return $css;
404 }
405
406 /**
407 * Frontend custom CSS.
408 *
409 * @param string $css The custom CSS.
410 *
411 * @return string $css The custom CSS.
412 */
413 public function frontend_custom_css( $css ) {
414 if ( ! is_singular( 'product' ) && ! Merchant_Modules::is_module_active( 'quick-view' ) ) {
415 return $css;
416 }
417
418 $css .= $this->get_module_custom_css();
419
420 return $css;
421 }
422
423 /**
424 * Backward compatibility.
425 *
426 * @return void
427 */
428 public function backward_compatibility() {
429 $option_key = 'merchant_trust_badges_default_badges';
430 $flag = get_option( $option_key, false );
431
432 if ( ! $flag ) {
433 $settings = $this->get_module_settings();
434 if ( empty( $settings['badges'] ) ) {
435 Merchant_Admin_Options::set(
436 $this->module_id,
437 'default_badges',
438 array(
439 'badge1',
440 'badge2',
441 'badge3',
442 )
443 );
444 }
445
446 update_option( $option_key, true );
447 }
448 }
449 }
450
451 // Initialize the module.
452 add_action( 'init', function () {
453 Merchant_Modules::create_module( new Merchant_Trust_Badges() );
454 } );
455