Completed
Push — add/php-test-for-business-hour... ( 87d7ff...7c58d5 )
by
unknown
60:32 queued 48:54
created

Jetpack_SEO_Utils   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 0
dl 0
loc 127
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A is_enabled_jetpack_seo() 0 26 4
A has_legacy_front_page_meta() 0 3 2
A get_front_page_meta_description() 0 9 3
A sanitize_front_page_meta_description() 0 3 1
B update_front_page_meta_description() 0 38 7
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
	 * The LEGACY_META_OPTION is used to support legacy usage on WPcom simple sites (free or paid).
14
	 * For WPorg JP sites, the JP seo-tools features were made free for all sites (free or paid).
15
	 */
16
	const LEGACY_META_OPTION = 'seo_meta_description';
17
18
	/**
19
	 * Used to check whether SEO tools are enabled for given site.
20
	 *
21
	 * @param int $site_id Optional. Defaults to current blog id if not given.
22
	 *
23
	 * @return bool True if SEO tools are enabled, false otherwise.
24
	 */
25
	public static function is_enabled_jetpack_seo( $site_id = 0 ) {
26
		/**
27
		 * Can be used by SEO plugin authors to disable the conflicting output of SEO Tools.
28
		 *
29
		 * @module seo-tools
30
		 *
31
		 * @since 5.0.0
32
		 *
33
		 * @param bool True if SEO Tools should be disabled, false otherwise.
34
		 */
35
		if ( apply_filters( 'jetpack_disable_seo_tools', false ) ) {
36
			return false;
37
		}
38
39
		if ( function_exists( 'has_any_blog_stickers' ) ) {
40
			// For WPCOM simple sites.
41
			if ( empty( $site_id ) ) {
42
				$site_id = get_current_blog_id();
43
			}
44
45
			return has_any_blog_stickers( array( 'business-plan', 'ecommerce-plan' ), $site_id );
46
		}
47
48
		// For all Jetpack sites.
49
		return true;
50
	}
51
52
	/**
53
	 * Checks if this option was set while it was freely available to all WPcom simple sites.
54
	 *
55
	 * @return bool True if we should enable legacy usage, false otherwise.
56
	 */
57
	public static function has_legacy_front_page_meta() {
58
		return ! self::is_enabled_jetpack_seo() && get_option( self::LEGACY_META_OPTION );
59
	}
60
61
	/**
62
	 * Returns front page meta description for current site.
63
	 *
64
	 * @return string Front page meta description string or empty string.
65
	 */
66
	public static function get_front_page_meta_description() {
67
		if ( self::is_enabled_jetpack_seo() ) {
68
			$front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
69
			return $front_page_meta ? $front_page_meta : get_option( self::LEGACY_META_OPTION, '' );
70
		}
71
72
		// Support legacy usage for WPcom simple sites.
73
		return get_option( self::LEGACY_META_OPTION, '' );
74
	}
75
76
	/**
77
	 * Sanitizes the custom front page meta description input.
78
	 *
79
	 * @param string $value Front page meta string.
80
	 *
81
	 * @return string The sanitized string.
82
	 */
83
	public static function sanitize_front_page_meta_description( $value ) {
84
		return wp_strip_all_tags( $value );
85
	}
86
87
	/**
88
	 * Updates the site option value for front page meta description.
89
	 *
90
	 * @param string $value New value for front page meta description.
91
	 *
92
	 * @return string Saved value, or empty string if no update was performed.
93
	 */
94
	public static function update_front_page_meta_description( $value ) {
95
		$front_page_description = self::sanitize_front_page_meta_description( $value );
96
97
		/**
98
		 * Can be used to limit the length of front page meta description.
99
		 *
100
		 * @module seo-tools
101
		 *
102
		 * @since 4.4.0
103
		 *
104
		 * @param int Maximum length of front page meta description. Defaults to 300.
105
		 */
106
		$description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
107
108
		if ( function_exists( 'mb_substr' ) ) {
109
			$front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
110
		} else {
111
			$front_page_description = substr( $front_page_description, 0, $description_max_length );
112
		}
113
114
		$can_set_meta       = self::is_enabled_jetpack_seo();
115
		$legacy_meta_option = get_option( self::LEGACY_META_OPTION );
116
		$has_old_meta       = ! empty( $legacy_meta_option );
117
		$option_name        = self::has_legacy_front_page_meta() ? self::LEGACY_META_OPTION : self::FRONT_PAGE_META_OPTION;
118
119
		$did_update = update_option( $option_name, $front_page_description );
120
121
		if ( $did_update && $has_old_meta && $can_set_meta ) {
122
			// Delete legacy option if user has switched to Business or eCommerce plan and updated the front page meta description.
123
			delete_option( self::LEGACY_META_OPTION );
124
		}
125
126
		if ( $did_update ) {
127
			return $front_page_description;
128
		}
129
130
		return '';
131
	}
132
}
133