• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Yoast / wordpress-seo / aaea12fe12c7195d02e0f7ea5fe67d379d3e3227

12 Sep 2025 10:36AM UTC coverage: 41.602%. First build
aaea12fe12c7195d02e0f7ea5fe67d379d3e3227

Pull #22551

github

web-flow
Merge 67ccafc8d into 166ff8a05
Pull Request #22551: Add delayed premium upsell

2370 of 9166 branches covered (25.86%)

Branch coverage included in aggregate %.

15 of 89 new or added lines in 10 files covered. (16.85%)

22173 of 49829 relevant lines covered (44.5%)

4.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.32
/src/introductions/infrastructure/introductions-seen-repository.php
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
class Introductions_Seen_Repository {
12

13
        public const USER_META_KEY = '_yoast_wpseo_introductions';
14

15
        public const DEFAULT_VALUE = [];
16

17
        /**
18
         * Holds the User_Helper instance.
19
         *
20
         * @var User_Helper
21
         */
22
        private $user_helper;
23

24
        /**
25
         * Constructs the class.
26
         *
27
         * @param User_Helper $user_helper The User_Helper.
28
         */
29
        public function __construct( User_Helper $user_helper ) {
2✔
30
                $this->user_helper = $user_helper;
2✔
31
        }
32

33
        /**
34
         * Retrieves the introductions.
35
         *
36
         * @param int $user_id User ID.
37
         *
38
         * @return array The introductions.
39
         *
40
         * @throws Invalid_User_Id_Exception If an invalid user ID is supplied.
41
         */
42
        public function get_all_introductions( $user_id ): array {
8✔
43
                $seen_introductions = $this->user_helper->get_meta( $user_id, self::USER_META_KEY, true );
8✔
44
                if ( $seen_introductions === false ) {
8✔
45
                        throw new Invalid_User_Id_Exception();
2✔
46
                }
47

48
                if ( \is_array( $seen_introductions ) ) {
6✔
49
                        return $seen_introductions;
2✔
50
                }
51

52
                /**
53
                 * Why could $value be invalid?
54
                 * - When the database row does not exist yet, $value can be an empty string.
55
                 * - Faulty data was stored?
56
                 */
57
                return self::DEFAULT_VALUE;
4✔
58
        }
59

60
        /**
61
         * Sets the introductions.
62
         *
63
         * @param int   $user_id       The user ID.
64
         * @param array $introductions The introductions.
65
         *
66
         * @return bool True on successful update, false on failure or if the value passed to the function is the same as
67
         *              the one that is already in the database.
68
         */
69
        public function set_all_introductions( $user_id, array $introductions ): bool {
2✔
70
                return $this->user_helper->update_meta( $user_id, self::USER_META_KEY, $introductions ) !== false;
2✔
71
        }
72

73
        /**
74
         * Retrieves whether an introduction is seen.
75
         *
76
         * @param int    $user_id         User ID.
77
         * @param string $introduction_id The introduction ID.
78
         *
79
         * @return bool Whether the introduction is seen.
80
         *
81
         * @throws Invalid_User_Id_Exception If an invalid user ID is supplied.
82
         */
83
        public function is_introduction_seen( $user_id, string $introduction_id ): bool {
14✔
84
                $introductions = $this->get_all_introductions( $user_id );
14✔
85

86
                if ( \array_key_exists( $introduction_id, $introductions ) ) {
14✔
87
                        if ( \is_array( $introductions[ $introduction_id ] ) ) {
12✔
88
                                return (bool) $introductions[ $introduction_id ]['is_seen'];
12✔
89
                        }
90
                        else {
NEW
91
                                return (bool) $introductions[ $introduction_id ];
×
92
                        }
93
                }
94

95
                return false;
2✔
96
        }
97

98
        /**
99
         * Sets the introduction as seen.
100
         *
101
         * @param int    $user_id         The user ID.
102
         * @param string $introduction_id The introduction ID.
103
         * @param bool   $is_seen         Whether the introduction is seen. Defaults to true.
104
         *
105
         * @return bool False on failure. Not having to update is a success.
106
         *
107
         * @throws Invalid_User_Id_Exception If an invalid user ID is supplied.
108
         */
109
        public function set_introduction( $user_id, string $introduction_id, bool $is_seen = true ): bool {
6✔
110
                $introductions = $this->get_all_introductions( $user_id );
6✔
111

112
                // Check if the wanted value is already set.
113
                if ( \array_key_exists( $introduction_id, $introductions ) ) {
6✔
114
                        if ( \is_array( $introductions[ $introduction_id ] ) ) {
2✔
115
                                // New format with seen_on timestamp.
116
                                if ( $introductions[ $introduction_id ]['is_seen'] === $is_seen ) {
2✔
117
                                        return true;
2✔
118
                                }
119
                        }
120
                                // Old format with just a boolean.
NEW
121
                        elseif ( $introductions[ $introduction_id ] === $is_seen ) {
×
NEW
122
                                        return true;
×
123
                        }
124
                }
125

126
                // If not, set it.
127
                $introductions[ $introduction_id ] = [
4✔
128
                        'is_seen' => $is_seen,
4✔
129
                        'seen_on' => ( $is_seen === true ) ? \time() : 0,
4✔
130
                ];
4✔
131

132
                return $this->set_all_introductions( $user_id, $introductions );
4✔
133
        }
134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc