bp_legacy_theme_ajax_querystring

This function looks scarier than it actually is.

Description

(string) bp_legacy_theme_ajax_querystring( (string) $query_string, (string) $object ); 

:) Each object loop (activity/members/groups/blogs/forums) contains default parameters to show specific information based on the page we are currently looking at.

The following function will take into account any cookies set in the JS and allow us to override the parameters sent. That way we can change the results returned without reloading the page.

By using cookies we can also make sure that user settings are retained across page loads.

Returns (string)

Query string for the component loops

Parameters (2)

0. $query_string (string)
Query string for the current request.
1. $object (string)
Object for cookie.

Usage

  1. if ( !function_exists( 'bp_legacy_theme_ajax_querystring' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-templates/bp-legacy/buddypress-functions.php'; 
  3.  
  4. // Query string for the current request. 
  5. $query_string = ''; 
  6.  
  7. // Object for cookie. 
  8. $object = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_legacy_theme_ajax_querystring($query_string, $object); 
  12.  

Defined (1)

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

/bp-templates/bp-legacy/buddypress-functions.php  
  1. function bp_legacy_theme_ajax_querystring( $query_string, $object ) { 
  2. if ( empty( $object ) ) 
  3. return ''; 
  4.  
  5. // Set up the cookies passed on this AJAX request. Store a local var to avoid conflicts. 
  6. if ( ! empty( $_POST['cookie'] ) ) { 
  7. $_BP_COOKIE = wp_parse_args( str_replace( '; ', '&', urldecode( $_POST['cookie'] ) ) ); 
  8. } else { 
  9. $_BP_COOKIE = &$_COOKIE; 
  10.  
  11. $qs = array(); 
  12.  
  13. /** 
  14. * Check if any cookie values are set. If there are then override the 
  15. * default params passed to the template loop. 
  16. */ 
  17.  
  18. // Activity stream filtering on action. 
  19. if ( ! empty( $_BP_COOKIE['bp-' . $object . '-filter'] ) && '-1' != $_BP_COOKIE['bp-' . $object . '-filter'] ) { 
  20. $qs[] = 'type=' . $_BP_COOKIE['bp-' . $object . '-filter']; 
  21.  
  22. if ( bp_is_active( 'activity' ) ) { 
  23. foreach ( $actions as $action ) { 
  24. if ( $action['key'] === $_BP_COOKIE['bp-' . $object . '-filter'] ) { 
  25. $qs[] = 'action=' . $_BP_COOKIE['bp-' . $object . '-filter']; 
  26.  
  27. if ( ! empty( $_BP_COOKIE['bp-' . $object . '-scope'] ) ) { 
  28. if ( 'personal' == $_BP_COOKIE['bp-' . $object . '-scope'] ) { 
  29. $qs[] = 'user_id=' . $user_id; 
  30.  
  31. // Activity stream scope only on activity directory. 
  32. if ( 'all' != $_BP_COOKIE['bp-' . $object . '-scope'] && ! bp_displayed_user_id() && ! bp_is_single_item() ) 
  33. $qs[] = 'scope=' . $_BP_COOKIE['bp-' . $object . '-scope']; 
  34.  
  35. // If page and search_terms have been passed via the AJAX post request, use those. 
  36. if ( ! empty( $_POST['page'] ) && '-1' != $_POST['page'] ) 
  37. $qs[] = 'page=' . absint( $_POST['page'] ); 
  38.  
  39. // Excludes activity just posted and avoids duplicate ids. 
  40. if ( ! empty( $_POST['exclude_just_posted'] ) ) { 
  41. $just_posted = wp_parse_id_list( $_POST['exclude_just_posted'] ); 
  42. $qs[] = 'exclude=' . implode( ', ', $just_posted ); 
  43.  
  44. // To get newest activities. 
  45. if ( ! empty( $_POST['offset'] ) ) { 
  46. $qs[] = 'offset=' . intval( $_POST['offset'] ); 
  47.  
  48. $object_search_text = bp_get_search_default_text( $object ); 
  49. if ( ! empty( $_POST['search_terms'] ) && $object_search_text != $_POST['search_terms'] && 'false' != $_POST['search_terms'] && 'undefined' != $_POST['search_terms'] ) 
  50. $qs[] = 'search_terms=' . urlencode( $_POST['search_terms'] ); 
  51.  
  52. // Now pass the querystring to override default values. 
  53. $query_string = empty( $qs ) ? '' : join( '&', (array) $qs ); 
  54.  
  55. $object_filter = ''; 
  56. if ( isset( $_BP_COOKIE['bp-' . $object . '-filter'] ) ) 
  57. $object_filter = $_BP_COOKIE['bp-' . $object . '-filter']; 
  58.  
  59. $object_scope = ''; 
  60. if ( isset( $_BP_COOKIE['bp-' . $object . '-scope'] ) ) 
  61. $object_scope = $_BP_COOKIE['bp-' . $object . '-scope']; 
  62.  
  63. $object_page = ''; 
  64. if ( isset( $_BP_COOKIE['bp-' . $object . '-page'] ) ) 
  65. $object_page = $_BP_COOKIE['bp-' . $object . '-page']; 
  66.  
  67. $object_search_terms = ''; 
  68. if ( isset( $_BP_COOKIE['bp-' . $object . '-search-terms'] ) ) 
  69. $object_search_terms = $_BP_COOKIE['bp-' . $object . '-search-terms']; 
  70.  
  71. $object_extras = ''; 
  72. if ( isset( $_BP_COOKIE['bp-' . $object . '-extras'] ) ) 
  73. $object_extras = $_BP_COOKIE['bp-' . $object . '-extras']; 
  74.  
  75. /** 
  76. * Filters the AJAX query string for the component loops. 
  77. * @since 1.7.0 
  78. * @param string $query_string The query string we are working with. 
  79. * @param string $object The type of page we are on. 
  80. * @param string $object_filter The current object filter. 
  81. * @param string $object_scope The current object scope. 
  82. * @param string $object_page The current object page. 
  83. * @param string $object_search_terms The current object search terms. 
  84. * @param string $object_extras The current object extras. 
  85. */ 
  86. return apply_filters( 'bp_legacy_theme_ajax_querystring', $query_string, $object, $object_filter, $object_scope, $object_page, $object_search_terms, $object_extras );