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

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