PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.6
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 / ai / generator / user-interface / ai-generator-integration.php

ai-generator-integration.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.6, at src/ai/generator/user-interface/ai-generator-integration.php

205 lines 6.7 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 -- Needed in the folder structure.
4
5 namespace Yoast\WP\SEO\AI\Generator\User_Interface;
6
7 use WPSEO_Addon_Manager;
8 use WPSEO_Admin_Asset_Manager;
9 use Yoast\WP\SEO\AI\Consent\Application\Consent_Endpoints_Repository;
10 use Yoast\WP\SEO\AI\Free_Sparks\Application\Free_Sparks_Endpoints_Repository;
11 use Yoast\WP\SEO\AI\Generator\Application\Generator_Endpoints_Repository;
12 use Yoast\WP\SEO\AI\HTTP_Request\Infrastructure\API_Client;
13 use Yoast\WP\SEO\Conditionals\AI_Conditional;
14 use Yoast\WP\SEO\Conditionals\AI_Editor_Conditional;
15 use Yoast\WP\SEO\Helpers\Current_Page_Helper;
16 use Yoast\WP\SEO\Helpers\Options_Helper;
17 use Yoast\WP\SEO\Helpers\User_Helper;
18 use Yoast\WP\SEO\Integrations\Integration_Interface;
19 use Yoast\WP\SEO\Introductions\Application\Ai_Fix_Assessments_Upsell;
20 use Yoast\WP\SEO\Introductions\Infrastructure\Introductions_Seen_Repository;
21
22 /**
23 * Ai_Generator_Integration class.
24 */
25 class Ai_Generator_Integration implements Integration_Interface {
26
27 /**
28 * Represents the admin asset manager.
29 *
30 * @var WPSEO_Admin_Asset_Manager
31 */
32 private $asset_manager;
33
34 /**
35 * Represents the add-on manager.
36 *
37 * @var WPSEO_Addon_Manager
38 */
39 private $addon_manager;
40
41 /**
42 * Holds the API client instance.
43 *
44 * @var API_Client
45 */
46 private $api_client;
47
48 /**
49 * Represents the current page helper.
50 *
51 * @var Current_Page_Helper
52 */
53 private $current_page_helper;
54
55 /**
56 * Represents the options manager.
57 *
58 * @var Options_Helper
59 */
60 private $options_helper;
61
62 /**
63 * Represents the user helper.
64 *
65 * @var User_Helper
66 */
67 private $user_helper;
68
69 /**
70 * Represents the introductions seen repository.
71 *
72 * @var Introductions_Seen_Repository
73 */
74 private $introductions_seen_repository;
75
76 /**
77 * Represents the endpoints repository.
78 *
79 * @var Generator_Endpoints_Repository
80 */
81 private $generator_endpoints_repository;
82
83 /**
84 * Represents the consent endpoints repository.
85 *
86 * @var Consent_Endpoints_Repository
87 */
88 private $consent_endpoints_repository;
89
90 /**
91 * Represents the free sparks endpoints repository.
92 *
93 * @var Free_Sparks_Endpoints_Repository
94 */
95 private $free_sparks_endpoints_repository;
96
97 /**
98 * Returns the conditionals based in which this loadable should be active.
99 *
100 * @return array<string>
101 */
102 public static function get_conditionals() {
103 return [ AI_Conditional::class, AI_Editor_Conditional::class ];
104 }
105
106 /**
107 * Constructs the class.
108 *
109 * @param WPSEO_Admin_Asset_Manager $asset_manager The admin asset manager.
110 * @param WPSEO_Addon_Manager $addon_manager The addon manager.
111 * @param API_Client $api_client The API client.
112 * @param Current_Page_Helper $current_page_helper The current page helper.
113 * @param Options_Helper $options_helper The options helper.
114 * @param User_Helper $user_helper The user helper.
115 * @param Introductions_Seen_Repository $introductions_seen_repository The introductions seen repository.
116 * @param Generator_Endpoints_Repository $generator_endpoints_repository The Generator endpoints repository.
117 * @param Consent_Endpoints_Repository $consent_endpoints_repository The Consent endpoints repository.
118 * @param Free_Sparks_Endpoints_Repository $free_sparks_endpoints_repository The Free Sparks endpoints repository.
119 */
120 public function __construct(
121 WPSEO_Admin_Asset_Manager $asset_manager,
122 WPSEO_Addon_Manager $addon_manager,
123 API_Client $api_client,
124 Current_Page_Helper $current_page_helper,
125 Options_Helper $options_helper,
126 User_Helper $user_helper,
127 Introductions_Seen_Repository $introductions_seen_repository,
128 Generator_Endpoints_Repository $generator_endpoints_repository,
129 Consent_Endpoints_Repository $consent_endpoints_repository,
130 Free_Sparks_Endpoints_Repository $free_sparks_endpoints_repository
131 ) {
132 $this->asset_manager = $asset_manager;
133 $this->addon_manager = $addon_manager;
134 $this->api_client = $api_client;
135 $this->current_page_helper = $current_page_helper;
136 $this->options_helper = $options_helper;
137 $this->user_helper = $user_helper;
138 $this->introductions_seen_repository = $introductions_seen_repository;
139 $this->generator_endpoints_repository = $generator_endpoints_repository;
140 $this->consent_endpoints_repository = $consent_endpoints_repository;
141 $this->free_sparks_endpoints_repository = $free_sparks_endpoints_repository;
142 }
143
144 /**
145 * Initializes the integration.
146 *
147 * This is the place to register hooks and filters.
148 *
149 * @return void
150 */
151 public function register_hooks() {
152 \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
153 // Enqueue after Elementor_Premium integration, which re-registers the assets.
154 \add_action( 'elementor/editor/before_enqueue_scripts', [ $this, 'enqueue_assets' ], 11 );
155 }
156
157 /**
158 * Gets the subscription status for Yoast SEO Premium and Yoast WooCommerce SEO.
159 *
160 * @return array<string, bool>
161 */
162 public function get_product_subscriptions() {
163 return [
164 'premiumSubscription' => $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::PREMIUM_SLUG ),
165 'wooCommerceSubscription' => $this->addon_manager->has_valid_subscription( WPSEO_Addon_Manager::WOOCOMMERCE_SLUG ),
166 ];
167 }
168
169 /**
170 * Returns the data that should be passed to the script.
171 *
172 * @return array<string|array<string>>
173 */
174 public function get_script_data() {
175 $user_id = $this->user_helper->get_current_user_id();
176
177 $endpoints = $this->generator_endpoints_repository->get_all_endpoints()
178 ->merge_with(
179 $this->consent_endpoints_repository->get_all_endpoints(),
180 )->merge_with(
181 $this->free_sparks_endpoints_repository->get_all_endpoints(),
182 )->to_paths_array();
183 return [
184 'hasConsent' => $this->user_helper->get_meta( $user_id, '_yoast_wpseo_ai_consent', true ),
185 'productSubscriptions' => $this->get_product_subscriptions(),
186 'hasSeenIntroduction' => $this->introductions_seen_repository->is_introduction_seen( $user_id, AI_Fix_Assessments_Upsell::ID ),
187 'requestTimeout' => $this->api_client->get_request_timeout(),
188 'isFreeSparks' => $this->options_helper->get( 'ai_free_sparks_started_on', null ) !== null,
189 'endpoints' => $endpoints,
190 ];
191 }
192
193 /**
194 * Enqueues the required assets.
195 *
196 * @return void
197 */
198 public function enqueue_assets() {
199
200 $this->asset_manager->enqueue_script( 'ai-generator' );
201 $this->asset_manager->localize_script( 'ai-generator', 'wpseoAiGenerator', $this->get_script_data() );
202 $this->asset_manager->enqueue_style( 'ai-generator' );
203 }
204 }
205