Jetpack_Core_API_Module_Toggle_Endpoint

This is the base class for every Core API endpoint Jetpack uses.

Defined (1)

The class is defined in the following location(s).

/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php  
  1. class Jetpack_Core_API_Module_Toggle_Endpoint 
  2. extends Jetpack_Core_API_XMLRPC_Consumer_Endpoint { 
  3.  
  4. /** 
  5. * Check if the module requires the site to be publicly accessible from WPCOM. 
  6. * If the site meets this requirement, the module is activated. Otherwise an error is returned. 
  7. * @since 4.3.0 
  8. * @param WP_REST_Request $request { 
  9. * Array of parameters received by request. 
  10. * @type string $slug Module slug. 
  11. * @type bool $active should module be activated. 
  12. * } 
  13. * @return WP_REST_Response|WP_Error A REST response if the request was served successfully, otherwise an error. 
  14. */ 
  15. public function process( $request ) { 
  16. if ( $request['active'] ) { 
  17. return $this->activate_module( $request ); 
  18. } else { 
  19. return $this->deactivate_module( $request ); 
  20.  
  21. /** 
  22. * If it's a valid Jetpack module, activate it. 
  23. * @since 4.3.0 
  24. * @param string|WP_REST_Request $request It's a WP_REST_Request when called from endpoint /module/<slug>/** 
  25. * and a string when called from Jetpack_Core_API_Data->update_data. 
  26. * { 
  27. * Array of parameters received by request. 
  28. * @type string $slug Module slug. 
  29. * } 
  30. * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error. 
  31. */ 
  32. public function activate_module( $request ) { 
  33. $module_slug = ''; 
  34.  
  35. if ( 
  36. is_array( $request ) 
  37. || is_object( $request ) 
  38. && isset( $request['slug'] ) 
  39. ) { 
  40. $module_slug = $request['slug']; 
  41. } else { 
  42. $module_slug = $request; 
  43.  
  44. if ( ! Jetpack::is_module( $module_slug ) ) { 
  45. return new WP_Error( 
  46. 'not_found',  
  47. esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ),  
  48. array( 'status' => 404 ) 
  49. ); 
  50.  
  51. if ( Jetpack::activate_module( $module_slug, false, false ) ) { 
  52. return rest_ensure_response( array( 
  53. 'code' => 'success',  
  54. 'message' => esc_html__( 'The requested Jetpack module was activated.', 'jetpack' ),  
  55. ) ); 
  56.  
  57. return new WP_Error( 
  58. 'activation_failed',  
  59. esc_html__( 'The requested Jetpack module could not be activated.', 'jetpack' ),  
  60. array( 'status' => 424 ) 
  61. ); 
  62.  
  63. /** 
  64. * If it's a valid Jetpack module, deactivate it. 
  65. * @since 4.3.0 
  66. * @param string|WP_REST_Request $request It's a WP_REST_Request when called from endpoint /module/<slug>/** 
  67. * and a string when called from Jetpack_Core_API_Data->update_data. 
  68. * { 
  69. * Array of parameters received by request. 
  70. * @type string $slug Module slug. 
  71. * } 
  72. * @return bool|WP_Error True if module was activated. Otherwise, a WP_Error instance with the corresponding error. 
  73. */ 
  74. public function deactivate_module( $request ) { 
  75. $module_slug = ''; 
  76.  
  77. if ( 
  78. is_array( $request ) 
  79. || is_object( $request ) 
  80. && isset( $request['slug'] ) 
  81. ) { 
  82. $module_slug = $request['slug']; 
  83. } else { 
  84. $module_slug = $request; 
  85.  
  86. if ( ! Jetpack::is_module( $module_slug ) ) { 
  87. return new WP_Error( 
  88. 'not_found',  
  89. esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ),  
  90. array( 'status' => 404 ) 
  91. ); 
  92.  
  93. if ( ! Jetpack::is_module_active( $module_slug ) ) { 
  94. return new WP_Error( 
  95. 'already_inactive',  
  96. esc_html__( 'The requested Jetpack module was already inactive.', 'jetpack' ),  
  97. array( 'status' => 409 ) 
  98. ); 
  99.  
  100. if ( Jetpack::deactivate_module( $module_slug ) ) { 
  101. return rest_ensure_response( array( 
  102. 'code' => 'success',  
  103. 'message' => esc_html__( 'The requested Jetpack module was deactivated.', 'jetpack' ),  
  104. ) ); 
  105. return new WP_Error( 
  106. 'deactivation_failed',  
  107. esc_html__( 'The requested Jetpack module could not be deactivated.', 'jetpack' ),  
  108. array( 'status' => 400 ) 
  109. ); 
  110.  
  111. /** 
  112. * Check that the current user has permissions to manage Jetpack modules. 
  113. * @since 4.3.0 
  114. * @return bool 
  115. */ 
  116. public function can_request() { 
  117. return current_user_can( 'jetpack_manage_modules' );