[ Index ] |
PHP Cross Reference of BBPress |
[Summary view] [Print] [Text view]
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 defined( 'ABSPATH' ) || exit; 15 16 /** 17 * Output the URL to use for theme-side bbPress AJAX requests 18 * 19 * @since 2.3.0 bbPress (r4543) 20 */ 21 function bbp_ajax_url() { 22 echo esc_url( bbp_get_ajax_url() ); 23 } 24 /** 25 * Return the URL to use for theme-side bbPress AJAX requests 26 * 27 * @since 2.3.0 bbPress (r4543) 28 * 29 * @global WP $wp 30 * @return string 31 */ 32 function bbp_get_ajax_url() { 33 global $wp; 34 35 $ssl = bbp_get_url_scheme(); 36 $url = trailingslashit( $wp->request ); 37 $base_url = home_url( $url, $ssl ); 38 $ajaxurl = add_query_arg( array( 'bbp-ajax' => 'true' ), $base_url ); 39 40 // Filter & return 41 return apply_filters( 'bbp_get_ajax_url', $ajaxurl ); 42 } 43 44 /** 45 * Is this a bbPress AJAX request? 46 * 47 * @since 2.3.0 bbPress (r4543) 48 * 49 * @return bool Looking for bbp-ajax 50 */ 51 function bbp_is_ajax() { 52 return (bool) ( ( isset( $_GET['bbp-ajax'] ) || isset( $_POST['bbp-ajax'] ) ) && ! empty( $_REQUEST['action'] ) ); 53 } 54 55 /** 56 * Hooked to the 'bbp_template_redirect' action, this is also the custom 57 * theme-side AJAX handler. 58 * 59 * This is largely taken from admin-ajax.php, but adapted specifically for 60 * theme-side bbPress-only AJAX requests. 61 * 62 * @since 2.3.0 bbPress (r4543) 63 * 64 * @param string $action Sanitized action from bbp_post_request/bbp_get_request 65 * 66 * @return If not a bbPress AJAX request 67 */ 68 function bbp_do_ajax( $action = '' ) { 69 70 // Bail if not a bbPress specific AJAX request 71 if ( ! bbp_is_ajax() ) { 72 return; 73 } 74 75 // Set WordPress core AJAX constant for back-compat 76 if ( ! defined( 'DOING_AJAX' ) ) { 77 define( 'DOING_AJAX', true ); 78 } 79 80 // Setup AJAX headers 81 bbp_ajax_headers(); 82 83 // Compat for targeted action hooks (without $action param) 84 $action = empty( $action ) 85 ? sanitize_key( $_REQUEST['action'] ) // isset checked by bbp_is_ajax() 86 : $action; 87 88 // Setup action key 89 $key = "bbp_ajax_{$action}"; 90 91 // Bail if no action is registered 92 if ( empty( $action ) || ! has_action( $key ) ) { 93 wp_die( '0', 400 ); 94 } 95 96 // Everything is 200 OK. 97 bbp_set_200(); 98 99 // Execute custom bbPress AJAX action 100 do_action( $key ); 101 102 // All done 103 wp_die( '0' ); 104 } 105 106 /** 107 * Send headers for AJAX specific requests 108 * 109 * This was abstracted from bbp_do_ajax() for use in custom theme-side AJAX 110 * implementations. 111 * 112 * @since 2.6.0 bbPress (r6757) 113 */ 114 function bbp_ajax_headers() { 115 116 // Set the header content type 117 @header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) ); 118 @header( 'X-Robots-Tag: noindex' ); 119 120 // Disable content sniffing in browsers that support it 121 send_nosniff_header(); 122 123 // Disable browser caching for all AJAX requests 124 nocache_headers(); 125 } 126 127 /** 128 * Helper method to return JSON response for bbPress AJAX calls 129 * 130 * @since 2.3.0 bbPress (r4542) 131 * 132 * @param bool $success 133 * @param string $content 134 * @param array $extras 135 */ 136 function bbp_ajax_response( $success = false, $content = '', $status = -1, $extras = array() ) { 137 138 // Set status to 200 if setting response as successful 139 if ( ( true === $success ) && ( -1 === $status ) ) { 140 $status = 200; 141 } 142 143 // Setup the response array 144 $response = array( 145 'success' => $success, 146 'status' => $status, 147 'content' => $content 148 ); 149 150 // Merge extra response parameters in 151 if ( ! empty( $extras ) && is_array( $extras ) ) { 152 $response = array_merge( $response, $extras ); 153 } 154 155 // Send back the JSON 156 @header( 'Content-type: application/json' ); 157 echo json_encode( $response ); 158 die(); 159 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sat Dec 21 01:00:52 2024 | Cross-referenced by PHPXref 0.7.1 |