Jetpack_Core_API_Module_List_Endpoint

The WordPress Core Jetpack Core API Module List Endpoint class.

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_List_Endpoint { 
  2.  
  3. /** 
  4. * A WordPress REST API callback method that accepts a request object and decides what to do with it. 
  5. * @param WP_REST_Request $request The request sent to the WP REST API. 
  6. * @since 4.3.0 
  7. * @return bool|Array|WP_Error a resulting value or object, or an error. 
  8. */ 
  9. public function process( $request ) { 
  10. if ( 'GET' === $request->get_method() ) { 
  11. return $this->get_modules( $request ); 
  12. } else { 
  13. return $this->activate_modules( $request ); 
  14.  
  15. /** 
  16. * Get a list of all Jetpack modules and their information. 
  17. * @since 4.3.0 
  18. * @return array Array of Jetpack modules. 
  19. */ 
  20. public function get_modules() { 
  21. require_once( JETPACK__PLUGIN_DIR . 'class.jetpack-admin.php' ); 
  22.  
  23. $modules = Jetpack_Admin::init()->get_modules(); 
  24. foreach ( $modules as $slug => $properties ) { 
  25. $modules[ $slug ]['options'] = 
  26. Jetpack_Core_Json_Api_Endpoints::prepare_options_for_response( $slug ); 
  27. if ( 
  28. isset( $modules[ $slug ]['requires_connection'] ) 
  29. && $modules[ $slug ]['requires_connection'] 
  30. && Jetpack::is_development_mode() 
  31. ) { 
  32. $modules[ $slug ]['activated'] = false; 
  33.  
  34. $modules = Jetpack::get_translated_modules( $modules ); 
  35.  
  36. return Jetpack_Core_Json_Api_Endpoints::prepare_modules_for_response( $modules ); 
  37.  
  38. /** 
  39. * Activate a list of valid Jetpack modules. 
  40. * @since 4.3.0 
  41. * @param WP_REST_Request $request { 
  42. * Array of parameters received by request. 
  43. * @type string $slug Module slug. 
  44. * } 
  45. * @return bool|WP_Error True if modules were activated. Otherwise, a WP_Error instance with the corresponding error. 
  46. */ 
  47. public static function activate_modules( $request ) { 
  48.  
  49. if ( 
  50. ! isset( $request['modules'] ) 
  51. || ! is_array( $request['modules'] ) 
  52. ) { 
  53. return new WP_Error( 
  54. 'not_found',  
  55. esc_html__( 'The requested Jetpack module was not found.', 'jetpack' ),  
  56. array( 'status' => 404 ) 
  57. ); 
  58.  
  59. $activated = array(); 
  60. $failed = array(); 
  61.  
  62. foreach ( $request['modules'] as $module ) { 
  63. if ( Jetpack::activate_module( $module, false, false ) ) { 
  64. $activated[] = $module; 
  65. } else { 
  66. $failed[] = $module; 
  67.  
  68. if ( empty( $failed ) ) { 
  69. return rest_ensure_response( array( 
  70. 'code' => 'success',  
  71. 'message' => esc_html__( 'All modules activated.', 'jetpack' ),  
  72. ) ); 
  73.  
  74. $error = ''; 
  75.  
  76. $activated_count = count( $activated ); 
  77. if ( $activated_count > 0 ) { 
  78. $activated_last = array_pop( $activated ); 
  79. $activated_text = $activated_count > 1 ? sprintf( 
  80. /** Translators: first variable is a list followed by the last item, which is the second variable. Example: dog, cat and bird. */ 
  81. __( '%1$s and %2$s', 'jetpack' ),  
  82. join( ', ', $activated ), $activated_last ) : $activated_last; 
  83.  
  84. $error = sprintf( 
  85. /** Translators: the variable is a module name. */ 
  86. _n( 'The module %s was activated.', 'The modules %s were activated.', $activated_count, 'jetpack' ),  
  87. $activated_text ) . ' '; 
  88.  
  89. $failed_count = count( $failed ); 
  90. if ( count( $failed ) > 0 ) { 
  91. $failed_last = array_pop( $failed ); 
  92. $failed_text = $failed_count > 1 ? sprintf( 
  93. /** Translators: first variable is a list followed by the last item, which is the second variable. Example: dog, cat and bird. */ 
  94. __( '%1$s and %2$s', 'jetpack' ),  
  95. join( ', ', $failed ), $failed_last ) : $failed_last; 
  96.  
  97. $error = sprintf( 
  98. /** Translators: the variable is a module name. */ 
  99. _n( 'The module %s failed to be activated.', 'The modules %s failed to be activated.', $failed_count, 'jetpack' ),  
  100. $failed_text ) . ' '; 
  101.  
  102. return new WP_Error( 
  103. 'activation_failed',  
  104. esc_html( $error ),  
  105. array( 'status' => 424 ) 
  106. ); 
  107.  
  108. /** 
  109. * A WordPress REST API permission callback method that accepts a request object and decides 
  110. * if the current user has enough privileges to act. 
  111. * @since 4.3.0 
  112. * @param WP_REST_Request $request The request sent to the WP REST API. 
  113. * @return bool does the current user have enough privilege. 
  114. */ 
  115. public function can_request( $request ) { 
  116. if ( 'GET' === $request->get_method() ) { 
  117. return current_user_can( 'jetpack_admin_page' ); 
  118. } else { 
  119. return current_user_can( 'jetpack_manage_modules' );