PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.10.3
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.10.3
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 / payment-logos / class-payment-logos.php

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

528 lines 14.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Payment Logos
5 *
6 * @package Merchant
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Payment Logos Class.
15 *
16 */
17 class Merchant_Payment_Logos extends Merchant_Add_Module {
18
19 /**
20 * Module ID.
21 *
22 */
23 const MODULE_ID = 'payment-logos';
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
44 // Module id.
45 $this->module_id = self::MODULE_ID;
46
47 // WooCommerce only.
48 $this->wc_only = true;
49
50 // Parent construct.
51 parent::__construct();
52
53 // Module section.
54 $this->module_section = 'build-trust';
55
56 // Module default settings.
57 $this->module_default_settings = array(
58 'logos' => '',
59 'title' => __( '🔒 Safe & Secure Checkout', 'merchant' ),
60 );
61
62 // Mount preview url.
63 $preview_url = site_url( '/' );
64
65 if ( function_exists( 'wc_get_products' ) ) {
66 $products = wc_get_products( array( 'limit' => 1 ) );
67
68 if ( ! empty( $products ) && ! empty( $products[0] ) ) {
69 $preview_url = get_permalink( $products[0]->get_id() );
70 }
71 }
72
73 // Module data.
74 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
75 $this->module_data[ 'preview_url' ] = $preview_url;
76
77 // Module options path.
78 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
79
80 // Is module preview page.
81 if ( is_admin() && parent::is_module_settings_page() ) {
82 self::$is_module_preview = true;
83
84 // Enqueue admin styles.
85 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
86
87 // Enqueue admin scripts.
88 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_js' ) );
89
90 // Admin preview box.
91 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
92
93 // Custom CSS.
94 // The custom CSS should be added here as well due to ensure preview box works properly.
95 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
96 }
97
98 if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) {
99 // Init translations.
100 $this->init_translations();
101 }
102
103 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
104 return;
105 }
106
107 // Return early if it's on admin but not in the respective module settings page.
108 if ( is_admin() && ! wp_doing_ajax() && ! parent::is_module_settings_page() ) {
109 return;
110 }
111
112 add_action( 'admin_init', array( $this, 'add_image_sizes' ) );
113 add_action( 'wp_ajax_merchant_regenerate_images', array( $this, 'regenerate_images' ) );
114
115 // Enqueue styles.
116 add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );
117
118 // Add payment logos after add to cart form on single product pages.
119 add_action( 'woocommerce_single_product_summary', array( $this, 'payment_logos_output' ), 30 );
120
121 // Custom CSS.
122 add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) );
123 }
124
125 /**
126 * Add custom image sizes for the merchant module.
127 *
128 * It runs on AJAX requests as it hooks into the `admin_init` hook, so
129 * there is no need to call it again in the `$this->regenerate_images` method.
130 *
131 * @return void
132 */
133 public function add_image_sizes() {
134 $settings = $this->get_module_settings();
135 $width = (int) ( $settings['image-max-width'] ?? 80 );
136 $height = (int) ( $settings['image-max-height'] ?? 100 );
137
138 add_image_size( 'merchant_payment_logos', $width, $height );
139 }
140
141 /**
142 * Regenerate image sizes for specified attachments.
143 *
144 * @return void
145 */
146 public function regenerate_images() {
147 check_ajax_referer( 'merchant', 'nonce' );
148
149 if ( ! current_user_can( 'manage_options' ) ) {
150 wp_send_json_error( esc_html__( 'You are not allowed to do this.', 'merchant' ) );
151 }
152
153 $attachments = array_map( 'sanitize_text_field', $_POST['attachments'] ?? array() );
154 $is_dimension_changed = filter_var( $_POST['is_dimension_changed'] ?? false, FILTER_VALIDATE_BOOLEAN );
155
156 if ( ! function_exists( 'wp_get_attachment_metadata' ) ) {
157 require_once ABSPATH . 'wp-admin/includes/image.php';
158 }
159
160 $generated_images = array();
161
162 foreach ( $attachments as $attachment_id ) {
163 $attachment_id = (int) $attachment_id;
164 if ( ! $attachment_id ) {
165 continue;
166 }
167
168 $attachment_metadata = wp_get_attachment_metadata( $attachment_id );
169
170 if ( $is_dimension_changed || empty( $attachment_metadata['sizes']['merchant_payment_logos'] ) ) {
171 // Regenerate image sizes and update metadata
172 $updated_metadata = wp_generate_attachment_metadata( $attachment_id, get_attached_file( $attachment_id ) );
173
174 if ( ! is_wp_error( $updated_metadata ) ) {
175 wp_update_attachment_metadata( $attachment_id, $updated_metadata );
176 $generated_images[] = $attachment_id;
177 }
178 }
179 }
180
181 wp_send_json_success( array( $generated_images ) );
182 }
183
184 /**
185 * Print shortcode content.
186 *
187 * @return string
188 */
189 public function shortcode_handler() {
190 // Check if module is active.
191 if ( ! Merchant_Modules::is_module_active( $this->module_id ) ) {
192 return '';
193 }
194
195 // Check if shortcode is enabled.
196 if ( ! $this->is_shortcode_enabled() ) {
197 return '';
198 }
199
200 // Check if we are on a single product page.
201 if ( ! is_singular( 'product' ) ) {
202 // If user is admin, show error message.
203 if ( current_user_can( 'manage_options' ) ) {
204 return $this->shortcode_placement_error();
205 }
206
207 return '';
208 }
209 ob_start();
210
211 $settings = $this->get_module_settings();
212 $is_placeholder = empty( $settings['logos'] ) ? true : false;
213 $logos = $this->get_logos( $settings['logos'] );
214 ?>
215 <div class="merchant-payment-logos">
216 <?php
217 if ( ! empty( $settings['title'] ) ) : ?>
218 <div class="merchant-payment-logos-title">
219 <strong><?php
220 echo esc_html( Merchant_Translator::translate( $settings['title'] ) ); ?></strong>
221 </div>
222 <?php
223 endif; ?>
224 <?php
225 if ( ! $is_placeholder ) : ?>
226 <div class="merchant-payment-logos-images">
227 <?php
228 foreach ( $logos as $image_id ) : ?>
229 <?php
230 $imagedata = wp_get_attachment_image_src( $image_id, 'merchant_payment_logos' ); ?>
231 <?php
232 if ( ! empty( $imagedata ) && ! empty( $imagedata[0] ) ) :
233 echo wp_kses_post( wp_get_attachment_image( $image_id, 'merchant_payment_logos' ) );
234 endif; ?>
235 <?php
236 endforeach; ?>
237 </div>
238 <?php
239 else : ?>
240 <div class="merchant-payment-logos-images is-placeholder">
241 <?php
242 foreach ( $logos as $logo_src ) : ?>
243 <img src="<?php
244 echo esc_url( $logo_src ); ?>"/>
245 <?php
246 endforeach; ?>
247 </div>
248 <?php
249 endif; ?>
250 </div>
251 <?php
252 $shortcode_content = ob_get_clean();
253
254 /**
255 * Filter the shortcode html content.
256 *
257 * @param string $shortcode_content shortcode html content
258 * @param string $module_id module id
259 * @param int $post_id product id
260 *
261 * @since 1.8
262 */
263 return apply_filters( 'merchant_module_shortcode_content_html', $shortcode_content, $this->module_id, get_the_ID() );
264 }
265
266 /**
267 * Init translations.
268 *
269 * @return void
270 */
271 public function init_translations() {
272 $settings = $this->get_module_settings();
273 if ( ! empty( $settings['title'] ) ) {
274 Merchant_Translator::register_string( $settings['title'], esc_html__( 'Payment logos text above logos', 'merchant' ) );
275 }
276 }
277
278 /**
279 * Admin enqueue CSS.
280 *
281 * @return void
282 */
283 public function admin_enqueue_css() {
284 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
285 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
286
287 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
288 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/payment-logos.min.css', array(), MERCHANT_VERSION );
289 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
290 }
291 }
292
293 /**
294 * Admin enqueue scripts.
295 *
296 * @return void
297 */
298 public function admin_enqueue_js() {
299 if ( $this->is_module_settings_page() ) {
300 wp_enqueue_script( "merchant-{$this->module_id}", MERCHANT_URI . "assets/js/modules/{$this->module_id}/admin/preview.min.js", array( 'jquery' ), MERCHANT_VERSION, true );
301 }
302 }
303
304 /**
305 * Enqueue CSS.
306 *
307 * @return void
308 */
309 public function enqueue_css() {
310 if ( ! is_singular( 'product' ) && ! Merchant_Modules::is_module_active( 'quick-view' ) ) {
311 return;
312 }
313
314 // Specific module styles.
315 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/payment-logos.min.css', array(), MERCHANT_VERSION );
316 }
317
318 /**
319 * Render admin preview
320 *
321 * @param Merchant_Admin_Preview $preview
322 * @param string $module
323 *
324 * @return Merchant_Admin_Preview
325 */
326 public function render_admin_preview( $preview, $module ) {
327 if ( self::MODULE_ID === $module ) {
328 ob_start();
329 self::admin_preview_content();
330 $content = ob_get_clean();
331
332 // HTML.
333 $preview->set_html( $content );
334
335 // Text Above the Logos.
336 $preview->set_text( 'title', '.merchant-payment-logos-title > strong' );
337
338 }
339
340 return $preview;
341 }
342
343 /**
344 * Admin preview content.
345 *
346 * @return void
347 */
348 public function admin_preview_content() {
349 ?>
350
351 <div class="mrc-preview-single-product-elements">
352 <div class="mrc-preview-left-column">
353 <div class="mrc-preview-product-image-wrapper">
354 <div class="mrc-preview-product-image"></div>
355 <div class="mrc-preview-product-image-thumbs">
356 <div class="mrc-preview-product-image-thumb"></div>
357 <div class="mrc-preview-product-image-thumb"></div>
358 <div class="mrc-preview-product-image-thumb"></div>
359 </div>
360 </div>
361 </div>
362 <div class="mrc-preview-right-column">
363 <div class="mrc-preview-text-placeholder"></div>
364 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
365 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
366 <div class="mrc-preview-text-placeholder mrc-mw-40 mrc-hide-on-smaller-screens"></div>
367 <div class="mrc-preview-addtocart-placeholder mrc-hide-on-smaller-screens"></div>
368 <?php $this->payment_logos_output(); ?>
369 </div>
370 </div>
371
372 <?php
373 }
374
375 /**
376 * Get logos.
377 *
378 * @param string $logos
379 * @return array $logos Array of logos.
380 */
381 public function get_logos( $logos ) {
382 return ! empty( $logos )
383 ? explode( ',', $logos )
384 : array(
385 MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/visa.svg',
386 MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/master.svg',
387 MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/pp.svg',
388 );
389 }
390
391 /**
392 * Get placeholder logos alternative descriptions.
393 *
394 * @return array $logos_alt Array of logos alternative descriptions.
395 */
396 public function get_placeholder_logos_alt_map() {
397 return array(
398 'visa.svg' => __( 'Visa', 'merchant' ),
399 'master.svg' => __( 'Mastercard', 'merchant' ),
400 'pp.svg' => __( 'PayPal', 'merchant' ),
401 );
402 }
403
404 /**
405 * Render payment logos.
406 * TODO: Render through template files.
407 *
408 * @return void
409 */
410 public function payment_logos_output() {
411 if ( $this->is_shortcode_enabled() ) {
412 return;
413 }
414
415 if ( is_archive() || is_page() ) {
416 return;
417 }
418
419 $settings = $this->get_module_settings();
420 $is_placeholder = empty( $settings[ 'logos' ] ) ? true : false;
421
422 $logos = $this->get_logos( $settings[ 'logos' ] );
423 $placeholder_logos_alt = $this->get_placeholder_logos_alt_map();
424
425 ?>
426
427 <div class="merchant-payment-logos">
428
429 <?php if ( ! empty( $settings[ 'title' ] ) ) : ?>
430 <div class="merchant-payment-logos-title">
431 <strong><?php echo esc_html( Merchant_Translator::translate( $settings[ 'title' ] ) ); ?></strong>
432 </div>
433 <?php endif; ?>
434
435 <?php if ( ! $is_placeholder ) : ?>
436
437 <div class="merchant-payment-logos-images">
438
439 <?php foreach ( $logos as $image_id ) {
440 echo wp_kses_post( wp_get_attachment_image( $image_id, 'merchant_payment_logos' ) );
441 } ?>
442
443 </div>
444
445 <?php else : ?>
446
447 <div class="merchant-payment-logos-images is-placeholder">
448 <?php foreach ( $logos as $logo_src ) :
449 $logo_basename = basename( $logo_src );
450 $logo_alt = isset( $placeholder_logos_alt[ $logo_basename ] ) ? $placeholder_logos_alt[ $logo_basename ] : '';
451 ?>
452 <img src="<?php echo esc_url( $logo_src ); ?>" alt="<?php echo esc_attr( $logo_alt ); ?>" />
453 <?php endforeach; ?>
454 </div>
455
456 <?php endif; ?>
457
458 </div>
459
460 <?php
461 }
462
463 /**
464 * Custom CSS.
465 *
466 * @return string
467 */
468 public function get_module_custom_css() {
469 $css = '';
470
471 // Font Size.
472 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'font-size', 18, '.merchant-payment-logos', '--mrc-plogos-font-size', 'px' );
473
474 // Text Color.
475 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'text-color', '#212121', '.merchant-payment-logos', '--mrc-plogos-text-color' );
476
477 // Margin Top.
478 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'margin-top', 20, '.merchant-payment-logos', '--mrc-plogos-margin-top', 'px' );
479
480 // Margin Bottom.
481 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'margin-bottom', 20, '.merchant-payment-logos', '--mrc-plogos-margin-bottom', 'px' );
482
483 // Align.
484 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'align', 'flex-start', '.merchant-payment-logos', '--mrc-plogos-align' );
485
486 // Image Max Width.
487 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'image-max-width', 80, '.merchant-payment-logos', '--mrc-plogos-image-max-width', 'px' );
488
489 // Image Max Height.
490 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'image-max-height', 100, '.merchant-payment-logos', '--mrc-plogos-image-max-height', 'px' );
491
492 return $css;
493 }
494
495 /**
496 * Admin custom CSS.
497 *
498 * @param string $css The custom CSS.
499 * @return string $css The custom CSS.
500 */
501 public function admin_custom_css( $css ) {
502 $css .= $this->get_module_custom_css();
503
504 return $css;
505 }
506
507 /**
508 * Frontend custom CSS.
509 *
510 * @param string $css The custom CSS.
511 * @return string $css The custom CSS.
512 */
513 public function frontend_custom_css( $css ) {
514 if ( ! is_singular( 'product' ) && ! Merchant_Modules::is_module_active( 'quick-view' ) ) {
515 return $css;
516 }
517
518 $css .= $this->get_module_custom_css();
519
520 return $css;
521 }
522 }
523
524 // Initialize the module.
525 add_action( 'init', function() {
526 new Merchant_Payment_Logos();
527 } );
528