| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
|
| 3 |
/** |
| 4 |
* Modules modify endpoint class. |
| 5 |
* |
| 6 |
* POST /sites/%s/jetpack/modules/%s/activate |
| 7 |
* POST /sites/%s/jetpack/modules/%s |
| 8 |
* POST /sites/%s/jetpack/modules |
| 9 |
*/ |
| 10 |
class Jetpack_JSON_API_Modules_Modify_Endpoint extends Jetpack_JSON_API_Modules_Endpoint { |
| 11 |
/** |
| 12 |
* Needed capabilities. |
| 13 |
* |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
protected $needed_capabilities = 'activate_plugins'; |
| 17 |
|
| 18 |
/** |
| 19 |
* The action. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
protected $action = 'default_action'; |
| 24 |
|
| 25 |
/** |
| 26 |
* The default action. |
| 27 |
*/ |
| 28 |
public function default_action() { |
| 29 |
$args = $this->input(); |
| 30 |
if ( isset( $args['active'] ) && is_bool( $args['active'] ) ) { |
| 31 |
if ( $args['active'] ) { |
| 32 |
return $this->activate_module(); |
| 33 |
} else { |
| 34 |
return $this->deactivate_module(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
return true; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Activate module. |
| 43 |
* |
| 44 |
* @return bool|WP_Error |
| 45 |
*/ |
| 46 |
protected function activate_module() { |
| 47 |
foreach ( $this->modules as $module ) { |
| 48 |
if ( Jetpack::is_module_active( $module ) ) { |
| 49 |
$error = __( 'The Jetpack Module is already activated.', 'jetpack' ); |
| 50 |
$this->log[ $module ][] = $error; |
| 51 |
continue; |
| 52 |
} |
| 53 |
$result = Jetpack::activate_module( $module, false, false ); |
| 54 |
if ( false === $result || ! Jetpack::is_module_active( $module ) ) { |
| 55 |
$error = __( 'There was an error while activating the module.', 'jetpack' ); |
| 56 |
$this->log[ $module ][] = $error; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! $this->bulk && isset( $error ) ) { |
| 61 |
return new WP_Error( 'activation_error', $error, 400 ); |
| 62 |
} |
| 63 |
|
| 64 |
return true; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Deactivate module. |
| 69 |
* |
| 70 |
* @return bool|WP_Error |
| 71 |
*/ |
| 72 |
protected function deactivate_module() { |
| 73 |
foreach ( $this->modules as $module ) { |
| 74 |
if ( ! Jetpack::is_module_active( $module ) ) { |
| 75 |
$error = __( 'The Jetpack Module is already deactivated.', 'jetpack' ); |
| 76 |
$this->log[ $module ] = $error; |
| 77 |
continue; |
| 78 |
} |
| 79 |
$result = Jetpack::deactivate_module( $module ); |
| 80 |
if ( false === $result || Jetpack::is_module_active( $module ) ) { |
| 81 |
$error = __( 'There was an error while deactivating the module.', 'jetpack' ); |
| 82 |
$this->log[ $module ] = $error; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
if ( ! $this->bulk && isset( $error ) ) { |
| 87 |
return new WP_Error( 'deactivation_error', $error, 400 ); |
| 88 |
} |
| 89 |
|
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|