Completed
Push — fix/build-module-headings ( f0325a...a37c2f )
by
unknown
160:08 queued 152:46
created

blog-display.php ➔ jetpack_the_content_customizer()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 15
Code Lines 11

Duplication

Lines 7
Ratio 46.67 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 6
nop 1
dl 7
loc 15
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * The functions to display Content or Excerpt in a theme.
4
 */
5
6
/**
7
 * If the theme doesn't support 'jetpack-content-options', don't continue.
8
 */
9
if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
10
	return;
11
}
12
13
/**
14
 * Get the Blog Display setting.
15
 * If theme is using both 'Content' and 'Excerpt' then this setting will be called 'Mixed'.
16
 */
17
$options      = get_theme_support( 'jetpack-content-options' );
18
$blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
19
$blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
20
sort( $blog_display );
21
$blog_display = implode( ', ', $blog_display );
22
$blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
23
24
/**
25
 * If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', don't continue.
26
 */
27
if ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) ) {
28
	return;
29
}
30
31
/**
32
 * Apply Content filters.
33
 */
34
function jetpack_blog_display_custom_excerpt( $content ) {
0 ignored issues
show
Unused Code introduced by
The parameter $content is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
35
	$post = get_post();
36
	if ( empty( $post->post_excerpt ) ) {
37
		$text = strip_shortcodes( $post->post_content );
38
		$text = str_replace( ']]>', ']]&gt;', $text );
39
		$text = strip_tags( $text );
40
		/** This filter is documented in wp-includes/formatting.php */
41
		$excerpt_length = apply_filters( 'excerpt_length', 55 );
42
		/** This filter is documented in wp-includes/formatting.php */
43
		$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[...]' );
44
		$words = preg_split( "/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY );
45
		if ( count( $words ) > $excerpt_length ) {
46
			array_pop( $words );
47
			$text = implode( ' ', $words );
48
			$text = $text . $excerpt_more;
49
		} else {
50
			$text = implode( ' ', $words );
51
		}
52
	} else {
53
		$text = wp_kses_post( $post->post_excerpt );
54
	}
55
	return sprintf( '<p>%s</p>', $text );
56
}
57
58
/**
59
 * Display Excerpt instead of Content.
60
 */
61
function jetpack_the_content_to_the_excerpt( $content ) {
62 View Code Duplication
	if ( is_home() || is_archive() ) {
63
		if ( post_password_required() ) {
64
			$content = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.' ) );
65
		} else {
66
			$content = jetpack_blog_display_custom_excerpt( $content );
67
		}
68
	}
69
	return $content;
70
}
71
72
/**
73
 * Display Content instead of Excerpt.
74
 */
75
function jetpack_the_excerpt_to_the_content( $content ) {
76 View Code Duplication
	if ( is_home() || is_archive() ) {
77
		ob_start();
78
		the_content( sprintf(
79
			/* translators: %s: Name of current post. */
80
			wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'jetpack' ), array( 'span' => array( 'class' => array() ) ) ),
81
			the_title( '<span class="screen-reader-text">"', '"</span>', false )
82
		) );
83
		$content = ob_get_clean();
84
	}
85
	return $content;
86
}
87
88
/**
89
 * Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them.
90
 */
91
function jetpack_the_content_customizer( $content ) {
92
	$class = jetpack_the_content_customizer_class();
93 View Code Duplication
	if ( is_home() || is_archive() ) {
94
		if ( post_password_required() ) {
95
			$excerpt = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.' ) );
96
		} else {
97
			$excerpt = jetpack_blog_display_custom_excerpt( $content );
98
		}
99
	}
100
	if ( empty( $excerpt ) ) {
101
		return $content;
102
	} else {
103
		return sprintf( '<div class="jetpack-blog-display %s jetpack-the-content">%s</div><div class="jetpack-blog-display %s jetpack-the-excerpt">%s</div>', $class, $content, $class, $excerpt );
104
	}
105
}
106
107
/**
108
 * Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them.
109
 */
110
function jetpack_the_excerpt_customizer( $excerpt ) {
111 View Code Duplication
	if ( is_home() || is_archive() ) {
112
		ob_start();
113
		the_content( sprintf(
114
			/* translators: %s: Name of current post. */
115
			wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'jetpack' ), array( 'span' => array( 'class' => array() ) ) ),
116
			the_title( '<span class="screen-reader-text">"', '"</span>', false )
117
		) );
118
		$content = ob_get_clean();
119
	}
120
	if ( empty( $content ) ) {
121
		return $excerpt;
122
	} else {
123
		return sprintf( '<div class="jetpack-blog-display jetpack-the-content">%s</div><div class="jetpack-blog-display jetpack-the-excerpt">%s</div>', $content, $excerpt );
124
	}
125
}
126
127
/**
128
 * Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display.
129
 */
130
function jetpack_the_excerpt_mixed_customizer( $content ) {
131
	if ( is_home() || is_archive() ) {
132
		jetpack_the_content_customizer_class( 'output-the-excerpt' );
0 ignored issues
show
Unused Code introduced by
The call to the function jetpack_the_content_customizer_class() seems unnecessary as the function has no side-effects.
Loading history...
133
		ob_start();
134
		the_content();
135
		$content = ob_get_clean();
136
	}
137
	return $content;
138
}
139
140
/**
141
 * Returns a class value, `output-the-content` by default.
142
 * Used for themes with a 'Mixed' Blog Display so we can tell which output is by default.
143
 */
144
function jetpack_the_content_customizer_class( $new_class = null ) {
145
	static $class;
146
	if ( isset( $new_class ) ) {
147
		// Assign a new class and return.
148
		$class = $new_class;
149
	} else if ( isset( $class ) ) {
150
		// Reset the class after getting value.
151
		$prev_class = $class;
152
		$class = null;
153
		return $prev_class;
154
	} else {
155
		// Return default class value.
156
		return 'output-the-content';
157
	}
158
}
159
160
if ( is_customize_preview() ) {
161
	/*
162
	 * Display Content and Excerpt if the default Blog Display is 'Content'
163
	 * and we are in the Customizer.
164
	 */
165
	if ( 'content' === $blog_display ) {
166
		add_filter( 'the_content', 'jetpack_the_content_customizer' );
167
	}
168
169
	/*
170
	 * Display Content and Excerpt if the default Blog Display is 'Excerpt'
171
	 * and we are in the Customizer.
172
	 */
173
	if ( 'excerpt' === $blog_display ) {
174
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_customizer' );
175
	}
176
177
	/*
178
	 * Display Content and Excerpt if the default Blog Display is 'Mixed'
179
	 * and we are in the Customizer.
180
	 */
181
	if ( 'mixed' === $blog_display ) {
182
		add_filter( 'the_content', 'jetpack_the_content_customizer' );
183
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_mixed_customizer' );
184
	}
185
} else {
186
	$display_option = get_option( 'jetpack_content_blog_display', $blog_display );
187
188
	/*
189
	 * Display Excerpt if the default Blog Display is 'Content'
190
	 * or default Blog Display is 'Mixed'
191
	 * and the Option picked is 'Post Excerpt'
192
	 * and we aren't in the Customizer.
193
	 */
194
	if ( ( 'content' === $blog_display || 'mixed' === $blog_display ) && 'excerpt' === $display_option ) {
195
		add_filter( 'the_content', 'jetpack_the_content_to_the_excerpt' );
196
	}
197
198
	/*
199
	 * Display Content if the default Blog Display is 'Excerpt'
200
	 * or default Blog Display is 'Mixed'
201
	 * and the Option picked is 'Full Post'
202
	 * and we aren't in the Customizer.
203
	 */
204
	if ( ( 'excerpt' === $blog_display || 'mixed' === $blog_display ) && 'content' === $display_option ) {
205
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_to_the_content' );
206
	}
207
}
208