featured-content.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  1. <?php
  2. /**
  3. * Twenty Fourteen Featured Content
  4. *
  5. * This module allows you to define a subset of posts to be displayed
  6. * in the theme's Featured Content area.
  7. *
  8. * For maximum compatibility with different methods of posting users
  9. * will designate a featured post tag to associate posts with. Since
  10. * this tag now has special meaning beyond that of a normal tags, users
  11. * will have the ability to hide it from the front-end of their site.
  12. */
  13. class Featured_Content {
  14. /**
  15. * The maximum number of posts a Featured Content area can contain.
  16. *
  17. * We define a default value here but themes can override
  18. * this by defining a "max_posts" entry in the second parameter
  19. * passed in the call to add_theme_support( 'featured-content' ).
  20. *
  21. * @see Featured_Content::init()
  22. *
  23. * @since Twenty Fourteen 1.0
  24. *
  25. * @static
  26. * @access public
  27. * @var int
  28. */
  29. public static $max_posts = 15;
  30. /**
  31. * Instantiate.
  32. *
  33. * All custom functionality will be hooked into the "init" action.
  34. *
  35. * @static
  36. * @access public
  37. * @since Twenty Fourteen 1.0
  38. */
  39. public static function setup() {
  40. add_action( 'init', array( __CLASS__, 'init' ), 30 );
  41. }
  42. /**
  43. * Conditionally hook into WordPress.
  44. *
  45. * Theme must declare that they support this module by adding
  46. * add_theme_support( 'featured-content' ); during after_setup_theme.
  47. *
  48. * If no theme support is found there is no need to hook into WordPress.
  49. * We'll just return early instead.
  50. *
  51. * @static
  52. * @access public
  53. * @since Twenty Fourteen 1.0
  54. */
  55. public static function init() {
  56. $theme_support = get_theme_support( 'featured-content' );
  57. // Return early if theme does not support Featured Content.
  58. if ( ! $theme_support ) {
  59. return;
  60. }
  61. /*
  62. * An array of named arguments must be passed as the second parameter
  63. * of add_theme_support().
  64. */
  65. if ( ! isset( $theme_support[0] ) ) {
  66. return;
  67. }
  68. // Return early if "featured_content_filter" has not been defined.
  69. if ( ! isset( $theme_support[0]['featured_content_filter'] ) ) {
  70. return;
  71. }
  72. $filter = $theme_support[0]['featured_content_filter'];
  73. // Theme can override the number of max posts.
  74. if ( isset( $theme_support[0]['max_posts'] ) ) {
  75. self::$max_posts = absint( $theme_support[0]['max_posts'] );
  76. }
  77. add_filter( $filter, array( __CLASS__, 'get_featured_posts' ) );
  78. add_action( 'customize_register', array( __CLASS__, 'customize_register' ), 9 );
  79. add_action( 'admin_init', array( __CLASS__, 'register_setting' ) );
  80. add_action( 'save_post', array( __CLASS__, 'delete_transient' ) );
  81. add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) );
  82. add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
  83. add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
  84. add_action( 'wp_loaded', array( __CLASS__, 'wp_loaded' ) );
  85. }
  86. /**
  87. * Hide "featured" tag from the front-end.
  88. *
  89. * Has to run on wp_loaded so that the preview filters of the customizer
  90. * have a chance to alter the value.
  91. *
  92. * @static
  93. * @access public
  94. * @since Twenty Fourteen 1.0
  95. */
  96. public static function wp_loaded() {
  97. if ( self::get_setting( 'hide-tag' ) ) {
  98. add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 2 );
  99. add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 );
  100. }
  101. }
  102. /**
  103. * Get featured posts.
  104. *
  105. * @static
  106. * @access public
  107. * @since Twenty Fourteen 1.0
  108. *
  109. * @return array Array of featured posts.
  110. */
  111. public static function get_featured_posts() {
  112. $post_ids = self::get_featured_post_ids();
  113. // No need to query if there is are no featured posts.
  114. if ( empty( $post_ids ) ) {
  115. return array();
  116. }
  117. $featured_posts = get_posts( array(
  118. 'include' => $post_ids,
  119. 'posts_per_page' => count( $post_ids ),
  120. ) );
  121. return $featured_posts;
  122. }
  123. /**
  124. * Get featured post IDs
  125. *
  126. * This function will return the an array containing the
  127. * post IDs of all featured posts.
  128. *
  129. * Sets the "featured_content_ids" transient.
  130. *
  131. * @static
  132. * @access public
  133. * @since Twenty Fourteen 1.0
  134. *
  135. * @return array Array of post IDs.
  136. */
  137. public static function get_featured_post_ids() {
  138. // Return array of cached results if they exist.
  139. $featured_ids = get_transient( 'featured_content_ids' );
  140. if ( ! empty( $featured_ids ) ) {
  141. return array_map( 'absint', (array) $featured_ids );
  142. }
  143. $settings = self::get_setting();
  144. // Return sticky post ids if no tag name is set.
  145. $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
  146. if ( $term ) {
  147. $tag = $term->term_id;
  148. } else {
  149. return self::get_sticky_posts();
  150. }
  151. // Query for featured posts.
  152. $featured = get_posts( array(
  153. 'numberposts' => $settings['quantity'],
  154. 'tax_query' => array(
  155. array(
  156. 'field' => 'term_id',
  157. 'taxonomy' => 'post_tag',
  158. 'terms' => $tag,
  159. ),
  160. ),
  161. ) );
  162. // Return array with sticky posts if no Featured Content exists.
  163. if ( ! $featured ) {
  164. return self::get_sticky_posts();
  165. }
  166. // Ensure correct format before save/return.
  167. $featured_ids = wp_list_pluck( (array) $featured, 'ID' );
  168. $featured_ids = array_map( 'absint', $featured_ids );
  169. set_transient( 'featured_content_ids', $featured_ids );
  170. return $featured_ids;
  171. }
  172. /**
  173. * Return an array with IDs of posts maked as sticky.
  174. *
  175. * @static
  176. * @access public
  177. * @since Twenty Fourteen 1.0
  178. *
  179. * @return array Array of sticky posts.
  180. */
  181. public static function get_sticky_posts() {
  182. $settings = self::get_setting();
  183. return array_slice( get_option( 'sticky_posts', array() ), 0, $settings['quantity'] );
  184. }
  185. /**
  186. * Delete featured content ids transient.
  187. *
  188. * Hooks in the "save_post" action.
  189. *
  190. * @see Featured_Content::validate_settings().
  191. *
  192. * @static
  193. * @access public
  194. * @since Twenty Fourteen 1.0
  195. */
  196. public static function delete_transient() {
  197. delete_transient( 'featured_content_ids' );
  198. }
  199. /**
  200. * Exclude featured posts from the home page blog query.
  201. *
  202. * Filter the home page posts, and remove any featured post ID's from it.
  203. * Hooked onto the 'pre_get_posts' action, this changes the parameters of
  204. * the query before it gets any posts.
  205. *
  206. * @static
  207. * @access public
  208. * @since Twenty Fourteen 1.0
  209. *
  210. * @param WP_Query $query WP_Query object.
  211. * @return WP_Query Possibly-modified WP_Query.
  212. */
  213. public static function pre_get_posts( $query ) {
  214. // Bail if not home or not main query.
  215. if ( ! $query->is_home() || ! $query->is_main_query() ) {
  216. return;
  217. }
  218. $page_on_front = get_option( 'page_on_front' );
  219. // Bail if the blog page is not the front page.
  220. if ( ! empty( $page_on_front ) ) {
  221. return;
  222. }
  223. $featured = self::get_featured_post_ids();
  224. // Bail if no featured posts.
  225. if ( ! $featured ) {
  226. return;
  227. }
  228. // We need to respect post ids already in the blacklist.
  229. $post__not_in = $query->get( 'post__not_in' );
  230. if ( ! empty( $post__not_in ) ) {
  231. $featured = array_merge( (array) $post__not_in, $featured );
  232. $featured = array_unique( $featured );
  233. }
  234. $query->set( 'post__not_in', $featured );
  235. }
  236. /**
  237. * Reset tag option when the saved tag is deleted.
  238. *
  239. * It's important to mention that the transient needs to be deleted,
  240. * too. While it may not be obvious by looking at the function alone,
  241. * the transient is deleted by Featured_Content::validate_settings().
  242. *
  243. * Hooks in the "delete_post_tag" action.
  244. *
  245. * @see Featured_Content::validate_settings().
  246. *
  247. * @static
  248. * @access public
  249. * @since Twenty Fourteen 1.0
  250. *
  251. * @param int $tag_id The term_id of the tag that has been deleted.
  252. * @return void
  253. */
  254. public static function delete_post_tag( $tag_id ) {
  255. $settings = self::get_setting();
  256. if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] ) {
  257. return;
  258. }
  259. $settings['tag-id'] = 0;
  260. $settings = self::validate_settings( $settings );
  261. update_option( 'featured-content', $settings );
  262. }
  263. /**
  264. * Hide featured tag from displaying when global terms are queried from the front-end.
  265. *
  266. * Hooks into the "get_terms" filter.
  267. *
  268. * @static
  269. * @access public
  270. * @since Twenty Fourteen 1.0
  271. *
  272. * @param array $terms List of term objects. This is the return value of get_terms().
  273. * @param array $taxonomies An array of taxonomy slugs.
  274. * @return array A filtered array of terms.
  275. *
  276. * @uses Featured_Content::get_setting()
  277. */
  278. public static function hide_featured_term( $terms, $taxonomies ) {
  279. // This filter is only appropriate on the front-end.
  280. if ( is_admin() ) {
  281. return $terms;
  282. }
  283. // We only want to hide the featured tag.
  284. if ( ! in_array( 'post_tag', $taxonomies ) ) {
  285. return $terms;
  286. }
  287. // Bail if no terms were returned.
  288. if ( empty( $terms ) ) {
  289. return $terms;
  290. }
  291. foreach( $terms as $order => $term ) {
  292. if ( self::get_setting( 'tag-id' ) == $term->term_id && 'post_tag' == $term->taxonomy ) {
  293. unset( $terms[ $order ] );
  294. }
  295. }
  296. return $terms;
  297. }
  298. /**
  299. * Hide featured tag from display when terms associated with a post object
  300. * are queried from the front-end.
  301. *
  302. * Hooks into the "get_the_terms" filter.
  303. *
  304. * @static
  305. * @access public
  306. * @since Twenty Fourteen 1.0
  307. *
  308. * @param array $terms A list of term objects. This is the return value of get_the_terms().
  309. * @param int $id The ID field for the post object that terms are associated with.
  310. * @param array $taxonomy An array of taxonomy slugs.
  311. * @return array Filtered array of terms.
  312. *
  313. * @uses Featured_Content::get_setting()
  314. */
  315. public static function hide_the_featured_term( $terms, $id, $taxonomy ) {
  316. // This filter is only appropriate on the front-end.
  317. if ( is_admin() ) {
  318. return $terms;
  319. }
  320. // Make sure we are in the correct taxonomy.
  321. if ( 'post_tag' != $taxonomy ) {
  322. return $terms;
  323. }
  324. // No terms? Return early!
  325. if ( empty( $terms ) ) {
  326. return $terms;
  327. }
  328. foreach( $terms as $order => $term ) {
  329. if ( self::get_setting( 'tag-id' ) == $term->term_id ) {
  330. unset( $terms[ $term->term_id ] );
  331. }
  332. }
  333. return $terms;
  334. }
  335. /**
  336. * Register custom setting on the Settings -> Reading screen.
  337. *
  338. * @static
  339. * @access public
  340. * @since Twenty Fourteen 1.0
  341. *
  342. * @return void
  343. */
  344. public static function register_setting() {
  345. register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
  346. }
  347. /**
  348. * Add settings to the Customizer.
  349. *
  350. * @static
  351. * @access public
  352. * @since Twenty Fourteen 1.0
  353. *
  354. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  355. */
  356. public static function customize_register( $wp_customize ) {
  357. $wp_customize->add_section( 'featured_content', array(
  358. 'title' => __( 'Featured Content', 'twentyfourteen' ),
  359. 'description' => sprintf( __( 'Use the <a href="%1$s">"featured" tag</a> to feature your posts. You can change this to a tag of your choice; if no posts match the tag, <a href="%2$s">sticky posts</a> will be displayed instead.', 'twentyfourteen' ), admin_url( '/edit.php?tag=featured' ), admin_url( '/edit.php?show_sticky=1' ) ),
  360. 'priority' => 130,
  361. 'theme_supports' => 'featured-content',
  362. ) );
  363. // Add Featured Content settings.
  364. $wp_customize->add_setting( 'featured-content[tag-name]', array(
  365. 'default' => 'featured',
  366. 'type' => 'option',
  367. 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
  368. ) );
  369. $wp_customize->add_setting( 'featured-content[hide-tag]', array(
  370. 'default' => true,
  371. 'type' => 'option',
  372. 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
  373. ) );
  374. // Add Featured Content controls.
  375. $wp_customize->add_control( 'featured-content[tag-name]', array(
  376. 'label' => __( 'Tag Name', 'twentyfourteen' ),
  377. 'section' => 'featured_content',
  378. 'priority' => 20,
  379. ) );
  380. $wp_customize->add_control( 'featured-content[hide-tag]', array(
  381. 'label' => __( 'Don&rsquo;t display tag on front end.', 'twentyfourteen' ),
  382. 'section' => 'featured_content',
  383. 'type' => 'checkbox',
  384. 'priority' => 30,
  385. ) );
  386. }
  387. /**
  388. * Enqueue the tag suggestion script.
  389. *
  390. * @static
  391. * @access public
  392. * @since Twenty Fourteen 1.0
  393. */
  394. public static function enqueue_scripts() {
  395. wp_enqueue_script( 'featured-content-suggest', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131022', true );
  396. }
  397. /**
  398. * Get featured content settings.
  399. *
  400. * Get all settings recognized by this module. This function
  401. * will return all settings whether or not they have been stored
  402. * in the database yet. This ensures that all keys are available
  403. * at all times.
  404. *
  405. * In the event that you only require one setting, you may pass
  406. * its name as the first parameter to the function and only that
  407. * value will be returned.
  408. *
  409. * @static
  410. * @access public
  411. * @since Twenty Fourteen 1.0
  412. *
  413. * @param string $key The key of a recognized setting.
  414. * @return mixed Array of all settings by default. A single value if passed as first parameter.
  415. */
  416. public static function get_setting( $key = 'all' ) {
  417. $saved = (array) get_option( 'featured-content' );
  418. $defaults = array(
  419. 'hide-tag' => 1,
  420. 'quantity' => 6,
  421. 'tag-id' => 0,
  422. 'tag-name' => 'featured',
  423. );
  424. $options = wp_parse_args( $saved, $defaults );
  425. $options = array_intersect_key( $options, $defaults );
  426. $options['quantity'] = self::sanitize_quantity( $options['quantity'] );
  427. if ( 'all' != $key ) {
  428. return isset( $options[ $key ] ) ? $options[ $key ] : false;
  429. }
  430. return $options;
  431. }
  432. /**
  433. * Validate featured content settings.
  434. *
  435. * Make sure that all user supplied content is in an expected
  436. * format before saving to the database. This function will also
  437. * delete the transient set in Featured_Content::get_featured_content().
  438. *
  439. * @static
  440. * @access public
  441. * @since Twenty Fourteen 1.0
  442. *
  443. * @param array $input Array of settings input.
  444. * @return array Validated settings output.
  445. */
  446. public static function validate_settings( $input ) {
  447. $output = array();
  448. if ( empty( $input['tag-name'] ) ) {
  449. $output['tag-id'] = 0;
  450. } else {
  451. $term = get_term_by( 'name', $input['tag-name'], 'post_tag' );
  452. if ( $term ) {
  453. $output['tag-id'] = $term->term_id;
  454. } else {
  455. $new_tag = wp_create_tag( $input['tag-name'] );
  456. if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) {
  457. $output['tag-id'] = $new_tag['term_id'];
  458. }
  459. }
  460. $output['tag-name'] = $input['tag-name'];
  461. }
  462. if ( isset( $input['quantity'] ) ) {
  463. $output['quantity'] = self::sanitize_quantity( $input['quantity'] );
  464. }
  465. $output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0;
  466. // Delete the featured post ids transient.
  467. self::delete_transient();
  468. return $output;
  469. }
  470. /**
  471. * Sanitize quantity of featured posts.
  472. *
  473. * @static
  474. * @access public
  475. * @since Twenty Fourteen 1.0
  476. *
  477. * @param int $input The value to sanitize.
  478. * @return int A number between 1 and FeaturedContent::$max_posts.
  479. */
  480. public static function sanitize_quantity( $input ) {
  481. $quantity = absint( $input );
  482. if ( $quantity > self::$max_posts ) {
  483. $quantity = self::$max_posts;
  484. } else if ( 1 > $quantity ) {
  485. $quantity = 1;
  486. }
  487. return $quantity;
  488. }
  489. } // Featured_Content
  490. Featured_Content::setup();