User
Source
File: classes/User.php
class User { public function __construct() { add_dozent_action( 'option_save_after', [ $this, 'can_publish_course' ] ); //User signup add_dozent_action( 'user_signup', [ $this, 'user_signup' ] ); add_dozent_action( 'become_an_instructor', [ $this, 'become_an_instructor' ] ); //Logout From the Dashboard add_dozent_action( 'dashboard_template_before_logout', [ $this, 'dashboard_logout' ] ); //Profile Edit From the Dashboard add_dozent_action( 'profile_edit', [ $this, 'profile_edit' ] ); //Reset Password add_dozent_action( 'reset_password', [ $this, 'reset_password' ] ); } public function user_signup() { dozent_checking_nonce(); $config = apply_filters( 'dozent_user_signup_form_validation_rules', [ [ 'field' => 'first_name', 'label' => __( 'First Name', 'dozent' ), 'rules' => 'required', ], [ 'field' => 'last_name', 'label' => __( 'Last Name', 'dozent' ), 'rules' => 'required', ], [ 'field' => 'email', 'label' => __( 'E-Mail', 'dozent' ), 'rules' => 'required|email', 'errors' => [ 'email' => __( 'You must provide a valid %s.', 'dozent' ), ], ], [ 'field' => 'user_login', 'label' => __( 'User Login', 'dozent' ), 'rules' => 'required', ], [ 'field' => 'password', 'label' => __( 'Password', 'dozent' ), 'rules' => 'required|confirm', ], [ 'field' => 'password_confirmation', 'label' => __( 'Password Confirmation', 'dozent' ), 'rules' => 'required', ], ] ); $validator = dozent_form_validate( $config ); /** * If validator fail, stop script */ if ( ! $validator->success ) { return; } $first_name = dozent_input_text( 'first_name' ); $last_name = dozent_input_text( 'last_name' ); $email = dozent_input_text( 'email' ); $user_login = dozent_input_text( 'user_login' ); $password = dozent_input_text( 'password' ); $userdata = apply_filters( 'dozent_user_signup_data', [ 'user_login' => $user_login, 'user_email' => $email, 'first_name' => $first_name, 'last_name' => $last_name, 'role' => 'subscriber', 'user_pass' => $password, ] ); do_action( 'dozent_user_signup_before', $userdata ); $user_id = wp_insert_user( $userdata ); if ( ! is_wp_error( $user_id ) ) { do_action( 'dozent_user_signup_after', $user_id ); /** * Should user login right after signup */ $login_after_signup = apply_filters( 'dozent_login_after_signup', true ); if ( $login_after_signup && ! is_admin() ) { $user = get_user_by( 'id', $user_id ); if ( $user ) { wp_set_current_user( $user_id, $user->user_login ); wp_set_auth_cookie( $user_id ); } } } else { dozent_add_form_errors( $user_id->get_error_messages() ); return; } dozent_set_flash_message( __( 'Signup done', 'dozent' ) ); if ( is_admin() ) { $this->upgrade_to_instructor( $user_id ); dozent_redirect(); } else { dozent_redirect( dozent_dashboard_uri() ); } } /** * When an user submit becoming instructor form. * * @since DozentLMS 1.0.0 * */ public function become_an_instructor() { $user_id = get_current_user_id(); $this->upgrade_to_instructor( $user_id ); dozent_set_flash_message( 'Thank you for your interest to become an instructor', 'dozent' ); } public function upgrade_to_instructor( $user_id ) { update_user_meta( $user_id, '_dozent_instructor', 'pending' ); $auto_approve = (bool) dozent_get_option( 'auto_approve_instructor' ); if ( $auto_approve || is_admin() ) { dozent_instructor_approve( $user_id ); } } /** * LogOut from the dashboard * * @since DozentLMS 1.0.0 */ public function dashboard_logout() { wp_logout(); wp_redirect( dozent_dashboard_uri() ); die(); } public function profile_edit() { dozent_checking_nonce(); $user_id = get_current_user_id(); $submit_btn = dozent_input_text( 'submit_btn' ); if ( $submit_btn === 'delete_profile_photo' ) { delete_user_meta( $user_id, '_dozent_profile_photo' ); dozent_redirect(); } $config = apply_filters( 'dozent_profile_edit_form_validation_rules', [ [ 'field' => 'first_name', 'label' => __( 'First Name', 'dozent' ), 'rules' => 'required', ], [ 'field' => 'last_name', 'label' => __( 'Last Name', 'dozent' ), 'rules' => 'required', ], ] ); $validator = dozent_form_validate( $config ); /** * If validator fail, stop script */ if ( ! $validator->success ) { return; } $first_name = dozent_input_text( 'first_name' ); $last_name = dozent_input_text( 'last_name' ); $display_name = dozent_input_text( 'display_name' ); $userdata = array( 'ID' => $user_id, 'first_name' => $first_name, 'last_name' => $last_name, 'display_name' => $display_name, ); $user_id = wp_update_user( $userdata ); if ( ! is_wp_error( $user_id ) ) { $bio = dozent_input_textarea( 'dozent_profile_bio' ); $job_title = dozent_input_text( 'dozent_profile_job_title' ); update_user_meta( $user_id, '_dozent_profile_bio', $bio ); update_user_meta( $user_id, '_dozent_profile_job_title', $job_title ); $social_links = dozent_get_user_social_links(); foreach ( $social_links as $key => $link ) { $user_social_value = dozent_input_text( $key ); if ( $user_social_value ) { update_user_meta( $user_id, $key, $user_social_value ); } else { delete_user_meta( $user_id, $key ); } } $this->photo_upload( $user_id ); } do_action( 'dozent_profile_update_after', $user_id ); dozent_set_flash_message( __( 'Profile has been udpated', 'dozent' ) ); dozent_redirect(); } /** * Upload Profile photo from the dashboard * * * @since DozentLMS 1.0.0 * * * @param int $user_id User ID */ public function photo_upload( $user_id ) { $files = dozent_sanitize_array( $_FILES ); $profile_photo = dozent_array_get( 'dozent_profile_photo_file', $files ); $profile_photo_size = dozent_array_get( 'size', $profile_photo ); $profile_photo_type = dozent_array_get( 'type', $profile_photo ); if ( $profile_photo_size && strpos( $profile_photo_type, 'image' ) !== false ) { if ( ! function_exists( 'wp_handle_upload' ) ) { require_once( ABSPATH . 'wp-admin/includes/file.php' ); } $upload_overrides = array( 'test_form' => false ); $movefile = wp_handle_upload( $profile_photo, $upload_overrides ); if ( $movefile && ! isset( $movefile['error'] ) ) { $file_path = dozent_array_get( 'file', $movefile ); $file_url = dozent_array_get( 'url', $movefile ); $media_id = wp_insert_attachment( array( 'guid' => $file_path, 'post_mime_type' => mime_content_type( $file_path ), 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ), 'post_content' => '', 'post_status' => 'inherit' ), $file_path, 0 ); if ( $media_id ) { // wp_generate_attachment_metadata() won't work if you do not include this file require_once( ABSPATH . 'wp-admin/includes/image.php' ); // Generate and save the attachment metas into the database wp_update_attachment_metadata( $media_id, wp_generate_attachment_metadata( $media_id, $file_path ) ); //Update it to user profile update_user_meta( $user_id, '_dozent_profile_photo', $media_id ); } } } } public function reset_password() { dozent_checking_nonce(); $config = apply_filters( 'dozent_reset_password_form_validation_rules', [ [ 'field' => 'current_password', 'label' => __( 'Current Password', 'dozent' ), 'rules' => 'required', ], [ 'field' => 'password', 'label' => __( 'New Password', 'dozent' ), 'rules' => 'required|confirm', ], [ 'field' => 'password_confirmation', 'label' => __( 'Confirm New Password', 'dozent' ), 'rules' => 'required', ], ] ); $validator = dozent_form_validate( $config ); /** * If validator fail, stop script */ if ( ! $validator->success ) { return; } $current_password = dozent_input_text( 'current_password' ); $new_password = dozent_input_text( 'password' ); //Check old password $user = wp_get_current_user(); $valid_current_password = wp_check_password( $current_password, $user->user_pass, $user->ID ); if ( $valid_current_password ) { //Change the password immediately $user_id = get_current_user_id(); wp_set_password( $new_password, $user->ID ); dozent_set_flash_message( __( 'Password has been reset successfully', 'dozent' ) ); } else { dozent_set_flash_message( __( 'Invalid current password', 'dozent' ), 'warning' ); } dozent_redirect(); } /** * Instructor can publish their course directly or not. * * * @since DozentLMS 1.0.0 * */ function can_publish_course() { $can_publish_course = (bool) dozent_get_option( 'instructor_can_publish_course' ); $instructor = get_role( DOZENT_INSTRUCTOR_ROLE ); if ( $can_publish_course ) { $instructor->add_cap( 'publish_dozent_course' ); } else { $instructor->remove_cap( 'publish_dozent_course' ); } } }
Methods
- __construct
- become_an_instructor — When an user submit becoming instructor form.
- can_publish_course — Instructor can publish their course directly or not.
- dashboard_logout — LogOut from the dashboard
- photo_upload — Upload Profile photo from the dashboard
- profile_edit
- reset_password
- upgrade_to_instructor
- user_signup