PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.1
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.1
16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 All 504 releases
← All changes | modules/seo-tools/class-jetpack-seo.php +58 -11 12.0.3 → 16.3-a.1 View file →
@@ -6,8 +6,10 @@
6 6 */
7 7
8 8 /**
9 9 * An SEO expert walks into a bar, bars, pub, public house, Irish pub, drinks, beer, wine, liquor, Grey Goose, Cristal...
10 + *
11 + * @phan-constructor-used-for-side-effects
10 12 */
11 13 class Jetpack_SEO {
12 14 /**
13 15 * Constructor.
@@ -36,9 +38,9 @@
36 38 add_post_type_support( 'page', 'excerpt' );
37 39 }
38 40
39 41 /**
40 - * Can be used to prevent SEO tools form modifying site titles.
42 + * Can be used to prevent SEO tools from modifying site titles.
41 43 *
42 44 * @module seo-tools
43 45 *
44 46 * @since 4.4.0
@@ -54,14 +56,46 @@
54 56 }
55 57
56 58 add_filter( 'jetpack_open_graph_tags', array( $this, 'set_custom_og_tags' ) );
57 59 Jetpack_SEO_Posts::register_post_meta();
58 - Jetpack_SEO_Posts::register_gutenberg_extension();
59 60 // Exclude posts with 'jetpack_seo_noindex' set true from the Jetpack sitemap.
60 61 add_filter( 'jetpack_sitemap_skip_post', array( 'Jetpack_SEO_Posts', 'exclude_noindex_posts_from_jetpack_sitemap' ), 10, 2 );
62 + add_action( 'rest_api_init', array( $this, 'add_custom_field_post_type_meta' ) );
61 63 }
62 64
63 65 /**
66 + * Add custom field meta to all public post types that don't already have it.
67 + */
68 + public function add_custom_field_post_type_meta() {
69 + /**
70 + * Filter the list of post types for which custom fields support is added.
71 + *
72 + * This filter allows modification of the post types that will be processed
73 + * to add support for custom fields if they do not already support it.
74 + *
75 + * @since 14.2
76 + *
77 + * @param array $post_types An array of post type names.
78 + */
79 + $post_types = apply_filters(
80 + 'jetpack_seo_custom_field_post_types',
81 + get_post_types(
82 + array(
83 + 'public' => true,
84 + 'show_ui' => true,
85 + '_builtin' => false,
86 + )
87 + )
88 + );
89 +
90 + foreach ( $post_types as $post_type ) {
91 + if ( ! post_type_supports( $post_type, 'custom-fields' ) ) {
92 + add_post_type_support( $post_type, 'custom-fields' );
93 + }
94 + }
95 + }
96 +
97 + /**
64 98 * Helper method to fetch authors.
65 99 */
66 100 private function get_authors() {
67 101 global $wp_query;
@@ -68,8 +102,11 @@
68 102
69 103 $authors = array();
70 104
71 105 foreach ( $wp_query->posts as $post ) {
106 + if ( ! $post instanceof WP_Post ) {
107 + continue;
108 + }
72 109 $authors[] = get_the_author_meta( 'display_name', (int) $post->post_author );
73 110 }
74 111
75 112 $authors = array_unique( $authors );
@@ -89,9 +126,14 @@
89 126 if ( ! empty( $custom_title ) ) {
90 127 $tags['og:title'] = $custom_title;
91 128 }
92 129
93 - $post_custom_description = Jetpack_SEO_Posts::get_post_custom_description( get_post() );
130 + // On archives and a latest-posts homepage, get_post() returns the first post in
131 + // the loop, whose description does not represent the page being viewed. Only use
132 + // the per-post custom description when we are actually on that singular post/page.
133 + $post_custom_description = is_singular()
134 + ? Jetpack_SEO_Posts::get_post_custom_description( get_post() )
135 + : '';
94 136 $front_page_meta = Jetpack_SEO_Utils::get_front_page_meta_description();
95 137
96 138 if ( class_exists( 'woocommerce' ) && is_shop() ) {
97 139 $shop_page_id = get_option( 'woocommerce_shop_page_id' );
@@ -114,11 +156,12 @@
114 156 */
115 157 public function meta_tags() {
116 158 global $wp_query;
117 159
118 - $period = '';
119 - $template = '';
120 - $meta = array();
160 + $post_count = is_countable( $wp_query->posts ) ? count( $wp_query->posts ) : 0;
161 + $period = '';
162 + $template = '';
163 + $meta = array();
121 164
122 165 /**
123 166 * Can be used to specify a list of themes that set their own meta tags.
124 167 *
@@ -160,9 +203,9 @@
160 203
161 204 $meta['description'] = sprintf(
162 205 /* translators: first property is an user's display name, the second is the site's title. */
163 206 _x( 'Read all of the posts by %1$s on %2$s', 'Read all of the posts by Author Name on Blog Title', 'jetpack' ),
164 - isset( $obj->display_name ) ? $obj->display_name : __( 'the author', 'jetpack' ),
207 + $obj->display_name ?? __( 'the author', 'jetpack' ),
165 208 get_bloginfo( 'title' )
166 209 );
167 210 } elseif ( is_tag() || is_category() || is_tax() ) {
168 211 $obj = get_queried_object();
@@ -191,9 +234,9 @@
191 234 /* translators: %1$s: Number of posts published. %2$l: Post author. %3$s: A year date. */
192 235 $template = _nx(
193 236 '%1$s post published by %2$l in the year %3$s', // Singular.
194 237 '%1$s posts published by %2$l in the year %3$s', // Plural.
195 - count( $wp_query->posts ), // Number.
238 + $post_count, // Number.
196 239 '10 posts published by John in the year 2012', // Context.
197 240 'jetpack'
198 241 );
199 242 } elseif ( is_month() ) {
@@ -202,9 +245,9 @@
202 245 /* translators: %1$s: Number of posts published. %2$l: Post author. %3$s: A month/year date. */
203 246 $template = _nx(
204 247 '%1$s post published by %2$l during %3$s', // Singular.
205 248 '%1$s posts published by %2$l during %3$s', // Plural.
206 - count( $wp_query->posts ), // Number.
249 + $post_count, // Number.
207 250 '10 posts publishes by John during May 2012', // Context.
208 251 'jetpack'
209 252 );
210 253 } elseif ( is_day() ) {
@@ -216,9 +259,9 @@
216 259 /* translators: %1$s: Number of posts published. %2$l: Post author. %3$s: A month/day/year date. */
217 260 $template = _nx(
218 261 '%1$s post published by %2$l on %3$s', // Singular.
219 262 '%1$s posts published by %2$l on %3$s', // Plural.
220 - count( $wp_query->posts ), // Number.
263 + $post_count, // Number.
221 264 '10 posts published by John on May 30, 2012', // Context.
222 265 'jetpack'
223 266 );
224 267 }
@@ -223,9 +266,9 @@
223 266 );
224 267 }
225 268
226 269 $authors = $this->get_authors();
227 - $meta['description'] = wp_sprintf( $template, count( $wp_query->posts ), $authors, $period );
270 + $meta['description'] = wp_sprintf( $template, $post_count, $authors, $period );
228 271 }
229 272
230 273 $mark_as_noindex = Jetpack_SEO_Posts::get_post_noindex_setting( get_post() );
231 274 if ( $mark_as_noindex ) {
@@ -241,8 +284,12 @@
241 284 *
242 285 * @param array Array that consists of meta name and meta content pairs.
243 286 */
244 287 $meta = apply_filters( 'jetpack_seo_meta_tags', $meta );
288 +
289 + if ( ! is_array( $meta ) ) {
290 + return;
291 + }
245 292
246 293 // Output them.
247 294 foreach ( $meta as $name => $content ) {
248 295 if ( ! empty( $content ) ) {