PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 6.2.2
Jetpack – WP Security, Backup, Speed, & Growth v6.2.2
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / theme-tools / featured-content.php
featured-content.php
679 lines 21.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! class_exists( 'Featured_Content' ) && isset( $GLOBALS['pagenow'] ) && 'plugins.php' !== $GLOBALS['pagenow'] ) {
4
5 /**
6 * Featured Content.
7 *
8 * This module will allow users to define a subset of posts to be displayed in a
9 * theme-designated featured content area.
10 *
11 * This feature will only be activated for themes that declare that they support
12 * it. This can be done by adding code similar to the following during the
13 * "after_setup_theme" action:
14 *
15 * add_theme_support( 'featured-content', array(
16 * 'filter' => 'mytheme_get_featured_content',
17 * 'max_posts' => 20,
18 * 'post_types' => array( 'post', 'page' ),
19 * ) );
20 *
21 * For maximum compatibility with different methods of posting users will
22 * designate a featured post tag to associate posts with. Since this tag now has
23 * special meaning beyond that of a normal tags, users will have the ability to
24 * hide it from the front-end of their site.
25 */
26 class Featured_Content {
27
28 /**
29 * The maximum number of posts that a Featured Content area can contain. We
30 * define a default value here but themes can override this by defining a
31 * "max_posts" entry in the second parameter passed in the call to
32 * add_theme_support( 'featured-content' ).
33 *
34 * @see Featured_Content::init()
35 */
36 public static $max_posts = 15;
37
38 /**
39 * The registered post types supported by Featured Content. Themes can add
40 * Featured Content support for registered post types by defining a
41 * 'post_types' argument (string|array) in the call to
42 * add_theme_support( 'featured-content' ).
43 *
44 * @see Featured_Content::init()
45 */
46 public static $post_types = array( 'post' );
47
48 /**
49 * The tag that is used to mark featured content. Users can define
50 * a custom tag name that will be stored in this variable.
51 *
52 * @see Featured_Content::hide_featured_term
53 */
54 public static $tag;
55
56 /**
57 * Instantiate.
58 *
59 * All custom functionality will be hooked into the "init" action.
60 */
61 public static function setup() {
62 add_action( 'init', array( __CLASS__, 'init' ), 30 );
63 }
64
65 /**
66 * Conditionally hook into WordPress.
67 *
68 * Themes must declare that they support this module by adding
69 * add_theme_support( 'featured-content' ); during after_setup_theme.
70 *
71 * If no theme support is found there is no need to hook into WordPress. We'll
72 * just return early instead.
73 *
74 * @uses Featured_Content::$max_posts
75 */
76 public static function init() {
77 $theme_support = get_theme_support( 'featured-content' );
78
79 // Return early if theme does not support featured content.
80 if ( ! $theme_support ) {
81 return;
82 }
83
84 /*
85 * An array of named arguments must be passed as the second parameter
86 * of add_theme_support().
87 */
88 if ( ! isset( $theme_support[0] ) ) {
89 return;
90 }
91
92 if ( isset( $theme_support[0]['featured_content_filter'] ) ) {
93 $theme_support[0]['filter'] = $theme_support[0]['featured_content_filter'];
94 unset( $theme_support[0]['featured_content_filter'] );
95 }
96
97 // Return early if "filter" has not been defined.
98 if ( ! isset( $theme_support[0]['filter'] ) ) {
99 return;
100 }
101
102 // Theme can override the number of max posts.
103 if ( isset( $theme_support[0]['max_posts'] ) ) {
104 self::$max_posts = absint( $theme_support[0]['max_posts'] );
105 }
106
107 add_filter( $theme_support[0]['filter'], array( __CLASS__, 'get_featured_posts' ) );
108 add_action( 'customize_register', array( __CLASS__, 'customize_register' ), 9 );
109 add_action( 'admin_init', array( __CLASS__, 'register_setting' ) );
110 add_action( 'save_post', array( __CLASS__, 'delete_transient' ) );
111 add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) );
112 add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
113 add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
114 add_action( 'switch_theme', array( __CLASS__, 'switch_theme' ) );
115 add_action( 'switch_theme', array( __CLASS__, 'delete_transient' ) );
116 add_action( 'wp_loaded', array( __CLASS__, 'wp_loaded' ) );
117 add_action( 'update_option_featured-content', array( __CLASS__, 'flush_post_tag_cache' ), 10, 2 );
118 add_action( 'delete_option_featured-content', array( __CLASS__, 'flush_post_tag_cache' ), 10, 2 );
119 add_action( 'split_shared_term', array( __CLASS__, 'jetpack_update_featured_content_for_split_terms', 10, 4 ) );
120
121
122 if ( isset( $theme_support[0]['additional_post_types'] ) ) {
123 $theme_support[0]['post_types'] = array_merge( array( 'post' ), (array) $theme_support[0]['additional_post_types'] );
124 unset( $theme_support[0]['additional_post_types'] );
125 }
126
127 // Themes can allow Featured Content pages
128 if ( isset( $theme_support[0]['post_types'] ) ) {
129 self::$post_types = array_merge( self::$post_types, (array) $theme_support[0]['post_types'] );
130
131 // register post_tag support for each post type
132 foreach ( self::$post_types as $post_type ) {
133 register_taxonomy_for_object_type( 'post_tag', $post_type );
134 }
135 }
136 }
137
138 /**
139 * Hide "featured" tag from the front-end.
140 *
141 * Has to run on wp_loaded so that the preview filters of the customizer
142 * have a chance to alter the value.
143 */
144 public static function wp_loaded() {
145 if ( self::get_setting( 'hide-tag' ) ) {
146 $settings = self::get_setting();
147
148 // This is done before setting filters for get_terms in order to avoid an infinite filter loop
149 self::$tag = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
150
151 add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 3 );
152 add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 );
153 }
154 }
155
156 /**
157 * Get featured posts
158 *
159 * This method is not intended to be called directly. Theme developers should
160 * place a filter directly in their theme and then pass its name as a value of
161 * the "filter" key in the array passed as the $args parameter during the call
162 * to: add_theme_support( 'featured-content', $args ).
163 *
164 * @uses Featured_Content::get_featured_post_ids()
165 *
166 * @return array
167 */
168 public static function get_featured_posts() {
169 $post_ids = self::get_featured_post_ids();
170
171 // No need to query if there is are no featured posts.
172 if ( empty( $post_ids ) ) {
173 return array();
174 }
175
176 $featured_posts = get_posts( array(
177 'include' => $post_ids,
178 'posts_per_page' => count( $post_ids ),
179 'post_type' => self::$post_types,
180 'suppress_filters' => false,
181 ) );
182
183 return $featured_posts;
184 }
185
186 /**
187 * Get featured post IDs
188 *
189 * This function will return the an array containing the post IDs of all
190 * featured posts.
191 *
192 * Sets the "featured_content_ids" transient.
193 *
194 * @return array Array of post IDs.
195 */
196 public static function get_featured_post_ids() {
197 // Return array of cached results if they exist.
198 $featured_ids = get_transient( 'featured_content_ids' );
199 if ( ! empty( $featured_ids ) ) {
200 return array_map(
201 'absint',
202 /**
203 * Filter the list of Featured Posts IDs.
204 *
205 * @module theme-tools
206 *
207 * @since 2.7.0
208 *
209 * @param array $featured_ids Array of post IDs.
210 */
211 apply_filters( 'featured_content_post_ids', (array) $featured_ids )
212 );
213 }
214
215 $settings = self::get_setting();
216
217 // Return empty array if no tag name is set.
218 $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
219 if ( ! $term ) {
220 $term = get_term_by( 'id', $settings['tag-id'], 'post_tag' );
221 }
222 if ( $term ) {
223 $tag = $term->term_id;
224 } else {
225 /** This action is documented in modules/theme-tools/featured-content.php */
226 return apply_filters( 'featured_content_post_ids', array() );
227 }
228
229 // Back compat for installs that have the quantity option still set.
230 $quantity = isset( $settings['quantity'] ) ? $settings['quantity'] : self::$max_posts;
231
232 // Query for featured posts.
233 $featured = get_posts( array(
234 'numberposts' => $quantity,
235 'post_type' => self::$post_types,
236 'suppress_filters' => false,
237 'tax_query' => array(
238 array(
239 'field' => 'term_id',
240 'taxonomy' => 'post_tag',
241 'terms' => $tag,
242 ),
243 ),
244 ) );
245
246 // Return empty array if no featured content exists.
247 if ( ! $featured ) {
248 /** This action is documented in modules/theme-tools/featured-content.php */
249 return apply_filters( 'featured_content_post_ids', array() );
250 }
251
252 // Ensure correct format before save/return.
253 $featured_ids = wp_list_pluck( (array) $featured, 'ID' );
254 $featured_ids = array_map( 'absint', $featured_ids );
255
256 set_transient( 'featured_content_ids', $featured_ids );
257
258 /** This action is documented in modules/theme-tools/featured-content.php */
259 return apply_filters( 'featured_content_post_ids', $featured_ids );
260 }
261
262 /**
263 * Delete Transient.
264 *
265 * Hooks in the "save_post" action.
266 * @see Featured_Content::validate_settings().
267 */
268 public static function delete_transient() {
269 delete_transient( 'featured_content_ids' );
270 }
271
272 /**
273 * Flush the Post Tag relationships cache.
274 *
275 * Hooks in the "update_option_featured-content" action.
276 */
277 public static function flush_post_tag_cache( $prev, $opts ) {
278 if ( ! empty( $opts ) && ! empty( $opts['tag-id'] ) ) {
279 $query = new WP_Query( array(
280 'tag_id' => (int) $opts['tag-id'],
281 'posts_per_page' => -1,
282 ) );
283 foreach ( $query->posts as $post ) {
284 wp_cache_delete( $post->ID, 'post_tag_relationships' );
285 }
286 }
287 }
288
289 /**
290 * Exclude featured posts from the blog query when the blog is the front-page,
291 * and user has not checked the "Also display tagged posts outside the Featured Content area" checkbox.
292 *
293 * Filter the home page posts, and remove any featured post ID's from it.
294 * Hooked onto the 'pre_get_posts' action, this changes the parameters of the
295 * query before it gets any posts.
296 *
297 * @uses Featured_Content::get_featured_post_ids();
298 * @uses Featured_Content::get_setting();
299 * @param WP_Query $query
300 * @return WP_Query Possibly modified WP_Query
301 */
302 public static function pre_get_posts( $query ) {
303
304 // Bail if not home or not main query.
305 if ( ! $query->is_home() || ! $query->is_main_query() ) {
306 return;
307 }
308
309 // Bail if the blog page is not the front page.
310 if ( 'posts' !== get_option( 'show_on_front' ) ) {
311 return;
312 }
313
314 $featured = self::get_featured_post_ids();
315
316 // Bail if no featured posts.
317 if ( ! $featured ) {
318 return;
319 }
320
321 $settings = self::get_setting();
322
323 // Bail if the user wants featured posts always displayed.
324 if ( true == $settings['show-all'] ) {
325 return;
326 }
327
328 // We need to respect post ids already in the blacklist.
329 $post__not_in = $query->get( 'post__not_in' );
330
331 if ( ! empty( $post__not_in ) ) {
332 $featured = array_merge( (array) $post__not_in, $featured );
333 $featured = array_unique( $featured );
334 }
335
336 $query->set( 'post__not_in', $featured );
337 }
338
339 /**
340 * Reset tag option when the saved tag is deleted.
341 *
342 * It's important to mention that the transient needs to be deleted, too.
343 * While it may not be obvious by looking at the function alone, the transient
344 * is deleted by Featured_Content::validate_settings().
345 *
346 * Hooks in the "delete_post_tag" action.
347 * @see Featured_Content::validate_settings().
348 *
349 * @param int $tag_id The term_id of the tag that has been deleted.
350 * @return void
351 */
352 public static function delete_post_tag( $tag_id ) {
353 $settings = self::get_setting();
354
355 if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] ) {
356 return;
357 }
358
359 $settings['tag-id'] = 0;
360 $settings = self::validate_settings( $settings );
361 update_option( 'featured-content', $settings );
362 }
363
364 /**
365 * Hide featured tag from displaying when global terms are queried from
366 * the front-end.
367 *
368 * Hooks into the "get_terms" filter.
369 *
370 * @uses Featured_Content::get_setting()
371 *
372 * @param array $terms A list of term objects. This is the return value of get_terms().
373 * @param array $taxonomies An array of taxonomy slugs.
374 * @return array $terms
375 */
376 public static function hide_featured_term( $terms, $taxonomies, $args ) {
377
378 // This filter is only appropriate on the front-end.
379 if ( is_admin() ) {
380 return $terms;
381 }
382
383 // We only want to hide the featured tag.
384 if ( ! in_array( 'post_tag', $taxonomies ) ) {
385 return $terms;
386 }
387
388 // Bail if no terms were returned.
389 if ( empty( $terms ) ) {
390 return $terms;
391 }
392
393 // Bail if term objects are unavailable.
394 if ( 'all' != $args['fields'] ) {
395 return $terms;
396 }
397
398 $settings = self::get_setting();
399
400 if ( false !== self::$tag ) {
401 foreach ( $terms as $order => $term ) {
402 if (
403 is_object( $term )
404 && (
405 $settings['tag-id'] === $term->term_id
406 || $settings['tag-name'] === $term->name
407 )
408 ) {
409 unset( $terms[ $order ] );
410 }
411 }
412 }
413
414 return $terms;
415 }
416
417 /**
418 * Hide featured tag from displaying when terms associated with a post object
419 * are queried from the front-end.
420 *
421 * Hooks into the "get_the_terms" filter.
422 *
423 * @uses Featured_Content::get_setting()
424 *
425 * @param array $terms A list of term objects. This is the return value of get_the_terms().
426 * @param int $id The ID field for the post object that terms are associated with.
427 * @param array $taxonomy An array of taxonomy slugs.
428 * @return array $terms
429 */
430 public static function hide_the_featured_term( $terms, $id, $taxonomy ) {
431
432 // This filter is only appropriate on the front-end.
433 if ( is_admin() ) {
434 return $terms;
435 }
436
437 // Make sure we are in the correct taxonomy.
438 if ( 'post_tag' != $taxonomy ) {
439 return $terms;
440 }
441
442 // No terms? Return early!
443 if ( empty( $terms ) ) {
444 return $terms;
445 }
446
447 $settings = self::get_setting();
448 $tag = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
449
450 if ( false !== $tag ) {
451 foreach ( $terms as $order => $term ) {
452 if ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) {
453 unset( $terms[ $order ] );
454 }
455 }
456 }
457
458 return $terms;
459 }
460
461 /**
462 * Register custom setting on the Settings -> Reading screen.
463 *
464 * @uses Featured_Content::render_form()
465 * @uses Featured_Content::validate_settings()
466 *
467 * @return void
468 */
469 public static function register_setting() {
470 add_settings_field( 'featured-content', __( 'Featured Content', 'jetpack' ), array( __class__, 'render_form' ), 'reading' );
471
472 // Register sanitization callback for the Customizer.
473 register_setting( 'featured-content', 'featured-content', array( __class__, 'validate_settings' ) );
474 }
475
476 /**
477 * Add settings to the Customizer.
478 *
479 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
480 */
481 public static function customize_register( $wp_customize ) {
482 $wp_customize->add_section( 'featured_content', array(
483 'title' => esc_html__( 'Featured Content', 'jetpack' ),
484 'description' => sprintf( __( 'Easily feature all posts with the <a href="%1$s">"featured" tag</a> or a tag of your choice. Your theme supports up to %2$s posts in its featured content area.', 'jetpack' ), admin_url( '/edit.php?tag=featured' ), absint( self::$max_posts ) ),
485 'priority' => 130,
486 'theme_supports' => 'featured-content',
487 ) );
488
489 /* Add Featured Content settings.
490 *
491 * Sanitization callback registered in Featured_Content::validate_settings().
492 * See http://themeshaper.com/2013/04/29/validation-sanitization-in-customizer/comment-page-1/#comment-12374
493 */
494 $wp_customize->add_setting( 'featured-content[tag-name]', array(
495 'type' => 'option',
496 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
497 ) );
498 $wp_customize->add_setting( 'featured-content[hide-tag]', array(
499 'default' => true,
500 'type' => 'option',
501 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
502 ) );
503 $wp_customize->add_setting( 'featured-content[show-all]', array(
504 'default' => false,
505 'type' => 'option',
506 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
507 ) );
508
509 // Add Featured Content controls.
510 $wp_customize->add_control( 'featured-content[tag-name]', array(
511 'label' => esc_html__( 'Tag name', 'jetpack' ),
512 'section' => 'featured_content',
513 'theme_supports' => 'featured-content',
514 'priority' => 20,
515 ) );
516 $wp_customize->add_control( 'featured-content[hide-tag]', array(
517 'label' => esc_html__( 'Do not display tag in post details and tag clouds.', 'jetpack' ),
518 'section' => 'featured_content',
519 'theme_supports' => 'featured-content',
520 'type' => 'checkbox',
521 'priority' => 30,
522 ) );
523 $wp_customize->add_control( 'featured-content[show-all]', array(
524 'label' => esc_html__( 'Also display tagged posts outside the Featured Content area.', 'jetpack' ),
525 'section' => 'featured_content',
526 'theme_supports' => 'featured-content',
527 'type' => 'checkbox',
528 'priority' => 40,
529 ) );
530 }
531
532 /**
533 * Enqueue the tag suggestion script.
534 */
535 public static function enqueue_scripts() {
536 wp_enqueue_script( 'featured-content-suggest', plugins_url( 'js/suggest.js', __FILE__ ), array( 'suggest' ), '20131022', true );
537 }
538
539 /**
540 * Renders all form fields on the Settings -> Reading screen.
541 */
542 public static function render_form() {
543 printf( __( 'The settings for Featured Content have <a href="%s">moved to Appearance &rarr; Customize</a>.', 'jetpack' ), admin_url( 'customize.php?#accordion-section-featured_content' ) );
544 }
545
546 /**
547 * Get settings
548 *
549 * Get all settings recognized by this module. This function will return all
550 * settings whether or not they have been stored in the database yet. This
551 * ensures that all keys are available at all times.
552 *
553 * In the event that you only require one setting, you may pass its name as the
554 * first parameter to the function and only that value will be returned.
555 *
556 * @param string $key The key of a recognized setting.
557 * @return mixed Array of all settings by default. A single value if passed as first parameter.
558 */
559 public static function get_setting( $key = 'all' ) {
560 $saved = (array) get_option( 'featured-content' );
561
562 /**
563 * Filter Featured Content's default settings.
564 *
565 * @module theme-tools
566 *
567 * @since 2.7.0
568 *
569 * @param array $args {
570 * Array of Featured Content Settings
571 *
572 * @type int hide-tag Default is 1.
573 * @type int tag-id Default is 0.
574 * @type string tag-name Default is empty.
575 * @type int show-all Default is 0.
576 * }
577 */
578 $defaults = apply_filters( 'featured_content_default_settings', array(
579 'hide-tag' => 1,
580 'tag-id' => 0,
581 'tag-name' => '',
582 'show-all' => 0,
583 ) );
584
585 $options = wp_parse_args( $saved, $defaults );
586 $options = array_intersect_key( $options, $defaults );
587
588 if ( 'all' != $key ) {
589 return isset( $options[ $key ] ) ? $options[ $key ] : false;
590 }
591
592 return $options;
593 }
594
595 /**
596 * Validate settings
597 *
598 * Make sure that all user supplied content is in an expected format before
599 * saving to the database. This function will also delete the transient set in
600 * Featured_Content::get_featured_content().
601 *
602 * @uses Featured_Content::delete_transient()
603 *
604 * @param array $input
605 * @return array $output
606 */
607 public static function validate_settings( $input ) {
608 $output = array();
609
610 if ( empty( $input['tag-name'] ) ) {
611 $output['tag-id'] = 0;
612 } else {
613 $term = get_term_by( 'name', $input['tag-name'], 'post_tag' );
614
615 if ( $term ) {
616 $output['tag-id'] = $term->term_id;
617 } else {
618 $new_tag = wp_create_tag( $input['tag-name'] );
619
620 if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) {
621 $output['tag-id'] = $new_tag['term_id'];
622 }
623 }
624
625 $output['tag-name'] = $input['tag-name'];
626 }
627
628 $output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0;
629
630 $output['show-all'] = isset( $input['show-all'] ) && $input['show-all'] ? 1 : 0;
631
632 self::delete_transient();
633
634 return $output;
635 }
636
637 /**
638 * Removes the quantity setting from the options array.
639 *
640 * @return void
641 */
642 public static function switch_theme() {
643 $option = (array) get_option( 'featured-content' );
644
645 if ( isset( $option['quantity'] ) ) {
646 unset( $option['quantity'] );
647 update_option( 'featured-content', $option );
648 }
649 }
650
651 public static function jetpack_update_featured_content_for_split_terms( $old_term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
652 $featured_content_settings = get_option( 'featured-content', array() );
653
654 // Check to see whether the stored tag ID is the one that's just been split.
655 if ( isset( $featured_content_settings['tag-id'] ) && $old_term_id == $featured_content_settings['tag-id'] && 'post_tag' == $taxonomy ) {
656 // We have a match, so we swap out the old tag ID for the new one and resave the option.
657 $featured_content_settings['tag-id'] = $new_term_id;
658 update_option( 'featured-content', $featured_content_settings );
659 }
660 }
661 }
662
663 Featured_Content::setup();
664
665 /**
666 * Adds the featured content plugin to the set of files for which action
667 * handlers should be copied when the theme context is loaded by the REST API.
668 *
669 * @param array $copy_dirs Copy paths with actions to be copied
670 * @return array Copy paths with featured content plugin
671 */
672 function wpcom_rest_api_featured_content_copy_plugin_actions( $copy_dirs ) {
673 $copy_dirs[] = __FILE__;
674 return $copy_dirs;
675 }
676 add_action( 'restapi_theme_action_copy_dirs', 'wpcom_rest_api_featured_content_copy_plugin_actions' );
677
678 } // end if ( ! class_exists( 'Featured_Content' ) && isset( $GLOBALS['pagenow'] ) && 'plugins.php' !== $GLOBALS['pagenow'] ) {
679