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