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