PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.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 19.0 All 129 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.6, at src/ai/authorization/infrastructure/code-verifier-user-meta-repository.php

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