Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.10% covered (warning)
87.10%
27 / 31
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Post_Seo
87.10% covered (warning)
87.10%
27 / 31
25.00% covered (danger)
25.00%
1 / 4
18.70
0.00% covered (danger)
0.00%
0 / 1
 get_description
88.89% covered (warning)
88.89%
16 / 18
0.00% covered (danger)
0.00%
0 / 1
9.11
 get_custom_description
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
 sanitize_schema_type
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 get_schema_type
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2/**
3 * Per-post SEO fields: the description and Schema.org type a post emits.
4 *
5 * @package automattic/jetpack-seo
6 */
7
8namespace Automattic\Jetpack\SEO;
9
10use Automattic\Block_Scanner;
11use WP_Post;
12
13/**
14 * Reads a post's SEO description and schema type.
15 *
16 * @since $$next-version$$
17 */
18class Post_Seo {
19
20    /**
21     * Schema.org types the per-post schema-type meta can hold; '' means no override.
22     */
23    const ALLOWED_SCHEMA_TYPES = array( '', 'article', 'faq' );
24
25    /**
26     * Build the meta description for a post on its own singular view.
27     *
28     * @param WP_Post|int|null $post Post, post ID, or null for the global post.
29     * @return string Post description or empty string.
30     */
31    public static function get_description( $post = null ) {
32        $post = get_post( $post );
33        if ( ! ( $post instanceof WP_Post ) ) {
34            return '';
35        }
36
37        // Check the post being described, not the global one.
38        if ( post_password_required( $post ) || ! is_singular() ) {
39            return '';
40        }
41
42        $custom_description = self::get_custom_description( $post );
43        if ( ! empty( $custom_description ) ) {
44            return $custom_description;
45        }
46
47        if ( ! empty( $post->post_excerpt ) ) {
48            return $post->post_excerpt;
49        }
50
51        // The fall-through reads raw post_content, which never passes the `the_content` paywall.
52        $content = $post->post_content;
53        if ( Content_Gate::is_gated( $post ) ) {
54            $content = Content_Gate::public_teaser( $post );
55            if ( '' === $content ) {
56                return '';
57            }
58        }
59
60        // WordPress.com can load an older block-delimiter copy without remove_blocks(); the content then keeps its Query Loop blocks.
61        if ( ! method_exists( Block_Scanner::class, 'remove_blocks' ) ) {
62            return $content;
63        }
64
65        // A Query Loop block lists other posts, so its text doesn't describe this one.
66        return Block_Scanner::remove_blocks( $content, 'query' );
67    }
68
69    /**
70     * The custom meta description saved for a post, when SEO output is live.
71     *
72     * @param WP_Post|int|null $post Post, post ID, or null for the global post.
73     * @return string Custom description or empty string.
74     */
75    public static function get_custom_description( $post = null ) {
76        $post = get_post( $post );
77        if ( ! ( $post instanceof WP_Post ) ) {
78            return '';
79        }
80
81        $custom_description = get_post_meta( $post->ID, Content_Coverage::META_DESCRIPTION, true );
82
83        if ( empty( $custom_description ) || ! Seo_Tools::is_enabled() ) {
84            return '';
85        }
86
87        return $custom_description;
88    }
89
90    /**
91     * Sanitize a schema type to the allowed list; unknown values become ''.
92     *
93     * @param mixed $value The submitted value.
94     * @return string A value from self::ALLOWED_SCHEMA_TYPES.
95     */
96    public static function sanitize_schema_type( $value ) {
97        $value = is_string( $value ) ? sanitize_key( $value ) : '';
98        return in_array( $value, self::ALLOWED_SCHEMA_TYPES, true ) ? $value : '';
99    }
100
101    /**
102     * Get the per-post schema-type override, if any.
103     *
104     * @param WP_Post|int|null $post Post, post ID, or null for the global post.
105     * @return string A value from self::ALLOWED_SCHEMA_TYPES ('' = no override).
106     */
107    public static function get_schema_type( $post = null ) {
108        $post = get_post( $post );
109        if ( ! ( $post instanceof WP_Post ) ) {
110            return '';
111        }
112
113        return self::sanitize_schema_type( (string) get_post_meta( $post->ID, Content_Coverage::META_SCHEMA_TYPE, true ) );
114    }
115}