Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SiteMeta Fix & Upgrade #5

Merged
merged 6 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [[*next-version*]] - YYYY-MM-DD
### Added
- `BlogOptions` now accepts a `null` blog ID,
which causes it to use the current site's options every time, regardless (#3).
- `BlogOptions` now accepts a `null` blog ID, which causes it to use the current site's options every time (#3).
- `SiteMeta` now accepts a `null` blog ID, which causes it to use the current site's meta every time (#5).

### Fixed
- `SiteMeta` now uses correct site meta functions (#5).

## [0.1.1-alpha1] - 2022-01-05
### Changed
Expand Down
42 changes: 24 additions & 18 deletions src/Options/SiteMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,15 @@ class SiteMeta implements MutableContainerInterface
{
use StringTranslatingTrait;

/**
* @var int
*/
/** @var int|null */
protected $siteId;

/**
* @var mixed
*/
protected $default;

/**
* @param int $siteId ID of the site.
* @param mixed $default The value that, if returned by WP, will indicate that the key is not found.
* @param int|null $siteId ID of the site, or `null` to always use current site.
*/
public function __construct(int $siteId, $default)
public function __construct(int $siteId)
{
$this->siteId = $siteId;
$this->default = $default;
}

/**
Expand Down Expand Up @@ -114,7 +105,8 @@ public function set(string $key, $value): void
public function unset(string $key): void
{
$siteId = $this->siteId;
$result = delete_network_option($siteId, $key);
/** @psalm-suppress PossiblyNullArgument Actually allows null site ID */
$result = delete_site_meta($siteId, $key);

if ($result === false) {
throw new ContainerException(
Expand All @@ -140,10 +132,23 @@ public function unset(string $key): void
protected function getMeta(string $name)
{
$siteId = $this->siteId;
$default = $this->default;
$value = get_network_option($siteId, $name, $default);

if ($value === $default) {
/** @psalm-suppress PossiblyNullArgument Actually allows null site ID */
$value = get_site_meta($siteId, $name, false);

/*
* There's no way to pass a control value that would be returned if not found.
* Due to the way meta retrieval works (via cache), it does not check for individual keys in the DB,
* but instead retrieves and caches them all. There's no additional check to return a special value
* that would reliably signify that the value is not found. Instead, it returns maybe unserialized value
* if it is found, and null otherwise. Therefore, null is the closest value to signifying its absence.
* https://github.com/WordPress/WordPress/blob/2699b3032a710335e47a4a3a9d3fd5e44c35bac0/wp-includes/meta.php#L665
*
* Also, the value can be an empty string if site ID is not found, or false if invalid, according to docs.
* There is no way to determine the site specified is missing or if its ID is invalid, or if the empty string
* is the actual value, without checking for existence of the site with the ID.
* This is the concern of a factory instantiating this class.
*/
if ($value === null) {
throw new UnexpectedValueException(
$this->__(
'Meta key "%1$s" for blog #%2$d does not exist',
Expand All @@ -169,7 +174,8 @@ protected function setMeta(string $name, $value): void
{
$siteId = $this->siteId;

$isSuccessful = update_network_option($siteId, $name, $value);
/** @psalm-suppress PossiblyNullArgument Actually allows null site ID */
$isSuccessful = update_site_meta($siteId, $name, $value);
if (!$isSuccessful) {
$newValue = $this->getMeta($name);
$isSuccessful = $value === $newValue;
Expand Down
60 changes: 27 additions & 33 deletions tests/functional/Options/SiteMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public function testHasTrue()
[$siteId, $default],
null
);
$fnGetNetworkOption = Functions\expect('get_network_option')
$fnGetNetworkOption = Functions\expect('get_site_meta')
->times(1)
->with($siteId, $optionName, $default)
->with($siteId, $optionName, false)
->andReturn($optionValue);
}

Expand All @@ -89,15 +89,14 @@ public function testHasFalse()
{
$siteId = rand(1, 99);
$optionName = uniqid('option-name');
$default = uniqid('default-value');
$subject = $this->createSubject(
[$siteId, $default],
[$siteId],
null
);
$fnGetNetworkOption = Functions\expect('get_network_option')
$fnGetNetworkOption = Functions\expect('get_site_meta')
->times(1)
->with($siteId, $optionName, $default)
->andReturn($default);
->with($siteId, $optionName, false)
->andReturn(null);
}

{
Expand Down Expand Up @@ -154,14 +153,13 @@ public function testGet($optionValue)
{
$siteId = rand(1, 99);
$optionName = uniqid('option-name');
$default = uniqid('default');
$subject = $this->createSubject(
[$siteId, $default],
[$siteId],
null
);
$fnGetNetworkOption = Functions\expect('get_network_option')
$fnGetNetworkOption = Functions\expect('get_site_meta')
->times(1)
->with($siteId, $optionName, $default)
->with($siteId, $optionName, false)
->andReturn($optionValue);
}

Expand Down Expand Up @@ -189,15 +187,14 @@ public function testGetNotFound()
$siteId = rand(1, 99);
$optionName = uniqid('option-name');
$optionValue = uniqid('option-value');
$default = uniqid('default');
$subject = $this->createSubject(
[$siteId, $default],
[$siteId],
null
);
$fnGetNetworkOption = Functions\expect('get_network_option')
$fnGetNetworkOption = Functions\expect('get_site_meta')
->times(1)
->with($siteId, $optionName, $default)
->andReturn($default);
->with($siteId, $optionName, false)
->andReturn(null);
$this->expectException(NotFoundExceptionInterface::class);
}

Expand Down Expand Up @@ -227,12 +224,11 @@ public function testSet($optionValue)
{
$siteId = rand(1, 99);
$optionName = uniqid('option-name');
$default = uniqid('default');
$subject = $this->createSubject(
[$siteId, $default],
[$siteId],
null
);
$fnUpdateNetworkOption = Functions\expect('update_network_option')
$fnUpdateNetworkOption = Functions\expect('update_site_meta')
->times(1)
->with($siteId, $optionName, $optionValue)
->andReturn(true);
Expand Down Expand Up @@ -262,18 +258,17 @@ public function testSetSame($optionValue)
{
$siteId = rand(1, 99);
$optionName = uniqid('option-name');
$default = uniqid('default');
$subject = $this->createSubject(
[$siteId, $default],
[$siteId],
null
);
$fnUpdateNetworkOption = Functions\expect('update_network_option')
$fnUpdateNetworkOption = Functions\expect('update_site_meta')
->times(1)
->with($siteId, $optionName, $optionValue)
->andReturn(false);
$fnGetNetworkOption = Functions\expect('get_network_option')
$fnGetNetworkOption = Functions\expect('get_site_meta')
->times(1)
->with($siteId, $optionName, $default)
->with($siteId, $optionName, false)
->andReturn($optionValue);
}

Expand All @@ -297,18 +292,17 @@ public function testSetFailure()
$siteId = rand(1, 99);
$optionName = uniqid('option-name');
$optionValue = uniqid('option-value');
$default = uniqid('default');
$subject = $this->createSubject(
[$siteId, $default],
[$siteId],
null
);
$fnUpdateNetworkOption = Functions\expect('update_network_option')
$fnUpdateNetworkOption = Functions\expect('update_site_meta')
->times(1)
->with($siteId, $optionName, $optionValue)
->andReturn(false);
$fnGetNetworkOption = Functions\expect('get_network_option')
$fnGetNetworkOption = Functions\expect('get_site_meta')
->times(1)
->with($siteId, $optionName, $default)
->with($siteId, $optionName, false)
->andReturn(uniqid('different-value'));
$this->expectException(ContainerExceptionInterface::class);
}
Expand All @@ -335,10 +329,10 @@ public function testUnset()
$blogId = rand(1, 99);
$optionName = uniqid('option-name');
$subject = $this->createSubject(
[$blogId, uniqid('default-value')],
[$blogId],
null
);
$fnDeleteNetworkOption = Functions\expect('delete_network_option')
$fnDeleteNetworkOption = Functions\expect('delete_site_meta')
->times(1)
->with($blogId, $optionName)
->andReturn(true);
Expand Down Expand Up @@ -366,10 +360,10 @@ public function testUnsetFailure()
$blogId = rand(1, 99);
$optionName = uniqid('option-name');
$subject = $this->createSubject(
[$blogId, uniqid('default-value')],
[$blogId],
null
);
$fnDeleteNetworkOption = Functions\expect('delete_network_option')
$fnDeleteNetworkOption = Functions\expect('delete_site_meta')
->times(1)
->with($blogId, $optionName)
->andReturn(false);
Expand Down