summaryrefslogtreecommitdiff
blob: d37b62557a6078f4211f97b1e38a038a1d888297 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
/**
 * The functions to display Content or Excerpt in a theme.
 */

/**
 * If the theme doesn't support 'jetpack-content-options', don't continue.
 */
if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
	return;
}

/**
 * Get the Blog Display setting.
 * If theme is using both 'Content' and 'Excerpt' then this setting will be called 'Mixed'.
 */
$options      = get_theme_support( 'jetpack-content-options' );
$blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
$blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
sort( $blog_display );
$blog_display = implode( ', ', $blog_display );
$blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;

/**
 * If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', don't continue.
 */
if ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) ) {
	return;
}

/**
 * Apply Content filters.
 */
function jetpack_blog_display_custom_excerpt( $content ) {
	$post = get_post();
	if ( empty( $post->post_excerpt ) ) {
		$text = strip_shortcodes( $post->post_content );
		$text = str_replace( ']]>', ']]&gt;', $text );
		$text = strip_tags( $text );
		/** This filter is documented in wp-includes/formatting.php */
		$excerpt_length = apply_filters( 'excerpt_length', 55 );
		/** This filter is documented in wp-includes/formatting.php */
		$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[...]' );

		/*
		 * translators: If your word count is based on single characters (e.g. East Asian characters),
		 * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
		 * Do not translate into your own language.
		 */
		if ( strpos( _x( 'words', 'Word count type. Do not translate!', 'jetpack' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
			$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
			preg_match_all( '/./u', $text, $words );
			$words = array_slice( $words[0], 0, $excerpt_length + 1 );
			$sep = '';
		} else {
			$words = preg_split( "/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY );
			$sep = ' ';
		}

		if ( count( $words ) > $excerpt_length ) {
			array_pop( $words );
			$text = implode( $sep, $words );
			$text = $text . $excerpt_more;
		} else {
			$text = implode( $sep, $words );
		}
	} else {
		$text = wp_kses_post( $post->post_excerpt );
	}
	return sprintf( '<p>%s</p>', $text );
}

/**
 * Display Excerpt instead of Content.
 */
function jetpack_the_content_to_the_excerpt( $content ) {
	if ( is_home() || is_archive() ) {
		if ( post_password_required() ) {
			$content = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) );
		} else {
			$content = jetpack_blog_display_custom_excerpt( $content );
		}
	}
	return $content;
}

/**
 * Display Content instead of Excerpt.
 */
function jetpack_the_excerpt_to_the_content( $content ) {
	if ( is_home() || is_archive() ) {
		ob_start();
		the_content( sprintf(
			wp_kses(
				/* translators: %s: Name of current post. Only visible to screen readers */
				__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'jetpack' ),
				array(
					'span' => array(
						'class' => array(),
					),
				)
			),
			get_the_title()
		) );
		$content = ob_get_clean();
	}
	return $content;
}

/**
 * Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them.
 */
function jetpack_the_content_customizer( $content ) {
	$class = jetpack_the_content_customizer_class();
	if ( is_home() || is_archive() ) {
		if ( post_password_required() ) {
			$excerpt = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack' ) );
		} else {
			$excerpt = jetpack_blog_display_custom_excerpt( $content );
		}
	}
	if ( empty( $excerpt ) ) {
		return $content;
	} else {
		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 );
	}
}

/**
 * Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them.
 */
function jetpack_the_excerpt_customizer( $excerpt ) {
	if ( is_home() || is_archive() ) {
		ob_start();
		the_content( sprintf(
			wp_kses(
				/* translators: %s: Name of current post. Only visible to screen readers */
				__( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'jetpack' ),
				array(
					'span' => array(
						'class' => array(),
					),
				)
			),
			get_the_title()
		) );
		$content = ob_get_clean();
	}
	if ( empty( $content ) ) {
		return $excerpt;
	} else {
		return sprintf( '<div class="jetpack-blog-display jetpack-the-content">%s</div><div class="jetpack-blog-display jetpack-the-excerpt">%s</div>', $content, $excerpt );
	}
}

/**
 * Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display.
 */
function jetpack_the_excerpt_mixed_customizer( $content ) {
	if ( is_home() || is_archive() ) {
		jetpack_the_content_customizer_class( 'output-the-excerpt' );
		ob_start();
		the_content();
		$content = ob_get_clean();
	}
	return $content;
}

/**
 * Returns a class value, `output-the-content` by default.
 * Used for themes with a 'Mixed' Blog Display so we can tell which output is by default.
 */
function jetpack_the_content_customizer_class( $new_class = null ) {
	static $class;
	if ( isset( $new_class ) ) {
		// Assign a new class and return.
		$class = $new_class;
	} else if ( isset( $class ) ) {
		// Reset the class after getting value.
		$prev_class = $class;
		$class = null;
		return $prev_class;
	} else {
		// Return default class value.
		return 'output-the-content';
	}
}

if ( is_customize_preview() ) {
	/*
	 * Display Content and Excerpt if the default Blog Display is 'Content'
	 * and we are in the Customizer.
	 */
	if ( 'content' === $blog_display ) {
		add_filter( 'the_content', 'jetpack_the_content_customizer' );
	}

	/*
	 * Display Content and Excerpt if the default Blog Display is 'Excerpt'
	 * and we are in the Customizer.
	 */
	if ( 'excerpt' === $blog_display ) {
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_customizer' );
	}

	/*
	 * Display Content and Excerpt if the default Blog Display is 'Mixed'
	 * and we are in the Customizer.
	 */
	if ( 'mixed' === $blog_display ) {
		add_filter( 'the_content', 'jetpack_the_content_customizer' );
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_mixed_customizer' );
	}
} else {
	$display_option = get_option( 'jetpack_content_blog_display', $blog_display );

	/*
	 * Display Excerpt if the default Blog Display is 'Content'
	 * or default Blog Display is 'Mixed'
	 * and the Option picked is 'Post Excerpt'
	 * and we aren't in the Customizer.
	 */
	if ( ( 'content' === $blog_display || 'mixed' === $blog_display ) && 'excerpt' === $display_option ) {
		add_filter( 'the_content', 'jetpack_the_content_to_the_excerpt' );
	}

	/*
	 * Display Content if the default Blog Display is 'Excerpt'
	 * or default Blog Display is 'Mixed'
	 * and the Option picked is 'Full Post'
	 * and we aren't in the Customizer.
	 */
	if ( ( 'excerpt' === $blog_display || 'mixed' === $blog_display ) && 'content' === $display_option ) {
		add_filter( 'the_excerpt', 'jetpack_the_excerpt_to_the_content' );
	}
}