Completed
Push — update/remove-disconnect-link ( 4b6a2c )
by
unknown
73:18 queued 63:47
created

Jetpack_Related_Posts_Customize::get_options()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 84
Code Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 67
nc 8
nop 1
dl 0
loc 84
rs 8.4704
c 0
b 0
f 0

How to fix   Long Method   

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
// Exit if file is accessed directly
4
if ( ! defined( 'ABSPATH' ) ) {
5
	exit;
6
}
7
8
/**
9
 * Class to include elements to modify Related Posts look in Customizer.
10
 *
11
 * @since 4.4.0
12
 */
13
class Jetpack_Related_Posts_Customize {
14
15
	/**
16
	 * Key for panel, section and prefix for options. Same option name than in Options > Reading.
17
	 *
18
	 * @var string
19
	 */
20
	var $prefix = 'jetpack_relatedposts';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $prefix.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
21
22
	/**
23
	 * @var string Control to focus when customizer loads.
24
	 */
25
	var $focus = '';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $focus.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
26
27
	/**
28
	 * Class initialization.
29
	 *
30
	 * @since 4.4.0
31
	 */
32
	function __construct() {
33
		add_action( 'customize_register', array( $this, 'customize_register' ) );
34
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) );
35
	}
36
37
	/**
38
	 * Initialize Customizer controls.
39
	 *
40
	 * @since 4.4.0
41
	 *
42
	 * @param WP_Customize_Manager $wp_customize Customizer instance.
43
	 */
44
	function customize_register( $wp_customize ) {
45
46
		$wp_customize->add_section( $this->prefix,
47
			array(
48
				'title' 	  => esc_html__( 'Related Posts', 'jetpack' ),
49
				'description' => '',
50
				'capability'  => 'edit_theme_options',
51
				'priority' 	  => 200,
52
			)
53
		);
54
55
		$selective_options = array();
56
57
		foreach ( $this->get_options( $wp_customize ) as $key => $field ) {
58
			$control_id = "$this->prefix[$key]";
59
			$selective_options[] = $control_id;
60
			$wp_customize->add_setting( $control_id,
61
				array(
62
					'default' 	 => isset( $field['default'] ) ? $field['default'] : '',
63
					'type' 		 => isset( $field['setting_type'] ) ? $field['setting_type'] : 'option',
64
					'capability' => isset( $field['capability'] ) ? $field['capability'] : 'edit_theme_options',
65
					'transport'  => isset( $field['transport'] ) ? $field['transport'] : 'postMessage',
66
				)
67
			);
68
			$control_settings = array(
69
				'label' 	  => isset( $field['label'] ) ? $field['label'] : '',
70
				'description' => isset( $field['description'] ) ? $field['description'] : '',
71
				'settings' 	  => $control_id,
72
				'type' 	      => isset( $field['control_type'] ) ? $field['control_type'] : 'text',
73
				'section' 	  => $this->prefix,
74
				'priority' 	  => 10,
75
				'active_callback' => isset( $field['active_callback'] ) ? $field['active_callback'] : __CLASS__ . '::is_single',
76
			);
77
			switch ( $field['control_type'] ) {
78
				case 'text':
79
				case 'checkbox':
80
				default:
81
					$wp_customize->add_control( new WP_Customize_Control( $wp_customize, $control_id, $control_settings ) );
82
					break;
83
				case 'select':
0 ignored issues
show
Unused Code introduced by
case 'select': if (i...ngs)); } break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
84
					if ( isset( $field['choices'] ) ) {
85
						$control_settings['choices'] = $field['choices'];
86
						$wp_customize->add_control( new WP_Customize_Control( $wp_customize, $control_id, $control_settings ) );
87
					}
88
					break;
89
				case 'message':
90
					$wp_customize->add_control( new Jetpack_Message_Control( $wp_customize, $control_id, $control_settings ) );
91
					break;
92
			}
93
		}
94
95
		// If selective refresh is available, implement it.
96
		if ( isset( $wp_customize->selective_refresh ) ) {
97
			$wp_customize->selective_refresh->add_partial( "$this->prefix", array(
98
				'selector'            => '.jp-relatedposts',
99
				'settings'            => $selective_options,
100
				'render_callback'     => __CLASS__ . '::render_callback',
101
				'container_inclusive' => false,
102
			) );
103
		}
104
105
	}
106
107
	/**
108
	 * Callback that outputs the headline based on user choice.
109
	 *
110
	 * @since 4.4.0
111
	 */
112
	public static function render_callback() {
113
		echo Jetpack_RelatedPosts::init()->get_headline();
114
	}
115
116
	/**
117
	 * Check that we're in a single post view.
118
	 *
119
	 * @since 4.4.0
120
	 *
121
	 * @return bool
122
	 */
123
	public static function is_single() {
124
		return is_single();
125
	}
126
127
	/**
128
	 * Check that we're not in a single post view.
129
	 *
130
	 * @since 4.4.0
131
	 *
132
	 * @return bool
133
	 */
134
	public static function is_not_single() {
135
		return ! is_single();
136
	}
137
138
	/**
139
	 * Return list of options to modify.
140
	 *
141
	 * @since 4.4.0
142
	 *
143
	 * @param object $wp_customize Instance of WP Customizer
144
	 *
145
	 * @return mixed|void
146
	 */
147
	function get_options( $wp_customize ) {
148
		$transport = isset( $wp_customize->selective_refresh ) ? 'postMessage' : 'refresh';
149
150
		// Get the correct translated string for preview in WP 4.7 and later.
151
		$switched_locale = function_exists( 'switch_to_locale' )
152
			? switch_to_locale( get_user_locale() )
153
			: false;
154
		$headline = __( 'Related', 'jetpack' );
155
		if ( $switched_locale ) {
156
			restore_previous_locale();
157
		}
158
159
		return apply_filters(
160
			'jetpack_related_posts_customize_options', array(
161
				'enabled'       => array(
162
					'control_type' => 'hidden',
163
					'default'      => 1,
164
					'setting_type' => 'option',
165
					'transport'    => $transport,
166
				),
167
				'show_headline'       => array(
168
					'label'        => esc_html__( 'Show a headline', 'jetpack' ),
169
					'description'  => esc_html__( 'This helps to clearly separate the related posts from post content.', 'jetpack' ),
170
					'control_type' => 'checkbox',
171
					'default'      => 1,
172
					'setting_type' => 'option',
173
					'transport'    => $transport,
174
				),
175
				'headline'       => array(
176
					'label'        => '',
177
					'description'  => esc_html__( 'Enter text to use as headline.', 'jetpack' ),
178
					'control_type' => 'text',
179
					'default'      => esc_html( $headline ),
180
					'setting_type' => 'option',
181
					'transport'    => $transport,
182
				),
183
				'show_thumbnails'     => array(
184
					'label'        => esc_html__( 'Show thumbnails', 'jetpack' ),
185
					'description'  => esc_html__( 'Use a large and visually striking layout.', 'jetpack' ),
186
					'control_type' => 'checkbox',
187
					'default'      => 1,
188
					'setting_type' => 'option',
189
					'transport'    => $transport,
190
				),
191
				'show_date'           => array(
192
					'label'        => esc_html__( 'Show date', 'jetpack' ),
193
					'description'  => esc_html__( 'Display date when entry was published.', 'jetpack' ),
194
					'control_type' => 'checkbox',
195
					'default'      => 1,
196
					'setting_type' => 'option',
197
					'transport'    => $transport,
198
				),
199
				'show_context'        => array(
200
					'label'        => esc_html__( 'Show context', 'jetpack' ),
201
					'description'  => esc_html__( "Display entry's category or tag.", 'jetpack' ),
202
					'control_type' => 'checkbox',
203
					'default'      => 1,
204
					'setting_type' => 'option',
205
					'transport'    => $transport,
206
				),
207
				'layout'        => array(
208
					'label'        => esc_html__( 'Layout', 'jetpack' ),
209
					'description'  => esc_html__( 'Arrange entries in different layouts.', 'jetpack' ),
210
					'control_type' => 'select',
211
					'choices'	   => array(
212
						'grid' => esc_html__( 'Grid', 'jetpack' ),
213
						'list' => esc_html__( 'List', 'jetpack' ),
214
					),
215
					'default'      => 'grid',
216
					'setting_type' => 'option',
217
					'transport'    => $transport,
218
				),
219
				'msg_go_to_single' => array(
220
					'description'     => esc_html__( 'Please visit a single post view to reveal the customization options.', 'jetpack' ),
221
					'control_type'    => 'message',
222
					'active_callback' => __CLASS__ . '::is_not_single',
223
				),
224
				'msg_example'      => array(
225
					'description'  => esc_html__( 'Please note that the related posts displayed now are only for previewing purposes.', 'jetpack' ),
226
					'control_type' => 'message',
227
				),
228
			)
229
		);
230
	}
231
232
	/**
233
	 * Enqueue assets for Customizer controls.
234
	 *
235
	 * @since 4.4.0
236
	 */
237
	function customize_controls_enqueue_scripts() {
238
		wp_enqueue_script( 'jetpack_related-posts-customizer', plugins_url( 'related-posts-customizer.js', __FILE__ ), array( 'customize-controls' ), JETPACK__VERSION);
239
	}
240
241
} // class end
242
243
/**
244
 * Control that displays a message in Customizer.
245
 *
246
 * @since 4.4.0
247
 */
248
class Jetpack_Message_Control extends WP_Customize_Control {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
249
250
	/**
251
	 * Render the message.
252
	 *
253
	 * @since 4.4.0
254
	 */
255
	public function render_content() {
256
		echo '<p class="description">' . esc_html( $this->description ) . '</p>';
257
	}
258
} // class end
259
260
// Initialize controls
261
new Jetpack_Related_Posts_Customize;