class-bwt-connect-settings.php
7 months ago
class-bwt-connect.php
4 months ago
class-gsc-connect-settings.php
7 months ago
class-gsc-connect.php
4 months ago
class-main.php
7 months ago
class-sanitize.php
7 months ago
class-sitemap-fields.php
4 months ago
class-sitemap-news-fields.php
4 months ago
class-sitemap-news-settings.php
3 months ago
class-sitemap-news.php
7 months ago
class-sitemap-settings.php
4 months ago
class-sitemap.php
4 months ago
class-sanitize.php
202 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Settings Sanitization |
| 4 | * |
| 5 | * @package XML Sitemap & Google News |
| 6 | */ |
| 7 | |
| 8 | namespace XMLSF\Admin; |
| 9 | |
| 10 | /** |
| 11 | * Sanitization Class |
| 12 | */ |
| 13 | class Sanitize { |
| 14 | |
| 15 | /** |
| 16 | * Sanitize server setting |
| 17 | * |
| 18 | * @param string $save Setting. |
| 19 | * |
| 20 | * @return string |
| 21 | */ |
| 22 | public static function server( $save ) { |
| 23 | $sanitized = empty( $save ) || ! \in_array( $save, array( 'core', 'plugin' ), true ) ? \XMLSF\get_default_settings( 'server' ) : $save; |
| 24 | |
| 25 | return $sanitized; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * Sanitize server setting |
| 30 | * |
| 31 | * @param mixed $save Settings array or empty value. |
| 32 | * |
| 33 | * @return mixed |
| 34 | */ |
| 35 | public static function disabled_providers( $save ) { |
| 36 | // Nothing to do really... |
| 37 | |
| 38 | return $save; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Sanitize taxonomies |
| 43 | * |
| 44 | * @param array $save Settings array. |
| 45 | * |
| 46 | * @return array |
| 47 | */ |
| 48 | public static function taxonomies( $save ) { |
| 49 | // Nothing to do really... |
| 50 | |
| 51 | return $save; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Sanitize taxonomies settings |
| 56 | * |
| 57 | * Clears the term_modified data from the database when settings have changed. |
| 58 | * |
| 59 | * @param array $save Settings array. |
| 60 | * |
| 61 | * @return array |
| 62 | */ |
| 63 | public static function taxonomy_settings( $save ) { |
| 64 | $save = (array) $save; |
| 65 | |
| 66 | // Sanitize limit. |
| 67 | if ( ! empty( $save['limit'] ) && \is_numeric( $save['limit'] ) ) { |
| 68 | $save['limit'] = \XMLSF\sanitize_number( $save['limit'], 1, 50000, 0 ); |
| 69 | } |
| 70 | |
| 71 | return $save; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Sanitize authors |
| 76 | * |
| 77 | * @param array $save Settings array. |
| 78 | * |
| 79 | * @return array |
| 80 | */ |
| 81 | public static function authors( $save ) { |
| 82 | // Nothing to do really... |
| 83 | |
| 84 | return $save; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sanitize author settings |
| 89 | * |
| 90 | * Clears the term_modified data from the database when settings have changed. |
| 91 | * |
| 92 | * @param array $save Settings array. |
| 93 | * |
| 94 | * @return array |
| 95 | */ |
| 96 | public static function author_settings( $save ) { |
| 97 | $save = (array) $save; |
| 98 | |
| 99 | // Sanitize limit. |
| 100 | if ( ! empty( $save['limit'] ) && \is_numeric( $save['limit'] ) ) { |
| 101 | $save['limit'] = \XMLSF\sanitize_number( $save['limit'], 1, 50000, 0 ); |
| 102 | } |
| 103 | |
| 104 | return $save; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Sanitize post types |
| 109 | * |
| 110 | * @param array $save Settings array. |
| 111 | * |
| 112 | * @return array |
| 113 | */ |
| 114 | public static function post_types( $save ) { |
| 115 | return $save; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Sanitize post type settings |
| 120 | * |
| 121 | * @param array $save Settings array. |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | public static function post_type_settings( $save = array() ) { |
| 126 | $save = (array) $save; |
| 127 | |
| 128 | // Sanitize limit. |
| 129 | if ( ! empty( $save['limit'] ) && \is_numeric( $save['limit'] ) ) { |
| 130 | $save['limit'] = \XMLSF\sanitize_number( $save['limit'], 1, 50000, 0 ); |
| 131 | } |
| 132 | |
| 133 | // Sanitize priorities. |
| 134 | foreach ( $save as $post_type => $settings ) { |
| 135 | if ( ! is_array( $settings ) ) { |
| 136 | continue; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | return $save; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Sanitize custom sitemaps |
| 145 | * |
| 146 | * @param string $save Text field input. |
| 147 | * |
| 148 | * @return array |
| 149 | */ |
| 150 | public static function custom_sitemaps_settings( $save ) { |
| 151 | if ( empty( $save ) ) { |
| 152 | return ''; |
| 153 | } |
| 154 | |
| 155 | // Build sanitized output. |
| 156 | $input = explode( PHP_EOL, \sanitize_textarea_field( $save ) ); |
| 157 | $sanitized = array(); |
| 158 | |
| 159 | foreach ( $input as $line ) { |
| 160 | $line = \filter_var( \esc_url( \trim( $line ) ), FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED ); |
| 161 | if ( ! empty( $line ) ) { |
| 162 | $sanitized[] = $line; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | return ! empty( $sanitized ) ? $sanitized : ''; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Sanitize custom URLs |
| 171 | * |
| 172 | * @param string $save Text field input. |
| 173 | * |
| 174 | * @return array |
| 175 | */ |
| 176 | public static function custom_urls_settings( $save ) { |
| 177 | if ( empty( $save ) ) { |
| 178 | return ''; |
| 179 | } |
| 180 | |
| 181 | $input = \explode( PHP_EOL, \wp_strip_all_tags( $save ) ); |
| 182 | |
| 183 | // Build sanitized output. |
| 184 | $sanitized = array(); |
| 185 | foreach ( $input as $line ) { |
| 186 | if ( empty( $line ) ) { |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | $arr = \explode( ' ', \trim( $line ) ); |
| 191 | |
| 192 | $url = \filter_var( \esc_url( \trim( $arr[0] ) ), FILTER_VALIDATE_URL ); |
| 193 | |
| 194 | if ( ! empty( $url ) ) { |
| 195 | $sanitized[] = $url; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | return ! empty( $sanitized ) ? $sanitized : ''; |
| 200 | } |
| 201 | } |
| 202 |