Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 75
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
jetpack_post_details_enqueue_scripts
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
240
jetpack_post_details_body_classes
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
156
jetpack_post_details_should_run
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
306
1<?php
2/**
3 * Theme Tools: functions for Post Details.
4 *
5 * @package automattic/jetpack-classic-theme-helper
6 */
7
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12if ( ! function_exists( 'jetpack_post_details_enqueue_scripts' ) ) {
13
14    /**
15     * The function to include Post Details in a theme's stylesheet.
16     */
17    function jetpack_post_details_enqueue_scripts() {
18        // Make sure we can proceed.
19        list( $should_run, $options, $definied, $post_details ) = jetpack_post_details_should_run();
20
21        if ( ! $should_run ) {
22            return;
23        }
24
25        list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options;
26        list( $date, $categories, $tags, $author, $comment )                                    = $definied;
27
28        $elements = array();
29
30        // If date option is unticked, add it to the list of classes.
31        if ( 1 !== (int) $date_option && ! empty( $date ) ) {
32            $elements[] = $date;
33        }
34
35        // If categories option is unticked, add it to the list of classes.
36        if ( 1 !== (int) $categories_option && ! empty( $categories ) ) {
37            $elements[] = $categories;
38        }
39
40        // If tags option is unticked, add it to the list of classes.
41        if ( 1 !== (int) $tags_option && ! empty( $tags ) ) {
42            $elements[] = $tags;
43        }
44
45        // If author option is unticked, add it to the list of classes.
46        if ( 1 !== (int) $author_option && ! empty( $author ) ) {
47            $elements[] = $author;
48        }
49
50        // If comment option is unticked, add it to the list of classes.
51        if ( 1 !== (int) $comment_option && ! empty( $comment ) ) {
52            $elements[] = $comment;
53        }
54
55        // If the Elements array is empty, return without setting custom CSS.
56        if ( empty( $elements ) ) {
57            return;
58        }
59
60        // Get the list of classes.
61        $elements = implode( ', ', $elements );
62
63        // Hide the classes with CSS.
64        $css = $elements . ' { clip: rect(1px, 1px, 1px, 1px); height: 1px; position: absolute; overflow: hidden; width: 1px; }';
65
66        // Add the CSS to the stylesheet.
67
68        if ( is_array( $post_details ) && isset( $post_details['stylesheet'] ) ) {
69            wp_add_inline_style( $post_details['stylesheet'], $css );
70        }
71    }
72    add_action( 'wp_enqueue_scripts', 'jetpack_post_details_enqueue_scripts' );
73
74}
75
76if ( ! function_exists( 'jetpack_post_details_body_classes' ) ) {
77
78    /**
79     * Adds custom classes to the array of body classes.
80     *
81     * @param array $classes Classes for the body element.
82     */
83    function jetpack_post_details_body_classes( $classes ) {
84        // Make sure we can proceed.
85        list( $should_run, $options, $definied ) = jetpack_post_details_should_run();
86
87        if ( ! $should_run ) {
88            return $classes;
89        }
90
91        list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options;
92        list( $date, $categories, $tags, $author, $comment )                                    = $definied;
93
94        // If date option is unticked, add a class of 'date-hidden' to the body.
95        if ( 1 !== (int) $date_option && ! empty( $date ) ) {
96            $classes[] = 'date-hidden';
97        }
98
99        // If categories option is unticked, add a class of 'categories-hidden' to the body.
100        if ( 1 !== (int) $categories_option && ! empty( $categories ) ) {
101            $classes[] = 'categories-hidden';
102        }
103
104        // If tags option is unticked, add a class of 'tags-hidden' to the body.
105        if ( 1 !== (int) $tags_option && ! empty( $tags ) ) {
106            $classes[] = 'tags-hidden';
107        }
108
109        // If author option is unticked, add a class of 'author-hidden' to the body.
110        if ( 1 !== (int) $author_option && ! empty( $author ) ) {
111            $classes[] = 'author-hidden';
112        }
113
114        // If comment option is unticked, add a class of 'comment-hidden' to the body.
115        if ( 1 !== (int) $comment_option && ! empty( $comment ) ) {
116            $classes[] = 'comment-hidden';
117        }
118
119        return $classes;
120    }
121    add_filter( 'body_class', 'jetpack_post_details_body_classes' );
122
123}
124
125if ( ! function_exists( 'jetpack_post_details_should_run' ) ) {
126
127    /**
128     * Determines if Post Details should run.
129     */
130    function jetpack_post_details_should_run() {
131        // Empty value representing falsy return value.
132        $void = array( false, null, null, null );
133
134        // If the theme doesn't support 'jetpack-content-options', don't continue.
135        if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
136            return $void;
137        }
138
139        $options      = get_theme_support( 'jetpack-content-options' );
140        $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
141
142        // If the theme doesn't support 'jetpack-content-options[ 'post-details' ]', don't continue.
143        if ( empty( $post_details ) ) {
144            return $void;
145        }
146
147        $date       = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
148        $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
149        $tags       = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
150        $author     = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
151        $comment    = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
152
153        // If there is no stylesheet and there are no date, categories, tags, author or comment declared, don't continue.
154        if (
155            empty( $post_details['stylesheet'] )
156            && ( empty( $date )
157                || empty( $categories )
158                || empty( $tags )
159                || empty( $author )
160                || empty( $comment ) )
161        ) {
162            return $void;
163        }
164
165        if ( class_exists( 'Jetpack_Options' ) ) {
166
167            $date_option       = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_date', 1 ); // @phan-suppress-current-line PhanUndeclaredClassMethod -- We're only running this code if the class exists.
168            $categories_option = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_categories', 1 ); // @phan-suppress-current-line PhanUndeclaredClassMethod -- We're only running this code if the class exists.
169            $tags_option       = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_tags', 1 ); // @phan-suppress-current-line PhanUndeclaredClassMethod -- We're only running this code if the class exists.
170            $author_option     = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_author', 1 ); // @phan-suppress-current-line PhanUndeclaredClassMethod -- We're only running this code if the class exists.
171            $comment_option    = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_content_post_details_comment', 1 ); // @phan-suppress-current-line PhanUndeclaredClassMethod -- We're only running this code if the class exists.
172
173            $options  = array( $date_option, $categories_option, $tags_option, $author_option, $comment_option );
174            $definied = array( $date, $categories, $tags, $author, $comment );
175
176            // If all the options are ticked, don't continue.
177            if ( array( 1, 1, 1, 1, 1 ) === $options ) {
178                return $void;
179            }
180
181            return array( true, $options, $definied, $post_details );
182
183        }
184    }
185
186}