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