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 / pre-orders / class-pre-orders.php

class-pre-orders.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.8, at inc/modules/pre-orders/class-pre-orders.php

354 lines 10.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Pre Orders.
5 *
6 * @package Merchant
7 */
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly
11 }
12
13 /**
14 * Pre Orders Class.
15 *
16 */
17 class Merchant_Pre_Orders extends Merchant_Add_Module {
18
19 /**
20 * Module ID.
21 *
22 */
23 const MODULE_ID = 'pre-orders';
24
25 /**
26 * Is module preview.
27 *
28 */
29 public static $is_module_preview = false;
30
31 /**
32 * Main functionality dependency.
33 *
34 */
35 public $main_func;
36
37 /**
38 * Constructor.
39 *
40 */
41 public function __construct( Merchant_Pre_Orders_Main_Functionality $main_func ) {
42
43 // Module id.
44 $this->module_id = self::MODULE_ID;
45
46 // WooCommerce only.
47 $this->wc_only = true;
48
49 // Parent construct.
50 parent::__construct();
51
52 $this->main_func = $main_func;
53
54 // Module section.
55 $this->module_section = 'boost-revenue';
56
57 // Module default settings.
58 $this->module_default_settings = array(
59 'button_text' => __( 'Pre Order Now!', 'merchant' ),
60 'additional_text' => __( 'Ships on {date}.', 'merchant' )
61 );
62
63 // Mount preview url.
64 $preview_url = site_url( '/' );
65
66 if ( function_exists( 'wc_get_page_id' ) ) {
67 $preview_url = get_permalink( wc_get_page_id( 'shop' ) );
68 }
69
70 // Module data.
71 $this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ];
72 $this->module_data[ 'preview_url' ] = $preview_url;
73
74 // Module options path.
75 $this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php';
76
77 // Is module preview page.
78 if ( is_admin() && parent::is_module_settings_page() ) {
79 self::$is_module_preview = true;
80
81 // Enqueue admin styles.
82 add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) );
83
84 // Render module essencial instructions before the module page body content.
85 add_action( 'merchant_admin_after_module_page_page_header', array( $this, 'admin_module_essencial_instructions' ) );
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 // TODO: Refactor the 'Merchant_Pre_Orders_Main_Functionality' class to load admin things separated from frontend things.
106 $main_func->init();
107
108 // Return early if it's on admin but not in the respective module settings page.
109 if ( is_admin() && ! parent::is_module_settings_page() ) {
110 return;
111 }
112
113 // Enqueue styles.
114 add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) );
115
116 // Enqueue scripts.
117 add_action( 'merchant_enqueue_after_main_css_js', array( $this, 'enqueue_scripts' ) );
118
119 // Localize script.
120 add_filter( 'merchant_localize_script', array( $this, 'localize_script' ) );
121
122 // Custom CSS.
123 add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) );
124
125 }
126
127 /**
128 * Init translations.
129 *
130 * @return void
131 */
132 public function init_translations() {
133 $settings = $this->get_module_settings();
134 if ( ! empty( $settings['button_text'] ) ) {
135 Merchant_Translator::register_string( $settings['button_text'], esc_html__( 'Pre orders button text', 'merchant' ) );
136 }
137 }
138
139 /**
140 * Admin enqueue CSS.
141 *
142 * @return void
143 */
144 public function admin_enqueue_css() {
145 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
146 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
147
148 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
149 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', [], MERCHANT_VERSION );
150 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
151 }
152 }
153
154 /**
155 * Enqueue CSS.
156 *
157 * @return void
158 */
159 public function enqueue_css() {
160
161 // Specific module styles.
162 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', array(), MERCHANT_VERSION );
163 }
164
165 /**
166 * Enqueue scripts.
167 *
168 * @return void
169 */
170 public function enqueue_scripts() {
171
172 // Register and enqueue the main module script.
173 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/pre-orders.min.js', array(), MERCHANT_VERSION, true );
174 }
175
176 /**
177 * Localize script with module settings.
178 *
179 * @param array $setting The merchant global object setting parameter.
180 * @return array $setting The merchant global object setting parameter.
181 */
182 public function localize_script( $setting ) {
183 $module_settings = $this->get_module_settings();
184
185 $setting[ 'pre_orders' ] = true;
186 $setting[ 'pre_orders_add_button_title' ] = Merchant_Translator::translate( $module_settings[ 'button_text' ] );
187
188 return $setting;
189 }
190
191 /**
192 * Render module essencial instructions.
193 *
194 * @return void
195 */
196 public function admin_module_essencial_instructions() { ?>
197 <div class="merchant-module-page-settings">
198 <div class="merchant-module-page-setting-box merchant-module-page-setting-box-style-2">
199 <div class="merchant-module-page-setting-title"><?php echo esc_html__( 'Tag Pre-Orders', 'merchant' ); ?></div>
200 <div class="merchant-module-page-setting-fields">
201 <div class="merchant-module-page-setting-field merchant-module-page-setting-field-content">
202 <div class="merchant-module-page-setting-field-inner">
203 <div class="merchant-tag-pre-orders">
204 <i class="dashicons dashicons-info"></i>
205 <p><?php echo esc_html__( 'Pre-orders captured by Merchant are tagged with "MerchantPreOrder" and can be found in your WooCommerce Order Section.', 'merchant' ); ?> <?php echo sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( admin_url( 'edit.php?post_type=shop_order' ) ), esc_html__( 'View Pre-Orders', 'merchant' ) ); ?></p>
206 </div>
207 </div>
208 </div>
209 </div>
210 </div>
211 </div>
212
213 <?php
214 }
215
216 /**
217 * Render admin preview
218 *
219 * @param Merchant_Admin_Preview $preview
220 * @param string $module
221 *
222 * @return Merchant_Admin_Preview
223 */
224 public function render_admin_preview( $preview, $module ) {
225 if ( self::MODULE_ID === $module ) {
226 $settings = $this->get_module_settings();
227
228 // Additional text.
229 $additional_text = $settings[ 'additional_text' ];
230 $time_format = date_i18n( get_option( 'date_format' ), strtotime( gmdate( 'Y-m-d', strtotime('+2 days') ) ) );
231 $text = $this->main_func->replaceDateTxt( $additional_text, $time_format );
232
233 ob_start();
234 self::admin_preview_content( $settings, $text );
235 $content = ob_get_clean();
236
237 // HTML.
238 $preview->set_html( $content );
239
240 // Button Text.
241 $preview->set_text( 'button_text', '.add_to_cart_button' );
242
243 // Additional Text.
244 $preview->set_text( 'additional_text', '.merchant-pre-orders-date', array(
245 array(
246 '{date}'
247 ),
248 array(
249 $time_format
250 )
251 ) );
252
253 }
254
255 return $preview;
256 }
257
258 /**
259 * Admin preview content.
260 *
261 * @return void
262 */
263 public function admin_preview_content( $settings, $text ) {
264 ?>
265
266 <div class="mrc-preview-single-product-elements">
267 <div class="mrc-preview-left-column">
268 <div class="mrc-preview-product-image-wrapper">
269 <div class="mrc-preview-product-image"></div>
270 <div class="mrc-preview-product-image-thumbs">
271 <div class="mrc-preview-product-image-thumb"></div>
272 <div class="mrc-preview-product-image-thumb"></div>
273 <div class="mrc-preview-product-image-thumb"></div>
274 </div>
275 </div>
276 </div>
277 <div class="mrc-preview-right-column">
278 <div class="mrc-preview-text-placeholder"></div>
279 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
280 <div class="mrc-preview-text-placeholder mrc-mw-40 mrc-hide-on-smaller-screens"></div>
281 <div class="mrc-preview-text-placeholder mrc-mw-30 mrc-hide-on-smaller-screens"></div>
282 <div class="merchant-pre-ordered-product">
283 <div class="merchant-pre-orders-date"><?php echo sprintf( '<div class="merchant-pre-orders-date">%s</div>', esc_html( $text ) ); ?></div>
284 <a href="#" class="add_to_cart_button"><?php echo esc_html( $settings[ 'button_text' ] ); ?></a>
285 </div>
286 </div>
287 </div>
288
289 <?php
290 }
291
292 /**
293 * Custom CSS.
294 *
295 * @return string
296 */
297 public function get_module_custom_css() {
298 $css = '';
299
300 // Text Color.
301 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'text-color', '#FFF', '.merchant-pre-ordered-product', '--mrc-po-text-color' );
302
303 // Text Color (hover).
304 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'text-hover-color', '#FFF', '.merchant-pre-ordered-product', '--mrc-po-text-hover-color' );
305
306 // Border Color.
307 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'border-color', '#212121', '.merchant-pre-ordered-product', '--mrc-po-border-color' );
308
309 // Border Color (hover).
310 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'border-hover-color', '#414141', '.merchant-pre-ordered-product', '--mrc-po-border-hover-color' );
311
312 // Background Color.
313 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'background-color', '#212121', '.merchant-pre-ordered-product', '--mrc-po-background-color' );
314
315 // Background Color (hover).
316 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'background-hover-color', '#414141', '.merchant-pre-ordered-product', '--mrc-po-background-hover-color' );
317
318 return $css;
319 }
320
321 /**
322 * Admin custom CSS.
323 *
324 * @param string $css The custom CSS.
325 * @return string $css The custom CSS.
326 */
327 public function admin_custom_css( $css ) {
328 $css .= $this->get_module_custom_css();
329
330 return $css;
331 }
332
333 /**
334 * Frontend custom CSS.
335 *
336 * @param string $css The custom CSS.
337 * @return string $css The custom CSS.
338 */
339 public function frontend_custom_css( $css ) {
340 $css .= $this->get_module_custom_css();
341
342 return $css;
343 }
344
345 }
346
347 // Main functionality.
348 require MERCHANT_DIR . 'inc/modules/pre-orders/class-pre-orders-main-functionality.php';
349
350 // Initialize the module.
351 add_action( 'init', function() {
352 new Merchant_Pre_Orders( new Merchant_Pre_Orders_Main_Functionality() );
353 } );
354