Controllers
1 week ago
DigitalProducts
5 months ago
Emails
3 months ago
Models
1 month ago
PaymentMethods
1 month ago
Repositories
2 months ago
Services
4 months ago
CheckoutFields.php
1 month ago
CurrencyFormatter.php
9 months ago
Init.php
2 years ago
StatSync.php
1 year ago
index.php
3 years ago
CheckoutFields.php
445 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership; |
| 4 | |
| 5 | use ProfilePress\Core\Classes\ExtensionManager as EM; |
| 6 | use ProfilePress\Core\Classes\FormRepository; |
| 7 | use ProfilePress\Core\Classes\PROFILEPRESS_sql as PROFILEPRESS_sql; |
| 8 | use ProfilePress\Core\Membership\Controllers\CheckoutSessionData; |
| 9 | use ProfilePress\Core\ShortcodeParser\Builder\FieldsShortcodeCallback; |
| 10 | |
| 11 | class CheckoutFields |
| 12 | { |
| 13 | const DB_OPTION_NAME = 'ppress_checkout_fields'; |
| 14 | |
| 15 | const ACCOUNT_EMAIL_ADDRESS = 'ppmb_email'; |
| 16 | const ACCOUNT_CONFIRM_EMAIL_ADDRESS = 'ppmb_email2'; |
| 17 | const ACCOUNT_USERNAME = 'ppmb_username'; |
| 18 | const ACCOUNT_PASSWORD = 'ppmb_password'; |
| 19 | const ACCOUNT_CONFIRM_PASSWORD = 'ppmb_password2'; |
| 20 | const ACCOUNT_WEBSITE = 'ppmb_website'; |
| 21 | const ACCOUNT_NICKNAME = 'ppmb_nickname'; |
| 22 | const ACCOUNT_DISPLAY_NAME = 'ppmb_display_name'; |
| 23 | const ACCOUNT_FIRST_NAME = 'ppmb_first_name'; |
| 24 | const ACCOUNT_LAST_NAME = 'ppmb_last_name'; |
| 25 | const ACCOUNT_BIO = 'ppmb_bio'; |
| 26 | |
| 27 | /** we using ppress because the constants is the usermeta key/id */ |
| 28 | const BILLING_ADDRESS = 'ppress_billing_address'; |
| 29 | const BILLING_CITY = 'ppress_billing_city'; |
| 30 | const BILLING_COUNTRY = 'ppress_billing_country'; |
| 31 | const BILLING_STATE = 'ppress_billing_state'; |
| 32 | const BILLING_POST_CODE = 'ppress_billing_postcode'; |
| 33 | const BILLING_PHONE_NUMBER = 'ppress_billing_phone'; |
| 34 | const VAT_NUMBER = 'ppress_vat_number'; |
| 35 | |
| 36 | public static function logged_in_hidden_fields() |
| 37 | { |
| 38 | return apply_filters('ppress_checkout_logged_in_hidden_fields', [ |
| 39 | CheckoutFields::ACCOUNT_EMAIL_ADDRESS, |
| 40 | CheckoutFields::ACCOUNT_CONFIRM_EMAIL_ADDRESS, |
| 41 | CheckoutFields::ACCOUNT_PASSWORD, |
| 42 | CheckoutFields::ACCOUNT_CONFIRM_PASSWORD, |
| 43 | CheckoutFields::ACCOUNT_USERNAME |
| 44 | ]); |
| 45 | } |
| 46 | |
| 47 | public static function standard_account_info_fields() |
| 48 | { |
| 49 | return apply_filters('ppress_standard_account_info_fields', [ |
| 50 | self::ACCOUNT_EMAIL_ADDRESS => [ |
| 51 | 'label' => esc_html__('Email Address', 'wp-user-avatar'), |
| 52 | 'required' => 'true', |
| 53 | 'field_type' => 'email', |
| 54 | 'logged_in_hide' => 'false', |
| 55 | 'deletable' => 'false' |
| 56 | ], |
| 57 | self::ACCOUNT_CONFIRM_EMAIL_ADDRESS => [ |
| 58 | 'label' => esc_html__('Confirm Email Address', 'wp-user-avatar'), |
| 59 | 'required' => 'true', |
| 60 | 'field_type' => 'email', |
| 61 | 'logged_in_hide' => 'true', |
| 62 | 'deletable' => 'true' |
| 63 | ], |
| 64 | self::ACCOUNT_FIRST_NAME => [ |
| 65 | 'label' => esc_html__('First Name', 'wp-user-avatar'), |
| 66 | 'required' => 'true', |
| 67 | 'field_type' => 'text', |
| 68 | 'logged_in_hide' => 'false', |
| 69 | 'deletable' => 'false' |
| 70 | ], |
| 71 | self::ACCOUNT_LAST_NAME => [ |
| 72 | 'label' => esc_html__('Last Name', 'wp-user-avatar'), |
| 73 | 'required' => 'false', |
| 74 | 'field_type' => 'text', |
| 75 | 'logged_in_hide' => 'false', |
| 76 | 'deletable' => 'true' |
| 77 | ], |
| 78 | self::ACCOUNT_USERNAME => [ |
| 79 | 'label' => esc_html__('Username', 'wp-user-avatar'), |
| 80 | 'required' => 'true', |
| 81 | 'field_type' => 'text', |
| 82 | 'logged_in_hide' => 'true', |
| 83 | 'deletable' => 'true' |
| 84 | ], |
| 85 | self::ACCOUNT_PASSWORD => [ |
| 86 | 'label' => esc_html__('Password', 'wp-user-avatar'), |
| 87 | 'required' => 'true', |
| 88 | 'field_type' => 'password', |
| 89 | 'logged_in_hide' => 'true', |
| 90 | 'deletable' => 'false' |
| 91 | ], |
| 92 | self::ACCOUNT_CONFIRM_PASSWORD => [ |
| 93 | 'label' => esc_html__('Confirm Password', 'wp-user-avatar'), |
| 94 | 'required' => 'true', |
| 95 | 'field_type' => 'password', |
| 96 | 'logged_in_hide' => 'true', |
| 97 | 'deletable' => 'true' |
| 98 | ], |
| 99 | self::ACCOUNT_WEBSITE => [ |
| 100 | 'label' => esc_html__('Website', 'wp-user-avatar'), |
| 101 | 'required' => 'false', |
| 102 | 'field_type' => 'text', |
| 103 | 'logged_in_hide' => 'true', |
| 104 | 'deletable' => 'true' |
| 105 | ], |
| 106 | self::ACCOUNT_NICKNAME => [ |
| 107 | 'label' => esc_html__('Nickname', 'wp-user-avatar'), |
| 108 | 'required' => 'false', |
| 109 | 'field_type' => 'text', |
| 110 | 'logged_in_hide' => 'true', |
| 111 | 'deletable' => 'true' |
| 112 | ], |
| 113 | self::ACCOUNT_DISPLAY_NAME => [ |
| 114 | 'label' => esc_html__('Display Name', 'wp-user-avatar'), |
| 115 | 'required' => 'false', |
| 116 | 'field_type' => 'text', |
| 117 | 'logged_in_hide' => 'true', |
| 118 | 'deletable' => 'true' |
| 119 | ], |
| 120 | self::ACCOUNT_BIO => [ |
| 121 | 'label' => esc_html__('Biographical Info', 'wp-user-avatar'), |
| 122 | 'required' => 'false', |
| 123 | 'field_type' => 'textarea', |
| 124 | 'logged_in_hide' => 'true', |
| 125 | 'deletable' => 'true' |
| 126 | ], |
| 127 | ]); |
| 128 | } |
| 129 | |
| 130 | public static function standard_billing_fields() |
| 131 | { |
| 132 | return apply_filters('ppress_standard_billing_fields', [ |
| 133 | self::BILLING_ADDRESS => [ |
| 134 | 'label' => esc_html__('Street Address', 'wp-user-avatar'), |
| 135 | 'required' => 'true', |
| 136 | 'field_type' => 'text', |
| 137 | 'logged_in_hide' => 'false', |
| 138 | 'deletable' => 'true', |
| 139 | 'width' => 'full' |
| 140 | ], |
| 141 | self::BILLING_CITY => [ |
| 142 | 'label' => esc_html__('City', 'wp-user-avatar'), |
| 143 | 'required' => 'true', |
| 144 | 'field_type' => 'text', |
| 145 | 'logged_in_hide' => 'false', |
| 146 | 'deletable' => 'false', |
| 147 | 'width' => 'half' |
| 148 | ], |
| 149 | self::BILLING_COUNTRY => [ |
| 150 | 'label' => esc_html__('Country', 'wp-user-avatar'), |
| 151 | 'required' => 'true', |
| 152 | 'field_type' => 'country', |
| 153 | 'logged_in_hide' => 'false', |
| 154 | 'deletable' => 'false', |
| 155 | 'width' => 'half' |
| 156 | ], |
| 157 | self::BILLING_STATE => [ |
| 158 | 'label' => esc_html__('State / Province', 'wp-user-avatar'), |
| 159 | 'required' => 'true', |
| 160 | 'field_type' => 'text', |
| 161 | 'logged_in_hide' => 'false', |
| 162 | 'deletable' => 'true', |
| 163 | 'width' => 'one-third' |
| 164 | ], |
| 165 | self::BILLING_POST_CODE => [ |
| 166 | 'label' => esc_html__('Zip / Postal Code', 'wp-user-avatar'), |
| 167 | 'required' => 'true', |
| 168 | 'field_type' => 'text', |
| 169 | 'logged_in_hide' => 'false', |
| 170 | 'deletable' => 'false', |
| 171 | 'width' => 'one-third' |
| 172 | ], |
| 173 | self::BILLING_PHONE_NUMBER => [ |
| 174 | 'label' => esc_html__('Phone', 'wp-user-avatar'), |
| 175 | 'required' => 'true', |
| 176 | 'field_type' => 'tel', |
| 177 | 'logged_in_hide' => 'false', |
| 178 | 'deletable' => 'true', |
| 179 | 'width' => 'one-third' |
| 180 | ] |
| 181 | ]); |
| 182 | } |
| 183 | |
| 184 | public static function standard_custom_fields() |
| 185 | { |
| 186 | $fields = []; |
| 187 | |
| 188 | if (EM::is_premium()) { |
| 189 | |
| 190 | $db_custom_fields = PROFILEPRESS_sql::get_profile_custom_fields(); |
| 191 | $db_contact_infos = PROFILEPRESS_sql::get_contact_info_fields(); |
| 192 | |
| 193 | if ( ! empty($db_contact_infos)) { |
| 194 | foreach ($db_contact_infos as $key => $value) { |
| 195 | $fields[$key] = [ |
| 196 | 'label' => $value, |
| 197 | 'required' => 'false', |
| 198 | 'field_type' => 'text', |
| 199 | 'logged_in_hide' => 'true', |
| 200 | 'deletable' => 'true' |
| 201 | ]; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | if ( ! empty($db_custom_fields)) { |
| 206 | |
| 207 | foreach ($db_custom_fields as $db_custom_field) { |
| 208 | |
| 209 | $field_key = $db_custom_field['field_key']; |
| 210 | $field_type = sanitize_text_field($db_custom_field['type']); |
| 211 | |
| 212 | $fields[$field_key] = [ |
| 213 | 'label' => ppress_woocommerce_field_transform($field_key, htmlspecialchars_decode($db_custom_field['label_name'])), |
| 214 | 'required' => 'false', |
| 215 | 'field_type' => $field_type, |
| 216 | 'logged_in_hide' => 'true', |
| 217 | 'deletable' => 'true' |
| 218 | ]; |
| 219 | |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | return $fields; |
| 225 | } |
| 226 | |
| 227 | public static function account_info_fields() |
| 228 | { |
| 229 | static $data = false; |
| 230 | |
| 231 | if (false === $data) { |
| 232 | |
| 233 | $standard_fields = self::standard_account_info_fields(); |
| 234 | |
| 235 | $custom_fields = self::standard_custom_fields(); |
| 236 | |
| 237 | $fields = [ |
| 238 | self::ACCOUNT_EMAIL_ADDRESS => [ |
| 239 | 'label' => $standard_fields[self::ACCOUNT_EMAIL_ADDRESS]['label'], |
| 240 | 'required' => $standard_fields[self::ACCOUNT_EMAIL_ADDRESS]['required'], |
| 241 | 'field_type' => $standard_fields[self::ACCOUNT_EMAIL_ADDRESS]['field_type'], |
| 242 | 'logged_in_hide' => $standard_fields[self::ACCOUNT_EMAIL_ADDRESS]['logged_in_hide'], |
| 243 | 'deletable' => $standard_fields[self::ACCOUNT_EMAIL_ADDRESS]['deletable'], |
| 244 | 'width' => 'full' |
| 245 | ], |
| 246 | self::ACCOUNT_USERNAME => [ |
| 247 | 'label' => $standard_fields[self::ACCOUNT_USERNAME]['label'], |
| 248 | 'required' => $standard_fields[self::ACCOUNT_USERNAME]['required'], |
| 249 | 'field_type' => $standard_fields[self::ACCOUNT_USERNAME]['field_type'], |
| 250 | 'logged_in_hide' => $standard_fields[self::ACCOUNT_USERNAME]['logged_in_hide'], |
| 251 | 'deletable' => $standard_fields[self::ACCOUNT_USERNAME]['deletable'], |
| 252 | 'width' => 'half' |
| 253 | ], |
| 254 | self::ACCOUNT_PASSWORD => [ |
| 255 | 'label' => $standard_fields[self::ACCOUNT_PASSWORD]['label'], |
| 256 | 'required' => $standard_fields[self::ACCOUNT_PASSWORD]['required'], |
| 257 | 'field_type' => $standard_fields[self::ACCOUNT_PASSWORD]['field_type'], |
| 258 | 'logged_in_hide' => $standard_fields[self::ACCOUNT_PASSWORD]['logged_in_hide'], |
| 259 | 'deletable' => $standard_fields[self::ACCOUNT_PASSWORD]['deletable'], |
| 260 | 'width' => 'half' |
| 261 | ], |
| 262 | self::ACCOUNT_FIRST_NAME => [ |
| 263 | 'label' => $standard_fields[self::ACCOUNT_FIRST_NAME]['label'], |
| 264 | 'required' => $standard_fields[self::ACCOUNT_FIRST_NAME]['required'], |
| 265 | 'field_type' => $standard_fields[self::ACCOUNT_FIRST_NAME]['field_type'], |
| 266 | 'logged_in_hide' => $standard_fields[self::ACCOUNT_FIRST_NAME]['logged_in_hide'], |
| 267 | 'deletable' => $standard_fields[self::ACCOUNT_FIRST_NAME]['deletable'], |
| 268 | 'width' => 'half' |
| 269 | ], |
| 270 | self::ACCOUNT_LAST_NAME => [ |
| 271 | 'label' => $standard_fields[self::ACCOUNT_LAST_NAME]['label'], |
| 272 | 'required' => $standard_fields[self::ACCOUNT_LAST_NAME]['required'], |
| 273 | 'field_type' => $standard_fields[self::ACCOUNT_LAST_NAME]['field_type'], |
| 274 | 'logged_in_hide' => $standard_fields[self::ACCOUNT_LAST_NAME]['logged_in_hide'], |
| 275 | 'deletable' => $standard_fields[self::ACCOUNT_LAST_NAME]['deletable'], |
| 276 | 'width' => 'half' |
| 277 | ], |
| 278 | ]; |
| 279 | |
| 280 | $collection = ppress_var(get_option(self::DB_OPTION_NAME, []), 'accountInfo', []); |
| 281 | |
| 282 | if ( ! empty($collection)) { |
| 283 | |
| 284 | /** @see https://stackoverflow.com/a/13036310/2648410 */ |
| 285 | array_walk($collection, function (&$v, $k) use ($standard_fields, $custom_fields) { |
| 286 | $v['label'] = stripslashes(wp_kses_post($v['label'] ?? ($standard_fields[$k]['label'] ?? ppress_var($custom_fields[$k], 'label', '')))); |
| 287 | $v['width'] = sanitize_text_field($v['width'] ?? ($standard_fields[$k]['width'] ?? ppress_var($custom_fields[$k], 'width', 'full'))); |
| 288 | $v['required'] = sanitize_text_field($v['required'] ?? ($standard_fields[$k]['required'] ?? ppress_var($custom_fields[$k], 'required', 'false'))); |
| 289 | $v['logged_in_hide'] = sanitize_text_field($v['logged_in_hide'] ?? ($standard_fields[$k]['logged_in_hide'] ?? ppress_var($custom_fields[$k], 'logged_in_hide', 'true'))); |
| 290 | $v['field_type'] = $standard_fields[$k]['field_type'] ?? ppress_var($custom_fields[$k] ?? [], 'field_type', 'text'); |
| 291 | $v['deletable'] = $standard_fields[$k]['deletable'] ?? ppress_var($custom_fields[$k] ?? [], 'deletable', 'true'); |
| 292 | }); |
| 293 | |
| 294 | $fields = $collection; |
| 295 | } |
| 296 | |
| 297 | $data = apply_filters('ppress_checkout_account_info_fields', $fields); |
| 298 | } |
| 299 | |
| 300 | return $data; |
| 301 | } |
| 302 | |
| 303 | public static function billing_fields() |
| 304 | { |
| 305 | static $data = false; |
| 306 | |
| 307 | if (false === $data) { |
| 308 | |
| 309 | $billing_fields = self::standard_billing_fields(); |
| 310 | |
| 311 | $collection = ppress_var(get_option(self::DB_OPTION_NAME, []), 'billing', []); |
| 312 | |
| 313 | if ( ! empty($collection)) { |
| 314 | |
| 315 | array_walk($collection, function (&$v, $k) use ($billing_fields) { |
| 316 | $v['label'] = wp_kses_post($v['label'] ?? ($billing_fields[$k]['label'] ?? '')); |
| 317 | $v['width'] = sanitize_text_field($v['width'] ?? ($billing_fields[$k]['width'] ?? 'full')); |
| 318 | $v['required'] = sanitize_text_field($v['required'] ?? ($billing_fields[$k]['required'] ?? 'false')); |
| 319 | $v['logged_in_hide'] = sanitize_text_field($v['logged_in_hide'] ?? ($billing_fields[$k]['logged_in_hide'] ?? 'true')); |
| 320 | $v['field_type'] = $billing_fields[$k]['field_type'] ?? 'text'; |
| 321 | $v['deletable'] = $billing_fields[$k]['deletable'] ?? 'true'; |
| 322 | }); |
| 323 | |
| 324 | $billing_fields = $collection; |
| 325 | } |
| 326 | |
| 327 | $data = apply_filters('ppress_checkout_billing_fields', $billing_fields); |
| 328 | } |
| 329 | |
| 330 | return $data; |
| 331 | } |
| 332 | |
| 333 | public static function get_field_id($field_id, $payment_method = '') |
| 334 | { |
| 335 | if (empty($payment_method)) return $field_id; |
| 336 | |
| 337 | return $payment_method . '_' . $field_id; |
| 338 | } |
| 339 | |
| 340 | public static function render_field($field_id, $is_required = false, $extra_attr = [], $payment_method = '') |
| 341 | { |
| 342 | $html_field = ''; |
| 343 | |
| 344 | $standard_billing_fields = self::standard_billing_fields(); |
| 345 | $custom_fields = self::standard_custom_fields(); |
| 346 | |
| 347 | $field_type = $standard_billing_fields[$field_id]['field_type'] ?? ($custom_fields[$field_id]['field_type'] ?? 'text'); |
| 348 | $form_type = is_user_logged_in() ? FormRepository::EDIT_PROFILE_TYPE : FormRepository::REGISTRATION_TYPE; |
| 349 | |
| 350 | $instance = new FieldsShortcodeCallback($form_type, 'checkout_field', 'ppmb'); |
| 351 | |
| 352 | $standard_billing_fields_keys = array_keys($standard_billing_fields); |
| 353 | // add vat number field so it is recognized |
| 354 | $standard_billing_fields_keys[] = self::VAT_NUMBER; |
| 355 | |
| 356 | if (in_array($field_id, $standard_billing_fields_keys) || in_array($field_id, array_keys($custom_fields))) { |
| 357 | |
| 358 | $args = [ |
| 359 | 'key' => in_array($field_id, $standard_billing_fields_keys) ? $payment_method . '_' . $field_id : $field_id, |
| 360 | 'type' => $field_type, |
| 361 | 'id' => self::get_field_id($field_id, $payment_method), |
| 362 | 'class' => 'ppress-checkout-field__input ' . $field_id, |
| 363 | 'required' => $is_required |
| 364 | ]; |
| 365 | |
| 366 | $args['value'] = get_user_meta(get_current_user_id(), $field_id, true); |
| 367 | |
| 368 | $plan_id = isset($_GET['plan']) ? $_GET['plan'] : 0; |
| 369 | |
| 370 | $session_country = CheckoutSessionData::get_tax_country(absint($plan_id)); |
| 371 | |
| 372 | $session_state = CheckoutSessionData::get_tax_state(absint($plan_id)); |
| 373 | |
| 374 | if ($field_id == self::BILLING_COUNTRY && ! empty($session_country)) { |
| 375 | $args['value'] = $session_country; |
| 376 | } |
| 377 | |
| 378 | if ($field_id == self::BILLING_STATE) { |
| 379 | |
| 380 | $state_country = ! empty($_POST['country']) ? $_POST['country'] : $session_country; |
| 381 | |
| 382 | $states = ! empty($state_country) ? ppress_array_of_world_states(sanitize_text_field($state_country)) : []; |
| 383 | |
| 384 | if ( ! empty($states)) { |
| 385 | $args['type'] = 'select'; |
| 386 | $args['key_value_options'] = $states; |
| 387 | } |
| 388 | |
| 389 | if ( ! empty($session_state)) { |
| 390 | $args['value'] = $session_state; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | $html_field = $instance->custom_profile_field($args + $extra_attr); |
| 395 | |
| 396 | } else { |
| 397 | |
| 398 | $args = [ |
| 399 | 'id' => self::get_field_id($field_id, $payment_method), |
| 400 | 'placeholder' => '', |
| 401 | 'class' => 'ppress-checkout-field__input ' . $field_id, |
| 402 | 'required' => $is_required |
| 403 | ] + $extra_attr; |
| 404 | |
| 405 | switch ($field_id) { |
| 406 | case self::ACCOUNT_USERNAME: |
| 407 | $html_field = $instance->username($args); |
| 408 | break; |
| 409 | case self::ACCOUNT_EMAIL_ADDRESS: |
| 410 | $html_field = $instance->email($args); |
| 411 | break; |
| 412 | case self::ACCOUNT_CONFIRM_EMAIL_ADDRESS: |
| 413 | $html_field = $instance->confirm_email($args); |
| 414 | break; |
| 415 | case self::ACCOUNT_PASSWORD: |
| 416 | $html_field = $instance->password($args); |
| 417 | break; |
| 418 | case self::ACCOUNT_CONFIRM_PASSWORD: |
| 419 | $html_field = $instance->confirm_password($args); |
| 420 | break; |
| 421 | case self::ACCOUNT_FIRST_NAME: |
| 422 | $html_field = $instance->first_name($args); |
| 423 | break; |
| 424 | case self::ACCOUNT_LAST_NAME: |
| 425 | $html_field = $instance->last_name($args); |
| 426 | break; |
| 427 | case self::ACCOUNT_DISPLAY_NAME: |
| 428 | $html_field = $instance->display_name($args); |
| 429 | break; |
| 430 | case self::ACCOUNT_NICKNAME: |
| 431 | $html_field = $instance->nickname($args); |
| 432 | break; |
| 433 | case self::ACCOUNT_WEBSITE: |
| 434 | $html_field = $instance->website($args); |
| 435 | break; |
| 436 | case self::ACCOUNT_BIO: |
| 437 | $html_field = $instance->bio($args); |
| 438 | break; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | return apply_filters('ppress_checkout_field_render', $html_field, $field_id, $field_type); |
| 443 | } |
| 444 | } |
| 445 |