Skip to content

Commit

Permalink
Merge branch 'develop' into feature/lang-site-default
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeelia committed Jul 24, 2023
2 parents 1d24c0b + d939bef commit 6dc00b8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
46 changes: 46 additions & 0 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,3 +813,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 );
}
54 changes: 54 additions & 0 deletions tests/php/TestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,60 @@ 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 ) );
}

/**
* Test the `get_language()` method
*
Expand Down

0 comments on commit 6dc00b8

Please sign in to comment.