PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 11.2
Jetpack – WP Security, Backup, Speed, & Growth v11.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 in Jetpack – WP Security, Backup, Speed, & Growth 11.2, at modules/theme-tools/featured-content.php

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