| 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 |
|
| 20 |
/** |
| 21 |
* Represents the Addon installation feature. |
| 22 |
*/ |
| 23 |
class Installation_Integration implements Integration_Interface { |
| 24 |
|
| 25 |
/** |
| 26 |
* The installation action. |
| 27 |
* |
| 28 |
* @var Addon_Install_Action |
| 29 |
*/ |
| 30 |
protected $addon_install_action; |
| 31 |
|
| 32 |
/** |
| 33 |
* The activation action. |
| 34 |
* |
| 35 |
* @var Addon_Activate_Action |
| 36 |
*/ |
| 37 |
protected $addon_activate_action; |
| 38 |
|
| 39 |
/** |
| 40 |
* The addon manager. |
| 41 |
* |
| 42 |
* @var WPSEO_Addon_Manager |
| 43 |
*/ |
| 44 |
protected $addon_manager; |
| 45 |
|
| 46 |
/** |
| 47 |
* {@inheritDoc} |
| 48 |
*/ |
| 49 |
public static function get_conditionals() { |
| 50 |
return [ |
| 51 |
Admin_Conditional::class, |
| 52 |
Licenses_Page_Conditional::class, |
| 53 |
Addon_Installation_Conditional::class, |
| 54 |
]; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Addon_Installation constructor. |
| 59 |
* |
| 60 |
* @param WPSEO_Addon_Manager $addon_manager The addon manager. |
| 61 |
* @param Addon_Activate_Action $addon_activate_action The addon activate action. |
| 62 |
* @param Addon_Install_Action $addon_install_action The addon install action. |
| 63 |
*/ |
| 64 |
public function __construct( |
| 65 |
WPSEO_Addon_Manager $addon_manager, |
| 66 |
Addon_Activate_Action $addon_activate_action, |
| 67 |
Addon_Install_Action $addon_install_action |
| 68 |
) { |
| 69 |
$this->addon_manager = $addon_manager; |
| 70 |
$this->addon_activate_action = $addon_activate_action; |
| 71 |
$this->addon_install_action = $addon_install_action; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Registers all hooks to WordPress. |
| 76 |
*/ |
| 77 |
public function register_hooks() { |
| 78 |
\add_action( 'wpseo_install_and_activate_addons', [ $this, 'install_and_activate_addons' ] ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Installs and activates missing addons. |
| 83 |
* |
| 84 |
* @return void |
| 85 |
*/ |
| 86 |
public function install_and_activate_addons() { |
| 87 |
if ( \filter_input( \INPUT_GET, 'action' ) !== 'install' ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
\check_admin_referer( 'wpseo_addon_installation', 'nonce' ); |
| 92 |
|
| 93 |
echo '<div class="wrap yoast wpseo_table_page">'; |
| 94 |
|
| 95 |
\printf( |
| 96 |
'<h1 id="wpseo-title" class="yoast-h1">%s</h1>', |
| 97 |
\esc_html__( 'Installing and activating addons', 'wordpress-seo' ) |
| 98 |
); |
| 99 |
|
| 100 |
$licensed_addons = $this->addon_manager->get_myyoast_site_information()->subscriptions; |
| 101 |
|
| 102 |
foreach ( $licensed_addons as $addon ) { |
| 103 |
\printf( '<p><strong>%s</strong></p>', \esc_html( $addon->product->name ) ); |
| 104 |
|
| 105 |
list( $installed, $output ) = $this->install_addon( $addon->product->slug, $addon->product->download ); |
| 106 |
|
| 107 |
if ( $installed ) { |
| 108 |
$activation_output = $this->activate_addon( $addon->product->slug ); |
| 109 |
|
| 110 |
$output = \array_merge( $output, $activation_output ); |
| 111 |
} |
| 112 |
|
| 113 |
echo '<p>'; |
| 114 |
echo \implode( '<br />', \array_map( 'esc_html', $output ) ); |
| 115 |
echo '</p>'; |
| 116 |
} |
| 117 |
|
| 118 |
\printf( |
| 119 |
/* 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 */ |
| 120 |
\esc_html__( '%1$s Continue to %2$s%3$s', 'wordpress-seo' ), |
| 121 |
'<a href="' . \esc_url( \admin_url( 'admin.php?page=wpseo_licenses' ) ) . '">', |
| 122 |
'Yoast SEO Premium', |
| 123 |
'</a>' |
| 124 |
); |
| 125 |
|
| 126 |
echo '</div>'; |
| 127 |
|
| 128 |
exit; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Activates an addon. |
| 133 |
* |
| 134 |
* @param string $addon_slug The addon to activate. |
| 135 |
* |
| 136 |
* @return array The output of the activation. |
| 137 |
*/ |
| 138 |
public function activate_addon( $addon_slug ) { |
| 139 |
$output = []; |
| 140 |
|
| 141 |
try { |
| 142 |
$this->addon_activate_action->activate_addon( $addon_slug ); |
| 143 |
|
| 144 |
/* Translators: %s expands to the name of the addon. */ |
| 145 |
$output[] = \__( 'Addon activated.', 'wordpress-seo' ); |
| 146 |
} catch ( User_Cannot_Activate_Plugins_Exception $exception ) { |
| 147 |
$output[] = \__( 'You are not allowed to activate plugins.', 'wordpress-seo' ); |
| 148 |
} catch ( Addon_Activation_Error_Exception $exception ) { |
| 149 |
$output[] = \sprintf( |
| 150 |
/* Translators:%s expands to the error message. */ |
| 151 |
\__( 'Addon activation failed because of an error: %s.', 'wordpress-seo' ), |
| 152 |
$exception->getMessage() |
| 153 |
); |
| 154 |
} |
| 155 |
|
| 156 |
return $output; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Installs an addon. |
| 161 |
* |
| 162 |
* @param string $addon_slug The slug of the addon to install. |
| 163 |
* @param string $addon_download The download URL of the addon. |
| 164 |
* |
| 165 |
* @return array The installation success state and the output of the installation. |
| 166 |
*/ |
| 167 |
public function install_addon( $addon_slug, $addon_download ) { |
| 168 |
$installed = false; |
| 169 |
$output = []; |
| 170 |
|
| 171 |
try { |
| 172 |
$installed = $this->addon_install_action->install_addon( $addon_slug, $addon_download ); |
| 173 |
} catch ( Addon_Already_Installed_Exception $exception ) { |
| 174 |
/* Translators: %s expands to the name of the addon. */ |
| 175 |
$output[] = \__( 'Addon installed.', 'wordpress-seo' ); |
| 176 |
|
| 177 |
$installed = true; |
| 178 |
} catch ( User_Cannot_Install_Plugins_Exception $exception ) { |
| 179 |
$output[] = \__( 'You are not allowed to install plugins.', 'wordpress-seo' ); |
| 180 |
} catch ( Addon_Installation_Error_Exception $exception ) { |
| 181 |
$output[] = \sprintf( |
| 182 |
/* Translators: %s expands to the error message. */ |
| 183 |
\__( 'Addon installation failed because of an error: %s.', 'wordpress-seo' ), |
| 184 |
$exception->getMessage() |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
return [ $installed, $output ]; |
| 189 |
} |
| 190 |
} |
| 191 |
|