Completed
Push — add/jetpack-start-reconnect-us... ( 7f1fbb )
by
unknown
10:36
created

post-details.php ➔ jetpack_post_details_enqueue_scripts()   C

Complexity

Conditions 12
Paths 33

Size

Total Lines 47
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 20
nc 33
nop 0
dl 0
loc 47
rs 5.1384
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * The function to include Post Details in a theme's stylesheet.
4
 */
5
function jetpack_post_details_enqueue_scripts() {
6
	// Make sure we can proceed.
7
	list( $should_run, $options, $definied, $post_details ) = jetpack_post_details_should_run();
8
9
	if ( ! $should_run ) {
10
		return;
11
	}
12
13
	list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options;
14
	list( $date, $categories, $tags, $author, $comment ) = $definied;
15
16
	$elements = array();
17
18
	// If date option is unticked, add it to the list of classes.
19
	if ( 1 != $date_option && ! empty( $date ) ) {
20
		$elements[] = $date;
21
	}
22
23
	// If categories option is unticked, add it to the list of classes.
24
	if ( 1 != $categories_option && ! empty( $categories ) ) {
25
		$elements[] = $categories;
26
	}
27
28
	// If tags option is unticked, add it to the list of classes.
29
	if ( 1 != $tags_option && ! empty( $tags ) ) {
30
		$elements[] = $tags;
31
	}
32
33
	// If author option is unticked, add it to the list of classes.
34
	if ( 1 != $author_option && ! empty( $author ) ) {
35
		$elements[] = $author;
36
	}
37
38
	// If comment option is unticked, add it to the list of classes.
39
	if ( 1 != $comment_option && ! empty( $comment ) ) {
40
		$elements[] = $comment;
41
	}
42
43
	// Get the list of classes.
44
	$elements = implode( ', ', $elements );
45
46
	// Hide the classes with CSS.
47
	$css = $elements . ' { clip: rect(1px, 1px, 1px, 1px); height: 1px; position: absolute; overflow: hidden; width: 1px; }';
48
49
	// Add the CSS to the stylesheet.
50
	wp_add_inline_style( $post_details['stylesheet'], $css );
51
}
52
add_action( 'wp_enqueue_scripts', 'jetpack_post_details_enqueue_scripts' );
53
54
/**
55
 * Adds custom classes to the array of body classes.
56
 */
57
function jetpack_post_details_body_classes( $classes ) {
58
	// Make sure we can proceed.
59
	list( $should_run, $options, $definied ) = jetpack_post_details_should_run();
60
61
	if ( ! $should_run ) {
62
		return $classes;
63
	}
64
65
	list( $date_option, $categories_option, $tags_option, $author_option, $comment_option ) = $options;
66
	list( $date, $categories, $tags, $author, $comment ) = $definied;
67
68
	// If date option is unticked, add a class of 'date-hidden' to the body.
69
	if ( 1 != $date_option && ! empty( $date ) ) {
70
		$classes[] = 'date-hidden';
71
	}
72
73
	// If categories option is unticked, add a class of 'categories-hidden' to the body.
74
	if ( 1 != $categories_option && ! empty( $categories ) ) {
75
		$classes[] = 'categories-hidden';
76
	}
77
78
	// If tags option is unticked, add a class of 'tags-hidden' to the body.
79
	if ( 1 != $tags_option && ! empty( $tags ) ) {
80
		$classes[] = 'tags-hidden';
81
	}
82
83
	// If author option is unticked, add a class of 'author-hidden' to the body.
84
	if ( 1 != $author_option && ! empty( $author ) ) {
85
		$classes[] = 'author-hidden';
86
	}
87
88
	// If comment option is unticked, add a class of 'comment-hidden' to the body.
89
	if ( 1 != $comment_option && ! empty( $comment ) ) {
90
		$classes[] = 'comment-hidden';
91
	}
92
93
	return $classes;
94
}
95
add_filter( 'body_class', 'jetpack_post_details_body_classes' );
96
97
/**
98
 * Determines if Post Details should run.
99
 */
100
function jetpack_post_details_should_run() {
101
	// Empty value representing falsy return value.
102
	$void = array( false, null, null, null );
103
104
	// If the theme doesn't support 'jetpack-content-options', don't continue.
105
	if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
106
		return $void;
107
	}
108
109
	$options      = get_theme_support( 'jetpack-content-options' );
110
	$post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
111
112
	// If the theme doesn't support 'jetpack-content-options[ 'post-details' ]', don't continue.
113
	if ( empty( $post_details ) ) {
114
		return $void;
115
	}
116
117
	$date       = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
118
	$categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
119
	$tags       = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
120
	$author     = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
121
	$comment    = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
122
123
	// If there is no stylesheet and there are no date, categories, tags, author or comment declared, don't continue.
124
	if ( empty( $post_details['stylesheet'] )
125
		 && ( empty( $date )
126
			 || empty( $categories )
127
			 || empty( $tags )
128
			 || empty( $author )
129
			 || empty( $comment ) ) ) {
130
		return $void;
131
	}
132
133
	$date_option       = get_option( 'jetpack_content_post_details_date', 1 );
134
	$categories_option = get_option( 'jetpack_content_post_details_categories', 1 );
135
	$tags_option       = get_option( 'jetpack_content_post_details_tags', 1 );
136
	$author_option     = get_option( 'jetpack_content_post_details_author', 1 );
137
	$comment_option    = get_option( 'jetpack_content_post_details_comment', 1 );
138
139
	$options  = array( $date_option, $categories_option, $tags_option, $author_option, $comment_option );
140
	$definied = array( $date, $categories, $tags, $author, $comment );
141
142
	// If all the options are ticked, don't continue.
143
	if ( array( 1, 1, 1, 1, 1 ) === $options ) {
144
		return $void;
145
	}
146
147
	return array( true, $options, $definied, $post_details );
148
}
149