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

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