| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Actions\Addon_Installation; |
| 4 |
|
| 5 |
use WPSEO_Addon_Manager; |
| 6 |
use Yoast\WP\SEO\Exceptions\Addon_Installation\Addon_Activation_Error_Exception; |
| 7 |
use Yoast\WP\SEO\Exceptions\Addon_Installation\User_Cannot_Activate_Plugins_Exception; |
| 8 |
use Yoast\WP\SEO\Helpers\Require_File_Helper; |
| 9 |
|
| 10 |
/** |
| 11 |
* Represents the endpoint for activating a specific Yoast Plugin on WordPress. |
| 12 |
*/ |
| 13 |
class Addon_Activate_Action { |
| 14 |
|
| 15 |
/** |
| 16 |
* The addon manager. |
| 17 |
* |
| 18 |
* @var WPSEO_Addon_Manager |
| 19 |
*/ |
| 20 |
protected $addon_manager; |
| 21 |
|
| 22 |
/** |
| 23 |
* The require file helper. |
| 24 |
* |
| 25 |
* @var Require_File_Helper |
| 26 |
*/ |
| 27 |
protected $require_file_helper; |
| 28 |
|
| 29 |
/** |
| 30 |
* Addon_Activate_Action constructor. |
| 31 |
* |
| 32 |
* @param WPSEO_Addon_Manager $addon_manager The addon manager. |
| 33 |
* @param Require_File_Helper $require_file_helper A file helper. |
| 34 |
*/ |
| 35 |
public function __construct( |
| 36 |
WPSEO_Addon_Manager $addon_manager, |
| 37 |
Require_File_Helper $require_file_helper |
| 38 |
) { |
| 39 |
$this->addon_manager = $addon_manager; |
| 40 |
$this->require_file_helper = $require_file_helper; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Activates the plugin based on the given plugin file. |
| 45 |
* |
| 46 |
* @param string $plugin_slug The plugin slug to get download url for. |
| 47 |
* |
| 48 |
* @return bool True when activation is successful. |
| 49 |
* |
| 50 |
* @throws Addon_Activation_Error_Exception Exception when the activation encounters an error. |
| 51 |
* @throws User_Cannot_Activate_Plugins_Exception Exception when the user is not allowed to activate. |
| 52 |
*/ |
| 53 |
public function activate_addon( $plugin_slug ) { |
| 54 |
if ( ! \current_user_can( 'activate_plugins' ) ) { |
| 55 |
throw new User_Cannot_Activate_Plugins_Exception(); |
| 56 |
} |
| 57 |
|
| 58 |
if ( $this->addon_manager->is_installed( $plugin_slug ) ) { |
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
$this->load_wordpress_classes(); |
| 63 |
|
| 64 |
$plugin_file = $this->addon_manager->get_plugin_file( $plugin_slug ); |
| 65 |
$activation_result = \activate_plugin( $plugin_file ); |
| 66 |
|
| 67 |
if ( $activation_result !== null && \is_wp_error( $activation_result ) ) { |
| 68 |
throw new Addon_Activation_Error_Exception( $activation_result->get_error_message() ); |
| 69 |
} |
| 70 |
|
| 71 |
return true; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Requires the files needed from WordPress itself. |
| 76 |
* |
| 77 |
* @codeCoverageIgnore Only loads a WordPress file. |
| 78 |
* |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
protected function load_wordpress_classes() { |
| 82 |
if ( ! \function_exists( 'get_plugins' ) ) { |
| 83 |
$this->require_file_helper->require_file_once( \ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|