File: /home/globfdxw/www/wp-content/plugins/wpforms-google-calendar/src/Provider/Validation.php
<?php
namespace WPFormsGoogleCalendar\Provider;
/**
* Google Calendar validation.
*
* @since 1.0.0
*/
class Validation {
/**
* Add hooks.
*
* @since 1.0.0
*/
public function hooks(): void {
add_filter( 'wpforms_settings_defaults', [ $this, 'register_settings_messages' ] );
}
/**
* Register validation messages in settings.
*
* @since 1.0.0
*
* @param array $settings Array of current form settings.
*
* @return array
* @noinspection PhpMissingParamTypeInspection
*/
public function register_settings_messages( $settings ): array {
$key = $this->get_key( 'end-before-start' );
$settings['validation'][ $key ] = [
'id' => $key,
'name' => esc_html__( 'Start Time Later than End Time', 'wpforms-google-calendar' ),
'type' => 'text',
'default' => $this->get_default_message( 'end-before-start' ),
];
return $settings;
}
/**
* Get a default validation message for a specific key.
*
* @since 1.0.0
*
* @param string $key Validation key.
*
* @return string
*/
private function get_default_message( string $key ): string {
$messages = [
'end-before-start' => esc_html__( 'Start time must be earlier than End time. Please adjust your selection.', 'wpforms-google-calendar' ),
];
return $messages[ $key ] ?? esc_html__( 'Validation failed. Please check your input.', 'wpforms-google-calendar' );
}
/**
* Get a validation message for a specific key.
*
* @since 1.0.0
*
* @param string $key Validation key.
*
* @return string
*/
public function get_validation_message( string $key ): string {
$formatted_key = $this->get_key( $key );
return (string) wpforms_setting( $formatted_key, $this->get_default_message( $key ) );
}
/**
* Get the formatted validation key.
*
* @since 1.0.0
*
* @param string $key Validation key.
*
* @return string
*/
private function get_key( string $key ): string {
return 'validation-google-calendar-' . $key;
}
}