Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 379
0.00% covered (danger)
0.00%
0 / 5
CRAP
n/a
0 / 0
jetpack_content_options_customize_register
0.00% covered (danger)
0.00%
0 / 321
0.00% covered (danger)
0.00%
0 / 1
4830
jetpack_post_thumbnail_supports
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
jetpack_content_options_sanitize_checkbox
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
jetpack_content_options_sanitize_blog_display
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
jetpack_content_options_customize_preview_js
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
110
Jetpack_Customize_Control_Title
n/a
0 / 0
n/a
0 / 0
1
n/a
0 / 0
 render_content
n/a
0 / 0
n/a
0 / 0
1
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Theme Tools: functions for Customizer enhancements.
4 *
5 * @package automattic/jetpack-classic-theme-helper
6 */
7
8use Automattic\Jetpack\Assets;
9
10if ( ! defined( 'ABSPATH' ) ) {
11    exit( 0 );
12}
13
14if ( ! function_exists( 'jetpack_content_options_customize_register' ) ) {
15
16    /**
17     * Add Content section to the Theme Customizer.
18     *
19     * @param WP_Customize_Manager $wp_customize Theme Customizer object.
20     */
21    function jetpack_content_options_customize_register( $wp_customize ) {
22        $options      = get_theme_support( 'jetpack-content-options' );
23        $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
24        $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
25        sort( $blog_display );
26        $blog_display         = implode( ', ', $blog_display );
27        $blog_display         = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
28        $author_bio           = ( ! empty( $options[0]['author-bio'] ) ) ? $options[0]['author-bio'] : null;
29        $author_bio_default   = ( isset( $options[0]['author-bio-default'] ) && false === $options[0]['author-bio-default'] ) ? '' : 1;
30        $post_details         = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
31        $date                 = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
32        $categories           = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
33        $tags                 = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
34        $author               = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
35        $comment              = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
36        $featured_images      = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null;
37        $fi_archive           = ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null;
38        $fi_post              = ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null;
39        $fi_page              = ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null;
40        $fi_portfolio         = ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null;
41        $fi_fallback          = ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null;
42        $fi_archive_default   = ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1;
43        $fi_post_default      = ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1;
44        $fi_page_default      = ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1;
45        $fi_portfolio_default = ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1;
46        $fi_fallback_default  = ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1;
47
48        // If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', 'jetpack-content-options[ 'author-bio' ]', 'jetpack-content-options[ 'post-details' ]' and 'jetpack-content-options[ 'featured-images' ]', don't continue.
49        if ( ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ), true ) )
50            && ( true !== $author_bio )
51            && ( ( empty( $post_details['stylesheet'] ) )
52                && ( empty( $date )
53                    || empty( $categories )
54                    || empty( $tags )
55                    || empty( $author )
56                    || empty( $comment ) ) )
57            && ( true !== $fi_archive && true !== $fi_post && true !== $fi_page && true !== $fi_portfolio && true !== $fi_fallback ) ) {
58            return;
59        }
60
61        if ( ! class_exists( 'Jetpack_Customize_Control_Title' ) ) {
62
63            /**
64             * New Customizer control type: Title.
65             */
66            class Jetpack_Customize_Control_Title extends WP_Customize_Control {
67                /**
68                 * Customizer control type.
69                 *
70                 * @var string
71                 */
72                public $type = 'title';
73
74                /**
75                 * Render the control's content.
76                 */
77                public function render_content() {
78                    ?>
79                    <span class="customize-control-title"><?php echo wp_kses_post( $this->label ); ?></span>
80                    <?php
81                }
82            }
83        }
84
85        // Add Content section.
86        $wp_customize->add_section(
87            'jetpack_content_options',
88            array(
89                'title'          => esc_html__( 'Content Options', 'jetpack-classic-theme-helper' ),
90                'theme_supports' => 'jetpack-content-options',
91                'priority'       => 100,
92            )
93        );
94
95        // Add Blog Display option.
96        if ( in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ), true ) ) {
97            if ( 'mixed' === $blog_display ) {
98                $blog_display_choices = array(
99                    'content' => esc_html__( 'Full post', 'jetpack-classic-theme-helper' ),
100                    'excerpt' => esc_html__( 'Post excerpt', 'jetpack-classic-theme-helper' ),
101                    'mixed'   => esc_html__( 'Default', 'jetpack-classic-theme-helper' ),
102                );
103
104                $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages, or opt for the theme\'s default combination of excerpt and full post.', 'jetpack-classic-theme-helper' );
105            } else {
106                $blog_display_choices = array(
107                    'content' => esc_html__( 'Full post', 'jetpack-classic-theme-helper' ),
108                    'excerpt' => esc_html__( 'Post excerpt', 'jetpack-classic-theme-helper' ),
109                );
110
111                $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages.', 'jetpack-classic-theme-helper' );
112
113                if ( 'mixed' === get_option( 'jetpack_content_blog_display' ) ) {
114                    update_option( 'jetpack_content_blog_display', $blog_display );
115                }
116            }
117
118            $wp_customize->add_setting(
119                'jetpack_content_blog_display',
120                array(
121                    'default'           => $blog_display,
122                    'type'              => 'option',
123                    'transport'         => 'postMessage',
124                    'sanitize_callback' => 'jetpack_content_options_sanitize_blog_display',
125                )
126            );
127
128            $wp_customize->add_control(
129                'jetpack_content_blog_display',
130                array(
131                    'section'     => 'jetpack_content_options',
132                    'label'       => esc_html__( 'Blog Display', 'jetpack-classic-theme-helper' ),
133                    'description' => $blog_display_description,
134                    'type'        => 'radio',
135                    'choices'     => $blog_display_choices,
136                )
137            );
138        }
139
140        // Add Author Bio option.
141        if ( true === $author_bio ) {
142            $wp_customize->add_setting( 'jetpack_content_author_bio_title' );
143
144            $wp_customize->add_control(
145                new Jetpack_Customize_Control_Title(
146                    $wp_customize,
147                    'jetpack_content_author_bio_title',
148                    array(
149                        'section' => 'jetpack_content_options',
150                        'label'   => esc_html__( 'Author Bio', 'jetpack-classic-theme-helper' ),
151                        'type'    => 'title',
152                    )
153                )
154            );
155
156            $wp_customize->add_setting(
157                'jetpack_content_author_bio',
158                array(
159                    'default'           => $author_bio_default,
160                    'type'              => 'option',
161                    'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
162                )
163            );
164
165            $wp_customize->add_control(
166                'jetpack_content_author_bio',
167                array(
168                    'section' => 'jetpack_content_options',
169                    'label'   => esc_html__( 'Display on single posts', 'jetpack-classic-theme-helper' ),
170                    'type'    => 'checkbox',
171                )
172            );
173        }
174
175        // Add Post Details options.
176        if ( ( ! empty( $post_details ) )
177            && ( ! empty( $post_details['stylesheet'] ) )
178            && ( ! empty( $date )
179                || ! empty( $categories )
180                || ! empty( $tags )
181                || ! empty( $author )
182                || ! empty( $comment ) ) ) {
183            $wp_customize->add_setting( 'jetpack_content_post_details_title' );
184
185            $wp_customize->add_control(
186                new Jetpack_Customize_Control_Title(
187                    $wp_customize,
188                    'jetpack_content_post_details_title',
189                    array(
190                        'section' => 'jetpack_content_options',
191                        'label'   => esc_html__( 'Post Details', 'jetpack-classic-theme-helper' ),
192                        'type'    => 'title',
193                    )
194                )
195            );
196
197            // Post Details: Date.
198            if ( ! empty( $date ) ) {
199                $wp_customize->add_setting(
200                    'jetpack_content_post_details_date',
201                    array(
202                        'default'           => 1,
203                        'type'              => 'option',
204                        'transport'         => 'postMessage',
205                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
206                    )
207                );
208
209                $wp_customize->add_control(
210                    'jetpack_content_post_details_date',
211                    array(
212                        'section' => 'jetpack_content_options',
213                        'label'   => esc_html__( 'Display date', 'jetpack-classic-theme-helper' ),
214                        'type'    => 'checkbox',
215                    )
216                );
217            }
218
219            // Post Details: Categories.
220            if ( ! empty( $categories ) ) {
221                $wp_customize->add_setting(
222                    'jetpack_content_post_details_categories',
223                    array(
224                        'default'           => 1,
225                        'type'              => 'option',
226                        'transport'         => 'postMessage',
227                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
228                    )
229                );
230
231                $wp_customize->add_control(
232                    'jetpack_content_post_details_categories',
233                    array(
234                        'section' => 'jetpack_content_options',
235                        'label'   => esc_html__( 'Display categories', 'jetpack-classic-theme-helper' ),
236                        'type'    => 'checkbox',
237                    )
238                );
239            }
240
241            // Post Details: Tags.
242            if ( ! empty( $tags ) ) {
243                $wp_customize->add_setting(
244                    'jetpack_content_post_details_tags',
245                    array(
246                        'default'           => 1,
247                        'type'              => 'option',
248                        'transport'         => 'postMessage',
249                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
250                    )
251                );
252
253                $wp_customize->add_control(
254                    'jetpack_content_post_details_tags',
255                    array(
256                        'section' => 'jetpack_content_options',
257                        'label'   => esc_html__( 'Display tags', 'jetpack-classic-theme-helper' ),
258                        'type'    => 'checkbox',
259                    )
260                );
261            }
262
263            // Post Details: Author.
264            if ( ! empty( $author ) ) {
265                $wp_customize->add_setting(
266                    'jetpack_content_post_details_author',
267                    array(
268                        'default'           => 1,
269                        'type'              => 'option',
270                        'transport'         => 'postMessage',
271                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
272                    )
273                );
274
275                $wp_customize->add_control(
276                    'jetpack_content_post_details_author',
277                    array(
278                        'section' => 'jetpack_content_options',
279                        'label'   => esc_html__( 'Display author', 'jetpack-classic-theme-helper' ),
280                        'type'    => 'checkbox',
281                    )
282                );
283            }
284
285            // Post Details: Comment link.
286            if ( ! empty( $comment ) ) {
287                $wp_customize->add_setting(
288                    'jetpack_content_post_details_comment',
289                    array(
290                        'default'           => 1,
291                        'type'              => 'option',
292                        'transport'         => 'postMessage',
293                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
294                    )
295                );
296
297                $wp_customize->add_control(
298                    'jetpack_content_post_details_comment',
299                    array(
300                        'section' => 'jetpack_content_options',
301                        'label'   => esc_html__( 'Display comment link', 'jetpack-classic-theme-helper' ),
302                        'type'    => 'checkbox',
303                    )
304                );
305            }
306        }
307
308        // Add Featured Images options.
309        if ( true === $fi_archive || true === $fi_post || true === $fi_page || true === $fi_portfolio || true === $fi_fallback ) {
310            $wp_customize->add_setting( 'jetpack_content_featured_images_title' );
311
312            $wp_customize->add_control(
313                new Jetpack_Customize_Control_Title(
314                    $wp_customize,
315                    'jetpack_content_featured_images_title',
316                    array(
317                        'section'         => 'jetpack_content_options',
318                        'label'           => esc_html__( 'Featured Images', 'jetpack-classic-theme-helper' ) . sprintf( '<a href="https://en.support.wordpress.com/featured-images/" class="customize-help-toggle dashicons dashicons-editor-help" title="%1$s" rel="noopener noreferrer" target="_blank"><span class="screen-reader-text">%1$s</span></a>', esc_html__( 'Learn more about Featured Images', 'jetpack-classic-theme-helper' ) ),
319                        'type'            => 'title',
320                        'active_callback' => 'jetpack_post_thumbnail_supports',
321                    )
322                )
323            );
324
325            // Featured Images: Archive.
326            if ( true === $fi_archive ) {
327                $wp_customize->add_setting(
328                    'jetpack_content_featured_images_archive',
329                    array(
330                        'default'           => $fi_archive_default,
331                        'type'              => 'option',
332                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
333                    )
334                );
335
336                $wp_customize->add_control(
337                    'jetpack_content_featured_images_archive',
338                    array(
339                        'section'         => 'jetpack_content_options',
340                        'label'           => esc_html__( 'Display on blog and archives', 'jetpack-classic-theme-helper' ),
341                        'type'            => 'checkbox',
342                        'active_callback' => 'jetpack_post_thumbnail_supports',
343                    )
344                );
345            }
346
347            // Featured Images: Post.
348            if ( true === $fi_post ) {
349                $wp_customize->add_setting(
350                    'jetpack_content_featured_images_post',
351                    array(
352                        'default'           => $fi_post_default,
353                        'type'              => 'option',
354                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
355                    )
356                );
357
358                $wp_customize->add_control(
359                    'jetpack_content_featured_images_post',
360                    array(
361                        'section'         => 'jetpack_content_options',
362                        'label'           => esc_html__( 'Display on single posts', 'jetpack-classic-theme-helper' ),
363                        'type'            => 'checkbox',
364                        'active_callback' => 'jetpack_post_thumbnail_supports',
365                    )
366                );
367            }
368
369            // Featured Images: Page.
370            if ( true === $fi_page ) {
371                $wp_customize->add_setting(
372                    'jetpack_content_featured_images_page',
373                    array(
374                        'default'           => $fi_page_default,
375                        'type'              => 'option',
376                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
377                    )
378                );
379
380                $wp_customize->add_control(
381                    'jetpack_content_featured_images_page',
382                    array(
383                        'section'         => 'jetpack_content_options',
384                        'label'           => esc_html__( 'Display on pages', 'jetpack-classic-theme-helper' ),
385                        'type'            => 'checkbox',
386                        'active_callback' => 'jetpack_post_thumbnail_supports',
387                    )
388                );
389            }
390
391            // Featured Images: Portfolio.
392            if ( true === $fi_portfolio && post_type_exists( 'jetpack-portfolio' ) ) {
393                $wp_customize->add_setting(
394                    'jetpack_content_featured_images_portfolio',
395                    array(
396                        'default'           => $fi_portfolio_default,
397                        'type'              => 'option',
398                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
399                    )
400                );
401
402                $wp_customize->add_control(
403                    'jetpack_content_featured_images_portfolio',
404                    array(
405                        'section'         => 'jetpack_content_options',
406                        'label'           => esc_html__( 'Display on single projects', 'jetpack-classic-theme-helper' ),
407                        'type'            => 'checkbox',
408                        'active_callback' => 'jetpack_post_thumbnail_supports',
409                    )
410                );
411            }
412
413            // Featured Images: Fallback.
414            if ( true === $fi_fallback ) {
415                $wp_customize->add_setting(
416                    'jetpack_content_featured_images_fallback',
417                    array(
418                        'default'           => $fi_fallback_default,
419                        'type'              => 'option',
420                        'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
421                    )
422                );
423
424                $wp_customize->add_control(
425                    'jetpack_content_featured_images_fallback',
426                    array(
427                        'section'         => 'jetpack_content_options',
428                        'label'           => esc_html__( 'Automatically use first image in post', 'jetpack-classic-theme-helper' ),
429                        'type'            => 'checkbox',
430                        'active_callback' => 'jetpack_post_thumbnail_supports',
431                    )
432                );
433            }
434        }
435    }
436    add_action( 'customize_register', 'jetpack_content_options_customize_register' );
437
438}
439
440if ( ! function_exists( 'jetpack_post_thumbnail_supports' ) ) {
441
442    /**
443     * Return whether the theme supports Post Thumbnails.
444     */
445    function jetpack_post_thumbnail_supports() {
446        return ( current_theme_supports( 'post-thumbnails' ) );
447    }
448
449}
450
451if ( ! function_exists( 'jetpack_content_options_sanitize_checkbox' ) ) {
452
453    /**
454     * Sanitize the checkbox.
455     *
456     * @param int $input The unsanitized value from the setting.
457     * @return int|string
458     */
459    function jetpack_content_options_sanitize_checkbox( $input ) {
460        return ( 1 === (int) $input ) ? 1 : '';
461    }
462
463}
464
465if ( ! function_exists( 'jetpack_content_options_sanitize_blog_display' ) ) {
466
467    /**
468     * Sanitize the Display value.
469     *
470     * @param string $display The unsanitized value from the setting.
471     * @return string
472     */
473    function jetpack_content_options_sanitize_blog_display( $display ) {
474        if ( ! in_array( $display, array( 'content', 'excerpt', 'mixed' ), true ) ) {
475            $display = 'content';
476        }
477        return $display;
478    }
479
480}
481
482if ( ! function_exists( 'jetpack_content_options_customize_preview_js' ) ) {
483
484    /**
485     * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
486     */
487    function jetpack_content_options_customize_preview_js() {
488        $options      = get_theme_support( 'jetpack-content-options' );
489        $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
490        $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
491        sort( $blog_display );
492        $blog_display = implode( ', ', $blog_display );
493        $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
494        $masonry      = ( ! empty( $options[0]['masonry'] ) ) ? $options[0]['masonry'] : null;
495        $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
496        $date         = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
497        $categories   = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
498        $tags         = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
499        $author       = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
500        $comment      = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
501
502        Assets::register_script(
503            'jetpack-content-options-customizer',
504            '../../dist/content-options/customizer.js',
505            __FILE__,
506            array(
507                'dependencies' => array(
508                    'customize-preview',
509                ),
510                'in_footer'    => true,
511                'enqueue'      => true,
512            )
513        );
514
515        wp_localize_script(
516            'jetpack-content-options-customizer',
517            'blogDisplay',
518            array(
519                'display' => get_option( 'jetpack_content_blog_display', $blog_display ),
520                'masonry' => $masonry,
521            )
522        );
523
524        wp_localize_script(
525            'jetpack-content-options-customizer',
526            'postDetails',
527            array(
528                'date'       => $date,
529                'categories' => $categories,
530                'tags'       => $tags,
531                'author'     => $author,
532                'comment'    => $comment,
533            )
534        );
535    }
536    add_action( 'customize_preview_init', 'jetpack_content_options_customize_preview_js' );
537
538}