PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.7
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.7
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-authorization / infrastructure / code-verifier-user-meta-repository.php

code-verifier-user-meta-repository.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.7, at src/ai-authorization/infrastructure/code-verifier-user-meta-repository.php

96 lines 2.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_Authorization\Infrastructure;
4
5 use RuntimeException;
6 use Yoast\WP\SEO\AI_Authorization\Domain\Code_Verifier;
7 use Yoast\WP\SEO\Helpers\Date_Helper;
8 use Yoast\WP\SEO\Helpers\User_Helper;
9
10 /**
11 * Class Code_Verifier_Repository
12 */
13 class Code_Verifier_User_Meta_Repository implements Code_Verifier_User_Meta_Repository_Interface {
14
15 private const CODE_VERIFIER_VALIDITY = 300; // 5 minutes
16
17 /**
18 * The date helper.
19 *
20 * @var Date_Helper
21 */
22 private $date_helper;
23
24 /**
25 * The user helper.
26 *
27 * @var User_Helper
28 */
29 private $user_helper;
30
31 /**
32 * Code_Verifier_Repository constructor.
33 *
34 * @param Date_Helper $date_helper The date helper.
35 * @param User_Helper $user_helper The user helper.
36 */
37 public function __construct( Date_Helper $date_helper, User_Helper $user_helper ) {
38 $this->date_helper = $date_helper;
39 $this->user_helper = $user_helper;
40 }
41
42 /**
43 * Store the verification code for a user.
44 *
45 * @param int $user_id The user ID.
46 * @param string $code The code verifier.
47 * @param int $created_at The time the code was created.
48 *
49 * @return void
50 */
51 public function store_code_verifier( int $user_id, string $code, int $created_at ): void {
52 $this->user_helper->update_meta(
53 $user_id,
54 'yoast_wpseo_ai_generator_code_verifier_for_blog_' . \get_current_blog_id(),
55 [
56 'code' => $code,
57 'created_at' => $created_at,
58 ],
59 );
60 }
61
62 /**
63 * Get the verification code for a user.
64 *
65 * @param int $user_id The user ID.
66 *
67 * @throws RuntimeException If the code verifier is not found or has expired.
68 * @return Code_Verifier The verification code or null if not found.
69 */
70 public function get_code_verifier( int $user_id ): ?Code_Verifier {
71 $data = $this->user_helper->get_meta( $user_id, 'yoast_wpseo_ai_generator_code_verifier_for_blog_' . \get_current_blog_id(), true );
72
73 if ( ! \is_array( $data ) || ! isset( $data['code'] ) || $data['code'] === '' ) {
74 throw new RuntimeException( 'Unable to retrieve the verification code.' );
75 }
76
77 if ( ! isset( $data['created_at'] ) || $data['created_at'] < ( $this->date_helper->current_time() - self::CODE_VERIFIER_VALIDITY ) ) {
78 $this->delete_code_verifier( $user_id );
79 throw new RuntimeException( 'Code verifier has expired.' );
80 }
81
82 return new Code_Verifier( $data['code'], $data['created_at'] );
83 }
84
85 /**
86 * Delete the verification code for a user.
87 *
88 * @param int $user_id The user ID.
89 *
90 * @return void
91 */
92 public function delete_code_verifier( int $user_id ): void {
93 $this->user_helper->delete_meta( $user_id, 'yoast_wpseo_ai_generator_code_verifier_for_blog_' . \get_current_blog_id() );
94 }
95 }
96