| 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 |
// Module id. |
| 43 |
$this->module_id = self::MODULE_ID; |
| 44 |
|
| 45 |
// WooCommerce only. |
| 46 |
$this->wc_only = true; |
| 47 |
|
| 48 |
// Parent construct. |
| 49 |
parent::__construct(); |
| 50 |
|
| 51 |
$this->main_func = $main_func; |
| 52 |
|
| 53 |
// Module section. |
| 54 |
$this->module_section = 'boost-revenue'; |
| 55 |
|
| 56 |
// Module default settings. |
| 57 |
$this->module_default_settings = array( |
| 58 |
'button_text' => __( 'Pre Order Now!', 'merchant' ), |
| 59 |
'additional_text' => __( 'Ships on {date}.', 'merchant' ), |
| 60 |
); |
| 61 |
|
| 62 |
// Mount preview url. |
| 63 |
$preview_url = site_url( '/' ); |
| 64 |
|
| 65 |
if ( function_exists( 'wc_get_page_id' ) ) { |
| 66 |
$preview_url = get_permalink( wc_get_page_id( 'shop' ) ); |
| 67 |
} |
| 68 |
|
| 69 |
// Module data. |
| 70 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 71 |
$this->module_data['preview_url'] = $preview_url; |
| 72 |
|
| 73 |
// Module options path. |
| 74 |
$this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 75 |
|
| 76 |
// Is module preview page. |
| 77 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 78 |
self::$is_module_preview = true; |
| 79 |
|
| 80 |
// Enqueue admin styles. |
| 81 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 82 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_js' ) ); |
| 83 |
|
| 84 |
// Admin preview box. |
| 85 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 86 |
|
| 87 |
// Custom CSS. |
| 88 |
// The custom CSS should be added here as well due to ensure preview box works properly. |
| 89 |
add_filter( 'merchant_custom_css', array( $this, 'admin_custom_css' ) ); |
| 90 |
} |
| 91 |
|
| 92 |
add_action( 'merchant_admin_before_include_modules_options', array( $this, 'help_banner' ) ); |
| 93 |
|
| 94 |
if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) { |
| 95 |
// Init translations. |
| 96 |
$this->init_translations(); |
| 97 |
} |
| 98 |
|
| 99 |
if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
// TODO: Refactor the 'Merchant_Pre_Orders_Main_Functionality' class to load admin things separated from frontend things. |
| 104 |
$main_func->init(); |
| 105 |
|
| 106 |
// Return early if it's on admin but not in the respective module settings page. |
| 107 |
if ( is_admin() && ! parent::is_module_settings_page() ) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
// Enqueue styles. |
| 112 |
add_action( 'merchant_enqueue_before_main_css_js', array( $this, 'enqueue_css' ) ); |
| 113 |
|
| 114 |
// Enqueue scripts. |
| 115 |
add_action( 'merchant_enqueue_after_main_css_js', array( $this, 'enqueue_scripts' ) ); |
| 116 |
|
| 117 |
// Localize script. |
| 118 |
add_filter( 'merchant_localize_script', array( $this, 'localize_script' ) ); |
| 119 |
|
| 120 |
// Custom CSS. |
| 121 |
add_filter( 'merchant_custom_css', array( $this, 'frontend_custom_css' ) ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Init translations. |
| 126 |
* |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
public function init_translations() { |
| 130 |
$settings = $this->get_module_settings(); |
| 131 |
if ( ! empty( $settings['rules'] ) ) { |
| 132 |
foreach ( $settings['rules'] as $rule ) { |
| 133 |
if ( ! empty( $rule['button_text'] ) ) { |
| 134 |
Merchant_Translator::register_string( $rule['button_text'], 'Pre order button text' ); |
| 135 |
} |
| 136 |
if ( ! empty( $rule['additional_text'] ) ) { |
| 137 |
Merchant_Translator::register_string( $rule['additional_text'], 'Pre order additional information' ); |
| 138 |
} |
| 139 |
if ( ! empty( $rule['cart_label_text'] ) ) { |
| 140 |
Merchant_Translator::register_string( $rule['cart_label_text'], 'Label text on cart' ); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Admin enqueue CSS. |
| 148 |
* |
| 149 |
* @return void |
| 150 |
*/ |
| 151 |
public function admin_enqueue_css() { |
| 152 |
$page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 153 |
$module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 154 |
|
| 155 |
if ( 'merchant' === $page && self::MODULE_ID === $module ) { |
| 156 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', array(), MERCHANT_VERSION ); |
| 157 |
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
public function admin_enqueue_js() { |
| 162 |
$page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 163 |
$module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 164 |
|
| 165 |
if ( 'merchant' === $page && self::MODULE_ID === $module ) { |
| 166 |
wp_enqueue_script( |
| 167 |
'merchant-admin-' . self::MODULE_ID, |
| 168 |
MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/admin/preview.min.js', |
| 169 |
array( 'jquery' ), |
| 170 |
MERCHANT_VERSION, |
| 171 |
true |
| 172 |
); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Enqueue CSS. |
| 178 |
* |
| 179 |
* @return void |
| 180 |
*/ |
| 181 |
public function enqueue_css() { |
| 182 |
// Specific module styles. |
| 183 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/pre-orders.min.css', array(), MERCHANT_VERSION ); |
| 184 |
|
| 185 |
// add inline style |
| 186 |
if ( is_singular( 'product' ) ) { |
| 187 |
wp_add_inline_style( 'merchant-' . self::MODULE_ID, $this->add_inline_style() ); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
public function add_inline_style() { |
| 192 |
ob_start(); |
| 193 |
$rule = $this->current_rule(); |
| 194 |
if ( ! empty( $rule ) ) { |
| 195 |
?> |
| 196 |
.woocommerce .merchant-pre-ordered-product{ |
| 197 |
--mrc-po-text-color: <?php |
| 198 |
echo esc_attr( $rule['text-color'] ); ?>; |
| 199 |
--mrc-po-text-hover-color: <?php |
| 200 |
echo esc_attr( $rule['text-hover-color'] ); ?>; |
| 201 |
--mrc-po-border-color: <?php |
| 202 |
echo esc_attr( $rule['border-color'] ); ?>; |
| 203 |
--mrc-po-border-hover-color: <?php |
| 204 |
echo esc_attr( $rule['border-hover-color'] ); ?>; |
| 205 |
--mrc-po-background-color: <?php |
| 206 |
echo esc_attr( $rule['background-color'] ); ?>; |
| 207 |
--mrc-po-background-hover-color: <?php |
| 208 |
echo esc_attr( $rule['background-hover-color'] ); ?>; |
| 209 |
} |
| 210 |
<?php |
| 211 |
} |
| 212 |
|
| 213 |
return ob_get_clean(); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Enqueue scripts. |
| 218 |
* |
| 219 |
* @return void |
| 220 |
*/ |
| 221 |
public function enqueue_scripts() { |
| 222 |
// Register and enqueue the main module script. |
| 223 |
wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/pre-orders.min.js', array(), MERCHANT_VERSION, true ); |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Localize script with module settings. |
| 228 |
* |
| 229 |
* @param array $setting The merchant global object setting parameter. |
| 230 |
* |
| 231 |
* @return array $setting The merchant global object setting parameter. |
| 232 |
*/ |
| 233 |
public function localize_script( $setting ) { |
| 234 |
//$module_settings = $this->get_module_settings(); |
| 235 |
|
| 236 |
$setting['pre_orders'] = true; |
| 237 |
$rule = $this->current_rule(); |
| 238 |
if ( ! empty( $rule ) && $rule['button_text'] ) { |
| 239 |
$setting['pre_orders_add_button_title'] = Merchant_Translator::translate( $rule['button_text'] ); |
| 240 |
} else { |
| 241 |
$setting['pre_orders_add_button_title'] = esc_html__( 'Pre Order Now!', 'merchant' ); |
| 242 |
} |
| 243 |
|
| 244 |
return $setting; |
| 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 |
$settings = $this->get_module_settings(); |
| 258 |
|
| 259 |
// Additional text. |
| 260 |
$additional_text = $settings['additional_text']; |
| 261 |
$time_format = date_i18n( get_option( 'date_format' ), strtotime( gmdate( 'Y-m-d', strtotime( '+2 days' ) ) ) ); |
| 262 |
$text = $this->main_func->replace_date_text( $additional_text, $time_format ); |
| 263 |
|
| 264 |
ob_start(); |
| 265 |
self::admin_preview_content( $settings, $text ); |
| 266 |
$content = ob_get_clean(); |
| 267 |
|
| 268 |
// HTML. |
| 269 |
$preview->set_html( $content ); |
| 270 |
|
| 271 |
// Button Text. |
| 272 |
$preview->set_text( 'button_text', '.add_to_cart_button' ); |
| 273 |
|
| 274 |
// Additional Text. |
| 275 |
$preview->set_text( 'additional_text', '.merchant-pre-orders-date', array( |
| 276 |
array( |
| 277 |
'{date}', |
| 278 |
), |
| 279 |
array( |
| 280 |
$time_format, |
| 281 |
), |
| 282 |
) ); |
| 283 |
} |
| 284 |
|
| 285 |
return $preview; |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Admin preview content. |
| 290 |
* |
| 291 |
* @return void |
| 292 |
*/ |
| 293 |
public function admin_preview_content( $settings, $text ) { |
| 294 |
?> |
| 295 |
|
| 296 |
<div class="mrc-preview-single-product-elements"> |
| 297 |
<div class="mrc-preview-left-column"> |
| 298 |
<div class="mrc-preview-product-image-wrapper"> |
| 299 |
<div class="mrc-preview-product-image"></div> |
| 300 |
<div class="mrc-preview-product-image-thumbs"> |
| 301 |
<div class="mrc-preview-product-image-thumb"></div> |
| 302 |
<div class="mrc-preview-product-image-thumb"></div> |
| 303 |
<div class="mrc-preview-product-image-thumb"></div> |
| 304 |
</div> |
| 305 |
</div> |
| 306 |
</div> |
| 307 |
<div class="mrc-preview-right-column"> |
| 308 |
<div class="mrc-preview-text-placeholder"></div> |
| 309 |
<div class="mrc-preview-text-placeholder mrc-mw-70"></div> |
| 310 |
<div class="mrc-preview-text-placeholder mrc-mw-40 mrc-hide-on-smaller-screens"></div> |
| 311 |
<div class="mrc-preview-text-placeholder mrc-mw-30 mrc-hide-on-smaller-screens"></div> |
| 312 |
<div class="merchant-pre-ordered-product"> |
| 313 |
<div class="merchant-pre-orders-date"><?php |
| 314 |
printf( '<div class="merchant-pre-orders-date">%s</div>', esc_html( $text ) ); ?></div> |
| 315 |
<a href="#" class="add_to_cart_button"><?php |
| 316 |
echo esc_html( $settings['button_text'] ); ?></a> |
| 317 |
</div> |
| 318 |
</div> |
| 319 |
</div> |
| 320 |
|
| 321 |
<?php |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* Custom CSS. |
| 326 |
* |
| 327 |
* @return string |
| 328 |
*/ |
| 329 |
public function get_module_custom_css() { |
| 330 |
$css = ''; |
| 331 |
|
| 332 |
if ( is_admin() || is_singular( 'product' ) ) { |
| 333 |
// Text Color. |
| 334 |
$css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'text-color', '#FFF', '.merchant-pre-ordered-product', '--mrc-po-text-color' ); |
| 335 |
|
| 336 |
// Text Color (hover). |
| 337 |
$css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'text-hover-color', '#FFF', '.merchant-pre-ordered-product', '--mrc-po-text-hover-color' ); |
| 338 |
|
| 339 |
// Border Color. |
| 340 |
$css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'border-color', '#212121', '.merchant-pre-ordered-product', '--mrc-po-border-color' ); |
| 341 |
|
| 342 |
// Border Color (hover). |
| 343 |
$css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'border-hover-color', '#414141', '.merchant-pre-ordered-product', '--mrc-po-border-hover-color' ); |
| 344 |
|
| 345 |
// Background Color. |
| 346 |
$css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'background-color', '#212121', '.merchant-pre-ordered-product', '--mrc-po-background-color' ); |
| 347 |
|
| 348 |
// Background Color (hover). |
| 349 |
$css .= Merchant_Custom_CSS::get_variable_css( 'pre-orders', 'background-hover-color', '#414141', '.merchant-pre-ordered-product', '--mrc-po-background-hover-color' ); |
| 350 |
} |
| 351 |
|
| 352 |
return $css; |
| 353 |
} |
| 354 |
|
| 355 |
/** |
| 356 |
* Admin custom CSS. |
| 357 |
* |
| 358 |
* @param string $css The custom CSS. |
| 359 |
* |
| 360 |
* @return string $css The custom CSS. |
| 361 |
*/ |
| 362 |
public function admin_custom_css( $css ) { |
| 363 |
$css .= $this->get_module_custom_css(); |
| 364 |
|
| 365 |
return $css; |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Frontend custom CSS. |
| 370 |
* |
| 371 |
* @param string $css The custom CSS. |
| 372 |
* |
| 373 |
* @return string $css The custom CSS. |
| 374 |
*/ |
| 375 |
public function frontend_custom_css( $css ) { |
| 376 |
$css .= $this->get_module_custom_css(); |
| 377 |
|
| 378 |
return $css; |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Get current product rule. |
| 383 |
* |
| 384 |
* @return array |
| 385 |
*/ |
| 386 |
private function current_rule() { |
| 387 |
if ( is_singular( 'product' ) ) { |
| 388 |
$product = wc_get_product( get_queried_object_id() ); |
| 389 |
$rule = Merchant_Pre_Orders_Main_Functionality::available_product_rule( $product->get_id() ); |
| 390 |
if ( empty( $rule ) && $product->is_type( 'variable' ) ) { |
| 391 |
$available_variations = $product->get_available_variations(); |
| 392 |
foreach ( $available_variations as $variation ) { |
| 393 |
$rule = Merchant_Pre_Orders_Main_Functionality::available_product_rule( $variation['variation_id'] ); |
| 394 |
if ( ! empty( $rule ) ) { |
| 395 |
break; |
| 396 |
} |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
return $rule; |
| 401 |
} |
| 402 |
|
| 403 |
return array(); |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Help banner. |
| 408 |
* |
| 409 |
* @return void |
| 410 |
*/ |
| 411 |
public function help_banner( $module_id ) { |
| 412 |
if ( $module_id === 'pre-orders' ) { |
| 413 |
?> |
| 414 |
<div class="merchant-module-page-setting-fields"> |
| 415 |
<div class="merchant-module-page-setting-field merchant-module-page-setting-field-content"> |
| 416 |
<div class="merchant-module-page-setting-field-inner"> |
| 417 |
<div class="merchant-tag-pre-orders"> |
| 418 |
<i class="dashicons dashicons-info"></i> |
| 419 |
<p> |
| 420 |
<?php |
| 421 |
echo esc_html__( |
| 422 |
'Pre-orders captured by Merchant are tagged with "MerchantPreOrder" and can be found in your WooCommerce Order Section.', |
| 423 |
'merchant' |
| 424 |
); |
| 425 |
printf( |
| 426 |
'<a href="%1s" target="_blank">%2s</a>', |
| 427 |
esc_url( admin_url( 'edit.php?post_type=shop_order' ) ), |
| 428 |
esc_html__( 'View Pre-Orders', 'merchant' ) |
| 429 |
); |
| 430 |
?></p> |
| 431 |
</div> |
| 432 |
</div> |
| 433 |
</div> |
| 434 |
</div> |
| 435 |
<?php |
| 436 |
} |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
// Main functionality. |
| 441 |
require MERCHANT_DIR . 'inc/modules/pre-orders/class-pre-orders-main-functionality.php'; |
| 442 |
|
| 443 |
// Initialize the module. |
| 444 |
add_action( 'init', function () { |
| 445 |
new Merchant_Pre_Orders( new Merchant_Pre_Orders_Main_Functionality() ); |
| 446 |
} ); |
| 447 |
|