Skip to content

Commit ebe36e8

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@60959 602fd350-edb4-49c9-b593-d223f7449a82
1 parent cffdb43 commit ebe36e8

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/wp-includes/option.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ function get_option( $option, $default_value = false ) {
134134
/**
135135
* Filters the value of all existing options before it is retrieved.
136136
*
137-
* Returning a truthy value from the filter will effectively short-circuit retrieval
138-
* and return the passed value instead.
137+
* Returning a value other than false from the filter will short-circuit retrieval
138+
* and return that value instead.
139139
*
140140
* @since 6.1.0
141141
*
@@ -2037,6 +2037,25 @@ function get_network_option( $network_id, $option, $default_value = false ) {
20372037
*/
20382038
$pre = apply_filters( "pre_site_option_{$option}", false, $option, $network_id, $default_value );
20392039

2040+
/**
2041+
* Filters the value of an existing network options before it is retrieved.
2042+
*
2043+
* Returning a value other than false from the filter will short-circuit retrieval
2044+
* and return that value instead.
2045+
*
2046+
* @since 6.9.0
2047+
*
2048+
* @param mixed $pre The value to return instead of the network option value. This differs
2049+
* from `$default`, which is used as the fallback value in the event
2050+
* the option doesn't exist elsewhere in get_network_option().
2051+
* Default false (to skip past the short-circuit).
2052+
* @param string $option Name of the option.
2053+
* @param int $network_id ID of the network.
2054+
* @param mixed $default_value The fallback value to return if the option does not exist.
2055+
* Default false.
2056+
*/
2057+
$pre = apply_filters( 'pre_site_option', $pre, $option, $network_id, $default_value );
2058+
20402059
if ( false !== $pre ) {
20412060
return $pre;
20422061
}

tests/phpunit/tests/option/networkOption.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,54 @@ public function test_delete_network_option_does_not_use_single_site_notoptions_c
420420
$this->assertIsArray( $network_notoptions_cache_after, 'Multisite notoptions cache should be set.' );
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
}

tests/phpunit/tests/option/option.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ public function test_add_option_should_respect_default_option_filter() {
8686
}
8787

8888
/**
89+
* Assert that the 'pre_option' hook is called once per call to get_option().
90+
*
8991
* @ticket 37930
9092
*
9193
* @covers ::get_option

0 commit comments

Comments
 (0)