plugins.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  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. include_once( 'class.jetpack-automatic-install-skin.php' );
  12. class Jetpack_Plugins {
  13. /**
  14. * Install and activate a plugin.
  15. *
  16. * @since 5.8.0
  17. *
  18. * @param string $slug Plugin slug.
  19. *
  20. * @return bool|WP_Error True if installation succeeded, error object otherwise.
  21. */
  22. public static function install_and_activate_plugin( $slug ) {
  23. $plugin_id = self::get_plugin_id_by_slug( $slug );
  24. if ( ! $plugin_id ) {
  25. $installed = self::install_plugin( $slug );
  26. if ( is_wp_error( $installed ) ) {
  27. return $installed;
  28. }
  29. $plugin_id = self::get_plugin_id_by_slug( $slug );
  30. } else if ( is_plugin_active( $plugin_id ) ) {
  31. return true; // Already installed and active
  32. }
  33. if ( ! current_user_can( 'activate_plugins' ) ) {
  34. return new WP_Error( 'not_allowed', __( 'You are not allowed to activate plugins on this site.', 'jetpack' ) );
  35. }
  36. $activated = activate_plugin( $plugin_id );
  37. if ( is_wp_error( $activated ) ) {
  38. return $activated;
  39. }
  40. return true;
  41. }
  42. /**
  43. * Install a plugin.
  44. *
  45. * @since 5.8.0
  46. *
  47. * @param string $slug Plugin slug.
  48. *
  49. * @return bool|WP_Error True if installation succeeded, error object otherwise.
  50. */
  51. public static function install_plugin( $slug ) {
  52. if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
  53. return new WP_Error( 'not_allowed', __( 'You are not allowed to install plugins on this site.', 'jetpack' ) );
  54. }
  55. $skin = new Jetpack_Automatic_Install_Skin();
  56. $upgrader = new Plugin_Upgrader( $skin );
  57. $zip_url = self::generate_wordpress_org_plugin_download_link( $slug );
  58. $result = $upgrader->install( $zip_url );
  59. if ( is_wp_error( $result ) ) {
  60. return $result;
  61. }
  62. $plugin = Jetpack_Plugins::get_plugin_id_by_slug( $slug );
  63. $error_code = 'install_error';
  64. if ( ! $plugin ) {
  65. $error = __( 'There was an error installing your plugin', 'jetpack' );
  66. }
  67. if ( ! $result ) {
  68. $error_code = $upgrader->skin->get_main_error_code();
  69. $message = $upgrader->skin->get_main_error_message();
  70. $error = $message ? $message : __( 'An unknown error occurred during installation', 'jetpack' );
  71. }
  72. if ( ! empty( $error ) ) {
  73. if ( 'download_failed' === $error_code ) {
  74. // For backwards compatibility: versions prior to 3.9 would return no_package instead of download_failed.
  75. $error_code = 'no_package';
  76. }
  77. return new WP_Error( $error_code, $error, 400 );
  78. }
  79. return (array) $upgrader->skin->get_upgrade_messages();
  80. }
  81. protected static function generate_wordpress_org_plugin_download_link( $plugin_slug ) {
  82. return "https://downloads.wordpress.org/plugin/$plugin_slug.latest-stable.zip";
  83. }
  84. public static function get_plugin_id_by_slug( $slug ) {
  85. // Check if get_plugins() function exists. This is required on the front end of the
  86. // site, since it is in a file that is normally only loaded in the admin.
  87. if ( ! function_exists( 'get_plugins' ) ) {
  88. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  89. }
  90. /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
  91. $plugins = apply_filters( 'all_plugins', get_plugins() );
  92. if ( ! is_array( $plugins ) ) {
  93. return false;
  94. }
  95. foreach ( $plugins as $plugin_file => $plugin_data ) {
  96. if ( self::get_slug_from_file_path( $plugin_file ) === $slug ) {
  97. return $plugin_file;
  98. }
  99. }
  100. return false;
  101. }
  102. protected static function get_slug_from_file_path( $plugin_file ) {
  103. // Similar to get_plugin_slug() method.
  104. $slug = dirname( $plugin_file );
  105. if ( '.' === $slug ) {
  106. $slug = preg_replace( "/(.+)\.php$/", "$1", $plugin_file );
  107. }
  108. return $slug;
  109. }
  110. }