featured-content.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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( 'switch_theme', array( __CLASS__, 'delete_transient' ) );
  81. add_action( 'save_post', array( __CLASS__, 'delete_transient' ) );
  82. add_action( 'delete_post_tag', array( __CLASS__, 'delete_post_tag' ) );
  83. add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
  84. add_action( 'pre_get_posts', array( __CLASS__, 'pre_get_posts' ) );
  85. add_action( 'wp_loaded', array( __CLASS__, 'wp_loaded' ) );
  86. }
  87. /**
  88. * Hide "featured" tag from the front-end.
  89. *
  90. * Has to run on wp_loaded so that the preview filters of the customizer
  91. * have a chance to alter the value.
  92. *
  93. * @static
  94. * @access public
  95. * @since Twenty Fourteen 1.0
  96. */
  97. public static function wp_loaded() {
  98. if ( self::get_setting( 'hide-tag' ) ) {
  99. add_filter( 'get_terms', array( __CLASS__, 'hide_featured_term' ), 10, 3 );
  100. add_filter( 'get_the_terms', array( __CLASS__, 'hide_the_featured_term' ), 10, 3 );
  101. }
  102. }
  103. /**
  104. * Get featured posts.
  105. *
  106. * @static
  107. * @access public
  108. * @since Twenty Fourteen 1.0
  109. *
  110. * @return array Array of featured posts.
  111. */
  112. public static function get_featured_posts() {
  113. $post_ids = self::get_featured_post_ids();
  114. // No need to query if there is are no featured posts.
  115. if ( empty( $post_ids ) ) {
  116. return array();
  117. }
  118. $featured_posts = get_posts( array(
  119. 'include' => $post_ids,
  120. 'posts_per_page' => count( $post_ids ),
  121. ) );
  122. return $featured_posts;
  123. }
  124. /**
  125. * Get featured post IDs
  126. *
  127. * This function will return the an array containing the
  128. * post IDs of all featured posts.
  129. *
  130. * Sets the "featured_content_ids" transient.
  131. *
  132. * @static
  133. * @access public
  134. * @since Twenty Fourteen 1.0
  135. *
  136. * @return array Array of post IDs.
  137. */
  138. public static function get_featured_post_ids() {
  139. // Get array of cached results if they exist.
  140. $featured_ids = get_transient( 'featured_content_ids' );
  141. if ( false === $featured_ids ) {
  142. $settings = self::get_setting();
  143. $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
  144. if ( $term ) {
  145. // Query for featured posts.
  146. $featured_ids = get_posts( array(
  147. 'fields' => 'ids',
  148. 'numberposts' => self::$max_posts,
  149. 'suppress_filters' => false,
  150. 'tax_query' => array(
  151. array(
  152. 'field' => 'term_id',
  153. 'taxonomy' => 'post_tag',
  154. 'terms' => $term->term_id,
  155. ),
  156. ),
  157. ) );
  158. }
  159. // Get sticky posts if no Featured Content exists.
  160. if ( ! $featured_ids ) {
  161. $featured_ids = self::get_sticky_posts();
  162. }
  163. set_transient( 'featured_content_ids', $featured_ids );
  164. }
  165. // Ensure correct format before return.
  166. return array_map( 'absint', $featured_ids );
  167. }
  168. /**
  169. * Return an array with IDs of posts maked as sticky.
  170. *
  171. * @static
  172. * @access public
  173. * @since Twenty Fourteen 1.0
  174. *
  175. * @return array Array of sticky posts.
  176. */
  177. public static function get_sticky_posts() {
  178. return array_slice( get_option( 'sticky_posts', array() ), 0, self::$max_posts );
  179. }
  180. /**
  181. * Delete featured content ids transient.
  182. *
  183. * Hooks in the "save_post" action.
  184. *
  185. * @see Featured_Content::validate_settings().
  186. *
  187. * @static
  188. * @access public
  189. * @since Twenty Fourteen 1.0
  190. */
  191. public static function delete_transient() {
  192. delete_transient( 'featured_content_ids' );
  193. }
  194. /**
  195. * Exclude featured posts from the home page blog query.
  196. *
  197. * Filter the home page posts, and remove any featured post ID's from it.
  198. * Hooked onto the 'pre_get_posts' action, this changes the parameters of
  199. * the query before it gets any posts.
  200. *
  201. * @static
  202. * @access public
  203. * @since Twenty Fourteen 1.0
  204. *
  205. * @param WP_Query $query WP_Query object.
  206. * @return WP_Query Possibly-modified WP_Query.
  207. */
  208. public static function pre_get_posts( $query ) {
  209. // Bail if not home or not main query.
  210. if ( ! $query->is_home() || ! $query->is_main_query() ) {
  211. return;
  212. }
  213. // Bail if the blog page is not the front page.
  214. if ( 'posts' !== get_option( 'show_on_front' ) ) {
  215. return;
  216. }
  217. $featured = self::get_featured_post_ids();
  218. // Bail if no featured posts.
  219. if ( ! $featured ) {
  220. return;
  221. }
  222. // We need to respect post ids already in the blacklist.
  223. $post__not_in = $query->get( 'post__not_in' );
  224. if ( ! empty( $post__not_in ) ) {
  225. $featured = array_merge( (array) $post__not_in, $featured );
  226. $featured = array_unique( $featured );
  227. }
  228. $query->set( 'post__not_in', $featured );
  229. }
  230. /**
  231. * Reset tag option when the saved tag is deleted.
  232. *
  233. * It's important to mention that the transient needs to be deleted,
  234. * too. While it may not be obvious by looking at the function alone,
  235. * the transient is deleted by Featured_Content::validate_settings().
  236. *
  237. * Hooks in the "delete_post_tag" action.
  238. *
  239. * @see Featured_Content::validate_settings().
  240. *
  241. * @static
  242. * @access public
  243. * @since Twenty Fourteen 1.0
  244. *
  245. * @param int $tag_id The term_id of the tag that has been deleted.
  246. */
  247. public static function delete_post_tag( $tag_id ) {
  248. $settings = self::get_setting();
  249. if ( empty( $settings['tag-id'] ) || $tag_id != $settings['tag-id'] ) {
  250. return;
  251. }
  252. $settings['tag-id'] = 0;
  253. $settings = self::validate_settings( $settings );
  254. update_option( 'featured-content', $settings );
  255. }
  256. /**
  257. * Hide featured tag from displaying when global terms are queried from the front-end.
  258. *
  259. * Hooks into the "get_terms" filter.
  260. *
  261. * @static
  262. * @access public
  263. * @since Twenty Fourteen 1.0
  264. *
  265. * @param array $terms List of term objects. This is the return value of get_terms().
  266. * @param array $taxonomies An array of taxonomy slugs.
  267. * @return array A filtered array of terms.
  268. *
  269. * @uses Featured_Content::get_setting()
  270. */
  271. public static function hide_featured_term( $terms, $taxonomies, $args ) {
  272. // This filter is only appropriate on the front-end.
  273. if ( is_admin() ) {
  274. return $terms;
  275. }
  276. // We only want to hide the featured tag.
  277. if ( ! in_array( 'post_tag', $taxonomies ) ) {
  278. return $terms;
  279. }
  280. // Bail if no terms were returned.
  281. if ( empty( $terms ) ) {
  282. return $terms;
  283. }
  284. // Bail if term objects are unavailable.
  285. if ( 'all' != $args['fields'] ) {
  286. return $terms;
  287. }
  288. $settings = self::get_setting();
  289. foreach( $terms as $order => $term ) {
  290. if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
  291. unset( $terms[ $order ] );
  292. }
  293. }
  294. return $terms;
  295. }
  296. /**
  297. * Hide featured tag from display when terms associated with a post object
  298. * are queried from the front-end.
  299. *
  300. * Hooks into the "get_the_terms" filter.
  301. *
  302. * @static
  303. * @access public
  304. * @since Twenty Fourteen 1.0
  305. *
  306. * @param array $terms A list of term objects. This is the return value of get_the_terms().
  307. * @param int $id The ID field for the post object that terms are associated with.
  308. * @param array $taxonomy An array of taxonomy slugs.
  309. * @return array Filtered array of terms.
  310. *
  311. * @uses Featured_Content::get_setting()
  312. */
  313. public static function hide_the_featured_term( $terms, $id, $taxonomy ) {
  314. // This filter is only appropriate on the front-end.
  315. if ( is_admin() ) {
  316. return $terms;
  317. }
  318. // Make sure we are in the correct taxonomy.
  319. if ( 'post_tag' != $taxonomy ) {
  320. return $terms;
  321. }
  322. // No terms? Return early!
  323. if ( empty( $terms ) ) {
  324. return $terms;
  325. }
  326. $settings = self::get_setting();
  327. foreach( $terms as $order => $term ) {
  328. if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
  329. unset( $terms[ $term->term_id ] );
  330. }
  331. }
  332. return $terms;
  333. }
  334. /**
  335. * Register custom setting on the Settings -> Reading screen.
  336. *
  337. * @static
  338. * @access public
  339. * @since Twenty Fourteen 1.0
  340. */
  341. public static function register_setting() {
  342. register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
  343. }
  344. /**
  345. * Add settings to the Customizer.
  346. *
  347. * @static
  348. * @access public
  349. * @since Twenty Fourteen 1.0
  350. *
  351. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  352. */
  353. public static function customize_register( $wp_customize ) {
  354. $wp_customize->add_section( 'featured_content', array(
  355. 'title' => __( 'Featured Content', 'twentyfourteen' ),
  356. 'description' => sprintf( __( 'Use a <a href="%1$s">tag</a> to feature your posts. If no posts match the tag, <a href="%2$s">sticky posts</a> will be displayed instead.', 'twentyfourteen' ),
  357. esc_url( add_query_arg( 'tag', _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ), admin_url( 'edit.php' ) ) ),
  358. admin_url( 'edit.php?show_sticky=1' )
  359. ),
  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' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
  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. 'tag-id' => 0,
  421. 'tag-name' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
  422. );
  423. $options = wp_parse_args( $saved, $defaults );
  424. $options = array_intersect_key( $options, $defaults );
  425. if ( 'all' != $key ) {
  426. return isset( $options[ $key ] ) ? $options[ $key ] : false;
  427. }
  428. return $options;
  429. }
  430. /**
  431. * Validate featured content settings.
  432. *
  433. * Make sure that all user supplied content is in an expected
  434. * format before saving to the database. This function will also
  435. * delete the transient set in Featured_Content::get_featured_content().
  436. *
  437. * @static
  438. * @access public
  439. * @since Twenty Fourteen 1.0
  440. *
  441. * @param array $input Array of settings input.
  442. * @return array Validated settings output.
  443. */
  444. public static function validate_settings( $input ) {
  445. $output = array();
  446. if ( empty( $input['tag-name'] ) ) {
  447. $output['tag-id'] = 0;
  448. } else {
  449. $term = get_term_by( 'name', $input['tag-name'], 'post_tag' );
  450. if ( $term ) {
  451. $output['tag-id'] = $term->term_id;
  452. } else {
  453. $new_tag = wp_create_tag( $input['tag-name'] );
  454. if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) {
  455. $output['tag-id'] = $new_tag['term_id'];
  456. }
  457. }
  458. $output['tag-name'] = $input['tag-name'];
  459. }
  460. $output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0;
  461. // Delete the featured post ids transient.
  462. self::delete_transient();
  463. return $output;
  464. }
  465. } // Featured_Content
  466. Featured_Content::setup();