PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.9
Jetpack – WP Security, Backup, Speed, & Growth v7.9
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / _inc / lib / plugins.php

plugins.php in Jetpack – WP Security, Backup, Speed, & Growth 7.9, at _inc/lib/plugins.php

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