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

234 lines 6.5 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 ) && is_admin() ) {
82 // Init translations.
83 $this->init_translations();
84 }
85
86 if ( ! Merchant_Modules::is_module_active( self::MODULE_ID ) ) {
87 return;
88 }
89
90 // Return early if it's on admin but not in the respective module settings page.
91 if ( is_admin() && ! parent::is_module_settings_page() ) {
92 return;
93 }
94
95 // Enqueue scripts.
96 add_action( 'merchant_enqueue_after_main_css_js', array( $this, 'enqueue_scripts' ) );
97
98 // Localize script.
99 add_filter( 'merchant_localize_script', array( $this, 'localize_script' ) );
100
101 // Add merchant selector and content to cart fragments.
102 add_filter( 'woocommerce_add_to_cart_fragments', array( $this, 'cart_count_fragment' ) );
103 }
104
105 /**
106 * Init translations.
107 *
108 * @return void
109 */
110 public function init_translations() {
111 $settings = $this->get_module_settings();
112 if ( ! empty( $settings['message'] ) ) {
113 Merchant_Translator::register_string( $settings['message'], esc_html__( 'Inactive tab messages: message text', 'merchant' ) );
114 }
115 if ( ! empty( $settings['abandoned_message'] ) ) {
116 Merchant_Translator::register_string( $settings['abandoned_message'], esc_html__( 'Inactive tab messages abandoned message', 'merchant' ) );
117 }
118 }
119
120 /**
121 * Admin enqueue CSS.
122 *
123 * @return void
124 */
125 public function admin_enqueue_css() {
126 $page = ( ! empty( $_GET['page'] ) ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
127 $module = ( ! empty( $_GET['module'] ) ) ? sanitize_text_field( wp_unslash( $_GET['module'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
128
129 if ( 'merchant' === $page && self::MODULE_ID === $module ) {
130 wp_enqueue_style( 'merchant-admin-' . self::MODULE_ID, MERCHANT_URI . 'assets/css/modules/' . self::MODULE_ID . '/admin/preview.min.css', array(), MERCHANT_VERSION );
131 }
132 }
133
134 /**
135 * Enqueue scripts.
136 *
137 * @return void
138 */
139 public function enqueue_scripts() {
140
141 // Register and enqueue the main module script.
142 wp_enqueue_script( 'merchant-' . self::MODULE_ID, MERCHANT_URI . 'assets/js/modules/' . self::MODULE_ID . '/inactive-tab-message.min.js', array(), MERCHANT_VERSION, true );
143 }
144
145 /**
146 * Localize script with module settings.
147 *
148 * @param array $setting The merchant global object setting parameter.
149 * @return array $setting The merchant global object setting parameter.
150 */
151 public function localize_script( $setting ) {
152 $module_settings = $this->get_module_settings();
153
154 $setting['inactive_tab_messsage'] = Merchant_Translator::translate( $module_settings[ 'message' ] );
155 $setting['inactive_tab_abandoned_message'] = Merchant_Translator::translate( $module_settings[ 'abandoned_message' ] );
156 $setting['inactive_tab_cart_count'] = '0';
157
158 if ( function_exists( 'WC' ) ) {
159 $setting['inactive_tab_cart_count'] = WC()->cart->get_cart_contents_count();
160 }
161
162 return $setting;
163 }
164
165 /**
166 * Render admin preview
167 *
168 * @param Merchant_Admin_Preview $preview
169 * @param string $module
170 *
171 * @return Merchant_Admin_Preview
172 */
173 public function render_admin_preview( $preview, $module ) {
174 if ( self::MODULE_ID === $module ) {
175 ob_start();
176 self::admin_preview_content();
177 $content = ob_get_clean();
178
179 // HTML.
180 $preview->set_html( $content );
181
182 // Message.
183 $preview->set_text( 'message', '.mrc-inactive-tab-message-text' );
184
185 }
186
187 return $preview;
188 }
189
190 /**
191 * Admin preview content.
192 *
193 * @return void
194 */
195 public function admin_preview_content() {
196 $settings = $this->get_module_settings();
197 $favicon_url = get_site_icon_url() ? get_site_icon_url( 512 ) : MERCHANT_URI . 'inc/modules/' . self::MODULE_ID . '/admin/images/wplogo.svg';
198
199 ?>
200
201 <div class="mrc-preview-inactive-tab-message">
202 <div class="mrc-inactive-tab-message-icon-wrapper">
203 <div class="mrc-inactive-tab-message__icon">
204 <img src="<?php echo esc_url( $favicon_url ); ?>" />
205 </div>
206 </div>
207 <div class="mrc-inactive-tab-message-text">
208 <?php echo esc_html( Merchant_Translator::translate( $settings[ 'message' ] ) ); ?>
209 </div>
210 </div>
211
212 <?php
213 }
214
215 /**
216 * Cart count fragments.
217 *
218 * @param array $fragments The cart fragments.
219 * @return array $fragments The cart fragments.
220 */
221 public function cart_count_fragment( $fragments ) {
222 if ( Merchant_Modules::is_module_active( 'inactive-tab-message' ) ) {
223 $fragments['.merchant_cart_count'] = WC()->cart->get_cart_contents_count();
224 }
225
226 return $fragments;
227 }
228 }
229
230 // Initialize the module.
231 add_action( 'init', function() {
232 new Merchant_Inactive_Tab_Message();
233 } );
234