PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 18.3
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v18.3
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 / dialog-integration.php

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

144 lines 3.4 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 WPSEO_Admin_Asset_Manager;
9 use Yoast\WP\SEO\Conditionals\Addon_Installation_Conditional;
10 use Yoast\WP\SEO\Conditionals\Admin\Licenses_Page_Conditional;
11 use Yoast\WP\SEO\Conditionals\Admin_Conditional;
12 use Yoast\WP\SEO\Integrations\Integration_Interface;
13
14 /**
15 * Represents the Addon installation feature.
16 */
17 class Dialog_Integration implements Integration_Interface {
18
19 /**
20 * The addon manager.
21 *
22 * @var WPSEO_Addon_Manager
23 */
24 protected $addon_manager;
25
26 /**
27 * The addons.
28 *
29 * @var array
30 */
31 protected $owned_addons;
32
33 /**
34 * {@inheritDoc}
35 */
36 public static function get_conditionals() {
37 return [
38 Admin_Conditional::class,
39 Licenses_Page_Conditional::class,
40 Addon_Installation_Conditional::class,
41 ];
42 }
43
44 /**
45 * Addon_Installation constructor.
46 *
47 * @param WPSEO_Addon_Manager $addon_manager The addon manager.
48 */
49 public function __construct( WPSEO_Addon_Manager $addon_manager ) {
50 $this->addon_manager = $addon_manager;
51 }
52
53 /**
54 * Registers all hooks to WordPress.
55 */
56 public function register_hooks() {
57 \add_action( 'admin_init', [ $this, 'start_addon_installation' ] );
58 }
59
60 /**
61 * Starts the addon installation flow.
62 *
63 * @return void
64 */
65 public function start_addon_installation() {
66 // Only show the dialog when we explicitly want to see it.
67 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reason: This is not a form.
68 if ( ! isset( $_GET['install'] ) || $_GET['install'] !== 'true' ) {
69 return;
70 }
71
72 $this->bust_myyoast_addon_information_cache();
73 $this->owned_addons = $this->get_owned_addons();
74
75 if ( \count( $this->owned_addons ) > 0 ) {
76 \add_action( 'admin_enqueue_scripts', [ $this, 'show_modal' ] );
77 }
78 else {
79 \add_action( 'admin_notices', [ $this, 'throw_no_owned_addons_warning' ] );
80 }
81 }
82
83 /**
84 * Throws a no owned addons warning.
85 *
86 * @return void
87 */
88 public function throw_no_owned_addons_warning() {
89 echo '<div class="notice notice-warning"><p>' .
90 \sprintf(
91 /* translators: %1$s expands to Yoast SEO */
92 \esc_html__(
93 'No %1$s plugins have been installed. You don\'t seem to own any active subscriptions.',
94 'wordpress-seo'
95 ),
96 'Yoast SEO'
97 ) .
98 '</p></div>';
99 }
100
101 /**
102 * Shows the modal.
103 *
104 * @return void
105 */
106 public function show_modal() {
107 \wp_localize_script(
108 WPSEO_Admin_Asset_Manager::PREFIX . 'addon-installation',
109 'wpseoAddonInstallationL10n',
110 [
111 'addons' => $this->owned_addons,
112 'nonce' => \wp_create_nonce( 'wpseo_addon_installation' ),
113 ]
114 );
115
116 $asset_manager = new WPSEO_Admin_Asset_Manager();
117 $asset_manager->enqueue_script( 'addon-installation' );
118 }
119
120 /**
121 * Retrieves a list of owned addons for the site in MyYoast.
122 *
123 * @return array List of owned addons with slug as key and name as value.
124 */
125 protected function get_owned_addons() {
126 $owned_addons = [];
127
128 foreach ( $this->addon_manager->get_myyoast_site_information()->subscriptions as $addon ) {
129 $owned_addons[] = $addon->product->name;
130 }
131
132 return $owned_addons;
133 }
134
135 /**
136 * Bust the site information transients to have fresh data.
137 *
138 * @return void
139 */
140 protected function bust_myyoast_addon_information_cache() {
141 $this->addon_manager->remove_site_information_transients();
142 }
143 }
144