Нет описания

featured-content.php 23KB

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