Completed
Push — kraftbj-patch-2 ( 82c983...ae9d16 )
by
unknown
513:58 queued 503:20
created

blog-display.php ➔ jetpack_blog_display_custom_excerpt()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 38
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

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