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

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