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 / task-list / application / tasks / improve-default-meta-descriptions.php

improve-default-meta-descriptions.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.6, at src/task-list/application/tasks/improve-default-meta-descriptions.php

181 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3 namespace Yoast\WP\SEO\Task_List\Application\Tasks;
4
5 use Yoast\WP\SEO\Helpers\Indexable_Helper;
6 use Yoast\WP\SEO\Helpers\Options_Helper;
7 use Yoast\WP\SEO\Task_List\Application\Tasks\Child_Tasks\Improve_Default_Meta_Descriptions_Child;
8 use Yoast\WP\SEO\Task_List\Domain\Components\Call_To_Action_Entry;
9 use Yoast\WP\SEO\Task_List\Domain\Components\Copy_Set;
10 use Yoast\WP\SEO\Task_List\Domain\Tasks\Abstract_Post_Type_Parent_Task;
11 use Yoast\WP\SEO\Task_List\Domain\Tasks\Child_Task_Interface;
12 use Yoast\WP\SEO\Task_List\Infrastructure\Indexables\Recent_Content_Indexable_Collector;
13
14 /**
15 * Represents the task for setting custom meta descriptions on recent content.
16 */
17 class Improve_Default_Meta_Descriptions extends Abstract_Post_Type_Parent_Task {
18
19 use Recent_Content_Task_Trait;
20
21 /**
22 * The default maximum number of content items to retrieve.
23 *
24 * @var int
25 */
26 public const DEFAULT_LIMIT = 100;
27
28 /**
29 * Holds the id.
30 *
31 * @var string
32 */
33 protected $id = 'improve-default-meta-descriptions';
34
35 /**
36 * Holds the priority.
37 *
38 * @var string
39 */
40 protected $priority = 'medium';
41
42 /**
43 * Holds the recent content indexable collector.
44 *
45 * @var Recent_Content_Indexable_Collector
46 */
47 private $recent_content_indexable_collector;
48
49 /**
50 * Holds the indexable helper.
51 *
52 * @var Indexable_Helper
53 */
54 private $indexable_helper;
55
56 /**
57 * Holds the options helper.
58 *
59 * @var Options_Helper
60 */
61 private $options_helper;
62
63 /**
64 * Constructs the task.
65 *
66 * @param Recent_Content_Indexable_Collector $recent_content_indexable_collector The recent content indexable collector.
67 * @param Indexable_Helper $indexable_helper The indexable helper.
68 * @param Options_Helper $options_helper The options helper.
69 */
70 public function __construct(
71 Recent_Content_Indexable_Collector $recent_content_indexable_collector,
72 Indexable_Helper $indexable_helper,
73 Options_Helper $options_helper
74 ) {
75 $this->recent_content_indexable_collector = $recent_content_indexable_collector;
76 $this->indexable_helper = $indexable_helper;
77 $this->options_helper = $options_helper;
78 }
79
80 /**
81 * Returns the task's link.
82 *
83 * @return string|null
84 */
85 public function get_link(): ?string {
86 return null;
87 }
88
89 /**
90 * Returns the task's call to action entry.
91 *
92 * @return Call_To_Action_Entry|null
93 */
94 public function get_call_to_action(): ?Call_To_Action_Entry {
95 return null;
96 }
97
98 /**
99 * Returns the task's copy set.
100 *
101 * @return Copy_Set
102 */
103 public function get_copy_set(): Copy_Set {
104 $post_type = \get_post_type_object( $this->get_post_type() );
105
106 return new Copy_Set(
107 /* translators: %1$s expands to the post type label this task is about */
108 \sprintf( \__( 'Improve default meta descriptions of your recent content: %1$s', 'wordpress-seo' ), $post_type->label ),
109 \sprintf(
110 /* translators: %1$s expands to <p>, %2$s expands to </p>, %3$s expands to <p>, %4$s expands to </p>, %5$s expands to <strong>, %6$s expands to </strong>, %7$s expands to <strong> and %8$s expands to </strong>. */
111 \__( '%1$sDefault meta descriptions don\'t always highlight what makes your page unique. Write your own to improve clarity and drive more clicks.%2$s%3$sShort on time? In %5$sYoast SEO Premium%6$s, use %7$sAI Generate%8$s to create tailored meta descriptions in seconds.%4$s', 'wordpress-seo' ),
112 '<p>',
113 '</p>',
114 '<p>',
115 '</p>',
116 '<strong>',
117 '</strong>',
118 '<strong>',
119 '</strong>',
120 ),
121 );
122 }
123
124 /**
125 * Populates the child tasks by querying content modified in the last two months.
126 *
127 * @return Child_Task_Interface[]
128 */
129 public function populate_child_tasks(): array {
130 // @TODO: There's a lot of code duplication that can be abstracted in all the parent tasks so far.
131 $post_type = $this->get_post_type();
132
133 if ( empty( $post_type ) ) {
134 return [];
135 }
136
137 $two_months_ago = $this->get_recency_timestamp();
138
139 $recent_content_items = $this->recent_content_indexable_collector->get_recent_content_for_meta_descriptions(
140 $post_type,
141 $two_months_ago,
142 self::DEFAULT_LIMIT,
143 );
144
145 $child_tasks = [];
146 foreach ( $recent_content_items as $content_item_data ) {
147 $child_tasks[] = new Improve_Default_Meta_Descriptions_Child(
148 $this,
149 $content_item_data,
150 );
151 }
152
153 return $child_tasks;
154 }
155
156 /**
157 * Returns whether the task is valid.
158 *
159 * The task is only shown when:
160 * - Indexables are being indexed.
161 * - The global meta description template for this post type is empty or contains only hardcoded text without replacevars.
162 * If it contains even one replacevar (%%...%%), descriptions can be considered customised in each post and the task is unnecessary.
163 *
164 * @return bool
165 */
166 public function is_valid(): bool {
167 if ( ! $this->indexable_helper->should_index_indexables() ) {
168 return false;
169 }
170
171 $metadesc = $this->options_helper->get( 'metadesc-' . $this->get_post_type() );
172
173 if ( empty( $metadesc ) ) {
174 return true;
175 }
176
177 // If the template contains at least one replacevar (%%...%%), the task is not valid.
178 return ! (bool) \preg_match( '/%%[^%]+%%/', $metadesc );
179 }
180 }
181