| @@ -12,8 +12,10 @@ | ||
| 12 | 12 | class Merchant_Modules { |
| 13 | 13 | |
| 14 | 14 | /** |
| 15 | 15 | * The modules container. |
| 16 | + * | |
| 17 | + * @var array<string, Merchant_Add_Module> | |
| 16 | 18 | */ |
| 17 | 19 | private $container = array(); |
| 18 | 20 | |
| 19 | 21 | /** |
| @@ -57,24 +59,10 @@ | ||
| 57 | 59 | wp_send_json_error( 'You are not allowed to do this.' ); |
| 58 | 60 | } |
| 59 | 61 | |
| 60 | 62 | 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 | - | |
| 63 | + self::set_module_active( $module ); | |
| 75 | 64 | wp_send_json_success(); |
| 76 | - | |
| 77 | 65 | } |
| 78 | 66 | |
| 79 | 67 | wp_send_json_error(); |
| 80 | 68 | } |
| @@ -91,24 +79,10 @@ | ||
| 91 | 79 | wp_send_json_error( 'You are not allowed to do this.' ); |
| 92 | 80 | } |
| 93 | 81 | |
| 94 | 82 | 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 | - | |
| 83 | + self::set_module_inactive( $module ); | |
| 109 | 84 | wp_send_json_success(); |
| 110 | - | |
| 111 | 85 | } |
| 112 | 86 | |
| 113 | 87 | wp_send_json_error(); |
| 114 | 88 | } |
| @@ -164,12 +138,12 @@ | ||
| 164 | 138 | * Get the module instance. |
| 165 | 139 | * |
| 166 | 140 | * @param string $module_id The module ID. |
| 167 | 141 | * |
| 168 | - * @return Merchant_Add_Module|mixed The module instance. | |
| 142 | + * @return Merchant_Add_Module|null The module instance, or null if not registered. | |
| 169 | 143 | */ |
| 170 | 144 | public static function get_module( $module_id ) { |
| 171 | - return static::instance()->container[ $module_id ]; | |
| 145 | + return static::instance()->container[ $module_id ] ?? null; | |
| 172 | 146 | } |
| 173 | 147 | |
| 174 | 148 | /** |
| 175 | 149 | * Determines if a module has already been added to the container. |
| @@ -178,9 +152,67 @@ | ||
| 178 | 152 | * |
| 179 | 153 | * @return bool |
| 180 | 154 | */ |
| 181 | 155 | public static function is_module_created( $module_id ) { |
| 182 | - return in_array( $module_id, static::instance()->container, true ); | |
| 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 ); | |
| 183 | 215 | } |
| 184 | 216 | |
| 185 | 217 | /** |
| 186 | 218 | * Check if a specific module is activated |