%1$s', 'advanced-responsive-video-embedder' ), esc_html( $id_or_url ), ); arve_errors()->add( 'validate_thumbnail', $error_msg, $id_or_url ); return ''; } /** * Validates a URL and returns the validated URL or an error message. Upgrades // to https:// if needed. * * @param string $arg_name The name of the argument being validated. * @param string $url The value of the argument being validated. Can be an URL or a HTML string with the a embed code. * @return string The validated URL or an error message. */ function validate_url( string $arg_name, string $url ): string { if ( '' === $url ) { return $url; } $url = valid_url( $url ); if ( ! $url ) { // invalid url $error_msg = sprintf( // Translators: 1 URL 2 Attr name __( 'Invalid URL %1$s in %2$s', 'advanced-responsive-video-embedder' ), esc_html( $url ), esc_html( $arg_name ) ); arve_errors()->add( "validate_url $arg_name", $error_msg ); } return (string) $url; } /** * @param mixed $value */ function validate_type_bool( string $attr_name, $value ): bool { if ( is_bool( $value ) ) { return $value; } if ( in_array( $value, [ 'true', '1', 'y', 'yes', 'on', ], true ) ) { return true; } if ( in_array( $value, [ 'false', '0', 'n', 'no', 'off', ], true ) ) { return false; } $error_code = "validate_bool $attr_name"; arve_errors()->add( $error_code, sprintf( // Translators: %1$s = attribute name, %2$s = attribute value __( '%1$s %2$s not valid', 'advanced-responsive-video-embedder' ), esc_html( $attr_name ), esc_html( $value ) ), compact( 'attr_name', 'value' ) ); return false; } function validate_align( string $align ): string { switch ( $align ) { case '': case 'none': return ''; case 'left': case 'right': case 'center': case 'wide': case 'full': return $align; } arve_errors()->add( 'validate_align', // Translators: %s is align value sprintf( __( 'Align %s not valid', 'advanced-responsive-video-embedder' ), esc_html( $align ) ) ); return ''; } function validate_aspect_ratio( ?string $aspect_ratio ): ?string { // first time we set it it will be an empty string if ( '' === $aspect_ratio || null === $aspect_ratio ) { return $aspect_ratio; } $ratio = explode( ':', $aspect_ratio ); if ( empty( $ratio[0] ) || false === filter_var( $ratio[0], FILTER_VALIDATE_FLOAT ) || empty( $ratio[1] ) || false === filter_var( $ratio[1], FILTER_VALIDATE_FLOAT ) ) { arve_errors()->add( 'validate_aspect_ratio', // Translators: %s is aspect_ratio value sprintf( __( 'Aspect ratio %s is not valid', 'advanced-responsive-video-embedder' ), $aspect_ratio ), ); return '16:9'; } // valid aspect ratio return $aspect_ratio; } /** * @param mixed $height * * @return mixed */ function validate_height( $height ) { if ( ! is_numeric( $height ) ) { arve_errors()->add( 'validate_height', // Translators: attribute sprintf( __( 'Height not numeric', 'advanced-responsive-video-embedder' ), $height ) ); return 0; } return $height; } /** * @param mixed $value */ function validate_type_int( string $prop_name, $value ): int { if ( is_int( $value ) ) { return $value; } if ( is_string( $value ) && ctype_digit( $value ) ) { return (int) $value; } arve_errors()->add( "validate_int $prop_name", sprintf( // translators: attribute name, value __( '%1$s: %2$s is not valid', 'advanced-responsive-video-embedder' ), $prop_name, $value ) ); return 0; }