HEX
Server: LiteSpeed
System: Linux server315.web-hosting.com 4.18.0-553.54.1.lve.el8.x86_64 #1 SMP Wed Jun 4 13:01:13 UTC 2025 x86_64
User: globfdxw (6114)
PHP: 8.1.34
Disabled: NONE
Upload Files
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;
	}
}