Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.95% |
68 / 81 |
|
66.67% |
6 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Jetpack_SEO_Posts | |
83.95% |
68 / 81 |
|
66.67% |
6 / 9 |
18.19 | |
0.00% |
0 / 1 |
| get_post_description | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_post_custom_description | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_post_custom_html_title | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| get_post_noindex_setting | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
4.37 | |||
| exclude_noindex_posts_from_jetpack_sitemap | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| register_post_meta | |
100.00% |
45 / 45 |
|
100.00% |
1 / 1 |
1 | |||
| sanitize_schema_type | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_post_schema_type | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_post_seo_coverage | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Class containing utility static methods for managing SEO options for Posts and Pages. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | use Automattic\Jetpack\SEO\Post_Seo; |
| 9 | |
| 10 | /** |
| 11 | * Provides static utility methods for managing SEO options for Posts and Pages. |
| 12 | * |
| 13 | * The SEO package owns the logic of the methods that forward to it. |
| 14 | */ |
| 15 | class Jetpack_SEO_Posts { |
| 16 | /** |
| 17 | * Key of the post meta values that will be used to store post custom data. |
| 18 | */ |
| 19 | const DESCRIPTION_META_KEY = 'advanced_seo_description'; |
| 20 | const HTML_TITLE_META_KEY = 'jetpack_seo_html_title'; |
| 21 | const NOINDEX_META_KEY = 'jetpack_seo_noindex'; |
| 22 | const SCHEMA_TYPE_META_KEY = 'jetpack_seo_schema_type'; |
| 23 | const POST_META_KEYS_ARRAY = array( |
| 24 | self::DESCRIPTION_META_KEY, |
| 25 | self::HTML_TITLE_META_KEY, |
| 26 | self::NOINDEX_META_KEY, |
| 27 | self::SCHEMA_TYPE_META_KEY, |
| 28 | ); |
| 29 | |
| 30 | /** |
| 31 | * Allowed Schema.org types that can be stored in the per-post schema-type |
| 32 | * meta. Empty string means "no override" — Schema_Builder picks a sensible |
| 33 | * default for the post. Keep in sync with Post_Seo::ALLOWED_SCHEMA_TYPES in |
| 34 | * the SEO package, which is what Schema_Builder reads. |
| 35 | */ |
| 36 | const ALLOWED_SCHEMA_TYPES = array( '', 'article', 'faq' ); |
| 37 | |
| 38 | /** |
| 39 | * Build meta description for post SEO. |
| 40 | * |
| 41 | * @param WP_Post|null $post Source of data for custom description. |
| 42 | * |
| 43 | * @return string Post description or empty string. |
| 44 | */ |
| 45 | public static function get_post_description( $post = null ) { |
| 46 | return Post_Seo::get_description( $post ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Returns post's custom meta description if it is set, and if |
| 51 | * SEO tools are enabled for current blog. |
| 52 | * |
| 53 | * @param WP_Post|null $post Source of data for custom description. |
| 54 | * |
| 55 | * @return string Custom description or empty string |
| 56 | */ |
| 57 | public static function get_post_custom_description( $post = null ) { |
| 58 | return Post_Seo::get_custom_description( $post ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Gets a custom HTML title for a post if one is set, and if |
| 63 | * SEO tools are enabled for the current blog. |
| 64 | * |
| 65 | * @param WP_Post|null $post Source of data for the custom HTML title. |
| 66 | * |
| 67 | * @return string Custom HTML title or an empty string if not set. |
| 68 | */ |
| 69 | public static function get_post_custom_html_title( $post = null ) { |
| 70 | $post = get_post( $post ); |
| 71 | if ( ! ( $post instanceof WP_Post ) ) { |
| 72 | return ''; |
| 73 | } |
| 74 | |
| 75 | $custom_html_title = get_post_meta( $post->ID, self::HTML_TITLE_META_KEY, true ); |
| 76 | |
| 77 | if ( empty( $custom_html_title ) || ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) { |
| 78 | return ''; |
| 79 | } |
| 80 | |
| 81 | return $custom_html_title; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Gets the `jetpack_seo_noindex` setting for a post, if |
| 86 | * SEO tools are enabled for the current blog. |
| 87 | * |
| 88 | * @param WP_Post|null $post Provided post or defaults to the global post. |
| 89 | * |
| 90 | * @return bool True if post should be marked as noindex, false otherwise. |
| 91 | */ |
| 92 | public static function get_post_noindex_setting( $post = null ) { |
| 93 | $post = get_post( $post ); |
| 94 | if ( ! ( $post instanceof WP_Post ) ) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | $mark_as_noindex = get_post_meta( $post->ID, self::NOINDEX_META_KEY, true ); |
| 99 | |
| 100 | if ( empty( $mark_as_noindex ) || ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | return (bool) $mark_as_noindex; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Filter callback for `jetpack_sitemap_skip_post`; if a post has `jetpack_seo_noindex` set to true, |
| 109 | * then exclude that post from the Jetpack sitemap. |
| 110 | * |
| 111 | * @param bool $skip Whether to skip the post in the sitemap. |
| 112 | * @param WP_Post $post The post to check. |
| 113 | * |
| 114 | * @return bool |
| 115 | */ |
| 116 | public static function exclude_noindex_posts_from_jetpack_sitemap( $skip, $post ) { |
| 117 | $exclude = self::get_post_noindex_setting( $post ); |
| 118 | if ( $exclude ) { |
| 119 | $skip = true; |
| 120 | } |
| 121 | return $skip; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Registers the SEO post meta keys for use in the REST API: |
| 126 | * - self::DESCRIPTION_META_KEY |
| 127 | * - self::HTML_TITLE_META_KEY |
| 128 | * - self::NOINDEX_META_KEY |
| 129 | * - self::SCHEMA_TYPE_META_KEY |
| 130 | */ |
| 131 | public static function register_post_meta() { |
| 132 | $description_args = array( |
| 133 | 'type' => 'string', |
| 134 | 'description' => __( 'Custom post description to be used in HTML <meta /> tag.', 'jetpack' ), |
| 135 | 'single' => true, |
| 136 | 'default' => '', |
| 137 | 'show_in_rest' => array( |
| 138 | 'name' => self::DESCRIPTION_META_KEY, |
| 139 | ), |
| 140 | ); |
| 141 | |
| 142 | $html_title_args = array( |
| 143 | 'type' => 'string', |
| 144 | 'description' => __( 'Custom title to be used in HTML <title /> tag.', 'jetpack' ), |
| 145 | 'single' => true, |
| 146 | 'default' => '', |
| 147 | 'show_in_rest' => array( |
| 148 | 'name' => self::HTML_TITLE_META_KEY, |
| 149 | ), |
| 150 | ); |
| 151 | |
| 152 | $noindex_args = array( |
| 153 | 'type' => 'boolean', |
| 154 | 'description' => __( 'Whether to hide the post from search engines and the Jetpack sitemap.', 'jetpack' ), |
| 155 | 'single' => true, |
| 156 | 'default' => false, |
| 157 | 'show_in_rest' => array( |
| 158 | 'name' => self::NOINDEX_META_KEY, |
| 159 | ), |
| 160 | ); |
| 161 | |
| 162 | $schema_type_args = array( |
| 163 | 'type' => 'string', |
| 164 | 'description' => __( 'Schema.org type to emit as JSON-LD for this post.', 'jetpack' ), |
| 165 | 'single' => true, |
| 166 | 'default' => '', |
| 167 | 'sanitize_callback' => array( __CLASS__, 'sanitize_schema_type' ), |
| 168 | 'show_in_rest' => array( |
| 169 | 'name' => self::SCHEMA_TYPE_META_KEY, |
| 170 | // Enum so core REST rejects an unknown schema type with a proper |
| 171 | // rest_invalid_param error; the sanitize_callback is the |
| 172 | // defense-in-depth fallback for non-REST writes. |
| 173 | 'schema' => array( |
| 174 | 'type' => 'string', |
| 175 | 'enum' => self::ALLOWED_SCHEMA_TYPES, |
| 176 | ), |
| 177 | ), |
| 178 | ); |
| 179 | |
| 180 | register_meta( 'post', self::DESCRIPTION_META_KEY, $description_args ); |
| 181 | register_meta( 'post', self::HTML_TITLE_META_KEY, $html_title_args ); |
| 182 | register_meta( 'post', self::NOINDEX_META_KEY, $noindex_args ); |
| 183 | register_meta( 'post', self::SCHEMA_TYPE_META_KEY, $schema_type_args ); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Sanitize a schema type to the allowed list. Unknown values become '' |
| 188 | * (no override) rather than erroring, so a non-REST write can't store junk. |
| 189 | * |
| 190 | * @param string $value The submitted value. |
| 191 | * @return string A value from self::ALLOWED_SCHEMA_TYPES. |
| 192 | */ |
| 193 | public static function sanitize_schema_type( $value ) { |
| 194 | return Post_Seo::sanitize_schema_type( $value ); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Get the per-post schema-type override, if any. |
| 199 | * |
| 200 | * @param WP_Post|int|null $post Post or post ID. |
| 201 | * @return string A value from self::ALLOWED_SCHEMA_TYPES ('' = no override). |
| 202 | */ |
| 203 | public static function get_post_schema_type( $post = null ) { |
| 204 | return Post_Seo::get_schema_type( $post ); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Factual per-post SEO field coverage — presence/state only, never a score. |
| 209 | * |
| 210 | * Single source of truth shared by the Content tab, the edit.php columns, |
| 211 | * and the Overview coverage card so the three never drift. Reports whether |
| 212 | * each field has been *set*, independent of whether SEO tools are currently |
| 213 | * active (this is an authoring/audit view, not front-end emission). |
| 214 | * |
| 215 | * @param WP_Post|int|null $post Post or post ID. |
| 216 | * @return array{has_custom_title:bool,has_description:bool,has_schema_type:bool,noindex:bool} |
| 217 | */ |
| 218 | public static function get_post_seo_coverage( $post = null ) { |
| 219 | $post = get_post( $post ); |
| 220 | if ( ! ( $post instanceof WP_Post ) ) { |
| 221 | return array( |
| 222 | 'has_custom_title' => false, |
| 223 | 'has_description' => false, |
| 224 | 'has_schema_type' => false, |
| 225 | 'noindex' => false, |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | return array( |
| 230 | 'has_custom_title' => '' !== (string) get_post_meta( $post->ID, self::HTML_TITLE_META_KEY, true ), |
| 231 | 'has_description' => '' !== (string) get_post_meta( $post->ID, self::DESCRIPTION_META_KEY, true ), |
| 232 | 'has_schema_type' => '' !== self::get_post_schema_type( $post ), |
| 233 | 'noindex' => (bool) get_post_meta( $post->ID, self::NOINDEX_META_KEY, true ), |
| 234 | ); |
| 235 | } |
| 236 | } |