Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/15/2023 10:23:05 AM (22 months ago)
Author:
imath
Message:

Improve BP_Component methods relative to setting BP Rewrites

So far BP_Component::add_rewrite_tags(),
BP_Component::add_rewrite_rules() & BP_Component::add_permastructs()
were just firing hooks. Thanks to the BP Rewrites plugin we were able to
test how we can use them to actually enjoy the WP Rewrite API in
BuddyPress. Compared to what was tested in this feature as plugin, some
improvements has been brought so that it's easier to benefit from the BP
Rewrites API for custom components (analyzing the included PHPUnit tests
can give BP plugin authors a preview about how to process).

Introduce the BP_Component::pre_query() method to avoid superfluous Post
queries when a BuddyPress page is displayed.

The src/bp-core/bp-rewrites.php file has also been inited, this is the
place where all Core component rewrites function will be placed.

Props r-a-y, johnjamesjacoby, boonebgorges

See #4954

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/testcases/core/class-bp-component.php

    r13314 r13422  
    9393        $this->assertEquals( $expected, $example->block_globals['bp/example-block']->props );
    9494    }
     95
     96    /**
     97     * @group bp_rewrites
     98     */
     99    public function test_component_rewrite_globals() {
     100        $expected = array(
     101            'directory'          => 'bp_examples',
     102            'single_item'        => 'bp_example',
     103            'single_item_action' => 'bp_example_action',
     104        );
     105
     106        $example = new BPTest_Component(
     107            array(
     108                'globals' => array(
     109                    'rewrite_ids' => array(
     110                        'directory'          => 'examples ',
     111                        'single_item'        => 'Exam?ple',
     112                        'single_item_action' => 'bp_example_action',
     113                    )
     114                ),
     115            )
     116        );
     117
     118        do_action( 'bp_setup_globals' );
     119
     120        $this->assertEquals( $expected, $example->rewrite_ids );
     121    }
     122
     123    /**
     124     * @group bp_rewrites
     125     */
     126    public function test_component_add_rewrite_tags() {
     127        $example = new BPTest_Component(
     128            array(
     129                'globals' => array(
     130                    'rewrite_ids' => array(
     131                        'directory'      => 'examples',
     132                        'directory_type' => 'example_type',
     133                    )
     134                ),
     135            )
     136        );
     137
     138        do_action( 'bp_setup_globals' );
     139
     140        $expected_directory_regex = '([1]{1,})';
     141        $rewrite_tags             = array(
     142            'directory_type' => '([^/]+)',
     143        );
     144
     145        $example->add_rewrite_tags( $rewrite_tags );
     146
     147        global $wp_rewrite;
     148
     149        $position = array_search( '%' . $example->rewrite_ids['directory'] . '%', $wp_rewrite->rewritecode, true );
     150        $this->assertEquals( $wp_rewrite->rewritereplace[ $position ], $expected_directory_regex );
     151
     152        $position = array_search( '%' . $example->rewrite_ids['directory_type'] . '%', $wp_rewrite->rewritecode, true );
     153        $this->assertEquals( $wp_rewrite->rewritereplace[ $position ], $rewrite_tags['directory_type'] );
     154    }
     155
     156    /**
     157     * @group bp_rewrites
     158     */
     159    public function test_component_add_rewrite_rules() {
     160        $example = new BPTest_Component(
     161            array(
     162                'globals' => array(
     163                    'root_slug'   => 'examples',
     164                    'rewrite_ids' => array(
     165                        'directory'             => 'examples',
     166                        'directory_type'        => 'example_type',
     167                        'single_item'           => 'example',
     168                        'single_item_component' => 'example_component',
     169                    )
     170                ),
     171            )
     172        );
     173
     174        do_action( 'bp_setup_globals' );
     175
     176        $rewrite_tags = array(
     177            'directory_type' => '([^/]+)',
     178        );
     179
     180        $example->add_rewrite_tags( $rewrite_tags );
     181
     182        $rewrite_rules = array(
     183            'directory_type' => array(
     184                'order' => 95,
     185                'regex' => $example->root_slug . '/type/([^/]+)/?$',
     186                'query' => 'index.php?' . $example->rewrite_ids['directory'] . '=1&' . $example->rewrite_ids['directory_type'] . '=$matches[1]',
     187            ),
     188        );
     189
     190        $example->add_rewrite_rules( $rewrite_rules );
     191
     192        global $wp_rewrite;
     193        $this->assertEquals( $wp_rewrite->extra_rules_top[ $rewrite_rules['directory_type']['regex'] ], $rewrite_rules['directory_type']['query'] );
     194    }
     195
     196    /**
     197     * @group bp_rewrites
     198     */
     199    public function test_component_add_permastructs() {
     200        $example = new BPTest_Component(
     201            array(
     202                'globals' => array(
     203                    'has_directory' => true,
     204                    'root_slug'     => 'examples',
     205                    'rewrite_ids'   => array(
     206                        'directory'      => 'examples',
     207                        'example_signup' => 'signup',
     208                    )
     209                ),
     210            )
     211        );
     212
     213        do_action( 'bp_setup_globals' );
     214
     215        $expected     = 'example-signup/%' . $example->rewrite_ids['example_signup'] . '%';
     216        $permastructs = array(
     217            $example->rewrite_ids['example_signup'] => array(
     218                'permastruct' => $expected,
     219                'args'        => array(),
     220            ),
     221        );
     222
     223        $example->add_permastructs( $permastructs );
     224
     225        global $wp_rewrite;
     226
     227        // The directory permastruct should be created automatically.
     228        $this->assertTrue( isset( $wp_rewrite->extra_permastructs['bp_examples'] ) );
     229
     230        // The custom permastruct should be created as requested.
     231        $this->assertEquals( $wp_rewrite->extra_permastructs[ $example->rewrite_ids['example_signup'] ]['struct'], $expected );
     232    }
    95233}
Note: See TracChangeset for help on using the changeset viewer.