Current File : /dom405686/wp-content/plugins/wordpress-seo/src/abilities/application/abstract-fields-updater.php
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\Abilities\Application;
/**
* Validates and saves the fields an ability changes, one at a time.
*
* Fields that cannot be applied or saved, or are saved in a sanitized form, are collected in a single warning, while
* the other fields are still saved.
* How a field is saved is up to the extending class.
*
* @internal Backs the abilities that update settings. Not meant to be used outside of Yoast SEO, so it can change at
* any time without notice.
*/
abstract class Abstract_Fields_Updater {
use Schema_Type_Cast_Trait;
// phpcs:disable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint -- The fields added through filters can hold any value.
/**
* Saves a single field.
*
* @param string $field_name The name of the field.
* @param mixed $value The value to save.
* @param array<string, mixed> $context What the extending class needs to save the field, as passed to
* update_fields().
*
* @return bool Whether the field was saved as provided.
*/
abstract protected function save_field( string $field_name, $value, array $context ): bool;
/**
* Returns the stored value of a single field.
*
* @param string $field_name The name of the field.
* @param array<string, mixed> $context What the extending class needs to read the field, as passed to
* update_fields().
*
* @return mixed The stored value.
*/
abstract protected function get_field_value( string $field_name, array $context );
/**
* Validates and saves the fields present in the input (patch semantics).
*
* @param array<string, array<string, mixed>> $fields The JSON schema of each field, keyed by field name.
* @param array<string, callable> $validators The validator of each field that has one, keyed by field
* name. It receives the value and the field name, and
* returns a warning to skip saving the field.
* @param array<string, mixed> $input The fields to change, keyed by field name.
* @param array<string, mixed> $context What the extending class needs to save the fields.
*
* @return array{warning: string|null, saved_any: bool} A warning when some fields could not be changed, and
* whether any field was saved.
*/
protected function update_fields( array $fields, array $validators, array $input, array $context = [] ): array {
$warnings = [];
$saved_any = false;
$rejected_any = false;
foreach ( \array_keys( \array_intersect_key( $fields, $input ) ) as $field_name ) {
$update = $this->update_field( $validators, $field_name, $input[ $field_name ], $context );
if ( $update['saved'] ) {
$saved_any = true;
}
else {
$rejected_any = true;
}
if ( $update['warning'] !== null ) {
$warnings[] = $update['warning'];
}
}
if ( $rejected_any ) {
$warnings[] = ( $saved_any ) ? \__( 'The other settings were saved.', 'wordpress-seo' ) : \__( 'No other settings were saved.', 'wordpress-seo' );
}
$warning = ( $warnings !== [] ) ? \implode( ' ', $warnings ) : null;
return [
'warning' => $warning,
'saved_any' => $saved_any,
];
}
/**
* Validates and saves a single field.
*
* @param array<string, callable> $validators The validator of each field that has one, keyed by field name.
* @param string $field_name The name of the field.
* @param mixed $value The value to save.
* @param array<string, mixed> $context What the extending class needs to save the field.
*
* @return array{saved: bool, warning: string|null} Whether the field was saved, and a warning when it was not
* saved as provided.
*/
protected function update_field( array $validators, string $field_name, $value, array $context ): array {
$warning = $this->validate_field( $validators, $field_name, $value );
if ( $warning !== null ) {
return [
'saved' => false,
'warning' => $warning,
];
}
$previous_value = $this->get_field_value( $field_name, $context );
if ( $this->save_field( $field_name, $value, $context ) ) {
return [
'saved' => true,
'warning' => null,
];
}
// A value that got sanitized into something else is stored all the same, which only the stored value tells apart from a rejected one.
if ( $this->get_field_value( $field_name, $context ) !== $previous_value ) {
return [
'saved' => true,
'warning' => \sprintf(
/* translators: %s expands to the name of a setting. */
\__( 'The %s setting was saved after being cleaned up, so its saved value differs from the one provided.', 'wordpress-seo' ),
$field_name,
),
];
}
return [
'saved' => false,
'warning' => \sprintf(
/* translators: %s expands to the name of a setting. */
\__( 'The %s setting could not be saved as provided, so its current value is returned.', 'wordpress-seo' ),
$field_name,
),
];
}
/**
* Validates a single field with its validator, if any.
*
* @param array<string, callable> $validators The validator of each field that has one, keyed by field name.
* @param string $field_name The name of the field.
* @param mixed $value The value to save.
*
* @return string|null A warning when the value cannot be saved, null otherwise.
*/
private function validate_field( array $validators, string $field_name, $value ): ?string {
if ( ! isset( $validators[ $field_name ] ) ) {
return null;
}
$warning = \call_user_func( $validators[ $field_name ], $value, $field_name );
// Anything but a non-empty string cannot be relayed as a warning, so the value is saved instead.
if ( ! \is_string( $warning ) || $warning === '' ) {
return null;
}
return $warning;
}
// phpcs:enable SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint.DisallowedMixedTypeHint
}
Distinguish Your Business From The Rest With The People That Make Up Your Team
We go Beyond a Resume to find high value, committed people
ready, willing, and able to add true value toward your organization's mission.
The right people make or break your business
We focus on resolving two types of challenges surrounding staffing and recruiting at Beyond a Resume: The first challenge comes when a business continues to grow, but does not have the full-time, internal, talent acquisition resources to hire the right people or talent necessary to keep up with their rate of progression as a company.
The second is a business that has a dedicated internal Human Resources Team, but requires outsourced assistance when the volume of people and talent needed exceeds short term internal capacity, OR the position(s) they seek to fill demand a specific skill set that is difficult to find, requiring added bandwidth.
In nearly any line of business, growth is a challenge in and of itself. Yet, the real problems arise in managing growth to ensure your progress remains profitable, sustainable, and repeatable. The biggest factor in this equation is usually found in your people. When you have the right people, success is unlimited. On the other hand, when you don’t have the right people in place or are having trouble finding enough of the right people, failure or stagnation is imminent.
The frustration that comes with the process of “hiring” can bring you to new levels of stress as the time cost involved eats away at your production elsewhere. If you have experienced this in the past, or are currently struggling with this issue, you’re certainly not alone. You may simply need the right partner to take the worst parts of this process off your plate so you can concentrate on your responsibilities, while we work diligently to reduce the noise and deliver only the ideal candidates worth your time and attention.
People can make or break your business and we go Beyond a Resume to find high value, committed people ready, willing, and able to add true value.