Make WordPress Core

Ticket #45875: 45875.diff

File 45875.diff, 1.7 KB (added by andizer, 5 years ago)

Patch with unittests

  • src/wp-includes/load.php

     
    11821182}
    11831183
    11841184/**
     1185 * Determines whether the current request is a WordPress REST request.
     1186 *
     1187 * @since 5.1.0
     1188 *
     1189 * @return bool True if it's a WordPress REST request, false otherwise.
     1190 */
     1191function wp_doing_rest_request() {
     1192        /**
     1193         * Filters whether the current request is a WordPress REST request.
     1194         *
     1195         * @since 5.1.0
     1196         *
     1197         * @param bool $wp_doing_rest_request Whether the current request is a WordPress REST request.
     1198         */
     1199        return apply_filters( 'wp_doing_rest_request', defined( 'REST_REQUEST' ) && REST_REQUEST );
     1200}
     1201
     1202/**
    11851203 * Check whether variable is a WordPress Error.
    11861204 *
    11871205 * Returns true if $thing is an object of the WP_Error class.
  • tests/phpunit/tests/load/doingRestRequest.php

     
     1<?php
     2/**
     3 * Tests for wp_doing_rest_request().
     4 *
     5 * @group load.php
     6 */
     7class Tests_Functions_Doing_Rest_Request extends WP_UnitTestCase {
     8
     9        /**
     10         * Tests if a REST request has been done.
     11         */
     12        public function test_wp_doing_rest_request() {
     13                $this->assertFalse( wp_doing_rest_request() );
     14
     15                add_filter( 'wp_doing_rest_request', '__return_true' );
     16                $this->assertTrue( wp_doing_rest_request() );
     17
     18                define( 'REST_REQUEST', TRUE );
     19
     20                $this->assertTrue( wp_doing_rest_request() );
     21
     22                add_filter( 'wp_doing_rest_request', '__return_false' );
     23                $this->assertFalse( wp_doing_rest_request() );
     24        }
     25}