| @@ -1,5 +1,10 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 3 | +use LearnPress\Models\UserItems\UserItemModel; | |
| 4 | +use LearnPress\Models\UserModel; | |
| 5 | +use LearnPress\Services\UserService; | |
| 6 | + | |
| 2 | 7 | /** |
| 3 | 8 | * Common functions to process actions about user |
| 4 | 9 | * |
| 5 | 10 | * @author ThimPress |
| @@ -5,14 +10,31 @@ | ||
| 5 | 10 | * @author ThimPress |
| 6 | 11 | * @package LearnPress/Functions/User |
| 7 | 12 | * @version 1.0 |
| 8 | 13 | */ |
| 9 | - | |
| 10 | 14 | function learn_press_get_user_profile_tabs() { |
| 11 | 15 | return LP_Profile::instance()->get_tabs(); |
| 12 | 16 | } |
| 13 | 17 | |
| 14 | 18 | /** |
| 19 | + * Ensure new users receive a public slug. | |
| 20 | + * | |
| 21 | + * @param int $user_id | |
| 22 | + * | |
| 23 | + * @return void | |
| 24 | + */ | |
| 25 | +function learn_press_maybe_generate_user_pretty_slug_on_register( int $user_id ) { | |
| 26 | + $userModel = UserModel::find( $user_id, true ); | |
| 27 | + if ( ! $userModel ) { | |
| 28 | + return; | |
| 29 | + } | |
| 30 | + | |
| 31 | + $userModel->generate_pretty_slug(); | |
| 32 | +} | |
| 33 | + | |
| 34 | +//add_action( 'user_register', 'learn_press_maybe_generate_user_pretty_slug_on_register' ); | |
| 35 | + | |
| 36 | +/** | |
| 15 | 37 | * Delete user data by user ID |
| 16 | 38 | * |
| 17 | 39 | * @param int $user_id |
| 18 | 40 | * @param int $course_id |
| @@ -50,32 +72,13 @@ | ||
| 50 | 72 | @$wpdb->query( $query ); |
| 51 | 73 | } |
| 52 | 74 | |
| 53 | 75 | /** |
| 54 | - * Get user_item_id field in table learnpress_user_items | |
| 55 | - * with the user_id, item_id. If $course_id is not passed | |
| 56 | - * then item_id is ID of a course. Otherwise, item_id is | |
| 57 | - * ID of an item (like quiz/lesson). | |
| 58 | - * | |
| 59 | - * @param int $user_id | |
| 60 | - * @param int $item_id | |
| 61 | - * @param int $course_id | |
| 62 | - * | |
| 63 | - * @return bool | |
| 64 | - * @editor tungnx | |
| 65 | - * @reason this function only get cache, not handle get user_item_id | |
| 66 | - * @depecated 4.1.6.9 | |
| 67 | - */ | |
| 68 | -function learn_press_get_user_item_id( $user_id, $item_id, $course_id = 0 /* added 3.0.0 */ ) { | |
| 69 | - return false; | |
| 70 | -} | |
| 71 | - | |
| 72 | -/** | |
| 73 | 76 | * Get current user ID |
| 74 | 77 | * |
| 75 | - * @return int | |
| 78 | + * @return int|string LP_User is int, LP_User_Guest is string | |
| 76 | 79 | */ |
| 77 | -function learn_press_get_current_user_id(): int { | |
| 80 | +function learn_press_get_current_user_id() { | |
| 78 | 81 | $user = learn_press_get_current_user(); |
| 79 | 82 | |
| 80 | 83 | if ( ! $user ) { |
| 81 | 84 | return 0; |
| @@ -89,9 +92,9 @@ | ||
| 89 | 92 | * If current user is not logged in, return a GUEST user |
| 90 | 93 | * |
| 91 | 94 | * @param bool $create_temp - Optional. Create temp user if user is not logged in. |
| 92 | 95 | * |
| 93 | - * @return bool|LP_User|LP_User_Guest | |
| 96 | + * @return LP_User|LP_User_Guest | |
| 94 | 97 | * @editor tungnx |
| 95 | 98 | * @modify 4.1.4 |
| 96 | 99 | * @version 1.0.1 |
| 97 | 100 | */ |
| @@ -107,260 +110,53 @@ | ||
| 107 | 110 | } |
| 108 | 111 | |
| 109 | 112 | if ( ! function_exists( 'learn_press_get_user' ) ) { |
| 110 | 113 | /** |
| 111 | - * Get user by ID. Return false if the user does not exists. | |
| 114 | + * Get user by ID. Return false if the user does not exist. | |
| 115 | + * If user_id = 0, return a guest user. | |
| 112 | 116 | * |
| 113 | - * @param int $user_id | |
| 117 | + * @param int $user_id | |
| 114 | 118 | * @param bool $current |
| 115 | 119 | * |
| 116 | - * @return LP_User|LP_User_Guest|mixed | |
| 117 | - * Todo: check this function - tungnx | |
| 120 | + * @return LP_User|LP_User_Guest|false | |
| 121 | + * @since 3.0.0 | |
| 122 | + * @version 4.0.1 | |
| 118 | 123 | */ |
| 119 | - function learn_press_get_user( $user_id, $current = false, $force_new = false ) { | |
| 120 | - $is_guest = false; | |
| 121 | - if ( ! is_null( LearnPress::instance()->session ) && $user_id != LearnPress::instance()->session->guest_user_id ) { | |
| 122 | - if ( $current && ! get_user_by( 'id', $user_id ) ) { | |
| 123 | - $user_id = get_current_user_id(); | |
| 124 | - } | |
| 125 | - } | |
| 126 | - | |
| 127 | - if ( ! $user_id && isset( LearnPress::instance()->session ) ) { | |
| 128 | - if ( ! LearnPress::instance()->session->guest_user_id ) { | |
| 129 | - LearnPress::instance()->session->set_customer_session_cookie( 1 ); | |
| 130 | - LearnPress::instance()->session->guest_user_id = time(); | |
| 131 | - } | |
| 132 | - | |
| 133 | - $user_id = LearnPress::instance()->session->guest_user_id; | |
| 134 | - $is_guest = true; | |
| 135 | - } | |
| 136 | - | |
| 137 | - if ( ! $user_id ) { | |
| 124 | + function learn_press_get_user( $user_id = 0, $current = false, $force_new = false ) { | |
| 125 | + $userClass = $user_id && get_user_by( 'ID', $user_id ) ? 'LP_User' : ( $user_id == 0 ? 'LP_User_Guest' : false ); | |
| 126 | + if ( false === $userClass ) { | |
| 138 | 127 | return false; |
| 139 | 128 | } |
| 140 | 129 | |
| 141 | - $user_id = '' . $user_id; | |
| 142 | - | |
| 143 | - if ( $force_new || ! array_key_exists( $user_id, LP_Global::$users ) ) { | |
| 144 | - /** | |
| 145 | - * LP Hook. | |
| 146 | - * | |
| 147 | - * Filter the default class name to get LP user. | |
| 148 | - * | |
| 149 | - * @since 3.3.0 | |
| 150 | - */ | |
| 151 | - $userClass = apply_filters( 'learn-press/user-class', $is_guest ? 'LP_User_Guest' : 'LP_User', $is_guest ); | |
| 152 | - | |
| 153 | - LP_Global::$users[ $user_id ] = new $userClass( $user_id ); | |
| 154 | - | |
| 155 | - do_action( 'learn-press/get-user', LP_Global::$users[ $user_id ], $user_id ); | |
| 156 | - } | |
| 157 | - | |
| 158 | - return LP_Global::$users[ $user_id ]; | |
| 130 | + return new $userClass( $user_id ); | |
| 159 | 131 | } |
| 160 | 132 | } |
| 161 | 133 | |
| 162 | 134 | /** |
| 163 | - * Add more 2 user roles teacher and student | |
| 135 | + * Add roles and add capabilities for roles | |
| 164 | 136 | */ |
| 165 | 137 | function learn_press_add_user_roles() { |
| 138 | + $item_types = [ | |
| 139 | + LP_COURSE_CPT, | |
| 140 | + LP_LESSON_CPT, | |
| 141 | + LP_ORDER_CPT, | |
| 142 | + ]; | |
| 166 | 143 | |
| 167 | - $settings = LP_Settings::instance(); | |
| 168 | - | |
| 169 | - /* translators: user role */ | |
| 170 | - _x( 'LP Instructor', 'User role' ); | |
| 171 | - | |
| 172 | - add_role( | |
| 173 | - LP_TEACHER_ROLE, | |
| 174 | - 'LP Instructor', | |
| 175 | - array() | |
| 176 | - ); | |
| 177 | - | |
| 178 | - $course_cap = LP_COURSE_CPT . 's'; | |
| 179 | - $lesson_cap = LP_LESSON_CPT . 's'; | |
| 180 | - $order_cap = LP_ORDER_CPT . 's'; | |
| 181 | - | |
| 182 | - $teacher = get_role( LP_TEACHER_ROLE ); | |
| 183 | - if ( $teacher ) { | |
| 184 | - $teacher->add_cap( 'read_private_' . $course_cap ); | |
| 185 | - $teacher->add_cap( 'delete_published_' . $course_cap ); | |
| 186 | - $teacher->add_cap( 'edit_published_' . $course_cap ); | |
| 187 | - $teacher->add_cap( 'edit_' . $course_cap ); | |
| 188 | - $teacher->add_cap( 'delete_' . $course_cap ); | |
| 189 | - $teacher->add_cap( 'unfiltered_html' ); | |
| 190 | - | |
| 191 | - $settings->get( 'required_review' ); | |
| 192 | - | |
| 193 | - if ( $settings->get( 'required_review' ) == 'yes' ) { | |
| 194 | - $teacher->remove_cap( 'publish_' . $course_cap ); | |
| 195 | - } else { | |
| 196 | - $teacher->add_cap( 'publish_' . $course_cap ); | |
| 197 | - } | |
| 198 | - | |
| 199 | - $teacher->add_cap( 'read_private_' . $lesson_cap ); | |
| 200 | - $teacher->add_cap( 'delete_published_' . $lesson_cap ); | |
| 201 | - $teacher->add_cap( 'edit_published_' . $lesson_cap ); | |
| 202 | - $teacher->add_cap( 'edit_' . $lesson_cap ); | |
| 203 | - $teacher->add_cap( 'delete_' . $lesson_cap ); | |
| 204 | - $teacher->add_cap( 'publish_' . $lesson_cap ); | |
| 205 | - $teacher->add_cap( 'upload_files' ); | |
| 206 | - $teacher->add_cap( 'read' ); | |
| 207 | - $teacher->add_cap( 'edit_posts' ); | |
| 144 | + foreach ( $item_types as $item_type ) { | |
| 145 | + UserService::instance()->add_capabilities_for_roles( $item_type ); | |
| 208 | 146 | } |
| 209 | - | |
| 210 | - // administrator | |
| 211 | - $admin = get_role( 'administrator' ); | |
| 212 | - if ( $admin ) { | |
| 213 | - $admin->add_cap( 'read_private_' . $course_cap ); | |
| 214 | - $admin->add_cap( 'delete_' . $course_cap ); | |
| 215 | - $admin->add_cap( 'delete_published_' . $course_cap ); | |
| 216 | - $admin->add_cap( 'edit_' . $course_cap ); | |
| 217 | - $admin->add_cap( 'edit_published_' . $course_cap ); | |
| 218 | - $admin->add_cap( 'publish_' . $course_cap ); | |
| 219 | - $admin->add_cap( 'delete_private_' . $course_cap ); | |
| 220 | - $admin->add_cap( 'edit_private_' . $course_cap ); | |
| 221 | - $admin->add_cap( 'delete_others_' . $course_cap ); | |
| 222 | - $admin->add_cap( 'edit_others_' . $course_cap ); | |
| 223 | - | |
| 224 | - $admin->add_cap( 'read_private_' . $lesson_cap ); | |
| 225 | - $admin->add_cap( 'delete_' . $lesson_cap ); | |
| 226 | - $admin->add_cap( 'delete_published_' . $lesson_cap ); | |
| 227 | - $admin->add_cap( 'edit_' . $lesson_cap ); | |
| 228 | - $admin->add_cap( 'edit_published_' . $lesson_cap ); | |
| 229 | - $admin->add_cap( 'publish_' . $lesson_cap ); | |
| 230 | - $admin->add_cap( 'delete_private_' . $lesson_cap ); | |
| 231 | - $admin->add_cap( 'edit_private_' . $lesson_cap ); | |
| 232 | - $admin->add_cap( 'delete_others_' . $lesson_cap ); | |
| 233 | - $admin->add_cap( 'edit_others_' . $lesson_cap ); | |
| 234 | - | |
| 235 | - $admin->add_cap( 'delete_' . $order_cap ); | |
| 236 | - $admin->add_cap( 'delete_published_' . $order_cap ); | |
| 237 | - $admin->add_cap( 'edit_' . $order_cap ); | |
| 238 | - $admin->add_cap( 'edit_published_' . $order_cap ); | |
| 239 | - $admin->add_cap( 'publish_' . $order_cap ); | |
| 240 | - $admin->add_cap( 'delete_private_' . $order_cap ); | |
| 241 | - $admin->add_cap( 'edit_private_' . $order_cap ); | |
| 242 | - $admin->add_cap( 'delete_others_' . $order_cap ); | |
| 243 | - $admin->add_cap( 'edit_others_' . $order_cap ); | |
| 244 | - } | |
| 245 | 147 | } |
| 246 | 148 | |
| 247 | -add_action( 'init', 'learn_press_add_user_roles' ); | |
| 248 | - | |
| 249 | 149 | /** |
| 250 | - * @param null $user_id | |
| 251 | - * @param array $args | |
| 252 | - * | |
| 253 | - * @return mixed | |
| 254 | - */ | |
| 255 | -function learn_press_get_user_questions( $user_id = null, $args = array() ) { | |
| 256 | - if ( ! $user_id ) { | |
| 257 | - $user_id = get_current_user_id(); | |
| 258 | - } | |
| 259 | - | |
| 260 | - return learn_press_get_user( $user_id )->get_questions( $args ); | |
| 261 | -} | |
| 262 | - | |
| 263 | -/** | |
| 264 | - * Get the type of current user | |
| 265 | - * | |
| 266 | - * @param null $check_type | |
| 267 | - * | |
| 268 | - * @return bool|string | |
| 269 | - */ | |
| 270 | -function learn_press_current_user_is( $check_type = null ) { | |
| 271 | - global $current_user; | |
| 272 | - $user_roles = $current_user->roles; | |
| 273 | - $user_type = ''; | |
| 274 | - | |
| 275 | - if ( in_array( 'lpr_teacher', $user_roles ) ) { | |
| 276 | - $user_type = 'instructor'; | |
| 277 | - } elseif ( in_array( 'lp_teacher', $user_roles ) ) { | |
| 278 | - $user_type = 'instructor'; | |
| 279 | - } elseif ( in_array( 'administrator', $user_roles ) ) { | |
| 280 | - $user_type = 'administrator'; | |
| 281 | - } | |
| 282 | - | |
| 283 | - return $check_type ? $check_type == $user_type : $user_type; | |
| 284 | -} | |
| 285 | - | |
| 286 | -function learn_press_user_has_roles( $roles, $user_id = null ) { | |
| 287 | - $has_role = false; | |
| 288 | - if ( ! $user_id ) { | |
| 289 | - $user = wp_get_current_user(); | |
| 290 | - } else { | |
| 291 | - $user = get_user_by( 'id', $user_id ); | |
| 292 | - } | |
| 293 | - $available_roles = (array) $user->roles; | |
| 294 | - if ( is_array( $roles ) ) { | |
| 295 | - foreach ( $roles as $role ) { | |
| 296 | - if ( in_array( $role, $available_roles ) ) { | |
| 297 | - $has_role = true; | |
| 298 | - break; // only need one of roles is in available | |
| 299 | - } | |
| 300 | - } | |
| 301 | - } else { | |
| 302 | - if ( in_array( $roles, $available_roles ) ) { | |
| 303 | - $has_role = true; | |
| 304 | - } | |
| 305 | - } | |
| 306 | - | |
| 307 | - return $has_role; | |
| 308 | -} | |
| 309 | - | |
| 310 | -function learn_press_current_user_can_view_profile_section( $section, $user ) { | |
| 311 | - $current_user = wp_get_current_user(); | |
| 312 | - $view = true; | |
| 313 | - if ( $user->get_data( 'user_login' ) != $current_user->user_login && $section == LP_Settings::instance()->get( | |
| 314 | - 'profile_endpoints.profile-orders', | |
| 315 | - 'profile-orders' | |
| 316 | - ) ) { | |
| 317 | - $view = false; | |
| 318 | - } | |
| 319 | - | |
| 320 | - return apply_filters( 'learn_press_current_user_can_view_profile_section', $view, $section, $user ); | |
| 321 | -} | |
| 322 | - | |
| 323 | -/*function learn_press_profile_tab_courses_content( $current, $tab, $user ) { | |
| 324 | - learn_press_get_template( | |
| 325 | - 'profile/tabs/courses.php', | |
| 326 | - array( | |
| 327 | - 'user' => $user, | |
| 328 | - 'current' => $current, | |
| 329 | - 'tab' => $tab, | |
| 330 | - ) | |
| 331 | - ); | |
| 332 | -}*/ | |
| 333 | - | |
| 334 | -function learn_press_profile_tab_quizzes_content( $current, $tab, $user ) { | |
| 335 | - learn_press_get_template( | |
| 336 | - 'profile/tabs/quizzes.php', | |
| 337 | - array( | |
| 338 | - 'user' => $user, | |
| 339 | - 'current' => $current, | |
| 340 | - 'tab' => $tab, | |
| 341 | - ) | |
| 342 | - ); | |
| 343 | -} | |
| 344 | - | |
| 345 | -function learn_press_profile_tab_orders_content( $current, $tab, $user ) { | |
| 346 | - learn_press_get_template( | |
| 347 | - 'profile/tabs/orders.php', | |
| 348 | - array( | |
| 349 | - 'user' => $user, | |
| 350 | - 'current' => $current, | |
| 351 | - 'tab' => $tab, | |
| 352 | - ) | |
| 353 | - ); | |
| 354 | -} | |
| 355 | - | |
| 356 | -/** | |
| 357 | 150 | * Get queried user in profile link |
| 358 | 151 | * |
| 359 | 152 | * @return false|WP_User |
| 360 | 153 | * @since 3.0.0 |
| 154 | + * @deprecated 4.2.9.1 | |
| 361 | 155 | */ |
| 362 | 156 | function learn_press_get_profile_user() { |
| 157 | + return LP_Profile::instance()->get_user_current(); | |
| 158 | + | |
| 363 | 159 | return LP_Profile::get_queried_user(); |
| 364 | 160 | } |
| 365 | 161 | |
| 366 | 162 | |
| @@ -393,18 +189,19 @@ | ||
| 393 | 189 | * .... |
| 394 | 190 | * field_name_n => value n |
| 395 | 191 | * ) |
| 396 | 192 | * @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields. |
| 397 | - * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0). | |
| 398 | - * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0). | |
| 193 | + * @param bool $update_cache - Optional. Should be update to cache or not (since 3.0.0). | |
| 194 | + * @param bool $update_extra_fields_as_meta - Optional. Update extra fields as item meta (since 3.1.0). | |
| 399 | 195 | * |
| 400 | 196 | * @return mixed |
| 401 | 197 | */ |
| 402 | -function learn_press_update_user_item_field( $fields, $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) { | |
| 198 | +function learn_press_update_user_item_field( array $fields = array(), $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) { | |
| 403 | 199 | global $wpdb; |
| 404 | 200 | |
| 405 | - // Table fields | |
| 201 | + // Table fields format. | |
| 406 | 202 | $table_fields = array( |
| 203 | + 'user_item_id' => '%d', | |
| 407 | 204 | 'user_id' => '%d', |
| 408 | 205 | 'item_id' => '%d', |
| 409 | 206 | 'ref_id' => '%d', |
| 410 | 207 | 'start_time' => '%s', |
| @@ -426,31 +223,27 @@ | ||
| 426 | 223 | if ( LP_COURSE_CPT === $item_type ) { |
| 427 | 224 | if ( 'completed' === $fields['status'] ) { |
| 428 | 225 | $fields['status'] = 'finished'; |
| 429 | 226 | } |
| 430 | - } else { | |
| 431 | - if ( 'finished' === $fields['status'] ) { | |
| 227 | + } elseif ( 'finished' === $fields['status'] ) { | |
| 432 | 228 | $fields['status'] = 'completed'; |
| 433 | - } | |
| 434 | 229 | } |
| 435 | 230 | } |
| 436 | 231 | |
| 437 | - $data = array(); | |
| 438 | - $data_format = array(); | |
| 439 | - $date_time_fields = array( | |
| 440 | - 'start_time', | |
| 441 | - 'end_time', | |
| 442 | - ); | |
| 443 | - | |
| 232 | + $data = array(); | |
| 233 | + $data_format = array(); | |
| 444 | 234 | foreach ( $fields as $field => $value ) { |
| 445 | 235 | if ( ! empty( $table_fields[ $field ] ) ) { |
| 446 | 236 | $data[ $field ] = $value; |
| 447 | 237 | |
| 448 | 238 | // Do not format the date-time field if it's value is NULL |
| 449 | - if ( in_array( $field, $date_time_fields ) && ! $value ) { | |
| 239 | + if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) { | |
| 450 | 240 | $data[ $field ] = null; |
| 451 | 241 | $data_format[] = ''; |
| 452 | 242 | } else { |
| 243 | + if ( $value instanceof LP_Datetime ) { | |
| 244 | + $data[ $field ] = $value->toSql(); | |
| 245 | + } | |
| 453 | 246 | $data_format[] = $table_fields[ $field ]; |
| 454 | 247 | } |
| 455 | 248 | } |
| 456 | 249 | } |
| @@ -461,18 +254,17 @@ | ||
| 461 | 254 | array( 'user_item_id' => $fields['user_item_id'] ) |
| 462 | 255 | ); |
| 463 | 256 | } |
| 464 | 257 | |
| 465 | - if ( $where && empty( $where['user_id'] ) ) { | |
| 466 | - $where['user_id'] = ! empty( $fields['user_id'] ) ? $fields['user_id'] : learn_press_get_current_user_id(); | |
| 467 | - } | |
| 468 | - | |
| 258 | + // Set where and where format | |
| 469 | 259 | $where_format = array(); |
| 260 | + if ( is_array( $where ) && ! empty( $where ) ) { | |
| 261 | + if ( empty( $where['user_id'] ) ) { | |
| 262 | + $where['user_id'] = ! empty( $fields['user_id'] ) ? $fields['user_id'] : learn_press_get_current_user_id(); | |
| 263 | + } | |
| 470 | 264 | |
| 471 | - // Build where and where format | |
| 472 | - if ( $where ) { | |
| 473 | 265 | foreach ( $where as $field => $value ) { |
| 474 | - if ( ! empty( $table_fields[ $field ] ) ) { | |
| 266 | + if ( isset( $table_fields[ $field ] ) ) { | |
| 475 | 267 | $where_format[] = $table_fields[ $field ]; |
| 476 | 268 | } |
| 477 | 269 | } |
| 478 | 270 | } |
| @@ -483,17 +275,9 @@ | ||
| 483 | 275 | |
| 484 | 276 | $inserted = false; |
| 485 | 277 | $updated = false; |
| 486 | 278 | |
| 487 | - // Ensure all fields are instance of LP_Datetime have to | |
| 488 | - // convert to string of datetime. | |
| 489 | - foreach ( $data as $k => $v ) { | |
| 490 | - if ( $v instanceof LP_Datetime ) { | |
| 491 | - $data[ $k ] = $v->toSql(); | |
| 492 | - } | |
| 493 | - } | |
| 494 | - | |
| 495 | - // If $where is not empty consider we are updating | |
| 279 | + // Update to database. | |
| 496 | 280 | if ( $where ) { |
| 497 | 281 | $updated = $wpdb->update( |
| 498 | 282 | $wpdb->learnpress_user_items, |
| 499 | 283 | $data, |
| @@ -501,16 +285,14 @@ | ||
| 501 | 285 | $data_format, |
| 502 | 286 | $where_format |
| 503 | 287 | ); |
| 504 | 288 | } else { |
| 505 | - | |
| 506 | - // Otherwise, insert a new one | |
| 289 | + // Insert to database. | |
| 507 | 290 | if ( $wpdb->insert( |
| 508 | 291 | $wpdb->learnpress_user_items, |
| 509 | 292 | $data, |
| 510 | 293 | $data_format |
| 511 | - ) | |
| 512 | - ) { | |
| 294 | + ) ) { | |
| 513 | 295 | $inserted = $wpdb->insert_id; |
| 514 | 296 | } |
| 515 | 297 | } |
| 516 | 298 | |
| @@ -529,12 +311,51 @@ | ||
| 529 | 311 | } elseif ( $updated ) { |
| 530 | 312 | $updated_item = learn_press_get_user_item( $where ); |
| 531 | 313 | } |
| 532 | 314 | |
| 315 | + // Clear caches. | |
| 316 | + if ( $updated_item ) { | |
| 317 | + // Clear cache total students enrolled. | |
| 318 | + if ( isset( $updated_item->item_type ) | |
| 319 | + && LP_COURSE_CPT === $updated_item->item_type | |
| 320 | + && isset( $updated_item->item_id ) ) { | |
| 321 | + $lp_course_cache = new LP_Course_Cache( true ); | |
| 322 | + $lp_course_cache->clean_total_students_enrolled( $updated_item->item_id ); | |
| 323 | + $lp_course_cache->clean_total_students_enrolled_or_purchased( $updated_item->item_id ); | |
| 324 | + // Clear cache count students many courses. | |
| 325 | + $lp_courses_cache = new LP_Courses_Cache( true ); | |
| 326 | + $lp_courses_cache->clear_cache_on_group( LP_Courses_Cache::KEYS_COUNT_STUDENT_COURSES ); | |
| 327 | + } | |
| 328 | + // Clear cache user item. | |
| 329 | + $lp_user_items_cache = new LP_User_Items_Cache(); | |
| 330 | + $lp_user_items_cache->clean_user_item( | |
| 331 | + array( | |
| 332 | + $updated_item->user_id, | |
| 333 | + $updated_item->item_id, | |
| 334 | + $updated_item->item_type, | |
| 335 | + ) | |
| 336 | + ); | |
| 337 | + // Clear cache userItemModel | |
| 338 | + $userItemModel = UserItemModel::find_user_item( | |
| 339 | + $updated_item->user_id, | |
| 340 | + $updated_item->item_id, | |
| 341 | + $updated_item->item_type, | |
| 342 | + $updated_item->ref_id, | |
| 343 | + $updated_item->ref_type, | |
| 344 | + true | |
| 345 | + ); | |
| 346 | + $userItemModel->clean_caches(); | |
| 347 | + } | |
| 348 | + | |
| 349 | + do_action( 'learn-press/updated-user-item-field', $updated_item ); | |
| 350 | + | |
| 533 | 351 | /** |
| 534 | 352 | * If there is some fields does not contain in the main table |
| 535 | - * then consider update them as meta data. | |
| 353 | + * then consider update them as metadata. | |
| 354 | + * | |
| 355 | + * @comment by tungnx - 4.1.7.3 | |
| 536 | 356 | */ |
| 357 | + /* | |
| 537 | 358 | if ( $updated_item && $update_extra_fields_as_meta ) { |
| 538 | 359 | $extra_fields = array_diff_key( $fields, $table_fields ); |
| 539 | 360 | if ( $extra_fields ) { |
| 540 | 361 | foreach ( $extra_fields as $meta_key => $meta_value ) { |
| @@ -552,11 +373,11 @@ | ||
| 552 | 373 | learn_press_update_user_item_meta( $updated_item->user_item_id, $meta_key, $meta_value ); |
| 553 | 374 | } |
| 554 | 375 | } |
| 555 | 376 | } |
| 556 | - } | |
| 377 | + }*/ | |
| 557 | 378 | |
| 558 | - do_action( 'learn-press/updated-user-item-meta', $updated_item ); | |
| 379 | + // do_action( 'learn-press/updated-user-item-meta', $updated_item ); | |
| 559 | 380 | |
| 560 | 381 | return $updated_item; |
| 561 | 382 | } |
| 562 | 383 | |
| @@ -563,9 +384,9 @@ | ||
| 563 | 384 | /** |
| 564 | 385 | * Get user item row(s) from user items table by multiple WHERE conditional |
| 565 | 386 | * |
| 566 | 387 | * @param array|int $where |
| 567 | - * @param bool $single | |
| 388 | + * @param bool $single | |
| 568 | 389 | * |
| 569 | 390 | * @return array |
| 570 | 391 | */ |
| 571 | 392 | function learn_press_get_user_item( $where, $single = true ) { |
| @@ -604,9 +425,9 @@ | ||
| 604 | 425 | SELECT * |
| 605 | 426 | FROM {$wpdb->prefix}learnpress_user_items |
| 606 | 427 | WHERE " . join( ' AND ', $where_str ) . ' |
| 607 | 428 | ORDER BY user_item_id DESC |
| 608 | - ', | |
| 429 | + ', | |
| 609 | 430 | $where |
| 610 | 431 | ); |
| 611 | 432 | if ( $single || ! empty( $where['user_item_id'] ) ) { |
| 612 | 433 | $item = $wpdb->get_row( $query ); |
| @@ -620,11 +441,11 @@ | ||
| 620 | 441 | |
| 621 | 442 | /** |
| 622 | 443 | * Get user item meta from user_itemmeta table |
| 623 | 444 | * |
| 624 | - * @param int $user_item_id . | |
| 445 | + * @param int $user_item_id . | |
| 625 | 446 | * @param string $meta_key . |
| 626 | - * @param bool $single . | |
| 447 | + * @param bool $single . | |
| 627 | 448 | * |
| 628 | 449 | * @return mixed |
| 629 | 450 | */ |
| 630 | 451 | function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) { |
| @@ -638,11 +459,11 @@ | ||
| 638 | 459 | |
| 639 | 460 | /** |
| 640 | 461 | * Add user item meta into table user_itemmeta |
| 641 | 462 | * |
| 642 | - * @param int $user_item_id | |
| 463 | + * @param int $user_item_id | |
| 643 | 464 | * @param string $meta_key |
| 644 | - * @param mixed $meta_value | |
| 465 | + * @param mixed $meta_value | |
| 645 | 466 | * @param string $prev_value |
| 646 | 467 | * |
| 647 | 468 | * @return false|int |
| 648 | 469 | */ |
| @@ -652,11 +473,11 @@ | ||
| 652 | 473 | |
| 653 | 474 | /** |
| 654 | 475 | * Update user item meta to table user_itemmeta |
| 655 | 476 | * |
| 656 | - * @param int $user_item_id | |
| 477 | + * @param int $user_item_id | |
| 657 | 478 | * @param string $meta_key |
| 658 | - * @param mixed $meta_value | |
| 479 | + * @param mixed $meta_value | |
| 659 | 480 | * @param string $prev_value |
| 660 | 481 | * |
| 661 | 482 | * @return bool|int |
| 662 | 483 | */ |
| @@ -667,12 +488,12 @@ | ||
| 667 | 488 | |
| 668 | 489 | /** |
| 669 | 490 | * Update user item meta to table user_itemmeta |
| 670 | 491 | * |
| 671 | - * @param int $object_id | |
| 492 | + * @param int $object_id | |
| 672 | 493 | * @param string $meta_key |
| 673 | - * @param mixed $meta_value | |
| 674 | - * @param bool $delete_all | |
| 494 | + * @param mixed $meta_value | |
| 495 | + * @param bool $delete_all | |
| 675 | 496 | * |
| 676 | 497 | * @return bool|int |
| 677 | 498 | */ |
| 678 | 499 | function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) { |
| @@ -678,98 +499,8 @@ | ||
| 678 | 499 | function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) { |
| 679 | 500 | return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all ); |
| 680 | 501 | } |
| 681 | 502 | |
| 682 | -/** | |
| 683 | - * Get temp users. | |
| 684 | - * | |
| 685 | - * @return array | |
| 686 | - * @depecated 4.1.6.9 | |
| 687 | - */ | |
| 688 | -/*function learn_press_get_temp_users() { | |
| 689 | - return false; | |
| 690 | - if ( false === ( $temp_users = LP_Object_Cache::get( 'learn-press/temp-users' ) ) ) { | |
| 691 | - global $wpdb; | |
| 692 | - $query = $wpdb->prepare( | |
| 693 | - " | |
| 694 | - SELECT ID | |
| 695 | - FROM {$wpdb->users} u | |
| 696 | - INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id AND um.meta_key = %s AND um.meta_value = %s | |
| 697 | - LEFT JOIN {$wpdb->usermeta} um2 ON u.ID = um2.user_id AND um2.meta_key = %s | |
| 698 | - ", | |
| 699 | - '_lp_temp_user', | |
| 700 | - 'yes', | |
| 701 | - '_lp_expiration' | |
| 702 | - ); | |
| 703 | - | |
| 704 | - $temp_users = $wpdb->get_col( $query ); | |
| 705 | - | |
| 706 | - LP_Object_Cache::set( 'learn-press/temp-users', $temp_users ); | |
| 707 | - } | |
| 708 | - | |
| 709 | - return $temp_users; | |
| 710 | -}*/ | |
| 711 | - | |
| 712 | -/** | |
| 713 | - * Update field created_time after added user item meta | |
| 714 | - * | |
| 715 | - * @use updated_{meta_type}_meta hook | |
| 716 | - * | |
| 717 | - * @param $meta_id | |
| 718 | - * @param $object_id | |
| 719 | - * @param $meta_key | |
| 720 | - * @param $_meta_value | |
| 721 | - */ | |
| 722 | -function _learn_press_update_created_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) { | |
| 723 | - global $wpdb; | |
| 724 | - $wpdb->update( | |
| 725 | - $wpdb->learnpress_user_itemmeta, | |
| 726 | - array( 'create_time' => current_time( 'mysql' ) ), | |
| 727 | - array( 'meta_id' => $meta_id ), | |
| 728 | - array( '%s' ), | |
| 729 | - array( '%d' ) | |
| 730 | - ); | |
| 731 | -} | |
| 732 | - | |
| 733 | -// add_action( 'added_learnpress_user_item_meta', '_learn_press_update_created_time_user_item_meta', 10, 4 ); | |
| 734 | - | |
| 735 | -/** | |
| 736 | - * Update field updated_time after updated user item meta | |
| 737 | - * | |
| 738 | - * @use updated_{meta_type}_meta hook | |
| 739 | - * | |
| 740 | - * @param $meta_id | |
| 741 | - * @param $object_id | |
| 742 | - * @param $meta_key | |
| 743 | - * @param $_meta_value | |
| 744 | - */ | |
| 745 | -function _learn_press_update_updated_time_user_item_meta( $meta_id, $object_id, $meta_key, $_meta_value ) { | |
| 746 | - global $wpdb; | |
| 747 | - $wpdb->update( | |
| 748 | - $wpdb->learnpress_user_itemmeta, | |
| 749 | - array( 'update_time' => current_time( 'mysql' ) ), | |
| 750 | - array( 'meta_id' => $meta_id ), | |
| 751 | - array( '%s' ), | |
| 752 | - array( '%d' ) | |
| 753 | - ); | |
| 754 | -} | |
| 755 | - | |
| 756 | -// add_action( 'updated_learnpress_user_item_meta', '_learn_press_update_updated_time_user_item_meta', 10, 4 ); | |
| 757 | - | |
| 758 | -/** | |
| 759 | - * @param $status | |
| 760 | - * @param int $quiz_id | |
| 761 | - * @param int $user_id | |
| 762 | - * @param int $course_id | |
| 763 | - * | |
| 764 | - * @return bool|mixed | |
| 765 | - */ | |
| 766 | -function learn_press_user_has_quiz_status( $status, $quiz_id = 0, $user_id = 0, $course_id = 0 ) { | |
| 767 | - $user = learn_press_get_user( $user_id ); | |
| 768 | - | |
| 769 | - return $user->has_quiz_status( $status, $quiz_id, $course_id ); | |
| 770 | -} | |
| 771 | - | |
| 772 | 503 | if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) { |
| 773 | 504 | /** |
| 774 | 505 | * Filter the avatar |
| 775 | 506 | * |
| @@ -774,109 +505,105 @@ | ||
| 774 | 505 | * Filter the avatar |
| 775 | 506 | * |
| 776 | 507 | * @param string $avatar |
| 777 | 508 | * @param string $id_or_email |
| 778 | - * @param array $size | |
| 509 | + * @param array $args | |
| 779 | 510 | * |
| 780 | 511 | * @return string |
| 512 | + * @since 1.0.0 | |
| 513 | + * @version 1.0.2 | |
| 781 | 514 | */ |
| 782 | - function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $size = array() ) { | |
| 515 | + function learn_press_pre_get_avatar_callback( $avatar, $id_or_email = '', $args = array() ) { | |
| 516 | + try { | |
| 517 | + if ( ( isset( $args['gravatar'] ) && $args['gravatar'] ) || ( $args['default'] && $args['force_default'] ) ) { | |
| 518 | + return $avatar; | |
| 519 | + } | |
| 783 | 520 | |
| 784 | - $profile = LP_Profile::instance(); | |
| 521 | + $user_id = 0; | |
| 785 | 522 | |
| 786 | - if ( ! $profile->is_enable_avatar() ) { | |
| 787 | - return $avatar; | |
| 788 | - } | |
| 523 | + /** | |
| 524 | + * Get the ID of user from $id_or_email | |
| 525 | + */ | |
| 526 | + if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) { | |
| 527 | + $user = get_user_by( 'email', $id_or_email ); | |
| 528 | + if ( $user ) { | |
| 529 | + $user_id = $user->ID; | |
| 530 | + } | |
| 531 | + } elseif ( is_numeric( $id_or_email ) ) { | |
| 532 | + $user_id = $id_or_email; | |
| 533 | + } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) { | |
| 534 | + $user_id = $id_or_email->user_id; | |
| 535 | + } elseif ( $id_or_email instanceof WP_Comment ) { | |
| 536 | + $user = get_user_by( 'email', $id_or_email->comment_author_email ); | |
| 537 | + if ( $user ) { | |
| 538 | + $user_id = $user->ID; | |
| 539 | + } | |
| 540 | + } | |
| 789 | 541 | |
| 790 | - if ( ( isset( $size['gravatar'] ) && $size['gravatar'] ) || ( $size['default'] && $size['force_default'] ) ) { | |
| 791 | - return $avatar; | |
| 792 | - } | |
| 542 | + if ( ! $user_id ) { | |
| 543 | + return $avatar; | |
| 544 | + } | |
| 793 | 545 | |
| 794 | - $user_id = 0; | |
| 795 | - | |
| 796 | - /** | |
| 797 | - * Get the ID of user from $id_or_email | |
| 798 | - */ | |
| 799 | - if ( ! is_numeric( $id_or_email ) && is_string( $id_or_email ) ) { | |
| 800 | - $user = get_user_by( 'email', $id_or_email ); | |
| 801 | - if ( $user ) { | |
| 802 | - $user_id = $user->ID; | |
| 546 | + $user = UserModel::find( $user_id, true ); | |
| 547 | + if ( ! $user ) { | |
| 548 | + return $avatar; | |
| 803 | 549 | } |
| 804 | - } elseif ( is_numeric( $id_or_email ) ) { | |
| 805 | - $user_id = $id_or_email; | |
| 806 | - } elseif ( is_object( $id_or_email ) && isset( $id_or_email->user_id ) && $id_or_email->user_id ) { | |
| 807 | - $user_id = $id_or_email->user_id; | |
| 808 | - } elseif ( $id_or_email instanceof WP_Comment ) { | |
| 809 | - $user = get_user_by( 'email', $id_or_email->comment_author_email ); | |
| 810 | - if ( $user ) { | |
| 811 | - $user_id = $user->ID; | |
| 812 | - } | |
| 813 | - } | |
| 814 | 550 | |
| 815 | - if ( ! $user_id ) { | |
| 816 | - return $avatar; | |
| 817 | - } | |
| 818 | - | |
| 819 | - $user = LP_User_Factory::get_user( $user_id ); | |
| 820 | - | |
| 821 | - $profile_picture_src = $user->get_upload_profile_src(); | |
| 822 | - if ( $profile_picture_src ) { | |
| 823 | - $setting_size = learn_press_get_avatar_thumb_size(); | |
| 824 | - $img_size = ''; | |
| 825 | - | |
| 826 | - // Get avatar size | |
| 827 | - if ( ! is_array( $size ) ) { | |
| 828 | - if ( $size === 'thumbnail' ) { | |
| 829 | - $img_size = ''; | |
| 830 | - $height = $setting_size['height']; | |
| 831 | - $width = $setting_size['width']; | |
| 551 | + $class = array( 'avatar', 'avatar-' . (int) $args['size'], 'photo' ); | |
| 552 | + if ( $args['class'] ) { | |
| 553 | + if ( is_array( $args['class'] ) ) { | |
| 554 | + $class = array_merge( $class, $args['class'] ); | |
| 832 | 555 | } else { |
| 833 | - $height = 250; | |
| 834 | - $width = 250; | |
| 556 | + $class[] = $args['class']; | |
| 835 | 557 | } |
| 836 | - } else { | |
| 837 | - $img_size = $size['size']; | |
| 838 | - $height = $size['height']; | |
| 839 | - $width = $size['width']; | |
| 840 | 558 | } |
| 841 | 559 | |
| 842 | - $avatar = '<img alt="' . esc_attr( $user->get_data( 'display_name' ) ) . '" src="' . esc_url_raw( $profile_picture_src ) . '" class="avatar avatar-' . $img_size . ' photo" height="' . $height . '" width="' . $width . '" />'; | |
| 560 | + $avatar_url = $user->get_avatar_url(); | |
| 561 | + $html_img = sprintf( | |
| 562 | + "<img src='%s' srcset='%s' class='%s' height='%d' width='%d' decoding='async'/>", | |
| 563 | + esc_url( $avatar_url ), | |
| 564 | + esc_url( $avatar_url ) . ' 2x', | |
| 565 | + esc_attr( implode( ' ', $class ) ), | |
| 566 | + (int) $args['height'], | |
| 567 | + (int) $args['width'] | |
| 568 | + ); | |
| 569 | + | |
| 570 | + return $html_img; | |
| 571 | + } catch ( Throwable $e ) { | |
| 572 | + error_log( $e->getMessage() ); | |
| 573 | + | |
| 574 | + return $avatar; | |
| 843 | 575 | } |
| 844 | - | |
| 845 | - return $avatar; | |
| 846 | 576 | } |
| 847 | 577 | } |
| 848 | -add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 1, 5 ); | |
| 578 | +add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 999, 3 ); | |
| 849 | 579 | |
| 850 | 580 | |
| 851 | 581 | function learn_press_user_profile_picture_upload_dir( $width_user = true ) { |
| 852 | - static $upload_dir; | |
| 853 | - if ( ! $upload_dir ) { | |
| 854 | - $upload_dir = wp_upload_dir(); | |
| 855 | - $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user ); | |
| 856 | - if ( $width_user ) { | |
| 857 | - $subdir .= '/' . get_current_user_id(); | |
| 858 | - } | |
| 859 | - $subdir = '/' . $subdir; | |
| 582 | + $upload_dir = wp_upload_dir(); | |
| 583 | + $subdir = apply_filters( 'learn_press_user_profile_folder', 'learn-press-profile', $width_user ); | |
| 584 | + if ( $width_user ) { | |
| 585 | + $subdir .= '/' . get_current_user_id(); | |
| 586 | + } | |
| 587 | + $subdir = '/' . $subdir; | |
| 860 | 588 | |
| 861 | - if ( ! empty( $upload_dir['subdir'] ) ) { | |
| 862 | - $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] ); | |
| 863 | - $u_path = str_replace( '\\', '/', $upload_dir['path'] ); | |
| 589 | + if ( ! empty( $upload_dir['subdir'] ) ) { | |
| 590 | + $u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] ); | |
| 591 | + $u_path = str_replace( '\\', '/', $upload_dir['path'] ); | |
| 864 | 592 | |
| 865 | - $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path ); | |
| 866 | - $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] ); | |
| 867 | - } else { | |
| 868 | - $upload_dir['path'] = $upload_dir['path'] . $subdir; | |
| 869 | - $upload_dir['url'] = $upload_dir['url'] . $subdir; | |
| 870 | - } | |
| 593 | + $upload_dir['path'] = str_replace( $u_subdir, $subdir, $u_path ); | |
| 594 | + $upload_dir['url'] = str_replace( $u_subdir, $subdir, $upload_dir['url'] ); | |
| 595 | + } else { | |
| 596 | + $upload_dir['path'] = $upload_dir['path'] . $subdir; | |
| 597 | + $upload_dir['url'] = $upload_dir['url'] . $subdir; | |
| 598 | + } | |
| 871 | 599 | |
| 872 | - $upload_dir['subdir'] = $subdir; | |
| 600 | + $upload_dir['subdir'] = $subdir; | |
| 873 | 601 | |
| 874 | - // Point path/url to main site if we are in multisite | |
| 875 | - if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) { | |
| 876 | - foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) { | |
| 877 | - $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] ); | |
| 878 | - } | |
| 602 | + // Point path/url to main site if we are in multisite | |
| 603 | + if ( is_multisite() && ! ( is_main_network() && is_main_site() && defined( 'MULTISITE' ) ) ) { | |
| 604 | + foreach ( array( 'path', 'url', 'basedir', 'baseurl' ) as $v ) { | |
| 605 | + $upload_dir[ $v ] = str_replace( '/sites/' . get_current_blog_id(), '', $upload_dir[ $v ] ); | |
| 879 | 606 | } |
| 880 | 607 | } |
| 881 | 608 | |
| 882 | 609 | return $upload_dir; |
| @@ -881,8 +608,9 @@ | ||
| 881 | 608 | |
| 882 | 609 | return $upload_dir; |
| 883 | 610 | } |
| 884 | 611 | |
| 612 | +/* | |
| 885 | 613 | add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 ); |
| 886 | 614 | function _learn_press_before_purchase_course_handler( $course_id, $cart ) { |
| 887 | 615 | // Redirect to login page if user is not logged in |
| 888 | 616 | if ( ! is_user_logged_in() ) { |
| @@ -892,9 +620,9 @@ | ||
| 892 | 620 | 'learn_press_purchase_course_login_redirect', |
| 893 | 621 | learn_press_get_login_url( $return_url ) |
| 894 | 622 | ); |
| 895 | 623 | if ( $redirect !== false ) { |
| 896 | - learn_press_add_message( __( 'Please login to enroll this course', 'learnpress' ) ); | |
| 624 | + learn_press_add_message( __( 'Please log in to enroll in this course', 'learnpress' ) ); | |
| 897 | 625 | |
| 898 | 626 | if ( learn_press_is_ajax() ) { |
| 899 | 627 | learn_press_send_json( |
| 900 | 628 | array( |
| @@ -910,9 +638,9 @@ | ||
| 910 | 638 | } else { |
| 911 | 639 | $user = learn_press_get_current_user(); |
| 912 | 640 | $redirect = false; |
| 913 | 641 | if ( $user->has_finished_course( $course_id ) ) { |
| 914 | - learn_press_add_message( __( 'You have already finished course', 'learnpress' ) ); | |
| 642 | + learn_press_add_message( __( 'You have already finished the course', 'learnpress' ) ); | |
| 915 | 643 | $redirect = true; |
| 916 | 644 | } elseif ( $user->has_purchased_course( $course_id ) ) { |
| 917 | 645 | learn_press_add_message( __( 'You have already enrolled in this course', 'learnpress' ) ); |
| 918 | 646 | $redirect = true; |
| @@ -921,10 +649,11 @@ | ||
| 921 | 649 | wp_redirect( get_the_permalink( $course_id ) ); |
| 922 | 650 | exit(); |
| 923 | 651 | } |
| 924 | 652 | } |
| 925 | -} | |
| 653 | +}*/ | |
| 926 | 654 | |
| 655 | +/* | |
| 927 | 656 | function learn_press_user_is( $role, $user_id = 0 ) { |
| 928 | 657 | if ( ! $user_id ) { |
| 929 | 658 | $user = learn_press_get_current_user(); |
| 930 | 659 | } else { |
| @@ -937,10 +666,11 @@ | ||
| 937 | 666 | return $user->is_instructor(); |
| 938 | 667 | } |
| 939 | 668 | |
| 940 | 669 | return $role; |
| 941 | -} | |
| 670 | +}*/ | |
| 942 | 671 | |
| 672 | +/* | |
| 943 | 673 | function learn_press_profile_tab_edit_content( $current, $tab, $user ) { |
| 944 | 674 | learn_press_get_template( |
| 945 | 675 | 'profile/tabs/edit.php', |
| 946 | 676 | array( |
| @@ -948,25 +678,11 @@ | ||
| 948 | 678 | 'current' => $current, |
| 949 | 679 | 'tab' => $tab, |
| 950 | 680 | ) |
| 951 | 681 | ); |
| 952 | -} | |
| 682 | +}*/ | |
| 953 | 683 | |
| 954 | -function learn_press_get_profile_endpoints() { | |
| 955 | - $endpoints = (array) LP_Settings::instance()->get( 'profile_endpoints' ); | |
| 956 | - $tabs = LP_Profile::instance()->get_tabs(); | |
| 957 | - if ( $tabs ) { | |
| 958 | - foreach ( $tabs as $slug => $info ) { | |
| 959 | - if ( empty( $endpoints[ $slug ] ) ) { | |
| 960 | - $endpoints[ $slug ] = $slug; | |
| 961 | - } | |
| 962 | - } | |
| 963 | - } | |
| 964 | - | |
| 965 | - return apply_filters( 'learn_press_profile_tab_endpoints', $endpoints ); | |
| 966 | -} | |
| 967 | - | |
| 968 | - | |
| 684 | +/* | |
| 969 | 685 | function learn_press_update_user_option( $name, $value, $id = 0 ) { |
| 970 | 686 | if ( ! $id ) { |
| 971 | 687 | $id = get_current_user_id(); |
| 972 | 688 | } |
| @@ -973,16 +689,11 @@ | ||
| 973 | 689 | $key = 'learnpress_user_options'; |
| 974 | 690 | $options = get_user_option( $key, $id ); |
| 975 | 691 | $options[ $name ] = $value; |
| 976 | 692 | update_user_option( $id, $key, $options, true ); |
| 977 | -} | |
| 693 | +}*/ | |
| 978 | 694 | |
| 979 | -/** | |
| 980 | - * @param $name | |
| 981 | - * @param int $id | |
| 982 | - * | |
| 983 | - * @return bool | |
| 984 | - */ | |
| 695 | +/* | |
| 985 | 696 | function learn_press_delete_user_option( $name, $id = 0 ) { |
| 986 | 697 | if ( ! $id ) { |
| 987 | 698 | $id = get_current_user_id(); |
| 988 | 699 | } |
| @@ -995,16 +706,11 @@ | ||
| 995 | 706 | return true; |
| 996 | 707 | } |
| 997 | 708 | |
| 998 | 709 | return false; |
| 999 | -} | |
| 710 | +}*/ | |
| 1000 | 711 | |
| 1001 | -/** | |
| 1002 | - * @param $name | |
| 1003 | - * @param int $id | |
| 1004 | - * | |
| 1005 | - * @return bool | |
| 1006 | - */ | |
| 712 | +/* | |
| 1007 | 713 | function learn_press_get_user_option( $name, $id = 0 ) { |
| 1008 | 714 | if ( ! $id ) { |
| 1009 | 715 | $id = get_current_user_id(); |
| 1010 | 716 | } |
| @@ -1014,9 +720,9 @@ | ||
| 1014 | 720 | return $options[ $name ]; |
| 1015 | 721 | } |
| 1016 | 722 | |
| 1017 | 723 | return false; |
| 1018 | -} | |
| 724 | +}*/ | |
| 1019 | 725 | |
| 1020 | 726 | /** |
| 1021 | 727 | * Update user basic information. |
| 1022 | 728 | * |
| @@ -1027,18 +733,18 @@ | ||
| 1027 | 733 | function learn_press_update_user_profile_basic_information( $wp_error = false ) { |
| 1028 | 734 | $user_id = get_current_user_id(); |
| 1029 | 735 | |
| 1030 | 736 | if ( ! $user_id ) { |
| 1031 | - return new WP_Error( 2, 'User is invalid!' ); | |
| 737 | + return new WP_Error( 2, 'The user is invalid' ); | |
| 1032 | 738 | } |
| 1033 | 739 | |
| 1034 | 740 | $update_data = array( |
| 1035 | 741 | 'ID' => $user_id, |
| 1036 | - 'first_name' => filter_input( INPUT_POST, 'first_name', FILTER_SANITIZE_STRING ), | |
| 1037 | - 'last_name' => filter_input( INPUT_POST, 'last_name', FILTER_SANITIZE_STRING ), | |
| 1038 | - 'description' => filter_input( INPUT_POST, 'description', FILTER_SANITIZE_STRING ), | |
| 1039 | - 'display_name' => filter_input( INPUT_POST, 'account_display_name', FILTER_SANITIZE_STRING ), | |
| 1040 | - 'user_email' => filter_input( INPUT_POST, 'account_email', FILTER_SANITIZE_EMAIL ), | |
| 742 | + 'first_name' => LP_Request::get_param( 'first_name' ), | |
| 743 | + 'last_name' => LP_Request::get_param( 'last_name' ), | |
| 744 | + 'description' => LP_Request::get_param( 'description', '', 'html' ), | |
| 745 | + 'display_name' => LP_Request::get_param( 'account_display_name' ), | |
| 746 | + 'user_email' => LP_Request::get_param( 'account_email' ), | |
| 1041 | 747 | ); |
| 1042 | 748 | |
| 1043 | 749 | $update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data ); |
| 1044 | 750 | $update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' ); |
| @@ -1045,9 +751,9 @@ | ||
| 1045 | 751 | |
| 1046 | 752 | $return = LP_Forms_Handler::update_user_data( $update_data, $update_meta ); |
| 1047 | 753 | |
| 1048 | 754 | // Update for social. |
| 1049 | - $socials = LP_Request::get_array( 'user_profile_social' ); | |
| 755 | + $socials = LP_Request::get_param( 'user_profile_social' ); | |
| 1050 | 756 | $extra_data = get_user_meta( $user_id, '_lp_extra_info', true ); |
| 1051 | 757 | |
| 1052 | 758 | if ( ! empty( $extra_data ) ) { |
| 1053 | 759 | $socials = array_merge( $extra_data, $socials ); |
| @@ -1063,55 +769,51 @@ | ||
| 1063 | 769 | } |
| 1064 | 770 | |
| 1065 | 771 | /** |
| 1066 | 772 | * Update new password. |
| 773 | + * | |
| 774 | + * @version 1.0.1 | |
| 1067 | 775 | */ |
| 1068 | -function learn_press_update_user_profile_change_password( $wp_error = false ) { | |
| 1069 | - $user_id = get_current_user_id(); | |
| 1070 | - | |
| 1071 | - if ( ! $user_id ) { | |
| 1072 | - return new WP_Error( 2, 'User is invalid!' ); | |
| 1073 | - } | |
| 1074 | - | |
| 1075 | - $old_pass = filter_input( INPUT_POST, 'pass0' ); | |
| 1076 | - $check_old_pass = false; | |
| 1077 | - | |
| 1078 | - if ( $old_pass ) { | |
| 776 | +function learn_press_update_user_profile_change_password() { | |
| 777 | + try { | |
| 1079 | 778 | $user = wp_get_current_user(); |
| 1080 | - require_once ABSPATH . 'wp-includes/class-phpass.php'; | |
| 1081 | - $wp_hasher = new PasswordHash( 8, true ); | |
| 779 | + if ( ! $user ) { | |
| 780 | + throw new Exception( __( 'The user is invalid', 'learnpress' ) ); | |
| 781 | + } | |
| 1082 | 782 | |
| 1083 | - if ( $wp_hasher->CheckPassword( $old_pass, $user->data->user_pass ) ) { | |
| 1084 | - $check_old_pass = true; | |
| 783 | + $old_pass = LP_Request::get_param( 'pass0', '', 'text', 'post' ); | |
| 784 | + if ( empty( $old_pass ) ) { | |
| 785 | + throw new Exception( __( 'Enter the old password!', 'learnpress' ) ); | |
| 1085 | 786 | } |
| 1086 | - } | |
| 1087 | 787 | |
| 1088 | - try { | |
| 788 | + $check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID ); | |
| 1089 | 789 | if ( ! $check_old_pass ) { |
| 1090 | - throw new Exception( __( 'Old password incorrect!', 'learnpress' ) ); | |
| 1091 | - } else { | |
| 1092 | - $new_pass = filter_input( INPUT_POST, 'pass1' ); | |
| 1093 | - $new_pass2 = filter_input( INPUT_POST, 'pass2' ); | |
| 790 | + throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) ); | |
| 791 | + } | |
| 1094 | 792 | |
| 1095 | - if ( ! $new_pass || ! $new_pass2 || ( $new_pass != $new_pass2 ) ) { | |
| 1096 | - throw new Exception( __( 'Confirmation password incorrect!', 'learnpress' ) ); | |
| 1097 | - } else { | |
| 1098 | - $update_data = array( | |
| 1099 | - 'user_pass' => $new_pass, | |
| 1100 | - 'ID' => get_current_user_id(), | |
| 1101 | - ); | |
| 1102 | - $return = wp_update_user( $update_data ); | |
| 793 | + $new_pass = LP_Request::get_param( 'pass1', '', 'text', 'post' ); | |
| 794 | + $new_pass_confirm = LP_Request::get_param( 'pass2', '', 'text', 'post' ); | |
| 795 | + if ( empty( $new_pass ) || empty( $new_pass_confirm ) | |
| 796 | + || $new_pass != $new_pass_confirm ) { | |
| 797 | + throw new Exception( __( 'Incorrect confirmation password!', 'learnpress' ) ); | |
| 798 | + } | |
| 1103 | 799 | |
| 1104 | - if ( is_wp_error( $return ) ) { | |
| 1105 | - return $wp_error ? $return : false; | |
| 1106 | - } | |
| 800 | + // Update user password | |
| 801 | + // wp_set_password( $new_pass, $user->ID ); | |
| 802 | + $update_data = array( | |
| 803 | + 'user_pass' => $new_pass, | |
| 804 | + 'ID' => $user->ID, | |
| 805 | + ); | |
| 1107 | 806 | |
| 1108 | - return $return; | |
| 1109 | - } | |
| 807 | + $rs = wp_update_user( $update_data ); | |
| 808 | + if ( is_wp_error( $rs ) ) { | |
| 809 | + throw new Exception( $rs->get_error_message() ); | |
| 1110 | 810 | } |
| 1111 | 811 | } catch ( Exception $ex ) { |
| 1112 | - return $wp_error ? new WP_Error( 'UPDATE_PROFILE_ERROR', $ex->getMessage() ) : false; | |
| 812 | + return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() ); | |
| 1113 | 813 | } |
| 814 | + | |
| 815 | + return true; | |
| 1114 | 816 | } |
| 1115 | 817 | |
| 1116 | 818 | function learn_press_get_avatar_thumb_size() { |
| 1117 | 819 | $option = LP_Settings::get_option( |
| @@ -1128,8 +830,11 @@ | ||
| 1128 | 830 | 'height' => 250, |
| 1129 | 831 | ); |
| 1130 | 832 | } |
| 1131 | 833 | |
| 834 | + // For option get_avatar_url | |
| 835 | + $option['size'] = $option['width']; | |
| 836 | + | |
| 1132 | 837 | return $option; |
| 1133 | 838 | } |
| 1134 | 839 | |
| 1135 | 840 | function learn_press_get_course_thumbnail_dimensions() { |
| @@ -1150,34 +855,14 @@ | ||
| 1150 | 855 | |
| 1151 | 856 | return $option; |
| 1152 | 857 | } |
| 1153 | 858 | |
| 1154 | -/** | |
| 1155 | - * Set a fake cookie to | |
| 1156 | - */ | |
| 1157 | -function learn_press_set_user_cookie_for_guest() { | |
| 1158 | - if ( ! is_admin() && ! headers_sent() ) { | |
| 1159 | - $guest_key = '_wordpress_lp_guest'; | |
| 1160 | - | |
| 1161 | - if ( is_user_logged_in() ) { | |
| 1162 | - if ( ! empty( $_COOKIE[ $guest_key ] ) ) { | |
| 1163 | - learn_press_remove_cookie( $guest_key ); | |
| 1164 | - } | |
| 1165 | - } else { | |
| 1166 | - if ( empty( $_COOKIE[ $guest_key ] ) ) { | |
| 1167 | - learn_press_setcookie( $guest_key, md5( time() ), time() + 3600 ); | |
| 1168 | - } | |
| 1169 | - } | |
| 1170 | - } | |
| 1171 | -} | |
| 1172 | - | |
| 1173 | -add_action( 'wp', 'learn_press_set_user_cookie_for_guest' ); | |
| 1174 | - | |
| 859 | +/* | |
| 1175 | 860 | function learn_press_get_user_avatar( $user_id = 0, $size = '' ) { |
| 1176 | 861 | $user = learn_press_get_user( $user_id ); |
| 1177 | 862 | |
| 1178 | 863 | return $user->get_profile_picture( '', $size ); |
| 1179 | -} | |
| 864 | +}*/ | |
| 1180 | 865 | |
| 1181 | 866 | /** |
| 1182 | 867 | * Get profile instance for an user to view. |
| 1183 | 868 | * |
| @@ -1188,16 +873,9 @@ | ||
| 1188 | 873 | function learn_press_get_profile( $for_user = 0 ) { |
| 1189 | 874 | return LP_Profile::instance( $for_user ); |
| 1190 | 875 | } |
| 1191 | 876 | |
| 1192 | -/** | |
| 1193 | - * Remove items from learnpress_user_items. | |
| 1194 | - * | |
| 1195 | - * @param int $user_id | |
| 1196 | - * @param int $item_id | |
| 1197 | - * @param int $course_id | |
| 1198 | - * @param bool $include_course - Optional. If TRUE then remove course and it's items | |
| 1199 | - */ | |
| 877 | +/* | |
| 1200 | 878 | function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) { |
| 1201 | 879 | global $wpdb; |
| 1202 | 880 | |
| 1203 | 881 | settype( $item_id, 'array' ); |
| @@ -1220,44 +898,30 @@ | ||
| 1220 | 898 | } |
| 1221 | 899 | |
| 1222 | 900 | $query = $wpdb->prepare( |
| 1223 | 901 | " |
| 1224 | - DELETE | |
| 1225 | - FROM {$wpdb->learnpress_user_items} | |
| 1226 | - WHERE user_id = %d | |
| 1227 | - AND ( item_id IN(" . join( ',', $format ) . ") | |
| 1228 | - $where ) | |
| 1229 | - ", | |
| 902 | + DELETE | |
| 903 | + FROM {$wpdb->learnpress_user_items} | |
| 904 | + WHERE user_id = %d | |
| 905 | + AND ( item_id IN(" . join( ',', $format ) . ") | |
| 906 | + $where ) | |
| 907 | + ", | |
| 1230 | 908 | $args |
| 1231 | 909 | ); |
| 1232 | -} | |
| 910 | +}*/ | |
| 1233 | 911 | |
| 1234 | 912 | /** |
| 1235 | 913 | * Get user profile link |
| 1236 | 914 | * |
| 1237 | - * @param int $user_id | |
| 915 | + * @param int $user_id | |
| 1238 | 916 | * @param null $tab |
| 1239 | 917 | * |
| 1240 | 918 | * @return mixed|string |
| 1241 | 919 | */ |
| 1242 | -function learn_press_user_profile_link( $user_id = 0, $tab = null ) { | |
| 920 | +function learn_press_user_profile_link( $user_id = 0, $tab = '' ) { | |
| 1243 | 921 | if ( ! $user_id ) { |
| 1244 | 922 | $user_id = get_current_user_id(); |
| 1245 | 923 | } |
| 1246 | - $user = false; | |
| 1247 | - $deleted = in_array( $user_id, LP_User_Factory::$_deleted_users ); | |
| 1248 | - if ( ! $deleted ) { | |
| 1249 | - if ( is_numeric( $user_id ) ) { | |
| 1250 | - $user = get_user_by( 'id', $user_id ); | |
| 1251 | - } else { | |
| 1252 | - $user = get_user_by( 'login', urldecode( $user_id ) ); | |
| 1253 | - } | |
| 1254 | - } else { | |
| 1255 | - return ''; | |
| 1256 | - } | |
| 1257 | - if ( ! $deleted && ! $user ) { | |
| 1258 | - LP_User_Factory::$_deleted_users[] = $user_id; | |
| 1259 | - } | |
| 1260 | 924 | |
| 1261 | 925 | $user = learn_press_get_user( $user_id ); |
| 1262 | 926 | |
| 1263 | 927 | if ( ! $user ) { |
| @@ -1264,38 +928,33 @@ | ||
| 1264 | 928 | return ''; |
| 1265 | 929 | } |
| 1266 | 930 | |
| 1267 | 931 | global $wp_query; |
| 1268 | - $args = array( | |
| 1269 | - 'user' => $user->get_username(), | |
| 932 | + $wp_user = get_userdata( $user_id ); | |
| 933 | + if ( ! $wp_user instanceof WP_User ) { | |
| 934 | + return ''; | |
| 935 | + } | |
| 936 | + | |
| 937 | + $user_model = new UserModel( $wp_user->data ); | |
| 938 | + $args = array( | |
| 939 | + 'user' => $user_model->get_slug_link(), | |
| 1270 | 940 | ); |
| 1271 | 941 | |
| 1272 | - if ( isset( $args['user'] ) ) { | |
| 1273 | - if ( '' === $tab ) { | |
| 1274 | - $tab = learn_press_get_current_profile_tab(); | |
| 1275 | - } | |
| 1276 | - if ( $tab ) { | |
| 1277 | - $args['tab'] = $tab; | |
| 1278 | - } | |
| 942 | + if ( $tab ) { | |
| 943 | + $args['tab'] = $tab; | |
| 944 | + } | |
| 1279 | 945 | |
| 1280 | - /** | |
| 1281 | - * If no tab is selected in profile and is current user | |
| 1282 | - * then no need the username in profile link. | |
| 1283 | - */ | |
| 1284 | - if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) { | |
| 1285 | - unset( $args['user'] ); | |
| 1286 | - } | |
| 946 | + /** | |
| 947 | + * If no tab is selected in profile and is current user | |
| 948 | + * then no need the username in profile link. | |
| 949 | + */ | |
| 950 | + if ( ( $user_id == get_current_user_id() ) && ! isset( $args['tab'] ) ) { | |
| 951 | + //unset( $args['user'] ); | |
| 1287 | 952 | } |
| 1288 | 953 | |
| 1289 | - /*$args = array_map( | |
| 1290 | - function ( $string ) { | |
| 1291 | - return preg_replace( '/\s/', '+', $string ); | |
| 1292 | - }, | |
| 1293 | - $args | |
| 1294 | - );*/ | |
| 1295 | 954 | $profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) ); |
| 1296 | 955 | if ( $profile_link ) { |
| 1297 | - if ( get_option( 'permalink_structure' ) /*&& learn_press_get_page_id( 'profile' )*/ ) { | |
| 956 | + if ( get_option( 'permalink_structure' ) ) { | |
| 1298 | 957 | $url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) ); |
| 1299 | 958 | } else { |
| 1300 | 959 | $url = esc_url_raw( add_query_arg( $args, $profile_link ) ); |
| 1301 | 960 | } |
| @@ -1305,66 +964,9 @@ | ||
| 1305 | 964 | |
| 1306 | 965 | return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab ); |
| 1307 | 966 | } |
| 1308 | 967 | |
| 1309 | -/**********************************************/ | |
| 1310 | -/* Functions are used for hooks */ | |
| 1311 | -/**********************************************/ | |
| 1312 | - | |
| 1313 | -function learn_press_hk_before_start_quiz( $true, $quiz_id, $course_id, $user_id ) { | |
| 1314 | - if ( 'yes' !== get_post_meta( $quiz_id, '_lp_archive_history', true ) ) { | |
| 1315 | - learn_press_remove_user_items( $user_id, $quiz_id, $course_id ); | |
| 1316 | - } | |
| 1317 | - | |
| 1318 | - return $true; | |
| 1319 | -} | |
| 1320 | - | |
| 1321 | -add_filter( 'learn-press/before-start-quiz', 'learn_press_hk_before_start_quiz', 10, 4 ); | |
| 1322 | - | |
| 1323 | -/*function learn_press_default_user_item_status( $item_id ) { | |
| 1324 | - $status = ''; | |
| 1325 | - switch ( learn_press_get_post_type( $item_id ) ) { | |
| 1326 | - case LP_LESSON_CPT: | |
| 1327 | - $status = 'started'; | |
| 1328 | - break; | |
| 1329 | - case LP_QUIZ_CPT: | |
| 1330 | - $status = 'viewed'; | |
| 1331 | - break; | |
| 1332 | - case LP_COURSE_CPT: | |
| 1333 | - $status = 'enrolled'; | |
| 1334 | - } | |
| 1335 | - | |
| 1336 | - return apply_filters( 'learn-press/default-user-item-status', $status, $item_id ); | |
| 1337 | -}*/ | |
| 1338 | - | |
| 1339 | -/** | |
| 1340 | - * Get current state of distraction mode | |
| 1341 | - * | |
| 1342 | - * @return mixed | |
| 1343 | - * @since 3.1.0 | |
| 1344 | - */ | |
| 1345 | -function learn_press_get_user_distraction() { | |
| 1346 | - if ( is_user_logged_in() ) { | |
| 1347 | - return get_user_option( 'distraction_mode', get_current_user_id() ); | |
| 1348 | - } else { | |
| 1349 | - return LearnPress::instance()->session->distraction_mode; | |
| 1350 | - } | |
| 1351 | -} | |
| 1352 | - | |
| 1353 | -function learn_press_get_user_role( $user_id ) { | |
| 1354 | - if ( $user = learn_press_get_user( $user_id ) ) { | |
| 1355 | - return $user->get_role(); | |
| 1356 | - } | |
| 1357 | - | |
| 1358 | - return false; | |
| 1359 | -} | |
| 1360 | - | |
| 1361 | -/** | |
| 1362 | - * @param array $args | |
| 1363 | - * @param bool $wp_error | |
| 1364 | - * | |
| 1365 | - * @return bool|int|LP_User_Item|mixed|WP_Error | |
| 1366 | - */ | |
| 968 | +/* | |
| 1367 | 969 | function learn_press_create_user_item( $args = array(), $wp_error = false ) { |
| 1368 | 970 | global $wpdb; |
| 1369 | 971 | |
| 1370 | 972 | $defaults = array( |
| @@ -1369,15 +971,15 @@ | ||
| 1369 | 971 | |
| 1370 | 972 | $defaults = array( |
| 1371 | 973 | 'user_id' => get_current_user_id(), |
| 1372 | 974 | 'item_id' => '', |
| 1373 | - 'start_time' => current_time( 'mysql', true ), | |
| 975 | + 'start_time' => time(), | |
| 1374 | 976 | 'end_time' => '', |
| 1375 | 977 | 'graduation' => '', |
| 1376 | 978 | 'item_type' => '', |
| 1377 | 979 | 'status' => '', |
| 1378 | 980 | 'ref_id' => 0, |
| 1379 | - 'ref_type' => 0, | |
| 981 | + 'ref_type' => '', | |
| 1380 | 982 | 'parent_id' => 0, |
| 1381 | 983 | 'create_meta' => array(), |
| 1382 | 984 | ); |
| 1383 | 985 | |
| @@ -1391,9 +993,10 @@ | ||
| 1391 | 993 | |
| 1392 | 994 | return 0; |
| 1393 | 995 | } |
| 1394 | 996 | |
| 1395 | - if ( empty( $item_data['item_type'] ) && $post_type = learn_press_get_post_type( $item_data['item_id'] ) ) { | |
| 997 | + $post_type = learn_press_get_post_type( $item_data['item_id'] ); | |
| 998 | + if ( empty( $item_data['item_type'] ) && $post_type ) { | |
| 1396 | 999 | $item_data['item_type'] = $post_type; |
| 1397 | 1000 | } |
| 1398 | 1001 | |
| 1399 | 1002 | // Get id and type of ref if they are null |
| @@ -1416,9 +1019,10 @@ | ||
| 1416 | 1019 | } |
| 1417 | 1020 | } |
| 1418 | 1021 | |
| 1419 | 1022 | // Filter |
| 1420 | - if ( ! $item_data = apply_filters( 'learn-press/create-user-item-data', $item_data ) ) { | |
| 1023 | + $item_data = apply_filters( 'learn-press/create-user-item-data', $item_data ); | |
| 1024 | + if ( ! $item_data ) { | |
| 1421 | 1025 | if ( $wp_error ) { |
| 1422 | 1026 | return new WP_Error( 'invalid_item_data', __( 'Invalid item data.', 'learnpress' ) ); |
| 1423 | 1027 | } |
| 1424 | 1028 | |
| @@ -1433,13 +1037,10 @@ | ||
| 1433 | 1037 | unset( $item_data['create_meta'] ); |
| 1434 | 1038 | } |
| 1435 | 1039 | |
| 1436 | 1040 | $user_item = new LP_User_Item( $item_data ); |
| 1437 | - | |
| 1438 | - $result = $user_item->update( true, false ); | |
| 1439 | - | |
| 1041 | + $result = $user_item->update(); | |
| 1440 | 1042 | if ( ! $result || is_wp_error( $result ) ) { |
| 1441 | - | |
| 1442 | 1043 | if ( $wp_error && is_wp_error( $result ) ) { |
| 1443 | 1044 | return $result; |
| 1444 | 1045 | } |
| 1445 | 1046 | |
| @@ -1461,46 +1062,11 @@ | ||
| 1461 | 1062 | |
| 1462 | 1063 | do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta ); |
| 1463 | 1064 | |
| 1464 | 1065 | return $user_item; |
| 1465 | -} | |
| 1066 | +}*/ | |
| 1466 | 1067 | |
| 1467 | -/** | |
| 1468 | - * @param array $args | |
| 1469 | - * @param bool $wp_error - Optional. TRUE will return WP_Error on fail. | |
| 1470 | - * | |
| 1471 | - * @return bool|array|LP_User_Item|WP_Error | |
| 1472 | - */ | |
| 1473 | -function learn_press_create_user_item_for_quiz( $args = array(), $wp_error = false ) { | |
| 1474 | - global $wpdb; | |
| 1475 | - | |
| 1476 | - $item_data = wp_parse_args( | |
| 1477 | - $args, | |
| 1478 | - array( | |
| 1479 | - 'item_type' => LP_QUIZ_CPT, | |
| 1480 | - 'status' => LP_ITEM_STARTED, | |
| 1481 | - 'graduation' => LP_COURSE_GRADUATION_IN_PROGRESS, | |
| 1482 | - 'user_id' => get_current_user_id(), | |
| 1483 | - ) | |
| 1484 | - ); | |
| 1485 | - | |
| 1486 | - $user_item = learn_press_create_user_item( $item_data, $wp_error ); | |
| 1487 | - | |
| 1488 | - if ( $user_item && ! is_wp_error( $user_item ) ) { | |
| 1489 | - $user_item = new LP_User_Item_Quiz( $user_item->get_data() ); | |
| 1490 | - $user_item->update( true ); | |
| 1491 | - } | |
| 1492 | - | |
| 1493 | - return $user_item; | |
| 1494 | -} | |
| 1495 | - | |
| 1496 | -/** | |
| 1497 | - * Get list user_item_id for Quiz in table learnpress_user_items | |
| 1498 | - * | |
| 1499 | - * @param int $quiz_id | |
| 1500 | - * @param int $course_id | |
| 1501 | - * @return array || false | |
| 1502 | - */ | |
| 1068 | +/* | |
| 1503 | 1069 | function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) { |
| 1504 | 1070 | global $wpdb; |
| 1505 | 1071 | |
| 1506 | 1072 | $query = $wpdb->prepare( "SELECT user_item_id FROM $wpdb->learnpress_user_items WHERE ref_id=%d AND item_id=%d", $course_id, $quiz_id ); |
| @@ -1510,161 +1076,11 @@ | ||
| 1510 | 1076 | return $col; |
| 1511 | 1077 | } else { |
| 1512 | 1078 | return false; |
| 1513 | 1079 | } |
| 1514 | -} | |
| 1080 | +}*/ | |
| 1515 | 1081 | |
| 1516 | 1082 | /** |
| 1517 | - * Create new user item prepare for user starts a quiz | |
| 1518 | - * Update error retry course not work - Nhamdv. | |
| 1519 | - * | |
| 1520 | - * @param int $quiz_id | |
| 1521 | - * @param int $user_id | |
| 1522 | - * @param int $course_id | |
| 1523 | - * @param bool $wp_error | |
| 1524 | - * | |
| 1525 | - * @return array|bool|LP_User_Item|WP_Error | |
| 1526 | - * @since 4.0.0 | |
| 1527 | - */ | |
| 1528 | -function learn_press_user_start_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) { | |
| 1529 | - if ( ! $user_id ) { | |
| 1530 | - $user_id = get_current_user_id(); | |
| 1531 | - } | |
| 1532 | - | |
| 1533 | - global $wpdb; | |
| 1534 | - | |
| 1535 | - $query = $wpdb->prepare( | |
| 1536 | - " | |
| 1537 | - SELECT user_item_id, item_id id, item_type type | |
| 1538 | - FROM {$wpdb->learnpress_user_items} | |
| 1539 | - WHERE user_item_id = (SELECT max(user_item_id) | |
| 1540 | - FROM {$wpdb->learnpress_user_items} | |
| 1541 | - WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress')) | |
| 1542 | - ", | |
| 1543 | - $user_id, | |
| 1544 | - $course_id | |
| 1545 | - ); | |
| 1546 | - | |
| 1547 | - $parent = $wpdb->get_row( $query ); | |
| 1548 | - | |
| 1549 | - do_action( 'learn-press/before-user-start-quiz', $quiz_id, $user_id, $course_id ); | |
| 1550 | - | |
| 1551 | - $user = learn_press_get_user( $user_id ); | |
| 1552 | - $course_data = $user->get_course_data( $course_id ); | |
| 1553 | - $quiz_data = $course_data->get_item( $quiz_id ); | |
| 1554 | - | |
| 1555 | - $quiz = LP_Quiz::get_quiz( $quiz_id ); | |
| 1556 | - $duration = $quiz->get_duration(); | |
| 1557 | - $user_quiz = learn_press_create_user_item_for_quiz( | |
| 1558 | - array( | |
| 1559 | - 'user_item_id' => $quiz_data ? $quiz_data->get_user_item_id() : 0, | |
| 1560 | - 'item_id' => $quiz->get_id(), | |
| 1561 | - 'duration' => $duration ? $duration->get() : 0, | |
| 1562 | - 'user_id' => $user_id, | |
| 1563 | - 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0, | |
| 1564 | - 'ref_type' => $parent ? $parent->type : '', | |
| 1565 | - 'ref_id' => $parent ? $parent->id : '', | |
| 1566 | - ), | |
| 1567 | - $wp_error | |
| 1568 | - ); | |
| 1569 | - | |
| 1570 | - if ( $user_quiz && ! is_wp_error( $user_quiz ) ) { | |
| 1571 | - do_action( 'learn-press/user-started-quiz', $user_quiz, $quiz_id, $user_id, $course_id ); | |
| 1572 | - } | |
| 1573 | - | |
| 1574 | - // Reset first cache | |
| 1575 | - $user_quiz->get_status( 'status', true ); | |
| 1576 | - | |
| 1577 | - return $user_quiz; | |
| 1578 | -} | |
| 1579 | - | |
| 1580 | -/** | |
| 1581 | - * Function retake quiz. | |
| 1582 | - * | |
| 1583 | - * @param [type] $quiz_id | |
| 1584 | - * @param integer $user_id | |
| 1585 | - * @param integer $course_id | |
| 1586 | - * @param boolean $wp_error | |
| 1587 | - * | |
| 1588 | - * @return void | |
| 1589 | - */ | |
| 1590 | -function learn_press_user_retake_quiz( $quiz_id, $user_id = 0, $course_id = 0, $wp_error = false ) { | |
| 1591 | - if ( ! $user_id ) { | |
| 1592 | - $user_id = get_current_user_id(); | |
| 1593 | - } | |
| 1594 | - | |
| 1595 | - if ( ! $course_id ) { | |
| 1596 | - return new WP_Error( 'invalid_course_id', esc_html__( 'Invalid Course ID.', 'learnpress' ) ); | |
| 1597 | - } | |
| 1598 | - | |
| 1599 | - global $wpdb; | |
| 1600 | - | |
| 1601 | - $query = $wpdb->prepare( | |
| 1602 | - " | |
| 1603 | - SELECT user_item_id, item_id id, item_type type | |
| 1604 | - FROM {$wpdb->learnpress_user_items} | |
| 1605 | - WHERE user_item_id = (SELECT max(user_item_id) | |
| 1606 | - FROM {$wpdb->learnpress_user_items} | |
| 1607 | - WHERE user_id = %d AND item_id = %d AND status IN ('enrolled', 'in-progress')) | |
| 1608 | - ", | |
| 1609 | - $user_id, | |
| 1610 | - $course_id | |
| 1611 | - ); | |
| 1612 | - | |
| 1613 | - $parent = $wpdb->get_row( $query ); | |
| 1614 | - | |
| 1615 | - if ( ! $parent ) { | |
| 1616 | - return new WP_Error( 'invalid_user_item', esc_html__( 'Invalid Quiz', 'learnpress' ) ); | |
| 1617 | - } | |
| 1618 | - | |
| 1619 | - $data = learn_press_get_user_item( | |
| 1620 | - array( | |
| 1621 | - 'item_id' => $quiz_id, | |
| 1622 | - 'user_id' => $user_id, | |
| 1623 | - 'parent_id' => $parent ? absint( $parent->user_item_id ) : 0, | |
| 1624 | - 'ref_type' => $parent ? $parent->type : LP_COURSE_CPT, | |
| 1625 | - 'ref_id' => $parent ? $parent->id : '', | |
| 1626 | - ) | |
| 1627 | - ); | |
| 1628 | - | |
| 1629 | - $user_item = new LP_User_Item_Quiz( $data ); | |
| 1630 | - | |
| 1631 | - $ratake_count = get_post_meta( $quiz_id, '_lp_retake_count', true ); | |
| 1632 | - | |
| 1633 | - if ( $ratake_count > 0 ) { | |
| 1634 | - $user_item->update_retake_count(); | |
| 1635 | - } | |
| 1636 | - | |
| 1637 | - // Create new result in table learnpress_user_item_results. | |
| 1638 | - LP_User_Items_Result_DB::instance()->insert( $data->user_item_id ); | |
| 1639 | - | |
| 1640 | - // Remove user_item_meta. | |
| 1641 | - learn_press_delete_user_item_meta( $data->user_item_id, '_lp_question_checked' ); | |
| 1642 | - | |
| 1643 | - $user_item->set_status( LP_ITEM_STARTED ) | |
| 1644 | - ->set_start_time( current_time( 'mysql', 1 ) ) // Error Retake when change timezone - Nhamdv | |
| 1645 | - ->set_end_time( '' ) | |
| 1646 | - ->set_graduation( LP_COURSE_GRADUATION_IN_PROGRESS ) | |
| 1647 | - ->update(); | |
| 1648 | - | |
| 1649 | - // Reset first cache | |
| 1650 | - $user_item->get_status( 'status', true ); | |
| 1651 | - | |
| 1652 | - // Error Retake when change timezone - Nhamdv | |
| 1653 | - // learn_press_update_user_item_field( | |
| 1654 | - // array( | |
| 1655 | - // 'start_time' => current_time( 'mysql', true ), | |
| 1656 | - // ), | |
| 1657 | - // array( | |
| 1658 | - // 'user_item_id' => $data->user_item_id, | |
| 1659 | - // ) | |
| 1660 | - // ); | |
| 1661 | - | |
| 1662 | - return $user_item; | |
| 1663 | -} | |
| 1664 | - | |
| 1665 | - | |
| 1666 | -/** | |
| 1667 | 1083 | * Prepares list of questions for rest api. |
| 1668 | 1084 | * |
| 1669 | 1085 | * @param int[] $question_ids |
| 1670 | 1086 | * @param array $args |
| @@ -1671,9 +1087,9 @@ | ||
| 1671 | 1087 | * |
| 1672 | 1088 | * @return array |
| 1673 | 1089 | * @since 3.3.0 |
| 1674 | 1090 | */ |
| 1675 | -function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ) : array { | |
| 1091 | +function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ): array { | |
| 1676 | 1092 | if ( is_numeric( $args ) ) { |
| 1677 | 1093 | |
| 1678 | 1094 | } else { |
| 1679 | 1095 | $args = wp_parse_args( |
| @@ -1712,12 +1128,12 @@ | ||
| 1712 | 1128 | |
| 1713 | 1129 | if ( $instantCheck || $status == 'completed' ) { |
| 1714 | 1130 | $theExplanation = $question->get_explanation(); |
| 1715 | 1131 | $checked = in_array( $id, $checkedQuestions ); |
| 1716 | - $hasExplanation = ! ! $theExplanation; | |
| 1132 | + $hasExplanation = (bool) $theExplanation; | |
| 1717 | 1133 | } |
| 1718 | 1134 | |
| 1719 | - $mark = $question->get_mark() ? $question->get_mark() : 1; | |
| 1135 | + $mark = $question->get_mark() ? $question->get_mark() : 1; | |
| 1720 | 1136 | |
| 1721 | 1137 | $questionData = array( |
| 1722 | 1138 | 'object' => $question, |
| 1723 | 1139 | 'id' => absint( $id ), |
| @@ -1795,12 +1211,33 @@ | ||
| 1795 | 1211 | if ( ! current_user_can( 'edit_user', $user_id ) ) { |
| 1796 | 1212 | return; |
| 1797 | 1213 | } |
| 1798 | 1214 | |
| 1215 | + if ( array_key_exists( 'lp_user_slug', $_POST ) ) { | |
| 1216 | + $userModel = UserModel::find( $user_id, true ); | |
| 1217 | + if ( ! $userModel ) { | |
| 1218 | + return; | |
| 1219 | + } | |
| 1220 | + | |
| 1221 | + $slug_result = $userModel->update_user_nicename( (string) $_POST['lp_user_slug'] ); | |
| 1222 | + if ( is_wp_error( $slug_result ) ) { | |
| 1223 | + wp_safe_redirect( | |
| 1224 | + add_query_arg( | |
| 1225 | + 'lp-message', | |
| 1226 | + urlencode( $slug_result->get_error_message() ), | |
| 1227 | + wp_get_referer() | |
| 1228 | + ) | |
| 1229 | + ); | |
| 1230 | + die(); | |
| 1231 | + } | |
| 1232 | + } | |
| 1233 | + | |
| 1799 | 1234 | if ( isset( $_POST['_lp_extra_info'] ) ) { |
| 1800 | - update_user_meta( $user_id, '_lp_extra_info', LP_Helper::sanitize_params_submitted( $_POST['_lp_extra_info'] ) ); | |
| 1235 | + $extra_info = LP_Request::get_param( '_lp_extra_info', array(), '', 'post' ); | |
| 1236 | + update_user_meta( $user_id, '_lp_extra_info', $extra_info ); | |
| 1801 | 1237 | } |
| 1802 | 1238 | } |
| 1239 | + | |
| 1803 | 1240 | add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' ); |
| 1804 | 1241 | add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' ); |
| 1805 | 1242 | |
| 1806 | 1243 | /** |
| @@ -1808,9 +1245,9 @@ | ||
| 1808 | 1245 | * |
| 1809 | 1246 | * @param int $user_id |
| 1810 | 1247 | * |
| 1811 | 1248 | * @return array |
| 1812 | - * @since 4.0.0 | |
| 1249 | + * @since 4.0.1 | |
| 1813 | 1250 | */ |
| 1814 | 1251 | function learn_press_get_user_extra_profile_info( $user_id = 0 ) { |
| 1815 | 1252 | if ( ! $user_id ) { |
| 1816 | 1253 | $user_id = get_current_user_id(); |
| @@ -1816,30 +1253,61 @@ | ||
| 1816 | 1253 | $user_id = get_current_user_id(); |
| 1817 | 1254 | } |
| 1818 | 1255 | |
| 1819 | 1256 | $extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id ); |
| 1820 | - $extra_fields = learn_press_get_user_extra_profile_fields(); | |
| 1257 | + $social_fields = learn_press_social_profiles(); | |
| 1821 | 1258 | |
| 1822 | - $extra_profile_info = wp_parse_args( | |
| 1823 | - $extra_profile_info, | |
| 1824 | - array_fill_keys( array_keys( $extra_fields ), '' ) | |
| 1825 | - ); | |
| 1259 | + $user_socials = array(); | |
| 1260 | + foreach ( $social_fields as $key => $label ) { | |
| 1261 | + $key = sanitize_key( $key ); | |
| 1262 | + $user_socials[ $key ] = ''; | |
| 1263 | + if ( is_array( $extra_profile_info ) && isset( $extra_profile_info[ $key ] ) ) { | |
| 1264 | + $user_socials[ $key ] = esc_url_raw( $extra_profile_info[ $key ] ); | |
| 1265 | + } | |
| 1266 | + } | |
| 1826 | 1267 | |
| 1827 | - return apply_filters( 'learn-press/user-extra-profile-info', $extra_profile_info, $user_id ); | |
| 1268 | + return apply_filters( 'learn-press/user-extra-profile-info', $user_socials, $user_id ); | |
| 1828 | 1269 | } |
| 1829 | 1270 | |
| 1271 | +/** | |
| 1272 | + * @return array | |
| 1273 | + * @since 3.x.x | |
| 1274 | + * @version 4.0.2 | |
| 1275 | + */ | |
| 1830 | 1276 | function learn_press_social_profiles() { |
| 1831 | 1277 | return apply_filters( |
| 1832 | 1278 | 'learn-press/social-profiles', |
| 1833 | 1279 | array( |
| 1834 | - 'facebook', | |
| 1835 | - 'twitter', | |
| 1836 | - 'youtube', | |
| 1837 | - 'linkedin', | |
| 1280 | + 'facebook' => learn_press_social_profile_name( 'facebook' ), | |
| 1281 | + 'twitter' => learn_press_social_profile_name( 'twitter' ), | |
| 1282 | + 'youtube' => learn_press_social_profile_name( 'youtube' ), | |
| 1283 | + 'linkedin' => learn_press_social_profile_name( 'linkedin' ), | |
| 1838 | 1284 | ) |
| 1839 | 1285 | ); |
| 1840 | 1286 | } |
| 1841 | 1287 | |
| 1288 | +function learn_press_social_profile_name( $key ) { | |
| 1289 | + $name = ''; | |
| 1290 | + switch ( $key ) { | |
| 1291 | + case 'facebook': | |
| 1292 | + $name = esc_html__( 'Facebook Profile', 'learnpress' ); | |
| 1293 | + break; | |
| 1294 | + case 'twitter': | |
| 1295 | + $name = esc_html__( 'X Profile', 'learnpress' ); | |
| 1296 | + break; | |
| 1297 | + case 'youtube': | |
| 1298 | + $name = esc_html__( 'Youtube Channel', 'learnpress' ); | |
| 1299 | + break; | |
| 1300 | + case 'linkedin': | |
| 1301 | + $name = esc_html__( 'Linkedin Profile', 'learnpress' ); | |
| 1302 | + break; | |
| 1303 | + default: | |
| 1304 | + $name = ucfirst( $key ); | |
| 1305 | + } | |
| 1306 | + | |
| 1307 | + return apply_filters( 'learn-press/social-profile-name', $name, $key ); | |
| 1308 | +} | |
| 1309 | + | |
| 1842 | 1310 | function lp_add_default_fields( $fields ) { |
| 1843 | 1311 | $first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' ); |
| 1844 | 1312 | |
| 1845 | 1313 | if ( $first_name === 'yes' ) { |
| @@ -1846,10 +1314,10 @@ | ||
| 1846 | 1314 | ?> |
| 1847 | 1315 | <li class="form-field"> |
| 1848 | 1316 | <label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label> |
| 1849 | 1317 | <input id="reg_first_name" name="reg_first_name" type="text" |
| 1850 | - placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>" | |
| 1851 | - value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>"> | |
| 1318 | + placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>" | |
| 1319 | + value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>"> | |
| 1852 | 1320 | </li> |
| 1853 | 1321 | <?php |
| 1854 | 1322 | } |
| 1855 | 1323 | |
| @@ -1859,10 +1327,10 @@ | ||
| 1859 | 1327 | ?> |
| 1860 | 1328 | <li class="form-field"> |
| 1861 | 1329 | <label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label> |
| 1862 | 1330 | <input id="reg_last_name" name="reg_last_name" type="text" |
| 1863 | - placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>" | |
| 1864 | - value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>"> | |
| 1331 | + placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>" | |
| 1332 | + value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>"> | |
| 1865 | 1333 | </li> |
| 1866 | 1334 | <?php |
| 1867 | 1335 | } |
| 1868 | 1336 | |
| @@ -1872,10 +1340,10 @@ | ||
| 1872 | 1340 | ?> |
| 1873 | 1341 | <li class="form-field"> |
| 1874 | 1342 | <label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label> |
| 1875 | 1343 | <input id="reg_display_name" name="reg_display_name" type="text" |
| 1876 | - placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>" | |
| 1877 | - value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>"> | |
| 1344 | + placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>" | |
| 1345 | + value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>"> | |
| 1878 | 1346 | </li> |
| 1879 | 1347 | <?php |
| 1880 | 1348 | } |
| 1881 | 1349 | } |
| @@ -1883,9 +1351,9 @@ | ||
| 1883 | 1351 | add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' ); |
| 1884 | 1352 | |
| 1885 | 1353 | function lp_custom_register_fields_display() { |
| 1886 | 1354 | ?> |
| 1887 | - <?php $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); ?> | |
| 1355 | + <?php $custom_fields = LP_Profile::get_register_fields_custom(); ?> | |
| 1888 | 1356 | |
| 1889 | 1357 | <?php if ( $custom_fields ) : ?> |
| 1890 | 1358 | <?php foreach ( $custom_fields as $custom_field ) : ?> |
| 1891 | 1359 | <?php |
| @@ -1896,11 +1364,12 @@ | ||
| 1896 | 1364 | <style> |
| 1897 | 1365 | .required label { |
| 1898 | 1366 | font-weight: bold; |
| 1899 | 1367 | } |
| 1368 | + | |
| 1900 | 1369 | .required label:after { |
| 1901 | 1370 | content: ' *'; |
| 1902 | - display:inline; | |
| 1371 | + display: inline; | |
| 1903 | 1372 | } |
| 1904 | 1373 | </style> |
| 1905 | 1374 | <?php |
| 1906 | 1375 | } |
| @@ -1919,10 +1388,10 @@ | ||
| 1919 | 1388 | case 'tel': |
| 1920 | 1389 | ?> |
| 1921 | 1390 | <label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label> |
| 1922 | 1391 | <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]" |
| 1923 | - type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text" | |
| 1924 | - value="" /> | |
| 1392 | + type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text" | |
| 1393 | + value=""/> | |
| 1925 | 1394 | <?php |
| 1926 | 1395 | break; |
| 1927 | 1396 | case 'textarea': |
| 1928 | 1397 | ?> |
| @@ -1933,9 +1402,9 @@ | ||
| 1933 | 1402 | case 'checkbox': |
| 1934 | 1403 | ?> |
| 1935 | 1404 | <label> |
| 1936 | 1405 | <input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]" |
| 1937 | - type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1"> | |
| 1406 | + type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1"> | |
| 1938 | 1407 | <?php echo esc_html( $custom_field['name'] ); ?> |
| 1939 | 1408 | </label> |
| 1940 | 1409 | <?php |
| 1941 | 1410 | break; |
| @@ -1981,9 +1450,9 @@ | ||
| 1981 | 1450 | return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id ); |
| 1982 | 1451 | } |
| 1983 | 1452 | |
| 1984 | 1453 | function lp_get_user_custom_fields() { |
| 1985 | - $custom_fields = LP_Settings::instance()->get( 'register_profile_fields' ); | |
| 1454 | + $custom_fields = LP_Settings::get_option( 'register_profile_fields', array() ); | |
| 1986 | 1455 | |
| 1987 | 1456 | $output = array(); |
| 1988 | 1457 | |
| 1989 | 1458 | if ( $custom_fields ) { |
| @@ -2001,50 +1470,37 @@ | ||
| 2001 | 1470 | * @param $key |
| 2002 | 1471 | * |
| 2003 | 1472 | * @return bool |
| 2004 | 1473 | * @since 4.0.0 |
| 1474 | + * @deprecated 4.3.2 | |
| 2005 | 1475 | */ |
| 2006 | 1476 | function learn_press_is_social_profile( $key ) { |
| 1477 | + _deprecated_function( __FUNCTION__, '4.3.2' ); | |
| 1478 | + | |
| 1479 | + return true; | |
| 1480 | + | |
| 2007 | 1481 | $is_socials = learn_press_social_profiles(); |
| 2008 | 1482 | |
| 2009 | 1483 | return in_array( $key, $is_socials ); |
| 2010 | 1484 | } |
| 2011 | 1485 | |
| 2012 | -function learn_press_social_profile_name( $key ) { | |
| 2013 | - $name = ''; | |
| 2014 | - switch ( $key ) { | |
| 2015 | - case 'facebook': | |
| 2016 | - $name = esc_html__( 'Facebook Profile', 'learnpress' ); | |
| 2017 | - break; | |
| 2018 | - case 'twitter': | |
| 2019 | - $name = esc_html__( 'Twitter Profile', 'learnpress' ); | |
| 2020 | - break; | |
| 2021 | - case 'googleplus': | |
| 2022 | - $name = esc_html__( 'Google Profile', 'learnpress' ); | |
| 2023 | - break; | |
| 2024 | - case 'youtube': | |
| 2025 | - $name = esc_html__( 'Youtube Channel', 'learnpress' ); | |
| 2026 | - break; | |
| 2027 | - case 'linkedin': | |
| 2028 | - $name = esc_html__( 'Linkedin Profile', 'learnpress' ); | |
| 2029 | - break; | |
| 2030 | - default: | |
| 2031 | - $name = ucfirst( $key ); | |
| 2032 | - } | |
| 2033 | - | |
| 2034 | - return apply_filters( 'learn-press/social-profile-name', $name, $key ); | |
| 2035 | -} | |
| 2036 | - | |
| 2037 | 1486 | /** |
| 2038 | 1487 | * Get extra profile fields will be registered in backend profile. |
| 2039 | 1488 | * |
| 2040 | 1489 | * @return array |
| 2041 | 1490 | * @since 4.0.0 |
| 1491 | + * @deprecated 4.3.2 | |
| 2042 | 1492 | */ |
| 2043 | 1493 | function learn_press_get_user_extra_profile_fields() { |
| 1494 | + _deprecated_function( __FUNCTION__, '4.3.2' ); | |
| 1495 | + return array(); | |
| 1496 | + | |
| 2044 | 1497 | $socials = learn_press_social_profiles(); |
| 2045 | - $fields = array(); | |
| 2046 | 1498 | |
| 1499 | + return $socials; | |
| 1500 | + | |
| 1501 | + $fields = array(); | |
| 1502 | + | |
| 2047 | 1503 | foreach ( $socials as $social ) { |
| 2048 | 1504 | $fields[ $social ] = learn_press_social_profile_name( $social ); |
| 2049 | 1505 | } |
| 2050 | 1506 | |
| @@ -2065,10 +1521,13 @@ | ||
| 2065 | 1521 | |
| 2066 | 1522 | learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) ); |
| 2067 | 1523 | learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) ); |
| 2068 | 1524 | } |
| 1525 | + | |
| 2069 | 1526 | add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 ); |
| 1527 | +add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 ); | |
| 2070 | 1528 | |
| 1529 | +/* | |
| 2071 | 1530 | function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) { |
| 2072 | 1531 | if ( empty( $user_id ) ) { |
| 2073 | 1532 | return false; |
| 2074 | 1533 | } |
| @@ -2109,6 +1568,5 @@ | ||
| 2109 | 1568 | 'all' => count( $posts ), |
| 2110 | 1569 | 'publish' => count( $public ), |
| 2111 | 1570 | 'pending' => count( $pending ), |
| 2112 | 1571 | ); |
| 2113 | - | |
| 2114 | -} | |
| 1572 | +}*/ | |