summaryrefslogtreecommitdiff
blob: 8f15ca69e31bcbd152c16a564336e21c7c7236fa (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
<?php

// Exit if file is accessed directly
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class to include elements to modify Related Posts look in Customizer.
 *
 * @since 4.4.0
 */
class Jetpack_Related_Posts_Customize {

	/**
	 * Key for panel, section and prefix for options. Same option name than in Options > Reading.
	 *
	 * @var string
	 */
	var $prefix = 'jetpack_relatedposts';

	/**
	 * @var string Control to focus when customizer loads.
	 */
	var $focus = '';

	/**
	 * Class initialization.
	 *
	 * @since 4.4.0
	 */
	function __construct() {
		add_action( 'customize_register', array( $this, 'customize_register' ) );
		add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) );
	}

	/**
	 * Initialize Customizer controls.
	 *
	 * @since 4.4.0
	 *
	 * @param WP_Customize_Manager $wp_customize Customizer instance.
	 */
	function customize_register( $wp_customize ) {

		$wp_customize->add_section( $this->prefix,
			array(
				'title' 	  => esc_html__( 'Related Posts', 'jetpack' ),
				'description' => '',
				'capability'  => 'edit_theme_options',
				'priority' 	  => 200,
			)
		);

		$selective_options = array();

		foreach ( $this->get_options( $wp_customize ) as $key => $field ) {
			$control_id = "$this->prefix[$key]";
			$selective_options[] = $control_id;
			$wp_customize->add_setting( $control_id,
				array(
					'default' 	 => isset( $field['default'] ) ? $field['default'] : '',
					'type' 		 => isset( $field['setting_type'] ) ? $field['setting_type'] : 'option',
					'capability' => isset( $field['capability'] ) ? $field['capability'] : 'edit_theme_options',
					'transport'  => isset( $field['transport'] ) ? $field['transport'] : 'postMessage',
				)
			);
			$control_settings = array(
				'label' 	  => isset( $field['label'] ) ? $field['label'] : '',
				'description' => isset( $field['description'] ) ? $field['description'] : '',
				'settings' 	  => $control_id,
				'type' 	      => isset( $field['control_type'] ) ? $field['control_type'] : 'text',
				'section' 	  => $this->prefix,
				'priority' 	  => 10,
				'active_callback' => isset( $field['active_callback'] ) ? $field['active_callback'] : __CLASS__ . '::is_single',
			);
			switch ( $field['control_type'] ) {
				case 'text':
				case 'checkbox':
				default:
					$wp_customize->add_control( new WP_Customize_Control( $wp_customize, $control_id, $control_settings ) );
					break;
				case 'select':
					if ( isset( $field['choices'] ) ) {
						$control_settings['choices'] = $field['choices'];
						$wp_customize->add_control( new WP_Customize_Control( $wp_customize, $control_id, $control_settings ) );
					}
					break;
				case 'message':
					$wp_customize->add_control( new Jetpack_Message_Control( $wp_customize, $control_id, $control_settings ) );
					break;
			}
		}

		// If selective refresh is available, implement it.
		if ( isset( $wp_customize->selective_refresh ) ) {
			$wp_customize->selective_refresh->add_partial( "$this->prefix", array(
				'selector'            => '.jp-relatedposts:not(.jp-relatedposts-block)',
				'settings'            => $selective_options,
				'render_callback'     => __CLASS__ . '::render_callback',
				'container_inclusive' => false,
			) );
		}

	}

	/**
	 * Callback that outputs the headline based on user choice.
	 *
	 * @since 4.4.0
	 */
	public static function render_callback() {
		echo Jetpack_RelatedPosts::init()->get_headline();
	}

	/**
	 * Check whether the current post contains a Related Posts block.
	 *
	 * @since 6.9.0
	 *
	 * @return bool
	 */
	public static function contains_related_posts_block() {
		if ( has_block( 'jetpack/related-posts' ) ) {
			return true;
		}

		return false;
	}

	/**
	 * Check that we're in a single post view.
	 * Will return `false` if the current post contains a Related Posts block,
	 * because in that case we want to hide the Customizer controls.
	 *
	 * @since 4.4.0
	 *
	 * @return bool
	 */
	public static function is_single() {
		if ( self::contains_related_posts_block() ) {
			return false;
		}
		return is_single();
	}

	/**
	 * Check that we're not in a single post view.
	 * Will return `false` if the current post contains a Related Posts block,
	 * because in that case we want to hide the Customizer controls.
	 *
	 * @since 4.4.0
	 *
	 * @return bool
	 */
	public static function is_not_single() {
		if ( self::contains_related_posts_block() ) {
			return false;
		}
		return ! is_single();
	}

	/**
	 * Return list of options to modify.
	 *
	 * @since 4.4.0
	 *
	 * @param object $wp_customize Instance of WP Customizer
	 *
	 * @return mixed|void
	 */
	function get_options( $wp_customize ) {
		$transport = isset( $wp_customize->selective_refresh ) ? 'postMessage' : 'refresh';

		$switched_locale = switch_to_locale( get_user_locale() );
		$headline = __( 'Related', 'jetpack' );
		if ( $switched_locale ) {
			restore_previous_locale();
		}

		/**
		 * The filter allows you to change the options used to display Related Posts in the Customizer.
		 *
		 * @module related-posts
		 *
		 * @since 4.4.0
		 *
		 * @param array $options Array of options used to display Related Posts in the Customizer.
		 */
		return apply_filters(
			'jetpack_related_posts_customize_options', array(
				'enabled'       => array(
					'control_type' => 'hidden',
					'default'      => 1,
					'setting_type' => 'option',
					'transport'    => $transport,
				),
				'show_headline'       => array(
					'label'        => esc_html__( 'Show a headline', 'jetpack' ),
					'description'  => esc_html__( 'This helps to clearly separate the related posts from post content.', 'jetpack' ),
					'control_type' => 'checkbox',
					'default'      => 1,
					'setting_type' => 'option',
					'transport'    => $transport,
				),
				'headline'       => array(
					'label'        => '',
					'description'  => esc_html__( 'Enter text to use as headline.', 'jetpack' ),
					'control_type' => 'text',
					'default'      => esc_html( $headline ),
					'setting_type' => 'option',
					'transport'    => $transport,
				),
				'show_thumbnails'     => array(
					'label'        => esc_html__( 'Show thumbnails', 'jetpack' ),
					'description'  => esc_html__( 'Show a thumbnail image where available.', 'jetpack' ),
					'control_type' => 'checkbox',
					'default'      => 1,
					'setting_type' => 'option',
					'transport'    => $transport,
				),
				'show_date'           => array(
					'label'        => esc_html__( 'Show date', 'jetpack' ),
					'description'  => esc_html__( 'Display date when entry was published.', 'jetpack' ),
					'control_type' => 'checkbox',
					'default'      => 1,
					'setting_type' => 'option',
					'transport'    => $transport,
				),
				'show_context'        => array(
					'label'        => esc_html__( 'Show context', 'jetpack' ),
					'description'  => esc_html__( "Display entry's category or tag.", 'jetpack' ),
					'control_type' => 'checkbox',
					'default'      => 1,
					'setting_type' => 'option',
					'transport'    => $transport,
				),
				'layout'        => array(
					'label'        => esc_html__( 'Layout', 'jetpack' ),
					'description'  => esc_html__( 'Arrange entries in different layouts.', 'jetpack' ),
					'control_type' => 'select',
					'choices'	   => array(
						'grid' => esc_html__( 'Grid', 'jetpack' ),
						'list' => esc_html__( 'List', 'jetpack' ),
					),
					'default'      => 'grid',
					'setting_type' => 'option',
					'transport'    => $transport,
				),
				'msg_go_to_single' => array(
					'description'     => esc_html__( 'Please visit a single post view to reveal the customization options.', 'jetpack' ),
					'control_type'    => 'message',
					'active_callback' => __CLASS__ . '::is_not_single',
				),
				'msg_example'      => array(
					'description'  => esc_html__( 'Please note that the related posts displayed now are only for previewing purposes.', 'jetpack' ),
					'control_type' => 'message',
				),
			)
		);
	}

	/**
	 * Enqueue assets for Customizer controls.
	 *
	 * @since 4.4.0
	 */
	function customize_controls_enqueue_scripts() {
		wp_enqueue_script(
			'jetpack_related-posts-customizer',
			Jetpack::get_file_url_for_environment(
				'_inc/build/related-posts/related-posts-customizer.min.js',
				'modules/related-posts/related-posts-customizer.js'
			),
			array( 'customize-controls' ),
			JETPACK__VERSION
		);
	}

} // class end

/**
 * Control that displays a message in Customizer.
 *
 * @since 4.4.0
 */
class Jetpack_Message_Control extends WP_Customize_Control {

	/**
	 * Render the message.
	 *
	 * @since 4.4.0
	 */
	public function render_content() {
		echo '<p class="description">' . esc_html( $this->description ) . '</p>';
	}
} // class end

// Initialize controls
new Jetpack_Related_Posts_Customize;