WordPress.org

Make WordPress Core

Ticket #35235: 35235.2.diff

File 35235.2.diff, 3.0 KB (added by swissspidy, 5 years ago)
  • src/wp-includes/class-wp-rewrite.php

    diff --git src/wp-includes/class-wp-rewrite.php src/wp-includes/class-wp-rewrite.php
    index 035bbc3..e6f768f 100644
    class WP_Rewrite { 
    17451745        }
    17461746
    17471747        /**
     1748         * Removes a permalink structure.
     1749         *
     1750         * @since 4.5.0
     1751         * @access public
     1752         *
     1753         * @param string $name Name for permalink structure.
     1754         */
     1755        public function remove_permastruct( $name ) {
     1756                unset( $this->extra_permastructs[ $name ] );
     1757        }
     1758
     1759        /**
    17481760         * Removes rewrite rules and then recreate rewrite rules.
    17491761         *
    17501762         * Calls WP_Rewrite::wp_rewrite_rules() after removing the 'rewrite_rules' option.
  • src/wp-includes/rewrite.php

    diff --git src/wp-includes/rewrite.php src/wp-includes/rewrite.php
    index 7f3b2ed..fe6b99a 100644
    function add_rewrite_tag( $tag, $regex, $query = '' ) { 
    178178 * @since 3.0.0
    179179 *
    180180 * @see WP_Rewrite::add_permastruct()
    181  * @global WP_Rewrite $wp_rewrite
     181 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
    182182 *
    183183 * @param string $name   Name for permalink structure.
    184184 * @param string $struct Permalink structure.
    function add_permastruct( $name, $struct, $args = array() ) { 
    198198}
    199199
    200200/**
     201 * Remove permalink structure.
     202 *
     203 * Can only be used to remove permastructs that were added using add_permastruct().
     204 * Built-in permastructs cannot be removed.
     205 *
     206 * @since 4.5.0
     207 *
     208 * @see WP_Rewrite::remove_permastruct()
     209 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
     210 *
     211 * @param string $name Name for permalink structure.
     212 */
     213function remove_permastruct( $name ) {
     214        global $wp_rewrite;
     215
     216        $wp_rewrite->remove_permastruct( $name );
     217}
     218
     219/**
    201220 * Add a new feed type like /atom1/.
    202221 *
    203222 * @since 2.1.0
  • new file tests/phpunit/tests/rewrite/permastructs.php

    diff --git tests/phpunit/tests/rewrite/permastructs.php tests/phpunit/tests/rewrite/permastructs.php
    new file mode 100644
    index 0000000..3413398
    - +  
     1<?php
     2
     3/**
     4 * @group rewrite
     5 */
     6class Tests_Rewrite_Permastructs extends WP_UnitTestCase {
     7
     8        public function setUp() {
     9                parent::setUp();
     10
     11                $this->set_permalink_structure( '/%postname%/' );
     12        }
     13
     14        public function test_add_permastruct(  ) {
     15                global $wp_rewrite;
     16
     17                add_permastruct( 'foo', 'bar/%foo%' );
     18                $this->assertEqualSets( array(
     19                        'with_front'  => true,
     20                        'ep_mask'     => EP_NONE,
     21                        'paged'       => true,
     22                        'feed'        => true,
     23                        'walk_dirs'   => true,
     24                        'endpoints'   => true,
     25                        'forcomments' => false,
     26                        'struct'      => '/bar/%foo%',
     27                ), $wp_rewrite->extra_permastructs['foo'] );
     28        }
     29
     30        public function test_remove_permastruct(  ) {
     31                global $wp_rewrite;
     32
     33                add_permastruct( 'foo', 'bar/%foo%' );
     34                $this->assertInternalType( 'array', $wp_rewrite->extra_permastructs['foo'] );
     35                $this->assertSame( '/bar/%foo%', $wp_rewrite->extra_permastructs['foo']['struct'] );
     36
     37                remove_permastruct( 'foo' );
     38                $this->assertFalse( isset( $wp_rewrite->extra_permastructs['foo'] ) );
     39        }
     40}