/wp-content/plugins/jetpack/json-endpoints/jetpack/class.jetpack-json-api-plugins-endpoint.php

https://gitlab.com/Gashler/sg · PHP · 197 lines · 154 code · 33 blank · 10 comment · 35 complexity · ac48c50cb45ada7611fa4f4e6b89a046 MD5 · raw file

  1. <?php
  2. /**
  3. * Base class for working with plugins.
  4. */
  5. abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoint {
  6. protected $plugins = array();
  7. protected $network_wide = false;
  8. protected $bulk = true;
  9. protected $log;
  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. );
  26. protected function result() {
  27. $plugins = $this->get_plugins();
  28. if ( ! $this->bulk && ! empty( $plugins ) ) {
  29. return array_pop( $plugins );
  30. }
  31. return array( 'plugins' => $plugins );
  32. }
  33. protected function validate_input( $plugin ) {
  34. if ( is_wp_error( $error = parent::validate_input( $plugin ) ) ) {
  35. return $error;
  36. }
  37. if ( is_wp_error( $error = $this->validate_network_wide() ) ) {
  38. return $error;
  39. }
  40. $args = $this->input();
  41. // find out what plugin, or plugins we are dealing with
  42. // validate the requested plugins
  43. if ( ! isset( $plugin ) || empty( $plugin ) ) {
  44. if ( ! $args['plugins'] || empty( $args['plugins'] ) ) {
  45. return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin.', 'jetpack' ), 400 );
  46. }
  47. if ( is_array( $args['plugins'] ) ) {
  48. $this->plugins = $args['plugins'];
  49. } else {
  50. $this->plugins[] = $args['plugins'];
  51. }
  52. } else {
  53. $this->bulk = false;
  54. $this->plugins[] = urldecode( $plugin );
  55. }
  56. if ( is_wp_error( $error = $this->validate_plugins() ) ) {
  57. return $error;
  58. };
  59. return true;
  60. }
  61. /**
  62. * Walks through submitted plugins to make sure they are valid
  63. * @return bool|WP_Error
  64. */
  65. protected function validate_plugins() {
  66. if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
  67. return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' ));
  68. }
  69. foreach( $this->plugins as $index => $plugin ) {
  70. if ( ! preg_match( "/\.php$/", $plugin ) ) {
  71. $plugin = $plugin . '.php';
  72. $this->plugins[ $index ] = $plugin;
  73. }
  74. if ( is_wp_error( $error = $this->validate_plugin( $plugin ) ) ) {
  75. return $error;
  76. }
  77. }
  78. return true;
  79. }
  80. protected function format_plugin( $plugin_file, $plugin_data ) {
  81. $plugin = array();
  82. $plugin['id'] = preg_replace("/(.+)\.php$/", "$1", $plugin_file );
  83. $plugin['slug'] = $this->get_plugin_slug( $plugin_file );
  84. $plugin['active'] = Jetpack::is_plugin_active( $plugin_file );
  85. $plugin['name'] = $plugin_data['Name'];
  86. $plugin['plugin_url'] = $plugin_data['PluginURI'];
  87. $plugin['version'] = $plugin_data['Version'];
  88. $plugin['description'] = $plugin_data['Description'];
  89. $plugin['author'] = $plugin_data['Author'];
  90. $plugin['author_url'] = $plugin_data['AuthorURI'];
  91. $plugin['network'] = $plugin_data['Network'];
  92. $plugin['update'] = $this->get_plugin_updates( $plugin_file );
  93. $plugin['next_autoupdate'] = date( 'Y-m-d H:i:s', wp_next_scheduled( 'wp_maybe_auto_update' ) );
  94. $plugin['autoupdate'] = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins', array() ) );
  95. if ( ! empty ( $this->log[ $plugin_file ] ) ) {
  96. $plugin['log'] = $this->log[ $plugin_file ];
  97. }
  98. return $plugin;
  99. }
  100. protected function get_plugins() {
  101. $plugins = array();
  102. $installed_plugins = get_plugins();
  103. foreach( $this->plugins as $plugin ) {
  104. if ( ! isset( $installed_plugins[ $plugin ] ) )
  105. continue;
  106. $plugins[] = $this->format_plugin( $plugin, $installed_plugins[ $plugin ] );
  107. }
  108. $args = $this->query_args();
  109. if ( isset( $args['offset'] ) ) {
  110. $plugins = array_slice( $plugins, (int) $args['offset'] );
  111. }
  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. if ( isset( $args['network_wide'] ) && $args['network_wide'] ) {
  120. $this->network_wide = true;
  121. }
  122. if ( $this->network_wide && ! current_user_can( 'manage_network_plugins' ) ) {
  123. return new WP_Error( 'unauthorized', __( 'This user is not authorized to manage plugins network wide.', 'jetpack' ), 403 );
  124. }
  125. return true;
  126. }
  127. protected function validate_plugin( $plugin ) {
  128. if ( ! isset( $plugin) || empty( $plugin ) ) {
  129. return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin to activate.', 'jetpack' ), 400 );
  130. }
  131. if ( is_wp_error( $error = validate_plugin( urldecode( $plugin ) ) ) ) {
  132. return new WP_Error( 'unknown_plugin', $error->get_error_messages() , 404 );
  133. }
  134. return true;
  135. }
  136. protected function get_plugin_updates( $plugin_file ) {
  137. $plugin_updates = get_plugin_updates();
  138. if ( isset( $plugin_updates[ $plugin_file ] ) ){
  139. return $plugin_updates[ $plugin_file ]->update;
  140. }
  141. return null;
  142. }
  143. protected function get_plugin_slug( $plugin_file ) {
  144. $update_plugins = get_site_transient( 'update_plugins' );
  145. if ( isset( $update_plugins->no_update ) ) {
  146. if ( isset( $update_plugins->no_update[ $plugin_file ] ) ) {
  147. $slug = $update_plugins->no_update[ $plugin_file ]->slug;
  148. }
  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. }
  155. // Try to infer from the plugin file if not cached
  156. if ( empty( $slug) ) {
  157. $slug = dirname( $plugin_file );
  158. if ( '.' === $slug ) {
  159. $slug = preg_replace("/(.+)\.php$/", "$1", $plugin_file );
  160. }
  161. }
  162. return $slug;
  163. }
  164. }