Completed
Push — add/carousel-lightbox-single-i... ( 204ac6...43c884 )
by
unknown
09:26
created

Jetpack_SEO_Utils::is_enabled_jetpack_seo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 13
rs 9.4285
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
		if ( function_exists( 'has_blog_sticker' ) ) {
26
			// For WPCOM sites
27
			if ( empty( $site_id ) ) {
28
				$site_id = get_current_blog_id();
29
			}
30
31
			return has_blog_sticker( 'unlimited-premium-themes', $site_id );
32
		}
33
34
		// For all Jetpack sites
35
		return true;
36
	}
37
38
	/**
39
	 * Checks if this option was set while it was still available under free plan.
40
	 *
41
	 * @return bool True if we should enable grandfathering, false otherwise.
42
	 */
43
	public static function has_grandfathered_front_page_meta() {
44
		return ! self::is_enabled_jetpack_seo() && get_option( self::GRANDFATHERED_META_OPTION );
45
	}
46
47
	/**
48
	 * Returns front page meta description for current site.
49
	 *
50
	 * Since we allowed non-business users to set Front page meta description for some time,
51
	 * before bundling it with other SEO tools features that require a business plan,
52
	 * we are supporting grandfathering here.
53
	 *
54
	 * @return string Front page meta description string or empty string.
55
	 */
56
	public static function get_front_page_meta_description() {
57
		if ( self::is_enabled_jetpack_seo() ) {
58
			$front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
59
			return  $front_page_meta ? $front_page_meta : get_option( self::GRANDFATHERED_META_OPTION, '' );
60
		}
61
62
		// Support grandfathering for non-business users.
63
		return get_option( self::GRANDFATHERED_META_OPTION, '' );
64
	}
65
66
	/**
67
	 * Updates the site option value for front page meta description.
68
	 *
69
	 * We are taking care to update the correct option, in case the value is grandfathered for current site.
70
	 *
71
	 * @param $value string New value for front page meta description.
72
	 *
73
	 * @return string Saved value, or empty string if no update was performed.
74
	 */
75
	public static function update_front_page_meta_description( $value ) {
76
		$front_page_description = sanitize_text_field( $value );
77
78
		/**
79
		 * Can be used to limit the lenght of front page meta description.
80
		 *
81
		 * @module seo-tools
82
		 *
83
		 * @since 4.4.0
84
		 *
85
		 * @param int Maximum length of front page meta description. Defaults to 300.
86
		 */
87
		$description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
88
89
		if ( function_exists( 'mb_substr' ) ) {
90
			$front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
91
		} else {
92
			$front_page_description = substr( $front_page_description, 0, $description_max_length );
93
		}
94
95
		$can_set_meta = self::is_enabled_jetpack_seo();
96
		$grandfathered_meta_option = get_option( self::GRANDFATHERED_META_OPTION );
97
		$has_old_meta = ! empty( $grandfathered_meta_option );
98
		$option_name = self::has_grandfathered_front_page_meta() ? self::GRANDFATHERED_META_OPTION : self::FRONT_PAGE_META_OPTION;
99
100
		$did_update = update_option( $option_name, $front_page_description );
101
102
		if ( $did_update && $has_old_meta && $can_set_meta ) {
103
			// Delete grandfathered option if user has switched to Business plan and updated meta description.
104
			delete_option( 'seo_meta_description' );
105
		}
106
107
		if ( $did_update ) {
108
			return $front_page_description;
109
		}
110
111
		return '';
112
	}
113
}
114