PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/davodey/WordPress
PHP | 541 lines | 226 code | 68 blank | 247 comment | 42 complexity | 28084d823141f87537142dc2b99db321 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  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. // Return array of cached results if they exist.
  140. $featured_ids = get_transient( 'featured_content_ids' );
  141. if ( ! empty( $featured_ids ) ) {
  142. return array_map( 'absint', (array) $featured_ids );
  143. }
  144. $settings = self::get_setting();
  145. // Return sticky post ids if no tag name is set.
  146. $term = get_term_by( 'name', $settings['tag-name'], 'post_tag' );
  147. if ( $term ) {
  148. $tag = $term->term_id;
  149. } else {
  150. return self::get_sticky_posts();
  151. }
  152. // Query for featured posts.
  153. $featured = get_posts( array(
  154. 'numberposts' => self::$max_posts,
  155. 'tax_query' => array(
  156. array(
  157. 'field' => 'term_id',
  158. 'taxonomy' => 'post_tag',
  159. 'terms' => $tag,
  160. ),
  161. ),
  162. ) );
  163. // Return array with sticky posts if no Featured Content exists.
  164. if ( ! $featured ) {
  165. return self::get_sticky_posts();
  166. }
  167. // Ensure correct format before save/return.
  168. $featured_ids = wp_list_pluck( (array) $featured, 'ID' );
  169. $featured_ids = array_map( 'absint', $featured_ids );
  170. set_transient( 'featured_content_ids', $featured_ids );
  171. return $featured_ids;
  172. }
  173. /**
  174. * Return an array with IDs of posts maked as sticky.
  175. *
  176. * @static
  177. * @access public
  178. * @since Twenty Fourteen 1.0
  179. *
  180. * @return array Array of sticky posts.
  181. */
  182. public static function get_sticky_posts() {
  183. $settings = self::get_setting();
  184. return array_slice( get_option( 'sticky_posts', array() ), 0, self::$max_posts );
  185. }
  186. /**
  187. * Delete featured content ids transient.
  188. *
  189. * Hooks in the "save_post" action.
  190. *
  191. * @see Featured_Content::validate_settings().
  192. *
  193. * @static
  194. * @access public
  195. * @since Twenty Fourteen 1.0
  196. */
  197. public static function delete_transient() {
  198. delete_transient( 'featured_content_ids' );
  199. }
  200. /**
  201. * Exclude featured posts from the home page blog query.
  202. *
  203. * Filter the home page posts, and remove any featured post ID's from it.
  204. * Hooked onto the 'pre_get_posts' action, this changes the parameters of
  205. * the query before it gets any posts.
  206. *
  207. * @static
  208. * @access public
  209. * @since Twenty Fourteen 1.0
  210. *
  211. * @param WP_Query $query WP_Query object.
  212. * @return WP_Query Possibly-modified WP_Query.
  213. */
  214. public static function pre_get_posts( $query ) {
  215. // Bail if not home or not main query.
  216. if ( ! $query->is_home() || ! $query->is_main_query() ) {
  217. return;
  218. }
  219. $page_on_front = get_option( 'page_on_front' );
  220. // Bail if the blog page is not the front page.
  221. if ( ! empty( $page_on_front ) ) {
  222. return;
  223. }
  224. $featured = self::get_featured_post_ids();
  225. // Bail if no featured posts.
  226. if ( ! $featured ) {
  227. return;
  228. }
  229. // We need to respect post ids already in the blacklist.
  230. $post__not_in = $query->get( 'post__not_in' );
  231. if ( ! empty( $post__not_in ) ) {
  232. $featured = array_merge( (array) $post__not_in, $featured );
  233. $featured = array_unique( $featured );
  234. }
  235. $query->set( 'post__not_in', $featured );
  236. }
  237. /**
  238. * Reset tag option when the saved tag is deleted.
  239. *
  240. * It's important to mention that the transient needs to be deleted,
  241. * too. While it may not be obvious by looking at the function alone,
  242. * the transient is deleted by Featured_Content::validate_settings().
  243. *
  244. * Hooks in the "delete_post_tag" action.
  245. *
  246. * @see Featured_Content::validate_settings().
  247. *
  248. * @static
  249. * @access public
  250. * @since Twenty Fourteen 1.0
  251. *
  252. * @param int $tag_id The term_id of the tag that has been deleted.
  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, $args ) {
  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. // Bail if term objects are unavailable.
  292. if ( 'all' != $args['fields'] ) {
  293. return $terms;
  294. }
  295. $settings = self::get_setting();
  296. foreach( $terms as $order => $term ) {
  297. if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
  298. unset( $terms[ $order ] );
  299. }
  300. }
  301. return $terms;
  302. }
  303. /**
  304. * Hide featured tag from display when terms associated with a post object
  305. * are queried from the front-end.
  306. *
  307. * Hooks into the "get_the_terms" filter.
  308. *
  309. * @static
  310. * @access public
  311. * @since Twenty Fourteen 1.0
  312. *
  313. * @param array $terms A list of term objects. This is the return value of get_the_terms().
  314. * @param int $id The ID field for the post object that terms are associated with.
  315. * @param array $taxonomy An array of taxonomy slugs.
  316. * @return array Filtered array of terms.
  317. *
  318. * @uses Featured_Content::get_setting()
  319. */
  320. public static function hide_the_featured_term( $terms, $id, $taxonomy ) {
  321. // This filter is only appropriate on the front-end.
  322. if ( is_admin() ) {
  323. return $terms;
  324. }
  325. // Make sure we are in the correct taxonomy.
  326. if ( 'post_tag' != $taxonomy ) {
  327. return $terms;
  328. }
  329. // No terms? Return early!
  330. if ( empty( $terms ) ) {
  331. return $terms;
  332. }
  333. $settings = self::get_setting();
  334. foreach( $terms as $order => $term ) {
  335. if ( ( $settings['tag-id'] === $term->term_id || $settings['tag-name'] === $term->name ) && 'post_tag' === $term->taxonomy ) {
  336. unset( $terms[ $term->term_id ] );
  337. }
  338. }
  339. return $terms;
  340. }
  341. /**
  342. * Register custom setting on the Settings -> Reading screen.
  343. *
  344. * @static
  345. * @access public
  346. * @since Twenty Fourteen 1.0
  347. */
  348. public static function register_setting() {
  349. register_setting( 'featured-content', 'featured-content', array( __CLASS__, 'validate_settings' ) );
  350. }
  351. /**
  352. * Add settings to the Customizer.
  353. *
  354. * @static
  355. * @access public
  356. * @since Twenty Fourteen 1.0
  357. *
  358. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  359. */
  360. public static function customize_register( $wp_customize ) {
  361. $wp_customize->add_section( 'featured_content', array(
  362. 'title' => __( 'Featured Content', 'twentyfourteen' ),
  363. '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' ),
  364. esc_url( add_query_arg( 'tag', _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ), admin_url( 'edit.php' ) ) ),
  365. admin_url( 'edit.php?show_sticky=1' )
  366. ),
  367. 'priority' => 130,
  368. 'theme_supports' => 'featured-content',
  369. ) );
  370. // Add Featured Content settings.
  371. $wp_customize->add_setting( 'featured-content[tag-name]', array(
  372. 'default' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
  373. 'type' => 'option',
  374. 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
  375. ) );
  376. $wp_customize->add_setting( 'featured-content[hide-tag]', array(
  377. 'default' => true,
  378. 'type' => 'option',
  379. 'sanitize_js_callback' => array( __CLASS__, 'delete_transient' ),
  380. ) );
  381. // Add Featured Content controls.
  382. $wp_customize->add_control( 'featured-content[tag-name]', array(
  383. 'label' => __( 'Tag Name', 'twentyfourteen' ),
  384. 'section' => 'featured_content',
  385. 'priority' => 20,
  386. ) );
  387. $wp_customize->add_control( 'featured-content[hide-tag]', array(
  388. 'label' => __( 'Don&rsquo;t display tag on front end.', 'twentyfourteen' ),
  389. 'section' => 'featured_content',
  390. 'type' => 'checkbox',
  391. 'priority' => 30,
  392. ) );
  393. }
  394. /**
  395. * Enqueue the tag suggestion script.
  396. *
  397. * @static
  398. * @access public
  399. * @since Twenty Fourteen 1.0
  400. */
  401. public static function enqueue_scripts() {
  402. wp_enqueue_script( 'featured-content-suggest', get_template_directory_uri() . '/js/featured-content-admin.js', array( 'jquery', 'suggest' ), '20131022', true );
  403. }
  404. /**
  405. * Get featured content settings.
  406. *
  407. * Get all settings recognized by this module. This function
  408. * will return all settings whether or not they have been stored
  409. * in the database yet. This ensures that all keys are available
  410. * at all times.
  411. *
  412. * In the event that you only require one setting, you may pass
  413. * its name as the first parameter to the function and only that
  414. * value will be returned.
  415. *
  416. * @static
  417. * @access public
  418. * @since Twenty Fourteen 1.0
  419. *
  420. * @param string $key The key of a recognized setting.
  421. * @return mixed Array of all settings by default. A single value if passed as first parameter.
  422. */
  423. public static function get_setting( $key = 'all' ) {
  424. $saved = (array) get_option( 'featured-content' );
  425. $defaults = array(
  426. 'hide-tag' => 1,
  427. 'tag-id' => 0,
  428. 'tag-name' => _x( 'featured', 'featured content default tag slug', 'twentyfourteen' ),
  429. );
  430. $options = wp_parse_args( $saved, $defaults );
  431. $options = array_intersect_key( $options, $defaults );
  432. if ( 'all' != $key ) {
  433. return isset( $options[ $key ] ) ? $options[ $key ] : false;
  434. }
  435. return $options;
  436. }
  437. /**
  438. * Validate featured content settings.
  439. *
  440. * Make sure that all user supplied content is in an expected
  441. * format before saving to the database. This function will also
  442. * delete the transient set in Featured_Content::get_featured_content().
  443. *
  444. * @static
  445. * @access public
  446. * @since Twenty Fourteen 1.0
  447. *
  448. * @param array $input Array of settings input.
  449. * @return array Validated settings output.
  450. */
  451. public static function validate_settings( $input ) {
  452. $output = array();
  453. if ( empty( $input['tag-name'] ) ) {
  454. $output['tag-id'] = 0;
  455. } else {
  456. $term = get_term_by( 'name', $input['tag-name'], 'post_tag' );
  457. if ( $term ) {
  458. $output['tag-id'] = $term->term_id;
  459. } else {
  460. $new_tag = wp_create_tag( $input['tag-name'] );
  461. if ( ! is_wp_error( $new_tag ) && isset( $new_tag['term_id'] ) ) {
  462. $output['tag-id'] = $new_tag['term_id'];
  463. }
  464. }
  465. $output['tag-name'] = $input['tag-name'];
  466. }
  467. $output['hide-tag'] = isset( $input['hide-tag'] ) && $input['hide-tag'] ? 1 : 0;
  468. // Delete the featured post ids transient.
  469. self::delete_transient();
  470. return $output;
  471. }
  472. } // Featured_Content
  473. Featured_Content::setup();