PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.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 / actions / addon-installation / addon-activate-action.php

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

87 lines 2.3 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 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