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

Jetpack_Related_Posts_Customize::render_callback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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' 	  => 89,
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
		return apply_filters(
150
			'jetpack_related_posts_customize_options', array(
151
				'show_headline'       => array(
152
					'label'        => esc_html__( 'Show a headline', 'jetpack' ),
153
					'description'  => esc_html__( 'This helps to clearly separate the related posts from post content.', 'jetpack' ),
154
					'control_type' => 'checkbox',
155
					'default'      => 1,
156
					'setting_type' => 'option',
157
					'transport'    => $transport,
158
				),
159
				'headline'       => array(
160
					'label'        => '',
161
					'description'  => esc_html__( 'Enter text to use as headline.', 'jetpack' ),
162
					'control_type' => 'text',
163
					'default'      => esc_html__( 'Related', 'jetpack' ),
164
					'setting_type' => 'option',
165
					'transport'    => $transport,
166
				),
167
				'show_thumbnails'     => array(
168
					'label'        => esc_html__( 'Show thumbnails', 'jetpack' ),
169
					'description'  => esc_html__( 'Use a large and visually striking layout.', 'jetpack' ),
170
					'control_type' => 'checkbox',
171
					'default'      => 1,
172
					'setting_type' => 'option',
173
					'transport'    => $transport,
174
				),
175
				'show_date'           => array(
176
					'label'        => esc_html__( 'Show date', 'jetpack' ),
177
					'description'  => esc_html__( 'Display date when entry was published.', 'jetpack' ),
178
					'control_type' => 'checkbox',
179
					'default'      => 1,
180
					'setting_type' => 'option',
181
					'transport'    => $transport,
182
				),
183
				'show_context'        => array(
184
					'label'        => esc_html__( 'Show context', 'jetpack' ),
185
					'description'  => esc_html__( "Display entry's category or tag.", 'jetpack' ),
186
					'control_type' => 'checkbox',
187
					'default'      => 1,
188
					'setting_type' => 'option',
189
					'transport'    => $transport,
190
				),
191
				'layout'        => array(
192
					'label'        => esc_html__( 'Layout', 'jetpack' ),
193
					'description'  => esc_html__( 'Arrange entries in different layouts.', 'jetpack' ),
194
					'control_type' => 'select',
195
					'choices'	   => array(
196
						'grid' => esc_html__( 'Grid', 'jetpack' ),
197
						'list' => esc_html__( 'List', 'jetpack' ),
198
					),
199
					'default'      => 'grid',
200
					'setting_type' => 'option',
201
					'transport'    => $transport,
202
				),
203
				'msg_go_to_single' => array(
204
					'description'     => esc_html__( 'Please visit a single post view to reveal the customization options.', 'jetpack' ),
205
					'control_type'    => 'message',
206
					'active_callback' => __CLASS__ . '::is_not_single',
207
				),
208
				'msg_example'      => array(
209
					'description'  => esc_html__( 'Please note that the related posts displayed now are only for previewing purposes.', 'jetpack' ),
210
					'control_type' => 'message',
211
				),
212
			)
213
		);
214
	}
215
216
	/**
217
	 * Enqueue assets for Customizer controls.
218
	 *
219
	 * @since 4.4.0
220
	 */
221
	function customize_controls_enqueue_scripts() {
222
		wp_enqueue_script( 'jetpack_related-posts-customizer', plugins_url( 'related-posts-customizer.js', __FILE__ ), array( 'customize-controls' ), JETPACK__VERSION);
223
	}
224
225
} // class end
226
227
/**
228
 * Control that displays a message in Customizer.
229
 *
230
 * @since 4.4.0
231
 */
232
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...
233
234
	/**
235
	 * Render the message.
236
	 *
237
	 * @since 4.4.0
238
	 */
239
	public function render_content() {
240
		echo '<p class="description">' . esc_html( $this->description ) . '</p>';
241
	}
242
} // class end
243
244
// Initialize controls
245
new Jetpack_Related_Posts_Customize;