PluginProbe
LoginPress | wp-login Custom Login Page Customizer / trunk
LoginPress | wp-login Custom Login Page Customizer vtrunk
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.2 1.0.20 1.0.21 1.0.22 1.0.23 1.0.3 1.0.4 All 118 releases
loginpress / include / customizer-validation.php

customizer-validation.php in LoginPress | wp-login Custom Login Page Customizer trunk, at include/customizer-validation.php

177 lines 5.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * LoginPress Customizer Validation Functions.
4 *
5 * Customizer: Sanitization Callbacks.
6 *
7 * This file demonstrates how to define sanitization callback functions for various data types.
8 *
9 * @package LoginPress
10 * @since 1.1.16
11 * @version 6.2.0
12 */
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 /**
19 * Checkbox sanitization callback example.
20 *
21 * Sanitization callback for 'checkbox' type controls. This callback sanitizes `$checked`
22 * as a boolean value, either TRUE or FALSE.
23 *
24 * @param bool $checked Whether the checkbox is checked.
25 * @return bool Whether the checkbox is checked.
26 */
27 function loginpress_sanitize_checkbox( $checked ) {
28 // Boolean check.
29 return ( true === $checked );
30 }
31
32 /**
33 * Select sanitization callback example.
34 *
35 * - Sanitization: select
36 * - Control: select, radio
37 *
38 * Sanitization callback for 'select' and 'radio' type controls. This callback sanitizes `$input`
39 * as a slug, and then validates `$input` against the choices defined for the control.
40 *
41 * @since 1.1.16
42 * @see sanitize_key() https://developer.wordpress.org/reference/functions/sanitize_key/
43 * @see $wp_customize->get_control() https://developer.wordpress.org/reference/classes/wp_customize_manager/get_control/
44 *
45 * @param string $input Slug to sanitize.
46 * @param WP_Customize_Setting $setting Setting instance.
47 * @return string Sanitized slug if it is a valid choice; otherwise, the setting default.
48 */
49 function loginpress_sanitize_select( $input, $setting ) {
50
51 // Ensure input is a slug.
52 $input = sanitize_key( $input );
53
54 // Get list of choices from the control associated with the setting.
55 $control = $setting->manager->get_control( $setting->id );
56 $choices = $control ? $control->choices : array();
57
58 // If the input is a valid key, return it; otherwise, return the default.
59 return ( array_key_exists( $input, $choices ) ? $input : $setting->default );
60 }
61
62 /**
63 * Image sanitization callback example.
64 *
65 * Checks the image's file extension and mime type against a whitelist. If they're allowed,
66 * send back the filename, otherwise, return the setting default.
67 *
68 * - Sanitization: image file extension
69 * - Control: text, WP_Customize_Image_Control
70 *
71 * @see wp_check_filetype() https://developer.wordpress.org/reference/functions/wp_check_filetype/
72 *
73 * @param string $image Image filename.
74 * @param WP_Customize_Setting $setting Setting instance.
75 * @return string The image filename if the extension is allowed; otherwise, the setting default.
76 *
77 * @since 1.1.17
78 *
79 * @version 3.0.0
80 */
81 function loginpress_sanitize_image( $image, $setting ) {
82
83 /**
84 * Array of valid image file types.
85 *
86 * The array includes image mime types that are included in wp_get_mime_types()
87 */
88 $mimes = array(
89 'jpg|jpeg|jpe' => 'image/jpeg',
90 'gif' => 'image/gif',
91 'png' => 'image/png',
92 'bmp' => 'image/bmp',
93 'tif|tiff' => 'image/tiff',
94 'ico' => 'image/x-icon',
95 );
96
97 // Allowed svg mime type in version 1.2.2.
98 $allowed_mime = get_allowed_mime_types();
99
100 /**
101 * Filter the list of mime types that are allowed for uploads.
102 *
103 * @since 1.6.1
104 */
105 $extra_mimes = array(
106 'svg' => 'image/svg+xml', // Allowed svg mime type in version 1.2.2.
107 'webp' => 'image/webp', // Allowed webp mime type in version 1.6.1.
108 );
109
110 foreach ( $extra_mimes as $key => $value ) {
111 $mime_check = isset( $allowed_mime[ $key ] ) ? true : false;
112 if ( $mime_check ) {
113 $allow_mime = array( $key => $value );
114 $mimes = array_merge( $mimes, $allow_mime );
115 }
116 }
117
118 $file_type = false;
119
120 /**
121 * Return an array with file extension and mime_type.
122 *
123 * @since 3.0.0
124 * @version 3.0.3
125 */
126 if ( ! empty( $image ) ) {
127
128 // Return an array with file extension and mime_type.
129 $file = wp_check_filetype( $image, $mimes );
130
131 // If $image has a valid mime_type, return it; otherwise, return the default.
132 return ( $file['ext'] ? $image : loginpress_image_content_type( $image, $mimes, $setting ) );
133 }
134
135 return $setting->default;
136 }
137
138 /**
139 * If CDN is being used get sanitization option.
140 *
141 * @param string $image The image URL.
142 * @param array<string, string> $mimes The mime type allowed.
143 * @param object $setting The settings object.
144 *
145 * @version 3.0.2
146 * @return mixed The images based on content type.
147 */
148 function loginpress_image_content_type( $image, $mimes, $setting ) {
149 $headers = get_headers( $image, true );
150 $content_type = false;
151 $file_type = false;
152 $content_types_can = array( 'Content-Type', 'content-type' );
153 foreach ( $content_types_can as $type ) {
154 if ( isset( $headers[ $type ] ) && ! empty( $headers[ $type ] ) ) {
155 $content_type = $headers[ $type ];
156 break;
157 }
158 }
159
160 if ( $content_type ) {
161
162 $file_type = in_array( $content_type, $mimes, true );
163
164 if ( is_array( $content_type ) ) {
165 foreach ( $content_type as $type ) {
166 $file_type = $type ? in_array( $type, $mimes, true ) : false;
167 if ( $file_type ) {
168 break;
169 }
170 }
171 }
172 }
173
174 // If $image has a valid mime_type, return it; otherwise, return the default.
175 return ( $file_type ? $image : ( isset( $setting->default ) ? $setting->default : '' ) );
176 }
177