Skip to:
Content

bbPress.org

Changeset 7763


Ignore:
Timestamp:
10/01/2026 04:50:02 AM (8 days 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.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/CHANGELOG.md

    r7751 r7763  
    124124### Fixed
    125125
     126- Avoided block editor choice queries when their target script is unavailable.
    126127- Kept forum exclusions active in topic and reply queries using OR metadata conditions.
    127128- Corrected topic edit-lock owner detection.
  • trunk/src/includes/common/blocks.php

    r7755 r7763  
    130130                 */
    131131                public function enqueue_block_editor_assets() {
     132
     133                        // Bail if the script receiving this data is unavailable.
     134                        if ( ! wp_script_is( 'bbp-admin-blocks', 'registered' ) ) {
     135                                return;
     136                        }
    132137
    133138                        // Localize script data for the block editor (script is loaded via block.json).
    … …  
    382387                        $placeholders       = implode( ', ', array_fill( 0, count( $statuses ), '%s' ) );
    383388                        $forum_placeholders = implode( ', ', array_fill( 0, count( $forum_ids ), '%d' ) );
    384                         $values             = array_merge( array( bbp_get_topic_tag_tax_id(), bbp_get_topic_post_type() ), $statuses, $forum_ids, $forum_ids, $forum_ids );
     389                        $values             = array_merge( array( bbp_get_topic_tag_tax_id(), bbp_get_topic_post_type() ), $statuses, $forum_ids );
    385390                        $query              = "SELECT DISTINCT tt.term_id
    386391                                FROM {$bbp_db->term_taxonomy} AS tt
    … …  
    390395                                WHERE tt.taxonomy = %s AND p.post_type = %s AND p.post_status IN ({$placeholders})
    391396                                        AND p.post_parent IN ({$forum_placeholders})
    392                                         AND CAST( fm.meta_value AS UNSIGNED ) IN ({$forum_placeholders})
    393                                         AND NOT EXISTS (
    394                                                 SELECT 1 FROM {$bbp_db->postmeta} AS excluded_fm
    395                                                 WHERE excluded_fm.post_id = p.ID AND excluded_fm.meta_key = '_bbp_forum_id'
    396                                                 AND CAST( excluded_fm.meta_value AS UNSIGNED ) NOT IN ({$forum_placeholders})
    397                                         )";
     397                                        AND CAST( fm.meta_value AS UNSIGNED ) = p.post_parent";
    398398                        $term_ids = $bbp_db->get_col( $bbp_db->prepare( $query, $values ) );
    399399
  • 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.