um-actions-form.php 33.7 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
<?php if ( ! defined( 'ABSPATH' ) ) exit;


/**
 * Error handling: blocked emails
 *
 * @param $args
 */
function um_submit_form_errors_hook__blockedemails( $args ) {
	$emails = UM()->options()->get( 'blocked_emails' );
	if ( ! $emails ) {
		return;
	}

	$emails = strtolower( $emails );
	$emails = array_map( 'rtrim', explode( "\n", $emails ) );

	if ( isset( $args['user_email'] ) && is_email( $args['user_email'] ) ) {
		if ( in_array( strtolower( $args['user_email'] ), $emails ) ) {
			exit( wp_redirect( esc_url( add_query_arg( 'err', 'blocked_email' ) ) ) );
		}

		$domain       = explode( '@', $args['user_email'] );
		$check_domain = str_replace( $domain[0], '*', $args['user_email'] );

		if ( in_array( strtolower( $check_domain ), $emails ) ) {
			exit( wp_redirect( esc_url( add_query_arg( 'err', 'blocked_domain' ) ) ) );
		}
	}

	if ( isset( $args['username'] ) && is_email( $args['username'] ) ) {
		if ( in_array( strtolower( $args['username'] ), $emails ) ) {
			exit( wp_redirect( esc_url( add_query_arg( 'err', 'blocked_email' ) ) ) );
		}

		$domain       = explode( '@', $args['username'] );
		$check_domain = str_replace( $domain[0], '*', $args['username'] );

		if ( in_array( strtolower( $check_domain ), $emails ) ) {
			exit( wp_redirect( esc_url( add_query_arg( 'err', 'blocked_domain' ) ) ) );
		}
	}
}
add_action( 'um_submit_form_errors_hook__blockedemails', 'um_submit_form_errors_hook__blockedemails', 10 );


/**
 * Error handling: blocked IPs
 *
 * @param $args
 */
function um_submit_form_errors_hook__blockedips( $args ) {
	$ips = UM()->options()->get( 'blocked_ips' );
	if ( ! $ips ) {
		return;
	}

	$ips = array_map( 'rtrim', explode( "\n", $ips ) );
	$user_ip = um_user_ip();

	foreach ( $ips as $ip ) {
		$ip = str_replace( '*', '', $ip );
		if ( ! empty( $ip ) && strpos( $user_ip, $ip ) === 0 ) {
			exit( wp_redirect( esc_url( add_query_arg( 'err', 'blocked_ip' ) ) ) );
		}
	}
}
add_action( 'um_submit_form_errors_hook__blockedips', 'um_submit_form_errors_hook__blockedips', 10 );


/**
 * Error handling: blocked words during sign up
 *
 * @param $args
 */
function um_submit_form_errors_hook__blockedwords( $args ) {
	$words = UM()->options()->get( 'blocked_words' );
	if ( empty( $words ) ) {
		return;
	}

	$form_id = $args['form_id'];
	$mode = $args['mode'];
	$fields = unserialize( $args['custom_fields'] );

	$words = strtolower( $words );
	$words = array_map( 'rtrim', explode( "\n", $words ) );
	if ( ! empty( $fields ) && is_array( $fields ) ) {
		foreach ( $fields as $key => $array ) {
			if ( isset( $array['validate'] ) && in_array( $array['validate'], array( 'unique_username', 'unique_email', 'unique_username_or_email' ) ) ) {
				if ( ! UM()->form()->has_error( $key ) && isset( $args[ $key ] ) && in_array( strtolower( $args[ $key ] ), $words ) ) {
					UM()->form()->add_error( $key, __( 'You are not allowed to use this word as your username.', 'ultimate-member' ) );
				}
			}
		}
	}
}
add_action( 'um_submit_form_errors_hook__blockedwords', 'um_submit_form_errors_hook__blockedwords', 10 );


/**
 * Error handling
 *
 * @param $args
 */
function um_submit_form_errors_hook( $args ) {
	$mode = $args['mode'];

	/**
	 * UM hook
	 *
	 * @type action
	 * @title um_submit_form_errors_hook__blockedips
	 * @description Submit form validation
	 * @input_vars
	 * [{"var":"$args","type":"array","desc":"Form Arguments"}]
	 * @change_log
	 * ["Since: 2.0"]
	 * @usage add_action( 'um_submit_form_errors_hook__blockedips', 'function_name', 10, 1 );
	 * @example
	 * <?php
	 * add_action( 'um_submit_form_errors_hook__blockedips', 'my_submit_form_errors_hook__blockedips', 10, 1 );
	 * function my_submit_form_errors_hook__blockedips( $args ) {
	 *     // your code here
	 * }
	 * ?>
	 */
	do_action( 'um_submit_form_errors_hook__blockedips', $args );


	/**
	 * UM hook
	 *
	 * @type action
	 * @title um_submit_form_errors_hook__blockedemails
	 * @description Submit form validation
	 * @input_vars
	 * [{"var":"$args","type":"array","desc":"Form Arguments"}]
	 * @change_log
	 * ["Since: 2.0"]
	 * @usage add_action( 'um_submit_form_errors_hook__blockedemails', 'function_name', 10, 1 );
	 * @example
	 * <?php
	 * add_action( 'um_submit_form_errors_hook__blockedemails', 'my_submit_form_errors_hook__blockedemails', 10, 1 );
	 * function my_submit_form_errors_hook__blockedemails( $args ) {
	 *     // your code here
	 * }
	 * ?>
	 */
	do_action( 'um_submit_form_errors_hook__blockedemails', $args );

	if ( $mode == 'register' ) {


		/**
		 * UM hook
		 *
		 * @type action
		 * @title um_submit_form_errors_hook__registration
		 * @description Submit registration form validation
		 * @input_vars
		 * [{"var":"$args","type":"array","desc":"Form Arguments"}]
		 * @change_log
		 * ["Since: 2.0"]
		 * @usage add_action( 'um_submit_form_errors_hook__registration', 'function_name', 10, 1 );
		 * @example
		 * <?php
		 * add_action( 'um_submit_form_errors_hook__registration', 'my_submit_form_errors_registration', 10, 1 );
		 * function my_submit_form_errors_registration( $args ) {
		 *     // your code here
		 * }
		 * ?>
		 */
		do_action( 'um_submit_form_errors_hook__registration', $args );

	} elseif ( $mode == 'profile' ) {


		/**
		 * UM hook
		 *
		 * @type action
		 * @title um_submit_form_errors_hook__registration
		 * @description Submit registration form validation
		 * @input_vars
		 * [{"var":"$args","type":"array","desc":"Form Arguments"}]
		 * @change_log
		 * ["Since: 2.0"]
		 * @usage add_action( 'um_submit_form_errors_hook__registration', 'function_name', 10, 1 );
		 * @example
		 * <?php
		 * add_action( 'um_submit_form_errors_hook__profile', 'my_submit_form_errors_hook__profile', 10, 1 );
		 * function my_submit_form_errors_registration( $args ) {
		 *     // your code here
		 * }
		 * ?>
		 */
		do_action( 'um_submit_form_errors_hook__profile', $args );

	} elseif ( $mode == 'login' ) {


		/**
		 * UM hook
		 *
		 * @type action
		 * @title um_submit_form_errors_hook_login
		 * @description Submit login form validation
		 * @input_vars
		 * [{"var":"$args","type":"array","desc":"Form Arguments"}]
		 * @change_log
		 * ["Since: 2.0"]
		 * @usage add_action( 'um_submit_form_errors_hook_login', 'function_name', 10, 1 );
		 * @example
		 * <?php
		 * add_action( 'um_submit_form_errors_hook_login', 'my_submit_form_errors_hook_login', 10, 1 );
		 * function my_submit_form_errors_hook_login( $args ) {
		 *     // your code here
		 * }
		 * ?>
		 */
		do_action( 'um_submit_form_errors_hook_login', $args );


		/**
		 * UM hook
		 *
		 * @type action
		 * @title um_submit_form_errors_hook_logincheck
		 * @description Submit login form validation
		 * @input_vars
		 * [{"var":"$args","type":"array","desc":"Form Arguments"}]
		 * @change_log
		 * ["Since: 2.0"]
		 * @usage add_action( 'um_submit_form_errors_hook_logincheck', 'function_name', 10, 1 );
		 * @example
		 * <?php
		 * add_action( 'um_submit_form_errors_hook_logincheck', 'my_submit_form_errors_hook_logincheck', 10, 1 );
		 * function my_submit_form_errors_hook_logincheck( $args ) {
		 *     // your code here
		 * }
		 * ?>
		 */
		do_action( 'um_submit_form_errors_hook_logincheck', $args );

	}


	if ( $mode != 'login' ) {


		/**
		 * UM hook
		 *
		 * @type action
		 * @title um_submit_form_errors_hook__blockedwords
		 * @description Submit form validation
		 * @input_vars
		 * [{"var":"$args","type":"array","desc":"Form Arguments"}]
		 * @change_log
		 * ["Since: 2.0"]
		 * @usage add_action( 'um_submit_form_errors_hook__blockedwords', 'function_name', 10, 1 );
		 * @example
		 * <?php
		 * add_action( 'um_submit_form_errors_hook__blockedwords', 'my_submit_form_errors_hook__blockedwords', 10, 1 );
		 * function my_submit_form_errors_hook__blockedwords( $args ) {
		 *     // your code here
		 * }
		 * ?>
		 */
		do_action( 'um_submit_form_errors_hook__blockedwords', $args );


		/**
		 * UM hook
		 *
		 * @type action
		 * @title um_submit_form_errors_hook_
		 * @description Submit form validation
		 * @input_vars
		 * [{"var":"$args","type":"array","desc":"Form Arguments"}]
		 * @change_log
		 * ["Since: 2.0"]
		 * @usage add_action( 'um_submit_form_errors_hook_', 'function_name', 10, 1 );
		 * @example
		 * <?php
		 * add_action( 'um_submit_form_errors_hook_', 'my_submit_form_errors_hook', 10, 1 );
		 * function my_submit_form_errors_hook( $args ) {
		 *     // your code here
		 * }
		 * ?>
		 */
		do_action( 'um_submit_form_errors_hook_', $args );

	}

}
add_action( 'um_submit_form_errors_hook', 'um_submit_form_errors_hook', 10 );


/**
 * Error processing: Conditions
 * @staticvar int     $counter
 * @param     array   $condition
 * @param     array   $fields
 * @param     array   $args
 * @param     boolean $reset
 * @return    boolean
 * @throws    Exception
 */
function um_check_conditions_on_submit( $condition, $fields, $args, $reset = false ) {
	static $counter = 0;
	if ( $reset ) {
		$counter = 0;
	}
	$continue = false;

	list( $visibility, $parent_key, $op, $parent_value ) = $condition;

	if ( ! isset( $args[ $parent_key ] ) ) {
		$continue = true;
		return $continue;
	}

	if ( ! empty( $fields[ $parent_key ]['conditions'] ) ) {
		foreach ( $fields[ $parent_key ]['conditions'] as $parent_condition ) {
			if ( 64 > $counter++ ) {
				$continue = um_check_conditions_on_submit( $parent_condition, $fields, $args );
			} else {
				throw new Exception( 'Endless recursion in the function ' . __FUNCTION__, 512 );
			}
			if ( ! empty( $continue ) ) {
				return $continue;
			}
		}
	}

	$cond_value = ( $fields[ $parent_key ]['type'] == 'radio' ) ? $args[ $parent_key ][0] : $args[ $parent_key ];

	if ( $visibility == 'hide' ) {
		if ( $op == 'empty' ) {
			if ( empty( $cond_value ) ) {
				$continue = true;
			}
		} elseif ( $op == 'not empty' ) {
			if ( ! empty( $cond_value ) ) {
				$continue = true;
			}
		} elseif ( $op == 'equals to' ) {
			if ( $cond_value == $parent_value ) {
				$continue = true;
			}
		} elseif ( $op == 'not equals' ) {
			if ( $cond_value != $parent_value ) {
				$continue = true;
			}
		} elseif ( $op == 'greater than' ) {
			if ( $cond_value > $parent_value ) {
				$continue = true;
			}
		} elseif ( $op == 'less than' ) {
			if ( $cond_value < $parent_value ) {
				$continue = true;
			}
		} elseif ( $op == 'contains' ) {
			if ( is_string( $cond_value ) && strstr( $cond_value, $parent_value ) ) {
				$continue = true;
			}
			if( is_array( $cond_value ) && in_array( $parent_value, $cond_value ) ) {
				$continue = true;
			}
		}
	} elseif ( $visibility == 'show' ) {
		if ( $op == 'empty' ) {
			if ( ! empty( $cond_value ) ) {
				$continue = true;
			}
		} elseif ( $op == 'not empty' ) {
			if ( empty( $cond_value ) ) {
				$continue = true;
			}
		} elseif ( $op == 'equals to' ) {
			if ( $cond_value != $parent_value ) {
				$continue = true;
			}
		} elseif ( $op == 'not equals' ) {
			if ( $cond_value == $parent_value ) {
				$continue = true;
			}
		} elseif ( $op == 'greater than' ) {
			if ( $cond_value <= $parent_value ) {
				$continue = true;
			}
		} elseif ( $op == 'less than' ) {
			if ( $cond_value >= $parent_value ) {
				$continue = true;
			}
		} elseif ( $op == 'contains' ) {
			if ( is_string( $cond_value ) && ! strstr( $cond_value, $parent_value ) ) {
				$continue = true;
			}
			if( is_array( $cond_value ) && !in_array( $parent_value, $cond_value ) ) {
				$continue = true;
			}
		}
	}

	return $continue;
}


/**
 * Error processing hook : standard
 *
 * @param $args
 */
function um_submit_form_errors_hook_( $args ) {
	$form_id = $args['form_id'];
	$mode = $args['mode'];
	$fields = unserialize( $args['custom_fields'] );

	$um_profile_photo = um_profile('profile_photo');
	if ( get_post_meta( $form_id, '_um_profile_photo_required', true ) && ( empty( $args['profile_photo'] ) && empty( $um_profile_photo ) ) ) {
		UM()->form()->add_error('profile_photo', __( 'Profile Photo is required.', 'ultimate-member' ) );
	}

	if ( ! empty( $fields ) ) {

		$can_edit = false;
		$current_user_roles = [];
		if ( is_user_logged_in() ) {

			$can_edit = UM()->roles()->um_current_user_can( 'edit', $args['user_id'] );

			um_fetch_user( get_current_user_id() );
			$current_user_roles = um_user( 'roles' );
			um_reset_user();
		}

		foreach ( $fields as $key => $array ) {

			if ( $mode == 'profile' ) {
				$restricted_fields = UM()->fields()->get_restricted_fields_for_edit();
				if ( is_array( $restricted_fields ) && in_array( $key, $restricted_fields ) ) {
					continue;
				}
			}

			$can_view = true;
			if ( isset( $array['public'] ) && $mode != 'register' ) {

				switch ( $array['public'] ) {
					case '1': // Everyone
						break;
					case '2': // Members
						if ( ! is_user_logged_in() ) {
							$can_view = false;
						}
						break;
					case '-1': // Only visible to profile owner and admins
						if ( ! is_user_logged_in() ) {
							$can_view = false;
						} elseif ( $args['user_id'] != get_current_user_id() && ! $can_edit ) {
							$can_view = false;
						}
						break;
					case '-2': // Only specific member roles
						if ( ! is_user_logged_in() ) {
							$can_view = false;
						} elseif ( ! empty( $array['roles'] ) && count( array_intersect( $current_user_roles, $array['roles'] ) ) <= 0 ) {
							$can_view = false;
						}
						break;
					case '-3': // Only visible to profile owner and specific roles
						if ( ! is_user_logged_in() ) {
							$can_view = false;
						} elseif ( $args['user_id'] != get_current_user_id() && ! empty( $array['roles'] ) && count( array_intersect( $current_user_roles, $array['roles'] ) ) <= 0 ) {
							$can_view = false;
						}
						break;
					default:
						$can_view = apply_filters( 'um_can_view_field_custom', $can_view, $array );
						break;
				}

			}

			$can_view = apply_filters( 'um_can_view_field', $can_view, $array );

			if ( ! $can_view ) {
				continue;
			}


			/**
			 * UM hook
			 *
			 * @type filter
			 * @title um_get_custom_field_array
			 * @description Extend custom field data on submit form error
			 * @input_vars
			 * [{"var":"$array","type":"array","desc":"Field data"},
			 * {"var":"$fields","type":"array","desc":"All fields"}]
			 * @change_log
			 * ["Since: 2.0"]
			 * @usage
			 * <?php add_filter( 'um_get_custom_field_array', 'function_name', 10, 2 ); ?>
			 * @example
			 * <?php
			 * add_filter( 'um_get_custom_field_array', 'my_get_custom_field_array', 10, 2 );
			 * function my_get_custom_field_array( $array, $fields ) {
			 *     // your code here
			 *     return $array;
			 * }
			 * ?>
			 */
			$array = apply_filters( 'um_get_custom_field_array', $array, $fields );

			if ( ! empty( $array['conditions'] ) ) {
				try {
					foreach ( $array['conditions'] as $condition ) {
						$continue = um_check_conditions_on_submit( $condition, $fields, $args, true );
						if ( $continue === true ) {
							continue 2;
						}
					}
				} catch ( Exception $e ) {
					UM()->form()->add_error( $key, sprintf( __( '%s - wrong conditions.', 'ultimate-member' ), $array['title'] ) );
					$notice = '<div class="um-field-error">' . sprintf( __( '%s - wrong conditions.', 'ultimate-member' ), $array['title'] ) . '</div><!-- ' . $e->getMessage() . ' -->';
					add_action( 'um_after_profile_fields', function() use ( $notice ) {
						echo $notice;
					}, 900 );
				}
			}

			if ( isset( $array['type'] ) && $array['type'] == 'checkbox' && isset( $array['required'] ) && $array['required'] == 1 && ! isset( $args[ $key ] ) ) {
				UM()->form()->add_error( $key, sprintf( __( '%s is required.', 'ultimate-member' ), $array['title'] ) );
			}

			if ( isset( $array['type'] ) && $array['type'] == 'radio' && isset( $array['required'] ) && $array['required'] == 1 && ! isset( $args[ $key ] ) && ! in_array( $key, array( 'role_radio', 'role_select' ) ) ) {
				UM()->form()->add_error( $key, sprintf( __( '%s is required.', 'ultimate-member'), $array['title'] ) );
			}

			if ( isset( $array['type'] ) && $array['type'] == 'multiselect' && isset( $array['required'] ) && $array['required'] == 1 && ! isset( $args[ $key ] ) && ! in_array( $key, array( 'role_radio', 'role_select' ) ) ) {
				UM()->form()->add_error( $key, sprintf( __( '%s is required.', 'ultimate-member' ), $array['title'] ) );
			}

			/* WordPress uses the default user role if the role wasn't chosen in the registration form. That is why we should use submitted data to validate fields Roles (Radio) and Roles (Dropdown). */
			if ( in_array( $key, array( 'role_radio', 'role_select' ) ) && isset( $array['required'] ) && $array['required'] == 1 && empty( UM()->form()->post_form['submitted']['role'] ) ) {
				UM()->form()->add_error( 'role', __( 'Please specify account type.', 'ultimate-member' ) );
				UM()->form()->post_form[ $key ] = '';
			}

			/**
			 * UM hook
			 *
			 * @type action
			 * @title um_add_error_on_form_submit_validation
			 * @description Submit form validation
			 * @input_vars
			 * [{"var":"$field","type":"array","desc":"Field Data"},
			 * {"var":"$key","type":"string","desc":"Field Key"},
			 * {"var":"$args","type":"array","desc":"Form Arguments"}]
			 * @change_log
			 * ["Since: 2.0"]
			 * @usage add_action( 'um_add_error_on_form_submit_validation', 'function_name', 10, 3 );
			 * @example
			 * <?php
			 * add_action( 'um_add_error_on_form_submit_validation', 'my_add_error_on_form_submit_validation', 10, 3 );
			 * function my_add_error_on_form_submit_validation( $field, $key, $args ) {
			 *     // your code here
			 * }
			 * ?>
			 */
			do_action( 'um_add_error_on_form_submit_validation', $array, $key, $args );

			if ( ! empty( $array['required'] ) ) {
				if ( ! isset( $args[ $key ] ) || $args[ $key ] == '' || $args[ $key ] == 'empty_file' ) {
					if ( empty( $array['label'] ) ) {
						UM()->form()->add_error( $key, __( 'This field is required', 'ultimate-member' ) );
					} else {
						UM()->form()->add_error( $key, sprintf( __( '%s is required', 'ultimate-member' ), $array['label'] ) );
					}
				}
			}

			if ( isset( $args[ $key ] ) ) {

				if ( isset( $array['max_words'] ) && $array['max_words'] > 0 ) {
					if ( str_word_count( $args[ $key ], 0, "éèàôù" ) > $array['max_words'] ) {
						UM()->form()->add_error( $key, sprintf( __( 'You are only allowed to enter a maximum of %s words', 'ultimate-member' ), $array['max_words'] ) );
					}
				}

				if ( isset( $array['min_chars'] ) && $array['min_chars'] > 0 ) {
					if ( $args[ $key ] && mb_strlen( $args[ $key ] ) < $array['min_chars'] ) {
						if ( empty( $array['label'] ) ) {
							UM()->form()->add_error( $key, sprintf( __( 'This field must contain at least %s characters', 'ultimate-member' ), $array['min_chars'] ) );
						} else {
							UM()->form()->add_error( $key, sprintf( __( 'Your %s must contain at least %s characters', 'ultimate-member' ), $array['label'], $array['min_chars'] ) );
						}
					}
				}

				if ( isset( $array['max_chars'] ) && $array['max_chars'] > 0 ) {
					if ( $args[ $key ] && mb_strlen( $args[ $key ] ) > $array['max_chars'] ) {
						if ( empty( $array['label'] ) ) {
							UM()->form()->add_error( $key, sprintf( __( 'This field must contain less than %s characters', 'ultimate-member' ), $array['max_chars'] ) );
						} else {
							UM()->form()->add_error( $key, sprintf( __( 'Your %s must contain less than %s characters', 'ultimate-member' ), $array['label'], $array['max_chars'] ) );
						}
					}
				}

				if ( isset( $array['type'] ) && $array['type'] == 'textarea' && UM()->profile()->get_show_bio_key( $args ) !== $key ) {
					if ( ! isset( $array['html'] ) || $array['html'] == 0 ) {
						if ( wp_strip_all_tags( $args[ $key ] ) != trim( $args[ $key ] ) ) {
							UM()->form()->add_error( $key, __( 'You can not use HTML tags here', 'ultimate-member' ) );
						}
					}
				}

				if ( isset( $array['force_good_pass'] ) && $array['force_good_pass'] && ! empty( $args['user_password'] ) ) {
					if ( isset( $args['user_login'] ) && strpos( strtolower( $args['user_login'] ), strtolower( $args['user_password'] )  ) > -1 ) {
						UM()->form()->add_error( 'user_password', __( 'Your password cannot contain the part of your username', 'ultimate-member' ));
					}

					if ( isset( $args['user_email'] ) && strpos( strtolower( $args['user_email'] ), strtolower( $args['user_password'] )  ) > -1 ) {
						UM()->form()->add_error( 'user_password', __( 'Your password cannot contain the part of your email address', 'ultimate-member' ));
					}

					if ( ! UM()->validation()->strong_pass( $args[ $key ] ) ) {
						UM()->form()->add_error( $key, __( 'Your password must contain at least one lowercase letter, one capital letter and one number', 'ultimate-member' ) );
					}
				}

				if ( isset( $array['force_confirm_pass'] ) && $array['force_confirm_pass'] == 1 ) {
					if ( $args[ 'confirm_' . $key ] == '' && ! UM()->form()->has_error( $key ) ) {
						UM()->form()->add_error( 'confirm_' . $key , __( 'Please confirm your password', 'ultimate-member' ) );
					}
					if ( $args[ 'confirm_' . $key ] != $args[$key] && !UM()->form()->has_error( $key ) ) {
						UM()->form()->add_error( 'confirm_' . $key , __( 'Your passwords do not match', 'ultimate-member' ) );
					}
				}

				if ( isset( $array['min_selections'] ) && $array['min_selections'] > 0 ) {
					if ( ( ! isset( $args[ $key ] ) ) || ( isset( $args[ $key ] ) && is_array( $args[ $key ] ) && count( $args[ $key ] ) < $array['min_selections'] ) ) {
						UM()->form()->add_error($key, sprintf( __( 'Please select at least %s choices', 'ultimate-member' ), $array['min_selections'] ) );
					}
				}

				if ( isset( $array['max_selections'] ) && $array['max_selections'] > 0 ) {
					if ( isset( $args[ $key ] ) && is_array( $args[ $key ] ) && count( $args[ $key ] ) > $array['max_selections'] ) {
						UM()->form()->add_error( $key, sprintf( __( 'You can only select up to %s choices', 'ultimate-member' ), $array['max_selections'] ) );
					}
				}

				if ( isset( $array['min'] ) && is_numeric( $args[ $key ] ) ) {
					if ( isset( $args[ $key ] )  && $args[ $key ] < $array['min'] ) {
						UM()->form()->add_error( $key, sprintf( __( 'Minimum number limit is %s', 'ultimate-member' ), $array['min'] ) );
					}
				}

				if ( isset( $array['max'] ) && is_numeric( $args[ $key ] )  ) {
					if ( isset( $args[ $key ] ) && $args[ $key ] > $array['max'] ) {
						UM()->form()->add_error( $key, sprintf( __( 'Maximum number limit is %s', 'ultimate-member' ), $array['max'] ) );
					}
				}

				if ( ! empty( $array['validate'] ) ) {

					switch( $array['validate'] ) {

						case 'custom':
							$custom = $array['custom_validate'];
							/**
							 * UM hook
							 *
							 * @type action
							 * @title um_custom_field_validation_{$custom}
							 * @description Submit form validation for custom field
							 * @input_vars
							 * [{"var":"$key","type":"string","desc":"Field Key"},
							 * {"var":"$field","type":"array","desc":"Field Data"},
							 * {"var":"$args","type":"array","desc":"Form Arguments"}]
							 * @change_log
							 * ["Since: 2.0"]
							 * @usage add_action( 'um_custom_field_validation_{$custom}', 'function_name', 10, 3 );
							 * @example
							 * <?php
							 * add_action( 'um_custom_field_validation_{$custom}', 'my_custom_field_validation', 10, 3 );
							 * function my_custom_field_validation( $key, $field, $args ) {
							 *     // your code here
							 * }
							 * ?>
							 */
							do_action( "um_custom_field_validation_{$custom}", $key, $array, $args );
							break;

						case 'numeric':
							if ( $args[ $key ] && ! is_numeric( $args[ $key ] ) ) {
								UM()->form()->add_error( $key, __( 'Please enter numbers only in this field', 'ultimate-member' ) );
							}
							break;

						case 'phone_number':
							if ( ! UM()->validation()->is_phone_number( $args[ $key ] ) ) {
								UM()->form()->add_error( $key, __( 'Please enter a valid phone number', 'ultimate-member' ) );
							}
							break;

						case 'youtube_url':
							if ( ! UM()->validation()->is_url( $args[ $key ], 'youtube.com' ) && ! UM()->validation()->is_url( $args[ $key ], 'youtu.be' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s username or profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'telegram_url':
							if ( ! UM()->validation()->is_url( $args[ $key ], 't.me' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s username or profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'soundcloud_url':
							if ( ! UM()->validation()->is_url( $args[ $key ], 'soundcloud.com' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s username or profile URL','ultimate-member'), $array['label'] ) );
							}
							break;

						case 'facebook_url':
							if ( ! UM()->validation()->is_url( $args[ $key ], 'facebook.com' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s username or profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'twitter_url':
							if ( ! UM()->validation()->is_url( $args[ $key ], 'twitter.com' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s username or profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'instagram_url':

							if ( ! UM()->validation()->is_url( $args[ $key ], 'instagram.com' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'google_url':
							if ( ! UM()->validation()->is_url( $args[ $key ], 'plus.google.com' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s username or profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'linkedin_url':
							if ( ! UM()->validation()->is_url( $args[ $key ], 'linkedin.com' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s username or profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'vk_url':
							if ( ! UM()->validation()->is_url( $args[ $key ], 'vk.com' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s username or profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'discord':
							if ( ! UM()->validation()->is_discord_id( $args[ $key ] ) ) {
								UM()->form()->add_error( $key, __( 'Please enter a valid Discord ID', 'ultimate-member' ) );
							}
							break;

						case 'tiktok_url':

							if ( ! UM()->validation()->is_url( $args[ $key ], 'tiktok.com' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'twitch_url':

							if ( ! UM()->validation()->is_url( $args[ $key ], 'twitch.tv' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'reddit_url':

							if ( ! UM()->validation()->is_url( $args[ $key ], 'reddit.com' ) ) {
								UM()->form()->add_error( $key, sprintf( __( 'Please enter a valid %s profile URL', 'ultimate-member' ), $array['label'] ) );
							}
							break;

						case 'url':
							if ( ! UM()->validation()->is_url( $args[ $key ] ) ) {
								UM()->form()->add_error( $key, __( 'Please enter a valid URL', 'ultimate-member' ) );
							}
							break;

						case 'unique_username':

							if ( $args[ $key ] == '' ) {
								UM()->form()->add_error( $key, __( 'You must provide a username', 'ultimate-member' ) );
							} elseif ( $mode == 'register' && username_exists( sanitize_user( $args[ $key ] ) ) ) {
								UM()->form()->add_error( $key, __( 'The username you entered is incorrect', 'ultimate-member' ) );
							} elseif ( is_email( $args[ $key ] ) ) {
								UM()->form()->add_error( $key, __( 'Username cannot be an email', 'ultimate-member' ) );
							} elseif ( ! UM()->validation()->safe_username( $args[ $key ] ) ) {
								UM()->form()->add_error( $key, __( 'Your username contains invalid characters', 'ultimate-member' ) );
							}

							break;

						case 'unique_username_or_email':

							if ( $args[ $key ] == '' ) {
								UM()->form()->add_error( $key, __( 'You must provide a username or email', 'ultimate-member' ) );
							} elseif ( $mode == 'register' && username_exists( sanitize_user( $args[ $key ] ) ) ) {
								UM()->form()->add_error( $key, __( 'The username you entered is incorrect', 'ultimate-member' ) );
							} elseif ( $mode == 'register' && email_exists( $args[ $key ] ) ) {
								UM()->form()->add_error( $key, __( 'The email you entered is incorrect', 'ultimate-member' ) );
							} elseif ( ! UM()->validation()->safe_username( $args[ $key ] ) ) {
								UM()->form()->add_error( $key, __( 'Your username contains invalid characters', 'ultimate-member' ) );
							}

							break;

						case 'unique_email':

							$args[ $key ] = trim( $args[ $key ] );

							if ( in_array( $key, array( 'user_email' ) ) ) {

								if ( ! isset( $args['user_id'] ) ){
									$args['user_id'] = um_get_requested_user();
								}

								$email_exists = email_exists( $args[ $key ] );

								if ( $args[ $key ] == '' && in_array( $key, array( 'user_email' ) ) ) {
									UM()->form()->add_error( $key, __( 'You must provide your email', 'ultimate-member' ) );
								} elseif ( in_array( $mode, array( 'register' ) ) && $email_exists  ) {
									UM()->form()->add_error( $key, __( 'The email you entered is incorrect', 'ultimate-member' ) );
								} elseif ( in_array( $mode, array( 'profile' ) ) && $email_exists && $email_exists != $args['user_id']  ) {
									UM()->form()->add_error( $key, __( 'The email you entered is incorrect', 'ultimate-member' ) );
								} elseif ( ! is_email( $args[ $key ] ) ) {
									UM()->form()->add_error( $key, __( 'The email you entered is incorrect', 'ultimate-member') );
								} elseif ( ! UM()->validation()->safe_username( $args[ $key ] ) ) {
									UM()->form()->add_error( $key,  __( 'Your email contains invalid characters', 'ultimate-member' ) );
								}

							} else {

								if ( $args[ $key ] != '' && ! is_email( $args[ $key ] ) ) {
									UM()->form()->add_error( $key, __( 'The email you entered is incorrect', 'ultimate-member' ) );
								} elseif ( $args[ $key ] != '' && email_exists( $args[ $key ] ) ) {
									UM()->form()->add_error( $key, __( 'The email you entered is incorrect', 'ultimate-member' ) );
								} elseif ( $args[ $key ] != '' ) {

									$users = get_users( 'meta_value=' . $args[ $key ] );

									foreach ( $users as $user ) {
										if ( $user->ID != $args['user_id'] ) {
											UM()->form()->add_error( $key, __( 'The email you entered is incorrect', 'ultimate-member' ) );
										}
									}

								}

							}

							break;

						case 'is_email':

							$args[ $key ] = trim( $args[ $key ] );

							if ( $args[ $key ] != '' && ! is_email( $args[ $key ] ) ) {
								UM()->form()->add_error( $key, __( 'This is not a valid email', 'ultimate-member' ) );
							}

							break;

						case 'unique_value':

							if ( $args[ $key ] != '' ) {

								$args_unique_meta = array(
									'meta_key'      => $key,
									'meta_value'    => $args[ $key ],
									'compare'       => '=',
									'exclude'       => array( $args['user_id'] ),
								);

								$meta_key_exists = get_users( $args_unique_meta );

								if ( $meta_key_exists ) {
									UM()->form()->add_error( $key , __( 'You must provide a unique value', 'ultimate-member' ) );
								}
							}
							break;

						case 'alphabetic':

							if ( $args[ $key ] != '' ) {

								if ( ! preg_match( '/^\p{L}+$/u', str_replace( ' ', '', $args[ $key ] ) ) ) {
									UM()->form()->add_error( $key, __( 'You must provide alphabetic letters', 'ultimate-member' ) );
								}

							}

							break;

						case 'lowercase':

							if ( $args[ $key ] != '' ) {

								if ( ! ctype_lower( str_replace(' ', '', $args[ $key ] ) ) ) {
									UM()->form()->add_error( $key , __( 'You must provide lowercase letters.', 'ultimate-member' ) );
								}
							}

							break;

					}

				}

			}

			if ( isset( $args['description'] ) ) {
				$max_chars = UM()->options()->get( 'profile_bio_maxchars' );
				$profile_show_bio = UM()->options()->get( 'profile_show_bio' );

				if ( $profile_show_bio ) {
					if ( mb_strlen( str_replace( array( "\r\n", "\n", "\r\t", "\t" ), ' ', $args['description'] ) ) > $max_chars && $max_chars ) {
						UM()->form()->add_error( 'description', sprintf( __( 'Your user description must contain less than %s characters', 'ultimate-member' ), $max_chars ) );
					}
				}

			}

		} // end if ( isset in args array )
	}
}
add_action( 'um_submit_form_errors_hook_', 'um_submit_form_errors_hook_', 10 );


/**
 * @param string $url
 *
 * @return string
 */
function um_invalid_nonce_redirect_url( $url ) {
	$url = add_query_arg( [
		'um-hash'   => substr( md5( rand() ), 0, 6 ),
	], remove_query_arg( 'um-hash', $url ) );

	return $url;
}
add_filter( 'um_login_invalid_nonce_redirect_url', 'um_invalid_nonce_redirect_url', 10, 1 );
add_filter( 'um_register_invalid_nonce_redirect_url', 'um_invalid_nonce_redirect_url', 10, 1 );