Api
1 day ago
Container
2 years ago
Deprecated
2 years ago
Divi
1 year ago
Elementor
7 months ago
Email
3 years ago
Email 2
2 years ago
Mioweb
1 year ago
Model
1 year ago
Repository
3 weeks ago
Service
1 day ago
Templates
2 years ago
Utils
1 day ago
Utils 2
2 years ago
services 2
2 years ago
styles 2
2 years ago
Bootstrap.php
1 day ago
EmailTemplatesProvider.php
3 years ago
FapiApi.php
2 years ago
FapiClients.php
2 years ago
FapiLevels.php
2 years ago
FapiMemberPlugin.php
1 year ago
FapiMemberTools.php
2 years ago
FapiMembership.php
2 years ago
FapiMembershipLoader.php
2 years ago
FapiSanitization.php
2 years ago
FapiTermEnvelope.php
4 years ago
FapiUserUtils.php
3 years ago
FapiSanitization.php
244 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember; |
| 4 | |
| 5 | use FapiMember\Utils\PostTypeHelper; |
| 6 | use RuntimeException; |
| 7 | use WP_Post; |
| 8 | use function in_array; |
| 9 | use function is_numeric; |
| 10 | |
| 11 | final class FapiSanitization { |
| 12 | |
| 13 | |
| 14 | const POST = 'POST'; |
| 15 | const GET = 'GET'; |
| 16 | |
| 17 | const VALID_LEVEL_ID = 'validLevelId'; |
| 18 | const VALID_PAGE_IDS = 'validPageIds'; |
| 19 | const VALID_PAGE_ID = 'validPageId'; |
| 20 | const ANY_STRING = 'anyString'; |
| 21 | const CHECKBOX = 'checkBox'; |
| 22 | const SINGLE_INT = 'singleInt'; |
| 23 | const INT_LIST = 'intList'; |
| 24 | const STR_LIST = 'strList'; |
| 25 | const VALID_EMAIL_TYPE = 'validEmailType'; |
| 26 | const VALID_OTHER_PAGE_TYPE = 'validOtherPageType'; |
| 27 | const VALID_DIRECTION = 'validDirection'; |
| 28 | const DATE = 'date'; |
| 29 | |
| 30 | public $fapiLevels; |
| 31 | |
| 32 | /** |
| 33 | * @param FapiLevels $fapiLevels |
| 34 | */ |
| 35 | public function __construct( $fapiLevels ) { |
| 36 | $this->fapiLevels = $fapiLevels; |
| 37 | } |
| 38 | |
| 39 | public function loadPostValue( $key, $sanitizer, $default = null ) { |
| 40 | return $this->loadFormValue( self::POST, $key, $sanitizer, $default ); |
| 41 | } |
| 42 | |
| 43 | public function loadFormValue( $method, $key, $sanitizer, $default = null ) { |
| 44 | switch ( $method ) { |
| 45 | case self::GET: |
| 46 | $arr = $_GET; |
| 47 | break; |
| 48 | case self::POST: |
| 49 | $arr = $_POST; |
| 50 | break; |
| 51 | default: |
| 52 | throw new RuntimeException( 'Not implemented method.' ); |
| 53 | } |
| 54 | |
| 55 | $raw = ( isset( $arr[ $key ] ) ) ? $arr[ $key ] : $default; |
| 56 | |
| 57 | if ( $raw === null && $sanitizer[1] !== 'checkBox') { |
| 58 | // input is missing |
| 59 | return null; |
| 60 | } |
| 61 | |
| 62 | if ( ! is_callable( $sanitizer ) ) { |
| 63 | throw new RuntimeException( 'Sanitizer should be callable.' ); |
| 64 | } |
| 65 | |
| 66 | return $sanitizer( $raw, $default ); |
| 67 | } |
| 68 | |
| 69 | public function loadGetValue( $key, $sanitizer, $default = null ) { |
| 70 | return $this->loadFormValue( self::GET, $key, $sanitizer, $default ); |
| 71 | } |
| 72 | |
| 73 | public function validLevelId( $input, $default ) { |
| 74 | $levelIds = $this->fapiLevels->allIds(); |
| 75 | if ( in_array( (int) $input, $levelIds, true ) ) { |
| 76 | return (int) $input; |
| 77 | } |
| 78 | |
| 79 | return $default; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param array<int> $input |
| 84 | * @return array<int> |
| 85 | */ |
| 86 | public function validLevelIds( array $input ) { |
| 87 | $levelIds = $this->fapiLevels->allIds(); |
| 88 | $out = array(); |
| 89 | |
| 90 | foreach ( $levelIds as $levelId ) { |
| 91 | if ( ! in_array( $levelId, $input, true ) ) { |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | $out[] = $levelId; |
| 96 | } |
| 97 | |
| 98 | return $out; |
| 99 | } |
| 100 | |
| 101 | public function validPageIds( $input, $default ) { |
| 102 | if ( ! is_array( $input ) ) { |
| 103 | throw new RuntimeException( 'This sanitizer only accepts array.' ); |
| 104 | } |
| 105 | |
| 106 | $pages = get_posts( |
| 107 | array( |
| 108 | 'post_type' => PostTypeHelper::getSupportedPostTypes(), |
| 109 | 'post_status' => array( 'publish' ), |
| 110 | 'numberposts' => -1, |
| 111 | ) |
| 112 | ); |
| 113 | $pageIds = array_reduce( |
| 114 | $pages, |
| 115 | static function ( $carry, $one ) { |
| 116 | /** |
| 117 | * @var WP_Post $one |
| 118 | */ |
| 119 | $carry[] = (int) $one->ID; |
| 120 | |
| 121 | return $carry; |
| 122 | }, |
| 123 | array() |
| 124 | ); |
| 125 | |
| 126 | $valid = array_filter( |
| 127 | $input, |
| 128 | static function ( $one ) use ( $pageIds ) { |
| 129 | return in_array( (int) $one, $pageIds, true ); |
| 130 | } |
| 131 | ); |
| 132 | |
| 133 | return array_map( 'intval', $input ); |
| 134 | } |
| 135 | |
| 136 | public function anyString( $input, $default ) { |
| 137 | if ( (string) $input === '' ) { |
| 138 | return $default; |
| 139 | } |
| 140 | |
| 141 | return (string) $input; |
| 142 | } |
| 143 | |
| 144 | public function singleInt( $input, $default ) { |
| 145 | if ( is_numeric( $input ) ) { |
| 146 | return intval( $input ); |
| 147 | } |
| 148 | |
| 149 | return $default; |
| 150 | } |
| 151 | |
| 152 | public function checkBox($input) { |
| 153 | if ($input === 'on') { |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @param array<mixed> $input |
| 162 | * @return array<int> |
| 163 | */ |
| 164 | public function intList( array $input ) { |
| 165 | $out = array(); |
| 166 | |
| 167 | foreach ( $input as $key => $value ) { |
| 168 | if ( ! is_numeric( $value ) ) { |
| 169 | continue; |
| 170 | } |
| 171 | |
| 172 | $out[ $key ] = (int) $value; |
| 173 | } |
| 174 | |
| 175 | return $out; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @param array<mixed> $input |
| 180 | * @return array<string> |
| 181 | */ |
| 182 | public function strList( array $input ) { |
| 183 | $out = array(); |
| 184 | |
| 185 | foreach ( $input as $key => $value ) { |
| 186 | if ( ! is_string( $value ) ) { |
| 187 | continue; |
| 188 | } |
| 189 | |
| 190 | $out[ $key ] = (string) $value; |
| 191 | } |
| 192 | |
| 193 | return $out; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | public function validEmailType( $input, $default ) { |
| 198 | if ( in_array( $input, FapiLevels::$emailTypes, true ) ) { |
| 199 | return $input; |
| 200 | } |
| 201 | |
| 202 | return $default; |
| 203 | } |
| 204 | |
| 205 | public function validOtherPageType( $input, $default ) { |
| 206 | if ( in_array( $input, FapiLevels::$pageTypes, true ) ) { |
| 207 | return $input; |
| 208 | } |
| 209 | |
| 210 | return $default; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * @param string $input |
| 215 | * @param string $default |
| 216 | * @return string |
| 217 | */ |
| 218 | public function validDirection( $input, $default ) { |
| 219 | if ( in_array( $input, array( 'up', 'down' ) ) ) { |
| 220 | return $input; |
| 221 | } |
| 222 | |
| 223 | return $default; |
| 224 | } |
| 225 | |
| 226 | public function validPageId( $input, $default ) { |
| 227 | $pages = get_posts( |
| 228 | array( |
| 229 | 'post_type' => PostTypeHelper::getSupportedPostTypes(), |
| 230 | 'post_status' => array( 'publish' ), |
| 231 | 'numberposts' => -1, |
| 232 | 'include' => array( $input ), |
| 233 | ) |
| 234 | ); |
| 235 | |
| 236 | if ( count( $pages ) > 0 ) { |
| 237 | return (int) $input; |
| 238 | } |
| 239 | |
| 240 | return $default; |
| 241 | } |
| 242 | |
| 243 | } |
| 244 |