Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
45.45% covered (danger)
45.45%
15 / 33
42.86% covered (danger)
42.86%
3 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_SEO_Utils
45.45% covered (danger)
45.45%
15 / 33
42.86% covered (danger)
42.86%
3 / 7
108.85
0.00% covered (danger)
0.00%
0 / 1
 is_enabled_jetpack_seo
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 has_legacy_front_page_meta
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 get_viewed_posts_page
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
4
 get_front_page_meta_description
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 sanitize_front_page_meta_description
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 update_front_page_meta_description
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
56
 remove_query_blocks
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2/**
3 * Class containing utility static methods that other SEO tools are relying on.
4 *
5 * @package automattic/jetpack
6 */
7
8/**
9 * Class containing utility static methods that other SEO tools are relying on.
10 */
11class Jetpack_SEO_Utils {
12    /**
13     * Site option name used to store front page meta description.
14     */
15    const FRONT_PAGE_META_OPTION = 'advanced_seo_front_page_description';
16
17    /**
18     * The LEGACY_META_OPTION is used to support legacy usage on WPcom simple sites (free or paid).
19     * For WPorg JP sites, the JP seo-tools features were made free for all sites (free or paid).
20     */
21    const LEGACY_META_OPTION = 'seo_meta_description';
22
23    /**
24     * Used to check whether SEO tools are enabled for given site.
25     *
26     * @return bool True if SEO tools are enabled, false otherwise.
27     */
28    public static function is_enabled_jetpack_seo() {
29        /**
30         * Can be used by SEO plugin authors to disable the conflicting output of SEO Tools.
31         *
32         * @module seo-tools
33         *
34         * @since 5.0.0
35         *
36         * @param bool True if SEO Tools should be disabled, false otherwise.
37         */
38        if ( apply_filters( 'jetpack_disable_seo_tools', false ) ) {
39            return false;
40        }
41
42        if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
43            return wpcom_site_has_feature( 'advanced-seo', get_current_blog_id() );
44        }
45
46        return true;
47    }
48
49    /**
50     * Checks if this option was set while it was freely available to all WPcom simple sites.
51     *
52     * @return bool True if we should enable legacy usage, false otherwise.
53     */
54    public static function has_legacy_front_page_meta() {
55        return ! self::is_enabled_jetpack_seo() && get_option( self::LEGACY_META_OPTION );
56    }
57
58    /**
59     * Gets the page set as the posts page in Settings > Reading, when that page is being viewed.
60     *
61     * Use it there instead of get_post(), which returns the first listed post.
62     *
63     * @return WP_Post|null The published posts page, or null on any other view.
64     */
65    public static function get_viewed_posts_page() {
66        global $wp_query;
67
68        if ( empty( $wp_query->is_posts_page ) ) {
69            return null;
70        }
71
72        $page = get_queried_object();
73
74        // WordPress still lists posts at an unpublished page's URL, and that page's text must stay private.
75        return $page instanceof WP_Post && 'publish' === $page->post_status ? $page : null;
76    }
77
78    /**
79     * Returns front page meta description for current site.
80     *
81     * @return string Front page meta description string or empty string.
82     */
83    public static function get_front_page_meta_description() {
84        if ( self::is_enabled_jetpack_seo() ) {
85            $front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
86            return $front_page_meta ? $front_page_meta : get_option( self::LEGACY_META_OPTION, '' );
87        }
88
89        // Support legacy usage for WPcom simple sites.
90        return get_option( self::LEGACY_META_OPTION, '' );
91    }
92
93    /**
94     * Sanitizes the custom front page meta description input.
95     *
96     * @param string $value Front page meta string.
97     *
98     * @return string The sanitized string.
99     */
100    public static function sanitize_front_page_meta_description( $value ) {
101        return wp_strip_all_tags( $value );
102    }
103
104    /**
105     * Updates the site option value for front page meta description.
106     *
107     * @param string $value New value for front page meta description.
108     *
109     * @return string Saved value, or empty string if no update was performed.
110     */
111    public static function update_front_page_meta_description( $value ) {
112        $front_page_description = self::sanitize_front_page_meta_description( $value );
113
114        /**
115         * Can be used to limit the length of front page meta description.
116         *
117         * @module seo-tools
118         *
119         * @since 4.4.0
120         *
121         * @param int Maximum length of front page meta description. Defaults to 300.
122         */
123        $description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
124
125        if ( function_exists( 'mb_substr' ) ) {
126            $front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
127        } else {
128            $front_page_description = substr( $front_page_description, 0, $description_max_length );
129        }
130
131        $can_set_meta       = self::is_enabled_jetpack_seo();
132        $legacy_meta_option = get_option( self::LEGACY_META_OPTION );
133        $has_old_meta       = ! empty( $legacy_meta_option );
134        $option_name        = self::has_legacy_front_page_meta() ? self::LEGACY_META_OPTION : self::FRONT_PAGE_META_OPTION;
135
136        $did_update = update_option( $option_name, $front_page_description );
137
138        if ( $did_update && $has_old_meta && $can_set_meta ) {
139            // Delete legacy option if user has switched to Business or eCommerce plan and updated the front page meta description.
140            delete_option( self::LEGACY_META_OPTION );
141        }
142
143        if ( $did_update ) {
144            return $front_page_description;
145        }
146
147        return '';
148    }
149
150    /**
151     * Remove content within wp:query blocks.
152     *
153     * @uses jetpack_og_remove_query_blocks
154     *
155     * @since 14.9
156     *
157     * @param string $content Post content.
158     *
159     * @return string Post content stripped from wp:query blocks.
160     */
161    public static function remove_query_blocks( $content ) {
162        if ( ! function_exists( 'jetpack_og_remove_query_blocks' ) ) {
163            return $content;
164        }
165
166        return jetpack_og_remove_query_blocks( $content );
167    }
168}