Completed
Push — renovate/node-12.x ( 02b777...439792 )
by
unknown
45:19 queued 36:16
created

update_front_page_meta_description()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 2
dl 0
loc 32
rs 9.0968
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
	 * 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
	 * Updates the site option value for front page meta description.
61
	 *
62
	 * @param string $value                     New value for front page meta description.
63
	 * @param bool   $delete_legacy_meta_option Delete the LEGACY_META_OPTION if true.
64
	 *
65
	 * @return string Saved value, or empty string if no update was performed.
66
	 */
67
	public static function update_front_page_meta_description( $value, $delete_legacy_meta_option = false ) {
68
		$front_page_description = sanitize_text_field( $value );
69
70
		/**
71
		 * Can be used to limit the length of front page meta description.
72
		 *
73
		 * @module seo-tools
74
		 *
75
		 * @since 4.4.0
76
		 *
77
		 * @param int Maximum length of front page meta description. Defaults to 300.
78
		 */
79
		$description_max_length = apply_filters( 'jetpack_seo_front_page_description_max_length', 300 );
80
81
		if ( function_exists( 'mb_substr' ) ) {
82
			$front_page_description = mb_substr( $front_page_description, 0, $description_max_length );
83
		} else {
84
			$front_page_description = substr( $front_page_description, 0, $description_max_length );
85
		}
86
87
		$did_update = update_option( self::FRONT_PAGE_META_OPTION, $front_page_description );
88
89
		if ( $delete_legacy_meta_option && $did_update ) {
90
			delete_option( 'seo_meta_description' );
91
		}
92
93
		if ( $did_update ) {
94
			return $front_page_description;
95
		}
96
97
		return '';
98
	}
99
}
100