Jetpack_JSON_API_Modules_Endpoint

Base class for working with Jetpack Modules.

Defined (1)

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

/json-endpoints/jetpack/class.jetpack-json-api-modules-endpoint.php  
  1. abstract class Jetpack_JSON_API_Modules_Endpoint extends Jetpack_JSON_API_Endpoint { 
  2.  
  3. protected $modules = array(); 
  4.  
  5. protected $bulk = true; 
  6.  
  7. static $_response_format = array( 
  8. 'id' => '(string) The module\'s ID',  
  9. 'active' => '(boolean) The module\'s status.',  
  10. 'name' => '(string) The module\'s name.',  
  11. 'description' => '(safehtml) The module\'s description.',  
  12. 'sort' => '(int) The module\'s display order.',  
  13. 'introduced' => '(string) The Jetpack version when the module was introduced.',  
  14. 'changed' => '(string) The Jetpack version when the module was changed.',  
  15. 'free' => '(boolean) The module\'s Free or Paid status.',  
  16. 'module_tags' => '(array) The module\'s tags.' 
  17. ); 
  18.  
  19. protected function result() { 
  20.  
  21. $modules = $this->get_modules(); 
  22.  
  23. if ( ! $this->bulk && ! empty( $modules ) ) { 
  24. return array_pop( $modules ); 
  25.  
  26. return array( 'modules' => $modules ); 
  27.  
  28.  
  29. /** 
  30. * Walks through either the submitted modules or list of themes and creates the global array 
  31. * @param $theme 
  32. * @return bool 
  33. */ 
  34. protected function validate_input( $module) { 
  35. $args = $this->input(); 
  36. // lets set what modules were requested, and validate them 
  37. if ( ! isset( $module ) || empty( $module ) ) { 
  38.  
  39. if ( ! $args['modules'] || empty( $args['modules'] ) ) { 
  40. return new WP_Error( 'missing_module', __( 'You are required to specify a module.', 'jetpack' ), 400 ); 
  41. if ( is_array( $args['modules'] ) ) { 
  42. $this->modules = $args['modules']; 
  43. } else { 
  44. $this->modules[] = $args['modules']; 
  45. } else { 
  46. $this->modules[] = urldecode( $module ); 
  47. $this->bulk = false; 
  48.  
  49. if ( is_wp_error( $error = $this->validate_modules() ) ) { 
  50. return $error; 
  51.  
  52. return parent::validate_input( $module ); 
  53.  
  54. /** 
  55. * Walks through submitted themes to make sure they are valid 
  56. * @return bool|WP_Error 
  57. */ 
  58. protected function validate_modules() { 
  59. foreach ( $this->modules as $module ) { 
  60. if ( ! Jetpack::is_module( $module ) ) { 
  61. return new WP_Error( 'unknown_jetpack_module', sprintf( __( 'Module not found: `%s`.', 'jetpack' ), $module ), 404 ); 
  62. return true; 
  63.  
  64. protected static function format_module( $module_slug ) { 
  65. $module_data = Jetpack::get_module( $module_slug ); 
  66.  
  67. $module = array(); 
  68. $module['id'] = $module_slug; 
  69. $module['active'] = Jetpack::is_module_active( $module_slug ); 
  70. $module['name'] = $module_data['name']; 
  71. $module['short_description'] = $module_data['description']; 
  72. $module['sort'] = $module_data['sort']; 
  73. $module['introduced'] = $module_data['introduced']; 
  74. $module['changed'] = $module_data['changed']; 
  75. $module['free'] = $module_data['free']; 
  76. $module['module_tags'] = $module_data['module_tags']; 
  77.  
  78. // Fetch the HTML formatted long description 
  79. ob_start(); 
  80. if ( Jetpack::is_active() && has_action( 'jetpack_module_more_info_connected_' . $module_slug ) ) { 
  81. /** This action is documented in class.jetpack-modules-list-table.php */ 
  82. do_action( 'jetpack_module_more_info_connected_' . $module_slug ); 
  83. } else { 
  84. /** This action is documented in class.jetpack-modules-list-table.php */ 
  85. do_action( 'jetpack_module_more_info_' . $module_slug ); 
  86. $module['description'] = ob_get_clean(); 
  87.  
  88. return $module; 
  89.  
  90. /** 
  91. * Format a list of modules for public display, using the supplied offset and limit args 
  92. * @uses WPCOM_JSON_API_Endpoint::query_args() 
  93. * @return array Public API modules objects 
  94. */ 
  95. protected function get_modules() { 
  96. $modules = array_values( $this->modules ); 
  97. // do offset & limit - we've already returned a 400 error if they're bad numbers 
  98. $args = $this->query_args(); 
  99.  
  100. if ( isset( $args['offset'] ) ) 
  101. $modules = array_slice( $modules, (int) $args['offset'] ); 
  102. if ( isset( $args['limit'] ) ) 
  103. $modules = array_slice( $modules, 0, (int) $args['limit'] ); 
  104.  
  105. return array_map( array( $this, 'format_module' ), $modules ); 
  106.