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

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

129 lines 3.6 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\Consent\User_Interface;
6
7 use WPSEO_Admin_Asset_Manager;
8 use Yoast\WP\SEO\AI\Consent\Application\Consent_Endpoints_Repository;
9 use Yoast\WP\SEO\Conditionals\New_Premium_Or_Free_AI_Conditional;
10 use Yoast\WP\SEO\Conditionals\User_Profile_Conditional;
11 use Yoast\WP\SEO\Helpers\Short_Link_Helper;
12 use Yoast\WP\SEO\Helpers\User_Helper;
13 use Yoast\WP\SEO\Integrations\Integration_Interface;
14
15 /**
16 * Ai_Consent_Integration class.
17 */
18 class Ai_Consent_Integration implements Integration_Interface {
19
20 /**
21 * Represents the admin asset manager.
22 *
23 * @var WPSEO_Admin_Asset_Manager
24 */
25 private $asset_manager;
26
27 /**
28 * Represents the user helper.
29 *
30 * @var User_Helper
31 */
32 private $user_helper;
33
34 /**
35 * The short link helper.
36 *
37 * @var Short_Link_Helper
38 */
39 protected $short_link_helper;
40
41 /**
42 * The endpoints repository.
43 *
44 * @var Consent_Endpoints_Repository
45 */
46 protected $endpoints_repository;
47
48 /**
49 * Returns the conditionals based in which this loadable should be active.
50 *
51 * @return array<string>
52 */
53 public static function get_conditionals(): array {
54 return [ User_Profile_Conditional::class, New_Premium_Or_Free_AI_Conditional::class ];
55 }
56
57 /**
58 * Constructs the class.
59 *
60 * @param WPSEO_Admin_Asset_Manager $asset_manager The admin asset manager.
61 * @param User_Helper $user_helper The user helper.
62 * @param Short_Link_Helper $short_link_helper The short link helper.
63 * @param Consent_Endpoints_Repository $endpoints_repository The endpoints repository.
64 */
65 public function __construct(
66 WPSEO_Admin_Asset_Manager $asset_manager,
67 User_Helper $user_helper,
68 Short_Link_Helper $short_link_helper,
69 Consent_Endpoints_Repository $endpoints_repository
70 ) {
71 $this->asset_manager = $asset_manager;
72 $this->user_helper = $user_helper;
73 $this->short_link_helper = $short_link_helper;
74 $this->endpoints_repository = $endpoints_repository;
75 }
76
77 /**
78 * Initializes the integration.
79 *
80 * This is the place to register hooks and filters.
81 *
82 * @return void
83 */
84 public function register_hooks() {
85 // Hide AI feature option in user profile if the user is not allowed to use it.
86 if ( \current_user_can( 'edit_posts' ) ) {
87 \add_action( 'wpseo_user_profile_additions', [ $this, 'render_user_profile' ], 12 );
88 }
89 \add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ], 11 );
90 }
91
92 /**
93 * Returns the script data for the AI consent button.
94 *
95 * @return array<string, string|bool>
96 */
97 public function get_script_data(): array {
98 return [
99 'hasConsent' => $this->user_helper->get_meta( $this->user_helper->get_current_user_id(), '_yoast_wpseo_ai_consent', true ),
100 'pluginUrl' => \plugins_url( '', \WPSEO_FILE ),
101 'linkParams' => $this->short_link_helper->get_query_params(),
102 'endpoints' => $this->endpoints_repository->get_all_endpoints()->to_paths_array(),
103 ];
104 }
105
106 /**
107 * Enqueues the required assets.
108 *
109 * @return void
110 */
111 public function enqueue_assets() {
112 $this->asset_manager->enqueue_style( 'ai-generator' );
113 $this->asset_manager->localize_script( 'ai-consent', 'wpseoAiConsent', $this->get_script_data() );
114 $this->asset_manager->enqueue_script( 'ai-consent' );
115 }
116
117 /**
118 * Renders the AI consent button for the user profile.
119 *
120 * @return void
121 */
122 public function render_user_profile() {
123 echo '<label for="ai-generator-consent-button">',
124 \esc_html__( 'AI features', 'wordpress-seo' ),
125 '</label>',
126 '<div id="ai-generator-consent" style="display:inline-block; margin-top: 28px; padding-left:5px;"></div>';
127 }
128 }
129