addon-install-action.php
3.86 KB
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
<?php
namespace Yoast\WP\SEO\Actions\Addon_Installation;
use Plugin_Upgrader;
use WP_Error;
use WPSEO_Addon_Manager;
use Yoast\WP\SEO\Exceptions\Addon_Installation\Addon_Already_Installed_Exception;
use Yoast\WP\SEO\Exceptions\Addon_Installation\Addon_Installation_Error_Exception;
use Yoast\WP\SEO\Exceptions\Addon_Installation\User_Cannot_Install_Plugins_Exception;
use Yoast\WP\SEO\Helpers\Require_File_Helper;
/**
* Represents the endpoint for downloading and installing a zip-file from MyYoast.
*/
class Addon_Install_Action {
/**
* The addon manager.
*
* @var WPSEO_Addon_Manager
*/
protected $addon_manager;
/**
* The require file helper.
*
* @var Require_File_Helper
*/
protected $require_file_helper;
/**
* Addon_Activate_Action constructor.
*
* @param WPSEO_Addon_Manager $addon_manager The addon manager.
* @param Require_File_Helper $require_file_helper A helper that can require files.
*/
public function __construct(
WPSEO_Addon_Manager $addon_manager,
Require_File_Helper $require_file_helper
) {
$this->addon_manager = $addon_manager;
$this->require_file_helper = $require_file_helper;
}
/**
* Installs the plugin based on the given slug.
*
* @param string $plugin_slug The plugin slug to install.
* @param string $download_url The plugin download URL.
*
* @return bool True when install is successful.
*
* @throws Addon_Already_Installed_Exception When the addon is already installed.
* @throws Addon_Installation_Error_Exception When the installation encounters an error.
* @throws User_Cannot_Install_Plugins_Exception When the user does not have the permissions to install plugins.
*/
public function install_addon( $plugin_slug, $download_url ) {
if ( ! \current_user_can( 'install_plugins' ) ) {
throw new User_Cannot_Install_Plugins_Exception( $plugin_slug );
}
if ( $this->is_installed( $plugin_slug ) ) {
throw new Addon_Already_Installed_Exception( $plugin_slug );
}
$this->load_wordpress_classes();
$install_result = $this->install( $download_url );
if ( \is_wp_error( $install_result ) ) {
throw new Addon_Installation_Error_Exception( $install_result->get_error_message() );
}
return $install_result;
}
/**
* Requires the files needed from WordPress itself.
*
* @codeCoverageIgnore
*
* @return void
*/
protected function load_wordpress_classes() {
if ( ! \class_exists( 'WP_Upgrader' ) ) {
$this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
}
if ( ! \class_exists( 'Plugin_Upgrader' ) ) {
$this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php' );
}
if ( ! \class_exists( 'WP_Upgrader_Skin' ) ) {
$this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php' );
}
if ( ! \function_exists( 'get_plugin_data' ) ) {
$this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/plugin.php' );
}
if ( ! \function_exists( 'request_filesystem_credentials' ) ) {
$this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/file.php' );
}
}
/**
* Checks is a plugin is installed.
*
* @param string $plugin_slug The plugin to check.
*
* @return bool True when plugin is installed.
*/
protected function is_installed( $plugin_slug ) {
return $this->addon_manager->get_plugin_file( $plugin_slug ) !== false;
}
/**
* Runs the installation by using the WordPress installation routine.
*
* @codeCoverageIgnore Contains WordPress specific logic.
*
* @param string $plugin_download The url to the download.
*
* @return bool|WP_Error True when success, WP_Error when something went wrong.
*/
protected function install( $plugin_download ) {
$plugin_upgrader = new Plugin_Upgrader();
return $plugin_upgrader->install( $plugin_download );
}
}