Yoast\WP\SEO\Abilities\Application

Archive_And_Special_Page_SEO_Settings_Updater::update │ public │ Yoast 1.0

Updates the global settings of a target and returns its new state.

Only fields present in the input are changed (patch semantics). Fields the target does not have, or that cannot be saved, are reported in a warning, while the other fields are still saved. A post type is only used for the archive of a content type, and ignored for the other targets.

Метод класса: Archive_And_Special_Page_SEO_Settings_Updater{}

Хуков нет.

Возвращает

array<string, mixed>|WP_Error. The new global settings of the target, plus a warning when some fields could not be changed, or an error when the target cannot be changed or no field is provided.

Использование

$Archive_And_Special_Page_SEO_Settings_Updater = new Archive_And_Special_Page_SEO_Settings_Updater();
$Archive_And_Special_Page_SEO_Settings_Updater->update( $input );
$input(array<string, mixed>) (обязательный)
The target, the post type for the archive of a content type, and the fields to change.

Код Archive_And_Special_Page_SEO_Settings_Updater::update() Yoast 28.9

public function update( array $input ) {
	try {
		$target = $this->target_repository->get( (string) ( $input['target'] ?? '' ), (string) ( $input['post_type'] ?? '' ) );
		$target->ensure_available();
	} catch ( Unavailable_Global_Setting_Target_Exception $exception ) {
		return new WP_Error( $exception->get_error_code(), $exception->getMessage(), [ 'status' => $exception->getCode() ] );
	}

	$fields = $this->field_map->get_fields();

	// The post type identifies the target rather than being a setting, so the input schema cannot catch this.
	if ( \array_intersect_key( $fields, $input ) === [] ) {
		return new WP_Error(
			'yoast_seo_no_settings',
			\__( 'Provide at least one setting to change.', 'wordpress-seo' ),
			[ 'status' => 400 ],
		);
	}

	$warnings = [];
	$result   = $this->update_fields( $fields, $this->field_map->get_validators(), $input, [ 'target' => $target ] );

	if ( $result['warning'] !== null ) {
		$warnings[] = $result['warning'];
	}

	if ( $result['saved_any'] && ! $target->is_enabled() ) {
		$warnings[] = \__( 'The settings were saved, but these pages are disabled on this site, so the settings have no effect until the pages are enabled in the Yoast SEO settings.', 'wordpress-seo' );
	}

	$data = $this->collector->get_target_settings( $target );

	if ( $warnings !== [] ) {
		$data['warning'] = \implode( ' ', $warnings );
	}

	return $data;
}