summaryrefslogtreecommitdiff
blob: 7851a5be49c9896ad4f719509238fc496b93da2b (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
<?php
/**
 * The function to include Post Details in a theme's stylesheet.
 */
function jetpack_post_details_enqueue_scripts() {
	// Make sure we can proceed.
	list( $should_run, $options, $definied, $post_details ) = jetpack_post_details_should_run();

	if ( ! $should_run ) {
		return;
	}

	list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options;
	list( $date, $categories, $tags, $author, $comment )                                    = $definied;

	$elements = array();

	// If date option is unticked, add it to the list of classes.
	if ( 1 != $date_option && ! empty( $date ) ) {
		$elements[] = $date;
	}

	// If categories option is unticked, add it to the list of classes.
	if ( 1 != $categories_option && ! empty( $categories ) ) {
		$elements[] = $categories;
	}

	// If tags option is unticked, add it to the list of classes.
	if ( 1 != $tags_option && ! empty( $tags ) ) {
		$elements[] = $tags;
	}

	// If author option is unticked, add it to the list of classes.
	if ( 1 != $author_option && ! empty( $author ) ) {
		$elements[] = $author;
	}

	// If comment option is unticked, add it to the list of classes.
	if ( 1 != $comment_option && ! empty( $comment ) ) {
		$elements[] = $comment;
	}

	// Get the list of classes.
	$elements = implode( ', ', $elements );

	// Hide the classes with CSS.
	$css = $elements . ' { clip: rect(1px, 1px, 1px, 1px); height: 1px; position: absolute; overflow: hidden; width: 1px; }';

	// Add the CSS to the stylesheet.
	wp_add_inline_style( $post_details['stylesheet'], $css );
}
add_action( 'wp_enqueue_scripts', 'jetpack_post_details_enqueue_scripts' );

/**
 * Adds custom classes to the array of body classes.
 */
function jetpack_post_details_body_classes( $classes ) {
	// Make sure we can proceed.
	list( $should_run, $options, $definied ) = jetpack_post_details_should_run();

	if ( ! $should_run ) {
		return $classes;
	}

	list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options;
	list( $date, $categories, $tags, $author, $comment )                                    = $definied;

	// If date option is unticked, add a class of 'date-hidden' to the body.
	if ( 1 != $date_option && ! empty( $date ) ) {
		$classes[] = 'date-hidden';
	}

	// If categories option is unticked, add a class of 'categories-hidden' to the body.
	if ( 1 != $categories_option && ! empty( $categories ) ) {
		$classes[] = 'categories-hidden';
	}

	// If tags option is unticked, add a class of 'tags-hidden' to the body.
	if ( 1 != $tags_option && ! empty( $tags ) ) {
		$classes[] = 'tags-hidden';
	}

	// If author option is unticked, add a class of 'author-hidden' to the body.
	if ( 1 != $author_option && ! empty( $author ) ) {
		$classes[] = 'author-hidden';
	}

	// If comment option is unticked, add a class of 'comment-hidden' to the body.
	if ( 1 != $comment_option && ! empty( $comment ) ) {
		$classes[] = 'comment-hidden';
	}

	return $classes;
}
add_filter( 'body_class', 'jetpack_post_details_body_classes' );

/**
 * Determines if Post Details should run.
 */
function jetpack_post_details_should_run() {
	// Empty value representing falsy return value.
	$void = array( false, null, null, null );

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

	$options      = get_theme_support( 'jetpack-content-options' );
	$post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;

	// If the theme doesn't support 'jetpack-content-options[ 'post-details' ]', don't continue.
	if ( empty( $post_details ) ) {
		return $void;
	}

	$date       = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
	$categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
	$tags       = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
	$author     = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
	$comment    = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;

	// If there is no stylesheet and there are no date, categories, tags, author or comment declared, don't continue.
	if (
		empty( $post_details['stylesheet'] )
		&& ( empty( $date )
			|| empty( $categories )
			|| empty( $tags )
			|| empty( $author )
			|| empty( $comment ) )
	) {
		return $void;
	}

	$date_option       = get_option( 'jetpack_content_post_details_date', 1 );
	$categories_option = get_option( 'jetpack_content_post_details_categories', 1 );
	$tags_option       = get_option( 'jetpack_content_post_details_tags', 1 );
	$author_option     = get_option( 'jetpack_content_post_details_author', 1 );
	$comment_option    = get_option( 'jetpack_content_post_details_comment', 1 );

	$options  = array( $date_option, $categories_option, $tags_option, $author_option, $comment_option );
	$definied = array( $date, $categories, $tags, $author, $comment );

	// If all the options are ticked, don't continue.
	if ( array( 1, 1, 1, 1, 1 ) === $options ) {
		return $void;
	}

	return array( true, $options, $definied, $post_details );
}