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

337 lines 9.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 * Constructor.
33 *
34 */
35 public function __construct() {
36
37 // Module id.
38 $this->module_id = self::MODULE_ID;
39
40 // WooCommerce only.
41 $this->wc_only = true;
42
43 // Parent construct.
44 parent::__construct();
45
46 // Module section.
47 $this->module_section = 'build-trust';
48
49 // Module default settings.
50 $this->module_default_settings = array(
51 'logos' => '',
52 'title' => __( '🔒 Safe & Secure Checkout', 'merchant' )
53 );
54
55 // Mount preview url.
56 $preview_url = site_url( '/' );
57
58 if ( function_exists( 'wc_get_products' ) ) {
59 $products = wc_get_products( array( 'limit' => 1 ) );
60
61 if ( ! empty( $products ) && ! empty( $products[0] ) ) {
62 $preview_url = get_permalink( $products[0]->get_id() );
63 }
64 }
65
66 // Module data.
67 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
68 $this->module_data[ 'preview_url' ] = $preview_url;
69
70 // Module options path.
71 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
72
73 // Is module preview page.
74 if ( is_admin() && parent::is_module_settings_page() ) {
75 self::$is_module_preview = true;
76
77 // Enqueue admin styles.
78 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
79
80 // Admin preview box.
81 add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 );
82
83 // Custom CSS.
84 // The custom CSS should be added here as well due to ensure preview box works properly.
85 add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) );
86
87 }
88
89 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
90 return;
91 }
92
93 // Return early if it's on admin but not in the respective module settings page.
94 if ( is_admin() && ! parent::is_module_settings_page() ) {
95 return;
96 }
97
98 // Enqueue styles.
99 add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );
100
101 // Add payment logos after add to cart form on single product pages.
102 add_action( 'woocommerce_after_add_to_cart_form', array( $this, 'payment_logos_output' ) );
103
104 // Custom CSS.
105 add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) );
106
107 }
108
109 /**
110 * Admin enqueue CSS.
111 *
112 * @return void
113 */
114 public function admin_enqueue_css() {
115 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
116 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
117
118 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
119 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/payment-logos.min.css', [], MERCHANT_VERSION );
120 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
121 }
122 }
123
124 /**
125 * Enqueue CSS.
126 *
127 * @return void
128 */
129 public function enqueue_css() {
130 if ( ! is_singular( 'product' ) && ! Merchant_Modules::is_module_active( 'quick-view' ) ) {
131 return;
132 }
133
134 // Specific module styles.
135 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/payment-logos.min.css', array(), MERCHANT_VERSION );
136 }
137
138 /**
139 * Render admin preview
140 *
141 * @param Merchant_Admin_Preview $preview
142 * @param string $module
143 *
144 * @return Merchant_Admin_Preview
145 */
146 public function render_admin_preview( $preview, $module ) {
147 if ( self::MODULE_ID === $module ) {
148 ob_start();
149 self::admin_preview_content();
150 $content = ob_get_clean();
151
152 // HTML.
153 $preview->set_html( $content );
154
155 // Text Above the Logos.
156 $preview->set_text( 'title', '.merchant-payment-logos-title > strong' );
157
158 }
159
160 return $preview;
161 }
162
163 /**
164 * Admin preview content.
165 *
166 * @return void
167 */
168 public function admin_preview_content() {
169 ?>
170
171 <div class="mrc-preview-single-product-elements">
172 <div class="mrc-preview-left-column">
173 <div class="mrc-preview-product-image-wrapper">
174 <div class="mrc-preview-product-image"></div>
175 <div class="mrc-preview-product-image-thumbs">
176 <div class="mrc-preview-product-image-thumb"></div>
177 <div class="mrc-preview-product-image-thumb"></div>
178 <div class="mrc-preview-product-image-thumb"></div>
179 </div>
180 </div>
181 </div>
182 <div class="mrc-preview-right-column">
183 <div class="mrc-preview-text-placeholder"></div>
184 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
185 <div class="mrc-preview-text-placeholder mrc-mw-30"></div>
186 <div class="mrc-preview-text-placeholder mrc-mw-40 mrc-hide-on-smaller-screens"></div>
187 <div class="mrc-preview-addtocart-placeholder mrc-hide-on-smaller-screens"></div>
188 <?php $this->payment_logos_output(); ?>
189 </div>
190 </div>
191
192 <?php
193 }
194
195 /**
196 * Get logos.
197 *
198 * @param string $logos
199 * @return array $logos Array of logos.
200 */
201 public function get_logos( $logos ) {
202 return ! empty( $logos )
203 ? explode( ',', $logos )
204 : array(
205 MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/visa.svg',
206 MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/master.svg',
207 MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/pp.svg'
208 );
209 }
210
211 /**
212 * Render payment logos.
213 * TODO: Render through template files.
214 *
215 * @return void
216 */
217 public function payment_logos_output() {
218 if ( is_archive() ) {
219 return;
220 }
221
222 $settings = $this->get_module_settings();
223 $is_placeholder = empty( $settings[ 'logos' ] ) ? true : false;
224
225 $logos = $this->get_logos( $settings[ 'logos' ] );
226
227 ?>
228
229 <div class="merchant-payment-logos">
230
231 <?php if ( ! empty( $settings[ 'title' ] ) ) : ?>
232 <div class="merchant-payment-logos-title">
233 <strong><?php echo esc_html( $settings[ 'title' ] ); ?></strong>
234 </div>
235 <?php endif; ?>
236
237 <?php if ( ! $is_placeholder ) : ?>
238
239 <div class="merchant-payment-logos-images">
240
241 <?php foreach ( $logos as $image_id ) : ?>
242
243 <?php $imagedata = wp_get_attachment_image_src( $image_id, 'full' ); ?>
244
245 <?php if ( ! empty( $imagedata ) && ! empty( $imagedata[0] ) ) : ?>
246
247 <?php echo sprintf( '<img src="%s" />', esc_url( $imagedata[0] ) ); ?>
248
249 <?php endif; ?>
250
251 <?php endforeach; ?>
252
253 </div>
254
255 <?php else : ?>
256
257 <div class="merchant-payment-logos-images is-placeholder">
258 <?php foreach ( $logos as $logo_src ) : ?>
259 <img src="<?php echo esc_url( $logo_src ); ?>" />
260 <?php endforeach; ?>
261 </div>
262
263 <?php endif; ?>
264
265 </div>
266
267 <?php
268
269 }
270
271 /**
272 * Custom CSS.
273 *
274 * @return string
275 */
276 public function get_module_custom_css() {
277 $css = '';
278
279 // Font Size.
280 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'font-size', 18, '.merchant-payment-logos', '--mrc-plogos-font-size', 'px' );
281
282 // Text Color.
283 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'text-color', '#212121', '.merchant-payment-logos', '--mrc-plogos-text-color' );
284
285 // Margin Top.
286 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'margin-top', 20, '.merchant-payment-logos', '--mrc-plogos-margin-top', 'px' );
287
288 // Margin Bottom.
289 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'margin-bottom', 20, '.merchant-payment-logos', '--mrc-plogos-margin-bottom', 'px' );
290
291 // Align.
292 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'align', 'flex-start', '.merchant-payment-logos', '--mrc-plogos-align' );
293
294 // Image Max Width.
295 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'image-max-width', 80, '.merchant-payment-logos', '--mrc-plogos-image-max-width', 'px' );
296
297 // Image Max Height.
298 $css .= Merchant_Custom_CSS::get_variable_css( 'payment-logos', 'image-max-height', 100, '.merchant-payment-logos', '--mrc-plogos-image-max-height', 'px' );
299
300 return $css;
301 }
302
303 /**
304 * Admin custom CSS.
305 *
306 * @param string $css The custom CSS.
307 * @return string $css The custom CSS.
308 */
309 public function admin_custom_css( $css ) {
310 $css .= $this->get_module_custom_css();
311
312 return $css;
313 }
314
315 /**
316 * Frontend custom CSS.
317 *
318 * @param string $css The custom CSS.
319 * @return string $css The custom CSS.
320 */
321 public function frontend_custom_css( $css ) {
322 if ( ! is_singular( 'product' ) && ! Merchant_Modules::is_module_active( 'quick-view' ) ) {
323 return $css;
324 }
325
326 $css .= $this->get_module_custom_css();
327
328 return $css;
329 }
330
331 }
332
333 // Initialize the module.
334 add_action( 'init', function() {
335 new Merchant_Payment_Logos();
336 } );
337