ApiService.php
7 months ago
ElementService.php
7 months ago
EmailService.php
7 months ago
FormService.php
7 months ago
LevelOrderService.php
7 months ago
LevelService.php
7 months ago
MembershipService.php
7 months ago
RedirectService.php
7 months ago
SanitizationService.php
7 months ago
StatisticsService.php
7 months ago
UserService.php
7 months ago
SanitizationService.php
263 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FapiMember\Service; |
| 4 | |
| 5 | use FapiMember\Container\Container; |
| 6 | use FapiMember\Model\Enums\Format; |
| 7 | use FapiMember\Model\Enums\Types\EmailType; |
| 8 | use FapiMember\Model\Enums\Types\ServicePageType; |
| 9 | use FapiMember\Repository\LevelRepository; |
| 10 | use FapiMember\Utils\DateTimeHelper; |
| 11 | use FapiMember\Utils\PostTypeHelper; |
| 12 | use WP_Post; |
| 13 | |
| 14 | class SanitizationService |
| 15 | { |
| 16 | private LevelRepository $levelRepository; |
| 17 | |
| 18 | public function __construct() |
| 19 | { |
| 20 | $this->levelRepository = Container::get(LevelRepository::class); |
| 21 | } |
| 22 | |
| 23 | public function validLevelId(mixed $input, mixed $default): mixed |
| 24 | { |
| 25 | if ($input === null) { |
| 26 | return $default; |
| 27 | } |
| 28 | |
| 29 | $input = (int) $input; |
| 30 | |
| 31 | if ($this->levelRepository->exists($input)) { |
| 32 | return $input; |
| 33 | } |
| 34 | |
| 35 | return $default; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @return array<int> |
| 40 | */ |
| 41 | public function validLevelIds(mixed $input): array |
| 42 | { |
| 43 | if (!is_array($input)) { |
| 44 | return []; |
| 45 | } |
| 46 | |
| 47 | $validLevelIds = []; |
| 48 | |
| 49 | foreach ($input as $levelId) { |
| 50 | if ($this->levelRepository->exists((int)$levelId)) { |
| 51 | $validLevelIds[] = (int) $levelId; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | return $validLevelIds; |
| 56 | } |
| 57 | |
| 58 | public function validPageId( $input, $default ): mixed |
| 59 | { |
| 60 | $pages = get_posts( |
| 61 | array( |
| 62 | 'post_type' => PostTypeHelper::getSupportedPostTypes(), |
| 63 | 'post_status' => array( 'publish' ), |
| 64 | 'numberposts' => -1, |
| 65 | 'include' => array( $input ), |
| 66 | ) |
| 67 | ); |
| 68 | |
| 69 | if ( count( $pages ) > 0 ) { |
| 70 | return (int) $input; |
| 71 | } |
| 72 | |
| 73 | return $default; |
| 74 | } |
| 75 | |
| 76 | public function validPageIds( $input, $default ): mixed |
| 77 | { |
| 78 | if (!is_array($input)) { |
| 79 | return []; |
| 80 | } |
| 81 | |
| 82 | $pages = get_posts( |
| 83 | array( |
| 84 | 'post_type' => PostTypeHelper::getSupportedPostTypes(), |
| 85 | 'post_status' => array( 'publish' ), |
| 86 | 'numberposts' => -1, |
| 87 | ) |
| 88 | ); |
| 89 | |
| 90 | $pageIds = array_reduce( |
| 91 | $pages, |
| 92 | static function ( $carry, $one ) { |
| 93 | /** |
| 94 | * @var WP_Post $one |
| 95 | */ |
| 96 | $carry[] = (int) $one->ID; |
| 97 | |
| 98 | return $carry; |
| 99 | }, |
| 100 | array() |
| 101 | ); |
| 102 | |
| 103 | $valid = array_filter( |
| 104 | $input, |
| 105 | static function ( $one ) use ( $pageIds ) { |
| 106 | return in_array( (int) $one, $pageIds, true ); |
| 107 | } |
| 108 | ); |
| 109 | |
| 110 | return array_map( 'intval', $input ); |
| 111 | } |
| 112 | |
| 113 | public function anyString( $input, $default ): mixed |
| 114 | { |
| 115 | if ( (string) $input === '' ) { |
| 116 | return $default; |
| 117 | } |
| 118 | |
| 119 | return (string) $input; |
| 120 | } |
| 121 | |
| 122 | public function singleInt($input, $default): mixed |
| 123 | { |
| 124 | if ( is_numeric( $input ) ) { |
| 125 | return intval( $input ); |
| 126 | } |
| 127 | |
| 128 | return $default; |
| 129 | } |
| 130 | |
| 131 | public function checkBox($input): mixed |
| 132 | { |
| 133 | if ($input === 'on') { |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @param array<mixed> $input |
| 142 | * @return array<int> |
| 143 | */ |
| 144 | public function intList( array $input ): mixed |
| 145 | { |
| 146 | $out = array(); |
| 147 | |
| 148 | foreach ( $input as $key => $value ) { |
| 149 | if ( ! is_numeric( $value ) ) { |
| 150 | continue; |
| 151 | } |
| 152 | |
| 153 | $out[ $key ] = (int) $value; |
| 154 | } |
| 155 | |
| 156 | return $out; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * @param array<mixed> $input |
| 162 | * @return array<string> |
| 163 | */ |
| 164 | public function strList(array $input): mixed |
| 165 | { |
| 166 | $out = array(); |
| 167 | |
| 168 | foreach ( $input as $key => $value ) { |
| 169 | if ( ! is_string( $value ) ) { |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | $out[ $key ] = (string) $value; |
| 174 | } |
| 175 | |
| 176 | return $out; |
| 177 | } |
| 178 | |
| 179 | public function validEmailType($input, $default): mixed |
| 180 | { |
| 181 | if (in_array($input, EmailType::getAvailableValues(), true )) { |
| 182 | return $input; |
| 183 | } |
| 184 | |
| 185 | return $default; |
| 186 | } |
| 187 | |
| 188 | public function validServicePageType($input, $default):mixed |
| 189 | { |
| 190 | if (in_array($input, ServicePageType::getAvailableValues(), true)) { |
| 191 | return $input; |
| 192 | } |
| 193 | |
| 194 | return $default; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @param string $input |
| 199 | * @param string $default |
| 200 | * @return string |
| 201 | */ |
| 202 | public function validDirection($input, $default): mixed |
| 203 | { |
| 204 | if (in_array($input, array('up', 'down'))) { |
| 205 | return $input; |
| 206 | } |
| 207 | |
| 208 | return $default; |
| 209 | } |
| 210 | |
| 211 | public function userProfileLevels(array $levels): array |
| 212 | { |
| 213 | if (!is_array($levels)) { |
| 214 | wp_die('Unknown input structure.'); |
| 215 | } |
| 216 | |
| 217 | $levels = array_filter( |
| 218 | $levels, |
| 219 | static function ($one) { |
| 220 | return (isset($one['check']) && $one['check'] === 'on'); |
| 221 | } |
| 222 | ); |
| 223 | |
| 224 | $membershipData = []; |
| 225 | |
| 226 | foreach ($levels as $id => $inputs) { |
| 227 | $registered = DateTimeHelper::createOrNull( |
| 228 | $inputs['registrationDate'] . 'T' . $inputs['registrationTime'], |
| 229 | Format::DATE_TIME_MINUTES, |
| 230 | ); |
| 231 | |
| 232 | if ($registered === null) { |
| 233 | $registered = DateTimeHelper::getNow(); |
| 234 | } |
| 235 | |
| 236 | $isUnlimited = false; |
| 237 | |
| 238 | if ( isset( $inputs['isUnlimited'] ) && $inputs['isUnlimited'] === 'on' ) { |
| 239 | $isUnlimited = true; |
| 240 | } |
| 241 | |
| 242 | if (isset($inputs['membershipUntil']) && $inputs['membershipUntil'] !== '') { |
| 243 | $until = DateTimeHelper::createOrNull( |
| 244 | $inputs['membershipUntil'] . 'T23:59:59', |
| 245 | Format::DATE_TIME, |
| 246 | ); |
| 247 | } else { |
| 248 | $until = null; |
| 249 | $isUnlimited = true; |
| 250 | } |
| 251 | |
| 252 | $membershipData[] = [ |
| 253 | 'level_id' => $id, |
| 254 | 'registered' => $registered, |
| 255 | 'until' => $until, |
| 256 | 'is_unlimited' => $isUnlimited, |
| 257 | ]; |
| 258 | } |
| 259 | |
| 260 | return $membershipData; |
| 261 | } |
| 262 | } |
| 263 |