PluginProbe
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant / 2.3.2
Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant v2.3.2
2.3.2 2.3.1 2.3.0 2.2.8 2.2.7 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.11.0 1.11.1 1.11.2 1.6 1.7 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.10 1.9.11 All 60 releases
merchant / inc / abilities / handlers / class-merchant-toggle-module-handler.php

class-merchant-toggle-module-handler.php in Product Labels, Quick View, Buy Now, Pre-Orders, Frequently Bought Together & More for WooCommerce – Merchant 2.3.2, at inc/abilities/handlers/class-merchant-toggle-module-handler.php

84 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Merchant Toggle Module Handler.
4 *
5 * Handles the merchant/toggle-module ability.
6 * Activates or deactivates a Merchant module.
7 *
8 * @package Merchant
9 * @since 2.3.0
10 */
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * Merchant_Toggle_Module_Handler
18 *
19 * Activates or deactivates a Merchant module, firing the
20 * same action hooks as the AJAX handler for consistency.
21 *
22 * @since 2.3.0
23 */
24 class Merchant_Toggle_Module_Handler extends Merchant_Abstract_Handler {
25
26 /**
27 * Handle the toggle-module request.
28 *
29 * @param array<string, mixed> $params { module_id: string, action: 'activate'|'deactivate' }
30 *
31 * @return array<string, mixed>|WP_Error Response envelope or WP_Error on failure.
32 */
33 public function handle( $params ) {
34 $module_id = isset( $params['module_id'] ) ? $params['module_id'] : '';
35 $action = isset( $params['action'] ) ? $params['action'] : '';
36
37 // Pre-flight: exists + pro gate (no active check — toggle changes activation).
38 $error = $this->preflight( $module_id );
39 if ( null !== $error ) {
40 return $error;
41 }
42
43 $active_modules = get_option( 'merchant-modules', array() );
44 $was_active = ! empty( $active_modules[ $module_id ] );
45
46 if ( 'activate' === $action && ! $was_active ) {
47 Merchant_Modules::set_module_active( $module_id );
48
49 return $this->success_response( $module_id, 'inactive', 'active' );
50 }
51
52 if ( 'deactivate' === $action && $was_active ) {
53 Merchant_Modules::set_module_inactive( $module_id );
54
55 return $this->success_response( $module_id, 'active', 'inactive' );
56 }
57
58 // Noop — already in the requested state.
59 $current = $was_active ? 'active' : 'inactive';
60
61 return $this->success_response( $module_id, $current, $current );
62 }
63
64 /**
65 * Build a success response.
66 *
67 * @param string $module_id Module identifier.
68 * @param string $previous_status Previous activation status.
69 * @param string $new_status New activation status.
70 *
71 * @return array<string, mixed> Response envelope.
72 */
73 private function success_response( $module_id, $previous_status, $new_status ) {
74 return array(
75 'success' => true,
76 'data' => array(
77 'module_id' => $module_id,
78 'previous_status' => $previous_status,
79 'new_status' => $new_status,
80 ),
81 );
82 }
83 }
84