Completed
Push — master ( aace4e...5b4844 )
by Bernhard
12:38
created

Jetpack_Plugins::install_plugin()   D

Complexity

Conditions 9
Paths 20

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 22
nc 20
nop 1
dl 0
loc 38
rs 4.909
c 0
b 0
f 0
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 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_plugin( $slug ) {
26
		if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
27
			return new WP_Error( 'not_allowed', __( 'You are not allowed to install plugins on this site.', 'jetpack' ) );
28
		}
29
30
		$skin     = new Jetpack_Automatic_Install_Skin();
31
		$upgrader = new Plugin_Upgrader( $skin );
32
		$zip_url  = self::generate_wordpress_org_plugin_download_link( $slug );
33
34
		$result = $upgrader->install( $zip_url );
35
36
		if ( is_wp_error( $result ) ) {
37
		  return $result;
38
		}
39
40
		$plugin     = Jetpack_Plugins::get_plugin_id_by_slug( $slug );
41
		$error_code = 'install_error';
42
		if ( ! $plugin ) {
43
		  $error = __( 'There was an error installing your plugin', 'jetpack' );
44
		}
45
46
		if ( ! $result ) {
47
		  $error_code                         = $upgrader->skin->get_main_error_code();
48
		  $message                            = $upgrader->skin->get_main_error_message();
49
		  $error = $message ? $message : __( 'An unknown error occurred during installation', 'jetpack' );
50
		}
51
52
		if ( ! empty( $error ) ) {
53
			if ( 'download_failed' === $error_code ) {
54
				// For backwards compatibility: versions prior to 3.9 would return no_package instead of download_failed.
55
				$error_code = 'no_package';
56
			}
57
58
			return new WP_Error( $error_code, $error, 400 );
59
		}
60
61
		return (array) $upgrader->skin->get_upgrade_messages();
62
	}
63
64
	 protected static function generate_wordpress_org_plugin_download_link( $plugin_slug ) {
65
		return "https://downloads.wordpress.org/plugin/$plugin_slug.latest-stable.zip";
66
	 }
67
68
	 public static function get_plugin_id_by_slug( $slug ) {
69
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
70
		$plugins = apply_filters( 'all_plugins', get_plugins() );
71
		if ( ! is_array( $plugins ) ) {
72
			return false;
73
		}
74
		foreach ( $plugins as $plugin_file => $plugin_data ) {
75
			if ( self::get_slug_from_file_path( $plugin_file ) === $slug ) {
76
				return $plugin_file;
77
			}
78
		}
79
80
		return false;
81
	}
82
83
	protected static function get_slug_from_file_path( $plugin_file ) {
84
		// Similar to get_plugin_slug() method.
85
		$slug = dirname( $plugin_file );
86
		if ( '.' === $slug ) {
87
			$slug = preg_replace( "/(.+)\.php$/", "$1", $plugin_file );
88
		}
89
90
		return $slug;
91
	}
92
}
93