Nessuna descrizione

plugins.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
  2. /**
  3. * Plugins Library
  4. *
  5. * Helper functions for installing and activating plugins.
  6. *
  7. * Used by the REST API
  8. *
  9. * @autounit api plugins
  10. */
  11. require_once 'class.jetpack-automatic-install-skin.php';
  12. /**
  13. * Plugins management tools.
  14. */
  15. class Jetpack_Plugins {
  16. /**
  17. * Install and activate a plugin.
  18. *
  19. * @since 5.8.0
  20. *
  21. * @param string $slug Plugin slug.
  22. *
  23. * @return bool|WP_Error True if installation succeeded, error object otherwise.
  24. */
  25. public static function install_and_activate_plugin( $slug ) {
  26. $plugin_id = self::get_plugin_id_by_slug( $slug );
  27. if ( ! $plugin_id ) {
  28. $installed = self::install_plugin( $slug );
  29. if ( is_wp_error( $installed ) ) {
  30. return $installed;
  31. }
  32. $plugin_id = self::get_plugin_id_by_slug( $slug );
  33. } elseif ( is_plugin_active( $plugin_id ) ) {
  34. return true; // Already installed and active.
  35. }
  36. if ( ! current_user_can( 'activate_plugins' ) ) {
  37. return new WP_Error( 'not_allowed', __( 'You are not allowed to activate plugins on this site.', 'jetpack' ) );
  38. }
  39. $activated = activate_plugin( $plugin_id );
  40. if ( is_wp_error( $activated ) ) {
  41. return $activated;
  42. }
  43. return true;
  44. }
  45. /**
  46. * Install a plugin.
  47. *
  48. * @since 5.8.0
  49. *
  50. * @param string $slug Plugin slug.
  51. *
  52. * @return bool|WP_Error True if installation succeeded, error object otherwise.
  53. */
  54. public static function install_plugin( $slug ) {
  55. if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
  56. return new WP_Error( 'not_allowed', __( 'You are not allowed to install plugins on this site.', 'jetpack' ) );
  57. }
  58. $skin = new Jetpack_Automatic_Install_Skin();
  59. $upgrader = new Plugin_Upgrader( $skin );
  60. $zip_url = self::generate_wordpress_org_plugin_download_link( $slug );
  61. $result = $upgrader->install( $zip_url );
  62. if ( is_wp_error( $result ) ) {
  63. return $result;
  64. }
  65. $plugin = self::get_plugin_id_by_slug( $slug );
  66. $error_code = 'install_error';
  67. if ( ! $plugin ) {
  68. $error = __( 'There was an error installing your plugin', 'jetpack' );
  69. }
  70. if ( ! $result ) {
  71. $error_code = $upgrader->skin->get_main_error_code();
  72. $message = $upgrader->skin->get_main_error_message();
  73. $error = $message ? $message : __( 'An unknown error occurred during installation', 'jetpack' );
  74. }
  75. if ( ! empty( $error ) ) {
  76. if ( 'download_failed' === $error_code ) {
  77. // For backwards compatibility: versions prior to 3.9 would return no_package instead of download_failed.
  78. $error_code = 'no_package';
  79. }
  80. return new WP_Error( $error_code, $error, 400 );
  81. }
  82. return (array) $upgrader->skin->get_upgrade_messages();
  83. }
  84. /**
  85. * Get WordPress.org zip download link from a plugin slug
  86. *
  87. * @param string $plugin_slug Plugin slug.
  88. */
  89. protected static function generate_wordpress_org_plugin_download_link( $plugin_slug ) {
  90. return "https://downloads.wordpress.org/plugin/$plugin_slug.latest-stable.zip";
  91. }
  92. /**
  93. * Get the plugin ID (composed of the plugin slug and the name of the main plugin file) from a plugin slug.
  94. *
  95. * @param string $slug Plugin slug.
  96. */
  97. public static function get_plugin_id_by_slug( $slug ) {
  98. // Check if get_plugins() function exists. This is required on the front end of the
  99. // site, since it is in a file that is normally only loaded in the admin.
  100. if ( ! function_exists( 'get_plugins' ) ) {
  101. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  102. }
  103. /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
  104. $plugins = apply_filters( 'all_plugins', get_plugins() );
  105. if ( ! is_array( $plugins ) ) {
  106. return false;
  107. }
  108. foreach ( $plugins as $plugin_file => $plugin_data ) {
  109. if ( self::get_slug_from_file_path( $plugin_file ) === $slug ) {
  110. return $plugin_file;
  111. }
  112. }
  113. return false;
  114. }
  115. /**
  116. * Get the plugin slug from the plugin ID (composed of the plugin slug and the name of the main plugin file)
  117. *
  118. * @param string $plugin_file Plugin file (ID -- e.g. hello-dolly/hello.php).
  119. */
  120. protected static function get_slug_from_file_path( $plugin_file ) {
  121. // Similar to get_plugin_slug() method.
  122. $slug = dirname( $plugin_file );
  123. if ( '.' === $slug ) {
  124. $slug = preg_replace( '/(.+)\.php$/', '$1', $plugin_file );
  125. }
  126. return $slug;
  127. }
  128. /**
  129. * Get the activation status for a plugin.
  130. *
  131. * @since 8.9.0
  132. *
  133. * @param string $plugin_file The plugin file to check.
  134. * @return string Either 'network-active', 'active' or 'inactive'.
  135. */
  136. public static function get_plugin_status( $plugin_file ) {
  137. if ( is_plugin_active_for_network( $plugin_file ) ) {
  138. return 'network-active';
  139. }
  140. if ( is_plugin_active( $plugin_file ) ) {
  141. return 'active';
  142. }
  143. return 'inactive';
  144. }
  145. /**
  146. * Returns a list of all plugins in the site.
  147. *
  148. * @since 8.9.0
  149. * @uses get_plugins()
  150. *
  151. * @return array
  152. */
  153. public static function get_plugins() {
  154. if ( ! function_exists( 'get_plugins' ) ) {
  155. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  156. }
  157. /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
  158. $plugins = apply_filters( 'all_plugins', get_plugins() );
  159. if ( is_array( $plugins ) && ! empty( $plugins ) ) {
  160. foreach ( $plugins as $plugin_slug => $plugin_data ) {
  161. $plugins[ $plugin_slug ]['active'] = in_array(
  162. self::get_plugin_status( $plugin_slug ),
  163. array( 'active', 'network-active' ),
  164. true
  165. );
  166. }
  167. return $plugins;
  168. }
  169. return array();
  170. }
  171. }