PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.2
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.2
28.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 All 130 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 28.2, at src/ai-consent/user-interface/ai-consent-integration.php

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