PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
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 / helpers / post-helper.php

post-helper.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI trunk, at src/helpers/post-helper.php

226 lines 5.4 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\Helpers;
4
5 use WP_Post;
6 use Yoast\WP\SEO\Repositories\Indexable_Repository;
7
8 /**
9 * A helper object for post related things.
10 */
11 class Post_Helper {
12
13 /**
14 * Holds the String_Helper instance.
15 *
16 * @var String_Helper
17 */
18 private $string;
19
20 /**
21 * Holds the Post_Type_Helper instance.
22 *
23 * @var Post_Type_Helper
24 */
25 private $post_type;
26
27 /**
28 * Represents the indexables repository.
29 *
30 * @var Indexable_Repository
31 */
32 private $repository;
33
34 /**
35 * Post_Helper constructor.
36 *
37 * @codeCoverageIgnore It only sets dependencies.
38 *
39 * @param String_Helper $string_helper The string helper.
40 * @param Post_Type_Helper $post_type_helper The string helper.
41 */
42 public function __construct( String_Helper $string_helper, Post_Type_Helper $post_type_helper ) {
43 $this->string = $string_helper;
44 $this->post_type = $post_type_helper;
45 }
46
47 /**
48 * Sets the indexable repository. Done to avoid circular dependencies.
49 *
50 * @required
51 *
52 * @param Indexable_Repository $repository The indexable repository.
53 *
54 * @return void
55 */
56 public function set_indexable_repository( Indexable_Repository $repository ) {
57 $this->repository = $repository;
58 }
59
60 /**
61 * Removes all shortcode tags from the given content.
62 *
63 * @codeCoverageIgnore It only wraps a WordPress function.
64 *
65 * @param string $content Content to remove all the shortcode tags from.
66 *
67 * @return string Content without shortcode tags.
68 */
69 public function strip_shortcodes( $content ) {
70 return \strip_shortcodes( $content );
71 }
72
73 /**
74 * Retrieves the post excerpt (without tags).
75 *
76 * @codeCoverageIgnore It only wraps another helper method.
77 *
78 * @param int $post_id Post ID.
79 *
80 * @return string Post excerpt (without tags).
81 */
82 public function get_the_excerpt( $post_id ) {
83 return $this->string->strip_all_tags( \get_the_excerpt( $post_id ) );
84 }
85
86 /**
87 * Retrieves the post type of the current post.
88 *
89 * @codeCoverageIgnore It only wraps a WordPress function.
90 *
91 * @param WP_Post|null $post The post.
92 *
93 * @return string|false Post type on success, false on failure.
94 */
95 public function get_post_type( $post = null ) {
96 return \get_post_type( $post );
97 }
98
99 /**
100 * Retrieves the post title with fallback to `No title`.
101 *
102 * @param int $post_id Optional. Post ID.
103 *
104 * @return string The post title with fallback to `No title`.
105 */
106 public function get_post_title_with_fallback( $post_id = 0 ) {
107 $post_title = \get_the_title( $post_id );
108 if ( $post_title ) {
109 return $post_title;
110 }
111
112 return \__( 'No title', 'wordpress-seo' );
113 }
114
115 /**
116 * Retrieves post data given a post ID.
117 *
118 * @codeCoverageIgnore It wraps a WordPress function.
119 *
120 * @param int $post_id Post ID.
121 *
122 * @return WP_Post|null The post.
123 */
124 public function get_post( $post_id ) {
125 return \get_post( $post_id );
126 }
127
128 /**
129 * Updates the has_public_posts field on attachments for a post_parent.
130 *
131 * An attachment is represented by their post parent when:
132 * - The attachment has a post parent.
133 * - The attachment inherits the post status.
134 *
135 * @codeCoverageIgnore It relies too much on dependencies.
136 *
137 * @param int $post_parent Post ID.
138 * @param int $has_public_posts Whether the parent is public.
139 *
140 * @return bool Whether the update was successful.
141 */
142 public function update_has_public_posts_on_attachments( $post_parent, $has_public_posts ) {
143 $query = $this->repository->query()
144 ->select( 'id' )
145 ->where( 'object_type', 'post' )
146 ->where( 'object_sub_type', 'attachment' )
147 ->where( 'post_status', 'inherit' )
148 ->where( 'post_parent', $post_parent );
149
150 if ( $has_public_posts !== null ) {
151 $query->where_raw( '( has_public_posts IS NULL OR has_public_posts <> %s )', [ $has_public_posts ] );
152 }
153 else {
154 $query->where_not_null( 'has_public_posts' );
155 }
156 $results = $query->find_array();
157
158 if ( empty( $results ) ) {
159 return true;
160 }
161
162 $updated = $this->repository->query()
163 ->set( 'has_public_posts', $has_public_posts )
164 ->where_id_in( \wp_list_pluck( $results, 'id' ) )
165 ->update_many();
166
167 return $updated !== false;
168 }
169
170 /**
171 * Determines if the post can be indexed.
172 *
173 * @param int $post_id Post ID to check.
174 *
175 * @return bool True if the post can be indexed.
176 */
177 public function is_post_indexable( $post_id ) {
178 // Don't index posts which are not public (i.e. viewable).
179 $post_type = \get_post_type( $post_id );
180
181 if ( ! $this->post_type->is_of_indexable_post_type( $post_type ) ) {
182 return false;
183 }
184
185 // Don't index excluded post statuses.
186 if ( \in_array( \get_post_status( $post_id ), $this->get_excluded_post_statuses(), true ) ) {
187 return false;
188 }
189
190 // Don't index revisions of posts.
191 if ( \wp_is_post_revision( $post_id ) ) {
192 return false;
193 }
194
195 // Don't index autosaves that are not caught by the auto-draft check.
196 if ( \wp_is_post_autosave( $post_id ) ) {
197 return false;
198 }
199
200 return true;
201 }
202
203 /**
204 * Retrieves the list of excluded post statuses.
205 *
206 * @return array The excluded post statuses.
207 */
208 public function get_excluded_post_statuses() {
209 return [ 'auto-draft' ];
210 }
211
212 /**
213 * Retrieves the list of public posts statuses.
214 *
215 * @return array The public post statuses.
216 */
217 public function get_public_post_statuses() {
218 /**
219 * Filter: 'wpseo_public_post_statuses' - List of public post statuses.
220 *
221 * @param array $post_statuses Post status list, defaults to array( 'publish' ).
222 */
223 return \apply_filters( 'wpseo_public_post_statuses', [ 'publish' ] );
224 }
225 }
226