bp_dtheme_ajax_querystring

This function looks scarier than it actually is.

Description

(string) bp_dtheme_ajax_querystring( $query_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 activity/members/groups/blogs/forums loops

Parameters (2)

0. $query_string
The query string.
1. $object
The object.

Usage

  1. if ( !function_exists( 'bp_dtheme_ajax_querystring' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-themes/bp-default/_inc/ajax.php'; 
  3.  
  4. // The query string. 
  5. $query_string = null; 
  6.  
  7. // The object. 
  8. $object = null; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = bp_dtheme_ajax_querystring($query_string, $object); 
  12.  

Defined (1)

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

/bp-themes/bp-default/_inc/ajax.php  
  1. function bp_dtheme_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 default params passed to the 
  15. * 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. $qs[] = 'action=' . $_BP_COOKIE['bp-' . $object . '-filter']; 
  22.  
  23. if ( ! empty( $_BP_COOKIE['bp-' . $object . '-scope'] ) ) { 
  24. if ( 'personal' == $_BP_COOKIE['bp-' . $object . '-scope'] ) { 
  25. $qs[] = 'user_id=' . $user_id; 
  26.  
  27. // Activity stream scope only on activity directory. 
  28. if ( 'all' != $_BP_COOKIE['bp-' . $object . '-scope'] && ! bp_displayed_user_id() && ! bp_is_single_item() ) 
  29. $qs[] = 'scope=' . $_BP_COOKIE['bp-' . $object . '-scope']; 
  30.  
  31. // If page and search_terms have been passed via the AJAX post request, use those. 
  32. if ( ! empty( $_POST['page'] ) && '-1' != $_POST['page'] ) 
  33. $qs[] = 'page=' . absint( $_POST['page'] ); 
  34.  
  35. // exludes activity just posted and avoids duplicate ids 
  36. if ( ! empty( $_POST['exclude_just_posted'] ) ) { 
  37. $just_posted = wp_parse_id_list( $_POST['exclude_just_posted'] ); 
  38. $qs[] = 'exclude=' . implode( ', ', $just_posted ); 
  39.  
  40. $object_search_text = bp_get_search_default_text( $object ); 
  41. if ( ! empty( $_POST['search_terms'] ) && $object_search_text != $_POST['search_terms'] && 'false' != $_POST['search_terms'] && 'undefined' != $_POST['search_terms'] ) 
  42. $qs[] = 'search_terms=' . $_POST['search_terms']; 
  43.  
  44. // Now pass the querystring to override default values. 
  45. $query_string = empty( $qs ) ? '' : join( '&', (array) $qs ); 
  46.  
  47. $object_filter = ''; 
  48. if ( isset( $_BP_COOKIE['bp-' . $object . '-filter'] ) ) 
  49. $object_filter = $_BP_COOKIE['bp-' . $object . '-filter']; 
  50.  
  51. $object_scope = ''; 
  52. if ( isset( $_BP_COOKIE['bp-' . $object . '-scope'] ) ) 
  53. $object_scope = $_BP_COOKIE['bp-' . $object . '-scope']; 
  54.  
  55. $object_page = ''; 
  56. if ( isset( $_BP_COOKIE['bp-' . $object . '-page'] ) ) 
  57. $object_page = $_BP_COOKIE['bp-' . $object . '-page']; 
  58.  
  59. $object_search_terms = ''; 
  60. if ( isset( $_BP_COOKIE['bp-' . $object . '-search-terms'] ) ) 
  61. $object_search_terms = $_BP_COOKIE['bp-' . $object . '-search-terms']; 
  62.  
  63. $object_extras = ''; 
  64. if ( isset( $_BP_COOKIE['bp-' . $object . '-extras'] ) ) 
  65. $object_extras = $_BP_COOKIE['bp-' . $object . '-extras']; 
  66.  
  67. return apply_filters( 'bp_dtheme_ajax_querystring', $query_string, $object, $object_filter, $object_scope, $object_page, $object_search_terms, $object_extras );