Skip to:
Content

bbPress.org


Ignore:
Timestamp:
10/01/2026 04:50:02 AM (7 weeks ago)
Author:
johnjamesjacoby
Message:

Blocks: Avoid unused editor queries.

Skip localization when the receiving script is unavailable, preventing front-end editor integrations from triggering expensive choice queries on unrelated logged-in requests. Simplify topic-tag visibility checks by requiring the canonical post parent and stored forum ID to agree, removing redundant forum lists and a correlated anti-join.

In trunk, for 2.7.

Props ocean90.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/common/block-visibility.php

    r7708 r7763  
    99 */
    1010class BBP_Tests_Common_Block_Visibility extends BBP_UnitTestCase {
     11
     12        /**
     13         * @covers ::BBP_Blocks::enqueue_block_editor_assets
     14         */
     15        public function test_editor_choices_only_load_for_registered_admin_script() {
     16                $forum_id = $this->factory->forum->create();
     17                $this->factory->topic->create( array( 'post_parent' => $forum_id, 'topic_meta' => array( 'forum_id' => $forum_id ) ) );
     18                $scripts     = wp_scripts();
     19                $registered  = isset( $scripts->registered['bbp-admin-blocks'] ) ? $scripts->registered['bbp-admin-blocks'] : null;
     20                $query_count = 0;
     21                $count_query = function( $query ) use ( &$query_count ) {
     22                        if ( false !== strpos( $query, 'SELECT DISTINCT tt.term_id' ) ) {
     23                                ++$query_count;
     24                        }
     25                        return $query;
     26                };
     27
     28                wp_deregister_script( 'bbp-admin-blocks' );
     29                add_filter( 'query', $count_query );
     30
     31                try {
     32                        $this->assertNotFalse( has_action( 'enqueue_block_editor_assets', array( bbpress()->blocks, 'enqueue_block_editor_assets' ) ) );
     33                        do_action( 'enqueue_block_editor_assets' );
     34                        $this->assertSame( 0, $query_count );
     35                        $this->assertFalse( $scripts->get_data( 'bbp-admin-blocks', 'data' ) );
     36
     37                        wp_register_script( 'bbp-admin-blocks', false );
     38                        bbpress()->blocks->enqueue_block_editor_assets();
     39                        $this->assertGreaterThan( 0, $query_count );
     40                        $this->assertStringContainsString( 'bbpBlocksJS', $scripts->get_data( 'bbp-admin-blocks', 'data' ) );
     41                } finally {
     42                        remove_filter( 'query', $count_query );
     43                        wp_deregister_script( 'bbp-admin-blocks' );
     44                        if ( null !== $registered ) {
     45                                $scripts->registered['bbp-admin-blocks'] = $registered;
     46                        }
     47                }
     48        }
    1149
    1250        /**
    … …  
    121159                $this->assertContains( 'public-block-tag', $labels );
    122160
     161                delete_post_meta( $public_topic, '_bbp_forum_id' );
     162                $labels = wp_list_pluck( BBP_Blocks::get_localize_script_data( 'topic_tags' ), 'label' );
     163                $this->assertNotContains( 'public-block-tag', $labels );
     164
     165                update_post_meta( $public_topic, '_bbp_forum_id', 'not-a-forum' );
     166                $labels = wp_list_pluck( BBP_Blocks::get_localize_script_data( 'topic_tags' ), 'label' );
     167                $this->assertNotContains( 'public-block-tag', $labels );
     168
    123169                wp_update_post( array( 'ID' => $public_topic, 'post_parent' => $private_forum ) );
    124170                update_post_meta( $public_topic, '_bbp_forum_id', $private_forum );
Note: See TracChangeset for help on using the changeset viewer.