| @@ -1,272 +1,272 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -/** | |
| 4 | - * Class Instructor | |
| 5 | - * @package TUTOR | |
| 6 | - * | |
| 7 | - * @since v.1.0.0 | |
| 8 | - */ | |
| 9 | - | |
| 10 | -namespace TUTOR; | |
| 11 | - | |
| 12 | -if ( ! defined( 'ABSPATH' ) ) | |
| 13 | - exit; | |
| 14 | - | |
| 15 | - | |
| 16 | -class Instructor { | |
| 17 | - | |
| 18 | - protected $error_msgs = ''; | |
| 19 | - public function __construct() { | |
| 20 | - add_action('template_redirect', array($this, 'register_instructor')); | |
| 21 | - add_action('template_redirect', array($this, 'apply_instructor')); | |
| 22 | - | |
| 23 | - //Add instructor from admin panel. | |
| 24 | - add_action('wp_ajax_tutor_add_instructor', array($this, 'add_new_instructor')); | |
| 25 | - | |
| 26 | - /** | |
| 27 | - * Instructor Approval | |
| 28 | - * Block Unblock | |
| 29 | - * | |
| 30 | - * @since v.1.5.3 | |
| 31 | - */ | |
| 32 | - add_action('wp_ajax_instructor_approval_action', array($this, 'instructor_approval_action')); | |
| 33 | - | |
| 34 | - /** | |
| 35 | - * Check if instructor can publish courses | |
| 36 | - * @since v.1.5.9 | |
| 37 | - */ | |
| 38 | - add_action('tutor_option_save_after', array($this, 'can_publish_tutor_courses')); | |
| 39 | - } | |
| 40 | - | |
| 41 | - /** | |
| 42 | - * Register new user and mark him as instructor | |
| 43 | - * | |
| 44 | - * @since v.1.0.0 | |
| 45 | - */ | |
| 46 | - public function register_instructor(){ | |
| 47 | - if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_register_instructor' ){ | |
| 48 | - return; | |
| 49 | - } | |
| 50 | - //Checking nonce | |
| 51 | - tutor_utils()->checking_nonce(); | |
| 52 | - | |
| 53 | - $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( | |
| 54 | - 'first_name' => __('First name field is required', 'tutor'), | |
| 55 | - 'last_name' => __('Last name field is required', 'tutor'), | |
| 56 | - 'email' => __('E-Mail field is required', 'tutor'), | |
| 57 | - 'user_login' => __('User Name field is required', 'tutor'), | |
| 58 | - 'password' => __('Password field is required', 'tutor'), | |
| 59 | - 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), | |
| 60 | - )); | |
| 61 | - | |
| 62 | - $validation_errors = array(); | |
| 63 | - foreach ($required_fields as $required_key => $required_value){ | |
| 64 | - if (empty($_POST[$required_key])){ | |
| 65 | - $validation_errors[$required_key] = $required_value; | |
| 66 | - } | |
| 67 | - } | |
| 68 | - | |
| 69 | - if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { | |
| 70 | - $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); | |
| 71 | - } | |
| 72 | - if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ | |
| 73 | - $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); | |
| 74 | - } | |
| 75 | - | |
| 76 | - if (count($validation_errors)){ | |
| 77 | - $this->error_msgs = $validation_errors; | |
| 78 | - add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); | |
| 79 | - return; | |
| 80 | - } | |
| 81 | - | |
| 82 | - $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); | |
| 83 | - $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); | |
| 84 | - $email = sanitize_text_field(tutor_utils()->input_old('email')); | |
| 85 | - $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); | |
| 86 | - $password = sanitize_text_field(tutor_utils()->input_old('password')); | |
| 87 | - | |
| 88 | - $userdata = array( | |
| 89 | - 'user_login' => $user_login, | |
| 90 | - 'user_email' => $email, | |
| 91 | - 'first_name' => $first_name, | |
| 92 | - 'last_name' => $last_name, | |
| 93 | - //'role' => tutor()->instructor_role, | |
| 94 | - 'user_pass' => $password, | |
| 95 | - ); | |
| 96 | - | |
| 97 | - $user_id = wp_insert_user( $userdata ) ; | |
| 98 | - if ( ! is_wp_error($user_id)){ | |
| 99 | - update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); | |
| 100 | - update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); | |
| 101 | - | |
| 102 | - do_action('tutor_new_instructor_after', $user_id); | |
| 103 | - | |
| 104 | - $user = get_user_by( 'id', $user_id ); | |
| 105 | - if( $user ) { | |
| 106 | - wp_set_current_user( $user_id, $user->user_login ); | |
| 107 | - wp_set_auth_cookie( $user_id ); | |
| 108 | - } | |
| 109 | - }else{ | |
| 110 | - $this->error_msgs = $user_id->get_error_messages(); | |
| 111 | - add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); | |
| 112 | - return; | |
| 113 | - } | |
| 114 | - | |
| 115 | - wp_redirect(tutor_utils()->input_old('_wp_http_referer')); | |
| 116 | - die(); | |
| 117 | - } | |
| 118 | - | |
| 119 | - public function tutor_instructor_form_validation_errors(){ | |
| 120 | - return $this->error_msgs; | |
| 121 | - } | |
| 122 | - | |
| 123 | - /** | |
| 124 | - * | |
| 125 | - * Usage for instructor applying when a user already logged in | |
| 126 | - * | |
| 127 | - * @since v.1.0.0 | |
| 128 | - */ | |
| 129 | - public function apply_instructor(){ | |
| 130 | - if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_apply_instructor' ){ | |
| 131 | - return; | |
| 132 | - } | |
| 133 | - //Checking nonce | |
| 134 | - tutor_utils()->checking_nonce(); | |
| 135 | - | |
| 136 | - $user_id = get_current_user_id(); | |
| 137 | - if ($user_id){ | |
| 138 | - if (tutor_utils()->is_instructor()){ | |
| 139 | - die(__('Already applied for instructor', 'tutor')); | |
| 140 | - }else{ | |
| 141 | - update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); | |
| 142 | - update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); | |
| 143 | - | |
| 144 | - do_action('tutor_new_instructor_after', $user_id); | |
| 145 | - } | |
| 146 | - }else{ | |
| 147 | - die(__('Permission denied', 'tutor')); | |
| 148 | - } | |
| 149 | - | |
| 150 | - wp_redirect(tutor_utils()->input_old('_wp_http_referer')); | |
| 151 | - die(); | |
| 152 | - } | |
| 153 | - | |
| 154 | - | |
| 155 | - public function add_new_instructor(){ | |
| 156 | - tutils()->checking_nonce(); | |
| 157 | - | |
| 158 | - $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( | |
| 159 | - 'first_name' => __('First name field is required', 'tutor'), | |
| 160 | - 'last_name' => __('Last name field is required', 'tutor'), | |
| 161 | - 'email' => __('E-Mail field is required', 'tutor'), | |
| 162 | - 'user_login' => __('User Name field is required', 'tutor'), | |
| 163 | - 'phone_number' => __('Phone Number field is required', 'tutor'), | |
| 164 | - 'password' => __('Password field is required', 'tutor'), | |
| 165 | - 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), | |
| 166 | - )); | |
| 167 | - | |
| 168 | - $validation_errors = array(); | |
| 169 | - foreach ($required_fields as $required_key => $required_value){ | |
| 170 | - if (empty($_POST[$required_key])){ | |
| 171 | - $validation_errors[$required_key] = $required_value; | |
| 172 | - } | |
| 173 | - } | |
| 174 | - | |
| 175 | - if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { | |
| 176 | - $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); | |
| 177 | - } | |
| 178 | - if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ | |
| 179 | - $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); | |
| 180 | - } | |
| 181 | - | |
| 182 | - if (count($validation_errors)){ | |
| 183 | - wp_send_json_error(array('errors' => $validation_errors)); | |
| 184 | - } | |
| 185 | - | |
| 186 | - $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); | |
| 187 | - $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); | |
| 188 | - $email = sanitize_text_field(tutor_utils()->input_old('email')); | |
| 189 | - $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); | |
| 190 | - $phone_number = sanitize_text_field(tutor_utils()->input_old('phone_number')); | |
| 191 | - $password = sanitize_text_field(tutor_utils()->input_old('password')); | |
| 192 | - $tutor_profile_bio = wp_kses_post(tutor_utils()->input_old('tutor_profile_bio')); | |
| 193 | - | |
| 194 | - $userdata = apply_filters('add_new_instructor_data', array( | |
| 195 | - 'user_login' => $user_login, | |
| 196 | - 'user_email' => $email, | |
| 197 | - 'first_name' => $first_name, | |
| 198 | - 'last_name' => $last_name, | |
| 199 | - 'role' => tutor()->instructor_role, | |
| 200 | - 'user_pass' => $password, | |
| 201 | - )); | |
| 202 | - | |
| 203 | - do_action('tutor_add_new_instructor_before'); | |
| 204 | - | |
| 205 | - $user_id = wp_insert_user( $userdata ) ; | |
| 206 | - if ( ! is_wp_error($user_id)) { | |
| 207 | - update_user_meta($user_id, 'phone_number', $phone_number); | |
| 208 | - update_user_meta($user_id, 'description', $tutor_profile_bio); | |
| 209 | - update_user_meta($user_id, '_tutor_profile_bio', $tutor_profile_bio); | |
| 210 | - update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); | |
| 211 | - update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'approved')); | |
| 212 | - | |
| 213 | - do_action('tutor_add_new_instructor_after', $user_id); | |
| 214 | - | |
| 215 | - wp_send_json_success(array('msg' => __('Instructor has been added successfully', 'tutor') )); | |
| 216 | - } | |
| 217 | - | |
| 218 | - wp_send_json_error(array('errors' => $user_id)); | |
| 219 | - } | |
| 220 | - | |
| 221 | - public function instructor_approval_action(){ | |
| 222 | - tutils()->checking_nonce(); | |
| 223 | - | |
| 224 | - $instructor_id = (int) sanitize_text_field(tutils()->array_get('instructor_id', $_POST)); | |
| 225 | - $action = sanitize_text_field(tutils()->array_get('action_name', $_POST)); | |
| 226 | - | |
| 227 | - if( 'approve' === $action ) { | |
| 228 | - do_action('tutor_before_approved_instructor', $instructor_id); | |
| 229 | - | |
| 230 | - update_user_meta($instructor_id, '_tutor_instructor_status', 'approved'); | |
| 231 | - update_user_meta($instructor_id, '_tutor_instructor_approved', tutor_time()); | |
| 232 | - | |
| 233 | - $instructor = new \WP_User($instructor_id); | |
| 234 | - $instructor->add_role(tutor()->instructor_role); | |
| 235 | - | |
| 236 | - //TODO: send E-Mail to this user about instructor approval, should via hook | |
| 237 | - do_action('tutor_after_approved_instructor', $instructor_id); | |
| 238 | - } | |
| 239 | - | |
| 240 | - if( 'blocked' === $action ) { | |
| 241 | - do_action('tutor_before_blocked_instructor', $instructor_id); | |
| 242 | - update_user_meta($instructor_id, '_tutor_instructor_status', 'blocked'); | |
| 243 | - | |
| 244 | - $instructor = new \WP_User($instructor_id); | |
| 245 | - $instructor->remove_role(tutor()->instructor_role); | |
| 246 | - do_action('tutor_after_blocked_instructor', $instructor_id); | |
| 247 | - | |
| 248 | - //TODO: send E-Mail to this user about instructor blocked, should via hook | |
| 249 | - } | |
| 250 | - | |
| 251 | - wp_send_json_success(); | |
| 252 | - } | |
| 253 | - | |
| 254 | - /** | |
| 255 | - * Can instructor publish courses directly | |
| 256 | - * Fixed in Gutenberg @since v.1.5.9 | |
| 257 | - */ | |
| 258 | - | |
| 259 | - public function can_publish_tutor_courses(){ | |
| 260 | - $can_publish_course = (bool) tutor_utils()->get_option('instructor_can_publish_course'); | |
| 261 | - | |
| 262 | - $instructor_role = tutor()->instructor_role; | |
| 263 | - $instructor = get_role( $instructor_role ); | |
| 264 | - | |
| 265 | - if ($can_publish_course){ | |
| 266 | - $instructor->add_cap('publish_tutor_courses'); | |
| 267 | - }else{ | |
| 268 | - $instructor->remove_cap('publish_tutor_courses'); | |
| 269 | - } | |
| 270 | - } | |
| 271 | - | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * Class Instructor | |
| 5 | + * @package TUTOR | |
| 6 | + * | |
| 7 | + * @since v.1.0.0 | |
| 8 | + */ | |
| 9 | + | |
| 10 | +namespace TUTOR; | |
| 11 | + | |
| 12 | +if ( ! defined( 'ABSPATH' ) ) | |
| 13 | + exit; | |
| 14 | + | |
| 15 | + | |
| 16 | +class Instructor { | |
| 17 | + | |
| 18 | + protected $error_msgs = ''; | |
| 19 | + public function __construct() { | |
| 20 | + add_action('template_redirect', array($this, 'register_instructor')); | |
| 21 | + add_action('template_redirect', array($this, 'apply_instructor')); | |
| 22 | + | |
| 23 | + //Add instructor from admin panel. | |
| 24 | + add_action('wp_ajax_tutor_add_instructor', array($this, 'add_new_instructor')); | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * Instructor Approval | |
| 28 | + * Block Unblock | |
| 29 | + * | |
| 30 | + * @since v.1.5.3 | |
| 31 | + */ | |
| 32 | + add_action('wp_ajax_instructor_approval_action', array($this, 'instructor_approval_action')); | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * Check if instructor can publish courses | |
| 36 | + * @since v.1.5.9 | |
| 37 | + */ | |
| 38 | + add_action('tutor_option_save_after', array($this, 'can_publish_tutor_courses')); | |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * Register new user and mark him as instructor | |
| 43 | + * | |
| 44 | + * @since v.1.0.0 | |
| 45 | + */ | |
| 46 | + public function register_instructor(){ | |
| 47 | + if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_register_instructor' ){ | |
| 48 | + return; | |
| 49 | + } | |
| 50 | + //Checking nonce | |
| 51 | + tutor_utils()->checking_nonce(); | |
| 52 | + | |
| 53 | + $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( | |
| 54 | + 'first_name' => __('First name field is required', 'tutor'), | |
| 55 | + 'last_name' => __('Last name field is required', 'tutor'), | |
| 56 | + 'email' => __('E-Mail field is required', 'tutor'), | |
| 57 | + 'user_login' => __('User Name field is required', 'tutor'), | |
| 58 | + 'password' => __('Password field is required', 'tutor'), | |
| 59 | + 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), | |
| 60 | + )); | |
| 61 | + | |
| 62 | + $validation_errors = array(); | |
| 63 | + foreach ($required_fields as $required_key => $required_value){ | |
| 64 | + if (empty($_POST[$required_key])){ | |
| 65 | + $validation_errors[$required_key] = $required_value; | |
| 66 | + } | |
| 67 | + } | |
| 68 | + | |
| 69 | + if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { | |
| 70 | + $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); | |
| 71 | + } | |
| 72 | + if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ | |
| 73 | + $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); | |
| 74 | + } | |
| 75 | + | |
| 76 | + if (count($validation_errors)){ | |
| 77 | + $this->error_msgs = $validation_errors; | |
| 78 | + add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); | |
| 79 | + return; | |
| 80 | + } | |
| 81 | + | |
| 82 | + $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); | |
| 83 | + $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); | |
| 84 | + $email = sanitize_text_field(tutor_utils()->input_old('email')); | |
| 85 | + $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); | |
| 86 | + $password = sanitize_text_field(tutor_utils()->input_old('password')); | |
| 87 | + | |
| 88 | + $userdata = array( | |
| 89 | + 'user_login' => $user_login, | |
| 90 | + 'user_email' => $email, | |
| 91 | + 'first_name' => $first_name, | |
| 92 | + 'last_name' => $last_name, | |
| 93 | + //'role' => tutor()->instructor_role, | |
| 94 | + 'user_pass' => $password, | |
| 95 | + ); | |
| 96 | + | |
| 97 | + $user_id = wp_insert_user( $userdata ) ; | |
| 98 | + if ( ! is_wp_error($user_id)){ | |
| 99 | + update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); | |
| 100 | + update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); | |
| 101 | + | |
| 102 | + do_action('tutor_new_instructor_after', $user_id); | |
| 103 | + | |
| 104 | + $user = get_user_by( 'id', $user_id ); | |
| 105 | + if( $user ) { | |
| 106 | + wp_set_current_user( $user_id, $user->user_login ); | |
| 107 | + wp_set_auth_cookie( $user_id ); | |
| 108 | + } | |
| 109 | + }else{ | |
| 110 | + $this->error_msgs = $user_id->get_error_messages(); | |
| 111 | + add_filter('tutor_instructor_register_validation_errors', array($this, 'tutor_instructor_form_validation_errors')); | |
| 112 | + return; | |
| 113 | + } | |
| 114 | + | |
| 115 | + wp_redirect(tutor_utils()->input_old('_wp_http_referer')); | |
| 116 | + die(); | |
| 117 | + } | |
| 118 | + | |
| 119 | + public function tutor_instructor_form_validation_errors(){ | |
| 120 | + return $this->error_msgs; | |
| 121 | + } | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * | |
| 125 | + * Usage for instructor applying when a user already logged in | |
| 126 | + * | |
| 127 | + * @since v.1.0.0 | |
| 128 | + */ | |
| 129 | + public function apply_instructor(){ | |
| 130 | + if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_apply_instructor' ){ | |
| 131 | + return; | |
| 132 | + } | |
| 133 | + //Checking nonce | |
| 134 | + tutor_utils()->checking_nonce(); | |
| 135 | + | |
| 136 | + $user_id = get_current_user_id(); | |
| 137 | + if ($user_id){ | |
| 138 | + if (tutor_utils()->is_instructor()){ | |
| 139 | + die(__('Already applied for instructor', 'tutor')); | |
| 140 | + }else{ | |
| 141 | + update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); | |
| 142 | + update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'pending')); | |
| 143 | + | |
| 144 | + do_action('tutor_new_instructor_after', $user_id); | |
| 145 | + } | |
| 146 | + }else{ | |
| 147 | + die(__('Permission denied', 'tutor')); | |
| 148 | + } | |
| 149 | + | |
| 150 | + wp_redirect(tutor_utils()->input_old('_wp_http_referer')); | |
| 151 | + die(); | |
| 152 | + } | |
| 153 | + | |
| 154 | + | |
| 155 | + public function add_new_instructor(){ | |
| 156 | + tutils()->checking_nonce(); | |
| 157 | + | |
| 158 | + $required_fields = apply_filters('tutor_instructor_registration_required_fields', array( | |
| 159 | + 'first_name' => __('First name field is required', 'tutor'), | |
| 160 | + 'last_name' => __('Last name field is required', 'tutor'), | |
| 161 | + 'email' => __('E-Mail field is required', 'tutor'), | |
| 162 | + 'user_login' => __('User Name field is required', 'tutor'), | |
| 163 | + 'phone_number' => __('Phone Number field is required', 'tutor'), | |
| 164 | + 'password' => __('Password field is required', 'tutor'), | |
| 165 | + 'password_confirmation' => __('Password Confirmation field is required', 'tutor'), | |
| 166 | + )); | |
| 167 | + | |
| 168 | + $validation_errors = array(); | |
| 169 | + foreach ($required_fields as $required_key => $required_value){ | |
| 170 | + if (empty($_POST[$required_key])){ | |
| 171 | + $validation_errors[$required_key] = $required_value; | |
| 172 | + } | |
| 173 | + } | |
| 174 | + | |
| 175 | + if (!filter_var(tutor_utils()->input_old('email'), FILTER_VALIDATE_EMAIL)) { | |
| 176 | + $validation_errors['email'] = __('Valid E-Mail is required', 'tutor'); | |
| 177 | + } | |
| 178 | + if (tutor_utils()->input_old('password') !== tutor_utils()->input_old('password_confirmation')){ | |
| 179 | + $validation_errors['password_confirmation'] = __('Confirm password does not matched with Password field', 'tutor'); | |
| 180 | + } | |
| 181 | + | |
| 182 | + if (count($validation_errors)){ | |
| 183 | + wp_send_json_error(array('errors' => $validation_errors)); | |
| 184 | + } | |
| 185 | + | |
| 186 | + $first_name = sanitize_text_field(tutor_utils()->input_old('first_name')); | |
| 187 | + $last_name = sanitize_text_field(tutor_utils()->input_old('last_name')); | |
| 188 | + $email = sanitize_text_field(tutor_utils()->input_old('email')); | |
| 189 | + $user_login = sanitize_text_field(tutor_utils()->input_old('user_login')); | |
| 190 | + $phone_number = sanitize_text_field(tutor_utils()->input_old('phone_number')); | |
| 191 | + $password = sanitize_text_field(tutor_utils()->input_old('password')); | |
| 192 | + $tutor_profile_bio = wp_kses_post(tutor_utils()->input_old('tutor_profile_bio')); | |
| 193 | + | |
| 194 | + $userdata = apply_filters('add_new_instructor_data', array( | |
| 195 | + 'user_login' => $user_login, | |
| 196 | + 'user_email' => $email, | |
| 197 | + 'first_name' => $first_name, | |
| 198 | + 'last_name' => $last_name, | |
| 199 | + 'role' => tutor()->instructor_role, | |
| 200 | + 'user_pass' => $password, | |
| 201 | + )); | |
| 202 | + | |
| 203 | + do_action('tutor_add_new_instructor_before'); | |
| 204 | + | |
| 205 | + $user_id = wp_insert_user( $userdata ) ; | |
| 206 | + if ( ! is_wp_error($user_id)) { | |
| 207 | + update_user_meta($user_id, 'phone_number', $phone_number); | |
| 208 | + update_user_meta($user_id, 'description', $tutor_profile_bio); | |
| 209 | + update_user_meta($user_id, '_tutor_profile_bio', $tutor_profile_bio); | |
| 210 | + update_user_meta($user_id, '_is_tutor_instructor', tutor_time()); | |
| 211 | + update_user_meta($user_id, '_tutor_instructor_status', apply_filters('tutor_initial_instructor_status', 'approved')); | |
| 212 | + | |
| 213 | + do_action('tutor_add_new_instructor_after', $user_id); | |
| 214 | + | |
| 215 | + wp_send_json_success(array('msg' => __('Instructor has been added successfully', 'tutor') )); | |
| 216 | + } | |
| 217 | + | |
| 218 | + wp_send_json_error(array('errors' => $user_id)); | |
| 219 | + } | |
| 220 | + | |
| 221 | + public function instructor_approval_action(){ | |
| 222 | + tutils()->checking_nonce(); | |
| 223 | + | |
| 224 | + $instructor_id = (int) sanitize_text_field(tutils()->array_get('instructor_id', $_POST)); | |
| 225 | + $action = sanitize_text_field(tutils()->array_get('action_name', $_POST)); | |
| 226 | + | |
| 227 | + if( 'approve' === $action ) { | |
| 228 | + do_action('tutor_before_approved_instructor', $instructor_id); | |
| 229 | + | |
| 230 | + update_user_meta($instructor_id, '_tutor_instructor_status', 'approved'); | |
| 231 | + update_user_meta($instructor_id, '_tutor_instructor_approved', tutor_time()); | |
| 232 | + | |
| 233 | + $instructor = new \WP_User($instructor_id); | |
| 234 | + $instructor->add_role(tutor()->instructor_role); | |
| 235 | + | |
| 236 | + //TODO: send E-Mail to this user about instructor approval, should via hook | |
| 237 | + do_action('tutor_after_approved_instructor', $instructor_id); | |
| 238 | + } | |
| 239 | + | |
| 240 | + if( 'blocked' === $action ) { | |
| 241 | + do_action('tutor_before_blocked_instructor', $instructor_id); | |
| 242 | + update_user_meta($instructor_id, '_tutor_instructor_status', 'blocked'); | |
| 243 | + | |
| 244 | + $instructor = new \WP_User($instructor_id); | |
| 245 | + $instructor->remove_role(tutor()->instructor_role); | |
| 246 | + do_action('tutor_after_blocked_instructor', $instructor_id); | |
| 247 | + | |
| 248 | + //TODO: send E-Mail to this user about instructor blocked, should via hook | |
| 249 | + } | |
| 250 | + | |
| 251 | + wp_send_json_success(); | |
| 252 | + } | |
| 253 | + | |
| 254 | + /** | |
| 255 | + * Can instructor publish courses directly | |
| 256 | + * Fixed in Gutenberg @since v.1.5.9 | |
| 257 | + */ | |
| 258 | + | |
| 259 | + public function can_publish_tutor_courses(){ | |
| 260 | + $can_publish_course = (bool) tutor_utils()->get_option('instructor_can_publish_course'); | |
| 261 | + | |
| 262 | + $instructor_role = tutor()->instructor_role; | |
| 263 | + $instructor = get_role( $instructor_role ); | |
| 264 | + | |
| 265 | + if ($can_publish_course){ | |
| 266 | + $instructor->add_cap('publish_tutor_courses'); | |
| 267 | + }else{ | |
| 268 | + $instructor->remove_cap('publish_tutor_courses'); | |
| 269 | + } | |
| 270 | + } | |
| 271 | + | |
| 272 | 272 | } |