Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
92.31% covered (success)
92.31%
12 / 13
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Seo_Tools
92.31% covered (success)
92.31%
12 / 13
80.00% covered (warning)
80.00%
4 / 5
13.08
0.00% covered (danger)
0.00%
0 / 1
 get_conflicting_plugin
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 is_disabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_enabled
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 has_legacy_front_page_meta
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_front_page_meta_description
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * SEO output availability and plugin ownership.
4 *
5 * @package automattic/jetpack-seo
6 */
7
8namespace Automattic\Jetpack\SEO;
9
10use Automattic\Jetpack\Constants;
11
12/**
13 * Separates plugin conflicts from other reasons to disable SEO output.
14 */
15class Seo_Tools {
16    /**
17     * Option holding the front page meta description.
18     *
19     * @since $$next-version$$
20     */
21    const FRONT_PAGE_META_OPTION = 'advanced_seo_front_page_description';
22
23    /**
24     * Legacy option the description is stored in on WordPress.com sites that set
25     * one while SEO tools were free for every Simple site.
26     *
27     * @since $$next-version$$
28     */
29    const LEGACY_FRONT_PAGE_META_OPTION = 'seo_meta_description';
30
31    /**
32     * Return the conflicting plugin's display name, or null when there is no conflict.
33     *
34     * @since 0.10.3
35     * @return string|null
36     */
37    public static function get_conflicting_plugin() {
38        /**
39         * Filters the name of the plugin managing SEO output instead of Jetpack.
40         *
41         * @since 0.10.3
42         * @param string|null $plugin The plugin's display name, or null for no conflict.
43         */
44        $plugin = apply_filters( 'jetpack_seo_conflicting_plugin', null );
45        return is_string( $plugin ) && '' !== trim( $plugin ) ? trim( $plugin ) : null;
46    }
47
48    /**
49     * Whether SEO output is disabled, defaulting to the plugin conflict state.
50     *
51     * @since 0.10.3
52     * @return bool
53     */
54    public static function is_disabled() {
55        /**
56         * Can be used by SEO plugin authors to disable the conflicting output of SEO Tools.
57         *
58         * @module seo-tools
59         *
60         * @since jetpack-5.0.0
61         *
62         * @param bool True if SEO Tools should be disabled, false otherwise.
63         */
64        return (bool) apply_filters( 'jetpack_disable_seo_tools', null !== self::get_conflicting_plugin() );
65    }
66
67    /**
68     * Whether Jetpack's SEO output is live for this site.
69     *
70     * @since $$next-version$$
71     * @return bool
72     */
73    public static function is_enabled() {
74        if ( self::is_disabled() ) {
75            return false;
76        }
77
78        if ( Constants::is_true( 'IS_WPCOM' ) && function_exists( 'wpcom_site_has_feature' ) ) {
79            return wpcom_site_has_feature( 'advanced-seo', get_current_blog_id() );
80        }
81
82        return true;
83    }
84
85    /**
86     * Whether the front page description still lives in the legacy option.
87     *
88     * True when SEO output is off and a description was saved while SEO tools were free on WordPress.com.
89     *
90     * @since $$next-version$$
91     * @return bool
92     */
93    public static function has_legacy_front_page_meta() {
94        return ! self::is_enabled() && (bool) get_option( self::LEGACY_FRONT_PAGE_META_OPTION );
95    }
96
97    /**
98     * The front page meta description the front end emits.
99     *
100     * @since $$next-version$$
101     * @return string
102     */
103    public static function get_front_page_meta_description() {
104        if ( self::is_enabled() ) {
105            $modern = get_option( self::FRONT_PAGE_META_OPTION );
106            return (string) ( $modern ? $modern : get_option( self::LEGACY_FRONT_PAGE_META_OPTION, '' ) );
107        }
108
109        return (string) get_option( self::LEGACY_FRONT_PAGE_META_OPTION, '' );
110    }
111}