Completed
Push — add/install-and-activate-plugi... ( a3f80a...09c01c )
by Bernhard
37:54 queued 26:22
created

Jetpack_Plugins::install_and_activate_plugin()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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