add( 'tk_invite_code_error', sprintf( '%s: %s', esc_html__( 'ERROR', 'all-in-one-invite-codes' ), esc_html__( 'You must include an Invite Code.', 'all-in-one-invite-codes' ) ) ); return $errors; } // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing -- WP-managed wp-login.php nonce already verified before this filter fires. $type = isset( $_GET['action'] ) ? strtolower( sanitize_key( wp_unslash( $_GET['action'] ) ) ) : ''; if ( '' === $type ) { $type = isset( $_POST['wp-submit'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['wp-submit'] ) ) ) : ''; } // phpcs:enable WordPress.Security.NonceVerification.Recommended, WordPress.Security.NonceVerification.Missing if ( '' === $type ) { $type = 'any'; } // Validate the code. $result = all_in_one_invite_codes_validate_code( $tk_invite_code, $user_email, $type ); if ( isset( $result['error'] ) ) { $errors->add( 'tk_invite_code_error', sprintf( '%s: %s', esc_html__( 'ERROR', 'all-in-one-invite-codes' ), wp_kses_post( $result['error'] ) ) ); } return $errors; } add_filter( 'registration_errors', 'all_in_one_invite_code_registration_errors', 10, 3 ); function all_in_one_invite_code_registration_save( $user_id ) { $tk_invite_code = all_in_one_invite_code_get_submitted_code(); if ( '' === $tk_invite_code ) { return; } // Save the invite code as user meta data to know the relation for later query's/ stats update_user_meta( $user_id, 'tk_all_in_one_invite_code', $tk_invite_code ); // Get the invite code — meta_query is the only way to look up by hash; bounded by 1 row. // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query $args = array( 'post_type' => 'tk_invite_codes', 'posts_per_page' => 1, 'no_found_rows' => true, 'meta_query' => array( array( 'key' => 'tk_all_in_one_invite_code', 'value' => $tk_invite_code, 'compare' => '=', ), ), ); // phpcs:enable WordPress.DB.SlowDBQuery.slow_db_query_meta_query $query = new WP_Query( $args ); // Get the invite code id if ( $query->have_posts() ) { while ( $query->have_posts() ) : $query->the_post(); $post_parent_post_id = get_the_ID(); endwhile; } // get the invite code options $all_in_one_invite_codes_options = get_post_meta( $post_parent_post_id, 'all_in_one_invite_codes_options', true ); $is_multiple_use = isset( $all_in_one_invite_codes_options['multiple_use'] ) ? true : false; if ( $is_multiple_use ) { $code_amount = isset( $all_in_one_invite_codes_options['generate_codes'] ) ? intval( $all_in_one_invite_codes_options['generate_codes'] ) : 0; if ( $code_amount > 0 ) { $code_amount = $code_amount - 1; $all_in_one_invite_codes_options['generate_codes'] = $code_amount; update_post_meta( $post_parent_post_id, 'all_in_one_invite_codes_options', $all_in_one_invite_codes_options ); } else { update_post_meta( $post_parent_post_id, 'tk_all_in_one_invite_code_status', 'Used' ); } } else { // Check if and how many new invite code should get created. if ( isset( $all_in_one_invite_codes_options['generate_codes'] ) && $all_in_one_invite_codes_options['generate_codes'] > 0 ) { // Alright, loop and create all needed codes for this user. for ( $i = 1; $i <= $all_in_one_invite_codes_options['generate_codes']; $i ++ ) { $args = array( 'post_type' => 'tk_invite_codes', 'post_author' => $user_id, 'post_parent' => $post_parent_post_id, 'post_status' => 'publish', 'post_title' => $tk_invite_code, ); $new_code_id = wp_insert_post( $args ); // Create and save the new invite code as post meta $code = all_in_one_invite_codes_md5( $new_code_id ); $code = wp_filter_post_kses( $code ); $args = array( 'ID' => $new_code_id, 'post_title' => $code, ); wp_update_post( $args ); update_post_meta( $new_code_id, 'tk_all_in_one_invite_code', $code ); // Assign the amount of new codes to the code witch should get created if one of this codes get used. $all_in_one_invite_codes_new_options['generate_codes'] = $all_in_one_invite_codes_options['generate_codes']; update_post_meta( $new_code_id, 'all_in_one_invite_codes_options', $all_in_one_invite_codes_new_options ); update_post_meta( $new_code_id, 'tk_all_in_one_invite_code_status', 'Active' ); } } $user_data = get_userdata( $user_id ); $all_in_one_invite_codes_options['email'] = $user_data->user_email; update_post_meta( $post_parent_post_id, 'all_in_one_invite_codes_options', $all_in_one_invite_codes_options ); update_post_meta( $post_parent_post_id, 'tk_all_in_one_invite_code_status', 'Used' ); } } add_action( 'user_register', 'all_in_one_invite_code_registration_save', 10, 1 );