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

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