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