Completed
Push — gm-17/payment-widget ( cb2702...55e70e )
by
unknown
13:32 queued 03:19
created

blog-display.php ➔ jetpack_the_excerpt_customizer()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 15

Duplication

Lines 16
Ratio 69.57 %

Importance

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