Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
3.45% covered (danger)
3.45%
1 / 29
16.67% covered (danger)
16.67%
1 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_SEO_Utils
3.45% covered (danger)
3.45%
1 / 29
16.67% covered (danger)
16.67%
1 / 6
343.93
0.00% covered (danger)
0.00%
0 / 1
 is_enabled_jetpack_seo
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
20
 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
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 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
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
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     * Returns front page meta description for current site.
60     *
61     * @return string Front page meta description string or empty string.
62     */
63    public static function get_front_page_meta_description() {
64        if ( self::is_enabled_jetpack_seo() ) {
65            $front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
66            return $front_page_meta ? $front_page_meta : get_option( self::LEGACY_META_OPTION, '' );
67        }
68
69        // Support legacy usage for WPcom simple sites.
70        return get_option( self::LEGACY_META_OPTION, '' );
71    }
72
73    /**
74     * Sanitizes the custom front page meta description input.
75     *
76     * @param string $value Front page meta string.
77     *
78     * @return string The sanitized string.
79     */
80    public static function sanitize_front_page_meta_description( $value ) {
81        return wp_strip_all_tags( $value );
82    }
83
84    /**
85     * Updates the site option value for front page meta description.
86     *
87     * @param string $value New value for front page meta description.
88     *
89     * @return string Saved value, or empty string if no update was performed.
90     */
91    public static function update_front_page_meta_description( $value ) {
92        $front_page_description = self::sanitize_front_page_meta_description( $value );
93
94        /**
95         * Can be used to limit the length of front page meta description.
96         *
97         * @module seo-tools
98         *
99         * @since 4.4.0
100         *
101         * @param int Maximum length of front page meta description. Defaults to 300.
102         */
103        $description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
104
105        if ( function_exists( 'mb_substr' ) ) {
106            $front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
107        } else {
108            $front_page_description = substr( $front_page_description, 0, $description_max_length );
109        }
110
111        $can_set_meta       = self::is_enabled_jetpack_seo();
112        $legacy_meta_option = get_option( self::LEGACY_META_OPTION );
113        $has_old_meta       = ! empty( $legacy_meta_option );
114        $option_name        = self::has_legacy_front_page_meta() ? self::LEGACY_META_OPTION : self::FRONT_PAGE_META_OPTION;
115
116        $did_update = update_option( $option_name, $front_page_description );
117
118        if ( $did_update && $has_old_meta && $can_set_meta ) {
119            // Delete legacy option if user has switched to Business or eCommerce plan and updated the front page meta description.
120            delete_option( self::LEGACY_META_OPTION );
121        }
122
123        if ( $did_update ) {
124            return $front_page_description;
125        }
126
127        return '';
128    }
129
130    /**
131     * Remove content within wp:query blocks.
132     *
133     * @uses jetpack_og_remove_query_blocks
134     *
135     * @since 14.9
136     *
137     * @param string $content Post content.
138     *
139     * @return string Post content stripped from wp:query blocks.
140     */
141    public static function remove_query_blocks( $content ) {
142        if ( ! function_exists( 'jetpack_og_remove_query_blocks' ) ) {
143            return $content;
144        }
145
146        return jetpack_og_remove_query_blocks( $content );
147    }
148}