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

444 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_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 * 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->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 * Render payment logos.
318 * TODO: Render through template files.
319 *
320 * @return void
321 */
322 public function payment_logos_output() {
323 if ( $this->is_shortcode_enabled() ) {
324 return;
325 }
326
327 if ( is_archive() ) {
328 return;
329 }
330
331 $settings = $this->get_module_settings();
332 $is_placeholder = empty( $settings[ 'logos' ] ) ? true : false;
333
334 $logos = $this->get_logos( $settings[ 'logos' ] );
335
336 ?>
337
338 <div class="merchant-payment-logos">
339
340 <?php if ( ! empty( $settings[ 'title' ] ) ) : ?>
341 <div class="merchant-payment-logos-title">
342 <strong><?php echo esc_html( Merchant_Translator::translate( $settings[ 'title' ] ) ); ?></strong>
343 </div>
344 <?php endif; ?>
345
346 <?php if ( ! $is_placeholder ) : ?>
347
348 <div class="merchant-payment-logos-images">
349
350 <?php foreach ( $logos as $image_id ) : ?>
351
352 <?php $imagedata = wp_get_attachment_image_src( $image_id, 'full' ); ?>
353
354 <?php if ( ! empty( $imagedata ) && ! empty( $imagedata[0] ) ) : ?>
355
356 <?php printf( '<img src="%s" />', esc_url( $imagedata[0] ) ); ?>
357
358 <?php endif; ?>
359
360 <?php endforeach; ?>
361
362 </div>
363
364 <?php else : ?>
365
366 <div class="merchant-payment-logos-images is-placeholder">
367 <?php foreach ( $logos as $logo_src ) : ?>
368 <img src="<?php echo esc_url( $logo_src ); ?>" />
369 <?php endforeach; ?>
370 </div>
371
372 <?php endif; ?>
373
374 </div>
375
376 <?php
377 }
378
379 /**
380 * Custom CSS.
381 *
382 * @return string
383 */
384 public function get_module_custom_css() {
385 $css = '';
386
387 // Font Size.
388 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'font-size', 18, '.merchant-payment-logos', '--mrc-plogos-font-size', 'px' );
389
390 // Text Color.
391 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'text-color', '#212121', '.merchant-payment-logos', '--mrc-plogos-text-color' );
392
393 // Margin Top.
394 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'margin-top', 20, '.merchant-payment-logos', '--mrc-plogos-margin-top', 'px' );
395
396 // Margin Bottom.
397 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'margin-bottom', 20, '.merchant-payment-logos', '--mrc-plogos-margin-bottom', 'px' );
398
399 // Align.
400 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'align', 'flex-start', '.merchant-payment-logos', '--mrc-plogos-align' );
401
402 // Image Max Width.
403 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'image-max-width', 80, '.merchant-payment-logos', '--mrc-plogos-image-max-width', 'px' );
404
405 // Image Max Height.
406 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'image-max-height', 100, '.merchant-payment-logos', '--mrc-plogos-image-max-height', 'px' );
407
408 return $css;
409 }
410
411 /**
412 * Admin custom CSS.
413 *
414 * @param string $css The custom CSS.
415 * @return string $css The custom CSS.
416 */
417 public function admin_custom_css( $css ) {
418 $css .= $this->get_module_custom_css();
419
420 return $css;
421 }
422
423 /**
424 * Frontend custom CSS.
425 *
426 * @param string $css The custom CSS.
427 * @return string $css The custom CSS.
428 */
429 public function frontend_custom_css( $css ) {
430 if ( ! is_singular( 'product' ) && ! Merchant_Modules::is_module_active( 'quick-view' ) ) {
431 return $css;
432 }
433
434 $css .= $this->get_module_custom_css();
435
436 return $css;
437 }
438 }
439
440 // Initialize the module.
441 add_action( 'init', function() {
442 new Merchant_Payment_Logos();
443 } );
444