Completed
Push — add/sync-rest-2 ( dcf3c8...9df295 )
by
unknown
10:28
created

Jetpack_Testimonial::setting_html()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 17

Duplication

Lines 9
Ratio 39.13 %
Metric Value
dl 9
loc 23
rs 9.0856
cc 3
eloc 17
nc 4
nop 0
1
<?php
2
3
class Jetpack_Testimonial {
4
	const CUSTOM_POST_TYPE       = 'jetpack-testimonial';
5
	const OPTION_NAME            = 'jetpack_testimonial';
6
	const OPTION_READING_SETTING = 'jetpack_testimonial_posts_per_page';
7
8
	public $version = '0.1';
9
10
	static function init() {
11
		static $instance = false;
12
13
		if ( ! $instance ) {
14
			$instance = new Jetpack_Testimonial;
15
		}
16
17
		return $instance;
18
	}
19
20
	/**
21
	 * Conditionally hook into WordPress.
22
	 *
23
	 * Setup user option for enabling CPT.
24
	 * If user has CPT enabled, show in admin.
25
	 */
26
	function __construct() {
27
		// Make sure the post types are loaded for imports
28
		add_action( 'import_start',                array( $this, 'register_post_types' ) );
29
30
		// If called via REST API, we need to register later in lifecycle
31
		add_action( 'restapi_theme_init',          array( $this, 'maybe_register_cpt' ) );
32
33
		// Add to REST API post type whitelist
34
		add_filter( 'rest_api_allowed_post_types', array( $this, 'allow_cpt_rest_api_type' ) );
35
36
		$this->maybe_register_cpt();
37
	}
38
39
	/**
40
	 * Registers the custom post types and adds action/filter handlers, but
41
	 * only if the site supports it
42
	 */
43
	function maybe_register_cpt() {
44
		// Add an option to enable the CPT
45
		add_action( 'admin_init', array( $this, 'settings_api_init' ) );
46
47
		// Check on theme switch if theme supports CPT and setting is disabled
48
		add_action( 'after_switch_theme', array( $this, 'activation_post_type_support' ) );
49
50
		$setting = get_option( self::OPTION_NAME, '0' );
51
52
		// Bail early if Testimonial option is not set and the theme doesn't declare support
53
		if ( empty( $setting ) && ! $this->site_supports_custom_post_type() ) {
54
			return;
55
		}
56
57
		// Enable Omnisearch for CPT.
58
		if ( class_exists( 'Jetpack_Omnisearch_Posts' ) ) {
59
			new Jetpack_Omnisearch_Posts( self::CUSTOM_POST_TYPE );
60
		}
61
62
		// CPT magic
63
		$this->register_post_types();
64
		add_action( sprintf( 'add_option_%s', self::OPTION_NAME ),               array( $this, 'flush_rules_on_enable' ), 10 );
65
		add_action( sprintf( 'update_option_%s', self::OPTION_NAME ),            array( $this, 'flush_rules_on_enable' ), 10 );
66
		add_action( sprintf( 'publish_%s', self::CUSTOM_POST_TYPE ),             array( $this, 'flush_rules_on_first_testimonial' ) );
67
		add_action( 'after_switch_theme',                                        array( $this, 'flush_rules_on_switch' ) );
68
69
		// Admin Customization
70
		add_filter( 'enter_title_here',                                          array( $this, 'change_default_title'    ) );
71
		add_filter( sprintf( 'manage_%s_posts_columns', self::CUSTOM_POST_TYPE), array( $this, 'edit_title_column_label' ) );
72
		add_filter( 'post_updated_messages',                                     array( $this, 'updated_messages'        ) );
73
		add_action( 'customize_register',                                        array( $this, 'customize_register'      ) );
74
75
		// Only add the 'Customize' sub-menu if the theme supports it.
76
		$num_testimonials = self::count_testimonials();
77
		if ( ! empty( $num_testimonials ) && current_theme_supports( self::CUSTOM_POST_TYPE ) ) {
78
			add_action( 'admin_menu',                                            array( $this, 'add_customize_page' ) );
79
		}
80
81
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
82
			// Track all the things
83
			add_action( sprintf( 'add_option_%s', self::OPTION_NAME ),                 array( $this, 'new_activation_stat_bump'  ) );
84
			add_action( sprintf( 'update_option_%s', self::OPTION_NAME ),              array( $this, 'update_option_stat_bump'   ), 11, 2 );
85
			add_action( sprintf( 'publish_%s', self::CUSTOM_POST_TYPE),                array( $this, 'new_testimonial_stat_bump' ) );
86
87
			// Add to Dotcom XML sitemaps
88
			add_filter( 'wpcom_sitemap_post_types',                                    array( $this, 'add_to_sitemap' ) );
89
		} else {
90
			// Add to Jetpack XML sitemap
91
			add_filter( 'jetpack_sitemap_post_types',                                  array( $this, 'add_to_sitemap' ) );
92
		}
93
94
		// Adjust CPT archive and custom taxonomies to obey CPT reading setting
95
		add_filter( 'pre_get_posts',                                             array( $this, 'query_reading_setting' ), 20 );
96
		add_filter( 'infinite_scroll_settings',                                  array( $this, 'infinite_scroll_click_posts_per_page' ) );
97
98
		// Register [jetpack_testimonials] always and
99
		// register [testimonials] if [testimonials] isn't already set
100
		add_shortcode( 'jetpack_testimonials',                                   array( $this, 'jetpack_testimonial_shortcode' ) );
101
102
		if ( ! shortcode_exists( 'testimonials' ) ) {
103
			add_shortcode( 'testimonials',                                       array( $this, 'jetpack_testimonial_shortcode' ) );
104
		}
105
106
		// If CPT was enabled programatically and no CPT items exist when user switches away, disable
107
		if ( $setting && $this->site_supports_custom_post_type() ) {
108
			add_action( 'switch_theme',                                          array( $this, 'deactivation_post_type_support' ) );
109
		}
110
	}
111
112
	/**
113
	 * Add a checkbox field in 'Settings' > 'Writing'
114
	 * for enabling CPT functionality.
115
	 *
116
	 * @return null
117
	 */
118
	function settings_api_init() {
119
		add_settings_field(
120
			self::OPTION_NAME,
121
			'<span class="cpt-options">' . __( 'Testimonials', 'jetpack' ) . '</span>',
122
			array( $this, 'setting_html' ),
123
			'writing',
124
			'jetpack_cpt_section'
125
		);
126
127
		register_setting(
128
			'writing',
129
			self::OPTION_NAME,
130
			'intval'
131
		);
132
133
		// Check if CPT is enabled first so that intval doesn't get set to NULL on re-registering
134
		if ( $this->site_supports_custom_post_type() ) {
135
			register_setting(
136
				'writing',
137
				self::OPTION_READING_SETTING,
138
				'intval'
139
			);
140
		}
141
	}
142
143
	/**
144
	 * HTML code to display a checkbox true/false option
145
	 * for the CPT setting.
146
	 *
147
	 * @return html
148
	 */
149
	function setting_html() {
150 View Code Duplication
		if ( current_theme_supports( self::CUSTOM_POST_TYPE ) ) : ?>
151
			<p><?php printf( __( 'Your theme supports Testimonials', 'jetpack' ) ); ?></p>
152
		<?php else : ?>
153
			<label for="<?php echo esc_attr( self::OPTION_NAME ); ?>">
154
				<input name="<?php echo esc_attr( self::OPTION_NAME ); ?>" id="<?php echo esc_attr( self::OPTION_NAME ); ?>" <?php echo checked( get_option( self::OPTION_NAME, '0' ), true, false ); ?> type="checkbox" value="1" />
155
				<?php esc_html_e( 'Enable Testimonials for this site.', 'jetpack' ); ?>
156
				<a target="_blank" href="http://en.support.wordpress.com/testimonials/"><?php esc_html_e( 'Learn More', 'jetpack' ); ?></a>
157
			</label>
158
		<?php endif;
159
160
		if ( $this->site_supports_custom_post_type() ) :
161
			printf( '<p><label for="%1$s">%2$s</label></p>',
162
				esc_attr( self::OPTION_READING_SETTING ),
163
				sprintf( __( 'Testimonial pages display at most %1$s testimonials', 'jetpack' ),
164
					sprintf( '<input name="%1$s" id="%1$s" type="number" step="1" min="1" value="%2$s" class="small-text" />',
165
						esc_attr( self::OPTION_READING_SETTING ),
166
						esc_attr( get_option( self::OPTION_READING_SETTING, '10' ) )
167
					)
168
				)
169
			);
170
		endif;
171
	}
172
173
	/**
174
	 * Should this Custom Post Type be made available?
175
	 */
176 View Code Duplication
	function site_supports_custom_post_type() {
177
		// If the current theme requests it.
178
		if ( current_theme_supports( self::CUSTOM_POST_TYPE ) || get_option( self::OPTION_NAME, '0' ) ) {
179
			return true;
180
		}
181
182
		// Otherwise, say no unless something wants to filter us to say yes.
183
		/** This action is documented in modules/custom-post-types/nova.php */
184
		return (bool) apply_filters( 'jetpack_enable_cpt', false, self::CUSTOM_POST_TYPE );
185
	}
186
187
	/**
188
	 * Add to REST API post type whitelist
189
	 */
190
	function allow_cpt_rest_api_type( $post_types ) {
191
		$post_types[] = self::CUSTOM_POST_TYPE;
192
193
		return $post_types;
194
	}
195
196
	/**
197
	 * Bump Testimonial > New Activation stat
198
	 */
199
	function new_activation_stat_bump() {
200
		/** This action is documented in modules/widgets/social-media-icons.php */
201
		do_action( 'jetpack_bump_stats_extras', 'testimonials', 'new-activation' );
202
	}
203
204
	/**
205
	 * Bump Testimonial > Option On/Off stats to get total active
206
	 */
207
	function update_option_stat_bump( $old, $new ) {
208
		if ( empty( $old ) && ! empty( $new ) ) {
209
			/** This action is documented in modules/widgets/social-media-icons.php */
210
			do_action( 'jetpack_bump_stats_extras', 'testimonials', 'option-on' );
211
		}
212
213
		if ( ! empty( $old ) && empty( $new ) ) {
214
			/** This action is documented in modules/widgets/social-media-icons.php */
215
			do_action( 'jetpack_bump_stats_extras', 'testimonials', 'option-off' );
216
		}
217
	}
218
219
	/**
220
	 * Bump Testimonial > Published Testimonials stat when testimonials are published
221
	 */
222
	function new_testimonial_stat_bump() {
223
		/** This action is documented in modules/widgets/social-media-icons.php */
224
		do_action ( 'jetpack_bump_stats_extras', 'testimonials', 'published-testimonials' );
225
	}
226
227
	/*
228
	 * Flush permalinks when CPT option is turned on/off
229
	 */
230
	function flush_rules_on_enable() {
231
		flush_rewrite_rules();
232
	}
233
234
	/*
235
	 * Count published testimonials and flush permalinks when first testimonial is published
236
	 */
237 View Code Duplication
	function flush_rules_on_first_testimonial() {
238
		$testimonials = get_transient( 'jetpack-testimonial-count-cache' );
239
240
		if ( false === $testimonials ) {
241
			flush_rewrite_rules();
242
			$testimonials = (int) wp_count_posts( self::CUSTOM_POST_TYPE )->publish;
243
244
			if ( ! empty( $testimonials ) ) {
245
				set_transient( 'jetpack-testimonial-count-cache', $testimonials, HOUR_IN_SECONDS * 12 );
246
			}
247
		}
248
	}
249
250
	/*
251
	 * Flush permalinks when CPT supported theme is activated
252
	 */
253
	function flush_rules_on_switch() {
254
		if ( current_theme_supports( self::CUSTOM_POST_TYPE ) ) {
255
			flush_rewrite_rules();
256
		}
257
	}
258
259
	/**
260
	 * On plugin/theme activation, check if current theme supports CPT
261
	 */
262
	static function activation_post_type_support() {
263
		if ( current_theme_supports( self::CUSTOM_POST_TYPE ) ) {
264
			update_option( self::OPTION_NAME, '1' );
265
		}
266
	}
267
268
	/**
269
	 * On theme switch, check if CPT item exists and disable if not
270
	 */
271 View Code Duplication
	function deactivation_post_type_support() {
272
		$portfolios = get_posts( array(
273
			'fields'           => 'ids',
274
			'posts_per_page'   => 1,
275
			'post_type'        => self::CUSTOM_POST_TYPE,
276
			'suppress_filters' => false
277
		) );
278
279
		if ( empty( $portfolios ) ) {
280
			update_option( self::OPTION_NAME, '0' );
281
		}
282
	}
283
284
	/**
285
	 * Register Post Type
286
	 */
287
	function register_post_types() {
288
		if ( post_type_exists( self::CUSTOM_POST_TYPE ) ) {
289
			return;
290
		}
291
292
		register_post_type( self::CUSTOM_POST_TYPE, array(
293
			'description' => __( 'Customer Testimonials', 'jetpack' ),
294
			'labels' => array(
295
				'name'                  => esc_html__( 'Testimonials',                   'jetpack' ),
296
				'singular_name'         => esc_html__( 'Testimonial',                    'jetpack' ),
297
				'menu_name'             => esc_html__( 'Testimonials',                   'jetpack' ),
298
				'all_items'             => esc_html__( 'All Testimonials',               'jetpack' ),
299
				'add_new'               => esc_html__( 'Add New',                        'jetpack' ),
300
				'add_new_item'          => esc_html__( 'Add New Testimonial',            'jetpack' ),
301
				'edit_item'             => esc_html__( 'Edit Testimonial',               'jetpack' ),
302
				'new_item'              => esc_html__( 'New Testimonial',                'jetpack' ),
303
				'view_item'             => esc_html__( 'View Testimonial',               'jetpack' ),
304
				'search_items'          => esc_html__( 'Search Testimonials',            'jetpack' ),
305
				'not_found'             => esc_html__( 'No Testimonials found',          'jetpack' ),
306
				'not_found_in_trash'    => esc_html__( 'No Testimonials found in Trash', 'jetpack' ),
307
				'filter_items_list'     => esc_html__( 'Filter Testimonials list',       'jetpack' ),
308
				'items_list_navigation' => esc_html__( 'Testimonial list navigation',    'jetpack' ),
309
				'items_list'            => esc_html__( 'Testimonials list',              'jetpack' ),
310
			),
311
			'supports' => array(
312
				'title',
313
				'editor',
314
				'thumbnail',
315
				'page-attributes',
316
				'revisions',
317
			),
318
			'rewrite' => array(
319
				'slug'       => 'testimonial',
320
				'with_front' => false,
321
				'feeds'      => false,
322
				'pages'      => true,
323
			),
324
			'public'          => true,
325
			'show_ui'         => true,
326
			'menu_position'   => 20, // below Pages
327
			'menu_icon'       => 'dashicons-testimonial',
328
			'capability_type' => 'page',
329
			'map_meta_cap'    => true,
330
			'has_archive'     => true,
331
			'query_var'       => 'testimonial',
332
			'show_in_rest'    => true,
333
		) );
334
	}
335
336
	/**
337
	 * Update messages for the Testimonial admin.
338
	 */
339 View Code Duplication
	function updated_messages( $messages ) {
340
		global $post;
341
342
		$messages[ self::CUSTOM_POST_TYPE ] = array(
343
			0  => '', // Unused. Messages start at index 1.
344
			1  => sprintf( __( 'Testimonial updated. <a href="%s">View testimonial</a>', 'jetpack'), esc_url( get_permalink( $post->ID ) ) ),
345
			2  => esc_html__( 'Custom field updated.', 'jetpack' ),
346
			3  => esc_html__( 'Custom field deleted.', 'jetpack' ),
347
			4  => esc_html__( 'Testimonial updated.', 'jetpack' ),
348
			/* translators: %s: date and time of the revision */
349
			5  => isset( $_GET['revision'] ) ? sprintf( esc_html__( 'Testimonial restored to revision from %s', 'jetpack'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
350
			6  => sprintf( __( 'Testimonial published. <a href="%s">View testimonial</a>', 'jetpack' ), esc_url( get_permalink( $post->ID ) ) ),
351
			7  => esc_html__( 'Testimonial saved.', 'jetpack' ),
352
			8  => sprintf( __( 'Testimonial submitted. <a target="_blank" href="%s">Preview testimonial</a>', 'jetpack'), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
353
			9  => sprintf( __( 'Testimonial scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview testimonial</a>', 'jetpack' ),
354
				// translators: Publish box date format, see http://php.net/date
355
				date_i18n( __( 'M j, Y @ G:i', 'jetpack' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post->ID) ) ),
356
			10 => sprintf( __( 'Testimonial draft updated. <a target="_blank" href="%s">Preview testimonial</a>', 'jetpack' ), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ),
357
		);
358
359
		return $messages;
360
	}
361
362
	/**
363
	 * Change ‘Enter Title Here’ text for the Testimonial.
364
	 */
365 View Code Duplication
	function change_default_title( $title ) {
366
		$screen = get_current_screen();
367
368
		if ( self::CUSTOM_POST_TYPE == $screen->post_type )
369
			$title = esc_html__( "Enter the customer's name here", 'jetpack' );
370
371
		return $title;
372
	}
373
374
	/**
375
	 * Change ‘Title’ column label on all Testimonials page.
376
	 */
377
	function edit_title_column_label( $columns ) {
378
		$columns['title'] = esc_html__( 'Customer Name', 'jetpack' );
379
380
		return $columns;
381
	}
382
383
	/**
384
	 * Follow CPT reading setting on CPT archive page
385
	 */
386
	function query_reading_setting( $query ) {
387
		if ( ! is_admin()
388
			&& $query->is_main_query()
389
			&& $query->is_post_type_archive( self::CUSTOM_POST_TYPE )
390
		) {
391
			$query->set( 'posts_per_page', get_option( self::OPTION_READING_SETTING, '10' ) );
392
		}
393
	}
394
395
	/*
396
	 * If Infinite Scroll is set to 'click', use our custom reading setting instead of core's `posts_per_page`.
397
	 */
398
	function infinite_scroll_click_posts_per_page( $settings ) {
399
		global $wp_query;
400
401
		if ( ! is_admin() && true === $settings['click_handle'] && $wp_query->is_post_type_archive( self::CUSTOM_POST_TYPE ) ) {
402
			$settings['posts_per_page'] = get_option( self::OPTION_READING_SETTING, $settings['posts_per_page'] );
403
		}
404
405
		return $settings;
406
	}
407
408
	/**
409
	 * Add CPT to Dotcom sitemap
410
	 */
411
	function add_to_sitemap( $post_types ) {
412
		$post_types[] = self::CUSTOM_POST_TYPE;
413
414
		return $post_types;
415
	}
416
417
	function set_testimonial_option() {
418
		$testimonials = wp_count_posts( self::CUSTOM_POST_TYPE );
419
		$published_testimonials = $testimonials->publish;
420
421
		update_option( self::OPTION_NAME, $published_testimonials );
422
	}
423
424
	function count_testimonials() {
425
		$testimonials = get_transient( 'jetpack-testimonial-count-cache' );
426
427
		if ( false === $testimonials ) {
428
			$testimonials = (int) wp_count_posts( self::CUSTOM_POST_TYPE )->publish;
429
430
			if ( ! empty( $testimonials ) ) {
431
				set_transient( 'jetpack-testimonial-count-cache', $testimonials, 60*60*12 );
432
			}
433
		}
434
435
		return $testimonials;
436
	}
437
438
	/**
439
	 * Adds a submenu link to the Customizer.
440
	 */
441
	function add_customize_page() {
442
		add_submenu_page(
443
			'edit.php?post_type=' . self::CUSTOM_POST_TYPE,
444
			esc_html__( 'Customize Testimonials Archive', 'jetpack' ),
445
			esc_html__( 'Customize', 'jetpack' ),
446
			'edit_theme_options',
447
			add_query_arg( array(
448
				'url' => urlencode( home_url( '/testimonial/' ) ),
449
				'autofocus[section]' => 'jetpack_testimonials'
450
			), 'customize.php' )
451
		);
452
	}
453
454
	/**
455
	 * Adds testimonial section to the Customizer.
456
	 */
457
	function customize_register( $wp_customize ) {
458
		jetpack_testimonial_custom_control_classes();
0 ignored issues
show
Unused Code introduced by
The call to the function jetpack_testimonial_custom_control_classes() seems unnecessary as the function has no side-effects.
Loading history...
459
460
		$wp_customize->add_section( 'jetpack_testimonials', array(
461
			'title'          => esc_html__( 'Testimonials', 'jetpack' ),
462
			'theme_supports' => self::CUSTOM_POST_TYPE,
463
			'priority'       => 130,
464
		) );
465
466
		$wp_customize->add_setting( 'jetpack_testimonials[page-title]', array(
467
			'default'              => esc_html__( 'Testimonials', 'jetpack' ),
468
			'sanitize_callback'    => array( 'Jetpack_Testimonial_Title_Control', 'sanitize_content' ),
469
			'sanitize_js_callback' => array( 'Jetpack_Testimonial_Title_Control', 'sanitize_content' ),
470
		) );
471
		$wp_customize->add_control( 'jetpack_testimonials[page-title]', array(
472
			'section' => 'jetpack_testimonials',
473
			'label'   => esc_html__( 'Testimonial Page Title', 'jetpack' ),
474
			'type'    => 'text',
475
		) );
476
477
		$wp_customize->add_setting( 'jetpack_testimonials[page-content]', array(
478
			'default'              => '',
479
			'sanitize_callback'    => array( 'Jetpack_Testimonial_Textarea_Control', 'sanitize_content' ),
480
			'sanitize_js_callback' => array( 'Jetpack_Testimonial_Textarea_Control', 'sanitize_content' ),
481
		) );
482
		$wp_customize->add_control( new Jetpack_Testimonial_Textarea_Control( $wp_customize, 'jetpack_testimonials[page-content]', array(
483
			'section'  => 'jetpack_testimonials',
484
			'settings' => 'jetpack_testimonials[page-content]',
485
			'label'    => esc_html__( 'Testimonial Page Content', 'jetpack' ),
486
		) ) );
487
488
		$wp_customize->add_setting( 'jetpack_testimonials[featured-image]', array(
489
			'default'              => '',
490
			'sanitize_callback'    => 'attachment_url_to_postid',
491
			'sanitize_js_callback' => 'attachment_url_to_postid',
492
			'theme_supports'       => 'post-thumbnails',
493
		) );
494
		$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'jetpack_testimonials[featured-image]', array(
495
			'section' => 'jetpack_testimonials',
496
			'label'   => esc_html__( 'Testimonial Page Featured Image', 'jetpack' ),
497
		) ) );
498
499
		// The featured image control doesn't display properly in the Customizer unless we coerce
500
		// it back into a URL sooner, since that's what WP_Customize_Upload_Control::to_json() expects
501
		if ( is_admin() ) {
502
			add_filter( 'theme_mod_jetpack_testimonials', array( $this, 'coerce_testimonial_image_to_url' ) );
503
		}
504
	}
505
506
	public function coerce_testimonial_image_to_url( $opt ) {
507
		if ( ! $opt || ! is_array( $opt ) ) {
508
			return $opt;
509
		}
510
		if ( ! isset( $opt['featured-image'] ) || ! is_scalar( $opt['featured-image'] ) ) {
511
			return $opt;
512
		}
513
		$url = wp_get_attachment_url( $opt['featured-image'] );
514
		if ( $url ) {
515
			$opt['featured-image'] = $url;
516
		}
517
		return $opt;
518
	}
519
520
	/**
521
	 * Our [testimonial] shortcode.
522
	 * Prints Testimonial data styled to look good on *any* theme.
523
	 *
524
	 * @return jetpack_testimonial_shortcode_html
525
	 */
526
	static function jetpack_testimonial_shortcode( $atts ) {
527
		// Default attributes
528
		$atts = shortcode_atts( array(
529
			'display_content' => true,
530
			'image'           => true,
531
			'columns'         => 1,
532
			'showposts'       => -1,
533
			'order'           => 'asc',
534
			'orderby'         => 'date',
535
		), $atts, 'testimonial' );
536
537
		// A little sanitization
538 View Code Duplication
		if ( $atts['display_content'] && 'true' != $atts['display_content'] && 'full' != $atts['display_content'] ) {
539
			$atts['display_content'] = false;
540
		}
541
542
		if ( $atts['image'] && 'true' != $atts['image'] ) {
543
			$atts['image'] = false;
544
		}
545
546
		$atts['columns'] = absint( $atts['columns'] );
547
548
		$atts['showposts'] = intval( $atts['showposts'] );
549
550 View Code Duplication
		if ( $atts['order'] ) {
551
			$atts['order'] = urldecode( $atts['order'] );
552
			$atts['order'] = strtoupper( $atts['order'] );
553
			if ( 'DESC' != $atts['order'] ) {
554
				$atts['order'] = 'ASC';
555
			}
556
		}
557
558 View Code Duplication
		if ( $atts['orderby'] ) {
559
			$atts['orderby'] = urldecode( $atts['orderby'] );
560
			$atts['orderby'] = strtolower( $atts['orderby'] );
561
			$allowed_keys = array('author', 'date', 'title', 'rand');
562
563
			$parsed = array();
564
			foreach ( explode( ',', $atts['orderby'] ) as $testimonial_index_number => $orderby ) {
565
				if ( ! in_array( $orderby, $allowed_keys ) ) {
566
					continue;
567
				}
568
				$parsed[] = $orderby;
569
			}
570
571
			if ( empty( $parsed ) ) {
572
				unset($atts['orderby']);
573
			} else {
574
				$atts['orderby'] = implode( ' ', $parsed );
575
			}
576
		}
577
578
		// enqueue shortcode styles when shortcode is used
579
		wp_enqueue_style( 'jetpack-testimonial-style', plugins_url( 'css/testimonial-shortcode.css', __FILE__ ), array(), '20140326' );
580
581
		return self::jetpack_testimonial_shortcode_html( $atts );
582
	}
583
584
	/**
585
	 * The Testimonial shortcode loop.
586
	 *
587
	 * @return html
588
	 */
589
	static function jetpack_testimonial_shortcode_html( $atts ) {
590
		// Default query arguments
591
		$defaults = array(
592
			'order'          => $atts['order'],
593
			'orderby'        => $atts['orderby'],
594
			'posts_per_page' => $atts['showposts'],
595
		);
596
597
		$args = wp_parse_args( $atts, $defaults );
598
		$args['post_type'] = self::CUSTOM_POST_TYPE; // Force this post type
599
		$query = new WP_Query( $args );
600
601
		$testimonial_index_number = 0;
602
603
		ob_start();
604
605
		// If we have testimonials, create the html
606
		if ( $query->have_posts() ) {
607
608
			?>
609
			<div class="jetpack-testimonial-shortcode column-<?php echo esc_attr( $atts['columns'] ); ?>">
610
				<?php  // open .jetpack-testimonial-shortcode
611
612
				// Construct the loop...
613
				while ( $query->have_posts() ) {
614
					$query->the_post();
615
					$post_id = get_the_ID();
616
					?>
617
					<div class="testimonial-entry <?php echo esc_attr( self::get_testimonial_class( $testimonial_index_number, $atts['columns'], has_post_thumbnail( $post_id ) ) ); ?>">
618
						<?php
619
						// The content
620 View Code Duplication
						if ( false !== $atts['display_content'] ) {
621
							if ( 'full' === $atts['display_content'] ) {
622
							?>
623
								<div class="testimonial-entry-content"><?php the_content(); ?></div>
624
							<?php
625
							} else {
626
							?>
627
								<div class="testimonial-entry-content"><?php the_excerpt(); ?></div>
628
							<?php
629
							}
630
						}
631
						?>
632
						<span class="testimonial-entry-title">&#8213; <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php echo esc_attr( the_title_attribute( ) ); ?>"><?php the_title(); ?></a></span>
633
						<?php
634
						// Featured image
635
						if ( false !== $atts['image'] ) :
636
							echo self::get_testimonial_thumbnail_link( $post_id );
637
						endif;
638
						?>
639
					</div><!-- close .testimonial-entry -->
640
					<?php
641
					$testimonial_index_number++;
642
				} // end of while loop
643
644
				wp_reset_postdata();
645
				?>
646
			</div><!-- close .jetpack-testimonial-shortcode -->
647
		<?php
648
		} else { ?>
649
			<p><em><?php _e( 'Your Testimonial Archive currently has no entries. You can start creating them on your dashboard.', 'jetpack' ); ?></p></em>
650
		<?php
651
		}
652
		$html = ob_get_clean();
653
654
		// Return the HTML block
655
		return $html;
656
	}
657
658
	/**
659
	 * Individual testimonial class
660
	 *
661
	 * @return string
662
	 */
663
	static function get_testimonial_class( $testimonial_index_number, $columns, $image ) {
664
		$class = array();
665
666
		$class[] = 'testimonial-entry-column-'.$columns;
667
668
		if( $columns > 1) {
669
			if ( ( $testimonial_index_number % 2 ) == 0 ) {
670
				$class[] = 'testimonial-entry-mobile-first-item-row';
671
			} else {
672
				$class[] = 'testimonial-entry-mobile-last-item-row';
673
			}
674
		}
675
676
		// add first and last classes to first and last items in a row
677
		if ( ( $testimonial_index_number % $columns ) == 0 ) {
678
			$class[] = 'testimonial-entry-first-item-row';
679
		} elseif ( ( $testimonial_index_number % $columns ) == ( $columns - 1 ) ) {
680
			$class[] = 'testimonial-entry-last-item-row';
681
		}
682
683
		// add class if testimonial has a featured image
684
		if ( false !== $image ) {
685
			$class[] = 'has-testimonial-thumbnail';
686
		}
687
688
		/**
689
		 * Filter the class applied to testimonial div in the testimonial
690
		 *
691
		 * @module custom-content-types
692
		 *
693
		 * @since 3.4.0
694
		 *
695
		 * @param string $class class name of the div.
696
		 * @param int $testimonial_index_number iterator count the number of columns up starting from 0.
697
		 * @param int $columns number of columns to display the content in.
698
		 * @param boolean $image has a thumbnail or not.
699
		 *
700
		 */
701
		return apply_filters( 'testimonial-entry-post-class', implode( " ", $class ) , $testimonial_index_number, $columns, $image );
702
	}
703
704
	/**
705
	 * Display the featured image if it's available
706
	 *
707
	 * @return html
708
	 */
709 View Code Duplication
	static function get_testimonial_thumbnail_link( $post_id ) {
710
		if ( has_post_thumbnail( $post_id ) ) {
711
			/**
712
			 * Change the thumbnail size for the Testimonial CPT.
713
			 *
714
			 * @module custom-content-types
715
			 *
716
			 * @since 3.4.0
717
			 *
718
			 * @param string|array $var Either a registered size keyword or size array.
719
			 */
720
			return '<a class="testimonial-featured-image" href="' . esc_url( get_permalink( $post_id ) ) . '">' . get_the_post_thumbnail( $post_id, apply_filters( 'jetpack_testimonial_thumbnail_size', 'thumbnail' ) ) . '</a>';
721
		}
722
	}
723
}
724
725
function jetpack_testimonial_custom_control_classes() {
726
	class Jetpack_Testimonial_Title_Control extends WP_Customize_Control {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
727
		public static function sanitize_content( $value ) {
728
			if ( '' != $value )
729
				$value = trim( convert_chars( wptexturize( $value ) ) );
730
731
			return $value;
732
		}
733
	}
734
735
	class Jetpack_Testimonial_Textarea_Control extends WP_Customize_Control {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
736
		public $type = 'textarea';
737
738
		public function render_content() {
739
			?>
740
			<label>
741
				<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
742
				<textarea rows="5" style="width:100%;" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
743
			</label>
744
		<?php
745
		}
746
747
		public static function sanitize_content( $value ) {
748
			if ( ! empty( $value ) )
749
				/** This filter is already documented in core. wp-includes/post-template.php */
750
				$value = apply_filters( 'the_content', $value );
751
752
			$value = preg_replace( '@<div id="jp-post-flair"([^>]+)?>(.+)?</div>@is', '', $value ); // Strip WPCOM and Jetpack post flair if included in content
753
754
			return $value;
755
		}
756
	}
757
}
758
759
add_action( 'init', array( 'Jetpack_Testimonial', 'init' ) );
760
761
// Check on plugin activation if theme supports CPT
762
register_activation_hook( __FILE__,                         array( 'Jetpack_Testimonial', 'activation_post_type_support' ) );
763
add_action( 'jetpack_activate_module_custom-content-types', array( 'Jetpack_Testimonial', 'activation_post_type_support' ) );
764