| 1 |
<?php |
| 2 |
|
| 3 |
namespace TUTOR; |
| 4 |
|
| 5 |
if ( ! defined( 'ABSPATH' ) ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
|
| 10 |
class User { |
| 11 |
|
| 12 |
private static $hide_registration_notice = false; |
| 13 |
|
| 14 |
public function __construct() { |
| 15 |
add_action( 'edit_user_profile', array( $this, 'edit_user_profile' ) ); |
| 16 |
add_action( 'show_user_profile', array( $this, 'edit_user_profile' ), 10, 1 ); |
| 17 |
|
| 18 |
add_action( 'profile_update', array( $this, 'profile_update' ) ); |
| 19 |
add_action( 'set_user_role', array( $this, 'set_user_role' ), 10, 3 ); |
| 20 |
|
| 21 |
add_action('wp_ajax_tutor_user_photo_remove', array($this, 'tutor_user_photo_remove')); |
| 22 |
add_action('wp_ajax_tutor_user_photo_upload', array($this, 'update_user_photo')); |
| 23 |
|
| 24 |
add_action( 'admin_notices', array( $this, 'show_registration_disabled' ) ); |
| 25 |
add_action( 'admin_init', array( $this, 'hide_notices' ) ); |
| 26 |
} |
| 27 |
|
| 28 |
private $profile_layout = array( |
| 29 |
'pp-circle', |
| 30 |
'pp-rectangle', |
| 31 |
'no-cp', |
| 32 |
); |
| 33 |
|
| 34 |
public function edit_user_profile($user){ |
| 35 |
include tutor()->path.'views/metabox/user-profile-fields.php'; |
| 36 |
} |
| 37 |
|
| 38 |
private function delete_existing_user_photo( $user_id, $type ) { |
| 39 |
$meta_key = $type == 'cover_photo' ? '_tutor_cover_photo' : '_tutor_profile_photo'; |
| 40 |
$photo_id = get_user_meta( $user_id, $meta_key, true ); |
| 41 |
is_numeric( $photo_id ) ? wp_delete_attachment( $photo_id, true ) : 0; |
| 42 |
delete_user_meta( $user_id, $meta_key ); |
| 43 |
} |
| 44 |
|
| 45 |
public function tutor_user_photo_remove(){ |
| 46 |
tutor_utils()->checking_nonce(); |
| 47 |
|
| 48 |
$this->delete_existing_user_photo(get_current_user_id(), tutor_sanitize_data( $_POST['photo_type'] )); |
| 49 |
} |
| 50 |
|
| 51 |
public function update_user_photo(){ |
| 52 |
tutor_utils()->checking_nonce(); |
| 53 |
|
| 54 |
$user_id = get_current_user_id(); |
| 55 |
$meta_key = sanitize_text_field( $_POST['photo_type'] ) == 'cover_photo' ? '_tutor_cover_photo' : '_tutor_profile_photo'; |
| 56 |
|
| 57 |
/** |
| 58 |
* Photo Update from profile |
| 59 |
*/ |
| 60 |
$photo = tutor_utils()->array_get('photo_file', $_FILES); |
| 61 |
$photo_size = tutor_utils()->array_get('size', $photo); |
| 62 |
$photo_type = tutor_utils()->array_get('type', $photo); |
| 63 |
|
| 64 |
if ( $photo_size && strpos( $photo_type, 'image' ) !== false ) { |
| 65 |
if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 66 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 67 |
} |
| 68 |
|
| 69 |
$upload_overrides = array( 'test_form' => false ); |
| 70 |
$movefile = wp_handle_upload( $photo, $upload_overrides ); |
| 71 |
|
| 72 |
if ( $movefile && ! isset( $movefile['error'] ) ) { |
| 73 |
$file_path = tutor_utils()->array_get( 'file', $movefile ); |
| 74 |
$file_url = tutor_utils()->array_get( 'url', $movefile ); |
| 75 |
|
| 76 |
$media_id = wp_insert_attachment( |
| 77 |
array( |
| 78 |
'guid' => $file_path, |
| 79 |
'post_mime_type' => mime_content_type( $file_path ), |
| 80 |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_url ) ), |
| 81 |
'post_content' => '', |
| 82 |
'post_status' => 'inherit', |
| 83 |
), |
| 84 |
$file_path, |
| 85 |
0 |
| 86 |
); |
| 87 |
|
| 88 |
if ( $media_id ) { |
| 89 |
// wp_generate_attachment_metadata() won't work if you do not include this file |
| 90 |
require_once ABSPATH . 'wp-admin/includes/image.php'; |
| 91 |
|
| 92 |
// Generate and save the attachment metas into the database |
| 93 |
wp_update_attachment_metadata( $media_id, wp_generate_attachment_metadata( $media_id, $file_path ) ); |
| 94 |
|
| 95 |
// Update it to user profile |
| 96 |
$this->delete_existing_user_photo( $user_id, tutor_sanitize_data($_POST['photo_type']) ); |
| 97 |
update_user_meta( $user_id, $meta_key, $media_id ); |
| 98 |
|
| 99 |
exit( json_encode( array( 'status' => 'success' ) ) ); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
public function profile_update($user_id){ |
| 106 |
if (tutor_utils()->array_get('tutor_action', $_POST) !== 'tutor_profile_update_by_wp' ){ |
| 107 |
return; |
| 108 |
} |
| 109 |
|
| 110 |
$_tutor_profile_job_title = sanitize_text_field( tutor_utils()->avalue_dot( '_tutor_profile_job_title', $_POST ) ); |
| 111 |
$_tutor_profile_bio = wp_kses_post( tutor_utils()->avalue_dot( '_tutor_profile_bio', $_POST ) ); |
| 112 |
$_tutor_profile_image = wp_kses_post( tutor_utils()->avalue_dot( '_tutor_profile_photo', $_POST ) ); |
| 113 |
|
| 114 |
update_user_meta( $user_id, '_tutor_profile_job_title', $_tutor_profile_job_title ); |
| 115 |
update_user_meta( $user_id, '_tutor_profile_bio', $_tutor_profile_bio ); |
| 116 |
update_user_meta( $user_id, '_tutor_profile_photo', $_tutor_profile_image ); |
| 117 |
} |
| 118 |
|
| 119 |
public function set_user_role( $user_id, $role, $old_roles ) { |
| 120 |
$instructor_role = tutor()->instructor_role; |
| 121 |
|
| 122 |
if ( in_array( $instructor_role, $old_roles ) ) { |
| 123 |
// tutor_utils()->remove_instructor_role($user_id); |
| 124 |
} |
| 125 |
|
| 126 |
// if ($role === $instructor_role){ |
| 127 |
if ( $role === $instructor_role || in_array( $instructor_role, $old_roles ) ) { |
| 128 |
tutor_utils()->add_instructor_role( $user_id ); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public function hide_notices() { |
| 133 |
if(is_admin() && isset( $_GET['tutor-hide-notice'] ) && $_GET['tutor-hide-notice']=='registration') { |
| 134 |
tutor_utils()->checking_nonce('get'); |
| 135 |
|
| 136 |
if ( isset( $_GET['tutor-registration'] ) && $_GET['tutor-registration'] === 'enable' ) { |
| 137 |
update_option( 'users_can_register', 1 ); |
| 138 |
} else { |
| 139 |
self::$hide_registration_notice = true; |
| 140 |
setcookie( 'tutor_notice_hide_registration', 1, time() + ( 86400 * 30 ), tutor()->basepath ); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
public function show_registration_disabled() { |
| 146 |
|
| 147 |
if ( |
| 148 |
self::$hide_registration_notice || |
| 149 |
!tutor_utils()->is_tutor_dashboard() || |
| 150 |
get_option( 'users_can_register' ) || |
| 151 |
isset( $_COOKIE['tutor_notice_hide_registration'] ) || |
| 152 |
! current_user_can( 'manage_options' ) |
| 153 |
) { |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
$hide_url = wp_nonce_url( add_query_arg( 'tutor-hide-notice', 'registration' ), tutor()->nonce_action, tutor()->nonce ); |
| 158 |
?> |
| 159 |
<div class="wrap tutor-user-registration-notice-wrapper"> |
| 160 |
<div class="tutor-user-registration-notice"> |
| 161 |
<div> |
| 162 |
<img src="<?php echo esc_url( tutor()->url . 'assets/images/icon-info-round.svg' ); ?>"/> |
| 163 |
</div> |
| 164 |
<div> |
| 165 |
<?php _e( 'As membership is turned off, students and instructors will not be able to sign up. <strong>Press Enable</strong> or go to <strong>Settings > General > Membership</strong> and enable "Anyone can register".' ); ?> |
| 166 |
</div> |
| 167 |
<div> |
| 168 |
<a href="<?php echo esc_url( add_query_arg( 'tutor-registration', 'enable', $hide_url ) ); ?>"><?php _e( 'Enable', 'tutor' ); ?></a> |
| 169 |
<hr/> |
| 170 |
<a href="<?php echo esc_url( $hide_url ); ?>"><?php _e( 'Dismiss', 'tutor' ); ?></a> |
| 171 |
</div> |
| 172 |
</div> |
| 173 |
</div> |
| 174 |
<?php |
| 175 |
} |
| 176 |
} |
| 177 |
|