| @@ -1,219 +1,277 @@ | ||
| 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 | -} | |
| 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 | +} | |