| @@ 47-90 (lines=44) @@ | ||
| 44 | * |
|
| 45 | * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error. |
|
| 46 | */ |
|
| 47 | public function activate_module( $request ) { |
|
| 48 | $module_slug = ''; |
|
| 49 | ||
| 50 | if ( |
|
| 51 | ( |
|
| 52 | is_array( $request ) |
|
| 53 | || is_object( $request ) |
|
| 54 | ) |
|
| 55 | && isset( $request['slug'] ) |
|
| 56 | ) { |
|
| 57 | $module_slug = $request['slug']; |
|
| 58 | } else { |
|
| 59 | $module_slug = $request; |
|
| 60 | } |
|
| 61 | ||
| 62 | if ( ! Jetpack::is_module( $module_slug ) ) { |
|
| 63 | return new WP_Error( |
|
| 64 | 'not_found', |
|
| 65 | esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ), |
|
| 66 | array( 'status' => 404 ) |
|
| 67 | ); |
|
| 68 | } |
|
| 69 | ||
| 70 | if ( ! Jetpack::active_plan_supports( $module_slug ) ) { |
|
| 71 | return new WP_Error( |
|
| 72 | 'not_supported', |
|
| 73 | esc_html__( 'The requested Jetpack module is not supported by your plan.', 'jetpack' ), |
|
| 74 | array( 'status' => 424 ) |
|
| 75 | ); |
|
| 76 | } |
|
| 77 | ||
| 78 | if ( Jetpack::activate_module( $module_slug, false, false ) ) { |
|
| 79 | return rest_ensure_response( array( |
|
| 80 | 'code' => 'success', |
|
| 81 | 'message' => esc_html__( 'The requested Jetpack module was activated.', 'jetpack' ), |
|
| 82 | ) ); |
|
| 83 | } |
|
| 84 | ||
| 85 | return new WP_Error( |
|
| 86 | 'activation_failed', |
|
| 87 | esc_html__( 'The requested Jetpack module could not be activated.', 'jetpack' ), |
|
| 88 | array( 'status' => 424 ) |
|
| 89 | ); |
|
| 90 | } |
|
| 91 | ||
| 92 | /** |
|
| 93 | * If it's a valid Jetpack module, deactivate it. |
|
| @@ 107-149 (lines=43) @@ | ||
| 104 | * |
|
| 105 | * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error. |
|
| 106 | */ |
|
| 107 | public function deactivate_module( $request ) { |
|
| 108 | $module_slug = ''; |
|
| 109 | ||
| 110 | if ( |
|
| 111 | ( |
|
| 112 | is_array( $request ) |
|
| 113 | || is_object( $request ) |
|
| 114 | ) |
|
| 115 | && isset( $request['slug'] ) |
|
| 116 | ) { |
|
| 117 | $module_slug = $request['slug']; |
|
| 118 | } else { |
|
| 119 | $module_slug = $request; |
|
| 120 | } |
|
| 121 | ||
| 122 | if ( ! Jetpack::is_module( $module_slug ) ) { |
|
| 123 | return new WP_Error( |
|
| 124 | 'not_found', |
|
| 125 | esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ), |
|
| 126 | array( 'status' => 404 ) |
|
| 127 | ); |
|
| 128 | } |
|
| 129 | ||
| 130 | if ( ! Jetpack::is_module_active( $module_slug ) ) { |
|
| 131 | return new WP_Error( |
|
| 132 | 'already_inactive', |
|
| 133 | esc_html__( 'The requested Jetpack module was already inactive.', 'jetpack' ), |
|
| 134 | array( 'status' => 409 ) |
|
| 135 | ); |
|
| 136 | } |
|
| 137 | ||
| 138 | if ( Jetpack::deactivate_module( $module_slug ) ) { |
|
| 139 | return rest_ensure_response( array( |
|
| 140 | 'code' => 'success', |
|
| 141 | 'message' => esc_html__( 'The requested Jetpack module was deactivated.', 'jetpack' ), |
|
| 142 | ) ); |
|
| 143 | } |
|
| 144 | return new WP_Error( |
|
| 145 | 'deactivation_failed', |
|
| 146 | esc_html__( 'The requested Jetpack module could not be deactivated.', 'jetpack' ), |
|
| 147 | array( 'status' => 400 ) |
|
| 148 | ); |
|
| 149 | } |
|
| 150 | ||
| 151 | /** |
|
| 152 | * Check that the current user has permissions to manage Jetpack modules. |
|