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

Feature/php8 enhancements #74

Merged
merged 10 commits into from
Jun 23, 2023
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
8 changes: 5 additions & 3 deletions src/Api/LocalizeScripts.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Dwnload\WpSettingsApi\Api;

Expand All @@ -15,7 +17,7 @@ class LocalizeScripts

/**
* Localized array to pass from PHP to JS.
* @var array $vars
* @var string[] $vars
*/
protected static array $vars = [];

Expand All @@ -24,7 +26,7 @@ class LocalizeScripts
* @param string $key
* @param string $value
*/
public function add(string $key, string $value)
public function add(string $key, string $value): void
{
self::$vars[$key] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
}
Expand Down
25 changes: 14 additions & 11 deletions src/Api/Options.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

namespace Dwnload\WpSettingsApi\Api;

Expand All @@ -7,9 +8,9 @@
use function array_key_exists;
use function get_option;
use function is_array;
use function str_contains;
use function str_repeat;
use function strlen;
use function strpos;
use function substr;

/**
Expand All @@ -26,10 +27,8 @@ class Options
* @param mixed $default (Optional) Default value if option is not found.
* Defaults to an empty string.
* @return mixed
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
*/
public static function getOption(string $option_key, string $section_id = null, $default = '')
public static function getOption(string $option_key, ?string $section_id = null, mixed $default = ''): mixed
{
if (empty($section_id)) {
$section_id = self::getSectionId($option_key);
Expand All @@ -43,24 +42,27 @@ public static function getOption(string $option_key, string $section_id = null,
* Get the full settings Section ID array.
* @param string $section_id The Section object ID the option belongs too.
* @return mixed Value set for the option. Defaults to an empty array.
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
*/
public static function getOptions(string $section_id)
public static function getOptions(string $section_id): mixed
{
return get_option($section_id, []);
}

/**
* Get the obfuscated option value.
* @param string $option_key Settings field key name in the section option array.
* @param string $section_id The Section object ID the option belongs too.
* @param mixed $default (Optional) Default value if option is not found.
* Defaults to an empty string.
* @param int $len (Optional) The Length of the un-obfuscated string. Defaults to `6`.
* @return mixed|string
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
*/
public static function getObfuscatedOption(string $option_key, string $section_id, $default = '', int $len = 6)
{
public static function getObfuscatedOption(
string $option_key,
string $section_id,
mixed $default = '',
int $len = 6
): mixed {
$value = self::getOption($option_key, $section_id, $default);

if (!empty($value)) {
Expand All @@ -77,10 +79,11 @@ public static function getObfuscatedOption(string $option_key, string $section_i
*/
public static function isObfuscated(string $value): bool
{
return strpos($value, '****') !== false;
return str_contains($value, '****');
}

/**
* Get the Section ID by option key.
* @param string $option_key
* @return string
*/
Expand Down
16 changes: 9 additions & 7 deletions src/Api/PluginSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
/**
* Class PluginInfo
* @package Dwnload\WpSettingsApi\Api
* phpcs:disable Generic.Commenting.DocComment.MissingShort
* phpcs:disable Squiz.Commenting.FunctionComment.Missing
*/
class PluginSettings extends BaseModel
{
Expand Down Expand Up @@ -43,7 +45,7 @@ public function getDomain(): string
return $this->domain;
}

protected function setDomain(string $domain)
protected function setDomain(string $domain): void
{
$this->domain = $domain;
}
Expand All @@ -53,7 +55,7 @@ public function getFile(): string
return $this->file;
}

protected function setFile(string $file)
protected function setFile(string $file): void
{
$this->file = $file;
}
Expand All @@ -68,7 +70,7 @@ public function getMenuSlug(): string
return $this->menu_slug ?? $this->domain;
}

protected function setMenuSlug(string $slug)
protected function setMenuSlug(string $slug): void
{
$this->menu_slug = $slug;
}
Expand All @@ -78,7 +80,7 @@ public function getMenuTitle(): string
return $this->menu_title;
}

protected function setMenuTitle(string $title)
protected function setMenuTitle(string $title): void
{
$this->menu_title = $title;
}
Expand All @@ -88,7 +90,7 @@ public function getPageTitle(): string
return $this->page_title;
}

protected function setPageTitle(string $title)
protected function setPageTitle(string $title): void
{
$this->page_title = $title;
}
Expand All @@ -98,7 +100,7 @@ public function getPrefix(): string
return $this->prefix;
}

protected function setPrefix(string $prefix)
protected function setPrefix(string $prefix): void
{
$this->prefix = $prefix;
}
Expand All @@ -108,7 +110,7 @@ public function getVersion(): string
return $this->version;
}

protected function setVersion(string $version)
protected function setVersion(string $version): void
{
$this->version = $version;
}
Expand Down
20 changes: 9 additions & 11 deletions src/Api/Sanitize.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Dwnload\WpSettingsApi\Api;

use Dwnload\WpSettingsApi\Settings\FieldManager;

/**
* Class Sanitize
*
* @package Dwnload\WpSettingsApi\Api
*/
class Sanitize
Expand All @@ -17,16 +18,12 @@ class Sanitize
* If the value is obscured (meaning it contains as least 4 stars "*****" in a row, we need
* to get the value from the database instead of the new incoming value from the settings view
* so that the obscured setting doesn't override the actual value in the DB.
*
* @param mixed $value
* @param array $options
* @param string $option_slug
*
* @return mixed
* phpcs:disable Inpsyde.CodeQuality.ArgumentTypeDeclaration.NoArgumentType
* phpcs:disable Inpsyde.CodeQuality.ReturnTypeDeclaration.NoReturnType
*/
public static function sanitizeObfuscated($value, array $options, string $option_slug)
public static function sanitizeObfuscated(mixed $value, array $options, string $option_slug): mixed
{
$section_id = self::getSectionId($option_slug);

Expand All @@ -39,16 +36,17 @@ public static function sanitizeObfuscated($value, array $options, string $option

/**
* Gets the Section ID of the option.
*
* @param string $option_slug
*
* @return string
*/
private static function getSectionId(string $option_slug): string
{
// Iterate over registered fields and see if we can find proper callback
// Iterate over registered fields and see if we can find proper callback.
foreach (FieldManager::getFields() as $fields) {
/** @var SettingField $field */
/**
* Filed object.
* @var SettingField $field
*/
foreach ($fields as $field) {
if ($field->getName() === $option_slug) {
return $field->getSectionId();
Expand Down
Loading