| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Wait list |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Wait list class. |
| 15 |
* |
| 16 |
*/ |
| 17 |
class Merchant_Wait_List extends Merchant_Add_Module { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module ID. |
| 21 |
* |
| 22 |
*/ |
| 23 |
const MODULE_ID = 'wait-list'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Module path. |
| 27 |
*/ |
| 28 |
const MODULE_DIR = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID; |
| 29 |
|
| 30 |
/** |
| 31 |
* Is module preview. |
| 32 |
* |
| 33 |
*/ |
| 34 |
public static $is_module_preview = false; |
| 35 |
|
| 36 |
/** |
| 37 |
* Constructor. |
| 38 |
* |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
// Module id. |
| 42 |
$this->module_id = self::MODULE_ID; |
| 43 |
|
| 44 |
// WooCommerce only. |
| 45 |
$this->wc_only = true; |
| 46 |
|
| 47 |
// Parent construct. |
| 48 |
parent::__construct(); |
| 49 |
|
| 50 |
// Module section. |
| 51 |
$this->module_section = 'boost-revenue'; |
| 52 |
|
| 53 |
// Module default settings. |
| 54 |
$this->module_default_settings = array( |
| 55 |
'form_title' => __( 'Email me when this item is back in stock.', 'merchant' ), |
| 56 |
'form_email_label' => __( 'Your Email Address', 'merchant' ), |
| 57 |
'form_button_text' => __( 'Notify Me', 'merchant' ), |
| 58 |
'form_success_message' => __( 'You have been successfully added to our stock waitlist. As soon as new stock becomes available, we will notify you via email.', |
| 59 |
'merchant' ), |
| 60 |
'email_new_subscriber' => __( 'Hello, thank you for joining the stock notification list for {product}. We will email you when the product is back in stock.', |
| 61 |
'merchant' ), |
| 62 |
'form_unsubscribe_message' => __( 'You have been successfully unsubscribed from our stock waitlist for this product.', 'merchant' ), |
| 63 |
'email_update' => __( 'Hello, thanks for your patience and finally the wait is over! Your {product} is now back in stock! We only have a limited amount of stock, and this email is not a guarantee you’ll get one. Add this {product} directly to your cart.', |
| 64 |
'merchant' ), |
| 65 |
'form_nonce_field' => wp_nonce_field( 'merchant_wait_list_action', 'merchant_wait_list_action', true, false ), |
| 66 |
); |
| 67 |
|
| 68 |
// Mount preview url. |
| 69 |
$preview_url = site_url( '/' ); |
| 70 |
|
| 71 |
if ( function_exists( 'wc_get_products' ) ) { |
| 72 |
$products = wc_get_products( array( 'limit' => 1 ) ); |
| 73 |
|
| 74 |
if ( ! empty( $products ) && ! empty( $products[0] ) ) { |
| 75 |
$preview_url = get_permalink( $products[0]->get_id() ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// Module data. |
| 80 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 81 |
$this->module_data['preview_url'] = $preview_url; |
| 82 |
|
| 83 |
// Module options path. |
| 84 |
$this->module_options_path = self::MODULE_DIR . '/admin/options.php'; |
| 85 |
|
| 86 |
// Is module preview page. |
| 87 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 88 |
self::$is_module_preview = true; |
| 89 |
|
| 90 |
// Enqueue admin styles. |
| 91 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 92 |
|
| 93 |
// Admin preview box. |
| 94 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 95 |
} |
| 96 |
|
| 97 |
if ( Merchant_Modules::is_module_active( self::MODULE_ID ) && is_admin() ) { |
| 98 |
// Init translations. |
| 99 |
$this->init_translations(); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Init translations. |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function init_translations() { |
| 109 |
$settings = $this->get_module_settings(); |
| 110 |
|
| 111 |
if ( ! empty( $settings['form_title'] ) ) { |
| 112 |
Merchant_Translator::register_string( $settings['form_title'], esc_html__( 'Wait list: Form title', 'merchant' ) ); |
| 113 |
} |
| 114 |
if ( ! empty( $settings['form_email_label'] ) ) { |
| 115 |
Merchant_Translator::register_string( $settings['form_email_label'], esc_html__( 'Wait list: Form email label', 'merchant' ) ); |
| 116 |
} |
| 117 |
if ( ! empty( $settings['form_button_text'] ) ) { |
| 118 |
Merchant_Translator::register_string( $settings['form_button_text'], esc_html__( 'Wait list: Form button text', 'merchant' ) ); |
| 119 |
} |
| 120 |
if ( ! empty( $settings['form_success_message'] ) ) { |
| 121 |
Merchant_Translator::register_string( $settings['form_success_message'], esc_html__( 'Wait list: Form success message', 'merchant' ) ); |
| 122 |
} |
| 123 |
if ( ! empty( $settings['email_new_subscriber'] ) ) { |
| 124 |
Merchant_Translator::register_string( $settings['email_new_subscriber'], esc_html__( 'Wait list: Email new subscriber', 'merchant' ) ); |
| 125 |
} |
| 126 |
if ( ! empty( $settings['email_update'] ) ) { |
| 127 |
Merchant_Translator::register_string( $settings['email_update'], esc_html__( 'Wait list: Email update', 'merchant' ) ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Admin enqueue CSS. |
| 133 |
* |
| 134 |
* @return void |
| 135 |
*/ |
| 136 |
public function admin_enqueue_css() { |
| 137 |
if ( parent::is_module_settings_page() ) { |
| 138 |
wp_enqueue_style( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/wait-list.min.css', array(), MERCHANT_VERSION ); |
| 139 |
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Render admin preview |
| 145 |
* |
| 146 |
* @param Merchant_Admin_Preview $preview |
| 147 |
* @param string $module |
| 148 |
* |
| 149 |
* @return Merchant_Admin_Preview |
| 150 |
*/ |
| 151 |
public function render_admin_preview( $preview, $module ) { |
| 152 |
if ( self::MODULE_ID === $module ) { |
| 153 |
ob_start(); |
| 154 |
self::admin_preview_content(); |
| 155 |
$content = ob_get_clean(); |
| 156 |
|
| 157 |
$preview->set_html( $content ); |
| 158 |
|
| 159 |
$preview->set_text( 'form_title', '.merchant-wait-list-title' ); |
| 160 |
$preview->set_text( 'form_button_text', '.merchant-wait-list-submit' ); |
| 161 |
$preview->set_attribute( 'form_email_label', '#merchant-wait-list-email', 'placeholder' ); |
| 162 |
} |
| 163 |
|
| 164 |
return $preview; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Admin preview content. |
| 169 |
* |
| 170 |
* @return void |
| 171 |
*/ |
| 172 |
public function admin_preview_content() { |
| 173 |
$settings = $this->get_module_settings(); |
| 174 |
?> |
| 175 |
|
| 176 |
<div class="mrc-preview-single-product-elements"> |
| 177 |
<div class="mrc-preview-left-column"> |
| 178 |
<div class="mrc-preview-product-image-wrapper"> |
| 179 |
<div class="mrc-preview-product-image"></div> |
| 180 |
<div class="mrc-preview-product-image-thumbs"> |
| 181 |
<div class="mrc-preview-product-image-thumb"></div> |
| 182 |
<div class="mrc-preview-product-image-thumb"></div> |
| 183 |
<div class="mrc-preview-product-image-thumb"></div> |
| 184 |
</div> |
| 185 |
</div> |
| 186 |
</div> |
| 187 |
<div class="mrc-preview-right-column"> |
| 188 |
<div class="mrc-preview-text-placeholder"></div> |
| 189 |
<div class="mrc-preview-text-placeholder mrc-mw-70"></div> |
| 190 |
<div class="mrc-preview-text-placeholder mrc-mw-30"></div> |
| 191 |
<div class="preview-merchant-wait-list"> |
| 192 |
<?php |
| 193 |
$preview_html = str_replace( 'required', '', $this->get_html( $settings ) ); |
| 194 |
echo wp_kses( $preview_html, merchant_kses_allowed_tags( array( 'forms', 'nonce' ) ) ); ?> |
| 195 |
</div> |
| 196 |
</div> |
| 197 |
</div> |
| 198 |
|
| 199 |
<?php |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Get the module HTML. |
| 204 |
* |
| 205 |
* @param array $settings |
| 206 |
* |
| 207 |
* @return string |
| 208 |
*/ |
| 209 |
public function get_html( $settings ) { |
| 210 |
$product_id = isset( $settings[ 'product_id' ] ) ? absint( $settings[ 'product_id' ] ) : 0; |
| 211 |
$html = '<div class="merchant-wait-list-container">'; |
| 212 |
$html .= '<div class="merchant-cover">'; |
| 213 |
$html .= '<div class="merchant-wait-list-loader">'; |
| 214 |
$html .= '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path opacity="0.4" d="M478.71 364.58zm-22 6.11l-27.83-15.9a15.92 15.92 0 0 1-6.94-19.2A184 184 0 1 1 256 72c5.89 0 11.71.29 17.46.83-.74-.07-1.48-.15-2.23-.21-8.49-.69-15.23-7.31-15.23-15.83v-32a16 16 0 0 1 15.34-16C266.24 8.46 261.18 8 256 8 119 8 8 119 8 256s111 248 248 248c98 0 182.42-56.95 222.71-139.42-4.13 7.86-14.23 10.55-22 6.11z" /><path d="M271.23 72.62c-8.49-.69-15.23-7.31-15.23-15.83V24.73c0-9.11 7.67-16.78 16.77-16.17C401.92 17.18 504 124.67 504 256a246 246 0 0 1-25 108.24c-4 8.17-14.37 11-22.26 6.45l-27.84-15.9c-7.41-4.23-9.83-13.35-6.2-21.07A182.53 182.53 0 0 0 440 256c0-96.49-74.27-175.63-168.77-183.38z" /></svg>'; |
| 215 |
$html .= '</div>'; |
| 216 |
$html .= '</div>'; |
| 217 |
$html .= '<form id="merchant-wait-list" class="merchant-wait-list" method="post">'; |
| 218 |
$html .= '<p class="merchant-wait-list-title">' . Merchant_Translator::translate( $settings[ 'form_title' ] ) . '</p>'; |
| 219 |
$html .= '<div class="merchant-wait-list-email">'; |
| 220 |
$html .= '<label for="merchant-wait-list-email">' . Merchant_Translator::translate( $settings[ 'form_email_label' ] ) . ' <abbr class="required" title="required">*</abbr></label>'; |
| 221 |
$html .= '<input type="email" name="merchant-wait-list-email" id="merchant-wait-list-email" value="" autocomplete="email" placeholder="' . Merchant_Translator::translate( $settings[ 'form_email_label' ] ) . '" required="">'; |
| 222 |
$html .= '</div>'; |
| 223 |
$html .= '<button class="merchant-wait-list-submit" type="submit" name="subscribe">' . Merchant_Translator::translate( $settings[ 'form_button_text' ] ) . '</button>'; |
| 224 |
$html .= $settings['form_nonce_field']; |
| 225 |
$html .= '<input type="hidden" name="merchant-wait-list-product-id" id="merchant-wait-list-product-id" value="' . $product_id . '" >'; |
| 226 |
$html .= '</form>'; |
| 227 |
$html .= '</div>'; |
| 228 |
|
| 229 |
return $html; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
// Initialize the module. |
| 234 |
add_action( 'init', function () { |
| 235 |
Merchant_Modules::create_module( new Merchant_Wait_List() ); |
| 236 |
} ); |
| 237 |
|