Make WordPress Core


Ignore:
Timestamp:
10/17/2025 06:46:42 PM (5 months ago)
Author:
johnjamesjacoby
Message:

Networks and Sites: add a general pre_site_option filter to get_network_option().

This change brings get_network_option() up-to-speed with get_option() by adding a more generalized way to short-circuit its return value.

It also introduces 2 new unit tests: one to mirror an existing pre_option test, and another to confirm that this new filter is working as intended.

Props audrasjb, Drivingralle, johnjamesjacoby, jorbin, nimeshatxecurify, rollybueno, shailu25, welcher, westonruter.

See #37930, r54145.

Fixes #56870.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/option/networkOption.php

    r59885 r60959  
    421421        $this->assertArrayHasKey( 'ticket_61730_notoption', $network_notoptions_cache_after, 'The option should be in the notoptions cache.' );
    422422    }
     423
     424    /**
     425     * Assert that the 'pre_site_option' hook is called once per call to get_network_option().
     426     *
     427     * @ticket 56870
     428     *
     429     * @group ms-required
     430     *
     431     * @covers ::get_network_option
     432     */
     433    public function test_get_network_option_should_call_pre_site_option_filter() {
     434        $filter = new MockAction();
     435
     436        add_filter( 'pre_site_option', array( $filter, 'filter' ) );
     437
     438        get_network_option( get_current_network_id(), 'ignored' );
     439
     440        $this->assertSame( 1, $filter->get_call_count() );
     441    }
     442
     443    /**
     444     * Verifies that the global 'pre_site_option' filter short-circuits get_network_option().
     445     *
     446     * @ticket 56870
     447     *
     448     * @group ms-required
     449     *
     450     * @covers ::get_network_option
     451     */
     452    public function test_pre_site_option_filter_short_circuits_get_network_option() {
     453        $option       = 'ticket_56870_pre_site_option_short_circuit';
     454        $network_id   = get_current_network_id();
     455        $default_val  = 'default-value';
     456        $expected_val = 'filtered-value';
     457
     458        $callback = function ( $pre, $opt, $net_id ) use ( $option, $network_id, $expected_val ) {
     459            // Ensure the filter is invoked for the requested option and network, then short-circuit.
     460            if ( $opt === $option && (int) $net_id === (int) $network_id ) {
     461                return $expected_val;
     462            }
     463            return $pre;
     464        };
     465
     466        add_filter( 'pre_site_option', $callback, 10, 3 );
     467
     468        $actual_val = get_network_option( $network_id, $option, $default_val );
     469
     470        // The global pre filter should short-circuit and return $expected_val regardless of storage or default.
     471        $this->assertSame( $expected_val, $actual_val );
     472    }
    423473}
Note: See TracChangeset for help on using the changeset viewer.