/includes/common/ajax.php

  1. <?php 
  2.  
  3. /** 
  4. * bbPress Common AJAX Functions 
  5. * 
  6. * Common AJAX functions are ones that are used to setup and/or use during 
  7. * bbPress specific, theme-side AJAX requests. 
  8. * 
  9. * @package bbPress 
  10. * @subpackage Ajax 
  11. */ 
  12.  
  13. // Exit if accessed directly 
  14. if ( !defined( 'ABSPATH' ) ) exit; 
  15.  
  16. /** 
  17. * Output the URL to use for theme-side bbPress AJAX requests 
  18. * 
  19. * @since bbPress (r4543) 
  20. * 
  21. * @uses bbp_get_ajax_url() To get the URL to use for AJAX requests 
  22. */ 
  23. function bbp_ajax_url() { 
  24. echo esc_url( bbp_get_ajax_url() ); 
  25. /** 
  26. * Return the URL to use for theme-side bbPress AJAX requests 
  27. * 
  28. * @since bbPress (r4543) 
  29. * 
  30. * @global WP $wp 
  31. * @return string 
  32. */ 
  33. function bbp_get_ajax_url() { 
  34. global $wp; 
  35.  
  36. $base_url = home_url( trailingslashit( $wp->request ), ( is_ssl() ? 'https' : 'http' ) ); 
  37. $ajaxurl = add_query_arg( array( 'bbp-ajax' => 'true' ), $base_url ); 
  38.  
  39. return apply_filters( 'bbp_get_ajax_url', $ajaxurl ); 
  40.  
  41. /** 
  42. * Is this a bbPress AJAX request? 
  43. * 
  44. * @since bbPress (r4543) 
  45. * 
  46. * @return bool Looking for bbp-ajax 
  47. */ 
  48. function bbp_is_ajax() { 
  49. return (bool) ( ( isset( $_GET['bbp-ajax'] ) || isset( $_POST['bbp-ajax'] ) ) && ! empty( $_REQUEST['action'] ) ); 
  50.  
  51. /** 
  52. * Hooked to the 'bbp_template_redirect' action, this is bbPress's custom 
  53. * theme-side ajax handler. 
  54. * 
  55. * @since bbPress (r4543) 
  56. * 
  57. * @return If not a bbPress ajax request 
  58. */ 
  59. function bbp_do_ajax() { 
  60.  
  61. // Bail if not an ajax request 
  62. if ( ! bbp_is_ajax() ) 
  63. return; 
  64.  
  65. // Set WordPress core ajax constant 
  66. define( 'DOING_AJAX', true ); 
  67.  
  68. // Set the header content type 
  69. @header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) ); 
  70.  
  71. // Disable content sniffing in browsers that support it 
  72. send_nosniff_header(); 
  73.  
  74. // Perform custom bbPress ajax 
  75. do_action( 'bbp_ajax_' . $_REQUEST['action'] ); 
  76.  
  77. // All done 
  78. die( '0' ); 
  79.  
  80. /** 
  81. * Helper method to return JSON response for the ajax calls 
  82. * 
  83. * @since bbPress (r4542) 
  84. * 
  85. * @param bool $success 
  86. * @param string $content 
  87. * @param array $extras 
  88. */ 
  89. function bbp_ajax_response( $success = false, $content = '', $status = -1, $extras = array() ) { 
  90.  
  91. // Set status to 200 if setting response as successful 
  92. if ( ( true === $success ) && ( -1 === $status ) ) 
  93. $status = 200; 
  94.  
  95. // Setup the response array 
  96. $response = array( 
  97. 'success' => $success,  
  98. 'status' => $status,  
  99. 'content' => $content 
  100. ); 
  101.  
  102. // Merge extra response parameters in 
  103. if ( !empty( $extras ) && is_array( $extras ) ) { 
  104. $response = array_merge( $response, $extras ); 
  105.  
  106. // Send back the JSON 
  107. @header( 'Content-type: application/json' ); 
  108. echo json_encode( $response ); 
  109. die(); 
.