PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.6
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.6
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 / inactive-tab-message / class-inactive-tab-message.php

class-inactive-tab-message.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.6, at inc/modules/inactive-tab-message/class-inactive-tab-message.php

216 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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