PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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 / integrations / admin / addon-installation / installation-integration.php

installation-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at src/integrations/admin/addon-installation/installation-integration.php

200 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Discussed in Tech Council, a better solution is being worked on.
4
5 namespace Yoast\WP\SEO\Integrations\Admin\Addon_Installation;
6
7 use WPSEO_Addon_Manager;
8 use Yoast\WP\SEO\Actions\Addon_Installation\Addon_Activate_Action;
9 use Yoast\WP\SEO\Actions\Addon_Installation\Addon_Install_Action;
10 use Yoast\WP\SEO\Conditionals\Addon_Installation_Conditional;
11 use Yoast\WP\SEO\Conditionals\Admin\Licenses_Page_Conditional;
12 use Yoast\WP\SEO\Conditionals\Admin_Conditional;
13 use Yoast\WP\SEO\Exceptions\Addon_Installation\Addon_Activation_Error_Exception;
14 use Yoast\WP\SEO\Exceptions\Addon_Installation\Addon_Already_Installed_Exception;
15 use Yoast\WP\SEO\Exceptions\Addon_Installation\Addon_Installation_Error_Exception;
16 use Yoast\WP\SEO\Exceptions\Addon_Installation\User_Cannot_Activate_Plugins_Exception;
17 use Yoast\WP\SEO\Exceptions\Addon_Installation\User_Cannot_Install_Plugins_Exception;
18 use Yoast\WP\SEO\Integrations\Integration_Interface;
19 use Yoast\WP\SEO\Plans\User_Interface\Plans_Page_Integration;
20
21 /**
22 * Represents the Addon installation feature.
23 */
24 class Installation_Integration implements Integration_Interface {
25
26 /**
27 * The installation action.
28 *
29 * @var Addon_Install_Action
30 */
31 protected $addon_install_action;
32
33 /**
34 * The activation action.
35 *
36 * @var Addon_Activate_Action
37 */
38 protected $addon_activate_action;
39
40 /**
41 * The addon manager.
42 *
43 * @var WPSEO_Addon_Manager
44 */
45 protected $addon_manager;
46
47 /**
48 * {@inheritDoc}
49 */
50 public static function get_conditionals() {
51 return [
52 Admin_Conditional::class,
53 Licenses_Page_Conditional::class,
54 Addon_Installation_Conditional::class,
55 ];
56 }
57
58 /**
59 * Addon_Installation constructor.
60 *
61 * @param WPSEO_Addon_Manager $addon_manager The addon manager.
62 * @param Addon_Activate_Action $addon_activate_action The addon activate action.
63 * @param Addon_Install_Action $addon_install_action The addon install action.
64 */
65 public function __construct(
66 WPSEO_Addon_Manager $addon_manager,
67 Addon_Activate_Action $addon_activate_action,
68 Addon_Install_Action $addon_install_action
69 ) {
70 $this->addon_manager = $addon_manager;
71 $this->addon_activate_action = $addon_activate_action;
72 $this->addon_install_action = $addon_install_action;
73 }
74
75 /**
76 * Registers all hooks to WordPress.
77 *
78 * @return void
79 */
80 public function register_hooks() {
81 \add_action( 'wpseo_install_and_activate_addons', [ $this, 'install_and_activate_addons' ] );
82 }
83
84 /**
85 * Installs and activates missing addons.
86 *
87 * @return void
88 */
89 public function install_and_activate_addons() {
90 if ( ! isset( $_GET['action'] ) || ! \is_string( $_GET['action'] ) ) {
91 return;
92 }
93
94 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: We are only strictly comparing action below.
95 $action = \wp_unslash( $_GET['action'] );
96 if ( $action !== 'install' ) {
97 return;
98 }
99
100 \check_admin_referer( 'wpseo_addon_installation', 'nonce' );
101
102 echo '<div class="wrap yoast wpseo_table_page">';
103
104 \printf(
105 '<h1 id="wpseo-title" class="yoast-h1">%s</h1>',
106 \esc_html__( 'Installing and activating addons', 'wordpress-seo' ),
107 );
108
109 $licensed_addons = $this->addon_manager->get_myyoast_site_information()->subscriptions;
110
111 foreach ( $licensed_addons as $addon ) {
112 \printf( '<p><strong>%s</strong></p>', \esc_html( $addon->product->name ) );
113
114 [ $installed, $output ] = $this->install_addon( $addon->product->slug, $addon->product->download );
115
116 if ( $installed ) {
117 $activation_output = $this->activate_addon( $addon->product->slug );
118
119 $output = \array_merge( $output, $activation_output );
120 }
121
122 echo '<p>';
123 echo \implode( '<br />', \array_map( 'esc_html', $output ) );
124 echo '</p>';
125 }
126
127 \printf(
128 /* translators: %1$s expands to an anchor tag to the admin premium page, %2$s expands to Yoast SEO Premium, %3$s expands to a closing anchor tag */
129 \esc_html__( '%1$s Continue to %2$s%3$s', 'wordpress-seo' ),
130 '<a href="' . \esc_url( \admin_url( 'admin.php?page=' . Plans_Page_Integration::PAGE ) ) . '">',
131 'Yoast SEO Premium',
132 '</a>',
133 );
134
135 echo '</div>';
136
137 exit();
138 }
139
140 /**
141 * Activates an addon.
142 *
143 * @param string $addon_slug The addon to activate.
144 *
145 * @return array The output of the activation.
146 */
147 public function activate_addon( $addon_slug ) {
148 $output = [];
149
150 try {
151 $this->addon_activate_action->activate_addon( $addon_slug );
152
153 /* Translators: %s expands to the name of the addon. */
154 $output[] = \__( 'Addon activated.', 'wordpress-seo' );
155 } catch ( User_Cannot_Activate_Plugins_Exception $exception ) {
156 $output[] = \__( 'You are not allowed to activate plugins.', 'wordpress-seo' );
157 } catch ( Addon_Activation_Error_Exception $exception ) {
158 $output[] = \sprintf(
159 /* Translators:%s expands to the error message. */
160 \__( 'Addon activation failed because of an error: %s.', 'wordpress-seo' ),
161 $exception->getMessage(),
162 );
163 }
164
165 return $output;
166 }
167
168 /**
169 * Installs an addon.
170 *
171 * @param string $addon_slug The slug of the addon to install.
172 * @param string $addon_download The download URL of the addon.
173 *
174 * @return array The installation success state and the output of the installation.
175 */
176 public function install_addon( $addon_slug, $addon_download ) {
177 $installed = false;
178 $output = [];
179
180 try {
181 $installed = $this->addon_install_action->install_addon( $addon_slug, $addon_download );
182 } catch ( Addon_Already_Installed_Exception $exception ) {
183 /* Translators: %s expands to the name of the addon. */
184 $output[] = \__( 'Addon installed.', 'wordpress-seo' );
185
186 $installed = true;
187 } catch ( User_Cannot_Install_Plugins_Exception $exception ) {
188 $output[] = \__( 'You are not allowed to install plugins.', 'wordpress-seo' );
189 } catch ( Addon_Installation_Error_Exception $exception ) {
190 $output[] = \sprintf(
191 /* Translators: %s expands to the error message. */
192 \__( 'Addon installation failed because of an error: %s.', 'wordpress-seo' ),
193 $exception->getMessage(),
194 );
195 }
196
197 return [ $installed, $output ];
198 }
199 }
200