Completed
Push — revert-5405-update/module-plac... ( 39c88e...712e0c )
by
unknown
21:19 queued 12:30
created

Jetpack_SEO_Posts::get_post_custom_description()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 1
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class containing utility static methods for managing SEO custom descriptions for Posts and Pages.
5
 */
6
class Jetpack_SEO_Posts {
7
	/**
8
	 * Key of the post meta value that will be used to store post custom description.
9
	 */
10
	const DESCRIPTION_META_KEY = 'advanced_seo_description';
11
12
	/**
13
	 * Build meta description for post SEO.
14
	 *
15
	 * @param WP_Post $post Source of data for custom description.
16
	 *
17
	 * @return string Post description or empty string.
18
	 */
19
	public static function get_post_description( $post ) {
20
		if ( empty( $post ) ) {
21
			return '';
22
		}
23
24
		if ( post_password_required() || ! is_singular() ) {
25
			return '';
26
		}
27
28
		// Business users can overwrite the description
29
		$custom_description = self::get_post_custom_description( $post );
30
31
		if ( ! empty( $custom_description ) ) {
32
			return $custom_description;
33
		}
34
35
		if ( ! empty( $post->post_excerpt ) ) {
36
			return $post->post_excerpt;
37
		}
38
39
		return $post->post_content;
40
	}
41
42
	/**
43
	 * Returns post's custom meta description if it is set, and if
44
	 * SEO tools are enabled for current blog.
45
	 *
46
	 * @param WP_Post $post Source of data for custom description
47
	 *
48
	 * @return string Custom description or empty string
49
	 */
50
	public static function get_post_custom_description( $post ) {
51
		if ( empty( $post ) ) {
52
			return '';
53
		}
54
55
		$custom_description = get_post_meta( $post->ID, self::DESCRIPTION_META_KEY, true );
56
57
		if ( empty( $custom_description ) || ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() ) {
58
			return '';
59
		}
60
61
		return $custom_description;
62
	}
63
}
64