PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/twentyfourteen/inc/featured-content.php

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