Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 127
0.00% covered (danger)
0.00%
0 / 7
CRAP
n/a
0 / 0
jetpack_blog_display_custom_excerpt
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
72
jetpack_the_content_to_the_excerpt
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
42
jetpack_the_excerpt_to_the_content
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
20
jetpack_the_content_customizer
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
42
jetpack_the_excerpt_customizer
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
jetpack_the_excerpt_mixed_customizer
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
20
jetpack_the_content_customizer_class
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Theme Tools: the functions to display Content or Excerpt in a theme.
4 *
5 * @package automattic/jetpack-classic-theme-helper
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * If the theme doesn't support 'jetpack-content-options', don't continue.
14 */
15if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
16    return;
17}
18
19/**
20 * Get the Blog Display setting.
21 * If theme is using both 'Content' and 'Excerpt' then this setting will be called 'Mixed'.
22 */
23$options      = get_theme_support( 'jetpack-content-options' );
24$blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
25$blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
26sort( $blog_display );
27$blog_display = implode( ', ', $blog_display );
28$blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
29
30/**
31 * If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', don't continue.
32 */
33if ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ), true ) ) {
34    return;
35}
36
37if ( ! function_exists( 'jetpack_blog_display_custom_excerpt' ) ) {
38
39    /**
40     * Apply Content filters.
41     *
42     * @since 9.7.0 Deprecated $content parameter.
43     *
44     * @param string $content Post content. Deprecated.
45     */
46    function jetpack_blog_display_custom_excerpt( $content = '' ) {
47        if ( ! empty( $content ) ) {
48            _doing_it_wrong(
49                'jetpack_blog_display_custom_excerpt',
50                esc_html__( 'You do not need to pass a $content parameter anymore.', 'jetpack-classic-theme-helper' ),
51                'jetpack-9.7.0'
52            );
53        }
54
55        $post = get_post();
56        if ( empty( $post ) ) {
57            return '';
58        }
59
60        if ( empty( $post->post_excerpt ) ) {
61            $text = strip_shortcodes( $post->post_content );
62            $text = str_replace( ']]>', ']]&gt;', $text );
63            $text = wp_strip_all_tags( $text );
64            /** This filter is documented in wp-includes/formatting.php */
65            $excerpt_length = apply_filters( 'excerpt_length', 55 );
66            $excerpt_length = is_numeric( $excerpt_length ) ? (int) $excerpt_length : 55;
67
68            /** This filter is documented in wp-includes/formatting.php */
69            $excerpt_more = apply_filters( 'excerpt_more', ' [...]' );
70
71            /*
72            * translators: If your word count is based on single characters (e.g. East Asian characters),
73            * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
74            * Do not translate into your own language.
75            */
76            if ( strpos( _x( 'words', 'Word count type. Do not translate!', 'jetpack-classic-theme-helper' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
77                $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
78                preg_match_all( '/./u', $text, $words );
79                $words = array_slice( $words[0], 0, $excerpt_length + 1 );
80                $sep   = '';
81            } else {
82                $words = preg_split( "/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY );
83                $sep   = ' ';
84            }
85
86            if ( count( $words ) > $excerpt_length ) {
87                array_pop( $words );
88                $text  = implode( $sep, $words );
89                $text .= $excerpt_more;
90            } else {
91                $text = implode( $sep, $words );
92            }
93        } else {
94            $text = wp_kses_post( $post->post_excerpt );
95        }
96        return sprintf( '<p>%s</p>', $text );
97    }
98
99}
100
101if ( ! function_exists( 'jetpack_the_content_to_the_excerptt' ) ) {
102
103    /**
104     * Display Excerpt instead of Content.
105     *
106     * @param string $content Post content.
107     */
108    function jetpack_the_content_to_the_excerpt( $content ) {
109        if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
110            if ( post_password_required() ) {
111                $excerpt = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack-classic-theme-helper' ) );
112            } else {
113                $excerpt = jetpack_blog_display_custom_excerpt();
114            }
115        }
116        if ( empty( $excerpt ) ) {
117            return $content;
118        } else {
119            return $excerpt;
120        }
121    }
122
123}
124
125if ( ! function_exists( 'jetpack_the_excerpt_to_the_content' ) ) {
126
127    /**
128     * Display Content instead of Excerpt.
129     *
130     * @param string $content The post excerpt.
131     */
132    function jetpack_the_excerpt_to_the_content( $content ) {
133        if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
134            ob_start();
135            the_content(
136                sprintf(
137                    wp_kses(
138                        /* translators: %s: Name of current post. Only visible to screen readers */
139                        __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'jetpack-classic-theme-helper' ),
140                        array(
141                            'span' => array(
142                                'class' => array(),
143                            ),
144                        )
145                    ),
146                    get_the_title()
147                )
148            );
149            $content = ob_get_clean();
150        }
151        return $content;
152    }
153
154}
155
156if ( ! function_exists( 'jetpack_the_content_customizer' ) ) {
157
158    /**
159     * Display both Content and Excerpt instead of Content in the Customizer so live preview can switch between them.
160     *
161     * @param string $content The post content.
162     */
163    function jetpack_the_content_customizer( $content ) {
164        $class = jetpack_the_content_customizer_class();
165        if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
166            if ( post_password_required() ) {
167                $excerpt = sprintf( '<p>%s</p>', esc_html__( 'There is no excerpt because this is a protected post.', 'jetpack-classic-theme-helper' ) );
168            } else {
169                $excerpt = jetpack_blog_display_custom_excerpt();
170            }
171        }
172        if ( empty( $excerpt ) ) {
173            return $content;
174        } else {
175            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 );
176        }
177    }
178
179}
180
181if ( ! function_exists( 'jetpack_the_excerpt_customizer' ) ) {
182
183    /**
184     * Display both Content and Excerpt instead of Excerpt in the Customizer so live preview can switch between them.
185     *
186     * @param string $excerpt The post excerpt.
187     */
188    function jetpack_the_excerpt_customizer( $excerpt ) {
189        if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
190            ob_start();
191            the_content(
192                sprintf(
193                    wp_kses(
194                        /* translators: %s: Name of current post. Only visible to screen readers */
195                        __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'jetpack-classic-theme-helper' ),
196                        array(
197                            'span' => array(
198                                'class' => array(),
199                            ),
200                        )
201                    ),
202                    get_the_title()
203                )
204            );
205            $content = ob_get_clean();
206        }
207        if ( empty( $content ) ) {
208            return $excerpt;
209        } else {
210            return sprintf( '<div class="jetpack-blog-display jetpack-the-content">%s</div><div class="jetpack-blog-display jetpack-the-excerpt">%s</div>', $content, $excerpt );
211        }
212    }
213
214}
215
216if ( ! function_exists( 'jetpack_the_excerpt_mixed_customizer' ) ) {
217
218    /**
219     * Display Content instead of Excerpt in the Customizer when theme uses a 'Mixed' display.
220     *
221     * @param string $content The post excerpt.
222     */
223    function jetpack_the_excerpt_mixed_customizer( $content ) {
224        if ( ( is_home() || is_archive() ) && ! is_post_type_archive( array( 'jetpack-testimonial', 'jetpack-portfolio', 'product' ) ) ) {
225            jetpack_the_content_customizer_class( 'output-the-excerpt' );
226            ob_start();
227            the_content();
228            $content = ob_get_clean();
229        }
230        return $content;
231    }
232
233}
234
235if ( ! function_exists( 'jetpack_the_content_customizer_class' ) ) {
236
237    /**
238     * Returns a class value, `output-the-content` by default.
239     * Used for themes with a 'Mixed' Blog Display so we can tell which output is by default.
240     *
241     * @param string|null $new_class CSS class added to content container.
242     */
243    function jetpack_the_content_customizer_class( $new_class = null ) {
244        static $class;
245        if ( isset( $new_class ) ) {
246            // Assign a new class and return.
247            $class = $new_class;
248        } elseif ( isset( $class ) ) {
249            // Reset the class after getting value.
250            $prev_class = $class;
251            $class      = null;
252            return $prev_class;
253        } else {
254            // Return default class value.
255            return 'output-the-content';
256        }
257    }
258
259}
260
261if ( is_customize_preview() ) {
262    /*
263     * Display Content and Excerpt if the default Blog Display is 'Content'
264     * and we are in the Customizer.
265     */
266    if ( 'content' === $blog_display ) {
267        add_filter( 'the_content', 'jetpack_the_content_customizer' );
268    }
269
270    /*
271     * Display Content and Excerpt if the default Blog Display is 'Excerpt'
272     * and we are in the Customizer.
273     */
274    if ( 'excerpt' === $blog_display ) {
275        add_filter( 'the_excerpt', 'jetpack_the_excerpt_customizer' );
276    }
277
278    /*
279     * Display Content and Excerpt if the default Blog Display is 'Mixed'
280     * and we are in the Customizer.
281     */
282    if ( 'mixed' === $blog_display ) {
283        add_filter( 'the_content', 'jetpack_the_content_customizer' );
284        add_filter( 'the_excerpt', 'jetpack_the_excerpt_mixed_customizer' );
285    }
286} else {
287    $display_option = get_option( 'jetpack_content_blog_display', $blog_display );
288
289    /*
290     * Display Excerpt if the default Blog Display is 'Content'
291     * or default Blog Display is 'Mixed'
292     * and the Option picked is 'Post Excerpt'
293     * and we aren't in the Customizer.
294     */
295    if ( ( 'content' === $blog_display || 'mixed' === $blog_display ) && 'excerpt' === $display_option ) {
296        add_filter( 'the_content', 'jetpack_the_content_to_the_excerpt' );
297    }
298
299    /*
300     * Display Content if the default Blog Display is 'Excerpt'
301     * or default Blog Display is 'Mixed'
302     * and the Option picked is 'Full Post'
303     * and we aren't in the Customizer.
304     */
305    if ( ( 'excerpt' === $blog_display || 'mixed' === $blog_display ) && 'content' === $display_option ) {
306        add_filter( 'the_excerpt', 'jetpack_the_excerpt_to_the_content' );
307    }
308}