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

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