Completed
Push — renovate/gridicons-3.x ( c004c1...f8ccd4 )
by
unknown
284:06 queued 275:32
created

_inc/lib/plugins.php (3 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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' ) );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'not_allowed'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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' ) );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'not_allowed'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with $error_code.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
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