81 // Theme can override the number of max posts. |
75 // Theme can override the number of max posts. |
82 if ( isset( $theme_support[0]['max_posts'] ) ) { |
76 if ( isset( $theme_support[0]['max_posts'] ) ) { |
83 self::$max_posts = absint( $theme_support[0]['max_posts'] ); |
77 self::$max_posts = absint( $theme_support[0]['max_posts'] ); |
84 } |
78 } |
85 |
79 |
86 add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) ); |
80 add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) ); |
87 add_action( 'customize_register', array( __CLASS__, 'customize_register' ), 9 ); |
81 add_action( 'customize_register', array( __CLASS__, 'customize_register' ), 9 ); |
88 add_action( 'admin_init', array( __CLASS__, 'register_setting' ) ); |
82 add_action( 'admin_init', array( __CLASS__, 'register_setting' ) ); |
89 add_action( 'switch_theme', array( __CLASS__, 'delete_transient' ) ); |
83 add_action( 'switch_theme', array( __CLASS__, 'delete_transient' ) ); |
90 add_action( 'save_post', array( __CLASS__, 'delete_transient' ) ); |
84 add_action( 'save_post', array( __CLASS__, 'delete_transient' ) ); |
91 add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) ); |
85 add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) ); |
92 add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) ); |
86 add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) ); |
93 add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) ); |
87 add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) ); |
94 add_action( 'wp_loaded', array( __CLASS__, 'wp_loaded' ) ); |
88 add_action( 'wp_loaded', array( __CLASS__, 'wp_loaded' ) ); |
95 } |
89 } |
96 |
90 |
97 /** |
91 /** |
98 * Hide "featured" tag from the front-end. |
92 * Hide "featured" tag from the front end. |
99 * |
93 * |
100 * Has to run on wp_loaded so that the preview filters of the Customizer |
94 * Has to run on wp_loaded so that the preview filters of the Customizer |
101 * have a chance to alter the value. |
95 * have a chance to alter the value. |
102 * |
96 * |
103 * @static |
|
104 * @access public |
|
105 * @since Twenty Fourteen 1.0 |
97 * @since Twenty Fourteen 1.0 |
106 */ |
98 */ |
107 public static function wp_loaded() { |
99 public static function wp_loaded() { |
108 if ( self::get_setting( 'hide-tag' ) ) { |
100 if ( self::get_setting( 'hide-tag' ) ) { |
109 add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 3 ); |
101 add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 3 ); |
110 add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 ); |
102 add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 ); |
111 } |
103 } |
112 } |
104 } |
113 |
105 |
114 /** |
106 /** |
115 * Get featured posts. |
107 * Get featured posts. |
116 * |
108 * |
117 * @static |
|
118 * @access public |
|
119 * @since Twenty Fourteen 1.0 |
109 * @since Twenty Fourteen 1.0 |
120 * |
110 * |
121 * @return array Array of featured posts. |
111 * @return array Array of featured posts. |
122 */ |
112 */ |
123 public static function get_featured_posts() { |
113 public static function get_featured_posts() { |
158 $settings = self::get_setting(); |
148 $settings = self::get_setting(); |
159 $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' ); |
149 $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' ); |
160 |
150 |
161 if ( $term ) { |
151 if ( $term ) { |
162 // Query for featured posts. |
152 // Query for featured posts. |
163 $featured_ids = get_posts( array( |
153 $featured_ids = get_posts( |
164 'fields' => 'ids', |
154 array( |
165 'numberposts' => self::$max_posts, |
155 'fields' => 'ids', |
166 'suppress_filters' => false, |
156 'numberposts' => self::$max_posts, |
167 'tax_query' => array( |
157 'suppress_filters' => false, |
168 array( |
158 'tax_query' => array( |
169 'field' => 'term_id', |
159 array( |
170 'taxonomy' => 'post_tag', |
160 'field' => 'term_id', |
171 'terms' => $term->term_id, |
161 'taxonomy' => 'post_tag', |
|
162 'terms' => $term->term_id, |
|
163 ), |
172 ), |
164 ), |
173 ), |
165 ) |
174 ) ); |
166 ); |
175 } |
167 } |
176 |
168 |
177 // Get sticky posts if no Featured Content exists. |
169 // Get sticky posts if no Featured Content exists. |
178 if ( ! $featured_ids ) { |
170 if ( ! $featured_ids ) { |
179 $featured_ids = self::get_sticky_posts(); |
171 $featured_ids = self::get_sticky_posts(); |
281 if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] ) { |
265 if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] ) { |
282 return; |
266 return; |
283 } |
267 } |
284 |
268 |
285 $settings['tag-id'] = 0; |
269 $settings['tag-id'] = 0; |
286 $settings = self::validate_settings( $settings ); |
270 $settings = self::validate_settings( $settings ); |
287 update_option( 'featured-content', $settings ); |
271 update_option( 'featured-content', $settings ); |
288 } |
272 } |
289 |
273 |
290 /** |
274 /** |
291 * Hide featured tag from displaying when global terms are queried from the front-end. |
275 * Hide featured tag from displaying when global terms are queried from the front end. |
292 * |
276 * |
293 * Hooks into the "get_terms" filter. |
277 * Hooks into the "get_terms" filter. |
294 * |
278 * |
295 * @static |
|
296 * @access public |
|
297 * @since Twenty Fourteen 1.0 |
279 * @since Twenty Fourteen 1.0 |
298 * |
280 * |
299 * @param array $terms List of term objects. This is the return value of get_terms(). |
281 * @param array $terms List of term objects. This is the return value of get_terms(). |
300 * @param array $taxonomies An array of taxonomy slugs. |
282 * @param array $taxonomies An array of taxonomy slugs. |
301 * @return array A filtered array of terms. |
283 * @return array A filtered array of terms. |
302 * |
284 * |
303 * @uses Featured_Content::get_setting() |
285 * @uses Featured_Content::get_setting() |
304 */ |
286 */ |
305 public static function hide_featured_term( $terms, $taxonomies, $args ) { |
287 public static function hide_featured_term( $terms, $taxonomies, $args ) { |
306 |
288 |
307 // This filter is only appropriate on the front-end. |
289 // This filter is only appropriate on the front end. |
308 if ( is_admin() ) { |
290 if ( is_admin() ) { |
309 return $terms; |
291 return $terms; |
310 } |
292 } |
311 |
293 |
312 // We only want to hide the featured tag. |
294 // We only want to hide the featured tag. |
379 } |
359 } |
380 |
360 |
381 /** |
361 /** |
382 * Register custom setting on the Settings -> Reading screen. |
362 * Register custom setting on the Settings -> Reading screen. |
383 * |
363 * |
384 * @static |
|
385 * @access public |
|
386 * @since Twenty Fourteen 1.0 |
364 * @since Twenty Fourteen 1.0 |
387 */ |
365 */ |
388 public static function register_setting() { |
366 public static function register_setting() { |
389 register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) ); |
367 register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) ); |
390 } |
368 } |
391 |
369 |
392 /** |
370 /** |
393 * Add settings to the Customizer. |
371 * Add settings to the Customizer. |
394 * |
372 * |
395 * @static |
|
396 * @access public |
|
397 * @since Twenty Fourteen 1.0 |
373 * @since Twenty Fourteen 1.0 |
398 * |
374 * |
399 * @param WP_Customize_Manager $wp_customize Customizer object. |
375 * @param WP_Customize_Manager $wp_customize Customizer object. |
400 */ |
376 */ |
401 public static function customize_register( $wp_customize ) { |
377 public static function customize_register( $wp_customize ) { |
402 $wp_customize->add_section( 'featured_content', array( |
378 $wp_customize->add_section( |
403 'title' => __( 'Featured Content', 'twentyfourteen' ), |
379 'featured_content', array( |
404 'description' => sprintf( __( 'Use a <a href="%1$s">tag</a> to feature your posts. If no posts match the tag, <a href="%2$s">sticky posts</a> will be displayed instead.', 'twentyfourteen' ), |
380 'title' => __( 'Featured Content', 'twentyfourteen' ), |
405 esc_url( add_query_arg( 'tag', _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ), admin_url( 'edit.php' ) ) ), |
381 'description' => sprintf( |
406 admin_url( 'edit.php?show_sticky=1' ) |
382 __( 'Use a <a href="%1$s">tag</a> to feature your posts. If no posts match the tag, <a href="%2$s">sticky posts</a> will be displayed instead.', 'twentyfourteen' ), |
407 ), |
383 esc_url( add_query_arg( 'tag', _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ), admin_url( 'edit.php' ) ) ), |
408 'priority' => 130, |
384 admin_url( 'edit.php?show_sticky=1' ) |
409 'theme_supports' => 'featured-content', |
385 ), |
410 ) ); |
386 'priority' => 130, |
|
387 'theme_supports' => 'featured-content', |
|
388 ) |
|
389 ); |
411 |
390 |
412 // Add Featured Content settings. |
391 // Add Featured Content settings. |
413 $wp_customize->add_setting( 'featured-content[tag-name]', array( |
392 $wp_customize->add_setting( |
414 'default' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ), |
393 'featured-content[tag-name]', array( |
415 'type' => 'option', |
394 'default' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ), |
416 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
395 'type' => 'option', |
417 ) ); |
396 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
418 $wp_customize->add_setting( 'featured-content[hide-tag]', array( |
397 ) |
419 'default' => true, |
398 ); |
420 'type' => 'option', |
399 $wp_customize->add_setting( |
421 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
400 'featured-content[hide-tag]', array( |
422 ) ); |
401 'default' => true, |
|
402 'type' => 'option', |
|
403 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ), |
|
404 ) |
|
405 ); |
423 |
406 |
424 // Add Featured Content controls. |
407 // Add Featured Content controls. |
425 $wp_customize->add_control( 'featured-content[tag-name]', array( |
408 $wp_customize->add_control( |
426 'label' => __( 'Tag Name', 'twentyfourteen' ), |
409 'featured-content[tag-name]', array( |
427 'section' => 'featured_content', |
410 'label' => __( 'Tag Name', 'twentyfourteen' ), |
428 'priority' => 20, |
411 'section' => 'featured_content', |
429 ) ); |
412 'priority' => 20, |
430 $wp_customize->add_control( 'featured-content[hide-tag]', array( |
413 ) |
431 'label' => __( 'Don’t display tag on front end.', 'twentyfourteen' ), |
414 ); |
432 'section' => 'featured_content', |
415 $wp_customize->add_control( |
433 'type' => 'checkbox', |
416 'featured-content[hide-tag]', array( |
434 'priority' => 30, |
417 'label' => __( 'Don’t display tag on front end.', 'twentyfourteen' ), |
435 ) ); |
418 'section' => 'featured_content', |
|
419 'type' => 'checkbox', |
|
420 'priority' => 30, |
|
421 ) |
|
422 ); |
436 } |
423 } |
437 |
424 |
438 /** |
425 /** |
439 * Enqueue the tag suggestion script. |
426 * Enqueue the tag suggestion script. |
440 * |
427 * |
441 * @static |
|
442 * @access public |
|
443 * @since Twenty Fourteen 1.0 |
428 * @since Twenty Fourteen 1.0 |
444 */ |
429 */ |
445 public static function enqueue_scripts() { |
430 public static function enqueue_scripts() { |
446 wp_enqueue_script( 'featured-content-suggest', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131022', true ); |
431 wp_enqueue_script( 'featured-content-suggest', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131022', true ); |
447 } |
432 } |