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'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string Control to focus when customizer loads. |
24
|
|
|
*/ |
25
|
|
|
var $focus = ''; |
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': |
|
|
|
|
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:not(.jp-relatedposts-block)', |
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 whether the current post contains a Related Posts block. |
118
|
|
|
* If we're on WP < 5.0, this automatically means it doesn't, |
119
|
|
|
* because block support is intrododuced in WP 5.0. |
120
|
|
|
* |
121
|
|
|
* @since 6.9.0 |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public static function contains_related_posts_block() { |
126
|
|
|
if ( ! function_exists( 'has_block' ) ) { |
127
|
|
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ( has_block( 'jetpack/related-posts' ) ) { |
131
|
|
|
return true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Check that we're in a single post view. |
139
|
|
|
* Will return `false` if the current post contains a Related Posts block, |
140
|
|
|
* because in that case we want to hide the Customizer controls. |
141
|
|
|
* |
142
|
|
|
* @since 4.4.0 |
143
|
|
|
* |
144
|
|
|
* @return bool |
145
|
|
|
*/ |
146
|
|
|
public static function is_single() { |
147
|
|
|
if ( self::contains_related_posts_block() ) { |
148
|
|
|
return false; |
149
|
|
|
} |
150
|
|
|
return is_single(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Check that we're not in a single post view. |
155
|
|
|
* Will return `false` if the current post contains a Related Posts block, |
156
|
|
|
* because in that case we want to hide the Customizer controls. |
157
|
|
|
* |
158
|
|
|
* @since 4.4.0 |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
|
|
public static function is_not_single() { |
163
|
|
|
if ( self::contains_related_posts_block() ) { |
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
return ! is_single(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Return list of options to modify. |
171
|
|
|
* |
172
|
|
|
* @since 4.4.0 |
173
|
|
|
* |
174
|
|
|
* @param object $wp_customize Instance of WP Customizer |
175
|
|
|
* |
176
|
|
|
* @return mixed|void |
177
|
|
|
*/ |
178
|
|
|
function get_options( $wp_customize ) { |
179
|
|
|
$transport = isset( $wp_customize->selective_refresh ) ? 'postMessage' : 'refresh'; |
180
|
|
|
|
181
|
|
|
// Get the correct translated string for preview in WP 4.7 and later. |
182
|
|
|
$switched_locale = function_exists( 'switch_to_locale' ) |
183
|
|
|
? switch_to_locale( get_user_locale() ) |
184
|
|
|
: false; |
185
|
|
|
$headline = __( 'Related', 'jetpack' ); |
186
|
|
|
if ( $switched_locale ) { |
187
|
|
|
restore_previous_locale(); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* The filter allows you to change the options used to display Related Posts in the Customizer. |
192
|
|
|
* |
193
|
|
|
* @module related-posts |
194
|
|
|
* |
195
|
|
|
* @since 4.4.0 |
196
|
|
|
* |
197
|
|
|
* @param array $options Array of options used to display Related Posts in the Customizer. |
198
|
|
|
*/ |
199
|
|
|
return apply_filters( |
200
|
|
|
'jetpack_related_posts_customize_options', array( |
201
|
|
|
'enabled' => array( |
202
|
|
|
'control_type' => 'hidden', |
203
|
|
|
'default' => 1, |
204
|
|
|
'setting_type' => 'option', |
205
|
|
|
'transport' => $transport, |
206
|
|
|
), |
207
|
|
|
'show_headline' => array( |
208
|
|
|
'label' => esc_html__( 'Show a headline', 'jetpack' ), |
209
|
|
|
'description' => esc_html__( 'This helps to clearly separate the related posts from post content.', 'jetpack' ), |
210
|
|
|
'control_type' => 'checkbox', |
211
|
|
|
'default' => 1, |
212
|
|
|
'setting_type' => 'option', |
213
|
|
|
'transport' => $transport, |
214
|
|
|
), |
215
|
|
|
'headline' => array( |
216
|
|
|
'label' => '', |
217
|
|
|
'description' => esc_html__( 'Enter text to use as headline.', 'jetpack' ), |
218
|
|
|
'control_type' => 'text', |
219
|
|
|
'default' => esc_html( $headline ), |
220
|
|
|
'setting_type' => 'option', |
221
|
|
|
'transport' => $transport, |
222
|
|
|
), |
223
|
|
|
'show_thumbnails' => array( |
224
|
|
|
'label' => esc_html__( 'Show thumbnails', 'jetpack' ), |
225
|
|
|
'description' => esc_html__( 'Show a thumbnail image where available.', 'jetpack' ), |
226
|
|
|
'control_type' => 'checkbox', |
227
|
|
|
'default' => 1, |
228
|
|
|
'setting_type' => 'option', |
229
|
|
|
'transport' => $transport, |
230
|
|
|
), |
231
|
|
|
'show_date' => array( |
232
|
|
|
'label' => esc_html__( 'Show date', 'jetpack' ), |
233
|
|
|
'description' => esc_html__( 'Display date when entry was published.', 'jetpack' ), |
234
|
|
|
'control_type' => 'checkbox', |
235
|
|
|
'default' => 1, |
236
|
|
|
'setting_type' => 'option', |
237
|
|
|
'transport' => $transport, |
238
|
|
|
), |
239
|
|
|
'show_context' => array( |
240
|
|
|
'label' => esc_html__( 'Show context', 'jetpack' ), |
241
|
|
|
'description' => esc_html__( "Display entry's category or tag.", 'jetpack' ), |
242
|
|
|
'control_type' => 'checkbox', |
243
|
|
|
'default' => 1, |
244
|
|
|
'setting_type' => 'option', |
245
|
|
|
'transport' => $transport, |
246
|
|
|
), |
247
|
|
|
'layout' => array( |
248
|
|
|
'label' => esc_html__( 'Layout', 'jetpack' ), |
249
|
|
|
'description' => esc_html__( 'Arrange entries in different layouts.', 'jetpack' ), |
250
|
|
|
'control_type' => 'select', |
251
|
|
|
'choices' => array( |
252
|
|
|
'grid' => esc_html__( 'Grid', 'jetpack' ), |
253
|
|
|
'list' => esc_html__( 'List', 'jetpack' ), |
254
|
|
|
), |
255
|
|
|
'default' => 'grid', |
256
|
|
|
'setting_type' => 'option', |
257
|
|
|
'transport' => $transport, |
258
|
|
|
), |
259
|
|
|
'msg_go_to_single' => array( |
260
|
|
|
'description' => esc_html__( 'Please visit a single post view to reveal the customization options.', 'jetpack' ), |
261
|
|
|
'control_type' => 'message', |
262
|
|
|
'active_callback' => __CLASS__ . '::is_not_single', |
263
|
|
|
), |
264
|
|
|
'msg_example' => array( |
265
|
|
|
'description' => esc_html__( 'Please note that the related posts displayed now are only for previewing purposes.', 'jetpack' ), |
266
|
|
|
'control_type' => 'message', |
267
|
|
|
), |
268
|
|
|
) |
269
|
|
|
); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Enqueue assets for Customizer controls. |
274
|
|
|
* |
275
|
|
|
* @since 4.4.0 |
276
|
|
|
*/ |
277
|
|
|
function customize_controls_enqueue_scripts() { |
278
|
|
|
wp_enqueue_script( |
279
|
|
|
'jetpack_related-posts-customizer', |
280
|
|
|
Jetpack::get_file_url_for_environment( |
281
|
|
|
'_inc/build/related-posts/related-posts-customizer.min.js', |
282
|
|
|
'modules/related-posts/related-posts-customizer.js' |
283
|
|
|
), |
284
|
|
|
array( 'customize-controls' ), |
285
|
|
|
JETPACK__VERSION |
286
|
|
|
); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
} // class end |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Control that displays a message in Customizer. |
293
|
|
|
* |
294
|
|
|
* @since 4.4.0 |
295
|
|
|
*/ |
296
|
|
|
class Jetpack_Message_Control extends WP_Customize_Control { |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Render the message. |
300
|
|
|
* |
301
|
|
|
* @since 4.4.0 |
302
|
|
|
*/ |
303
|
|
|
public function render_content() { |
304
|
|
|
echo '<p class="description">' . esc_html( $this->description ) . '</p>'; |
305
|
|
|
} |
306
|
|
|
} // class end |
307
|
|
|
|
308
|
|
|
// Initialize controls |
309
|
|
|
new Jetpack_Related_Posts_Customize; |
310
|
|
|
|
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
orexit
statements that have been added for debug purposes.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.