Jetpack_JSON_API_Plugins_Endpoint

Base class for working with plugins.

Defined (1)

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

/json-endpoints/jetpack/class.jetpack-json-api-plugins-endpoint.php  
  1. abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoint { 
  2.  
  3. protected $plugins = array(); 
  4.  
  5. protected $network_wide = false; 
  6.  
  7. protected $bulk = true; 
  8. protected $log; 
  9.  
  10. static $_response_format = array( 
  11. 'id' => '(safehtml) The plugin\'s ID',  
  12. 'slug' => '(safehtml) The plugin\'s .org slug',  
  13. 'active' => '(boolean) The plugin status.',  
  14. 'update' => '(object) The plugin update info.',  
  15. 'name' => '(safehtml) The name of the plugin.',  
  16. 'plugin_url' => '(url) Link to the plugin\'s web site.',  
  17. 'version' => '(safehtml) The plugin version number.',  
  18. 'description' => '(safehtml) Description of what the plugin does and/or notes from the author',  
  19. 'author' => '(safehtml) The author\'s name',  
  20. 'author_url' => '(url) The authors web site address',  
  21. 'network' => '(boolean) Whether the plugin can only be activated network wide.',  
  22. 'autoupdate' => '(boolean) Whether the plugin is automatically updated',  
  23. 'next_autoupdate' => '(string) Y-m-d H:i:s for next scheduled update event',  
  24. 'log' => '(array:safehtml) An array of update log strings.',  
  25. 'uninstallable' => '(boolean) Whether the plugin is unistallable.',  
  26. ); 
  27.  
  28. protected function result() { 
  29.  
  30. $plugins = $this->get_plugins(); 
  31.  
  32. if ( ! $this->bulk && ! empty( $plugins ) ) { 
  33. return array_pop( $plugins ); 
  34.  
  35. return array( 'plugins' => $plugins ); 
  36.  
  37.  
  38. protected function validate_input( $plugin ) { 
  39.  
  40. if ( is_wp_error( $error = parent::validate_input( $plugin ) ) ) { 
  41. return $error; 
  42.  
  43. if ( is_wp_error( $error = $this->validate_network_wide() ) ) { 
  44. return $error; 
  45.  
  46. $args = $this->input(); 
  47. // find out what plugin, or plugins we are dealing with 
  48. // validate the requested plugins 
  49. if ( ! isset( $plugin ) || empty( $plugin ) ) { 
  50. if ( ! $args['plugins'] || empty( $args['plugins'] ) ) { 
  51. return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin.', 'jetpack' ), 400 ); 
  52. if ( is_array( $args['plugins'] ) ) { 
  53. $this->plugins = $args['plugins']; 
  54. } else { 
  55. $this->plugins[] = $args['plugins']; 
  56. } else { 
  57. $this->bulk = false; 
  58. $this->plugins[] = urldecode( $plugin ); 
  59.  
  60. if ( is_wp_error( $error = $this->validate_plugins() ) ) { 
  61. return $error; 
  62. }; 
  63.  
  64. return true; 
  65.  
  66. /** 
  67. * Walks through submitted plugins to make sure they are valid 
  68. * @return bool|WP_Error 
  69. */ 
  70. protected function validate_plugins() { 
  71. if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { 
  72. return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' )); 
  73. foreach( $this->plugins as $index => $plugin ) { 
  74. if ( ! preg_match( "/\.php$/", $plugin ) ) { 
  75. $plugin = $plugin . '.php'; 
  76. $this->plugins[ $index ] = $plugin; 
  77. if ( is_wp_error( $error = $this->validate_plugin( $plugin ) ) ) { 
  78. return $error; 
  79. return true; 
  80.  
  81. protected function format_plugin( $plugin_file, $plugin_data ) { 
  82. $plugin = array(); 
  83. $plugin['id'] = preg_replace("/(.+)\.php$/", "$1", $plugin_file ); 
  84. $plugin['slug'] = $this->get_plugin_slug( $plugin_file ); 
  85. $plugin['active'] = Jetpack::is_plugin_active( $plugin_file ); 
  86. $plugin['name'] = $plugin_data['Name']; 
  87. $plugin['plugin_url'] = $plugin_data['PluginURI']; 
  88. $plugin['version'] = $plugin_data['Version']; 
  89. $plugin['description'] = $plugin_data['Description']; 
  90. $plugin['author'] = $plugin_data['Author']; 
  91. $plugin['author_url'] = $plugin_data['AuthorURI']; 
  92. $plugin['network'] = $plugin_data['Network']; 
  93. $plugin['update'] = $this->get_plugin_updates( $plugin_file ); 
  94. $plugin['next_autoupdate'] = date( 'Y-m-d H:i:s', wp_next_scheduled( 'wp_maybe_auto_update' ) ); 
  95. $plugin['autoupdate'] = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins', array() ) ); 
  96. $plugin['uninstallable'] = is_uninstallable_plugin( $plugin_file ); 
  97. if ( ! empty ( $this->log[ $plugin_file ] ) ) { 
  98. $plugin['log'] = $this->log[ $plugin_file ]; 
  99. return $plugin; 
  100.  
  101. protected function get_plugins() { 
  102. $plugins = array(); 
  103. $installed_plugins = get_plugins(); 
  104. foreach( $this->plugins as $plugin ) { 
  105. if ( ! isset( $installed_plugins[ $plugin ] ) ) 
  106. continue; 
  107. $plugins[] = $this->format_plugin( $plugin, $installed_plugins[ $plugin ] ); 
  108. $args = $this->query_args(); 
  109.  
  110. if ( isset( $args['offset'] ) ) { 
  111. $plugins = array_slice( $plugins, (int) $args['offset'] ); 
  112. if ( isset( $args['limit'] ) ) { 
  113. $plugins = array_slice( $plugins, 0, (int) $args['limit'] ); 
  114.  
  115. return $plugins; 
  116.  
  117. protected function validate_network_wide() { 
  118. $args = $this->input(); 
  119.  
  120. if ( isset( $args['network_wide'] ) && $args['network_wide'] ) { 
  121. $this->network_wide = true; 
  122.  
  123. if ( $this->network_wide && ! current_user_can( 'manage_network_plugins' ) ) { 
  124. return new WP_Error( 'unauthorized', __( 'This user is not authorized to manage plugins network wide.', 'jetpack' ), 403 ); 
  125.  
  126. return true; 
  127.  
  128.  
  129. protected function validate_plugin( $plugin ) { 
  130. if ( ! isset( $plugin) || empty( $plugin ) ) { 
  131. return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin to activate.', 'jetpack' ), 400 ); 
  132.  
  133. if ( is_wp_error( $error = validate_plugin( urldecode( $plugin ) ) ) ) { 
  134. return new WP_Error( 'unknown_plugin', $error->get_error_messages() , 404 ); 
  135.  
  136. return true; 
  137.  
  138. protected function get_plugin_updates( $plugin_file ) { 
  139. $plugin_updates = get_plugin_updates(); 
  140. if ( isset( $plugin_updates[ $plugin_file ] ) ) { 
  141. return $plugin_updates[ $plugin_file ]->update; 
  142. return null; 
  143.  
  144. protected function get_plugin_slug( $plugin_file ) { 
  145. $update_plugins = get_site_transient( 'update_plugins' ); 
  146. if ( isset( $update_plugins->no_update ) ) { 
  147. if ( isset( $update_plugins->no_update[ $plugin_file ] ) ) { 
  148. $slug = $update_plugins->no_update[ $plugin_file ]->slug; 
  149.  
  150. if ( empty( $slug ) && isset( $update_plugins->response ) ) { 
  151. if ( isset( $update_plugins->response[ $plugin_file ] ) ) { 
  152. $slug = $update_plugins->response[ $plugin_file ]->slug; 
  153.  
  154. // Try to infer from the plugin file if not cached 
  155. if ( empty( $slug) ) { 
  156. $slug = dirname( $plugin_file ); 
  157. if ( '.' === $slug ) { 
  158. $slug = preg_replace("/(.+)\.php$/", "$1", $plugin_file ); 
  159. return $slug;