Completed
Push — add/plugins-library ( d36715...4df2bb )
by Bernhard
36:05 queued 18:03
created

Jetpack_Plugins::get_plugin_id_by_slug()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 14
rs 9.2
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 ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
13
include_once ABSPATH . 'wp-admin/includes/file.php';
14
15
class Jetpack_Plugins {
16
17
	 /**
18
	 * Install a plugin.
19
	 *
20
	 * @since 5.8.0
21
	 *
22
	 * @param string $slug Plugin slug.
23
	 *
24
	 * @return bool|WP_Error True if installation succeeded, error object otherwise.
25
	 */
26
	public static function install_plugin( $slug ) {
27
		if ( is_multisite() && ! current_user_can( 'manage_network' ) ) {
28
			return new WP_Error( 'not_allowed', 'You are not allowed to install plugins on this site.' );
29
		}
30
31
		$skin     = new Jetpack_Automatic_Install_Skin();
32
		$upgrader = new Plugin_Upgrader( $skin );
33
		$zip_url  = self::generate_wordpress_org_plugin_download_link( $slug );
34
35
		return $upgrader->install( $zip_url );
36
	}
37
38
	 protected static function generate_wordpress_org_plugin_download_link( $plugin_slug ) {
39
		return "https://downloads.wordpress.org/plugin/{$plugin_slug}.latest-stable.zip";
40
	 }
41
42
	 public static function get_plugin_id_by_slug( $slug ) {
43
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
44
		$plugins = apply_filters( 'all_plugins', get_plugins() );
45
		if ( ! is_array( $plugins ) ) {
46
			return false;
47
		}
48
		foreach ( $plugins as $plugin_file => $plugin_data ) {
49
			if ( self::get_slug_from_file_path( $plugin_file ) === $slug ) {
50
				return $plugin_file;
51
			}
52
		}
53
54
		return false;
55
	}
56
57
	protected static function get_slug_from_file_path( $plugin_file ) {
58
		// Simular to get_plugin_slug() method.
59
		$slug = dirname( $plugin_file );
60
		if ( '.' === $slug ) {
61
			$slug = preg_replace( "/(.+)\.php$/", "$1", $plugin_file );
62
		}
63
64
		return $slug;
65
	}
66
}
67
68
/**
69
 * Allows us to capture that the site doesn't have proper file system access.
70
 * In order to update the plugin.
71
 */
72
class Jetpack_Automatic_Install_Skin extends Automatic_Upgrader_Skin {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
73
	/**
74
	 * Stores the last error key;
75
	 **/
76
	protected $main_error_code = 'install_error';
77
78
	/**
79
	 * Stores the last error message.
80
	 **/
81
	protected $main_error_message = 'An unknown error occurred during installation';
82
83
	/**
84
	 * Overwrites the set_upgrader to be able to tell if we e ven have the ability to write to the files.
85
	 *
86
	 * @param WP_Upgrader $upgrader
87
	 *
88
	 */
89
	public function set_upgrader( &$upgrader ) {
90
		parent::set_upgrader( $upgrader );
91
92
		// Check if we even have permission to.
93
		$result = $upgrader->fs_connect( array( WP_CONTENT_DIR, WP_PLUGIN_DIR ) );
94
		if ( ! $result ) {
95
			// set the string here since they are not available just yet
96
			$upgrader->generic_strings();
97
			$this->feedback( 'fs_unavailable' );
98
		}
99
	}
100
101
	/**
102
	 * Overwrites the error function
103
	 */
104
	public function error( $error ) {
105
		if ( is_wp_error( $error ) ) {
106
			$this->feedback( $error );
107
		}
108
	}
109
110
	private function set_main_error_code( $code ) {
111
		// Don't set the process_failed as code since it is not that helpful unless we don't have one already set.
112
		$this->main_error_code = ( $code === 'process_failed' && $this->main_error_code ? $this->main_error_code : $code );
113
	}
114
115
	private function set_main_error_message( $message, $code ) {
116
		// Don't set the process_failed as message since it is not that helpful unless we don't have one already set.
117
		$this->main_error_message = ( $code === 'process_failed' && $this->main_error_code ? $this->main_error_code : $message );
118
	}
119
120
	public function get_main_error_code() {
121
		return $this->main_error_code;
122
	}
123
124
	public function get_main_error_message() {
125
		return $this->main_error_message;
126
	}
127
128
	/**
129
	 * Overwrites the feedback function
130
	 */
131
	public function feedback( $data ) {
132
133
		$current_error = null;
134
		if ( is_wp_error( $data ) ) {
135
			$this->set_main_error_code( $data->get_error_code() );
136
			$string = $data->get_error_message();
137
		} elseif ( is_array( $data ) ) {
138
			return;
139
		} else {
140
			$string = $data;
141
		}
142
143
		if ( ! empty( $this->upgrader->strings[$string] ) ) {
144
			$this->set_main_error_code( $string );
145
146
			$current_error = $string;
147
			$string        = $this->upgrader->strings[$string];
148
		}
149
150
		if ( strpos( $string, '%' ) !== false ) {
151
			$args = func_get_args();
152
			$args = array_splice( $args, 1 );
153
			if ( ! empty( $args ) ) {
154
				$string = vsprintf( $string, $args );
155
			}
156
		}
157
158
		$string = trim( $string );
159
		$string = wp_kses(
160
			$string, array(
161
			'a'      => array(
162
				'href' => true
163
			),
164
			'br'     => true,
165
			'em'     => true,
166
			'strong' => true,
167
		)
168
		);
169
170
		$this->set_main_error_message( $string, $current_error );
171
		$this->messages[] = $string;
172
	}
173
}
174