HEX
Server: Apache/2.4.62 (Debian)
System: Linux plxsite 6.8.0-47-generic #47-Ubuntu SMP PREEMPT_DYNAMIC Fri Sep 27 21:40:26 UTC 2024 x86_64
User: root (0)
PHP: 8.1.30
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/plugins/admin-menu-editor/customizables/Schemas/Url.php
<?php

namespace YahnisElsts\AdminMenuEditor\Customizable\Schemas;

class Url extends StringSchema {
	public function parse($value, $errors = null, $stopOnFirstError = false) {
		$parsedValue = parent::parse($value, $errors, $stopOnFirstError);
		if ( ($parsedValue === null) || is_wp_error($parsedValue) ) {
			return $parsedValue;
		}

		//Optionally, allow an empty string.
		if ( ($parsedValue === '') && ($this->getMinLength() === 0) ) {
			return $parsedValue;
		}

		$filteredValue = filter_var($parsedValue, FILTER_VALIDATE_URL);
		if ( $filteredValue === false ) {
			return self::addError($errors, 'invalid_url', 'Value must be a valid URL');

		}

		$convertedValue = esc_url_raw($filteredValue);
		if ( empty($convertedValue) ) {
			//esc_url() documentation says it returns an empty string if the protocol
			//is not one of the allowed protocols, but I'm not 100% sure if that is
			//the *only* situation where it might return an empty string.
			return self::addError($errors, 'invalid_protocol', 'Invalid protocol or a malformed URL');
		}

		return $convertedValue;
	}

	public function getSimplifiedDataType() {
		return 'url';
	}
}