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

blog-display.php ➔ jetpack_blog_display_custom_excerpt()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 12
nop 1
dl 0
loc 50
rs 8.1575
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
 * @since 9.7.0 Deprecated $content parameter.
35
 *
36
 * @param string $content Post content. Deprecated.
37
 */
38
function jetpack_blog_display_custom_excerpt( $content = '' ) {
39
	if ( ! empty( $content ) ) {
40
		_doing_it_wrong(
41
			'jetpack_blog_display_custom_excerpt',
42
			esc_html__( 'You do not need to pass a $content parameter anymore.', 'jetpack' ),
43
			'jetpack-9.7.0'
44
		);
45
	}
46
47
	$post = get_post();
48
	if ( empty( $post ) ) {
49
		return '';
50
	}
51
52
	if ( empty( $post->post_excerpt ) ) {
53
		$text = strip_shortcodes( $post->post_content );
54
		$text = str_replace( ']]>', ']]&gt;', $text );
55
		$text = strip_tags( $text );
56
		/** This filter is documented in wp-includes/formatting.php */
57
		$excerpt_length = apply_filters( 'excerpt_length', 55 );
58
		/** This filter is documented in wp-includes/formatting.php */
59
		$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[...]' );
60
61
		/*
62
		 * translators: If your word count is based on single characters (e.g. East Asian characters),
63
		 * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
64
		 * Do not translate into your own language.
65
		 */
66
		if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
67
			$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
68
			preg_match_all( '/./u', $text, $words );
69
			$words = array_slice( $words[0], 0, $excerpt_length + 1 );
70
			$sep   = '';
71
		} else {
72
			$words = preg_split( "/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY );
73
			$sep   = ' ';
74
		}
75
76
		if ( count( $words ) > $excerpt_length ) {
77
			array_pop( $words );
78
			$text = implode( $sep, $words );
79
			$text = $text . $excerpt_more;
80
		} else {
81
			$text = implode( $sep, $words );
82
		}
83
	} else {
84
		$text = wp_kses_post( $post->post_excerpt );
85
	}
86
	return sprintf( '<p>%s</p>', $text );
87
}
88
89
/**
90
 * Display Excerpt instead of Content.
91
 */
92
function jetpack_the_content_to_the_excerpt( $content ) {
93 View Code Duplication
	if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
94
		if ( post_password_required() ) {
95
			$excerpt = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) );
96
		} else {
97
			$excerpt = jetpack_blog_display_custom_excerpt();
98
		}
99
	}
100
	if ( empty( $excerpt ) ) {
101
		return $content;
102
	} else {
103
		return $excerpt;
104
	}
105
}
106
107
/**
108
 * Display Content instead of Excerpt.
109
 */
110
function jetpack_the_excerpt_to_the_content( $content ) {
111 View Code Duplication
	if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
112
		ob_start();
113
		the_content(
114
			sprintf(
115
				wp_kses(
116
					/* translators: %s: Name of current post. Only visible to screen readers */
117
					__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'jetpack' ),
118
					array(
119
						'span' => array(
120
							'class' => array(),
121
						),
122
					)
123
				),
124
				get_the_title()
125
			)
126
		);
127
		$content = ob_get_clean();
128
	}
129
	return $content;
130
}
131
132
/**
133
 * Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them.
134
 */
135
function jetpack_the_content_customizer( $content ) {
136
	$class = jetpack_the_content_customizer_class();
137 View Code Duplication
	if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
138
		if ( post_password_required() ) {
139
			$excerpt = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) );
140
		} else {
141
			$excerpt = jetpack_blog_display_custom_excerpt();
142
		}
143
	}
144
	if ( empty( $excerpt ) ) {
145
		return $content;
146
	} else {
147
		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 );
148
	}
149
}
150
151
/**
152
 * Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them.
153
 */
154
function jetpack_the_excerpt_customizer( $excerpt ) {
155 View Code Duplication
	if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
156
		ob_start();
157
		the_content(
158
			sprintf(
159
				wp_kses(
160
					/* translators: %s: Name of current post. Only visible to screen readers */
161
					__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'jetpack' ),
162
					array(
163
						'span' => array(
164
							'class' => array(),
165
						),
166
					)
167
				),
168
				get_the_title()
169
			)
170
		);
171
		$content = ob_get_clean();
172
	}
173
	if ( empty( $content ) ) {
174
		return $excerpt;
175
	} else {
176
		return sprintf( '<div class="jetpack-blog-display jetpack-the-content">%s</div><div class="jetpack-blog-display jetpack-the-excerpt">%s</div>', $content, $excerpt );
177
	}
178
}
179
180
/**
181
 * Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display.
182
 */
183
function jetpack_the_excerpt_mixed_customizer( $content ) {
184
	if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
185
		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...
186
		ob_start();
187
		the_content();
188
		$content = ob_get_clean();
189
	}
190
	return $content;
191
}
192
193
/**
194
 * Returns a class value, `output-the-content` by default.
195
 * Used for themes with a 'Mixed' Blog Display so we can tell which output is by default.
196
 */
197
function jetpack_the_content_customizer_class( $new_class = null ) {
198
	static $class;
199
	if ( isset( $new_class ) ) {
200
		// Assign a new class and return.
201
		$class = $new_class;
202
	} elseif ( isset( $class ) ) {
203
		// Reset the class after getting value.
204
		$prev_class = $class;
205
		$class      = null;
206
		return $prev_class;
207
	} else {
208
		// Return default class value.
209
		return 'output-the-content';
210
	}
211
}
212
213
if ( is_customize_preview() ) {
214
	/*
215
	 * Display Content and Excerpt if the default Blog Display is 'Content'
216
	 * and we are in the Customizer.
217
	 */
218
	if ( 'content' === $blog_display ) {
219
		add_filter( 'the_content', 'jetpack_the_content_customizer' );
220
	}
221
222
	/*
223
	 * Display Content and Excerpt if the default Blog Display is 'Excerpt'
224
	 * and we are in the Customizer.
225
	 */
226
	if ( 'excerpt' === $blog_display ) {
227
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_customizer' );
228
	}
229
230
	/*
231
	 * Display Content and Excerpt if the default Blog Display is 'Mixed'
232
	 * and we are in the Customizer.
233
	 */
234
	if ( 'mixed' === $blog_display ) {
235
		add_filter( 'the_content', 'jetpack_the_content_customizer' );
236
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_mixed_customizer' );
237
	}
238
} else {
239
	$display_option = get_option( 'jetpack_content_blog_display', $blog_display );
240
241
	/*
242
	 * Display Excerpt if the default Blog Display is 'Content'
243
	 * or default Blog Display is 'Mixed'
244
	 * and the Option picked is 'Post Excerpt'
245
	 * and we aren't in the Customizer.
246
	 */
247
	if ( ( 'content' === $blog_display || 'mixed' === $blog_display ) && 'excerpt' === $display_option ) {
248
		add_filter( 'the_content', 'jetpack_the_content_to_the_excerpt' );
249
	}
250
251
	/*
252
	 * Display Content if the default Blog Display is 'Excerpt'
253
	 * or default Blog Display is 'Mixed'
254
	 * and the Option picked is 'Full Post'
255
	 * and we aren't in the Customizer.
256
	 */
257
	if ( ( 'excerpt' === $blog_display || 'mixed' === $blog_display ) && 'content' === $display_option ) {
258
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_to_the_content' );
259
	}
260
}
261