/modules/custom-post-types/testimonial.php

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