WordPress.org

Make WordPress Core

Ticket #39120: 39120.diff

File 39120.diff, 4.9 KB (added by birgire, 19 months ago)
  • src/wp-includes/class-wp-comment-query.php

    diff --git src/wp-includes/class-wp-comment-query.php src/wp-includes/class-wp-comment-query.php
    index d0e7cff..c831cba 100644
    class WP_Comment_Query { 
    11451145                        return 'DESC';
    11461146                }
    11471147        }
     1148
     1149        /**
     1150         * Retrieve query variable.
     1151         *
     1152         * @since 5.0.0
     1153         *
     1154         * @param  string $query_var Query variable key.
     1155         * @param  mixed  $default   Optional. Value to return if the query variable is not set. Default null.
     1156         * @return mixed             Contents of the query variable.
     1157         */
     1158        public function get( $query_var, $default = null ) {
     1159                if ( isset( $this->query_vars[ $query_var ] ) ) {
     1160                        return $this->query_vars[ $query_var ];
     1161                }
     1162
     1163                return $default;
     1164        }
     1165
     1166        /**
     1167         * Set query variable.
     1168         *
     1169         * @since 5.0.0
     1170         *
     1171         * @param string $query_var Query variable key.
     1172         * @param mixed  $value     Query variable value.
     1173         */
     1174        public function set( $query_var, $value ) {
     1175                $this->query_vars[ $query_var ] = $value;
     1176        }
    11481177}
  • src/wp-includes/class-wp-term-query.php

    diff --git src/wp-includes/class-wp-term-query.php src/wp-includes/class-wp-term-query.php
    index b7c3b42..68f87fc 100644
    class WP_Term_Query { 
    972972
    973973                return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
    974974        }
     975
     976        /**
     977         * Retrieve query variable.
     978         *
     979         * @since 5.0.0
     980         *
     981         * @param string $query_var Query variable key.
     982         * @param mixed  $default   Optional. Value to return if the query variable is not set. Default null.
     983         * @return mixed            Contents of the query variable.
     984         */
     985        public function get( $query_var, $default = null ) {
     986                if ( isset( $this->query_vars[ $query_var ] ) ) {
     987                        return $this->query_vars[ $query_var ];
     988                }
     989
     990                return $default;
     991        }
     992
     993        /**
     994         * Set query variable.
     995         *
     996         * @since 5.0.0
     997         *
     998         * @param string $query_var Query variable key.
     999         * @param mixed  $value     Query variable value.
     1000         */
     1001        public function set( $query_var, $value ) {
     1002                $this->query_vars[ $query_var ] = $value;
     1003        }
    9751004}
  • tests/phpunit/tests/comment/query.php

    diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
    index 9333800..f384c77 100644
    class Tests_Comment_Query extends WP_UnitTestCase { 
    48724872
    48734873                $this->assertEqualSets( $c1, $found );
    48744874        }
     4875
     4876        /**
     4877         * Test that the get and set methods for handling query variables should be supported.
     4878         *
     4879         * @ticket 39120
     4880         */
     4881        public function test_get_and_set_methods_for_handling_query_vars_should_be_supported() {
     4882
     4883                $comments = new WP_Comment_Query();
     4884
     4885                // With default query variable value as null.
     4886                $this->assertNull( $comments->get( 'fields' ) );
     4887                $this->assertNull( $comments->query_vars['fields'] );
     4888
     4889                // With query variable value set as a non-empty string.
     4890                $comments->set( 'fields', 'ids' );
     4891                $this->assertSame( 'ids', $comments->get( 'fields' ) );
     4892                $this->assertSame( 'ids', $comments->query_vars['fields'] );
     4893
     4894                // With query variable value set as an empty string.
     4895                $comments->set( 'fields', '' );
     4896                $this->assertSame( '', $comments->get( 'fields' ) );
     4897                $this->assertSame( '', $comments->query_vars['fields'] );
     4898
     4899                // Getting a non-existing query variable key.
     4900                $this->assertNull( $comments->get( 'does-not-exist' ) );
     4901                // Getting a non-existing query varable key, with a default query value as a non-empty string.
     4902                $this->assertSame( 'foo', $comments->get( 'does-not-exist', 'foo' ) );
     4903        }
    48754904}
  • tests/phpunit/tests/term/query.php

    diff --git tests/phpunit/tests/term/query.php tests/phpunit/tests/term/query.php
    index b517417..0514b94 100644
    class Tests_Term_Query extends WP_UnitTestCase { 
    614614                );
    615615                $this->assertSame( array(), $q->terms );
    616616        }
     617
     618        /**
     619         * Test that the get and set methods for handling query variables should be supported.
     620         *
     621         * @ticket 39120
     622         */
     623        public function test_get_and_set_methods_for_handling_query_vars_should_be_supported() {
     624
     625                $terms = new WP_Term_Query();
     626
     627                // With default query variable value as null.
     628                $this->assertNull( $terms->get( 'fields' ) );
     629                $this->assertNull( $terms->query_vars['fields'] );
     630
     631                // With query variable value set as a non-empty string.
     632                $terms->set( 'fields', 'ids' );
     633                $this->assertSame( 'ids', $terms->get( 'fields' ) );
     634                $this->assertSame( 'ids', $terms->query_vars['fields'] );
     635
     636                // With query variable value set as an empty string.
     637                $terms->set( 'fields', '' );
     638                $this->assertSame( '', $terms->get( 'fields' ) );
     639                $this->assertSame( '', $terms->query_vars['fields'] );
     640
     641                // Getting a non-existing query variable key.
     642                $this->assertNull( $terms->get( 'does-not-exist' ) );
     643                // Getting a non-existing query varable key, with a default query value as a non-empty string.
     644                $this->assertSame( 'foo', $terms->get( 'does-not-exist', 'foo' ) );
     645        }
    617646}