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

352 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 * Init translations.
128 *
129 * @return void
130 */
131 public function init_translations() {
132 $settings = $this->get_module_settings();
133 if ( ! empty( $settings['button_text'] ) ) {
134 Merchant_Translator::register_string( $settings['button_text'], esc_html__( 'Pre orders button text', 'merchant' ) );
135 }
136 }
137
138 /**
139 * Admin enqueue CSS.
140 *
141 * @return void
142 */
143 public function admin_enqueue_css() {
144 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
145 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
146
147 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
148 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', array(), MERCHANT_VERSION );
149 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
150 }
151 }
152
153 /**
154 * Enqueue CSS.
155 *
156 * @return void
157 */
158 public function enqueue_css() {
159
160 // Specific module styles.
161 wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', array(), MERCHANT_VERSION );
162 }
163
164 /**
165 * Enqueue scripts.
166 *
167 * @return void
168 */
169 public function enqueue_scripts() {
170
171 // Register and enqueue the main module script.
172 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/pre-orders.min.js', array(), MERCHANT_VERSION, true );
173 }
174
175 /**
176 * Localize script with module settings.
177 *
178 * @param array $setting The merchant global object setting parameter.
179 * @return array $setting The merchant global object setting parameter.
180 */
181 public function localize_script( $setting ) {
182 $module_settings = $this->get_module_settings();
183
184 $setting[ 'pre_orders' ] = true;
185 $setting[ 'pre_orders_add_button_title' ] = Merchant_Translator::translate( $module_settings[ 'button_text' ] );
186
187 return $setting;
188 }
189
190 /**
191 * Render module essencial instructions.
192 *
193 * @return void
194 */
195 public function admin_module_essencial_instructions() { ?>
196 <div class="merchant-module-page-settings">
197 <div class="merchant-module-page-setting-box merchant-module-page-setting-box-style-2">
198 <div class="merchant-module-page-setting-title"><?php echo esc_html__( 'Tag Pre-Orders', 'merchant' ); ?></div>
199 <div class="merchant-module-page-setting-fields">
200 <div class="merchant-module-page-setting-field merchant-module-page-setting-field-content">
201 <div class="merchant-module-page-setting-field-inner">
202 <div class="merchant-tag-pre-orders">
203 <i class="dashicons dashicons-info"></i>
204 <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 printf( '<a href="%s" target="_blank">%s</a>', esc_url( admin_url( 'edit.php?post_type=shop_order' ) ), esc_html__( 'View Pre-Orders', 'merchant' ) ); ?></p>
205 </div>
206 </div>
207 </div>
208 </div>
209 </div>
210 </div>
211
212 <?php
213 }
214
215 /**
216 * Render admin preview
217 *
218 * @param Merchant_Admin_Preview $preview
219 * @param string $module
220 *
221 * @return Merchant_Admin_Preview
222 */
223 public function render_admin_preview( $preview, $module ) {
224 if ( self::MODULE_ID === $module ) {
225 $settings = $this->get_module_settings();
226
227 // Additional text.
228 $additional_text = $settings[ 'additional_text' ];
229 $time_format = date_i18n( get_option( 'date_format' ), strtotime( gmdate( 'Y-m-d', strtotime('+2 days') ) ) );
230 $text = $this->main_func->replaceDateTxt( $additional_text, $time_format );
231
232 ob_start();
233 self::admin_preview_content( $settings, $text );
234 $content = ob_get_clean();
235
236 // HTML.
237 $preview->set_html( $content );
238
239 // Button Text.
240 $preview->set_text( 'button_text', '.add_to_cart_button' );
241
242 // Additional Text.
243 $preview->set_text( 'additional_text', '.merchant-pre-orders-date', array(
244 array(
245 '{date}',
246 ),
247 array(
248 $time_format,
249 ),
250 ) );
251
252 }
253
254 return $preview;
255 }
256
257 /**
258 * Admin preview content.
259 *
260 * @return void
261 */
262 public function admin_preview_content( $settings, $text ) {
263 ?>
264
265 <div class="mrc-preview-single-product-elements">
266 <div class="mrc-preview-left-column">
267 <div class="mrc-preview-product-image-wrapper">
268 <div class="mrc-preview-product-image"></div>
269 <div class="mrc-preview-product-image-thumbs">
270 <div class="mrc-preview-product-image-thumb"></div>
271 <div class="mrc-preview-product-image-thumb"></div>
272 <div class="mrc-preview-product-image-thumb"></div>
273 </div>
274 </div>
275 </div>
276 <div class="mrc-preview-right-column">
277 <div class="mrc-preview-text-placeholder"></div>
278 <div class="mrc-preview-text-placeholder mrc-mw-70"></div>
279 <div class="mrc-preview-text-placeholder mrc-mw-40 mrc-hide-on-smaller-screens"></div>
280 <div class="mrc-preview-text-placeholder mrc-mw-30 mrc-hide-on-smaller-screens"></div>
281 <div class="merchant-pre-ordered-product">
282 <div class="merchant-pre-orders-date"><?php printf( '<div class="merchant-pre-orders-date">%s</div>', esc_html( $text ) ); ?></div>
283 <a href="#" class="add_to_cart_button"><?php echo esc_html( $settings[ 'button_text' ] ); ?></a>
284 </div>
285 </div>
286 </div>
287
288 <?php
289 }
290
291 /**
292 * Custom CSS.
293 *
294 * @return string
295 */
296 public function get_module_custom_css() {
297 $css = '';
298
299 // Text Color.
300 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'text-color', '#FFF', '.merchant-pre-ordered-product', '--mrc-po-text-color' );
301
302 // Text Color (hover).
303 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'text-hover-color', '#FFF', '.merchant-pre-ordered-product', '--mrc-po-text-hover-color' );
304
305 // Border Color.
306 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'border-color', '#212121', '.merchant-pre-ordered-product', '--mrc-po-border-color' );
307
308 // Border Color (hover).
309 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'border-hover-color', '#414141', '.merchant-pre-ordered-product', '--mrc-po-border-hover-color' );
310
311 // Background Color.
312 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'background-color', '#212121', '.merchant-pre-ordered-product', '--mrc-po-background-color' );
313
314 // Background Color (hover).
315 $css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'background-hover-color', '#414141', '.merchant-pre-ordered-product', '--mrc-po-background-hover-color' );
316
317 return $css;
318 }
319
320 /**
321 * Admin custom CSS.
322 *
323 * @param string $css The custom CSS.
324 * @return string $css The custom CSS.
325 */
326 public function admin_custom_css( $css ) {
327 $css .= $this->get_module_custom_css();
328
329 return $css;
330 }
331
332 /**
333 * Frontend custom CSS.
334 *
335 * @param string $css The custom CSS.
336 * @return string $css The custom CSS.
337 */
338 public function frontend_custom_css( $css ) {
339 $css .= $this->get_module_custom_css();
340
341 return $css;
342 }
343 }
344
345 // Main functionality.
346 require MERCHANT_DIR . 'inc/modules/pre-orders/class-pre-orders-main-functionality.php';
347
348 // Initialize the module.
349 add_action( 'init', function() {
350 new Merchant_Pre_Orders( new Merchant_Pre_Orders_Main_Functionality() );
351 } );
352