Skip to:
Content

BuddyPress.org

Ticket #4954: 4954.ray.01b.ajax.patch

File 4954.ray.01b.ajax.patch, 3.1 KB (added by r-a-y, 11 years ago)

Requires 4954.ray.01.patch.

  • bp-core/bp-core-rewrite.php

    class BP_Rewrite { 
    2929         *
    3030         * @var array
    3131         */
    32         public $registered_query_vars = array();
     32        public $registered_query_vars = array( 'bp_component', 'bp_path' );
    3333
    3434        /**
    3535         * Constructor.
    class BP_Rewrite { 
    3838                add_filter( 'query_vars',                array( $this, 'query_vars' ) );
    3939                add_action( 'bp_generate_rewrite_rules', array( $this, 'rewrite_rules' ),      999 );
    4040                add_action( 'bp_parse_query',            array( $this, 'parse_query' ),        2 );
    41                 add_action( 'bp_init',                   array( $this, 'setup_ajax_globals' ), 2  );
     41                add_action( 'wp_loaded',                 array( $this, 'setup_ajax_globals' )  );
    4242                add_action( 'posts_request',             array( $this, 'posts_request' ),      10, 2 );
    4343        }
    4444
    class BP_Rewrite { 
    138138         * function like bp_is_current_action() during AJAX, it will not work.  This
    139139         * has the potential to break some plugins utilizing this behavior.
    140140         *
    141          * Fortunately, our old URI globals function handles AJAX requests just fine,
    142          * so let's bring it off the bench durng AJAX.
     141         * To workaround this, we manually load up the WP query on AJAX here.
    143142         */
    144143        public function setup_ajax_globals() {
    145144                // stop if we're not on an ajax request
    class BP_Rewrite { 
    147146                        return;
    148147                }
    149148
    150                 // sigh... hello old friend, we're stuck with you at least for now.
    151                 bp_core_set_uri_globals();
     149                // BP sends a 'cookie' parameter on AJAX requests
     150                // If this AJAX request doesn't have this parameter, stop now!
     151                if ( empty( $_POST['cookie'] ) ) {
     152                        return;
     153                }
     154
     155                // Fool WP into using the referer for AJAX requests
     156                // This is to pass WP::parse_request()'s stringent requirements
     157                //
     158                // @todo Needs further testing in different environments.
     159                //       Especially PHP_SELF on IIS...
     160                $_SERVER['PHP_SELF']    = 'index.php';
     161                $_SERVER['REQUEST_URI'] = bp_core_referrer();
     162
     163                // manually load our custom 'bp_parse_query' hook
     164                add_action( 'parse_query', array( $this, 'ajax_parse_query' ), 2 );
     165
     166                // load the WP query
     167                wp();
    152168        }
    153169
    154170        /**
    155171         * Add our query vars to WordPress.
    156172         */
    157173        public function query_vars( $vars ) {
    158                 $this->registered_query_vars[] = 'bp_component';
    159                 $this->registered_query_vars[] = 'bp_path';
    160 
    161174                return array_merge( $this->registered_query_vars, $vars );
    162175        }
    163176
    class BP_Rewrite { 
    226239        public static function get_bp_frontpage_component() {
    227240                return self::get_bp_frontpage_component_from_page_id( get_option( 'page_on_front' ) );
    228241        }
     242
     243        /**
     244         * AJAX-capable version of {@link bp_parse_query()}.
     245         *
     246         * bp_parse_query() does not run in the admin area.  When AJAX is called, WP
     247         * assumes we are in the admin area.  As a consequence, our custom
     248         * 'parse_query' actions won't run.
     249         *
     250         * This is a stub method to allow the 'bp_parse_query' hook to run.
     251         *
     252         * @param object $query The current WP_Query instance.
     253         */
     254        public function ajax_parse_query( $posts_query ) {
     255                do_action_ref_array( 'bp_parse_query', array( &$posts_query ) );
     256        }
    229257}