| 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\User_Profile_Conditional; |
| 10 |
use Yoast\WP\SEO\Helpers\Short_Link_Helper; |
| 11 |
use Yoast\WP\SEO\Helpers\User_Helper; |
| 12 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 13 |
|
| 14 |
/** |
| 15 |
* Ai_Consent_Integration class. |
| 16 |
*/ |
| 17 |
class Ai_Consent_Integration implements Integration_Interface { |
| 18 |
|
| 19 |
/** |
| 20 |
* Represents the admin asset manager. |
| 21 |
* |
| 22 |
* @var WPSEO_Admin_Asset_Manager |
| 23 |
*/ |
| 24 |
private $asset_manager; |
| 25 |
|
| 26 |
/** |
| 27 |
* Represents the user helper. |
| 28 |
* |
| 29 |
* @var User_Helper |
| 30 |
*/ |
| 31 |
private $user_helper; |
| 32 |
|
| 33 |
/** |
| 34 |
* The short link helper. |
| 35 |
* |
| 36 |
* @var Short_Link_Helper |
| 37 |
*/ |
| 38 |
protected $short_link_helper; |
| 39 |
|
| 40 |
/** |
| 41 |
* The endpoints repository. |
| 42 |
* |
| 43 |
* @var Consent_Endpoints_Repository |
| 44 |
*/ |
| 45 |
protected $endpoints_repository; |
| 46 |
|
| 47 |
/** |
| 48 |
* Returns the conditionals based in which this loadable should be active. |
| 49 |
* |
| 50 |
* @return array<string> |
| 51 |
*/ |
| 52 |
public static function get_conditionals(): array { |
| 53 |
return [ User_Profile_Conditional::class ]; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructs the class. |
| 58 |
* |
| 59 |
* @param WPSEO_Admin_Asset_Manager $asset_manager The admin asset manager. |
| 60 |
* @param User_Helper $user_helper The user helper. |
| 61 |
* @param Short_Link_Helper $short_link_helper The short link helper. |
| 62 |
* @param Consent_Endpoints_Repository $endpoints_repository The endpoints repository. |
| 63 |
*/ |
| 64 |
public function __construct( |
| 65 |
WPSEO_Admin_Asset_Manager $asset_manager, |
| 66 |
User_Helper $user_helper, |
| 67 |
Short_Link_Helper $short_link_helper, |
| 68 |
Consent_Endpoints_Repository $endpoints_repository |
| 69 |
) { |
| 70 |
$this->asset_manager = $asset_manager; |
| 71 |
$this->user_helper = $user_helper; |
| 72 |
$this->short_link_helper = $short_link_helper; |
| 73 |
$this->endpoints_repository = $endpoints_repository; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Initializes the integration. |
| 78 |
* |
| 79 |
* This is the place to register hooks and filters. |
| 80 |
* |
| 81 |
* @return void |
| 82 |
*/ |
| 83 |
public function register_hooks() { |
| 84 |
// Hide AI feature option in user profile if the user is not allowed to use it. |
| 85 |
if ( \current_user_can( 'edit_posts' ) ) { |
| 86 |
\add_action( 'wpseo_user_profile_additions', [ $this, 'render_user_profile' ], 12 ); |
| 87 |
} |
| 88 |
\add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ], 11 ); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Returns the script data for the AI consent button. |
| 93 |
* |
| 94 |
* @return array<string, string|bool> |
| 95 |
*/ |
| 96 |
public function get_script_data(): array { |
| 97 |
return [ |
| 98 |
'hasConsent' => $this->user_helper->get_meta( $this->user_helper->get_current_user_id(), '_yoast_wpseo_ai_consent', true ), |
| 99 |
'pluginUrl' => \plugins_url( '', \WPSEO_FILE ), |
| 100 |
'linkParams' => $this->short_link_helper->get_query_params(), |
| 101 |
'endpoints' => $this->endpoints_repository->get_all_endpoints()->to_paths_array(), |
| 102 |
'adminUrl' => \admin_url(), |
| 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 |
|