Jetpack_Testimonial

The Jetpack by WordPress.com Jetpack Testimonial class.

Defined (1)

The class is defined in the following location(s).

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