summaryrefslogtreecommitdiff
blob: 3be1693fa2778d3e339947c94c690ea4a40de7cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
 * Add Content section to the Theme Customizer.
 *
 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
 */
function jetpack_content_options_customize_register( $wp_customize ) {
	$options            = get_theme_support( 'jetpack-content-options' );
	$blog_display       = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
	$blog_display       = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
	sort( $blog_display );
	$blog_display       = implode( ', ', $blog_display );
	$blog_display       = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
	$author_bio         = ( ! empty( $options[0]['author-bio'] ) ) ? $options[0]['author-bio'] : null;
	$author_bio_default = ( isset( $options[0]['author-bio-default'] ) && false === $options[0]['author-bio-default'] ) ? '' : 1;
	$post_details       = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
	$date               = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
	$categories         = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
	$tags               = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
	$author             = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;
	$featured_images    = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null;
	$fi_archive         = ( ! empty( $featured_images['archive'] ) ) ? $featured_images['archive'] : null;
	$fi_post            = ( ! empty( $featured_images['post'] ) ) ? $featured_images['post'] : null;
	$fi_page            = ( ! empty( $featured_images['page'] ) ) ? $featured_images['page'] : null;
	$fi_archive_default = ( isset( $featured_images['archive-default'] ) && false === $featured_images['archive-default'] ) ? '' : 1;
	$fi_post_default    = ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1;
	$fi_page_default    = ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1;

	// 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.
	if ( ( ! in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) )
	    && ( true !== $author_bio )
	    && ( ( empty( $post_details['stylesheet'] ) )
			&& ( empty( $date )
				|| empty( $categories )
				|| empty( $tags )
				|| empty( $author ) ) )
		&& ( true !== $fi_archive && true !== $fi_post && true !== $fi_page ) ) {
	    return;
	}

	// New control type: Title.
	class Jetpack_Customize_Control_Title extends WP_Customize_Control {
		public $type = 'title';

		public function render_content() {
		?>
			<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
		<?php
		}
	}

	// Add Content section.
	$wp_customize->add_section( 'jetpack_content_options', array(
		'title'                        => esc_html__( 'Content Options', 'jetpack' ),
		'theme_supports'               => 'jetpack-content-options',
		'priority'                     => 100,
	) );

	// Add Blog Display option.
	if ( in_array( $blog_display, array( 'content', 'excerpt', 'mixed' ) ) ) {
		if ( 'mixed' === $blog_display ) {
			$blog_display_choices = array(
				'content' => esc_html__( 'Full post', 'jetpack' ),
				'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
				'mixed'   => esc_html__( 'Default', 'jetpack' ),
			);

			$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' );
		} else {
			$blog_display_choices = array(
				'content' => esc_html__( 'Full post', 'jetpack' ),
				'excerpt' => esc_html__( 'Post excerpt', 'jetpack' ),
			);

			$blog_display_description = esc_html__( 'Choose between a full post or an excerpt for the blog and archive pages.', 'jetpack' );

			if ( 'mixed' === get_option( 'jetpack_content_blog_display' ) ) {
				update_option( 'jetpack_content_blog_display', $blog_display );
			}
		}

		$wp_customize->add_setting( 'jetpack_content_blog_display', array(
			'default'                  => $blog_display,
			'type'                     => 'option',
			'transport'                => 'postMessage',
			'sanitize_callback'        => 'jetpack_content_options_sanitize_blog_display',
		) );

		$wp_customize->add_control( 'jetpack_content_blog_display', array(
			'section'                  => 'jetpack_content_options',
			'label'                    => esc_html__( 'Blog Display', 'jetpack' ),
			'description'              => $blog_display_description,
			'type'                     => 'radio',
			'choices'                  => $blog_display_choices,
		) );
	}

	// Add Author Bio option.
	if ( true === $author_bio ) {
		$wp_customize->add_setting( 'jetpack_content_author_bio_title' );

		$wp_customize->add_control( new Jetpack_Customize_Control_Title( $wp_customize, 'jetpack_content_author_bio_title', array(
			'section'                  => 'jetpack_content_options',
			'label'                    => esc_html__( 'Author Bio', 'jetpack' ),
			'type'                     => 'title',
		) ) );

		$wp_customize->add_setting( 'jetpack_content_author_bio', array(
			'default'                  => $author_bio_default,
			'type'                     => 'option',
			'sanitize_callback'        => 'jetpack_content_options_sanitize_checkbox',
		) );

		$wp_customize->add_control( 'jetpack_content_author_bio', array(
			'section'                  => 'jetpack_content_options',
			'label'                    => esc_html__( 'Display on single posts', 'jetpack' ),
			'type'                     => 'checkbox',
		) );
	}

	// Add Post Details options.
	if ( ( ! empty( $post_details ) )
		&& ( ! empty( $post_details['stylesheet'] ) )
		&& ( ! empty( $date )
			|| ! empty( $categories )
			|| ! empty( $tags )
			|| ! empty( $author ) ) ) {
		$wp_customize->add_setting( 'jetpack_content_post_details_title' );

		$wp_customize->add_control( new Jetpack_Customize_Control_Title( $wp_customize, 'jetpack_content_post_details_title', array(
			'section'                  => 'jetpack_content_options',
			'label'                    => esc_html__( 'Post Details', 'jetpack' ),
			'type'                     => 'title',
		) ) );

		// Post Details: Date
		if ( ! empty( $date ) ) {
			$wp_customize->add_setting( 'jetpack_content_post_details_date', array(
				'default'              => 1,
				'type'                 => 'option',
				'transport'            => 'postMessage',
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
			) );

			$wp_customize->add_control( 'jetpack_content_post_details_date', array(
				'section'              => 'jetpack_content_options',
				'label'                => esc_html__( 'Display date', 'jetpack' ),
				'type'                 => 'checkbox',
			) );
		}

		// Post Details: Categories
		if ( ! empty( $categories ) ) {
			$wp_customize->add_setting( 'jetpack_content_post_details_categories', array(
				'default'              => 1,
				'type'                 => 'option',
				'transport'            => 'postMessage',
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
			) );

			$wp_customize->add_control( 'jetpack_content_post_details_categories', array(
				'section'              => 'jetpack_content_options',
				'label'                => esc_html__( 'Display categories', 'jetpack' ),
				'type'                 => 'checkbox',
			) );
		}

		// Post Details: Tags
		if ( ! empty( $tags ) ) {
			$wp_customize->add_setting( 'jetpack_content_post_details_tags', array(
				'default'              => 1,
				'type'                 => 'option',
				'transport'            => 'postMessage',
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
			) );

			$wp_customize->add_control( 'jetpack_content_post_details_tags', array(
				'section'              => 'jetpack_content_options',
				'label'                => esc_html__( 'Display tags', 'jetpack' ),
				'type'                 => 'checkbox',
			) );
		}

		// Post Details: Author
		if ( ! empty( $author ) ) {
			$wp_customize->add_setting( 'jetpack_content_post_details_author', array(
				'default'              => 1,
				'type'                 => 'option',
				'transport'            => 'postMessage',
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
			) );

			$wp_customize->add_control( 'jetpack_content_post_details_author', array(
				'section'              => 'jetpack_content_options',
				'label'                => esc_html__( 'Display author', 'jetpack' ),
				'type'                 => 'checkbox',
			) );
		}
	}

	// Add Featured Images options.
	if ( true === $fi_archive || true === $fi_post || true === $fi_page ) {
		$wp_customize->add_setting( 'jetpack_content_featured_images_title' );

		$wp_customize->add_control( new Jetpack_Customize_Control_Title( $wp_customize, 'jetpack_content_featured_images_title', array(
			'section'                  => 'jetpack_content_options',
			'label'                    => esc_html__( 'Featured Images', 'jetpack' ),
			'type'                     => 'title',
		) ) );

		// Featured Images: Archive
		if ( true === $fi_archive ) {
			$wp_customize->add_setting( 'jetpack_content_featured_images_archive', array(
				'default'              => $fi_archive_default,
				'type'                 => 'option',
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
			) );

			$wp_customize->add_control( 'jetpack_content_featured_images_archive', array(
				'section'              => 'jetpack_content_options',
				'label'                => esc_html__( 'Display on blog and archives', 'jetpack' ),
				'type'                 => 'checkbox',
			) );
		}

		// Featured Images: Post
		if ( true === $fi_post ) {
			$wp_customize->add_setting( 'jetpack_content_featured_images_post', array(
				'default'              => $fi_post_default,
				'type'                 => 'option',
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
			) );

			$wp_customize->add_control( 'jetpack_content_featured_images_post', array(
				'section'              => 'jetpack_content_options',
				'label'                => esc_html__( 'Display on single posts', 'jetpack' ),
				'type'                 => 'checkbox',
			) );
		}

		// Featured Images: Page
		if ( true === $fi_page ) {
			$wp_customize->add_setting( 'jetpack_content_featured_images_page', array(
				'default'              => $fi_page_default,
				'type'                 => 'option',
				'sanitize_callback'    => 'jetpack_content_options_sanitize_checkbox',
			) );

			$wp_customize->add_control( 'jetpack_content_featured_images_page', array(
				'section'              => 'jetpack_content_options',
				'label'                => esc_html__( 'Display on pages', 'jetpack' ),
				'type'                 => 'checkbox',
			) );
		}
	}
}
add_action( 'customize_register', 'jetpack_content_options_customize_register' );

/**
 * Sanitize the checkbox.
 *
 * @param int $input.
 * @return boolean|string
 */
function jetpack_content_options_sanitize_checkbox( $input ) {
	return ( 1 == $input ) ? 1 : '';
}

/**
 * Sanitize the Display value.
 *
 * @param string $display.
 * @return string.
 */
function jetpack_content_options_sanitize_blog_display( $display ) {
	if ( ! in_array( $display, array( 'content', 'excerpt', 'mixed' ) ) ) {
		$display = 'content';
	}
	return $display;
}

/**
 * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
 */
function jetpack_content_options_customize_preview_js() {
	$options      = get_theme_support( 'jetpack-content-options' );
	$blog_display = ( ! empty( $options[0]['blog-display'] ) ) ? $options[0]['blog-display'] : null;
	$blog_display = preg_grep( '/^(content|excerpt)$/', (array) $blog_display );
	sort( $blog_display );
	$blog_display = implode( ', ', $blog_display );
	$blog_display = ( 'content, excerpt' === $blog_display ) ? 'mixed' : $blog_display;
	$masonry      = ( ! empty( $options[0]['masonry'] ) ) ? $options[0]['masonry'] : null;
	$post_details = ( ! empty( $options[0]['post-details'] ) ) ? $options[0]['post-details'] : null;
	$date         = ( ! empty( $post_details['date'] ) ) ? $post_details['date'] : null;
	$categories   = ( ! empty( $post_details['categories'] ) ) ? $post_details['categories'] : null;
	$tags         = ( ! empty( $post_details['tags'] ) ) ? $post_details['tags'] : null;
	$author       = ( ! empty( $post_details['author'] ) ) ? $post_details['author'] : null;

	wp_enqueue_script( 'jetpack-content-options-customizer', plugins_url( 'customizer.js', __FILE__ ), array( 'customize-preview' ), '1.0', true );

	wp_localize_script( 'jetpack-content-options-customizer', 'blogDisplay', array(
		'display'    => get_option( 'jetpack_content_blog_display', $blog_display ),
		'masonry'    => $masonry,
	) );

	wp_localize_script( 'jetpack-content-options-customizer', 'postDetails', array(
		'date'       => $date,
		'categories' => $categories,
		'tags'       => $tags,
		'author'     => $author,
	) );
}
add_action( 'customize_preview_init', 'jetpack_content_options_customize_preview_js' );