Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
37.04% covered (danger)
37.04%
10 / 27
50.00% covered (danger)
50.00%
3 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_SEO_Utils
37.04% covered (danger)
37.04%
10 / 27
50.00% covered (danger)
50.00%
3 / 6
89.14
0.00% covered (danger)
0.00%
0 / 1
 is_enabled_jetpack_seo
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 has_legacy_front_page_meta
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 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        return true;
43    }
44
45    /**
46     * Checks if this option was set while it was freely available to all WPcom simple sites.
47     *
48     * @return bool True if we should enable legacy usage, false otherwise.
49     */
50    public static function has_legacy_front_page_meta() {
51        return ! self::is_enabled_jetpack_seo() && get_option( self::LEGACY_META_OPTION );
52    }
53
54    /**
55     * Returns front page meta description for current site.
56     *
57     * @return string Front page meta description string or empty string.
58     */
59    public static function get_front_page_meta_description() {
60        if ( self::is_enabled_jetpack_seo() ) {
61            $front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
62            return $front_page_meta ? $front_page_meta : get_option( self::LEGACY_META_OPTION, '' );
63        }
64
65        // Support legacy usage for WPcom simple sites.
66        return get_option( self::LEGACY_META_OPTION, '' );
67    }
68
69    /**
70     * Sanitizes the custom front page meta description input.
71     *
72     * @param string $value Front page meta string.
73     *
74     * @return string The sanitized string.
75     */
76    public static function sanitize_front_page_meta_description( $value ) {
77        return wp_strip_all_tags( $value );
78    }
79
80    /**
81     * Updates the site option value for front page meta description.
82     *
83     * @param string $value New value for front page meta description.
84     *
85     * @return string Saved value, or empty string if no update was performed.
86     */
87    public static function update_front_page_meta_description( $value ) {
88        $front_page_description = self::sanitize_front_page_meta_description( $value );
89
90        /**
91         * Can be used to limit the length of front page meta description.
92         *
93         * @module seo-tools
94         *
95         * @since 4.4.0
96         *
97         * @param int Maximum length of front page meta description. Defaults to 300.
98         */
99        $description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
100
101        if ( function_exists( 'mb_substr' ) ) {
102            $front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
103        } else {
104            $front_page_description = substr( $front_page_description, 0, $description_max_length );
105        }
106
107        $can_set_meta       = self::is_enabled_jetpack_seo();
108        $legacy_meta_option = get_option( self::LEGACY_META_OPTION );
109        $has_old_meta       = ! empty( $legacy_meta_option );
110        $option_name        = self::has_legacy_front_page_meta() ? self::LEGACY_META_OPTION : self::FRONT_PAGE_META_OPTION;
111
112        $did_update = update_option( $option_name, $front_page_description );
113
114        if ( $did_update && $has_old_meta && $can_set_meta ) {
115            // Remove the legacy value after saving the modern description.
116            delete_option( self::LEGACY_META_OPTION );
117        }
118
119        if ( $did_update ) {
120            return $front_page_description;
121        }
122
123        return '';
124    }
125
126    /**
127     * Remove content within wp:query blocks.
128     *
129     * @uses jetpack_og_remove_query_blocks
130     *
131     * @since 14.9
132     *
133     * @param string $content Post content.
134     *
135     * @return string Post content stripped from wp:query blocks.
136     */
137    public static function remove_query_blocks( $content ) {
138        if ( ! function_exists( 'jetpack_og_remove_query_blocks' ) ) {
139            return $content;
140        }
141
142        return jetpack_og_remove_query_blocks( $content );
143    }
144}