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

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