Completed
Push — fix/conflicting-seo-plugins ( 153691 )
by
unknown
11:44
created

Jetpack_SEO_Utils::is_enabled_jetpack_seo()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class containing utility static methods that other SEO tools are relying on.
5
 */
6
class Jetpack_SEO_Utils {
7
	/**
8
	 * Site option name used to store front page meta description.
9
	 */
10
	const FRONT_PAGE_META_OPTION = 'advanced_seo_front_page_description';
11
12
	/**
13
	 * Old version of option name that was previously used under Free plan.
14
	 */
15
	const GRANDFATHERED_META_OPTION = 'seo_meta_description';
16
17
	/**
18
	 * Used to check whether SEO tools are enabled for given site.
19
	 *
20
	 * @param int $site_id Optional. Defaults to current blog id if not given.
21
	 *
22
	 * @return bool True if SEO tools are enabled, false otherwise.
23
	 */
24
	public static function is_enabled_jetpack_seo( $site_id = 0 ) {
25
		/**
26
		 * Can be used by SEO plugin authors to disable the conflicting output of SEO Tools.
27
		 *
28
		 * @module seo-tools
29
		 *
30
		 * @since 4.9.0
31
		 *
32
		 * @param bool True if SEO Tools should be disabled, false otherwise.
33
		 */
34
		if ( apply_filters( 'jetpack_disable_seo_tools', false ) ) {
35
			return false;
36
		}
37
38
		if ( function_exists( 'has_blog_sticker' ) ) {
39
			// For WPCOM sites
40
			if ( empty( $site_id ) ) {
41
				$site_id = get_current_blog_id();
42
			}
43
44
			return has_blog_sticker( 'business-plan', $site_id );
45
		}
46
47
		// For all Jetpack sites
48
		return true;
49
	}
50
51
	/**
52
	 * Checks if this option was set while it was still available under free plan.
53
	 *
54
	 * @return bool True if we should enable grandfathering, false otherwise.
55
	 */
56
	public static function has_grandfathered_front_page_meta() {
57
		return ! self::is_enabled_jetpack_seo() && get_option( self::GRANDFATHERED_META_OPTION );
58
	}
59
60
	/**
61
	 * Returns front page meta description for current site.
62
	 *
63
	 * Since we allowed non-business users to set Front page meta description for some time,
64
	 * before bundling it with other SEO tools features that require a business plan,
65
	 * we are supporting grandfathering here.
66
	 *
67
	 * @return string Front page meta description string or empty string.
68
	 */
69
	public static function get_front_page_meta_description() {
70
		if ( self::is_enabled_jetpack_seo() ) {
71
			$front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
72
			return  $front_page_meta ? $front_page_meta : get_option( self::GRANDFATHERED_META_OPTION, '' );
73
		}
74
75
		// Support grandfathering for non-business users.
76
		return get_option( self::GRANDFATHERED_META_OPTION, '' );
77
	}
78
79
	/**
80
	 * Updates the site option value for front page meta description.
81
	 *
82
	 * We are taking care to update the correct option, in case the value is grandfathered for current site.
83
	 *
84
	 * @param $value string New value for front page meta description.
85
	 *
86
	 * @return string Saved value, or empty string if no update was performed.
87
	 */
88
	public static function update_front_page_meta_description( $value ) {
89
		$front_page_description = sanitize_text_field( $value );
90
91
		/**
92
		 * Can be used to limit the lenght of front page meta description.
93
		 *
94
		 * @module seo-tools
95
		 *
96
		 * @since 4.4.0
97
		 *
98
		 * @param int Maximum length of front page meta description. Defaults to 300.
99
		 */
100
		$description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
101
102
		if ( function_exists( 'mb_substr' ) ) {
103
			$front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
104
		} else {
105
			$front_page_description = substr( $front_page_description, 0, $description_max_length );
106
		}
107
108
		$can_set_meta = self::is_enabled_jetpack_seo();
109
		$grandfathered_meta_option = get_option( self::GRANDFATHERED_META_OPTION );
110
		$has_old_meta = ! empty( $grandfathered_meta_option );
111
		$option_name = self::has_grandfathered_front_page_meta() ? self::GRANDFATHERED_META_OPTION : self::FRONT_PAGE_META_OPTION;
112
113
		$did_update = update_option( $option_name, $front_page_description );
114
115
		if ( $did_update && $has_old_meta && $can_set_meta ) {
116
			// Delete grandfathered option if user has switched to Business plan and updated meta description.
117
			delete_option( 'seo_meta_description' );
118
		}
119
120
		if ( $did_update ) {
121
			return $front_page_description;
122
		}
123
124
		return '';
125
	}
126
}
127