| 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 |
* @var array<string, Merchant_Add_Module> |
| 18 |
*/ |
| 19 |
private $container = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* Option name |
| 23 |
*/ |
| 24 |
public static $option = 'merchant-modules'; |
| 25 |
|
| 26 |
/** |
| 27 |
* The single class instance. |
| 28 |
*/ |
| 29 |
private static $instance = null; |
| 30 |
|
| 31 |
/** |
| 32 |
* Instance. |
| 33 |
*/ |
| 34 |
public static function instance() { |
| 35 |
if ( is_null( self::$instance ) ) { |
| 36 |
self::$instance = new self(); |
| 37 |
} |
| 38 |
return self::$instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Constructor. |
| 43 |
*/ |
| 44 |
public function __construct() { |
| 45 |
add_action( 'wp_ajax_merchant_module_activate', array( $this, 'activate_module' ) ); |
| 46 |
add_action( 'wp_ajax_merchant_module_deactivate', array( $this, 'deactivate_module' ) ); |
| 47 |
add_action( 'wp_ajax_merchant_module_feedback', array( $this, 'feedback_module' ) ); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Activate module with Ajax. |
| 52 |
*/ |
| 53 |
public function activate_module() { |
| 54 |
$nonce = ( isset( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 55 |
$module = ( isset( $_POST['module'] ) ) ? sanitize_text_field( wp_unslash( $_POST['module'] ) ) : ''; |
| 56 |
|
| 57 |
// Check current user capabilities |
| 58 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 59 |
wp_send_json_error( 'You are not allowed to do this.' ); |
| 60 |
} |
| 61 |
|
| 62 |
if ( wp_verify_nonce( $nonce, 'merchant' ) && ! empty( $module ) ) { |
| 63 |
self::set_module_active( $module ); |
| 64 |
wp_send_json_success(); |
| 65 |
} |
| 66 |
|
| 67 |
wp_send_json_error(); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Deactivate module with Ajax. |
| 72 |
*/ |
| 73 |
public function deactivate_module() { |
| 74 |
$nonce = ( isset( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 75 |
$module = ( isset( $_POST['module'] ) ) ? sanitize_text_field( wp_unslash( $_POST['module'] ) ) : ''; |
| 76 |
|
| 77 |
// Check current user capabilities |
| 78 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 79 |
wp_send_json_error( 'You are not allowed to do this.' ); |
| 80 |
} |
| 81 |
|
| 82 |
if ( wp_verify_nonce( $nonce, 'merchant' ) && ! empty( $module ) ) { |
| 83 |
self::set_module_inactive( $module ); |
| 84 |
wp_send_json_success(); |
| 85 |
} |
| 86 |
|
| 87 |
wp_send_json_error(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Feedback module with Ajax. |
| 92 |
*/ |
| 93 |
public function feedback_module() { |
| 94 |
$nonce = ( isset( $_POST['nonce'] ) ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : ''; |
| 95 |
$subject = ( isset( $_POST['subject'] ) ) ? sanitize_text_field( wp_unslash( $_POST['subject'] ) ) : ''; |
| 96 |
$message = ( isset( $_POST['message'] ) ) ? sanitize_textarea_field( wp_unslash( $_POST['message'] ) ) : ''; |
| 97 |
$module = ( isset( $_POST['module'] ) ) ? sanitize_text_field( wp_unslash( $_POST['module'] ) ) : ''; |
| 98 |
$from = get_bloginfo( 'admin_email' ); |
| 99 |
|
| 100 |
// Check current user capabilities |
| 101 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 102 |
wp_send_json_error( 'You are not allowed to do this.' ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( wp_verify_nonce( $nonce, 'merchant' ) ) { |
| 106 |
$response = wp_remote_post( 'https://athemes.com/merchant/', array( |
| 107 |
'body' => array( |
| 108 |
'mailsender' => true, |
| 109 |
'from' => $from, |
| 110 |
'subject' => $subject, |
| 111 |
'message' => $message, |
| 112 |
'module' => $module, |
| 113 |
), |
| 114 |
) ); |
| 115 |
|
| 116 |
if ( is_wp_error( $response ) ) { |
| 117 |
wp_send_json_error(); |
| 118 |
} |
| 119 |
|
| 120 |
wp_send_json_success(); |
| 121 |
} |
| 122 |
|
| 123 |
wp_send_json_error(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Creates and adds the module instance to the container. |
| 128 |
* |
| 129 |
* @param Merchant_Add_Module $module The module instance. |
| 130 |
* |
| 131 |
* @return void |
| 132 |
*/ |
| 133 |
public static function create_module( Merchant_Add_Module $module ) { |
| 134 |
static::instance()->container[ $module->module_id ] = $module; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get the module instance. |
| 139 |
* |
| 140 |
* @param string $module_id The module ID. |
| 141 |
* |
| 142 |
* @return Merchant_Add_Module|null The module instance, or null if not registered. |
| 143 |
*/ |
| 144 |
public static function get_module( $module_id ) { |
| 145 |
return static::instance()->container[ $module_id ] ?? null; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Determines if a module has already been added to the container. |
| 150 |
* |
| 151 |
* @param string $module_id The module ID. |
| 152 |
* |
| 153 |
* @return bool |
| 154 |
*/ |
| 155 |
public static function is_module_created( $module_id ) { |
| 156 |
return isset( static::instance()->container[ $module_id ] ); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Activate a module. |
| 161 |
* |
| 162 |
* Sets the module status to active in the database and fires |
| 163 |
* the merchant_admin_module_activated action hook. |
| 164 |
* |
| 165 |
* @param string $module_id The module identifier. |
| 166 |
* |
| 167 |
* @return void |
| 168 |
* |
| 169 |
* @since 2.3.0 |
| 170 |
*/ |
| 171 |
public static function set_module_active( $module_id ) { |
| 172 |
$modules = get_option( self::$option, array() ); |
| 173 |
|
| 174 |
$modules[ $module_id ] = true; |
| 175 |
|
| 176 |
update_option( self::$option, $modules ); |
| 177 |
|
| 178 |
/** |
| 179 |
* Fires when a module is activated. |
| 180 |
* |
| 181 |
* @param string $module_id The module identifier. |
| 182 |
* |
| 183 |
* @since 1.9.3 |
| 184 |
*/ |
| 185 |
do_action( 'merchant_admin_module_activated', $module_id ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Deactivate a module. |
| 190 |
* |
| 191 |
* Sets the module status to inactive in the database and fires |
| 192 |
* the merchant_admin_module_deactivated action hook. |
| 193 |
* |
| 194 |
* @param string $module_id The module identifier. |
| 195 |
* |
| 196 |
* @return void |
| 197 |
* |
| 198 |
* @since 2.3.0 |
| 199 |
*/ |
| 200 |
public static function set_module_inactive( $module_id ) { |
| 201 |
$modules = get_option( self::$option, array() ); |
| 202 |
|
| 203 |
$modules[ $module_id ] = false; |
| 204 |
|
| 205 |
update_option( self::$option, $modules ); |
| 206 |
|
| 207 |
/** |
| 208 |
* Fires when a module is deactivated. |
| 209 |
* |
| 210 |
* @param string $module_id The module identifier. |
| 211 |
* |
| 212 |
* @since 1.9.3 |
| 213 |
*/ |
| 214 |
do_action( 'merchant_admin_module_deactivated', $module_id ); |
| 215 |
} |
| 216 |
|
| 217 |
/** |
| 218 |
* Check if a specific module is activated |
| 219 |
*/ |
| 220 |
public static function is_module_active( $module ) { |
| 221 |
/** |
| 222 |
* Hook 'merchant_module_{$module}_deactivate' |
| 223 |
* |
| 224 |
* @since 1.0 |
| 225 |
*/ |
| 226 |
if ( apply_filters( "merchant_module_{$module}_deactivate", false ) ) { |
| 227 |
add_filter( "merchant_admin_module_{$module}_list_item_class", static function( $class_name ) { |
| 228 |
return $class_name . ' merchant-module-deactivated-by-bp'; |
| 229 |
} ); |
| 230 |
|
| 231 |
if ( isset( $_GET['module'] ) && $_GET['module'] === $module ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 232 |
add_filter( "merchant_admin_module_{$module}_activate_button_class", static function( $class_name ) { |
| 233 |
if ( strpos( $class_name, 'merchant-module-deactivated-by-bp' ) !== false ) { |
| 234 |
return $class_name; |
| 235 |
} |
| 236 |
|
| 237 |
return $class_name . ' merchant-module-deactivated-by-bp'; |
| 238 |
} ); |
| 239 |
} |
| 240 |
|
| 241 |
return false; |
| 242 |
} |
| 243 |
|
| 244 |
$modules = get_option( self::$option, array() ); |
| 245 |
|
| 246 |
// Preview Mode |
| 247 |
if ( isset( $_GET['preview'] ) && isset( $_GET['module'] ) && $_GET['module'] === $module && current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 248 |
return true; |
| 249 |
} |
| 250 |
|
| 251 |
// Preview Mode |
| 252 |
$operation_mode = Merchant_Option::get( 'global-settings', 'operating_mode', 'active' ); |
| 253 |
|
| 254 |
if ( 'inactive' === $operation_mode ) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
|
| 258 |
if ( 'preview' === $operation_mode ) { |
| 259 |
if ( ! function_exists( 'wp_get_current_user' ) ) { |
| 260 |
include ABSPATH . 'wp-includes/pluggable.php'; |
| 261 |
} |
| 262 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 263 |
return false; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if ( is_array( $modules ) && array_key_exists( $module, $modules ) && true === $modules[ $module ] ) { |
| 268 |
return true; |
| 269 |
} |
| 270 |
|
| 271 |
return false; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
Merchant_Modules::instance(); |
| 276 |
|
| 277 |
} |
| 278 |
|