featured-content.php 21 KB

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