PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / actions / addon-installation / addon-install-action.php

addon-install-action.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.2, at src/actions/addon-installation/addon-install-action.php

132 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\Actions\Addon_Installation;
4
5 use Plugin_Upgrader;
6 use WP_Error;
7 use WPSEO_Addon_Manager;
8 use Yoast\WP\SEO\Exceptions\Addon_Installation\Addon_Already_Installed_Exception;
9 use Yoast\WP\SEO\Exceptions\Addon_Installation\Addon_Installation_Error_Exception;
10 use Yoast\WP\SEO\Exceptions\Addon_Installation\User_Cannot_Install_Plugins_Exception;
11 use Yoast\WP\SEO\Helpers\Require_File_Helper;
12
13 /**
14 * Represents the endpoint for downloading and installing a zip-file from MyYoast.
15 */
16 class Addon_Install_Action {
17
18 /**
19 * The addon manager.
20 *
21 * @var WPSEO_Addon_Manager
22 */
23 protected $addon_manager;
24
25 /**
26 * The require file helper.
27 *
28 * @var Require_File_Helper
29 */
30 protected $require_file_helper;
31
32 /**
33 * Addon_Activate_Action constructor.
34 *
35 * @param WPSEO_Addon_Manager $addon_manager The addon manager.
36 * @param Require_File_Helper $require_file_helper A helper that can require files.
37 */
38 public function __construct(
39 WPSEO_Addon_Manager $addon_manager,
40 Require_File_Helper $require_file_helper
41 ) {
42 $this->addon_manager = $addon_manager;
43 $this->require_file_helper = $require_file_helper;
44 }
45
46 /**
47 * Installs the plugin based on the given slug.
48 *
49 * @param string $plugin_slug The plugin slug to install.
50 * @param string $download_url The plugin download URL.
51 *
52 * @return bool True when install is successful.
53 *
54 * @throws Addon_Already_Installed_Exception When the addon is already installed.
55 * @throws Addon_Installation_Error_Exception When the installation encounters an error.
56 * @throws User_Cannot_Install_Plugins_Exception When the user does not have the permissions to install plugins.
57 */
58 public function install_addon( $plugin_slug, $download_url ) {
59 if ( ! \current_user_can( 'install_plugins' ) ) {
60 throw new User_Cannot_Install_Plugins_Exception( $plugin_slug );
61 }
62
63 if ( $this->is_installed( $plugin_slug ) ) {
64 throw new Addon_Already_Installed_Exception( $plugin_slug );
65 }
66
67 $this->load_wordpress_classes();
68
69 $install_result = $this->install( $download_url );
70 if ( \is_wp_error( $install_result ) ) {
71 throw new Addon_Installation_Error_Exception( $install_result->get_error_message() );
72 }
73
74 return $install_result;
75 }
76
77 /**
78 * Requires the files needed from WordPress itself.
79 *
80 * @codeCoverageIgnore
81 *
82 * @return void
83 */
84 protected function load_wordpress_classes() {
85 if ( ! \class_exists( 'WP_Upgrader' ) ) {
86 $this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
87 }
88
89 if ( ! \class_exists( 'Plugin_Upgrader' ) ) {
90 $this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php' );
91 }
92
93 if ( ! \class_exists( 'WP_Upgrader_Skin' ) ) {
94 $this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/class-wp-upgrader-skin.php' );
95 }
96
97 if ( ! \function_exists( 'get_plugin_data' ) ) {
98 $this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/plugin.php' );
99 }
100
101 if ( ! \function_exists( 'request_filesystem_credentials' ) ) {
102 $this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/file.php' );
103 }
104 }
105
106 /**
107 * Checks is a plugin is installed.
108 *
109 * @param string $plugin_slug The plugin to check.
110 *
111 * @return bool True when plugin is installed.
112 */
113 protected function is_installed( $plugin_slug ) {
114 return $this->addon_manager->get_plugin_file( $plugin_slug ) !== false;
115 }
116
117 /**
118 * Runs the installation by using the WordPress installation routine.
119 *
120 * @codeCoverageIgnore Contains WordPress specific logic.
121 *
122 * @param string $plugin_download The url to the download.
123 *
124 * @return bool|WP_Error True when success, WP_Error when something went wrong.
125 */
126 protected function install( $plugin_download ) {
127 $plugin_upgrader = new Plugin_Upgrader();
128
129 return $plugin_upgrader->install( $plugin_download );
130 }
131 }
132