bbp_pre_get_posts_normalize_forum_visibility

Adjusts forum, topic, and reply queries to exclude items that might be contained inside hidden or private forums that the user does not have the capability to view.

Description

(WP_Query) bbp_pre_get_posts_normalize_forum_visibility( (null) $posts_query = null ); 

Doing it with an action allows us to trap all WP_Query's rather than needing to hardcode this logic into each query. It also protects forum content for plugins that might be doing their own queries.

Returns (WP_Query)

Parameters (1)

0. $posts_query — Optional. (null) => null
The posts query.

Usage

  1. if ( !function_exists( 'bbp_pre_get_posts_normalize_forum_visibility' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'bbpress/includes/forums/functions.php'; 
  3.  
  4. // The posts query. 
  5. $posts_query = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = bbp_pre_get_posts_normalize_forum_visibility($posts_query); 
  9.  

Defined (1)

The function is defined in the following location(s).

/includes/forums/functions.php  
  1. function bbp_pre_get_posts_normalize_forum_visibility( $posts_query = null ) { 
  2.  
  3. // Bail if all forums are explicitly allowed 
  4. if ( true === apply_filters( 'bbp_include_all_forums', false, $posts_query ) ) { 
  5. return; 
  6.  
  7. // Bail if $posts_query is not an object or of incorrect class 
  8. if ( !is_object( $posts_query ) || !is_a( $posts_query, 'WP_Query' ) ) { 
  9. return; 
  10.  
  11. // Get query post types array . 
  12. $post_types = (array) $posts_query->get( 'post_type' ); 
  13.  
  14. // Forums 
  15. if ( bbp_get_forum_post_type() === implode( '', $post_types ) ) { 
  16.  
  17. // Prevent accidental wp-admin post_row override 
  18. if ( is_admin() && isset( $_REQUEST['post_status'] ) ) { 
  19. return; 
  20.  
  21. /** Default ***********************************************************/ 
  22.  
  23. // Get any existing post status 
  24. $post_stati = $posts_query->get( 'post_status' ); 
  25.  
  26. // Default to public status 
  27. if ( empty( $post_stati ) ) { 
  28. $post_stati[] = bbp_get_public_status_id(); 
  29.  
  30. // Split the status string 
  31. } elseif ( is_string( $post_stati ) ) { 
  32. $post_stati = explode( ', ', $post_stati ); 
  33.  
  34. /** Private ***********************************************************/ 
  35.  
  36. // Remove bbp_get_private_status_id() if user is not capable 
  37. if ( ! current_user_can( 'read_private_forums' ) ) { 
  38. $key = array_search( bbp_get_private_status_id(), $post_stati ); 
  39. if ( !empty( $key ) ) { 
  40. unset( $post_stati[$key] ); 
  41.  
  42. // ...or add it if they are 
  43. } else { 
  44. $post_stati[] = bbp_get_private_status_id(); 
  45.  
  46. /** Hidden ************************************************************/ 
  47.  
  48. // Remove bbp_get_hidden_status_id() if user is not capable 
  49. if ( ! current_user_can( 'read_hidden_forums' ) ) { 
  50. $key = array_search( bbp_get_hidden_status_id(), $post_stati ); 
  51. if ( !empty( $key ) ) { 
  52. unset( $post_stati[$key] ); 
  53.  
  54. // ...or add it if they are 
  55. } else { 
  56. $post_stati[] = bbp_get_hidden_status_id(); 
  57.  
  58. // Add the statuses 
  59. $posts_query->set( 'post_status', array_unique( array_filter( $post_stati ) ) ); 
  60.  
  61. // Topics Or Replies 
  62. if ( array_intersect( array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ), $post_types ) ) { 
  63.  
  64. // Get forums to exclude 
  65. $forum_ids = bbp_exclude_forum_ids( 'meta_query' ); 
  66.  
  67. // Bail if no forums to exclude 
  68. if ( ! array_filter( $forum_ids ) ) { 
  69. return; 
  70.  
  71. // Get any existing meta queries 
  72. $meta_query = $posts_query->get( 'meta_query' ); 
  73.  
  74. // Add our meta query to existing 
  75. $meta_query[] = $forum_ids; 
  76.  
  77. // Set the meta_query var 
  78. $posts_query->set( 'meta_query', $meta_query );