PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.2
Jetpack – WP Security, Backup, Speed, & Growth v7.2.2
16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 All 503 releases
jetpack / modules / theme-tools / content-options / customizer.php

customizer.php in Jetpack – WP Security, Backup, Speed, & Growth 7.2.2, at modules/theme-tools/content-options/customizer.php

483 lines 16.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Add Content section to the Theme Customizer.
4 *
5 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
6 */
7 function jetpack_content_options_customize_register( $wp_customize ) {
8 $options = get_theme_support( 'jetpack-content-options' );
9 $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
10 $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
11 sort( $blog_display );
12 $blog_display = implode( ', ', $blog_display );
13 $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
14 $author_bio = ( ! empty( $options[0]['author-bio'] ) ) ? $options[0]['author-bio'] : null;
15 $author_bio_default = ( isset( $options[0]['author-bio-default'] ) && false === $options[0]['author-bio-default'] ) ? '' : 1;
16 $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
17 $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
18 $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
19 $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
20 $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
21 $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
22 $featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null;
23 $fi_archive = ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null;
24 $fi_post = ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null;
25 $fi_page = ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null;
26 $fi_portfolio = ( ! empty( $featured_images['portfolio'] ) ) ? $featured_images['portfolio'] : null;
27 $fi_fallback = ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null;
28 $fi_archive_default = ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1;
29 $fi_post_default = ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1;
30 $fi_page_default = ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1;
31 $fi_portfolio_default = ( isset( $featured_images['portfolio-default'] ) && false === $featured_images['portfolio-default'] ) ? '' : 1;
32 $fi_fallback_default = ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1;
33
34 // If the theme doesn't support 'jetpack-content-options[ 'blog-display' ]', 'jetpack-content-options[ 'author-bio' ]', 'jetpack-content-options[ 'post-details' ]' and 'jetpack-content-options[ 'featured-images' ]', don't continue.
35 if ( ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) )
36 && ( true !== $author_bio )
37 && ( ( empty( $post_details['stylesheet'] ) )
38 && ( empty( $date )
39 || empty( $categories )
40 || empty( $tags )
41 || empty( $author )
42 || empty( $comment ) ) )
43 && ( true !== $fi_archive && true !== $fi_post && true !== $fi_page && true !== $fi_portfolio && true !== $fi_fallback ) ) {
44 return;
45 }
46
47 // New control type: Title.
48 class Jetpack_Customize_Control_Title extends WP_Customize_Control {
49 public $type = 'title';
50
51 public function render_content() {
52 ?>
53 <span class="customize-control-title"><?php echo wp_kses_post( $this->label ); ?></span>
54 <?php
55 }
56 }
57
58 // Add Content section.
59 $wp_customize->add_section(
60 'jetpack_content_options',
61 array(
62 'title' => esc_html__( 'Content Options', 'jetpack' ),
63 'theme_supports' => 'jetpack-content-options',
64 'priority' => 100,
65 )
66 );
67
68 // Add Blog Display option.
69 if ( in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) ) {
70 if ( 'mixed' === $blog_display ) {
71 $blog_display_choices = array(
72 'content' => esc_html__( 'Full post', 'jetpack' ),
73 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
74 'mixed' => esc_html__( 'Default', 'jetpack' ),
75 );
76
77 $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages, or opt for the theme\'s default combination of excerpt and full post.', 'jetpack' );
78 } else {
79 $blog_display_choices = array(
80 'content' => esc_html__( 'Full post', 'jetpack' ),
81 'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
82 );
83
84 $blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages.', 'jetpack' );
85
86 if ( 'mixed' === get_option( 'jetpack_content_blog_display' ) ) {
87 update_option( 'jetpack_content_blog_display', $blog_display );
88 }
89 }
90
91 $wp_customize->add_setting(
92 'jetpack_content_blog_display',
93 array(
94 'default' => $blog_display,
95 'type' => 'option',
96 'transport' => 'postMessage',
97 'sanitize_callback' => 'jetpack_content_options_sanitize_blog_display',
98 )
99 );
100
101 $wp_customize->add_control(
102 'jetpack_content_blog_display',
103 array(
104 'section' => 'jetpack_content_options',
105 'label' => esc_html__( 'Blog Display', 'jetpack' ),
106 'description' => $blog_display_description,
107 'type' => 'radio',
108 'choices' => $blog_display_choices,
109 )
110 );
111 }
112
113 // Add Author Bio option.
114 if ( true === $author_bio ) {
115 $wp_customize->add_setting( 'jetpack_content_author_bio_title' );
116
117 $wp_customize->add_control(
118 new Jetpack_Customize_Control_Title(
119 $wp_customize,
120 'jetpack_content_author_bio_title',
121 array(
122 'section' => 'jetpack_content_options',
123 'label' => esc_html__( 'Author Bio', 'jetpack' ),
124 'type' => 'title',
125 )
126 )
127 );
128
129 $wp_customize->add_setting(
130 'jetpack_content_author_bio',
131 array(
132 'default' => $author_bio_default,
133 'type' => 'option',
134 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
135 )
136 );
137
138 $wp_customize->add_control(
139 'jetpack_content_author_bio',
140 array(
141 'section' => 'jetpack_content_options',
142 'label' => esc_html__( 'Display on single posts', 'jetpack' ),
143 'type' => 'checkbox',
144 )
145 );
146 }
147
148 // Add Post Details options.
149 if ( ( ! empty( $post_details ) )
150 && ( ! empty( $post_details['stylesheet'] ) )
151 && ( ! empty( $date )
152 || ! empty( $categories )
153 || ! empty( $tags )
154 || ! empty( $author )
155 || ! empty( $comment ) ) ) {
156 $wp_customize->add_setting( 'jetpack_content_post_details_title' );
157
158 $wp_customize->add_control(
159 new Jetpack_Customize_Control_Title(
160 $wp_customize,
161 'jetpack_content_post_details_title',
162 array(
163 'section' => 'jetpack_content_options',
164 'label' => esc_html__( 'Post Details', 'jetpack' ),
165 'type' => 'title',
166 )
167 )
168 );
169
170 // Post Details: Date
171 if ( ! empty( $date ) ) {
172 $wp_customize->add_setting(
173 'jetpack_content_post_details_date',
174 array(
175 'default' => 1,
176 'type' => 'option',
177 'transport' => 'postMessage',
178 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
179 )
180 );
181
182 $wp_customize->add_control(
183 'jetpack_content_post_details_date',
184 array(
185 'section' => 'jetpack_content_options',
186 'label' => esc_html__( 'Display date', 'jetpack' ),
187 'type' => 'checkbox',
188 )
189 );
190 }
191
192 // Post Details: Categories
193 if ( ! empty( $categories ) ) {
194 $wp_customize->add_setting(
195 'jetpack_content_post_details_categories',
196 array(
197 'default' => 1,
198 'type' => 'option',
199 'transport' => 'postMessage',
200 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
201 )
202 );
203
204 $wp_customize->add_control(
205 'jetpack_content_post_details_categories',
206 array(
207 'section' => 'jetpack_content_options',
208 'label' => esc_html__( 'Display categories', 'jetpack' ),
209 'type' => 'checkbox',
210 )
211 );
212 }
213
214 // Post Details: Tags
215 if ( ! empty( $tags ) ) {
216 $wp_customize->add_setting(
217 'jetpack_content_post_details_tags',
218 array(
219 'default' => 1,
220 'type' => 'option',
221 'transport' => 'postMessage',
222 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
223 )
224 );
225
226 $wp_customize->add_control(
227 'jetpack_content_post_details_tags',
228 array(
229 'section' => 'jetpack_content_options',
230 'label' => esc_html__( 'Display tags', 'jetpack' ),
231 'type' => 'checkbox',
232 )
233 );
234 }
235
236 // Post Details: Author
237 if ( ! empty( $author ) ) {
238 $wp_customize->add_setting(
239 'jetpack_content_post_details_author',
240 array(
241 'default' => 1,
242 'type' => 'option',
243 'transport' => 'postMessage',
244 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
245 )
246 );
247
248 $wp_customize->add_control(
249 'jetpack_content_post_details_author',
250 array(
251 'section' => 'jetpack_content_options',
252 'label' => esc_html__( 'Display author', 'jetpack' ),
253 'type' => 'checkbox',
254 )
255 );
256 }
257
258 // Post Details: Comment link
259 if ( ! empty( $comment ) ) {
260 $wp_customize->add_setting(
261 'jetpack_content_post_details_comment',
262 array(
263 'default' => 1,
264 'type' => 'option',
265 'transport' => 'postMessage',
266 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
267 )
268 );
269
270 $wp_customize->add_control(
271 'jetpack_content_post_details_comment',
272 array(
273 'section' => 'jetpack_content_options',
274 'label' => esc_html__( 'Display comment link', 'jetpack' ),
275 'type' => 'checkbox',
276 )
277 );
278 }
279 }
280
281 // Add Featured Images options.
282 if ( true === $fi_archive || true === $fi_post || true === $fi_page || true === $fi_portfolio || true === $fi_fallback ) {
283 $wp_customize->add_setting( 'jetpack_content_featured_images_title' );
284
285 $wp_customize->add_control(
286 new Jetpack_Customize_Control_Title(
287 $wp_customize,
288 'jetpack_content_featured_images_title',
289 array(
290 'section' => 'jetpack_content_options',
291 'label' => esc_html__( 'Featured Images', 'jetpack' ) . sprintf( '<a href="https://en.support.wordpress.com/featured-images/" class="customize-help-toggle dashicons dashicons-editor-help" title="%1$s" rel="noopener noreferrer" target="_blank"><span class="screen-reader-text">%1$s</span></a>', esc_html__( 'Learn more about Featured Images', 'jetpack' ) ),
292 'type' => 'title',
293 'active_callback' => 'jetpack_post_thumbnail_supports',
294 )
295 )
296 );
297
298 // Featured Images: Archive
299 if ( true === $fi_archive ) {
300 $wp_customize->add_setting(
301 'jetpack_content_featured_images_archive',
302 array(
303 'default' => $fi_archive_default,
304 'type' => 'option',
305 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
306 )
307 );
308
309 $wp_customize->add_control(
310 'jetpack_content_featured_images_archive',
311 array(
312 'section' => 'jetpack_content_options',
313 'label' => esc_html__( 'Display on blog and archives', 'jetpack' ),
314 'type' => 'checkbox',
315 'active_callback' => 'jetpack_post_thumbnail_supports',
316 )
317 );
318 }
319
320 // Featured Images: Post
321 if ( true === $fi_post ) {
322 $wp_customize->add_setting(
323 'jetpack_content_featured_images_post',
324 array(
325 'default' => $fi_post_default,
326 'type' => 'option',
327 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
328 )
329 );
330
331 $wp_customize->add_control(
332 'jetpack_content_featured_images_post',
333 array(
334 'section' => 'jetpack_content_options',
335 'label' => esc_html__( 'Display on single posts', 'jetpack' ),
336 'type' => 'checkbox',
337 'active_callback' => 'jetpack_post_thumbnail_supports',
338 )
339 );
340 }
341
342 // Featured Images: Page
343 if ( true === $fi_page ) {
344 $wp_customize->add_setting(
345 'jetpack_content_featured_images_page',
346 array(
347 'default' => $fi_page_default,
348 'type' => 'option',
349 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
350 )
351 );
352
353 $wp_customize->add_control(
354 'jetpack_content_featured_images_page',
355 array(
356 'section' => 'jetpack_content_options',
357 'label' => esc_html__( 'Display on pages', 'jetpack' ),
358 'type' => 'checkbox',
359 'active_callback' => 'jetpack_post_thumbnail_supports',
360 )
361 );
362 }
363
364 // Featured Images: Portfolio
365 if ( true === $fi_portfolio && post_type_exists( 'jetpack-portfolio' ) ) {
366 $wp_customize->add_setting(
367 'jetpack_content_featured_images_portfolio',
368 array(
369 'default' => $fi_portfolio_default,
370 'type' => 'option',
371 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
372 )
373 );
374
375 $wp_customize->add_control(
376 'jetpack_content_featured_images_portfolio',
377 array(
378 'section' => 'jetpack_content_options',
379 'label' => esc_html__( 'Display on single projects', 'jetpack' ),
380 'type' => 'checkbox',
381 'active_callback' => 'jetpack_post_thumbnail_supports',
382 )
383 );
384 }
385
386 // Featured Images: Fallback
387 if ( true === $fi_fallback ) {
388 $wp_customize->add_setting(
389 'jetpack_content_featured_images_fallback',
390 array(
391 'default' => $fi_fallback_default,
392 'type' => 'option',
393 'sanitize_callback' => 'jetpack_content_options_sanitize_checkbox',
394 )
395 );
396
397 $wp_customize->add_control(
398 'jetpack_content_featured_images_fallback',
399 array(
400 'section' => 'jetpack_content_options',
401 'label' => esc_html__( 'Automatically use first image in post', 'jetpack' ),
402 'type' => 'checkbox',
403 'active_callback' => 'jetpack_post_thumbnail_supports',
404 )
405 );
406 }
407 }
408 }
409 add_action( 'customize_register', 'jetpack_content_options_customize_register' );
410
411 /**
412 * Return whether the theme supports Post Thumbnails.
413 */
414 function jetpack_post_thumbnail_supports() {
415 return ( current_theme_supports( 'post-thumbnails' ) );
416 }
417
418 /**
419 * Sanitize the checkbox.
420 *
421 * @param int $input.
422 * @return boolean|string
423 */
424 function jetpack_content_options_sanitize_checkbox( $input ) {
425 return ( 1 == $input ) ? 1 : '';
426 }
427
428 /**
429 * Sanitize the Display value.
430 *
431 * @param string $display.
432 * @return string.
433 */
434 function jetpack_content_options_sanitize_blog_display( $display ) {
435 if ( ! in_array( $display, array( 'content', 'excerpt', 'mixed' ) ) ) {
436 $display = 'content';
437 }
438 return $display;
439 }
440
441 /**
442 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
443 */
444 function jetpack_content_options_customize_preview_js() {
445 $options = get_theme_support( 'jetpack-content-options' );
446 $blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
447 $blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
448 sort( $blog_display );
449 $blog_display = implode( ', ', $blog_display );
450 $blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
451 $masonry = ( ! empty( $options[0]['masonry'] ) ) ? $options[0]['masonry'] : null;
452 $post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
453 $date = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
454 $categories = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
455 $tags = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
456 $author = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
457 $comment = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
458
459 wp_enqueue_script( 'jetpack-content-options-customizer', plugins_url( 'customizer.js', __FILE__ ), array( 'customize-preview' ), '1.0', true );
460
461 wp_localize_script(
462 'jetpack-content-options-customizer',
463 'blogDisplay',
464 array(
465 'display' => get_option( 'jetpack_content_blog_display', $blog_display ),
466 'masonry' => $masonry,
467 )
468 );
469
470 wp_localize_script(
471 'jetpack-content-options-customizer',
472 'postDetails',
473 array(
474 'date' => $date,
475 'categories' => $categories,
476 'tags' => $tags,
477 'author' => $author,
478 'comment' => $comment,
479 )
480 );
481 }
482 add_action( 'customize_preview_init', 'jetpack_content_options_customize_preview_js' );
483