Completed
Push — update/changelog-5.1 ( 3a446e )
by
unknown
42s
created

customizer.php ➔ jetpack_post_thumbnail_supports()   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
 * 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_archive_default  = ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1;
27
	$fi_post_default     = ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1;
28
	$fi_page_default     = ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1;
29
	$fi_fallback         = ( ! empty( $featured_images['fallback'] ) ) ? $featured_images['fallback'] : null;
30
	$fi_fallback_default = ( isset( $featured_images['fallback-default'] ) && false === $featured_images['fallback-default'] ) ? '' : 1;
31
32
	// 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.
33
	if ( ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) )
34
	    && ( true !== $author_bio )
35
	    && ( ( empty( $post_details['stylesheet'] ) )
36
			&& ( empty( $date )
37
				|| empty( $categories )
38
				|| empty( $tags )
39
				|| empty( $author )
40
				|| empty( $comment ) ) )
41
		&& ( true !== $fi_archive && true !== $fi_post && true !== $fi_page && true !== $fi_fallback ) ) {
42
	    return;
43
	}
44
45
	// New control type: Title.
46
	class Jetpack_Customize_Control_Title extends WP_Customize_Control {
47
		public $type = 'title';
48
49
		public function render_content() {
50
		?>
51
			<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
52
		<?php
53
		}
54
	}
55
56
	// Add Content section.
57
	$wp_customize->add_section( 'jetpack_content_options', array(
58
		'title'                        => esc_html__( 'Content Options', 'jetpack' ),
59
		'theme_supports'               => 'jetpack-content-options',
60
		'priority'                     => 100,
61
	) );
62
63
	// Add Blog Display option.
64
	if ( in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) ) {
65
		if ( 'mixed' === $blog_display ) {
66
			$blog_display_choices = array(
67
				'content' => esc_html__( 'Full post', 'jetpack' ),
68
				'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
69
				'mixed'   => esc_html__( 'Default', 'jetpack' ),
70
			);
71
72
			$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' );
73
		} else {
74
			$blog_display_choices = array(
75
				'content' => esc_html__( 'Full post', 'jetpack' ),
76
				'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
77
			);
78
79
			$blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages.', 'jetpack' );
80
81
			if ( 'mixed' === get_option( 'jetpack_content_blog_display' ) ) {
82
				update_option( 'jetpack_content_blog_display', $blog_display );
83
			}
84
		}
85
86
		$wp_customize->add_setting( 'jetpack_content_blog_display', array(
87
			'default'                  => $blog_display,
88
			'type'                     => 'option',
89
			'transport'                => 'postMessage',
90
			'sanitize_callback'        => 'jetpack_content_options_sanitize_blog_display',
91
		) );
92
93
		$wp_customize->add_control( 'jetpack_content_blog_display', array(
94
			'section'                  => 'jetpack_content_options',
95
			'label'                    => esc_html__( 'Blog Display', 'jetpack' ),
96
			'description'              => $blog_display_description,
97
			'type'                     => 'radio',
98
			'choices'                  => $blog_display_choices,
99
		) );
100
	}
101
102
	// Add Author Bio option.
103
	if ( true === $author_bio ) {
104
		$wp_customize->add_setting( 'jetpack_content_author_bio_title' );
105
106
		$wp_customize->add_control( new Jetpack_Customize_Control_Title( $wp_customize, 'jetpack_content_author_bio_title', array(
107
			'section'                  => 'jetpack_content_options',
108
			'label'                    => esc_html__( 'Author Bio', 'jetpack' ),
109
			'type'                     => 'title',
110
		) ) );
111
112
		$wp_customize->add_setting( 'jetpack_content_author_bio', array(
113
			'default'                  => $author_bio_default,
114
			'type'                     => 'option',
115
			'sanitize_callback'        => 'jetpack_content_options_sanitize_checkbox',
116
		) );
117
118
		$wp_customize->add_control( 'jetpack_content_author_bio', array(
119
			'section'                  => 'jetpack_content_options',
120
			'label'                    => esc_html__( 'Display on single posts', 'jetpack' ),
121
			'type'                     => 'checkbox',
122
		) );
123
	}
124
125
	// Add Post Details options.
126
	if ( ( ! empty( $post_details ) )
127
		&& ( ! empty( $post_details['stylesheet'] ) )
128
		&& ( ! empty( $date )
129
			|| ! empty( $categories )
130
			|| ! empty( $tags )
131
			|| ! empty( $author )
132
			|| ! empty( $comment ) ) ) {
133
		$wp_customize->add_setting( 'jetpack_content_post_details_title' );
134
135
		$wp_customize->add_control( new Jetpack_Customize_Control_Title( $wp_customize, 'jetpack_content_post_details_title', array(
136
			'section'                  => 'jetpack_content_options',
137
			'label'                    => esc_html__( 'Post Details', 'jetpack' ),
138
			'type'                     => 'title',
139
		) ) );
140
141
		// Post Details: Date
142 View Code Duplication
		if ( ! empty( $date ) ) {
143
			$wp_customize->add_setting( 'jetpack_content_post_details_date', array(
144
				'default'              => 1,
145
				'type'                 => 'option',
146
				'transport'            => 'postMessage',
147
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
148
			) );
149
150
			$wp_customize->add_control( 'jetpack_content_post_details_date', array(
151
				'section'              => 'jetpack_content_options',
152
				'label'                => esc_html__( 'Display date', 'jetpack' ),
153
				'type'                 => 'checkbox',
154
			) );
155
		}
156
157
		// Post Details: Categories
158 View Code Duplication
		if ( ! empty( $categories ) ) {
159
			$wp_customize->add_setting( 'jetpack_content_post_details_categories', array(
160
				'default'              => 1,
161
				'type'                 => 'option',
162
				'transport'            => 'postMessage',
163
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
164
			) );
165
166
			$wp_customize->add_control( 'jetpack_content_post_details_categories', array(
167
				'section'              => 'jetpack_content_options',
168
				'label'                => esc_html__( 'Display categories', 'jetpack' ),
169
				'type'                 => 'checkbox',
170
			) );
171
		}
172
173
		// Post Details: Tags
174 View Code Duplication
		if ( ! empty( $tags ) ) {
175
			$wp_customize->add_setting( 'jetpack_content_post_details_tags', array(
176
				'default'              => 1,
177
				'type'                 => 'option',
178
				'transport'            => 'postMessage',
179
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
180
			) );
181
182
			$wp_customize->add_control( 'jetpack_content_post_details_tags', array(
183
				'section'              => 'jetpack_content_options',
184
				'label'                => esc_html__( 'Display tags', 'jetpack' ),
185
				'type'                 => 'checkbox',
186
			) );
187
		}
188
189
		// Post Details: Author
190 View Code Duplication
		if ( ! empty( $author ) ) {
191
			$wp_customize->add_setting( 'jetpack_content_post_details_author', array(
192
				'default'              => 1,
193
				'type'                 => 'option',
194
				'transport'            => 'postMessage',
195
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
196
			) );
197
198
			$wp_customize->add_control( 'jetpack_content_post_details_author', array(
199
				'section'              => 'jetpack_content_options',
200
				'label'                => esc_html__( 'Display author', 'jetpack' ),
201
				'type'                 => 'checkbox',
202
			) );
203
		}
204
205
		// Post Details: Comment link
206
		if ( ! empty( $comment ) ) {
207
			$wp_customize->add_setting( 'jetpack_content_post_details_comment', array(
208
				'default'              => 1,
209
				'type'                 => 'option',
210
				'transport'            => 'postMessage',
211
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
212
			) );
213
214
			$wp_customize->add_control( 'jetpack_content_post_details_comment', array(
215
				'section'              => 'jetpack_content_options',
216
				'label'                => esc_html__( 'Display comment link', 'jetpack' ),
217
				'type'                 => 'checkbox',
218
			) );
219
		}
220
	}
221
222
	// Add Featured Images options.
223
	if ( true === $fi_archive || true === $fi_post || true === $fi_page || true === $fi_fallback ) {
224
		$wp_customize->add_setting( 'jetpack_content_featured_images_title' );
225
226
		$wp_customize->add_control( new Jetpack_Customize_Control_Title( $wp_customize, 'jetpack_content_featured_images_title', array(
227
			'section'                  => 'jetpack_content_options',
228
			'label'                    => esc_html__( 'Featured Images', 'jetpack' ),
229
			'type'                     => 'title',
230
			'active_callback'          => 'jetpack_post_thumbnail_supports',
231
		) ) );
232
233
		// Featured Images: Archive
234 View Code Duplication
		if ( true === $fi_archive ) {
235
			$wp_customize->add_setting( 'jetpack_content_featured_images_archive', array(
236
				'default'              => $fi_archive_default,
237
				'type'                 => 'option',
238
				'transport'            => 'refresh',
239
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
240
			) );
241
242
			$wp_customize->add_control( 'jetpack_content_featured_images_archive', array(
243
				'section'              => 'jetpack_content_options',
244
				'label'                => esc_html__( 'Display on blog and archives', 'jetpack' ),
245
				'type'                 => 'checkbox',
246
				'active_callback'      => 'jetpack_post_thumbnail_supports',
247
			) );
248
		}
249
250
		// Featured Images: Post
251 View Code Duplication
		if ( true === $fi_post ) {
252
			$wp_customize->add_setting( 'jetpack_content_featured_images_post', array(
253
				'default'              => $fi_post_default,
254
				'type'                 => 'option',
255
				'transport'            => 'refresh',
256
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
257
			) );
258
259
			$wp_customize->add_control( 'jetpack_content_featured_images_post', array(
260
				'section'              => 'jetpack_content_options',
261
				'label'                => esc_html__( 'Display on single posts', 'jetpack' ),
262
				'type'                 => 'checkbox',
263
				'active_callback'      => 'jetpack_post_thumbnail_supports',
264
			) );
265
		}
266
267
		// Featured Images: Page
268 View Code Duplication
		if ( true === $fi_page ) {
269
			$wp_customize->add_setting( 'jetpack_content_featured_images_page', array(
270
				'default'              => $fi_page_default,
271
				'type'                 => 'option',
272
				'transport'            => 'refresh',
273
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
274
			) );
275
276
			$wp_customize->add_control( 'jetpack_content_featured_images_page', array(
277
				'section'              => 'jetpack_content_options',
278
				'label'                => esc_html__( 'Display on pages', 'jetpack' ),
279
				'type'                 => 'checkbox',
280
				'active_callback'      => 'jetpack_post_thumbnail_supports',
281
			) );
282
		}
283
284
		// Featured Images: Fallback
285 View Code Duplication
		if ( true === $fi_fallback ) {
286
			$wp_customize->add_setting( 'jetpack_content_featured_images_fallback', array(
287
				'default'              => $fi_fallback_default,
288
				'type'                 => 'option',
289
				'transport'            => 'refresh',
290
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
291
			) );
292
293
			$wp_customize->add_control( 'jetpack_content_featured_images_fallback', array(
294
				'section'              => 'jetpack_content_options',
295
				'label'                => esc_html__( 'Automatically use first image in post', 'jetpack' ),
296
				'type'                 => 'checkbox',
297
				'active_callback'      => 'jetpack_post_thumbnail_supports',
298
			) );
299
		}
300
	}
301
}
302
add_action( 'customize_register', 'jetpack_content_options_customize_register' );
303
304
/**
305
 * Return whether the theme supports Post Thumbnails.
306
 */
307
function jetpack_post_thumbnail_supports() {
308
	return ( current_theme_supports( 'post-thumbnails' ) );
309
}
310
311
/**
312
 * Sanitize the checkbox.
313
 *
314
 * @param int $input.
0 ignored issues
show
Documentation introduced by
There is no parameter named $input.. Did you maybe mean $input?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
315
 * @return boolean|string
316
 */
317
function jetpack_content_options_sanitize_checkbox( $input ) {
318
	return ( 1 == $input ) ? 1 : '';
319
}
320
321
/**
322
 * Sanitize the Display value.
323
 *
324
 * @param string $display.
0 ignored issues
show
Documentation introduced by
There is no parameter named $display.. Did you maybe mean $display?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
325
 * @return string.
0 ignored issues
show
Documentation introduced by
The doc-type string. could not be parsed: Unknown type name "string." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
326
 */
327
function jetpack_content_options_sanitize_blog_display( $display ) {
328
	if ( ! in_array( $display, array( 'content', 'excerpt', 'mixed' ) ) ) {
329
		$display = 'content';
330
	}
331
	return $display;
332
}
333
334
/**
335
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
336
 */
337
function jetpack_content_options_customize_preview_js() {
338
	$options      = get_theme_support( 'jetpack-content-options' );
339
	$blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
340
	$blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
341
	sort( $blog_display );
342
	$blog_display = implode( ', ', $blog_display );
343
	$blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
344
	$masonry      = ( ! empty( $options[0]['masonry'] ) ) ? $options[0]['masonry'] : null;
345
	$post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
346
	$date         = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
347
	$categories   = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
348
	$tags         = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
349
	$author       = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
350
	$comment      = ( ! empty( $post_details['comment'] ) ) ? $post_details['comment'] : null;
351
352
	wp_enqueue_script( 'jetpack-content-options-customizer', plugins_url( 'customizer.js', __FILE__ ), array( 'customize-preview' ), '1.0', true );
353
354
	wp_localize_script( 'jetpack-content-options-customizer', 'blogDisplay', array(
355
		'display'    => get_option( 'jetpack_content_blog_display', $blog_display ),
356
		'masonry'    => $masonry,
357
	) );
358
359
	wp_localize_script( 'jetpack-content-options-customizer', 'postDetails', array(
360
		'date'       => $date,
361
		'categories' => $categories,
362
		'tags'       => $tags,
363
		'author'     => $author,
364
		'comment'    => $comment,
365
	) );
366
}
367
add_action( 'customize_preview_init', 'jetpack_content_options_customize_preview_js' );
368