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