Completed
Push — story-block/add/more-media-opt... ( 53fda1...42cfc5 )
by
unknown
107:17 queued 96:49
created

Jetpack_SEO_Utils   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A is_enabled_jetpack_seo() 0 16 2
A get_front_page_meta_description() 0 12 3
A sanitize_front_page_meta_description() 0 3 1
A update_front_page_meta_description() 0 32 5
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
	 * Initially setting the front page meta description was for all sites, then the feature was grouped to a paid plan.
14
	 * The LEGACY_META_OPTION was added at that time to support legacy usage. Later on, a decision was made to have
15
	 * the JP seo-tools features for all JP sites (paid plan or not).
16
	 */
17
	const LEGACY_META_OPTION = 'seo_meta_description';
18
19
	/**
20
	 * Used to check whether SEO tools are enabled for given site.
21
	 *
22
	 * @return bool True if SEO tools are enabled, false otherwise.
23
	 */
24
	public static function is_enabled_jetpack_seo() {
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 5.0.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
		return true;
39
	}
40
41
	/**
42
	 * Returns front page meta description for current site.
43
	 *
44
	 * @return string Front page meta description string or empty string.
45
	 */
46
	public static function get_front_page_meta_description() {
47
		$front_page_meta = get_option( self::FRONT_PAGE_META_OPTION );
48
49
		if ( empty( $front_page_meta ) ) {
50
			$legacy_meta_option = get_option( self::LEGACY_META_OPTION );
51
			if ( ! empty( $legacy_meta_option ) ) {
52
				return self::update_front_page_meta_description( $legacy_meta_option, true );
53
			}
54
		}
55
56
		return $front_page_meta;
57
	}
58
59
	/**
60
	 * Sanitizes the custom front page meta description input.
61
	 *
62
	 * @param string $value Front page meta string.
63
	 *
64
	 * @return string The sanitized string.
65
	 */
66
	public static function sanitize_front_page_meta_description( $value ) {
67
		return wp_strip_all_tags( $value );
68
	}
69
70
	/**
71
	 * Updates the site option value for front page meta description.
72
	 *
73
	 * @param string $value                     New value for front page meta description.
74
	 * @param bool   $delete_legacy_meta_option Delete the LEGACY_META_OPTION if true.
75
	 *
76
	 * @return string Saved value, or empty string if no update was performed.
77
	 */
78
	public static function update_front_page_meta_description( $value, $delete_legacy_meta_option = false ) {
79
		$front_page_description = self::sanitize_front_page_meta_description( $value );
80
81
		/**
82
		 * Can be used to limit the length of front page meta description.
83
		 *
84
		 * @module seo-tools
85
		 *
86
		 * @since 4.4.0
87
		 *
88
		 * @param int Maximum length of front page meta description. Defaults to 300.
89
		 */
90
		$description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
91
92
		if ( function_exists( 'mb_substr' ) ) {
93
			$front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
94
		} else {
95
			$front_page_description = substr( $front_page_description, 0, $description_max_length );
96
		}
97
98
		$did_update = update_option( self::FRONT_PAGE_META_OPTION, $front_page_description );
99
100
		if ( $delete_legacy_meta_option && $did_update ) {
101
			delete_option( 'seo_meta_description' );
102
		}
103
104
		if ( $did_update ) {
105
			return $front_page_description;
106
		}
107
108
		return '';
109
	}
110
}
111