Skip to:
Content

bbPress.org

Changeset 7979


Ignore:
Timestamp:
10/12/2026 12:30:52 AM (5 weeks ago)
Author:
johnjamesjacoby
Message:

AJAX: Use WordPress termination handlers.

Replace direct die() calls in the administration suggestion callbacks and common AJAX response helper with wp_die(). This preserves the existing response bodies while allowing custom AJAX death handlers to run.

Add regression coverage for the suggestion and JSON responses.

In trunk, for 2.7.

Props immeet94.
See #3363.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/admin/classes/class-bbp-admin.php

    r7957 r7979  
    11101110                        }
    11111111                }
    1112                 die();
     1112                wp_die();
    11131113        }
    11141114
    … …  
    11971197                        }
    11981198                }
    1199                 die();
     1199                wp_die();
    12001200        }
    12011201
  • trunk/src/includes/common/ajax.php

    r7957 r7979  
    165165        header( 'Content-type: application/json' );
    166166        echo json_encode( $response );
    167         die();
     167        wp_die();
    168168}
  • trunk/tests/phpunit/testcases/admin/admin.php

    r7951 r7979  
    117117                        if ( ! is_null( $expected_message ) ) {
    118118                                $this->assertSame( $expected_message, $error->getMessage() );
     119                        }
     120                }
     121        }
     122
     123        private function capture_wp_die_output( $callback ) {
     124                $level = ob_get_level();
     125                ob_start();
     126
     127                try {
     128                        call_user_func( $callback );
     129                        $this->fail( 'Expected the request to terminate.' );
     130                } catch ( WPDieException $error ) {
     131                        $output = ob_get_clean();
     132                        $this->assertSame( '', $error->getMessage() );
     133
     134                        return $output;
     135                } finally {
     136                        while ( ob_get_level() > $level ) {
     137                                ob_end_clean();
    119138                        }
    120139                }
    … …  
    709728
    710729        /**
     730         * @covers BBP_Admin::suggest_topic
     731         * @covers BBP_Admin::suggest_user
     732         * @ticket 3363
     733         */
     734        public function test_ajax_suggestions_use_ajax_die_handler_after_output() {
     735                $admin = bbp_admin();
     736                $handler = function () {
     737                        return function ( $message ) {
     738                                throw new WPDieException( (string) $message );
     739                        };
     740                };
     741                add_filter( 'wp_doing_ajax', '__return_true' );
     742                add_filter( 'wp_die_ajax_handler', $handler );
     743
     744                try {
     745                        $this->create_administrator();
     746                        bbp_set_user_role( bbp_get_current_user_id(), bbp_get_keymaster_role() );
     747
     748                        $forum_id = $this->factory->forum->create();
     749                        $topic_id = $this->factory->topic->create(
     750                                array(
     751                                        'post_parent' => $forum_id,
     752                                        'post_title'  => 'Needle Topic',
     753                                )
     754                        );
     755                        $user_id = $this->factory->user->create(
     756                                array(
     757                                        'user_login'    => 'needle-user',
     758                                        'user_nicename' => 'needle-user',
     759                                )
     760                        );
     761
     762                        $_REQUEST = array(
     763                                'q'           => 'Needle',
     764                                '_ajax_nonce' => wp_create_nonce( 'bbp_suggest_topic_nonce' ),
     765                        );
     766                        $this->assertSame(
     767                                $topic_id . " - Needle Topic\n",
     768                                $this->capture_wp_die_output( array( $admin, 'suggest_topic' ) )
     769                        );
     770
     771                        $_REQUEST = array(
     772                                'q'           => 'needle',
     773                                '_ajax_nonce' => wp_create_nonce( 'bbp_suggest_user_nonce' ),
     774                        );
     775                        $this->assertSame(
     776                                $user_id . " - needle-user\n",
     777                                $this->capture_wp_die_output( array( $admin, 'suggest_user' ) )
     778                        );
     779                } finally {
     780                        remove_filter( 'wp_doing_ajax', '__return_true' );
     781                        remove_filter( 'wp_die_ajax_handler', $handler );
     782                }
     783        }
     784
     785        /**
    711786         * @covers BBP_Admin::update_screen
    712787         * @covers BBP_Admin::network_update_screen
  • trunk/tests/phpunit/testcases/common/ajax.php

    r7928 r7979  
    331331        }
    332332
     333        /**
     334         * @covers ::bbp_ajax_response
     335         * @ticket 3363
     336         */
     337        public function test_ajax_response_uses_ajax_die_handler_after_output() {
     338                $handler = $this->get_die_handler_filter();
     339                add_filter( 'wp_doing_ajax', '__return_true' );
     340                add_filter( 'wp_die_ajax_handler', $handler );
     341
     342                $level = ob_get_level();
     343                ob_start();
     344
     345                try {
     346                        $this->without_header_warnings(
     347                                function() {
     348                                        bbp_ajax_response( true, 'Content', -1, array( 'extra' => 'value' ) );
     349                                }
     350                        );
     351                        $this->fail( 'Expected the AJAX response to terminate.' );
     352                } catch ( BBP_Tests_Common_Ajax_Die_Exception $error ) {
     353                        $output = ob_get_clean();
     354                        $this->assertSame( '', $error->getMessage() );
     355                        $this->assertSame(
     356                                '{"success":true,"status":200,"content":"Content","extra":"value"}',
     357                                $output
     358                        );
     359                } finally {
     360                        while ( ob_get_level() > $level ) {
     361                                ob_end_clean();
     362                        }
     363
     364                        remove_filter( 'wp_doing_ajax', '__return_true' );
     365                        remove_filter( 'wp_die_ajax_handler', $handler );
     366                }
     367        }
     368
    333369        private function get_die_handler_filter() {
    334370                return function() {
Note: See TracChangeset for help on using the changeset viewer.