| 1 |
<?php |
| 2 |
|
| 3 |
use LearnPress\Models\UserItems\UserItemModel; |
| 4 |
use LearnPress\Models\UserModel; |
| 5 |
use LearnPress\Services\UserService; |
| 6 |
|
| 7 |
/** |
| 8 |
* Common functions to process actions about user |
| 9 |
* |
| 10 |
* @author ThimPress |
| 11 |
* @package LearnPress/Functions/User |
| 12 |
* @version 1.0 |
| 13 |
*/ |
| 14 |
function learn_press_get_user_profile_tabs() { |
| 15 |
return LP_Profile::instance()->get_tabs(); |
| 16 |
} |
| 17 |
|
| 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 |
/** |
| 37 |
* Delete user data by user ID |
| 38 |
* |
| 39 |
* @param int $user_id |
| 40 |
* @param int $course_id |
| 41 |
*/ |
| 42 |
function learn_press_delete_user_data( $user_id, $course_id = 0 ) { |
| 43 |
global $wpdb; |
| 44 |
// TODO: Should be deleted user's order and order data??? |
| 45 |
|
| 46 |
$query_args = array( $user_id ); |
| 47 |
|
| 48 |
if ( $course_id ) { |
| 49 |
$query_args[] = $course_id; |
| 50 |
} |
| 51 |
|
| 52 |
$query = $wpdb->prepare( |
| 53 |
" |
| 54 |
SELECT user_item_id |
| 55 |
FROM {$wpdb->prefix}learnpress_user_items |
| 56 |
WHERE user_id = %d |
| 57 |
" . ( $course_id ? ' AND item_id = %d' : '' ) . ' |
| 58 |
', |
| 59 |
$query_args |
| 60 |
); |
| 61 |
|
| 62 |
// delete all courses user has enrolled |
| 63 |
$query = $wpdb->prepare( |
| 64 |
" |
| 65 |
DELETE FROM {$wpdb->prefix}learnpress_user_items |
| 66 |
WHERE user_id = %d |
| 67 |
" . ( $course_id ? ' AND item_id = %d' : '' ) . ' |
| 68 |
', |
| 69 |
$query_args |
| 70 |
); |
| 71 |
|
| 72 |
@$wpdb->query( $query ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Get current user ID |
| 77 |
* |
| 78 |
* @return int|string LP_User is int, LP_User_Guest is string |
| 79 |
*/ |
| 80 |
function learn_press_get_current_user_id() { |
| 81 |
$user = learn_press_get_current_user(); |
| 82 |
|
| 83 |
if ( ! $user ) { |
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
return $user->get_id(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get the user by $user_id passed. If $user_id is NULL, get current user. |
| 92 |
* If current user is not logged in, return a GUEST user |
| 93 |
* |
| 94 |
* @param bool $create_temp - Optional. Create temp user if user is not logged in. |
| 95 |
* |
| 96 |
* @return LP_User|LP_User_Guest |
| 97 |
* @editor tungnx |
| 98 |
* @modify 4.1.4 |
| 99 |
* @version 1.0.1 |
| 100 |
*/ |
| 101 |
function learn_press_get_current_user( $create_temp = true ) { |
| 102 |
$user_id = get_current_user_id(); |
| 103 |
|
| 104 |
if ( $user_id ) { |
| 105 |
return learn_press_get_user( $user_id ); |
| 106 |
} |
| 107 |
|
| 108 |
// Return LP_User_Guest |
| 109 |
return learn_press_get_user( 0 ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! function_exists( 'learn_press_get_user' ) ) { |
| 113 |
/** |
| 114 |
* Get user by ID. Return false if the user does not exist. |
| 115 |
* If user_id = 0, return a guest user. |
| 116 |
* |
| 117 |
* @param int $user_id |
| 118 |
* @param bool $current |
| 119 |
* |
| 120 |
* @return LP_User|LP_User_Guest|false |
| 121 |
* @since 3.0.0 |
| 122 |
* @version 4.0.1 |
| 123 |
*/ |
| 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 ) { |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
return new $userClass( $user_id ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Add roles and add capabilities for roles |
| 136 |
*/ |
| 137 |
function learn_press_add_user_roles() { |
| 138 |
$item_types = [ |
| 139 |
LP_COURSE_CPT, |
| 140 |
LP_LESSON_CPT, |
| 141 |
LP_ORDER_CPT, |
| 142 |
]; |
| 143 |
|
| 144 |
foreach ( $item_types as $item_type ) { |
| 145 |
UserService::instance()->add_capabilities_for_roles( $item_type ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get queried user in profile link |
| 151 |
* |
| 152 |
* @return false|WP_User |
| 153 |
* @since 3.0.0 |
| 154 |
* @deprecated 4.2.9.1 |
| 155 |
*/ |
| 156 |
function learn_press_get_profile_user() { |
| 157 |
return LP_Profile::instance()->get_user_current(); |
| 158 |
|
| 159 |
return LP_Profile::get_queried_user(); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* Add instructor registration button to register page and admin bar |
| 165 |
*/ |
| 166 |
function learn_press_user_become_teacher_registration_form() { |
| 167 |
if ( LP_Settings::instance()->get( 'instructor_registration' ) != 'yes' ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
?> |
| 171 |
<p> |
| 172 |
<label for="become_teacher"> |
| 173 |
<input type="checkbox" name="become_teacher" id="become_teacher"> |
| 174 |
<?php esc_html_e( 'Want to become an instructor?', 'learnpress' ); ?> |
| 175 |
</label> |
| 176 |
</p> |
| 177 |
<?php |
| 178 |
} |
| 179 |
|
| 180 |
add_action( 'register_form', 'learn_press_user_become_teacher_registration_form' ); |
| 181 |
|
| 182 |
/** |
| 183 |
* Update data into table learnpress_user_items. |
| 184 |
* |
| 185 |
* @param array $fields - Fields and values to be updated. |
| 186 |
* Format: array( |
| 187 |
* field_name_1 => value 1, |
| 188 |
* field_name_2 => value 2, |
| 189 |
* .... |
| 190 |
* field_name_n => value n |
| 191 |
* ) |
| 192 |
* @param mixed $where - Optional. Fields with values for conditional update with the same format of $fields. |
| 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). |
| 195 |
* |
| 196 |
* @return mixed |
| 197 |
*/ |
| 198 |
function learn_press_update_user_item_field( array $fields = array(), $where = false, $update_cache = true, $update_extra_fields_as_meta = false ) { |
| 199 |
global $wpdb; |
| 200 |
|
| 201 |
// Table fields format. |
| 202 |
$table_fields = array( |
| 203 |
'user_item_id' => '%d', |
| 204 |
'user_id' => '%d', |
| 205 |
'item_id' => '%d', |
| 206 |
'ref_id' => '%d', |
| 207 |
'start_time' => '%s', |
| 208 |
'end_time' => '%s', |
| 209 |
'access_level' => '%d', |
| 210 |
'graduation' => '%s', |
| 211 |
'item_type' => '%s', |
| 212 |
'status' => '%s', |
| 213 |
'ref_type' => '%s', |
| 214 |
'parent_id' => '%d', |
| 215 |
); |
| 216 |
|
| 217 |
/** |
| 218 |
* Validate item status |
| 219 |
*/ |
| 220 |
if ( ! empty( $fields['item_id'] ) && ! empty( $fields['status'] ) ) { |
| 221 |
$item_type = learn_press_get_post_type( $fields['item_id'] ); |
| 222 |
|
| 223 |
if ( LP_COURSE_CPT === $item_type ) { |
| 224 |
if ( 'completed' === $fields['status'] ) { |
| 225 |
$fields['status'] = 'finished'; |
| 226 |
} |
| 227 |
} elseif ( 'finished' === $fields['status'] ) { |
| 228 |
$fields['status'] = 'completed'; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
$data = array(); |
| 233 |
$data_format = array(); |
| 234 |
foreach ( $fields as $field => $value ) { |
| 235 |
if ( ! empty( $table_fields[ $field ] ) ) { |
| 236 |
$data[ $field ] = $value; |
| 237 |
|
| 238 |
// Do not format the date-time field if it's value is NULL |
| 239 |
if ( in_array( $field, array( 'start_time', 'end_time' ) ) && empty( $value ) ) { |
| 240 |
$data[ $field ] = null; |
| 241 |
$data_format[] = ''; |
| 242 |
} else { |
| 243 |
if ( $value instanceof LP_Datetime ) { |
| 244 |
$data[ $field ] = $value->toSql(); |
| 245 |
} |
| 246 |
$data_format[] = $table_fields[ $field ]; |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
if ( ! empty( $fields['user_item_id'] ) ) { |
| 252 |
$where = wp_parse_args( |
| 253 |
$where, |
| 254 |
array( 'user_item_id' => $fields['user_item_id'] ) |
| 255 |
); |
| 256 |
} |
| 257 |
|
| 258 |
// Set where and where format |
| 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 |
} |
| 264 |
|
| 265 |
foreach ( $where as $field => $value ) { |
| 266 |
if ( isset( $table_fields[ $field ] ) ) { |
| 267 |
$where_format[] = $table_fields[ $field ]; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
if ( ! $data ) { |
| 273 |
return false; |
| 274 |
} |
| 275 |
|
| 276 |
$inserted = false; |
| 277 |
$updated = false; |
| 278 |
|
| 279 |
// Update to database. |
| 280 |
if ( $where ) { |
| 281 |
$updated = $wpdb->update( |
| 282 |
$wpdb->learnpress_user_items, |
| 283 |
$data, |
| 284 |
$where, |
| 285 |
$data_format, |
| 286 |
$where_format |
| 287 |
); |
| 288 |
} else { |
| 289 |
// Insert to database. |
| 290 |
if ( $wpdb->insert( |
| 291 |
$wpdb->learnpress_user_items, |
| 292 |
$data, |
| 293 |
$data_format |
| 294 |
) ) { |
| 295 |
$inserted = $wpdb->insert_id; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if ( $updated && ! empty( $where['user_item_id'] ) ) { |
| 300 |
$inserted = $where['user_item_id']; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* @var object|bool $updated_item |
| 305 |
*/ |
| 306 |
$updated_item = false; |
| 307 |
|
| 308 |
// Get the item we just have updated or inserted. |
| 309 |
if ( $inserted ) { |
| 310 |
$updated_item = learn_press_get_user_item( $inserted ); |
| 311 |
} elseif ( $updated ) { |
| 312 |
$updated_item = learn_press_get_user_item( $where ); |
| 313 |
} |
| 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 |
|
| 351 |
/** |
| 352 |
* If there is some fields does not contain in the main table |
| 353 |
* then consider update them as metadata. |
| 354 |
* |
| 355 |
* @comment by tungnx - 4.1.7.3 |
| 356 |
*/ |
| 357 |
/* |
| 358 |
if ( $updated_item && $update_extra_fields_as_meta ) { |
| 359 |
$extra_fields = array_diff_key( $fields, $table_fields ); |
| 360 |
if ( $extra_fields ) { |
| 361 |
foreach ( $extra_fields as $meta_key => $meta_value ) { |
| 362 |
if ( $meta_value == 'user_item_id' ) { |
| 363 |
continue; |
| 364 |
} |
| 365 |
|
| 366 |
if ( $meta_value === false ) { |
| 367 |
learn_press_delete_user_item_meta( $updated_item->user_item_id, $meta_key ); |
| 368 |
} else { |
| 369 |
|
| 370 |
if ( empty( $meta_value ) ) { |
| 371 |
$meta_value = ''; |
| 372 |
} |
| 373 |
learn_press_update_user_item_meta( $updated_item->user_item_id, $meta_key, $meta_value ); |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
}*/ |
| 378 |
|
| 379 |
// do_action( 'learn-press/updated-user-item-meta', $updated_item ); |
| 380 |
|
| 381 |
return $updated_item; |
| 382 |
} |
| 383 |
|
| 384 |
/** |
| 385 |
* Get user item row(s) from user items table by multiple WHERE conditional |
| 386 |
* |
| 387 |
* @param array|int $where |
| 388 |
* @param bool $single |
| 389 |
* |
| 390 |
* @return array |
| 391 |
*/ |
| 392 |
function learn_press_get_user_item( $where, $single = true ) { |
| 393 |
global $wpdb; |
| 394 |
|
| 395 |
// Table fields |
| 396 |
$table_fields = array( |
| 397 |
'user_item_id' => '%d', |
| 398 |
'user_id' => '%d', |
| 399 |
'item_id' => '%d', |
| 400 |
'ref_id' => '%d', |
| 401 |
'start_time' => '%s', |
| 402 |
'end_time' => '%s', |
| 403 |
'item_type' => '%s', |
| 404 |
'status' => '%s', |
| 405 |
'ref_type' => '%s', |
| 406 |
'parent_id' => '%d', |
| 407 |
); |
| 408 |
|
| 409 |
// If $where is a number consider we are searching the record with unique user_item_id |
| 410 |
if ( is_numeric( $where ) ) { |
| 411 |
$where = array( 'user_item_id' => $where ); |
| 412 |
} |
| 413 |
|
| 414 |
$where_str = array(); |
| 415 |
foreach ( $where as $field => $value ) { |
| 416 |
if ( ! empty( $table_fields[ $field ] ) ) { |
| 417 |
$where_str[] = "{$field} = " . $table_fields[ $field ]; |
| 418 |
} |
| 419 |
} |
| 420 |
$item = false; |
| 421 |
|
| 422 |
if ( $where_str ) { |
| 423 |
$query = $wpdb->prepare( |
| 424 |
" |
| 425 |
SELECT * |
| 426 |
FROM {$wpdb->prefix}learnpress_user_items |
| 427 |
WHERE " . join( ' AND ', $where_str ) . ' |
| 428 |
ORDER BY user_item_id DESC |
| 429 |
', |
| 430 |
$where |
| 431 |
); |
| 432 |
if ( $single || ! empty( $where['user_item_id'] ) ) { |
| 433 |
$item = $wpdb->get_row( $query ); |
| 434 |
} else { |
| 435 |
$item = $wpdb->get_results( $query ); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
return $item; |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Get user item meta from user_itemmeta table |
| 444 |
* |
| 445 |
* @param int $user_item_id . |
| 446 |
* @param string $meta_key . |
| 447 |
* @param bool $single . |
| 448 |
* |
| 449 |
* @return mixed |
| 450 |
*/ |
| 451 |
function learn_press_get_user_item_meta( $user_item_id = 0, $meta_key = '', $single = true ) { |
| 452 |
$meta = false; |
| 453 |
if ( metadata_exists( 'learnpress_user_item', $user_item_id, $meta_key ) ) { |
| 454 |
$meta = get_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $single ); |
| 455 |
} |
| 456 |
|
| 457 |
return $meta; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Add user item meta into table user_itemmeta |
| 462 |
* |
| 463 |
* @param int $user_item_id |
| 464 |
* @param string $meta_key |
| 465 |
* @param mixed $meta_value |
| 466 |
* @param string $prev_value |
| 467 |
* |
| 468 |
* @return false|int |
| 469 |
*/ |
| 470 |
function learn_press_add_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 471 |
return add_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value ); |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Update user item meta to table user_itemmeta |
| 476 |
* |
| 477 |
* @param int $user_item_id |
| 478 |
* @param string $meta_key |
| 479 |
* @param mixed $meta_value |
| 480 |
* @param string $prev_value |
| 481 |
* |
| 482 |
* @return bool|int |
| 483 |
*/ |
| 484 |
function learn_press_update_user_item_meta( $user_item_id, $meta_key, $meta_value, $prev_value = '' ) { |
| 485 |
return update_metadata( 'learnpress_user_item', $user_item_id, $meta_key, $meta_value, $prev_value ); |
| 486 |
} |
| 487 |
|
| 488 |
|
| 489 |
/** |
| 490 |
* Update user item meta to table user_itemmeta |
| 491 |
* |
| 492 |
* @param int $object_id |
| 493 |
* @param string $meta_key |
| 494 |
* @param mixed $meta_value |
| 495 |
* @param bool $delete_all |
| 496 |
* |
| 497 |
* @return bool|int |
| 498 |
*/ |
| 499 |
function learn_press_delete_user_item_meta( $object_id, $meta_key, $meta_value = '', $delete_all = false ) { |
| 500 |
return delete_metadata( 'learnpress_user_item', $object_id, $meta_key, $meta_value, $delete_all ); |
| 501 |
} |
| 502 |
|
| 503 |
if ( ! function_exists( 'learn_press_pre_get_avatar_callback' ) ) { |
| 504 |
/** |
| 505 |
* Filter the avatar |
| 506 |
* |
| 507 |
* @param string $avatar |
| 508 |
* @param string $id_or_email |
| 509 |
* @param array $args |
| 510 |
* |
| 511 |
* @return string |
| 512 |
* @since 1.0.0 |
| 513 |
* @version 1.0.2 |
| 514 |
*/ |
| 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 |
} |
| 520 |
|
| 521 |
$user_id = 0; |
| 522 |
|
| 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 |
} |
| 541 |
|
| 542 |
if ( ! $user_id ) { |
| 543 |
return $avatar; |
| 544 |
} |
| 545 |
|
| 546 |
$user = UserModel::find( $user_id, true ); |
| 547 |
if ( ! $user ) { |
| 548 |
return $avatar; |
| 549 |
} |
| 550 |
|
| 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'] ); |
| 555 |
} else { |
| 556 |
$class[] = $args['class']; |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 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; |
| 575 |
} |
| 576 |
} |
| 577 |
} |
| 578 |
add_filter( 'pre_get_avatar', 'learn_press_pre_get_avatar_callback', 999, 3 ); |
| 579 |
|
| 580 |
|
| 581 |
function learn_press_user_profile_picture_upload_dir( $width_user = true ) { |
| 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; |
| 588 |
|
| 589 |
if ( ! empty( $upload_dir['subdir'] ) ) { |
| 590 |
$u_subdir = str_replace( '\\', '/', $upload_dir['subdir'] ); |
| 591 |
$u_path = str_replace( '\\', '/', $upload_dir['path'] ); |
| 592 |
|
| 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 |
} |
| 599 |
|
| 600 |
$upload_dir['subdir'] = $subdir; |
| 601 |
|
| 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 ] ); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
return $upload_dir; |
| 610 |
} |
| 611 |
|
| 612 |
/* |
| 613 |
add_action( 'learn_press_before_purchase_course_handler', '_learn_press_before_purchase_course_handler', 10, 2 ); |
| 614 |
function _learn_press_before_purchase_course_handler( $course_id, $cart ) { |
| 615 |
// Redirect to login page if user is not logged in |
| 616 |
if ( ! is_user_logged_in() ) { |
| 617 |
$return_url = esc_url_raw( add_query_arg( $_POST, get_the_permalink( $course_id ) ) ); |
| 618 |
$return_url = apply_filters( 'learn_press_purchase_course_login_redirect_return_url', $return_url ); |
| 619 |
$redirect = apply_filters( |
| 620 |
'learn_press_purchase_course_login_redirect', |
| 621 |
learn_press_get_login_url( $return_url ) |
| 622 |
); |
| 623 |
if ( $redirect !== false ) { |
| 624 |
learn_press_add_message( __( 'Please log in to enroll in this course', 'learnpress' ) ); |
| 625 |
|
| 626 |
if ( learn_press_is_ajax() ) { |
| 627 |
learn_press_send_json( |
| 628 |
array( |
| 629 |
'redirect' => $redirect, |
| 630 |
'result' => 'success', |
| 631 |
) |
| 632 |
); |
| 633 |
} else { |
| 634 |
wp_redirect( $redirect ); |
| 635 |
exit(); |
| 636 |
} |
| 637 |
} |
| 638 |
} else { |
| 639 |
$user = learn_press_get_current_user(); |
| 640 |
$redirect = false; |
| 641 |
if ( $user->has_finished_course( $course_id ) ) { |
| 642 |
learn_press_add_message( __( 'You have already finished the course', 'learnpress' ) ); |
| 643 |
$redirect = true; |
| 644 |
} elseif ( $user->has_purchased_course( $course_id ) ) { |
| 645 |
learn_press_add_message( __( 'You have already enrolled in this course', 'learnpress' ) ); |
| 646 |
$redirect = true; |
| 647 |
} |
| 648 |
if ( $redirect ) { |
| 649 |
wp_redirect( get_the_permalink( $course_id ) ); |
| 650 |
exit(); |
| 651 |
} |
| 652 |
} |
| 653 |
}*/ |
| 654 |
|
| 655 |
/* |
| 656 |
function learn_press_user_is( $role, $user_id = 0 ) { |
| 657 |
if ( ! $user_id ) { |
| 658 |
$user = learn_press_get_current_user(); |
| 659 |
} else { |
| 660 |
$user = learn_press_get_user( $user_id ); |
| 661 |
} |
| 662 |
if ( $role == 'admin' ) { |
| 663 |
return $user->is_admin(); |
| 664 |
} |
| 665 |
if ( $role == 'instructor' ) { |
| 666 |
return $user->is_instructor(); |
| 667 |
} |
| 668 |
|
| 669 |
return $role; |
| 670 |
}*/ |
| 671 |
|
| 672 |
/* |
| 673 |
function learn_press_profile_tab_edit_content( $current, $tab, $user ) { |
| 674 |
learn_press_get_template( |
| 675 |
'profile/tabs/edit.php', |
| 676 |
array( |
| 677 |
'user' => $user, |
| 678 |
'current' => $current, |
| 679 |
'tab' => $tab, |
| 680 |
) |
| 681 |
); |
| 682 |
}*/ |
| 683 |
|
| 684 |
/* |
| 685 |
function learn_press_update_user_option( $name, $value, $id = 0 ) { |
| 686 |
if ( ! $id ) { |
| 687 |
$id = get_current_user_id(); |
| 688 |
} |
| 689 |
$key = 'learnpress_user_options'; |
| 690 |
$options = get_user_option( $key, $id ); |
| 691 |
$options[ $name ] = $value; |
| 692 |
update_user_option( $id, $key, $options, true ); |
| 693 |
}*/ |
| 694 |
|
| 695 |
/* |
| 696 |
function learn_press_delete_user_option( $name, $id = 0 ) { |
| 697 |
if ( ! $id ) { |
| 698 |
$id = get_current_user_id(); |
| 699 |
} |
| 700 |
$key = 'learnpress_user_options'; |
| 701 |
$options = get_user_option( $key, $id ); |
| 702 |
if ( is_array( $options ) && array_key_exists( $name, $options ) ) { |
| 703 |
unset( $options[ $name ] ); |
| 704 |
update_user_option( $id, $key, $options, true ); |
| 705 |
|
| 706 |
return true; |
| 707 |
} |
| 708 |
|
| 709 |
return false; |
| 710 |
}*/ |
| 711 |
|
| 712 |
/* |
| 713 |
function learn_press_get_user_option( $name, $id = 0 ) { |
| 714 |
if ( ! $id ) { |
| 715 |
$id = get_current_user_id(); |
| 716 |
} |
| 717 |
$key = 'learnpress_user_options'; |
| 718 |
$options = get_user_option( $key, $id ); |
| 719 |
if ( is_array( $options ) && array_key_exists( $name, $options ) ) { |
| 720 |
return $options[ $name ]; |
| 721 |
} |
| 722 |
|
| 723 |
return false; |
| 724 |
}*/ |
| 725 |
|
| 726 |
/** |
| 727 |
* Update user basic information. |
| 728 |
* |
| 729 |
* @param bool $wp_error - Optional. Return WP_Error object in case updating failed. |
| 730 |
* |
| 731 |
* @return bool|mixed|WP_Error |
| 732 |
*/ |
| 733 |
function learn_press_update_user_profile_basic_information( $wp_error = false ) { |
| 734 |
$user_id = get_current_user_id(); |
| 735 |
|
| 736 |
if ( ! $user_id ) { |
| 737 |
return new WP_Error( 2, 'The user is invalid' ); |
| 738 |
} |
| 739 |
|
| 740 |
$update_data = array( |
| 741 |
'ID' => $user_id, |
| 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' ), |
| 747 |
); |
| 748 |
|
| 749 |
$update_data = apply_filters( 'learn-press/update-profile-basic-information-data', $update_data ); |
| 750 |
$update_meta = LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ?? '' ); |
| 751 |
|
| 752 |
$return = LP_Forms_Handler::update_user_data( $update_data, $update_meta ); |
| 753 |
|
| 754 |
// Update for social. |
| 755 |
$socials = LP_Request::get_param( 'user_profile_social' ); |
| 756 |
$extra_data = get_user_meta( $user_id, '_lp_extra_info', true ); |
| 757 |
|
| 758 |
if ( ! empty( $extra_data ) ) { |
| 759 |
$socials = array_merge( $extra_data, $socials ); |
| 760 |
} |
| 761 |
|
| 762 |
update_user_meta( $user_id, '_lp_extra_info', $socials ); |
| 763 |
|
| 764 |
if ( is_wp_error( $return ) ) { |
| 765 |
return $wp_error ? $return : false; |
| 766 |
} |
| 767 |
|
| 768 |
return $return; |
| 769 |
} |
| 770 |
|
| 771 |
/** |
| 772 |
* Update new password. |
| 773 |
* |
| 774 |
* @version 1.0.1 |
| 775 |
*/ |
| 776 |
function learn_press_update_user_profile_change_password() { |
| 777 |
try { |
| 778 |
$user = wp_get_current_user(); |
| 779 |
if ( ! $user ) { |
| 780 |
throw new Exception( 'error_lp_profile_update_pass', 'The user is invalid' ); |
| 781 |
} |
| 782 |
|
| 783 |
$old_pass = LP_Request::get_param( 'pass0', '', 'text', 'post' ); |
| 784 |
if ( empty( $old_pass ) ) { |
| 785 |
throw new Exception( __( 'Enter the old password!', 'learnpress' ) ); |
| 786 |
} |
| 787 |
|
| 788 |
$check_old_pass = wp_check_password( $old_pass, $user->data->user_pass, $user->ID ); |
| 789 |
if ( ! $check_old_pass ) { |
| 790 |
throw new Exception( __( 'The old password is incorrect!', 'learnpress' ) ); |
| 791 |
} |
| 792 |
|
| 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 |
} |
| 799 |
|
| 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 |
); |
| 806 |
|
| 807 |
$rs = wp_update_user( $update_data ); |
| 808 |
if ( is_wp_error( $rs ) ) { |
| 809 |
throw new Exception( $rs->get_error_message() ); |
| 810 |
} |
| 811 |
} catch ( Exception $ex ) { |
| 812 |
return new WP_Error( 'error_lp_profile_update_pass', $ex->getMessage() ); |
| 813 |
} |
| 814 |
|
| 815 |
return true; |
| 816 |
} |
| 817 |
|
| 818 |
function learn_press_get_avatar_thumb_size() { |
| 819 |
$option = LP_Settings::get_option( |
| 820 |
'avatar_dimensions', |
| 821 |
array( |
| 822 |
'width' => 250, |
| 823 |
'height' => 250, |
| 824 |
) |
| 825 |
); |
| 826 |
|
| 827 |
if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) { |
| 828 |
$option = array( |
| 829 |
'width' => 250, |
| 830 |
'height' => 250, |
| 831 |
); |
| 832 |
} |
| 833 |
|
| 834 |
// For option get_avatar_url |
| 835 |
$option['size'] = $option['width']; |
| 836 |
|
| 837 |
return $option; |
| 838 |
} |
| 839 |
|
| 840 |
function learn_press_get_course_thumbnail_dimensions() { |
| 841 |
$option = LP_Settings::get_option( |
| 842 |
'course_thumbnail_dimensions', |
| 843 |
array( |
| 844 |
'width' => 500, |
| 845 |
'height' => 300, |
| 846 |
) |
| 847 |
); |
| 848 |
|
| 849 |
if ( ! isset( $option['width'] ) || ! isset( $option['height'] ) ) { |
| 850 |
$option = array( |
| 851 |
'width' => 500, |
| 852 |
'height' => 300, |
| 853 |
); |
| 854 |
} |
| 855 |
|
| 856 |
return $option; |
| 857 |
} |
| 858 |
|
| 859 |
/* |
| 860 |
function learn_press_get_user_avatar( $user_id = 0, $size = '' ) { |
| 861 |
$user = learn_press_get_user( $user_id ); |
| 862 |
|
| 863 |
return $user->get_profile_picture( '', $size ); |
| 864 |
}*/ |
| 865 |
|
| 866 |
/** |
| 867 |
* Get profile instance for an user to view. |
| 868 |
* |
| 869 |
* @param int $for_user |
| 870 |
* |
| 871 |
* @return LP_Profile|WP_Error |
| 872 |
*/ |
| 873 |
function learn_press_get_profile( $for_user = 0 ) { |
| 874 |
return LP_Profile::instance( $for_user ); |
| 875 |
} |
| 876 |
|
| 877 |
/* |
| 878 |
function learn_press_remove_user_items( $user_id, $item_id, $course_id, $include_course = false ) { |
| 879 |
global $wpdb; |
| 880 |
|
| 881 |
settype( $item_id, 'array' ); |
| 882 |
|
| 883 |
$format = array_fill( 0, sizeof( $item_id ), '%d' ); |
| 884 |
$where = ''; |
| 885 |
|
| 886 |
$args = array( $user_id ); |
| 887 |
$args = array_merge( $args, $item_id ); |
| 888 |
|
| 889 |
if ( $course_id ) { |
| 890 |
$args[] = $course_id; |
| 891 |
$where = 'AND ref_id = %d'; |
| 892 |
} |
| 893 |
|
| 894 |
if ( $include_course ) { |
| 895 |
$where .= ' OR ( item_id = %d AND item_type = %s )'; |
| 896 |
$args[] = $course_id; |
| 897 |
$args[] = LP_COURSE_CPT; |
| 898 |
} |
| 899 |
|
| 900 |
$query = $wpdb->prepare( |
| 901 |
" |
| 902 |
DELETE |
| 903 |
FROM {$wpdb->learnpress_user_items} |
| 904 |
WHERE user_id = %d |
| 905 |
AND ( item_id IN(" . join( ',', $format ) . ") |
| 906 |
$where ) |
| 907 |
", |
| 908 |
$args |
| 909 |
); |
| 910 |
}*/ |
| 911 |
|
| 912 |
/** |
| 913 |
* Get user profile link |
| 914 |
* |
| 915 |
* @param int $user_id |
| 916 |
* @param null $tab |
| 917 |
* |
| 918 |
* @return mixed|string |
| 919 |
*/ |
| 920 |
function learn_press_user_profile_link( $user_id = 0, $tab = '' ) { |
| 921 |
if ( ! $user_id ) { |
| 922 |
$user_id = get_current_user_id(); |
| 923 |
} |
| 924 |
|
| 925 |
$user = learn_press_get_user( $user_id ); |
| 926 |
|
| 927 |
if ( ! $user ) { |
| 928 |
return ''; |
| 929 |
} |
| 930 |
|
| 931 |
global $wp_query; |
| 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(), |
| 940 |
); |
| 941 |
|
| 942 |
if ( $tab ) { |
| 943 |
$args['tab'] = $tab; |
| 944 |
} |
| 945 |
|
| 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'] ); |
| 952 |
} |
| 953 |
|
| 954 |
$profile_link = trailingslashit( learn_press_get_page_link( 'profile' ) ); |
| 955 |
if ( $profile_link ) { |
| 956 |
if ( get_option( 'permalink_structure' ) ) { |
| 957 |
$url = trailingslashit( $profile_link . join( '/', array_values( $args ) ) ); |
| 958 |
} else { |
| 959 |
$url = esc_url_raw( add_query_arg( $args, $profile_link ) ); |
| 960 |
} |
| 961 |
} else { |
| 962 |
$url = get_author_posts_url( $user_id ); |
| 963 |
} |
| 964 |
|
| 965 |
return apply_filters( 'learn_press_user_profile_link', $url, $user_id, $tab ); |
| 966 |
} |
| 967 |
|
| 968 |
/* |
| 969 |
function learn_press_create_user_item( $args = array(), $wp_error = false ) { |
| 970 |
global $wpdb; |
| 971 |
|
| 972 |
$defaults = array( |
| 973 |
'user_id' => get_current_user_id(), |
| 974 |
'item_id' => '', |
| 975 |
'start_time' => time(), |
| 976 |
'end_time' => '', |
| 977 |
'graduation' => '', |
| 978 |
'item_type' => '', |
| 979 |
'status' => '', |
| 980 |
'ref_id' => 0, |
| 981 |
'ref_type' => '', |
| 982 |
'parent_id' => 0, |
| 983 |
'create_meta' => array(), |
| 984 |
); |
| 985 |
|
| 986 |
$item_data = wp_parse_args( $args, $defaults ); |
| 987 |
|
| 988 |
// Validate item_id and post type |
| 989 |
if ( empty( $item_data['item_id'] ) ) { |
| 990 |
if ( $wp_error ) { |
| 991 |
return new WP_Error( 'invalid_item_id', __( 'Invalid item id.', 'learnpress' ) ); |
| 992 |
} |
| 993 |
|
| 994 |
return 0; |
| 995 |
} |
| 996 |
|
| 997 |
$post_type = learn_press_get_post_type( $item_data['item_id'] ); |
| 998 |
if ( empty( $item_data['item_type'] ) && $post_type ) { |
| 999 |
$item_data['item_type'] = $post_type; |
| 1000 |
} |
| 1001 |
|
| 1002 |
// Get id and type of ref if they are null |
| 1003 |
if ( ! empty( $item_data['parent_id'] ) && ( empty( $item_data['ref_id'] ) || ( empty( $item_data['ref_type'] ) ) ) ) { |
| 1004 |
$parent = $wpdb->get_row( |
| 1005 |
$wpdb->prepare( |
| 1006 |
"SELECT * FROM {$wpdb->learnpress_user_items} WHERE %d", |
| 1007 |
$item_data['parent_id'] |
| 1008 |
) |
| 1009 |
); |
| 1010 |
|
| 1011 |
if ( $parent ) { |
| 1012 |
if ( empty( $item_data['ref_id'] ) ) { |
| 1013 |
$item_data['ref_id'] = $parent->item_id; |
| 1014 |
} |
| 1015 |
|
| 1016 |
if ( empty( $item_data['ref_type'] ) ) { |
| 1017 |
$item_data['ref_type'] = $parent->item_type; |
| 1018 |
} |
| 1019 |
} |
| 1020 |
} |
| 1021 |
|
| 1022 |
// Filter |
| 1023 |
$item_data = apply_filters( 'learn-press/create-user-item-data', $item_data ); |
| 1024 |
if ( ! $item_data ) { |
| 1025 |
if ( $wp_error ) { |
| 1026 |
return new WP_Error( 'invalid_item_data', __( 'Invalid item data.', 'learnpress' ) ); |
| 1027 |
} |
| 1028 |
|
| 1029 |
return 0; |
| 1030 |
} |
| 1031 |
|
| 1032 |
do_action( 'learn-press/before-create-user-item', $item_data ); |
| 1033 |
|
| 1034 |
$create_meta = ! empty( $item_data['create_meta'] ) ? $item_data['create_meta'] : false; |
| 1035 |
|
| 1036 |
if ( $create_meta ) { |
| 1037 |
unset( $item_data['create_meta'] ); |
| 1038 |
} |
| 1039 |
|
| 1040 |
$user_item = new LP_User_Item( $item_data ); |
| 1041 |
$result = $user_item->update(); |
| 1042 |
if ( ! $result || is_wp_error( $result ) ) { |
| 1043 |
if ( $wp_error && is_wp_error( $result ) ) { |
| 1044 |
return $result; |
| 1045 |
} |
| 1046 |
|
| 1047 |
return 0; |
| 1048 |
} |
| 1049 |
|
| 1050 |
do_action( 'learn-press/created-user-item', $user_item, $item_data ); |
| 1051 |
|
| 1052 |
$create_meta = apply_filters( 'learn-press/create-user-item-meta', $create_meta, $item_data ); |
| 1053 |
if ( ! $create_meta ) { |
| 1054 |
return $user_item; |
| 1055 |
} |
| 1056 |
|
| 1057 |
do_action( 'learn-press/before-create-user-item-meta', $create_meta ); |
| 1058 |
|
| 1059 |
foreach ( $create_meta as $key => $value ) { |
| 1060 |
learn_press_update_user_item_meta( $user_item->get_user_item_id(), $key, $value ); |
| 1061 |
} |
| 1062 |
|
| 1063 |
do_action( 'learn-press/created-user-item-meta', $user_item, $create_meta ); |
| 1064 |
|
| 1065 |
return $user_item; |
| 1066 |
}*/ |
| 1067 |
|
| 1068 |
/* |
| 1069 |
function learn_press_isset_user_item_for_quiz( $quiz_id, $course_id ) { |
| 1070 |
global $wpdb; |
| 1071 |
|
| 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 ); |
| 1073 |
$col = $wpdb->get_col( $query ); |
| 1074 |
|
| 1075 |
if ( ! empty( $col ) ) { |
| 1076 |
return $col; |
| 1077 |
} else { |
| 1078 |
return false; |
| 1079 |
} |
| 1080 |
}*/ |
| 1081 |
|
| 1082 |
/** |
| 1083 |
* Prepares list of questions for rest api. |
| 1084 |
* |
| 1085 |
* @param int[] $question_ids |
| 1086 |
* @param array $args |
| 1087 |
* |
| 1088 |
* @return array |
| 1089 |
* @since 3.3.0 |
| 1090 |
*/ |
| 1091 |
function learn_press_rest_prepare_user_questions( array $question_ids = array(), array $args = array() ): array { |
| 1092 |
if ( is_numeric( $args ) ) { |
| 1093 |
|
| 1094 |
} else { |
| 1095 |
$args = wp_parse_args( |
| 1096 |
$args, |
| 1097 |
array( |
| 1098 |
'instant_hint' => true, |
| 1099 |
'instant_check' => true, |
| 1100 |
'quiz_status' => '', |
| 1101 |
'checked_questions' => array(), |
| 1102 |
'hinted_questions' => array(), |
| 1103 |
'answered' => array(), |
| 1104 |
'show_correct_review' => true, |
| 1105 |
) |
| 1106 |
); |
| 1107 |
} |
| 1108 |
|
| 1109 |
$checkedQuestions = $args['checked_questions']; |
| 1110 |
$hintedQuestions = $args['hinted_questions']; |
| 1111 |
$instantHint = $args['instant_hint']; |
| 1112 |
$instantCheck = $args['instant_check']; |
| 1113 |
$quizStatus = $args['quiz_status']; |
| 1114 |
$answered = $args['answered']; |
| 1115 |
$status = $args['status'] ?? ''; |
| 1116 |
$questions = array(); |
| 1117 |
|
| 1118 |
if ( $question_ids ) { |
| 1119 |
foreach ( $question_ids as $id ) { |
| 1120 |
$question = learn_press_get_question( $id ); |
| 1121 |
$hasHint = false; |
| 1122 |
$hasExplanation = false; |
| 1123 |
$canCheck = false; |
| 1124 |
$hinted = false; |
| 1125 |
$checked = false; |
| 1126 |
$theHint = $question->get_hint(); |
| 1127 |
$theExplanation = ''; |
| 1128 |
|
| 1129 |
if ( $instantCheck || $status == 'completed' ) { |
| 1130 |
$theExplanation = $question->get_explanation(); |
| 1131 |
$checked = in_array( $id, $checkedQuestions ); |
| 1132 |
$hasExplanation = (bool) $theExplanation; |
| 1133 |
} |
| 1134 |
|
| 1135 |
$mark = $question->get_mark() ? $question->get_mark() : 1; |
| 1136 |
|
| 1137 |
$questionData = array( |
| 1138 |
'object' => $question, |
| 1139 |
'id' => absint( $id ), |
| 1140 |
'title' => $question->get_title(), |
| 1141 |
'type' => $question->get_type(), |
| 1142 |
'point' => $mark, |
| 1143 |
); |
| 1144 |
|
| 1145 |
$content = $question->get_content(); |
| 1146 |
if ( $content ) { |
| 1147 |
$questionData['content'] = $content; |
| 1148 |
} |
| 1149 |
|
| 1150 |
if ( $theHint ) { |
| 1151 |
$questionData['hint'] = $theHint; |
| 1152 |
} |
| 1153 |
|
| 1154 |
if ( $status == 'completed' || ( $checked && $theExplanation ) ) { |
| 1155 |
$questionData['explanation'] = $theExplanation; |
| 1156 |
} |
| 1157 |
|
| 1158 |
if ( $hasExplanation ) { |
| 1159 |
$questionData['has_explanation'] = $hasExplanation; |
| 1160 |
|
| 1161 |
if ( $checked ) { |
| 1162 |
$questionData['explanation'] = $theExplanation; |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
$with_true_or_false = ( $checked || ( $quizStatus == 'completed' && $args['show_correct_review'] ) ); |
| 1167 |
|
| 1168 |
// if ( $question->is_support( 'answer-options' ) ) { |
| 1169 |
$questionData['options'] = learn_press_get_question_options_for_js( |
| 1170 |
$question, |
| 1171 |
array( |
| 1172 |
'include_is_true' => $with_true_or_false, |
| 1173 |
'answer' => $answered[ $id ]['answered'] ?? '', |
| 1174 |
) |
| 1175 |
); |
| 1176 |
// } |
| 1177 |
|
| 1178 |
$questions[] = $questionData; |
| 1179 |
} |
| 1180 |
|
| 1181 |
/** |
| 1182 |
* Remove answered |
| 1183 |
*/ |
| 1184 |
if ( $quizStatus !== 'completed' ) { |
| 1185 |
if ( $checkedQuestions && $quizStatus ) { |
| 1186 |
|
| 1187 |
$omitIds = array_diff( $question_ids, $checkedQuestions ); |
| 1188 |
|
| 1189 |
if ( $omitIds ) { |
| 1190 |
foreach ( $omitIds as $omitId ) { |
| 1191 |
if ( ! empty( $answered[ $omitId ] ) ) { |
| 1192 |
unset( $answered[ $omitId ] ); |
| 1193 |
} |
| 1194 |
} |
| 1195 |
} |
| 1196 |
} |
| 1197 |
} |
| 1198 |
} |
| 1199 |
|
| 1200 |
return apply_filters( 'learn-press/list-questions-data', $questions ); |
| 1201 |
} |
| 1202 |
|
| 1203 |
/** |
| 1204 |
* Update extra profile data upon update user. |
| 1205 |
* |
| 1206 |
* @param int $user_id |
| 1207 |
* |
| 1208 |
* @since 4.0.0 |
| 1209 |
*/ |
| 1210 |
function learn_press_update_extra_user_profile_fields( $user_id ) { |
| 1211 |
if ( ! current_user_can( 'edit_user', $user_id ) ) { |
| 1212 |
return; |
| 1213 |
} |
| 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 |
|
| 1234 |
if ( isset( $_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 ); |
| 1237 |
} |
| 1238 |
} |
| 1239 |
|
| 1240 |
add_action( 'personal_options_update', 'learn_press_update_extra_user_profile_fields' ); |
| 1241 |
add_action( 'edit_user_profile_update', 'learn_press_update_extra_user_profile_fields' ); |
| 1242 |
|
| 1243 |
/** |
| 1244 |
* Get extra profile info data |
| 1245 |
* |
| 1246 |
* @param int $user_id |
| 1247 |
* |
| 1248 |
* @return array |
| 1249 |
* @since 4.0.1 |
| 1250 |
*/ |
| 1251 |
function learn_press_get_user_extra_profile_info( $user_id = 0 ) { |
| 1252 |
if ( ! $user_id ) { |
| 1253 |
$user_id = get_current_user_id(); |
| 1254 |
} |
| 1255 |
|
| 1256 |
$extra_profile_info = get_the_author_meta( '_lp_extra_info', $user_id ); |
| 1257 |
$social_fields = learn_press_social_profiles(); |
| 1258 |
|
| 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 |
} |
| 1267 |
|
| 1268 |
return apply_filters( 'learn-press/user-extra-profile-info', $user_socials, $user_id ); |
| 1269 |
} |
| 1270 |
|
| 1271 |
/** |
| 1272 |
* @return array |
| 1273 |
* @since 3.x.x |
| 1274 |
* @version 4.0.2 |
| 1275 |
*/ |
| 1276 |
function learn_press_social_profiles() { |
| 1277 |
return apply_filters( |
| 1278 |
'learn-press/social-profiles', |
| 1279 |
array( |
| 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' ), |
| 1284 |
) |
| 1285 |
); |
| 1286 |
} |
| 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 |
|
| 1310 |
function lp_add_default_fields( $fields ) { |
| 1311 |
$first_name = LP_Settings::get_option( 'enable_register_first_name', 'no' ); |
| 1312 |
|
| 1313 |
if ( $first_name === 'yes' ) { |
| 1314 |
?> |
| 1315 |
<li class="form-field"> |
| 1316 |
<label for="reg_first_name"><?php esc_html_e( 'First name', 'learnpress' ); ?></label> |
| 1317 |
<input id="reg_first_name" name="reg_first_name" type="text" |
| 1318 |
placeholder="<?php esc_attr_e( 'First name', 'learnpress' ); ?>" |
| 1319 |
value="<?php echo esc_attr( wp_unslash( $_POST['reg_first_name'] ?? '' ) ); ?>"> |
| 1320 |
</li> |
| 1321 |
<?php |
| 1322 |
} |
| 1323 |
|
| 1324 |
$last_name = LP_Settings::instance()->get( 'enable_register_last_name' ); |
| 1325 |
|
| 1326 |
if ( $last_name === 'yes' ) { |
| 1327 |
?> |
| 1328 |
<li class="form-field"> |
| 1329 |
<label for="reg_last_name"><?php esc_html_e( 'Last name', 'learnpress' ); ?></label> |
| 1330 |
<input id="reg_last_name" name="reg_last_name" type="text" |
| 1331 |
placeholder="<?php esc_attr_e( 'Last name', 'learnpress' ); ?>" |
| 1332 |
value="<?php echo esc_attr( wp_unslash( $_POST['reg_last_name'] ?? '' ) ); ?>"> |
| 1333 |
</li> |
| 1334 |
<?php |
| 1335 |
} |
| 1336 |
|
| 1337 |
$display_name = LP_Settings::instance()->get( 'enable_register_display_name' ); |
| 1338 |
|
| 1339 |
if ( $display_name === 'yes' ) { |
| 1340 |
?> |
| 1341 |
<li class="form-field"> |
| 1342 |
<label for="reg_display_name"><?php esc_html_e( 'Display name', 'learnpress' ); ?></label> |
| 1343 |
<input id="reg_display_name" name="reg_display_name" type="text" |
| 1344 |
placeholder="<?php esc_attr_e( 'Display name', 'learnpress' ); ?>" |
| 1345 |
value="<?php echo esc_attr( wp_unslash( $_POST['reg_display_name'] ?? '' ) ); ?>"> |
| 1346 |
</li> |
| 1347 |
<?php |
| 1348 |
} |
| 1349 |
} |
| 1350 |
|
| 1351 |
add_filter( 'learn-press/after-form-register-fields', 'lp_add_default_fields' ); |
| 1352 |
|
| 1353 |
function lp_custom_register_fields_display() { |
| 1354 |
?> |
| 1355 |
<?php $custom_fields = LP_Profile::get_register_fields_custom(); ?> |
| 1356 |
|
| 1357 |
<?php if ( $custom_fields ) : ?> |
| 1358 |
<?php foreach ( $custom_fields as $custom_field ) : ?> |
| 1359 |
<?php |
| 1360 |
$cf_class = ''; |
| 1361 |
if ( $custom_field['required'] == 'yes' ) { |
| 1362 |
$cf_class = ' required'; |
| 1363 |
?> |
| 1364 |
<style> |
| 1365 |
.required label { |
| 1366 |
font-weight: bold; |
| 1367 |
} |
| 1368 |
|
| 1369 |
.required label:after { |
| 1370 |
content: ' *'; |
| 1371 |
display: inline; |
| 1372 |
} |
| 1373 |
</style> |
| 1374 |
<?php |
| 1375 |
} |
| 1376 |
|
| 1377 |
if ( isset( $custom_field['id'] ) ) { |
| 1378 |
?> |
| 1379 |
<?php $value = $custom_field['id']; ?> |
| 1380 |
|
| 1381 |
<li class="form-field<?php echo esc_attr( $cf_class ); ?>"> |
| 1382 |
<?php |
| 1383 |
switch ( $custom_field['type'] ) { |
| 1384 |
case 'text': |
| 1385 |
case 'number': |
| 1386 |
case 'email': |
| 1387 |
case 'url': |
| 1388 |
case 'tel': |
| 1389 |
?> |
| 1390 |
<label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label> |
| 1391 |
<input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]" |
| 1392 |
type="<?php echo esc_attr( $custom_field['type'] ); ?>" class="regular-text" |
| 1393 |
value=""/> |
| 1394 |
<?php |
| 1395 |
break; |
| 1396 |
case 'textarea': |
| 1397 |
?> |
| 1398 |
<label for="description"><?php echo esc_html( $custom_field['name'] ); ?></label> |
| 1399 |
<textarea name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]"></textarea> |
| 1400 |
<?php |
| 1401 |
break; |
| 1402 |
case 'checkbox': |
| 1403 |
?> |
| 1404 |
<label> |
| 1405 |
<input name="_lp_custom_register_form[<?php echo esc_attr( $value ); ?>]" |
| 1406 |
type="<?php echo esc_attr( $custom_field['type'] ); ?>" value="1"> |
| 1407 |
<?php echo esc_html( $custom_field['name'] ); ?> |
| 1408 |
</label> |
| 1409 |
<?php |
| 1410 |
break; |
| 1411 |
} |
| 1412 |
?> |
| 1413 |
</li> |
| 1414 |
<?php } ?> |
| 1415 |
<?php endforeach; ?> |
| 1416 |
<?php endif; ?> |
| 1417 |
<?php |
| 1418 |
} |
| 1419 |
|
| 1420 |
add_action( 'learn-press/after-form-register-fields', 'lp_custom_register_fields_display' ); |
| 1421 |
|
| 1422 |
/** |
| 1423 |
* Custom register fields |
| 1424 |
* |
| 1425 |
* @param [type] $user_id |
| 1426 |
* |
| 1427 |
* @return void |
| 1428 |
*/ |
| 1429 |
function lp_user_custom_register_fields( $user_id, $fields = array() ) { |
| 1430 |
if ( ! empty( $fields ) ) { |
| 1431 |
update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $fields ) ); |
| 1432 |
} elseif ( isset( $_POST['_lp_custom_register'] ) ) { |
| 1433 |
update_user_meta( $user_id, '_lp_custom_register', LP_Helper::sanitize_params_submitted( $_POST['_lp_custom_register'] ) ); |
| 1434 |
} |
| 1435 |
} |
| 1436 |
|
| 1437 |
add_action( 'personal_options_update', 'lp_user_custom_register_fields' ); |
| 1438 |
add_action( 'edit_user_profile_update', 'lp_user_custom_register_fields' ); |
| 1439 |
|
| 1440 |
function lp_get_user_custom_register_fields( $user_id = 0 ) { |
| 1441 |
if ( ! $user_id ) { |
| 1442 |
$user_id = get_current_user_id(); |
| 1443 |
} |
| 1444 |
|
| 1445 |
$register_fields = get_the_author_meta( '_lp_custom_register', $user_id ); |
| 1446 |
$defaults = lp_get_user_custom_fields(); |
| 1447 |
|
| 1448 |
$extra_profile_info = wp_parse_args( $register_fields, $defaults ); |
| 1449 |
|
| 1450 |
return apply_filters( 'lp/user-custom-register-fields', $register_fields, $user_id ); |
| 1451 |
} |
| 1452 |
|
| 1453 |
function lp_get_user_custom_fields() { |
| 1454 |
$custom_fields = LP_Settings::get_option( 'register_profile_fields', array() ); |
| 1455 |
|
| 1456 |
$output = array(); |
| 1457 |
|
| 1458 |
if ( $custom_fields ) { |
| 1459 |
foreach ( $custom_fields as $field ) { |
| 1460 |
$output[ $field['id'] ] = ''; |
| 1461 |
} |
| 1462 |
} |
| 1463 |
|
| 1464 |
return $output; |
| 1465 |
} |
| 1466 |
|
| 1467 |
/** |
| 1468 |
* Check extra user data is a social profile. |
| 1469 |
* |
| 1470 |
* @param $key |
| 1471 |
* |
| 1472 |
* @return bool |
| 1473 |
* @since 4.0.0 |
| 1474 |
* @deprecated 4.3.2 |
| 1475 |
*/ |
| 1476 |
function learn_press_is_social_profile( $key ) { |
| 1477 |
_deprecated_function( __FUNCTION__, '4.3.2' ); |
| 1478 |
|
| 1479 |
return true; |
| 1480 |
|
| 1481 |
$is_socials = learn_press_social_profiles(); |
| 1482 |
|
| 1483 |
return in_array( $key, $is_socials ); |
| 1484 |
} |
| 1485 |
|
| 1486 |
/** |
| 1487 |
* Get extra profile fields will be registered in backend profile. |
| 1488 |
* |
| 1489 |
* @return array |
| 1490 |
* @since 4.0.0 |
| 1491 |
* @deprecated 4.3.2 |
| 1492 |
*/ |
| 1493 |
function learn_press_get_user_extra_profile_fields() { |
| 1494 |
_deprecated_function( __FUNCTION__, '4.3.2' ); |
| 1495 |
return array(); |
| 1496 |
|
| 1497 |
$socials = learn_press_social_profiles(); |
| 1498 |
|
| 1499 |
return $socials; |
| 1500 |
|
| 1501 |
$fields = array(); |
| 1502 |
|
| 1503 |
foreach ( $socials as $social ) { |
| 1504 |
$fields[ $social ] = learn_press_social_profile_name( $social ); |
| 1505 |
} |
| 1506 |
|
| 1507 |
return apply_filters( 'learn-press/user-extra-profile-fields', $fields ); |
| 1508 |
} |
| 1509 |
|
| 1510 |
/** |
| 1511 |
* Show courses user enrolled on backend |
| 1512 |
* |
| 1513 |
* @param $user |
| 1514 |
* |
| 1515 |
* @return void |
| 1516 |
*/ |
| 1517 |
function learn_press_user_profile_data( $user ) { |
| 1518 |
if ( ! is_admin() ) { |
| 1519 |
return; |
| 1520 |
} |
| 1521 |
|
| 1522 |
learn_press_admin_view( 'backend-user-profile', array( 'user' => $user ) ); |
| 1523 |
learn_press_admin_view( 'user/courses.php', array( 'user_id' => $user->ID ) ); |
| 1524 |
} |
| 1525 |
|
| 1526 |
add_action( 'edit_user_profile', 'learn_press_user_profile_data', 1000 ); |
| 1527 |
add_action( 'show_user_profile', 'learn_press_user_profile_data', 1000 ); |
| 1528 |
|
| 1529 |
/* |
| 1530 |
function learnpress_get_count_by_user( $user_id = '', $post_type = 'lp_course' ) { |
| 1531 |
if ( empty( $user_id ) ) { |
| 1532 |
return false; |
| 1533 |
} |
| 1534 |
|
| 1535 |
$args = array( |
| 1536 |
'author' => $user_id, |
| 1537 |
'posts_per_page' => - 1, |
| 1538 |
'post_type' => $post_type, |
| 1539 |
'post_status' => 'any', |
| 1540 |
); |
| 1541 |
|
| 1542 |
$posts = get_posts( $args ); |
| 1543 |
|
| 1544 |
$output = array( |
| 1545 |
'all' => count( $posts ), |
| 1546 |
'publish' => array(), |
| 1547 |
'pending' => array(), |
| 1548 |
); |
| 1549 |
|
| 1550 |
$pending = $public = array(); |
| 1551 |
|
| 1552 |
if ( ! empty( $posts ) ) { |
| 1553 |
foreach ( $posts as $post ) { |
| 1554 |
switch ( $post->post_status ) { |
| 1555 |
case 'pending': |
| 1556 |
$pending[] = $post; |
| 1557 |
break; |
| 1558 |
case 'publish': |
| 1559 |
$public[] = $post; |
| 1560 |
break; |
| 1561 |
default: |
| 1562 |
break; |
| 1563 |
} |
| 1564 |
} |
| 1565 |
} |
| 1566 |
|
| 1567 |
return array( |
| 1568 |
'all' => count( $posts ), |
| 1569 |
'publish' => count( $public ), |
| 1570 |
'pending' => count( $pending ), |
| 1571 |
); |
| 1572 |
}*/ |
| 1573 |
|