summaryrefslogtreecommitdiff
blob: bae23baa0a249df162ef8075f22ccedb797206c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
 * Plugins Library
 *
 * Helper functions for installing and activating plugins.
 *
 * Used by the REST API
 *
 * @package jetpack-plugins-installer
 */

namespace Automattic\Jetpack;

use Plugin_Upgrader;
use WP_Error;

/**
 * Plugins management tools.
 */
class Plugins_Installer {

	/**
	 * Ensures that plugins functions are loaded, as they are only loaded in admin context by default.
	 */
	private static function ensure_plugin_functions_are_loaded() {
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
	}
	/**
	 * Install and activate a plugin.
	 *
	 * @since-jetpack 5.8.0
	 *
	 * @param string $slug Plugin slug.
	 *
	 * @return bool|WP_Error True if installation succeeded, error object otherwise.
	 */
	public static function install_and_activate_plugin( $slug ) {
		$plugin_id = self::get_plugin_id_by_slug( $slug );

		if ( ! $plugin_id ) {
			$installed = self::install_plugin( $slug );
			if ( is_wp_error( $installed ) ) {
				return $installed;
			}
			$plugin_id = self::get_plugin_id_by_slug( $slug );
		} elseif ( self::is_plugin_active( $plugin_id ) ) {
			return true; // Already installed and active.
		}

		if ( ! current_user_can( 'activate_plugins' ) ) {
			return new WP_Error( 'not_allowed', __( 'You are not allowed to activate plugins on this site.', 'jetpack-plugins-installer' ) );
		}
		$activated = activate_plugin( $plugin_id );
		if ( is_wp_error( $activated ) ) {
			return $activated;
		}

		return true;
	}

	/**
	 * Install a plugin.
	 *
	 * @since-jetpack 5.8.0
	 *
	 * @param string $slug Plugin slug.
	 *
	 * @return bool|WP_Error True if installation succeeded, error object otherwise.
	 */
	public static function install_plugin( $slug ) {
		if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
			return new WP_Error( 'not_allowed', __( 'You are not allowed to install plugins on this site.', 'jetpack-plugins-installer' ) );
		}

		$skin     = new Automatic_Install_Skin();
		$upgrader = new Plugin_Upgrader( $skin );
		$zip_url  = self::generate_wordpress_org_plugin_download_link( $slug );
		$mc_stats = new A8c_Mc_Stats();

		$result = $upgrader->install( $zip_url );

		if ( is_wp_error( $result ) ) {
			$mc_stats->add( 'install-plugin', "fail-$slug" );
			return $result;
		}

		$plugin     = self::get_plugin_id_by_slug( $slug );
		$error_code = 'install_error';
		if ( ! $plugin ) {
			$error = __( 'There was an error installing your plugin', 'jetpack-plugins-installer' );
		}

		if ( ! $result ) {
			$error_code = $upgrader->skin->get_main_error_code();
			$message    = $upgrader->skin->get_main_error_message();
			$error      = $message ? $message : __( 'An unknown error occurred during installation', 'jetpack-plugins-installer' );
		}

		if ( ! empty( $error ) ) {
			if ( 'download_failed' === $error_code ) {
				// For backwards compatibility: versions prior to 3.9 would return no_package instead of download_failed.
				$error_code = 'no_package';
			}

			$mc_stats->add( 'install-plugin', "fail-$slug" );
			return new WP_Error( $error_code, $error, 400 );
		}

		$mc_stats->add( 'install-plugin', "success-$slug" );
		return (array) $upgrader->skin->get_upgrade_messages();
	}

	/**
	 * Get WordPress.org zip download link from a plugin slug
	 *
	 * @param string $plugin_slug Plugin slug.
	 */
	protected static function generate_wordpress_org_plugin_download_link( $plugin_slug ) {
		return "https://downloads.wordpress.org/plugin/$plugin_slug.latest-stable.zip";
	}

	/**
	 * Get the plugin ID (composed of the plugin slug and the name of the main plugin file) from a plugin slug.
	 *
	 * @param string $slug Plugin slug.
	 */
	public static function get_plugin_id_by_slug( $slug ) {
		// Check if get_plugins() function exists. This is required on the front end of the
		// site, since it is in a file that is normally only loaded in the admin.
		if ( ! function_exists( 'get_plugins' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}

		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
		$plugins = apply_filters( 'all_plugins', get_plugins() );
		if ( ! is_array( $plugins ) ) {
			return false;
		}

		foreach ( $plugins as $plugin_file => $plugin_data ) {
			if ( self::get_slug_from_file_path( $plugin_file ) === $slug ) {
				return $plugin_file;
			}
		}

		return false;
	}

	/**
	 * Get the plugin slug from the plugin ID (composed of the plugin slug and the name of the main plugin file)
	 *
	 * @param string $plugin_file Plugin file (ID -- e.g. hello-dolly/hello.php).
	 */
	protected static function get_slug_from_file_path( $plugin_file ) {
		// Similar to get_plugin_slug() method.
		$slug = dirname( $plugin_file );
		if ( '.' === $slug ) {
			$slug = preg_replace( '/(.+)\.php$/', '$1', $plugin_file );
		}

		return $slug;
	}

	/**
	 * Get the activation status for a plugin.
	 *
	 * @since-jetpack 8.9.0
	 *
	 * @param string $plugin_file The plugin file to check.
	 * @return string Either 'network-active', 'active' or 'inactive'.
	 */
	public static function get_plugin_status( $plugin_file ) {
		if ( self::is_plugin_active_for_network( $plugin_file ) ) {
			return 'network-active';
		}

		if ( self::is_plugin_active( $plugin_file ) ) {
			return 'active';
		}

		return 'inactive';
	}

	/**
	 * Safely checks if the plugin is active
	 *
	 * @since $next-version$
	 *
	 * @param string $plugin_file The plugin file to check.
	 * @return bool
	 */
	public static function is_plugin_active( $plugin_file ) {
		self::ensure_plugin_functions_are_loaded();
		return is_plugin_active( $plugin_file );
	}

	/**
	 * Safely checks if the plugin is active for network
	 *
	 * @since $next-version$
	 *
	 * @param string $plugin_file The plugin file to check.
	 * @return bool
	 */
	public static function is_plugin_active_for_network( $plugin_file ) {
		self::ensure_plugin_functions_are_loaded();
		return is_plugin_active_for_network( $plugin_file );
	}

	/**
	 * Returns a list of all plugins in the site.
	 *
	 * @since-jetpack 8.9.0
	 * @uses get_plugins()
	 *
	 * @return array
	 */
	public static function get_plugins() {
		self::ensure_plugin_functions_are_loaded();
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
		$plugins = apply_filters( 'all_plugins', get_plugins() );

		if ( is_array( $plugins ) && ! empty( $plugins ) ) {
			foreach ( $plugins as $plugin_slug => $plugin_data ) {
				$plugins[ $plugin_slug ]['active'] = in_array(
					self::get_plugin_status( $plugin_slug ),
					array( 'active', 'network-active' ),
					true
				);
			}
			return $plugins;
		}

		return array();
	}
}