Completed
Push — master-stable ( a82972...9baba8 )
by
unknown
27:19 queued 17:47
created

content-options.php ➔ jetpack_featured_images_get_settings()   F

Complexity

Conditions 17
Paths > 20000

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 17
eloc 21
nc 65536
nop 0
dl 0
loc 28
rs 2.7204
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
 * Content Options.
4
 *
5
 * This feature will only be activated for themes that declare their support.
6
 * This can be done by adding code similar to the following during the
7
 * 'after_setup_theme' action:
8
 *
9
	add_theme_support( 'jetpack-content-options', array(
10
		'blog-display'       => 'content', // the default setting of the theme: 'content', 'excerpt' or array( 'content', 'excerpt' ) for themes mixing both display.
11
		'author-bio'         => true, // display or not the author bio: true or false.
12
		'author-bio-default' => false, // the default setting of the author bio, if it's being displayed or not: true or false (only required if false).
13
		'masonry'            => '.site-main', // a CSS selector matching the elements that triggers a masonry refresh if the theme is using a masonry layout.
14
		'post-details'       => array(
15
			'stylesheet'        => 'themeslug-style', // name of the theme's stylesheet.
16
			'date'              => '.posted-on', // a CSS selector matching the elements that display the post date.
17
			'categories'        => '.cat-links', // a CSS selector matching the elements that display the post categories.
18
			'tags'              => '.tags-links', // a CSS selector matching the elements that display the post tags.
19
			'author'            => '.byline', // a CSS selector matching the elements that display the post author.
20
			'comment'           => '.comments-link', // a CSS selector matching the elements that display the comment link.
21
		),
22
		'featured-images'    => array(
23
			'archive'           => true, // enable or not the featured image check for archive pages: true or false.
24
			'archive-default'   => false, // the default setting of the featured image on archive pages, if it's being displayed or not: true or false (only required if false).
25
			'post'              => true, // enable or not the featured image check for single posts: true or false.
26
			'post-default'      => false, // the default setting of the featured image on single posts, if it's being displayed or not: true or false (only required if false).
27
			'page'              => true, // enable or not the featured image check for single pages: true or false.
28
			'page-default'      => false, // the default setting of the featured image on single pages, if it's being displayed or not: true or false (only required if false).
29
			'portfolio'         => true, // enable or not the featured image check for single projects: true or false.
30
			'portfolio-default' => false, // the default setting of the featured image on single projects, if it's being displayed or not: true or false (only required if false).
31
			'fallback'          => true, // enable or not the featured image fallback: true or false.
32
			'fallback-default'  => true, // the default setting for featured image fallbacks: true or false (only required if false)
33
		),
34
	) );
35
 *
36
 */
37
38
/**
39
 * Activate the Content Options plugin.
40
 *
41
 * @uses current_theme_supports()
42
 */
43
function jetpack_content_options_init() {
44
	// If the theme doesn't support 'jetpack-content-options', don't continue.
45
	if ( ! current_theme_supports( 'jetpack-content-options' ) ) {
46
		return;
47
	}
48
49
	// Load the Customizer options.
50
	require( dirname( __FILE__ ) . '/content-options/customizer.php' );
51
52
	// Load Blog Display function.
53
	require( dirname( __FILE__ ) . '/content-options/blog-display.php' );
54
55
	// Load Author Bio function.
56
	require( dirname( __FILE__ ) . '/content-options/author-bio.php' );
57
58
	// Load Post Details function.
59
	require( dirname( __FILE__ ) . '/content-options/post-details.php' );
60
61
	// Load Featured Images function.
62
	if ( jetpack_featured_images_should_load() ) {
63
		require( dirname( __FILE__ ) . '/content-options/featured-images.php' );
64
	}
65
66
	// Load Featured Images Fallback function.
67
	if ( jetpack_featured_images_fallback_should_load() ) {
68
		require( dirname( __FILE__ ) . '/content-options/featured-images-fallback.php' );
69
	}
70
}
71
add_action( 'init', 'jetpack_content_options_init' );
72
73
function jetpack_featured_images_get_settings() {
74
	$options         = get_theme_support( 'jetpack-content-options' );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 9 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
75
76
	$featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null;
77
78
	$settings        = array(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 8 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
79
		'archive'           => ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null,
80
		'post'              => ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null,
81
		'page'              => ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null,
82
		'portfolio'         => ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null,
83
		'archive-default'   => ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1,
84
		'post-default'      => ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1,
85
		'page-default'      => ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1,
86
		'portfolio-default' => ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1,
87
		'fallback'          => ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null,
88
		'fallback-default'  => ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1,
89
	);
90
91
	$settings        = array_merge( $settings, array(
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 8 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
92
		'archive-option'   => get_option( 'jetpack_content_featured_images_archive', $settings['archive-default'] ),
93
		'post-option'      => get_option( 'jetpack_content_featured_images_post', $settings['post-default'] ),
94
		'page-option'      => get_option( 'jetpack_content_featured_images_page', $settings['page-default'] ),
95
		'portfolio-option' => get_option( 'jetpack_content_featured_images_portfolio', $settings['portfolio-default'] ),
96
		'fallback-option'  => get_option( 'jetpack_content_featured_images_fallback', $settings['fallback-default'] ),
97
	) );
98
99
	return $settings;
100
}
101
102
function jetpack_featured_images_should_load() {
103
	// If the theme doesn't support post thumbnails, don't continue.
104
	if ( ! current_theme_supports( 'post-thumbnails' ) ) {
105
		return false;
106
	}
107
108
	$opts = jetpack_featured_images_get_settings();
109
110
	// If the theme doesn't support archive, post and page or if all the options are ticked and we aren't in the customizer, don't continue.
111
	if (
112
		( true !== $opts['archive'] && true !== $opts['post'] && true !== $opts['page'] )
113
		|| ( 1 === $opts['archive-option'] && 1 === $opts['post-option'] && 1 === $opts['page-option'] && ! is_customize_preview() )
114
	) {
115
		return false;
116
	}
117
118
	return true;
119
}
120
121
function jetpack_featured_images_fallback_should_load() {
122
	// If the theme doesn't support post thumbnails, don't continue.
123
	if ( ! current_theme_supports( 'post-thumbnails' ) ) {
124
		return false;
125
	}
126
127
	$opts = jetpack_featured_images_get_settings();
128
129
	// If the theme doesn't support fallback, don't continue.
130
	if ( true !== $opts['fallback'] ) {
131
		return false;
132
	}
133
134
	return true;
135
}
136