PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 1.8.2
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v1.8.2
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 / classes / class-merchant-modules.php

class-merchant-modules.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 1.8.2, at inc/classes/class-merchant-modules.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 * Merchant_Modules Class.
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly
8 }
9
10 if ( ! class_exists( 'Merchant_Modules' ) ) {
11
12 class Merchant_Modules {
13
14 /**
15 * The modules container.
16 */
17 private $container = array();
18
19 /**
20 * Option name
21 */
22 public static $option = 'merchant-modules';
23
24 /**
25 * The single class instance.
26 */
27 private static $instance = null;
28
29 /**
30 * Instance.
31 */
32 public static function instance() {
33 if ( is_null( self::$instance ) ) {
34 self::$instance = new self();
35 }
36 return self::$instance;
37 }
38
39 /**
40 * Constructor.
41 */
42 public function __construct() {
43
44 add_action( 'wp_ajax_merchant_module_activate', array( $this, 'activate_module' ) );
45 add_action( 'wp_ajax_merchant_module_deactivate', array( $this, 'deactivate_module' ) );
46 add_action( 'wp_ajax_merchant_module_feedback', array( $this, 'feedback_module' ) );
47 }
48
49 /**
50 * Activate module with Ajax.
51 */
52 public function activate_module() {
53 $nonce = ( isset( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
54 $module = ( isset( $_POST['module'] ) ) ? sanitize_text_field( wp_unslash( $_POST['module'] ) ) : '';
55
56 // Check current user capabilities
57 if ( ! current_user_can( 'manage_options' ) ) {
58 wp_send_json_error( 'You are not allowed to do this.' );
59 }
60
61 if ( wp_verify_nonce( $nonce, 'merchant' ) && ! empty( $module ) ) {
62
63 $modules = get_option( self::$option, array() );
64
65 $modules[ $module ] = true;
66
67 update_option( self::$option, $modules );
68
69 wp_send_json_success();
70
71 }
72
73 wp_send_json_error();
74 }
75
76 /**
77 * Deactivate module with Ajax.
78 */
79 public function deactivate_module() {
80 $nonce = ( isset( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
81 $module = ( isset( $_POST['module'] ) ) ? sanitize_text_field( wp_unslash( $_POST['module'] ) ) : '';
82
83 // Check current user capabilities
84 if ( ! current_user_can( 'manage_options' ) ) {
85 wp_send_json_error( 'You are not allowed to do this.' );
86 }
87
88 if ( wp_verify_nonce( $nonce, 'merchant' ) && ! empty( $module ) ) {
89
90 $modules = get_option( self::$option, array() );
91
92 $modules[ $module ] = false;
93
94 update_option( self::$option, $modules );
95
96 wp_send_json_success();
97
98 }
99
100 wp_send_json_error();
101 }
102
103 /**
104 * Feedback module with Ajax.
105 */
106 public function feedback_module() {
107 $nonce = ( isset( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : '';
108 $subject = ( isset( $_POST['subject'] ) ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : '';
109 $message = ( isset( $_POST['message'] ) ) ? sanitize_textarea_field( wp_unslash( $_POST['message'] ) ) : '';
110 $module = ( isset( $_POST['module'] ) ) ? sanitize_text_field( wp_unslash( $_POST['module'] ) ) : '';
111 $from = get_bloginfo( 'admin_email' );
112
113 // Check current user capabilities
114 if ( ! current_user_can( 'manage_options' ) ) {
115 wp_send_json_error( 'You are not allowed to do this.' );
116 }
117
118 if ( wp_verify_nonce( $nonce, 'merchant' ) ) {
119 $response = wp_remote_post( 'https://athemes.com/merchant/', array(
120 'body' => array(
121 'mailsender' => true,
122 'from' => $from,
123 'subject' => $subject,
124 'message' => $message,
125 'module' => $module,
126 ),
127 ) );
128
129 if ( is_wp_error( $response ) ) {
130 wp_send_json_error();
131 }
132
133 wp_send_json_success();
134 }
135
136 wp_send_json_error();
137 }
138
139 /**
140 * Creates and adds the module instance to the container.
141 *
142 * @param Merchant_Add_Module $module The module instance.
143 *
144 * @return void
145 */
146 public static function create_module( Merchant_Add_Module $module ) {
147 static::instance()->container[ $module->module_id ] = $module;
148 }
149
150 /**
151 * Get the module instance.
152 *
153 * @param string $module_id The module ID.
154 *
155 * @return Merchant_Add_Module|mixed The module instance.
156 */
157 public static function get_module( $module_id ) {
158 return static::instance()->container[ $module_id ];
159 }
160
161 /**
162 * Determines if a module has already been added to the container.
163 *
164 * @param string $module_id The module ID.
165 *
166 * @return bool
167 */
168 public static function is_module_created( $module_id ) {
169 return in_array( $module_id, static::instance()->container, true );
170 }
171
172 /**
173 * Check if a specific module is activated
174 */
175 public static function is_module_active( $module ) {
176 /**
177 * Hook 'merchant_module_{$module}_deactivate'
178 *
179 * @since 1.0
180 */
181 if ( apply_filters( "merchant_module_{$module}_deactivate", false ) ) {
182 add_filter( "merchant_admin_module_{$module}_list_item_class", static function( $class_name ) {
183 return $class_name . ' merchant-module-deactivated-by-bp';
184 } );
185
186 return false;
187 }
188
189 $modules = get_option( self::$option, array() );
190
191 // Preview Mode
192 if ( isset( $_GET['preview'] ) && isset( $_GET['module'] ) && $_GET['module'] === $module && current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
193 return true;
194 }
195
196 // Preview Mode
197 $operation_mode = Merchant_Option::get( 'global-settings', 'operating_mode', 'active' );
198
199 if ( 'inactive' === $operation_mode ) {
200 return false;
201 } elseif ( 'preview' === $operation_mode && ! current_user_can( 'manage_options' ) ) {
202 return false;
203 }
204
205 if ( is_array( $modules ) && array_key_exists( $module, $modules ) && true === $modules[ $module ] ) {
206 return true;
207 }
208
209 return false;
210 }
211 }
212
213 Merchant_Modules::instance();
214
215 }
216