Completed
Push — content-options-blog-display-7... ( 930de1 )
by
unknown
12:25
created

blog-display.php ➔ jetpack_the_content_to_the_excerpt()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 10

Duplication

Lines 11
Ratio 78.57 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 4
nop 1
dl 11
loc 14
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 ) {
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
		$blog = get_post( get_option( 'page_for_posts' ) );
79
		if ( get_the_content() === $blog->post_content ) {
80
			return $content;
81
		}
82
		if ( post_password_required() ) {
83
			$content = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) );
84
		} else {
85
			$content = jetpack_blog_display_custom_excerpt( $content );
86
		}
87
	}
88
	return $content;
89
}
90
91
/**
92
 * Display Content instead of Excerpt.
93
 */
94
function jetpack_the_excerpt_to_the_content( $content ) {
95 View Code Duplication
	if ( is_home() || is_archive() ) {
96
		ob_start();
97
		the_content( sprintf(
98
			/* translators: %s: Name of current post. */
99
			wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'jetpack' ), array( 'span' => array( 'class' => array() ) ) ),
100
			the_title( '<span class="screen-reader-text">"', '"</span>', false )
101
		) );
102
		$content = ob_get_clean();
103
	}
104
	return $content;
105
}
106
107
/**
108
 * Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them.
109
 */
110
function jetpack_the_content_customizer( $content ) {
111
	$class = jetpack_the_content_customizer_class();
112 View Code Duplication
	if ( is_home() || is_archive() ) {
113
		$blog = get_post( get_option( 'page_for_posts' ) );
114
		if ( get_the_content() === $blog->post_content ) {
115
			return $content;
116
		}
117
		if ( post_password_required() ) {
118
			$excerpt = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) );
119
		} else {
120
			$excerpt = jetpack_blog_display_custom_excerpt( $content );
121
		}
122
	}
123
	if ( empty( $excerpt ) ) {
124
		return $content;
125
	} else {
126
		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 );
127
	}
128
}
129
130
/**
131
 * Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them.
132
 */
133
function jetpack_the_excerpt_customizer( $excerpt ) {
134 View Code Duplication
	if ( is_home() || is_archive() ) {
135
		ob_start();
136
		the_content( sprintf(
137
			/* translators: %s: Name of current post. */
138
			wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'jetpack' ), array( 'span' => array( 'class' => array() ) ) ),
139
			the_title( '<span class="screen-reader-text">"', '"</span>', false )
140
		) );
141
		$content = ob_get_clean();
142
	}
143
	if ( empty( $content ) ) {
144
		return $excerpt;
145
	} else {
146
		return sprintf( '<div class="jetpack-blog-display jetpack-the-content">%s</div><div class="jetpack-blog-display jetpack-the-excerpt">%s</div>', $content, $excerpt );
147
	}
148
}
149
150
/**
151
 * Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display.
152
 */
153
function jetpack_the_excerpt_mixed_customizer( $content ) {
154
	if ( is_home() || is_archive() ) {
155
		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...
156
		ob_start();
157
		the_content();
158
		$content = ob_get_clean();
159
	}
160
	return $content;
161
}
162
163
/**
164
 * Returns a class value, `output-the-content` by default.
165
 * Used for themes with a 'Mixed' Blog Display so we can tell which output is by default.
166
 */
167
function jetpack_the_content_customizer_class( $new_class = null ) {
168
	static $class;
169
	if ( isset( $new_class ) ) {
170
		// Assign a new class and return.
171
		$class = $new_class;
172
	} else if ( isset( $class ) ) {
173
		// Reset the class after getting value.
174
		$prev_class = $class;
175
		$class = null;
176
		return $prev_class;
177
	} else {
178
		// Return default class value.
179
		return 'output-the-content';
180
	}
181
}
182
183
if ( is_customize_preview() ) {
184
	/*
185
	 * Display Content and Excerpt if the default Blog Display is 'Content'
186
	 * and we are in the Customizer.
187
	 */
188
	if ( 'content' === $blog_display ) {
189
		add_filter( 'the_content', 'jetpack_the_content_customizer' );
190
	}
191
192
	/*
193
	 * Display Content and Excerpt if the default Blog Display is 'Excerpt'
194
	 * and we are in the Customizer.
195
	 */
196
	if ( 'excerpt' === $blog_display ) {
197
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_customizer' );
198
	}
199
200
	/*
201
	 * Display Content and Excerpt if the default Blog Display is 'Mixed'
202
	 * and we are in the Customizer.
203
	 */
204
	if ( 'mixed' === $blog_display ) {
205
		add_filter( 'the_content', 'jetpack_the_content_customizer' );
206
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_mixed_customizer' );
207
	}
208
} else {
209
	$display_option = get_option( 'jetpack_content_blog_display', $blog_display );
210
211
	/*
212
	 * Display Excerpt if the default Blog Display is 'Content'
213
	 * or default Blog Display is 'Mixed'
214
	 * and the Option picked is 'Post Excerpt'
215
	 * and we aren't in the Customizer.
216
	 */
217
	if ( ( 'content' === $blog_display || 'mixed' === $blog_display ) && 'excerpt' === $display_option ) {
218
		add_filter( 'the_content', 'jetpack_the_content_to_the_excerpt' );
219
	}
220
221
	/*
222
	 * Display Content if the default Blog Display is 'Excerpt'
223
	 * or default Blog Display is 'Mixed'
224
	 * and the Option picked is 'Full Post'
225
	 * and we aren't in the Customizer.
226
	 */
227
	if ( ( 'excerpt' === $blog_display || 'mixed' === $blog_display ) && 'content' === $display_option ) {
228
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_to_the_content' );
229
	}
230
}
231