PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 10.5.3
Jetpack – WP Security, Backup, Speed, & Growth v10.5.3
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
206 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
12 require_once 'class.jetpack-automatic-install-skin.php';
13
14 use Automattic\Jetpack\A8c_Mc_Stats;
15
16 /**
17 * Plugins management tools.
18 */
19 class Jetpack_Plugins {
20
21 /**
22 * Install and activate a plugin.
23 *
24 * @since 5.8.0
25 *
26 * @param string $slug Plugin slug.
27 *
28 * @return bool|WP_Error True if installation succeeded, error object otherwise.
29 */
30 public static function install_and_activate_plugin( $slug ) {
31 $plugin_id = self::get_plugin_id_by_slug( $slug );
32
33 if ( ! $plugin_id ) {
34 $installed = self::install_plugin( $slug );
35 if ( is_wp_error( $installed ) ) {
36 return $installed;
37 }
38 $plugin_id = self::get_plugin_id_by_slug( $slug );
39 } elseif ( is_plugin_active( $plugin_id ) ) {
40 return true; // Already installed and active.
41 }
42
43 if ( ! current_user_can( 'activate_plugins' ) ) {
44 return new WP_Error( 'not_allowed', __( 'You are not allowed to activate plugins on this site.', 'jetpack' ) );
45 }
46 $activated = activate_plugin( $plugin_id );
47 if ( is_wp_error( $activated ) ) {
48 return $activated;
49 }
50
51 return true;
52 }
53
54 /**
55 * Install a plugin.
56 *
57 * @since 5.8.0
58 *
59 * @param string $slug Plugin slug.
60 *
61 * @return bool|WP_Error True if installation succeeded, error object otherwise.
62 */
63 public static function install_plugin( $slug ) {
64 if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
65 return new WP_Error( 'not_allowed', __( 'You are not allowed to install plugins on this site.', 'jetpack' ) );
66 }
67
68 $skin = new Jetpack_Automatic_Install_Skin();
69 $upgrader = new Plugin_Upgrader( $skin );
70 $zip_url = self::generate_wordpress_org_plugin_download_link( $slug );
71 $mc_stats = new A8c_Mc_Stats();
72
73 $result = $upgrader->install( $zip_url );
74
75 if ( is_wp_error( $result ) ) {
76 $mc_stats->add( 'install-plugin', "fail-$slug" );
77 return $result;
78 }
79
80 $plugin = self::get_plugin_id_by_slug( $slug );
81 $error_code = 'install_error';
82 if ( ! $plugin ) {
83 $error = __( 'There was an error installing your plugin', 'jetpack' );
84 }
85
86 if ( ! $result ) {
87 $error_code = $upgrader->skin->get_main_error_code();
88 $message = $upgrader->skin->get_main_error_message();
89 $error = $message ? $message : __( 'An unknown error occurred during installation', 'jetpack' );
90 }
91
92 if ( ! empty( $error ) ) {
93 if ( 'download_failed' === $error_code ) {
94 // For backwards compatibility: versions prior to 3.9 would return no_package instead of download_failed.
95 $error_code = 'no_package';
96 }
97
98 $mc_stats->add( 'install-plugin', "fail-$slug" );
99 return new WP_Error( $error_code, $error, 400 );
100 }
101
102 $mc_stats->add( 'install-plugin', "success-$slug" );
103 return (array) $upgrader->skin->get_upgrade_messages();
104 }
105
106 /**
107 * Get WordPress.org zip download link from a plugin slug
108 *
109 * @param string $plugin_slug Plugin slug.
110 */
111 protected static function generate_wordpress_org_plugin_download_link( $plugin_slug ) {
112 return "https://downloads.wordpress.org/plugin/$plugin_slug.latest-stable.zip";
113 }
114
115 /**
116 * Get the plugin ID (composed of the plugin slug and the name of the main plugin file) from a plugin slug.
117 *
118 * @param string $slug Plugin slug.
119 */
120 public static function get_plugin_id_by_slug( $slug ) {
121 // Check if get_plugins() function exists. This is required on the front end of the
122 // site, since it is in a file that is normally only loaded in the admin.
123 if ( ! function_exists( 'get_plugins' ) ) {
124 require_once ABSPATH . 'wp-admin/includes/plugin.php';
125 }
126
127 /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
128 $plugins = apply_filters( 'all_plugins', get_plugins() );
129 if ( ! is_array( $plugins ) ) {
130 return false;
131 }
132
133 foreach ( $plugins as $plugin_file => $plugin_data ) {
134 if ( self::get_slug_from_file_path( $plugin_file ) === $slug ) {
135 return $plugin_file;
136 }
137 }
138
139 return false;
140 }
141
142 /**
143 * Get the plugin slug from the plugin ID (composed of the plugin slug and the name of the main plugin file)
144 *
145 * @param string $plugin_file Plugin file (ID -- e.g. hello-dolly/hello.php).
146 */
147 protected static function get_slug_from_file_path( $plugin_file ) {
148 // Similar to get_plugin_slug() method.
149 $slug = dirname( $plugin_file );
150 if ( '.' === $slug ) {
151 $slug = preg_replace( '/(.+)\.php$/', '$1', $plugin_file );
152 }
153
154 return $slug;
155 }
156
157 /**
158 * Get the activation status for a plugin.
159 *
160 * @since 8.9.0
161 *
162 * @param string $plugin_file The plugin file to check.
163 * @return string Either 'network-active', 'active' or 'inactive'.
164 */
165 public static function get_plugin_status( $plugin_file ) {
166 if ( is_plugin_active_for_network( $plugin_file ) ) {
167 return 'network-active';
168 }
169
170 if ( is_plugin_active( $plugin_file ) ) {
171 return 'active';
172 }
173
174 return 'inactive';
175 }
176
177 /**
178 * Returns a list of all plugins in the site.
179 *
180 * @since 8.9.0
181 * @uses get_plugins()
182 *
183 * @return array
184 */
185 public static function get_plugins() {
186 if ( ! function_exists( 'get_plugins' ) ) {
187 require_once ABSPATH . 'wp-admin/includes/plugin.php';
188 }
189 /** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
190 $plugins = apply_filters( 'all_plugins', get_plugins() );
191
192 if ( is_array( $plugins ) && ! empty( $plugins ) ) {
193 foreach ( $plugins as $plugin_slug => $plugin_data ) {
194 $plugins[ $plugin_slug ]['active'] = in_array(
195 self::get_plugin_status( $plugin_slug ),
196 array( 'active', 'network-active' ),
197 true
198 );
199 }
200 return $plugins;
201 }
202
203 return array();
204 }
205 }
206