PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.4
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.4
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 / introductions / infrastructure / introductions-seen-repository.php

introductions-seen-repository.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.4, at src/introductions/infrastructure/introductions-seen-repository.php

137 lines 3.8 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\Introductions\Infrastructure;
4
5 use Yoast\WP\SEO\Helpers\User_Helper;
6 use Yoast\WP\SEO\Introductions\Domain\Invalid_User_Id_Exception;
7
8 /**
9 * Stores and retrieves whether the user has seen certain introductions.
10 *
11 * @makePublic
12 */
13 class Introductions_Seen_Repository {
14
15 public const USER_META_KEY = '_yoast_wpseo_introductions';
16
17 public const DEFAULT_VALUE = [];
18
19 /**
20 * Holds the User_Helper instance.
21 *
22 * @var User_Helper
23 */
24 private $user_helper;
25
26 /**
27 * Constructs the class.
28 *
29 * @param User_Helper $user_helper The User_Helper.
30 */
31 public function __construct( User_Helper $user_helper ) {
32 $this->user_helper = $user_helper;
33 }
34
35 /**
36 * Retrieves the introductions.
37 *
38 * @param int $user_id User ID.
39 *
40 * @return array The introductions.
41 *
42 * @throws Invalid_User_Id_Exception If an invalid user ID is supplied.
43 */
44 public function get_all_introductions( $user_id ): array {
45 $seen_introductions = $this->user_helper->get_meta( $user_id, self::USER_META_KEY, true );
46 if ( $seen_introductions === false ) {
47 throw new Invalid_User_Id_Exception();
48 }
49
50 if ( \is_array( $seen_introductions ) ) {
51 return $seen_introductions;
52 }
53
54 /**
55 * Why could $value be invalid?
56 * - When the database row does not exist yet, $value can be an empty string.
57 * - Faulty data was stored?
58 */
59 return self::DEFAULT_VALUE;
60 }
61
62 /**
63 * Sets the introductions.
64 *
65 * @param int $user_id The user ID.
66 * @param array $introductions The introductions.
67 *
68 * @return bool True on successful update, false on failure or if the value passed to the function is the same as
69 * the one that is already in the database.
70 */
71 public function set_all_introductions( $user_id, array $introductions ): bool {
72 return $this->user_helper->update_meta( $user_id, self::USER_META_KEY, $introductions ) !== false;
73 }
74
75 /**
76 * Retrieves whether an introduction is seen.
77 *
78 * @param int $user_id User ID.
79 * @param string $introduction_id The introduction ID.
80 *
81 * @return bool Whether the introduction is seen.
82 *
83 * @throws Invalid_User_Id_Exception If an invalid user ID is supplied.
84 */
85 public function is_introduction_seen( $user_id, string $introduction_id ): bool {
86 $introductions = $this->get_all_introductions( $user_id );
87
88 if ( \array_key_exists( $introduction_id, $introductions ) ) {
89 if ( \is_array( $introductions[ $introduction_id ] ) ) {
90 return (bool) $introductions[ $introduction_id ]['is_seen'];
91 }
92 else {
93 return (bool) $introductions[ $introduction_id ];
94 }
95 }
96
97 return false;
98 }
99
100 /**
101 * Sets the introduction as seen.
102 *
103 * @param int $user_id The user ID.
104 * @param string $introduction_id The introduction ID.
105 * @param bool $is_seen Whether the introduction is seen. Defaults to true.
106 *
107 * @return bool False on failure. Not having to update is a success.
108 *
109 * @throws Invalid_User_Id_Exception If an invalid user ID is supplied.
110 */
111 public function set_introduction( $user_id, string $introduction_id, bool $is_seen = true ): bool {
112 $introductions = $this->get_all_introductions( $user_id );
113
114 // Check if the wanted value is already set.
115 if ( \array_key_exists( $introduction_id, $introductions ) ) {
116 if ( \is_array( $introductions[ $introduction_id ] ) ) {
117 // New format with seen_on timestamp.
118 if ( $introductions[ $introduction_id ]['is_seen'] === $is_seen ) {
119 return true;
120 }
121 }
122 // Old format with just a boolean.
123 elseif ( $introductions[ $introduction_id ] === $is_seen ) {
124 return true;
125 }
126 }
127
128 // If not, set it.
129 $introductions[ $introduction_id ] = [
130 'is_seen' => $is_seen,
131 'seen_on' => ( $is_seen === true ) ? \time() : 0,
132 ];
133
134 return $this->set_all_introductions( $user_id, $introductions );
135 }
136 }
137