| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Inactive Tab Message. |
| 5 |
* |
| 6 |
* @package Merchant |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; // Exit if accessed directly |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Inactive Tab Message Class. |
| 15 |
* |
| 16 |
*/ |
| 17 |
class Merchant_Inactive_Tab_Message extends Merchant_Add_Module { |
| 18 |
|
| 19 |
/** |
| 20 |
* Module ID. |
| 21 |
* |
| 22 |
*/ |
| 23 |
const MODULE_ID = 'inactive-tab-message'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Is module preview. |
| 27 |
* |
| 28 |
*/ |
| 29 |
public static $is_module_preview = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor. |
| 33 |
* |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
|
| 37 |
// Module id. |
| 38 |
$this->module_id = self::MODULE_ID; |
| 39 |
|
| 40 |
// WooCommerce only. |
| 41 |
$this->wc_only = true; |
| 42 |
|
| 43 |
// Parent construct. |
| 44 |
parent::__construct(); |
| 45 |
|
| 46 |
// Module section. |
| 47 |
$this->module_section = 'reduce-abandonment'; |
| 48 |
|
| 49 |
// Module default settings. |
| 50 |
$this->module_default_settings = array( |
| 51 |
'message' => __( '✋ Don\'t forget this', 'merchant' ), |
| 52 |
'abandoned_message' => __( '✋ You left something in the cart', 'merchant' ) |
| 53 |
); |
| 54 |
|
| 55 |
// Mount preview url. |
| 56 |
$preview_url = site_url( '/' ); |
| 57 |
|
| 58 |
if ( function_exists( 'wc_get_page_id' ) ) { |
| 59 |
$preview_url = get_permalink( wc_get_page_id( 'shop' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
// Module data. |
| 63 |
$this->module_data = Merchant_Admin_Modules::$modules_data[ self::MODULE_ID ]; |
| 64 |
$this->module_data[ 'preview_url' ] = $preview_url; |
| 65 |
|
| 66 |
// Module options path. |
| 67 |
$this->module_options_path = MERCHANT_DIR . 'inc/modules/' . self::MODULE_ID . '/admin/options.php'; |
| 68 |
|
| 69 |
// Is module preview page. |
| 70 |
if ( is_admin() && parent::is_module_settings_page() ) { |
| 71 |
self::$is_module_preview = true; |
| 72 |
|
| 73 |
// Enqueue admin styles. |
| 74 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_css' ) ); |
| 75 |
|
| 76 |
// Admin preview box. |
| 77 |
add_filter( 'merchant_module_preview', array( $this, 'render_admin_preview' ), 10, 2 ); |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
// Return early if it's on admin but not in the respective module settings page. |
| 86 |
if ( is_admin() && ! parent::is_module_settings_page() ) { |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// Enqueue scripts. |
| 91 |
add_action( 'merchant_enqueue_after_main_css_js', array( $this, 'enqueue_scripts' ) ); |
| 92 |
|
| 93 |
// Localize script. |
| 94 |
add_filter( 'merchant_localize_script', array( $this, 'localize_script' ) ); |
| 95 |
|
| 96 |
// Add merchant selector and content to cart fragments. |
| 97 |
add_filter( 'woocommerce_add_to_cart_fragments', array( $this, 'cart_count_fragment' ) ); |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Admin enqueue CSS. |
| 103 |
* |
| 104 |
* @return void |
| 105 |
*/ |
| 106 |
public function admin_enqueue_css() { |
| 107 |
$page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 108 |
$module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 109 |
|
| 110 |
if ( 'merchant' === $page && self::MODULE_ID === $module ) { |
| 111 |
wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Enqueue scripts. |
| 117 |
* |
| 118 |
* @return void |
| 119 |
*/ |
| 120 |
public function enqueue_scripts() { |
| 121 |
|
| 122 |
// Register and enqueue the main module script. |
| 123 |
wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/inactive-tab-message.min.js', array(), MERCHANT_VERSION, true ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Localize script with module settings. |
| 128 |
* |
| 129 |
* @param array $setting The merchant global object setting parameter. |
| 130 |
* @return array $setting The merchant global object setting parameter. |
| 131 |
*/ |
| 132 |
public function localize_script( $setting ) { |
| 133 |
$module_settings = $this->get_module_settings(); |
| 134 |
|
| 135 |
$setting['inactive_tab_messsage'] = $module_settings[ 'message' ]; |
| 136 |
$setting['inactive_tab_abandoned_message'] = $module_settings[ 'abandoned_message' ]; |
| 137 |
$setting['inactive_tab_cart_count'] = '0'; |
| 138 |
|
| 139 |
if ( function_exists( 'WC' ) ) { |
| 140 |
$setting['inactive_tab_cart_count'] = WC()->cart->get_cart_contents_count(); |
| 141 |
} |
| 142 |
|
| 143 |
return $setting; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Render admin preview |
| 148 |
* |
| 149 |
* @param Merchant_Admin_Preview $preview |
| 150 |
* @param string $module |
| 151 |
* |
| 152 |
* @return Merchant_Admin_Preview |
| 153 |
*/ |
| 154 |
public function render_admin_preview( $preview, $module ) { |
| 155 |
if ( self::MODULE_ID === $module ) { |
| 156 |
ob_start(); |
| 157 |
self::admin_preview_content(); |
| 158 |
$content = ob_get_clean(); |
| 159 |
|
| 160 |
// HTML. |
| 161 |
$preview->set_html( $content ); |
| 162 |
|
| 163 |
// Message. |
| 164 |
$preview->set_text( 'message', '.mrc-inactive-tab-message-text' ); |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
return $preview; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Admin preview content. |
| 173 |
* |
| 174 |
* @return void |
| 175 |
*/ |
| 176 |
public function admin_preview_content() { |
| 177 |
$settings = $this->get_module_settings(); |
| 178 |
$favicon_url = get_site_icon_url() ? get_site_icon_url( 512 ) : MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/wplogo.svg'; |
| 179 |
|
| 180 |
?> |
| 181 |
|
| 182 |
<div class="mrc-preview-inactive-tab-message"> |
| 183 |
<div class="mrc-inactive-tab-message-icon-wrapper"> |
| 184 |
<div class="mrc-inactive-tab-message__icon"> |
| 185 |
<img src="<?php echo esc_url( $favicon_url ); ?>" /> |
| 186 |
</div> |
| 187 |
</div> |
| 188 |
<div class="mrc-inactive-tab-message-text"> |
| 189 |
<?php echo esc_html( $settings[ 'message' ] ); ?> |
| 190 |
</div> |
| 191 |
</div> |
| 192 |
|
| 193 |
<?php |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Cart count fragments. |
| 198 |
* |
| 199 |
* @param array $fragments The cart fragments. |
| 200 |
* @return array $fragments The cart fragments. |
| 201 |
*/ |
| 202 |
public function cart_count_fragment( $fragments ) { |
| 203 |
if ( Merchant_Modules::is_module_active( 'inactive-tab-message' ) ) { |
| 204 |
$fragments['.merchant_cart_count'] = WC()->cart->get_cart_contents_count(); |
| 205 |
} |
| 206 |
|
| 207 |
return $fragments; |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
// Initialize the module. |
| 213 |
add_action( 'init', function() { |
| 214 |
new Merchant_Inactive_Tab_Message(); |
| 215 |
} ); |
| 216 |
|