Completed
Push — fix/sync_published_post_action ( 131f15...e65cb4 )
by
unknown
09:19 queued 01:28
created

post-details.php ➔ jetpack_post_details_body_classes()   D

Complexity

Conditions 10
Paths 17

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 15
nc 17
nop 1
dl 0
loc 33
rs 4.8196
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 ) = $options;
14
	list( $date, $categories, $tags, $author ) = $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
	// Get the list of classes.
39
	$elements = implode( ', ', $elements );
40
41
	// Hide the classes with CSS.
42
	$css = $elements . ' { position: absolute; clip: rect(1px, 1px, 1px, 1px); }';
43
44
	// Add the CSS to the stylesheet.
45
	wp_add_inline_style( $post_details['stylesheet'], $css );
46
}
47
add_action( 'wp_enqueue_scripts', 'jetpack_post_details_enqueue_scripts' );
48
49
/**
50
 * Adds custom classes to the array of body classes.
51
 */
52
function jetpack_post_details_body_classes( $classes ) {
53
	// Make sure we can proceed.
54
	list( $should_run, $options, $definied ) = jetpack_post_details_should_run();
55
56
	if ( ! $should_run ) {
57
		return $classes;
58
	}
59
60
	list( $date_option, $categories_option, $tags_option, $author_option ) = $options;
61
	list( $date, $categories, $tags, $author ) = $definied;
62
63
	// If date option is unticked, add a class of 'date-hidden' to the body.
64
	if ( 1 != $date_option && ! empty( $date ) ) {
65
		$classes[] = 'date-hidden';
66
	}
67
68
	// If categories option is unticked, add a class of 'categories-hidden' to the body.
69
	if ( 1 != $categories_option && ! empty( $categories ) ) {
70
		$classes[] = 'categories-hidden';
71
	}
72
73
	// If tags option is unticked, add a class of 'tags-hidden' to the body.
74
	if ( 1 != $tags_option && ! empty( $tags ) ) {
75
		$classes[] = 'tags-hidden';
76
	}
77
78
	// If author option is unticked, add a class of 'author-hidden' to the body.
79
	if ( 1 != $author_option && ! empty( $author ) ) {
80
		$classes[] = 'author-hidden';
81
	}
82
83
	return $classes;
84
}
85
add_filter( 'body_class', 'jetpack_post_details_body_classes' );
86
87
/**
88
 * Determines if Post Details should run.
89
 */
90
function jetpack_post_details_should_run() {
91
	// Empty value representing falsy return value.
92
	$void = array( false, null, null, null );
93
94
	// If the theme doesn't support 'jetpack-content-options', don't continue.
95
	if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
96
		return $void;
97
	}
98
99
	$options      = get_theme_support( 'jetpack-content-options' );
100
	$post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
101
102
	// If the theme doesn't support 'jetpack-content-options[ 'post-details' ]', don't continue.
103
	if ( empty( $post_details ) ) {
104
		return $void;
105
	}
106
107
	$date       = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
108
	$categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
109
	$tags       = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
110
	$author     = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
111
112
	// If there is no stylesheet and there are no date, categories, tags or author declared, don't continue.
113
	if ( empty( $post_details['stylesheet'] )
114
		 && ( empty( $date )
115
			 || empty( $categories )
116
			 || empty( $tags )
117
			 || empty( $author ) ) ) {
118
		return $void;
119
	}
120
121
	$date_option       = get_option( 'jetpack_content_post_details_date', 1 );
122
	$categories_option = get_option( 'jetpack_content_post_details_categories', 1 );
123
	$tags_option       = get_option( 'jetpack_content_post_details_tags', 1 );
124
	$author_option     = get_option( 'jetpack_content_post_details_author', 1 );
125
126
	$options  = array( $date_option, $categories_option, $tags_option, $author_option );
127
	$definied = array( $date, $categories, $tags, $author );
128
129
	// If all the options are ticked, don't continue.
130
	if ( array( 1, 1, 1, 1 ) === $options ) {
131
		return $void;
132
	}
133
134
	return array( true, $options, $definied, $post_details );
135
}
136