PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 28.5
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v28.5
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 / application / introductions-collector.php

introductions-collector.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 28.5, at src/introductions/application/introductions-collector.php

134 lines 3.5 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\Application;
4
5 use Yoast\WP\SEO\Introductions\Domain\Introduction_Interface;
6 use Yoast\WP\SEO\Introductions\Domain\Introduction_Item;
7 use Yoast\WP\SEO\Introductions\Domain\Introductions_Bucket;
8 use Yoast\WP\SEO\Introductions\Infrastructure\Introductions_Seen_Repository;
9
10 /**
11 * Manages the collection of introductions.
12 */
13 class Introductions_Collector {
14
15 /**
16 * Holds all the introductions.
17 *
18 * @var Introduction_Interface[]
19 */
20 private $introductions;
21
22 /**
23 * Constructs the collector.
24 *
25 * @param Introduction_Interface ...$introductions All the introductions.
26 */
27 public function __construct( Introduction_Interface ...$introductions ) {
28 $this->introductions = $this->add_introductions( ...$introductions );
29 }
30
31 /**
32 * Gets the data for the introductions.
33 *
34 * @param int $user_id The user ID.
35 *
36 * @return array The list of introductions.
37 */
38 public function get_for( $user_id ) {
39 $bucket = new Introductions_Bucket();
40 $metadata = $this->get_metadata( $user_id );
41
42 foreach ( $this->introductions as $introduction ) {
43 if ( ! $introduction->should_show() ) {
44 continue;
45 }
46 if ( $this->is_seen( $introduction->get_id(), $metadata ) ) {
47 continue;
48 }
49 $bucket->add_introduction(
50 new Introduction_Item( $introduction->get_id(), $introduction->get_priority() ),
51 );
52 }
53
54 return $bucket->to_array();
55 }
56
57 /**
58 * Filters introductions with the 'wpseo_introductions' filter.
59 *
60 * @param Introduction_Interface ...$introductions The introductions.
61 *
62 * @return Introduction_Interface[]
63 */
64 private function add_introductions( Introduction_Interface ...$introductions ) {
65 /**
66 * Filter: Adds the possibility to add additional introductions to be included.
67 *
68 * @internal
69 *
70 * @param Introduction_Interface $introductions This filter expects a list of Introduction_Interface instances and
71 * expects only Introduction_Interface implementations to be added to the list.
72 */
73 $filtered_introductions = (array) \apply_filters( 'wpseo_introductions', $introductions );
74
75 return \array_filter(
76 $filtered_introductions,
77 static function ( $introduction ) {
78 return \is_a( $introduction, Introduction_Interface::class );
79 },
80 );
81 }
82
83 /**
84 * Retrieves the introductions metadata for the user.
85 *
86 * @param int $user_id The user ID.
87 *
88 * @return array The introductions' metadata.
89 */
90 public function get_metadata( $user_id ) {
91 $metadata = \get_user_meta( $user_id, Introductions_Seen_Repository::USER_META_KEY, true );
92 if ( \is_array( $metadata ) ) {
93 return $metadata;
94 }
95
96 return [];
97 }
98
99 /**
100 * Determines whether the user has seen the introduction.
101 *
102 * @param string $name The name.
103 * @param string[] $metadata The metadata.
104 *
105 * @return bool Whether the user has seen the introduction.
106 */
107 private function is_seen( $name, $metadata ) {
108 if ( \array_key_exists( $name, $metadata ) ) {
109 if ( \is_array( $metadata[ $name ] ) ) {
110 return (bool) ( $metadata[ $name ]['is_seen'] );
111 }
112 return (bool) $metadata[ $name ];
113 }
114
115 return false;
116 }
117
118 /**
119 * Checks if the given introduction ID is a known ID to the system.
120 *
121 * @param string $introduction_id The introduction ID to check.
122 *
123 * @return bool
124 */
125 public function is_available_introduction( string $introduction_id ): bool {
126 foreach ( $this->introductions as $introduction ) {
127 if ( $introduction->get_id() === $introduction_id ) {
128 return true;
129 }
130 }
131 return false;
132 }
133 }
134