diff --git a/includes/utils.php b/includes/utils.php index 41a20a0016..420811769f 100644 --- a/includes/utils.php +++ b/includes/utils.php @@ -818,3 +818,49 @@ function get_elasticsearch_error_reason( $response ) : string { return ''; } + +/** + * Use the correct set_transient option function depending on the context (multisite or not) + * + * @since 4.7.0 + * @param string $transient Transient name. Expected to not be SQL-escaped. + * Must be 172 characters or fewer in length. + * @param mixed $value Transient value. Must be serializable if non-scalar. + * Expected to not be SQL-escaped. + * @param int $expiration Optional. Time until expiration in seconds. Default 0 (no expiration). + * @return bool True if the value was set, false otherwise. + */ +function set_transient( $transient, $value, $expiration = 0 ) { + if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { + return \set_site_transient( $transient, $value, $expiration ); + } + return \set_transient( $transient, $value, $expiration ); +} + +/** + * Use the correct get_transient function depending on the context (multisite or not) + * + * @since 4.7.0 + * @param string $transient Transient name. Expected to not be SQL-escaped. + * @return mixed Value of transient. + */ +function get_transient( $transient ) { + if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { + return \get_site_transient( $transient ); + } + return \get_transient( $transient ); +} + +/** + * Use the correct delete_transient function depending on the context (multisite or not) + * + * @since 4.7.0 + * @param string $transient Transient name. Expected to not be SQL-escaped. + * @return bool True if the transient was deleted, false otherwise. + */ +function delete_transient( $transient ) { + if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { + return \delete_site_transient( $transient ); + } + return \delete_transient( $transient ); +} diff --git a/tests/php/TestUtils.php b/tests/php/TestUtils.php index 211159cd04..ed356b6ba1 100644 --- a/tests/php/TestUtils.php +++ b/tests/php/TestUtils.php @@ -389,4 +389,58 @@ public function testGetElasticsearchErrorReason() { ]; $this->assertSame( '', Utils\get_elasticsearch_error_reason( $not_an_error ) ); } + + /** + * Test the `set_transient` function + * + * @since 4.7.0 + * @group utils + */ + public function test_set_transient() { + $filter_name = is_multisite() ? + 'expiration_of_site_transient_foo' : + 'expiration_of_transient_foo'; + + $check_expiration = function ( $expiration ) { + $this->assertSame( 1, $expiration ); + return $expiration; + }; + add_filter( $filter_name, $check_expiration ); + + Utils\set_transient( 'foo', 'bar', 1 ); + + $this->assertSame( 1, did_filter( $filter_name ) ); + } + + /** + * Test the `get_transient` function + * + * @since 4.7.0 + * @group utils + */ + public function test_get_transient() { + Utils\get_transient( 'foo' ); + + $filter_name = is_multisite() ? + 'pre_site_transient_foo' : + 'pre_transient_foo'; + + $this->assertSame( 1, did_filter( $filter_name ) ); + } + + /** + * Test the `delete_transient` function + * + * @since 4.7.0 + * @group utils + */ + public function test_delete_transient() { + Utils\delete_transient( 'foo' ); + + $filter_name = is_multisite() ? + 'delete_site_transient_foo' : + 'delete_transient_foo'; + + $this->assertSame( 1, did_action( $filter_name ) ); + } }