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