@@ -25,8 +25,8 @@ discard block |
||
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Check if WooCommerce is present. |
| 28 | - * |
|
| 29 | - * @deprecated since 1.9.0 use Sensei_WC::is_woocommerce_present() |
|
| 28 | + * |
|
| 29 | + * @deprecated since 1.9.0 use Sensei_WC::is_woocommerce_present() |
|
| 30 | 30 | * @access public |
| 31 | 31 | * @since 1.0.2 |
| 32 | 32 | * @static |
@@ -34,14 +34,14 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | public static function sensei_is_woocommerce_present () { |
| 36 | 36 | |
| 37 | - return Sensei_WC::is_woocommerce_present(); |
|
| 37 | + return Sensei_WC::is_woocommerce_present(); |
|
| 38 | 38 | |
| 39 | 39 | } // End sensei_is_woocommerce_present() |
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | 42 | * Check if WooCommerce is active. |
| 43 | - * |
|
| 44 | - * @deprecated since 1.9.0 use Sensei_WC::is_woocommerce_active |
|
| 43 | + * |
|
| 44 | + * @deprecated since 1.9.0 use Sensei_WC::is_woocommerce_active |
|
| 45 | 45 | * @access public |
| 46 | 46 | * @since 1.0.2 |
| 47 | 47 | * @static |
@@ -186,15 +186,15 @@ discard block |
||
| 186 | 186 | add_filter( 'comments_clauses', array( __CLASS__, 'comment_any_status_filter' ) ); |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - //Get the comments |
|
| 190 | - /** |
|
| 191 | - * This filter runs inside Sensei_Utils::sensei_check_for_activity |
|
| 192 | - * |
|
| 193 | - * It runs while getting the comments for the given request. |
|
| 194 | - * |
|
| 195 | - * @param int|array $comments |
|
| 196 | - */ |
|
| 197 | - $comments = apply_filters('sensei_check_for_activity', get_comments( $args ) ); |
|
| 189 | + //Get the comments |
|
| 190 | + /** |
|
| 191 | + * This filter runs inside Sensei_Utils::sensei_check_for_activity |
|
| 192 | + * |
|
| 193 | + * It runs while getting the comments for the given request. |
|
| 194 | + * |
|
| 195 | + * @param int|array $comments |
|
| 196 | + */ |
|
| 197 | + $comments = apply_filters('sensei_check_for_activity', get_comments( $args ) ); |
|
| 198 | 198 | |
| 199 | 199 | remove_filter( 'comments_clauses', array( __CLASS__, 'comment_multiple_status_filter' ) ); |
| 200 | 200 | remove_filter( 'comments_clauses', array( __CLASS__, 'comment_any_status_filter' ) ); |
@@ -279,18 +279,18 @@ discard block |
||
| 279 | 279 | return $dataset_changes; |
| 280 | 280 | } // End sensei_delete_activities() |
| 281 | 281 | |
| 282 | - /** |
|
| 283 | - * Delete all activity for specified user |
|
| 284 | - * @access public |
|
| 282 | + /** |
|
| 283 | + * Delete all activity for specified user |
|
| 284 | + * @access public |
|
| 285 | 285 | * @since 1.5.0 |
| 286 | - * @param integer $user_id User ID |
|
| 287 | - * @return boolean |
|
| 288 | - */ |
|
| 289 | - public static function delete_all_user_activity( $user_id = 0 ) { |
|
| 286 | + * @param integer $user_id User ID |
|
| 287 | + * @return boolean |
|
| 288 | + */ |
|
| 289 | + public static function delete_all_user_activity( $user_id = 0 ) { |
|
| 290 | 290 | |
| 291 | - $dataset_changes = false; |
|
| 291 | + $dataset_changes = false; |
|
| 292 | 292 | |
| 293 | - if( $user_id ) { |
|
| 293 | + if( $user_id ) { |
|
| 294 | 294 | |
| 295 | 295 | $activities = Sensei_Utils::sensei_check_for_activity( array( 'user_id' => $user_id ), true ); |
| 296 | 296 | |
@@ -334,35 +334,35 @@ discard block |
||
| 334 | 334 | return $activity_value; |
| 335 | 335 | } // End sensei_get_activity_value() |
| 336 | 336 | |
| 337 | - /** |
|
| 338 | - * Checks if a user (by email) has bought an item. |
|
| 339 | - * |
|
| 340 | - * @deprecated since 1.9.0 use Sensei_WC::has_customer_bought_product($user_id, $product_id) |
|
| 341 | - * @access public |
|
| 342 | - * @since 1.0.0 |
|
| 343 | - * @param string $customer_email |
|
| 344 | - * @param int $user_id |
|
| 345 | - * @param int $product_id |
|
| 346 | - * @return bool |
|
| 347 | - */ |
|
| 348 | - public static function sensei_customer_bought_product ( $customer_email, $user_id, $product_id ) { |
|
| 337 | + /** |
|
| 338 | + * Checks if a user (by email) has bought an item. |
|
| 339 | + * |
|
| 340 | + * @deprecated since 1.9.0 use Sensei_WC::has_customer_bought_product($user_id, $product_id) |
|
| 341 | + * @access public |
|
| 342 | + * @since 1.0.0 |
|
| 343 | + * @param string $customer_email |
|
| 344 | + * @param int $user_id |
|
| 345 | + * @param int $product_id |
|
| 346 | + * @return bool |
|
| 347 | + */ |
|
| 348 | + public static function sensei_customer_bought_product ( $customer_email, $user_id, $product_id ) { |
|
| 349 | 349 | |
| 350 | - $emails = array(); |
|
| 350 | + $emails = array(); |
|
| 351 | 351 | |
| 352 | - if ( $user_id ) { |
|
| 353 | - $user = get_user_by( 'id', intval( $user_id ) ); |
|
| 354 | - $emails[] = $user->user_email; |
|
| 355 | - } |
|
| 352 | + if ( $user_id ) { |
|
| 353 | + $user = get_user_by( 'id', intval( $user_id ) ); |
|
| 354 | + $emails[] = $user->user_email; |
|
| 355 | + } |
|
| 356 | 356 | |
| 357 | - if ( is_email( $customer_email ) ) |
|
| 358 | - $emails[] = $customer_email; |
|
| 357 | + if ( is_email( $customer_email ) ) |
|
| 358 | + $emails[] = $customer_email; |
|
| 359 | 359 | |
| 360 | - if ( sizeof( $emails ) == 0 ) |
|
| 361 | - return false; |
|
| 360 | + if ( sizeof( $emails ) == 0 ) |
|
| 361 | + return false; |
|
| 362 | 362 | |
| 363 | - return Sensei_WC::has_customer_bought_product( $user_id, $product_id ); |
|
| 363 | + return Sensei_WC::has_customer_bought_product( $user_id, $product_id ); |
|
| 364 | 364 | |
| 365 | - } // End sensei_customer_bought_product() |
|
| 365 | + } // End sensei_customer_bought_product() |
|
| 366 | 366 | |
| 367 | 367 | /** |
| 368 | 368 | * Load the WordPress rich text editor |
@@ -398,7 +398,7 @@ discard block |
||
| 398 | 398 | /** |
| 399 | 399 | * Save quiz answers submitted by users |
| 400 | 400 | * @param array $submitted User's quiz answers |
| 401 | - * @param int $user_id |
|
| 401 | + * @param int $user_id |
|
| 402 | 402 | * @return boolean Whether the answers were saved or not |
| 403 | 403 | */ |
| 404 | 404 | public static function sensei_save_quiz_answers( $submitted = array(), $user_id = 0 ) { |
@@ -467,72 +467,72 @@ discard block |
||
| 467 | 467 | |
| 468 | 468 | require_once( ABSPATH . 'wp-admin/includes/admin.php' ); |
| 469 | 469 | |
| 470 | - /** |
|
| 471 | - * Filter the data array for the Sensei wp_handle_upload function call |
|
| 472 | - * |
|
| 473 | - * This filter was mainly added for Unit Testing purposes. |
|
| 474 | - * |
|
| 475 | - * @since 1.7.4 |
|
| 476 | - * |
|
| 477 | - * @param array $file_upload_args { |
|
| 478 | - * array of current values |
|
| 479 | - * |
|
| 480 | - * @type string test_form set to false by default |
|
| 481 | - * } |
|
| 482 | - */ |
|
| 483 | - $file_upload_args = apply_filters( 'sensei_file_upload_args', array('test_form' => false ) ); |
|
| 470 | + /** |
|
| 471 | + * Filter the data array for the Sensei wp_handle_upload function call |
|
| 472 | + * |
|
| 473 | + * This filter was mainly added for Unit Testing purposes. |
|
| 474 | + * |
|
| 475 | + * @since 1.7.4 |
|
| 476 | + * |
|
| 477 | + * @param array $file_upload_args { |
|
| 478 | + * array of current values |
|
| 479 | + * |
|
| 480 | + * @type string test_form set to false by default |
|
| 481 | + * } |
|
| 482 | + */ |
|
| 483 | + $file_upload_args = apply_filters( 'sensei_file_upload_args', array('test_form' => false ) ); |
|
| 484 | 484 | |
| 485 | - $file_return = wp_handle_upload( $file, $file_upload_args ); |
|
| 485 | + $file_return = wp_handle_upload( $file, $file_upload_args ); |
|
| 486 | 486 | |
| 487 | - if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) { |
|
| 488 | - return false; |
|
| 489 | - } else { |
|
| 487 | + if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) { |
|
| 488 | + return false; |
|
| 489 | + } else { |
|
| 490 | 490 | |
| 491 | - $filename = $file_return['file']; |
|
| 491 | + $filename = $file_return['file']; |
|
| 492 | 492 | |
| 493 | - $attachment = array( |
|
| 494 | - 'post_mime_type' => $file_return['type'], |
|
| 495 | - 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
| 496 | - 'post_content' => '', |
|
| 497 | - 'post_status' => 'inherit', |
|
| 498 | - 'guid' => $file_return['url'] |
|
| 499 | - ); |
|
| 493 | + $attachment = array( |
|
| 494 | + 'post_mime_type' => $file_return['type'], |
|
| 495 | + 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), |
|
| 496 | + 'post_content' => '', |
|
| 497 | + 'post_status' => 'inherit', |
|
| 498 | + 'guid' => $file_return['url'] |
|
| 499 | + ); |
|
| 500 | 500 | |
| 501 | - $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
| 501 | + $attachment_id = wp_insert_attachment( $attachment, $filename ); |
|
| 502 | 502 | |
| 503 | - require_once(ABSPATH . 'wp-admin/includes/image.php'); |
|
| 504 | - $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 505 | - wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 503 | + require_once(ABSPATH . 'wp-admin/includes/image.php'); |
|
| 504 | + $attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename ); |
|
| 505 | + wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
| 506 | 506 | |
| 507 | - if( 0 < intval( $attachment_id ) ) { |
|
| 508 | - return $attachment_id; |
|
| 509 | - } |
|
| 510 | - } |
|
| 507 | + if( 0 < intval( $attachment_id ) ) { |
|
| 508 | + return $attachment_id; |
|
| 509 | + } |
|
| 510 | + } |
|
| 511 | 511 | |
| 512 | - return false; |
|
| 512 | + return false; |
|
| 513 | 513 | } |
| 514 | 514 | |
| 515 | 515 | /** |
| 516 | 516 | * Grade quiz automatically |
| 517 | - * |
|
| 518 | - * This function grades each question automatically if the are auto gradable. |
|
| 519 | - * It store all question grades. |
|
| 520 | - * |
|
| 521 | - * @deprecated since 1.7.4 use WooThemes_Sensei_Grading::grade_quiz_auto instead |
|
| 522 | - * |
|
| 517 | + * |
|
| 518 | + * This function grades each question automatically if the are auto gradable. |
|
| 519 | + * It store all question grades. |
|
| 520 | + * |
|
| 521 | + * @deprecated since 1.7.4 use WooThemes_Sensei_Grading::grade_quiz_auto instead |
|
| 522 | + * |
|
| 523 | 523 | * @param integer $quiz_id ID of quiz |
| 524 | 524 | * @param array $submitted questions id ans answers { |
| 525 | - * @type int $question_id |
|
| 526 | - * @type mixed $answer |
|
| 527 | - * } |
|
| 525 | + * @type int $question_id |
|
| 526 | + * @type mixed $answer |
|
| 527 | + * } |
|
| 528 | 528 | * @param integer $total_questions Total questions in quiz (not used) |
| 529 | - * @param string $quiz_grade_type Optional defaults to auto |
|
| 530 | - * |
|
| 529 | + * @param string $quiz_grade_type Optional defaults to auto |
|
| 530 | + * |
|
| 531 | 531 | * @return int $quiz_grade total sum of all question grades |
| 532 | 532 | */ |
| 533 | 533 | public static function sensei_grade_quiz_auto( $quiz_id = 0, $submitted = array(), $total_questions = 0, $quiz_grade_type = 'auto' ) { |
| 534 | 534 | |
| 535 | - return Sensei_Grading::grade_quiz_auto( $quiz_id, $submitted, $total_questions, $quiz_grade_type ); |
|
| 535 | + return Sensei_Grading::grade_quiz_auto( $quiz_id, $submitted, $total_questions, $quiz_grade_type ); |
|
| 536 | 536 | |
| 537 | 537 | } // End sensei_grade_quiz_auto() |
| 538 | 538 | |
@@ -541,7 +541,7 @@ discard block |
||
| 541 | 541 | * @param integer $quiz_id ID of quiz |
| 542 | 542 | * @param integer $grade Grade received |
| 543 | 543 | * @param integer $user_id ID of user being graded |
| 544 | - * @param string $quiz_grade_type default 'auto' |
|
| 544 | + * @param string $quiz_grade_type default 'auto' |
|
| 545 | 545 | * @return boolean |
| 546 | 546 | */ |
| 547 | 547 | public static function sensei_grade_quiz( $quiz_id = 0, $grade = 0, $user_id = 0, $quiz_grade_type = 'auto' ) { |
@@ -565,21 +565,21 @@ discard block |
||
| 565 | 565 | |
| 566 | 566 | /** |
| 567 | 567 | * Grade question automatically |
| 568 | - * |
|
| 569 | - * This function checks the question typ and then grades it accordingly. |
|
| 570 | - * |
|
| 571 | - * @deprecated since 1.7.4 use WooThemes_Sensei_Grading::grade_question_auto instead |
|
| 572 | - * |
|
| 568 | + * |
|
| 569 | + * This function checks the question typ and then grades it accordingly. |
|
| 570 | + * |
|
| 571 | + * @deprecated since 1.7.4 use WooThemes_Sensei_Grading::grade_question_auto instead |
|
| 572 | + * |
|
| 573 | 573 | * @param integer $question_id |
| 574 | - * @param string $question_type of the standard Sensei question types |
|
| 574 | + * @param string $question_type of the standard Sensei question types |
|
| 575 | 575 | * @param string $answer |
| 576 | - * @param int $user_id |
|
| 577 | - * |
|
| 576 | + * @param int $user_id |
|
| 577 | + * |
|
| 578 | 578 | * @return int $question_grade |
| 579 | 579 | */ |
| 580 | 580 | public static function sensei_grade_question_auto( $question_id = 0, $question_type = '', $answer = '', $user_id = 0 ) { |
| 581 | 581 | |
| 582 | - return WooThemes_Sensei_Grading::grade_question_auto( $question_id, $question_type, $answer, $user_id ); |
|
| 582 | + return WooThemes_Sensei_Grading::grade_question_auto( $question_id, $question_type, $answer, $user_id ); |
|
| 583 | 583 | |
| 584 | 584 | } // end sensei_grade_question_auto |
| 585 | 585 | |
@@ -587,7 +587,7 @@ discard block |
||
| 587 | 587 | * Grade question |
| 588 | 588 | * @param integer $question_id ID of question |
| 589 | 589 | * @param integer $grade Grade received |
| 590 | - * @param int $user_id |
|
| 590 | + * @param int $user_id |
|
| 591 | 591 | * @return boolean |
| 592 | 592 | */ |
| 593 | 593 | public static function sensei_grade_question( $question_id = 0, $grade = 0, $user_id = 0 ) { |
@@ -626,35 +626,35 @@ discard block |
||
| 626 | 626 | } |
| 627 | 627 | |
| 628 | 628 | |
| 629 | - /** |
|
| 630 | - * Alias to Woothemes_Sensei_Utils::sensei_start_lesson |
|
| 631 | - * |
|
| 632 | - * @since 1.7.4 |
|
| 633 | - * |
|
| 634 | - * @param integer $user_id |
|
| 635 | - * @param integer $lesson_id |
|
| 636 | - * @param bool $complete |
|
| 637 | - * |
|
| 638 | - * @return mixed boolean or comment_ID |
|
| 639 | - */ |
|
| 640 | - public static function user_start_lesson( $user_id = 0, $lesson_id = 0, $complete = false ) { |
|
| 629 | + /** |
|
| 630 | + * Alias to Woothemes_Sensei_Utils::sensei_start_lesson |
|
| 631 | + * |
|
| 632 | + * @since 1.7.4 |
|
| 633 | + * |
|
| 634 | + * @param integer $user_id |
|
| 635 | + * @param integer $lesson_id |
|
| 636 | + * @param bool $complete |
|
| 637 | + * |
|
| 638 | + * @return mixed boolean or comment_ID |
|
| 639 | + */ |
|
| 640 | + public static function user_start_lesson( $user_id = 0, $lesson_id = 0, $complete = false ) { |
|
| 641 | 641 | |
| 642 | - return self::sensei_start_lesson( $lesson_id, $user_id, $complete ); |
|
| 642 | + return self::sensei_start_lesson( $lesson_id, $user_id, $complete ); |
|
| 643 | 643 | |
| 644 | - }// end user_start_lesson() |
|
| 644 | + }// end user_start_lesson() |
|
| 645 | 645 | |
| 646 | 646 | /** |
| 647 | 647 | * Mark a lesson as started for user |
| 648 | - * |
|
| 649 | - * Will also start the lesson course for the user if the user hans't started taking it already. |
|
| 650 | - * |
|
| 651 | - * @since 1.6.0 |
|
| 652 | - * |
|
| 648 | + * |
|
| 649 | + * Will also start the lesson course for the user if the user hans't started taking it already. |
|
| 650 | + * |
|
| 651 | + * @since 1.6.0 |
|
| 652 | + * |
|
| 653 | 653 | * @param integer $lesson_id ID of lesson |
| 654 | 654 | * @param int| string $user_id default 0 |
| 655 | - * @param bool $complete default false |
|
| 656 | - * |
|
| 657 | - * @return mixed boolean or comment_ID |
|
| 655 | + * @param bool $complete default false |
|
| 656 | + * |
|
| 657 | + * @return mixed boolean or comment_ID |
|
| 658 | 658 | */ |
| 659 | 659 | public static function sensei_start_lesson( $lesson_id = 0, $user_id = 0, $complete = false ) { |
| 660 | 660 | |
@@ -700,21 +700,21 @@ discard block |
||
| 700 | 700 | $metadata['start'] = current_time('mysql'); |
| 701 | 701 | $activity_logged = Sensei_Utils::update_lesson_status( $user_id, $lesson_id, $status, $metadata ); |
| 702 | 702 | |
| 703 | - } else { |
|
| 703 | + } else { |
|
| 704 | 704 | |
| 705 | - // if users is already taking the lesson and the status changes to complete update it |
|
| 706 | - $current_user_activity = get_comment($activity_logged); |
|
| 707 | - if( $status=='complete' && |
|
| 708 | - $status != $current_user_activity->comment_approved ){ |
|
| 705 | + // if users is already taking the lesson and the status changes to complete update it |
|
| 706 | + $current_user_activity = get_comment($activity_logged); |
|
| 707 | + if( $status=='complete' && |
|
| 708 | + $status != $current_user_activity->comment_approved ){ |
|
| 709 | 709 | |
| 710 | - $comment = array(); |
|
| 711 | - $comment['comment_ID'] = $activity_logged; |
|
| 712 | - $comment['comment_approved'] = $status; |
|
| 713 | - wp_update_comment( $comment ); |
|
| 710 | + $comment = array(); |
|
| 711 | + $comment['comment_ID'] = $activity_logged; |
|
| 712 | + $comment['comment_approved'] = $status; |
|
| 713 | + wp_update_comment( $comment ); |
|
| 714 | 714 | |
| 715 | - } |
|
| 715 | + } |
|
| 716 | 716 | |
| 717 | - } |
|
| 717 | + } |
|
| 718 | 718 | |
| 719 | 719 | if ( $complete ) { |
| 720 | 720 | // Run this *after* the lesson status has been created/updated |
@@ -863,7 +863,7 @@ discard block |
||
| 863 | 863 | /** |
| 864 | 864 | * Returns the answer_notes for a specific question and user, or sensei_user_answer entry |
| 865 | 865 | * |
| 866 | - * @deprecated since 1.7.5 use Sensei()->quiz->get_user_question_feedback instead |
|
| 866 | + * @deprecated since 1.7.5 use Sensei()->quiz->get_user_question_feedback instead |
|
| 867 | 867 | * @param mixed $question |
| 868 | 868 | * @param int $user_id |
| 869 | 869 | * @return string |
@@ -923,7 +923,7 @@ discard block |
||
| 923 | 923 | * Add answer notes to question |
| 924 | 924 | * @param integer $question_id ID of question |
| 925 | 925 | * @param integer $user_id ID of user |
| 926 | - * @param string $notes |
|
| 926 | + * @param string $notes |
|
| 927 | 927 | * @return boolean |
| 928 | 928 | */ |
| 929 | 929 | public static function sensei_add_answer_notes( $question_id = 0, $user_id = 0, $notes = '' ) { |
@@ -981,17 +981,17 @@ discard block |
||
| 981 | 981 | * @return void |
| 982 | 982 | */ |
| 983 | 983 | public static function sort_array_by_key( $array, $key ) { |
| 984 | - $sorter = array(); |
|
| 985 | - $ret = array(); |
|
| 986 | - reset( $array ); |
|
| 987 | - foreach ( $array as $ii => $va ) { |
|
| 988 | - $sorter[$ii] = $va[$key]; |
|
| 989 | - } // End For Loop |
|
| 990 | - asort( $sorter ); |
|
| 991 | - foreach ( $sorter as $ii => $va ) { |
|
| 992 | - $ret[$ii] = $array[$ii]; |
|
| 993 | - } // End For Loop |
|
| 994 | - $array = $ret; |
|
| 984 | + $sorter = array(); |
|
| 985 | + $ret = array(); |
|
| 986 | + reset( $array ); |
|
| 987 | + foreach ( $array as $ii => $va ) { |
|
| 988 | + $sorter[$ii] = $va[$key]; |
|
| 989 | + } // End For Loop |
|
| 990 | + asort( $sorter ); |
|
| 991 | + foreach ( $sorter as $ii => $va ) { |
|
| 992 | + $ret[$ii] = $array[$ii]; |
|
| 993 | + } // End For Loop |
|
| 994 | + $array = $ret; |
|
| 995 | 995 | } // End sort_array_by_key() |
| 996 | 996 | |
| 997 | 997 | /** |
@@ -1004,21 +1004,21 @@ discard block |
||
| 1004 | 1004 | $questions_array = array(); |
| 1005 | 1005 | if ( 0 < $quiz_id ) { |
| 1006 | 1006 | $question_args = array( 'post_type' => 'question', |
| 1007 | - 'posts_per_page' => -1, |
|
| 1008 | - 'orderby' => 'ID', |
|
| 1009 | - 'order' => 'ASC', |
|
| 1010 | - 'meta_query' => array( |
|
| 1007 | + 'posts_per_page' => -1, |
|
| 1008 | + 'orderby' => 'ID', |
|
| 1009 | + 'order' => 'ASC', |
|
| 1010 | + 'meta_query' => array( |
|
| 1011 | 1011 | array( |
| 1012 | 1012 | 'key' => '_quiz_id', |
| 1013 | 1013 | 'value' => $quiz_id, |
| 1014 | 1014 | ) |
| 1015 | 1015 | ), |
| 1016 | - 'post_status' => 'any', |
|
| 1017 | - 'suppress_filters' => 0 |
|
| 1018 | - ); |
|
| 1019 | - $questions_array = get_posts( $question_args ); |
|
| 1020 | - } // End If Statement |
|
| 1021 | - return $questions_array; |
|
| 1016 | + 'post_status' => 'any', |
|
| 1017 | + 'suppress_filters' => 0 |
|
| 1018 | + ); |
|
| 1019 | + $questions_array = get_posts( $question_args ); |
|
| 1020 | + } // End If Statement |
|
| 1021 | + return $questions_array; |
|
| 1022 | 1022 | } // End lesson_quiz_questions() |
| 1023 | 1023 | |
| 1024 | 1024 | /** |
@@ -1178,7 +1178,7 @@ discard block |
||
| 1178 | 1178 | * Set the status message displayed to the user for a quiz |
| 1179 | 1179 | * @param integer $lesson_id ID of quiz lesson |
| 1180 | 1180 | * @param integer $user_id ID of user |
| 1181 | - * @param bool $is_lesson |
|
| 1181 | + * @param bool $is_lesson |
|
| 1182 | 1182 | * @return array Status code and message |
| 1183 | 1183 | */ |
| 1184 | 1184 | public static function sensei_user_quiz_status_message( $lesson_id = 0, $user_id = 0, $is_lesson = false ) { |
@@ -1261,20 +1261,20 @@ discard block |
||
| 1261 | 1261 | } |
| 1262 | 1262 | } |
| 1263 | 1263 | |
| 1264 | - // add next lesson button |
|
| 1265 | - $nav_id_array = sensei_get_prev_next_lessons( $lesson_id ); |
|
| 1266 | - $next_lesson_id = absint( $nav_id_array['next_lesson'] ); |
|
| 1264 | + // add next lesson button |
|
| 1265 | + $nav_id_array = sensei_get_prev_next_lessons( $lesson_id ); |
|
| 1266 | + $next_lesson_id = absint( $nav_id_array['next_lesson'] ); |
|
| 1267 | 1267 | |
| 1268 | - // Output HTML |
|
| 1269 | - if ( ( 0 < $next_lesson_id ) ) { |
|
| 1270 | - $message .= ' ' . '<a class="next-lesson" href="' . esc_url( get_permalink( $next_lesson_id ) ) |
|
| 1271 | - . '" rel="next"><span class="meta-nav"></span>'. __( 'Next Lesson' ,'woothemes-sensei') |
|
| 1272 | - .'</a>'; |
|
| 1268 | + // Output HTML |
|
| 1269 | + if ( ( 0 < $next_lesson_id ) ) { |
|
| 1270 | + $message .= ' ' . '<a class="next-lesson" href="' . esc_url( get_permalink( $next_lesson_id ) ) |
|
| 1271 | + . '" rel="next"><span class="meta-nav"></span>'. __( 'Next Lesson' ,'woothemes-sensei') |
|
| 1272 | + .'</a>'; |
|
| 1273 | 1273 | |
| 1274 | - } |
|
| 1274 | + } |
|
| 1275 | 1275 | |
| 1276 | 1276 | } |
| 1277 | - // Lesson/Quiz not complete |
|
| 1277 | + // Lesson/Quiz not complete |
|
| 1278 | 1278 | else { |
| 1279 | 1279 | // Lesson/Quiz isn't "complete" instead it's ungraded (previously this "state" meant that it *was* complete) |
| 1280 | 1280 | if ( isset( $user_lesson_status->comment_approved ) && 'ungraded' == $user_lesson_status->comment_approved ) { |
@@ -1332,7 +1332,7 @@ discard block |
||
| 1332 | 1332 | $message = apply_filters( 'sensei_user_quiz_status_' . $status, $message ); |
| 1333 | 1333 | |
| 1334 | 1334 | if( $is_lesson && ! in_array( $status, array( 'login_required', 'not_started_course' ) ) ) { |
| 1335 | - $quiz_id = Sensei()->lesson->lesson_quizzes( $lesson_id ); |
|
| 1335 | + $quiz_id = Sensei()->lesson->lesson_quizzes( $lesson_id ); |
|
| 1336 | 1336 | $extra = '<p><a class="button" href="' . esc_url( get_permalink( $quiz_id ) ) . '" title="' . __( 'View the lesson quiz', 'woothemes-sensei' ) . '">' . __( 'View the lesson quiz', 'woothemes-sensei' ) . '</a></p>'; |
| 1337 | 1337 | } |
| 1338 | 1338 | |
@@ -1393,18 +1393,18 @@ discard block |
||
| 1393 | 1393 | $user_id = get_current_user_id(); |
| 1394 | 1394 | } |
| 1395 | 1395 | |
| 1396 | - if ( ! $user_id > 0 ) { |
|
| 1396 | + if ( ! $user_id > 0 ) { |
|
| 1397 | 1397 | |
| 1398 | - $user_started_course = false; |
|
| 1398 | + $user_started_course = false; |
|
| 1399 | 1399 | |
| 1400 | - } else { |
|
| 1400 | + } else { |
|
| 1401 | 1401 | |
| 1402 | - $activity_args = array( |
|
| 1403 | - 'post_id' => $course_id, |
|
| 1404 | - 'user_id' => $user_id, |
|
| 1405 | - 'type' => 'sensei_course_status', |
|
| 1406 | - 'field' => 'comment_ID' |
|
| 1407 | - ); |
|
| 1402 | + $activity_args = array( |
|
| 1403 | + 'post_id' => $course_id, |
|
| 1404 | + 'user_id' => $user_id, |
|
| 1405 | + 'type' => 'sensei_course_status', |
|
| 1406 | + 'field' => 'comment_ID' |
|
| 1407 | + ); |
|
| 1408 | 1408 | |
| 1409 | 1409 | $user_course_status_id = Sensei_Utils::sensei_get_activity_value( $activity_args ); |
| 1410 | 1410 | |
@@ -1413,7 +1413,7 @@ discard block |
||
| 1413 | 1413 | $user_started_course = $user_course_status_id; |
| 1414 | 1414 | |
| 1415 | 1415 | } |
| 1416 | - } |
|
| 1416 | + } |
|
| 1417 | 1417 | } |
| 1418 | 1418 | |
| 1419 | 1419 | /** |
@@ -1542,7 +1542,7 @@ discard block |
||
| 1542 | 1542 | * Check if a user has completed a course or not |
| 1543 | 1543 | * |
| 1544 | 1544 | * @param int | WP_Post | WP_Comment $course course_id or sensei_course_status entry |
| 1545 | - * |
|
| 1545 | + * |
|
| 1546 | 1546 | * @param int $user_id |
| 1547 | 1547 | * @return boolean |
| 1548 | 1548 | */ |
@@ -1569,14 +1569,14 @@ discard block |
||
| 1569 | 1569 | } |
| 1570 | 1570 | } |
| 1571 | 1571 | |
| 1572 | - if( is_a( $course, 'WP_Post' ) ){ |
|
| 1573 | - $course = $course->ID; |
|
| 1574 | - } |
|
| 1572 | + if( is_a( $course, 'WP_Post' ) ){ |
|
| 1573 | + $course = $course->ID; |
|
| 1574 | + } |
|
| 1575 | 1575 | |
| 1576 | 1576 | $user_course_status = Sensei_Utils::user_course_status( $course , $user_id ); |
| 1577 | 1577 | if( isset( $user_course_status->comment_approved ) ){ |
| 1578 | - $user_course_status = $user_course_status->comment_approved; |
|
| 1579 | - } |
|
| 1578 | + $user_course_status = $user_course_status->comment_approved; |
|
| 1579 | + } |
|
| 1580 | 1580 | |
| 1581 | 1581 | } |
| 1582 | 1582 | if( $user_course_status && 'complete' == $user_course_status ) { |
@@ -1601,11 +1601,11 @@ discard block |
||
| 1601 | 1601 | $user_id = get_current_user_id(); |
| 1602 | 1602 | } |
| 1603 | 1603 | |
| 1604 | - $activity_args = array( |
|
| 1605 | - 'post_id' => $lesson_id, |
|
| 1606 | - 'user_id' => $user_id, |
|
| 1607 | - 'type' => 'sensei_lesson_status', |
|
| 1608 | - 'field' => 'comment_ID' ); |
|
| 1604 | + $activity_args = array( |
|
| 1605 | + 'post_id' => $lesson_id, |
|
| 1606 | + 'user_id' => $user_id, |
|
| 1607 | + 'type' => 'sensei_lesson_status', |
|
| 1608 | + 'field' => 'comment_ID' ); |
|
| 1609 | 1609 | |
| 1610 | 1610 | $user_lesson_status_id = Sensei_Utils::sensei_get_activity_value( $activity_args ); |
| 1611 | 1611 | if( $user_lesson_status_id ) { |
@@ -1618,7 +1618,7 @@ discard block |
||
| 1618 | 1618 | /** |
| 1619 | 1619 | * Check if a user has completed a lesson or not |
| 1620 | 1620 | * |
| 1621 | - * @uses Sensei() |
|
| 1621 | + * @uses Sensei() |
|
| 1622 | 1622 | * @param mixed $lesson lesson_id or sensei_lesson_status entry |
| 1623 | 1623 | * @param int $user_id |
| 1624 | 1624 | * @return boolean |
@@ -1639,10 +1639,10 @@ discard block |
||
| 1639 | 1639 | $user_id = get_current_user_id(); |
| 1640 | 1640 | } |
| 1641 | 1641 | |
| 1642 | - // the user is not logged in |
|
| 1643 | - if( ! $user_id > 0 ){ |
|
| 1644 | - return false; |
|
| 1645 | - } |
|
| 1642 | + // the user is not logged in |
|
| 1643 | + if( ! $user_id > 0 ){ |
|
| 1644 | + return false; |
|
| 1645 | + } |
|
| 1646 | 1646 | $_user_lesson_status = Sensei_Utils::user_lesson_status( $lesson, $user_id ); |
| 1647 | 1647 | |
| 1648 | 1648 | if ( $_user_lesson_status ) { |
@@ -1721,9 +1721,9 @@ discard block |
||
| 1721 | 1721 | */ |
| 1722 | 1722 | public static function user_lesson_status( $lesson_id = 0, $user_id = 0 ) { |
| 1723 | 1723 | |
| 1724 | - if( ! $user_id ) { |
|
| 1725 | - $user_id = get_current_user_id(); |
|
| 1726 | - } |
|
| 1724 | + if( ! $user_id ) { |
|
| 1725 | + $user_id = get_current_user_id(); |
|
| 1726 | + } |
|
| 1727 | 1727 | |
| 1728 | 1728 | if( $lesson_id > 0 && $user_id > 0 ) { |
| 1729 | 1729 | |
@@ -1774,12 +1774,12 @@ discard block |
||
| 1774 | 1774 | * Sets the status for the lesson |
| 1775 | 1775 | * |
| 1776 | 1776 | * @since 1.7.0 |
| 1777 | - * |
|
| 1777 | + * |
|
| 1778 | 1778 | * @param int|string $user_id |
| 1779 | 1779 | * @param int|string $lesson_id |
| 1780 | 1780 | * @param string $status |
| 1781 | 1781 | * @param array $metadata |
| 1782 | - * |
|
| 1782 | + * |
|
| 1783 | 1783 | * @return mixed false or comment_ID |
| 1784 | 1784 | */ |
| 1785 | 1785 | public static function update_lesson_status( $user_id, $lesson_id, $status = 'in-progress', $metadata = array() ) { |
@@ -1895,7 +1895,7 @@ discard block |
||
| 1895 | 1895 | /** |
| 1896 | 1896 | * Adjust the comment query to be faster on the database, used by Analysis admin |
| 1897 | 1897 | * @since 1.7.0 |
| 1898 | - * @param array $pieces |
|
| 1898 | + * @param array $pieces |
|
| 1899 | 1899 | * @return array $pieces |
| 1900 | 1900 | */ |
| 1901 | 1901 | public static function comment_total_sum_meta_value_filter( $pieces ) { |
@@ -1930,360 +1930,360 @@ discard block |
||
| 1930 | 1930 | return $pieces; |
| 1931 | 1931 | } |
| 1932 | 1932 | |
| 1933 | - /** |
|
| 1934 | - * |
|
| 1935 | - * Alias to Woothemes_Sensei_Utils::update_user_data |
|
| 1936 | - * @since 1.7.4 |
|
| 1937 | - * |
|
| 1938 | - * @param string $data_key maximum 39 characters allowed |
|
| 1939 | - * @param int $post_id |
|
| 1940 | - * @param mixed $value |
|
| 1941 | - * @param int $user_id |
|
| 1942 | - * |
|
| 1943 | - * @return bool $success |
|
| 1944 | - */ |
|
| 1945 | - public static function add_user_data( $data_key, $post_id , $value = '' , $user_id = 0 ){ |
|
| 1946 | - |
|
| 1947 | - return self::update_user_data( $data_key, $post_id, $value , $user_id ); |
|
| 1948 | - |
|
| 1949 | - }// end add_user_data |
|
| 1950 | - |
|
| 1951 | - /** |
|
| 1952 | - * add user specific data to the passed in sensei post type id |
|
| 1953 | - * |
|
| 1954 | - * This function saves comment meta on the users current status. If no status is available |
|
| 1955 | - * status will be created. It only operates on the available sensei Post types: course, lesson, quiz. |
|
| 1956 | - * |
|
| 1957 | - * @since 1.7.4 |
|
| 1958 | - * |
|
| 1959 | - * @param string $data_key maximum 39 characters allowed |
|
| 1960 | - * @param int $post_id |
|
| 1961 | - * @param mixed $value |
|
| 1962 | - * @param int $user_id |
|
| 1963 | - * |
|
| 1964 | - * @return bool $success |
|
| 1965 | - */ |
|
| 1966 | - public static function update_user_data( $data_key, $post_id, $value = '' , $user_id = 0 ){ |
|
| 1967 | - |
|
| 1968 | - if( ! ( $user_id > 0 ) ){ |
|
| 1969 | - $user_id = get_current_user_id(); |
|
| 1970 | - } |
|
| 1971 | - |
|
| 1972 | - $supported_post_types = array( 'course', 'lesson' ); |
|
| 1973 | - $post_type = get_post_type( $post_id ); |
|
| 1974 | - if( empty( $post_id ) || empty( $data_key ) |
|
| 1975 | - || ! is_int( $post_id ) || ! ( intval( $post_id ) > 0 ) || ! ( intval( $user_id ) > 0 ) |
|
| 1976 | - || !get_userdata( $user_id ) |
|
| 1977 | - || ! in_array( $post_type, $supported_post_types ) ){ |
|
| 1978 | - |
|
| 1979 | - return false; |
|
| 1980 | - } |
|
| 1981 | - |
|
| 1982 | - // check if there and existing Sensei status on this post type if not create it |
|
| 1983 | - // and get the activity ID |
|
| 1984 | - $status_function = 'user_'.$post_type.'_status'; |
|
| 1985 | - $sensei_user_status = self::$status_function( $post_id ,$user_id ); |
|
| 1986 | - if( ! isset( $sensei_user_status->comment_ID ) ){ |
|
| 1987 | - |
|
| 1988 | - $start_function = 'user_start_'.$post_type; |
|
| 1989 | - $sensei_user_activity_id = self::$start_function( $user_id, $post_id ); |
|
| 1990 | - |
|
| 1991 | - }else{ |
|
| 1992 | - |
|
| 1993 | - $sensei_user_activity_id = $sensei_user_status->comment_ID; |
|
| 1994 | - |
|
| 1995 | - } |
|
| 1996 | - |
|
| 1997 | - // store the data |
|
| 1998 | - $success = update_comment_meta( $sensei_user_activity_id, $data_key, $value ); |
|
| 1999 | - |
|
| 2000 | - return $success; |
|
| 2001 | - |
|
| 2002 | - }//update_user_data |
|
| 2003 | - |
|
| 2004 | - /** |
|
| 2005 | - * Get the user data stored on the passed in post type |
|
| 2006 | - * |
|
| 2007 | - * This function gets the comment meta on the lesson or course status |
|
| 2008 | - * |
|
| 2009 | - * @since 1.7.4 |
|
| 2010 | - * |
|
| 2011 | - * @param $data_key |
|
| 2012 | - * @param $post_id |
|
| 2013 | - * @param int $user_id |
|
| 2014 | - * |
|
| 2015 | - * @return mixed $user_data_value |
|
| 2016 | - */ |
|
| 2017 | - public static function get_user_data( $data_key, $post_id, $user_id = 0 ){ |
|
| 2018 | - |
|
| 2019 | - $user_data_value = true; |
|
| 2020 | - |
|
| 2021 | - if( ! ( $user_id > 0 ) ){ |
|
| 2022 | - $user_id = get_current_user_id(); |
|
| 2023 | - } |
|
| 2024 | - |
|
| 2025 | - $supported_post_types = array( 'course', 'lesson' ); |
|
| 2026 | - $post_type = get_post_type( $post_id ); |
|
| 2027 | - if( empty( $post_id ) || empty( $data_key ) |
|
| 2028 | - || ! ( intval( $post_id ) > 0 ) || ! ( intval( $user_id ) > 0 ) |
|
| 2029 | - || ! get_userdata( $user_id ) |
|
| 2030 | - || !in_array( $post_type, $supported_post_types ) ){ |
|
| 2031 | - |
|
| 2032 | - return false; |
|
| 2033 | - } |
|
| 2034 | - |
|
| 2035 | - // check if there and existing Sensei status on this post type if not create it |
|
| 2036 | - // and get the activity ID |
|
| 2037 | - $status_function = 'user_'.$post_type.'_status'; |
|
| 2038 | - $sensei_user_status = self::$status_function( $post_id ,$user_id ); |
|
| 2039 | - if( ! isset( $sensei_user_status->comment_ID ) ){ |
|
| 2040 | - return false; |
|
| 2041 | - } |
|
| 2042 | - |
|
| 2043 | - $sensei_user_activity_id = $sensei_user_status->comment_ID; |
|
| 2044 | - $user_data_value = get_comment_meta( $sensei_user_activity_id , $data_key, true ); |
|
| 2045 | - |
|
| 2046 | - return $user_data_value; |
|
| 2047 | - |
|
| 2048 | - }// end get_user_data |
|
| 2049 | - |
|
| 2050 | - /** |
|
| 2051 | - * Delete the Sensei user data for the given key, Sensei post type and user combination. |
|
| 2052 | - * |
|
| 2053 | - * @param int $data_key |
|
| 2054 | - * @param int $post_id |
|
| 2055 | - * @param int $user_id |
|
| 2056 | - * |
|
| 2057 | - * @return bool $deleted |
|
| 2058 | - */ |
|
| 2059 | - public static function delete_user_data( $data_key, $post_id , $user_id ){ |
|
| 2060 | - $deleted = true; |
|
| 1933 | + /** |
|
| 1934 | + * |
|
| 1935 | + * Alias to Woothemes_Sensei_Utils::update_user_data |
|
| 1936 | + * @since 1.7.4 |
|
| 1937 | + * |
|
| 1938 | + * @param string $data_key maximum 39 characters allowed |
|
| 1939 | + * @param int $post_id |
|
| 1940 | + * @param mixed $value |
|
| 1941 | + * @param int $user_id |
|
| 1942 | + * |
|
| 1943 | + * @return bool $success |
|
| 1944 | + */ |
|
| 1945 | + public static function add_user_data( $data_key, $post_id , $value = '' , $user_id = 0 ){ |
|
| 1946 | + |
|
| 1947 | + return self::update_user_data( $data_key, $post_id, $value , $user_id ); |
|
| 1948 | + |
|
| 1949 | + }// end add_user_data |
|
| 1950 | + |
|
| 1951 | + /** |
|
| 1952 | + * add user specific data to the passed in sensei post type id |
|
| 1953 | + * |
|
| 1954 | + * This function saves comment meta on the users current status. If no status is available |
|
| 1955 | + * status will be created. It only operates on the available sensei Post types: course, lesson, quiz. |
|
| 1956 | + * |
|
| 1957 | + * @since 1.7.4 |
|
| 1958 | + * |
|
| 1959 | + * @param string $data_key maximum 39 characters allowed |
|
| 1960 | + * @param int $post_id |
|
| 1961 | + * @param mixed $value |
|
| 1962 | + * @param int $user_id |
|
| 1963 | + * |
|
| 1964 | + * @return bool $success |
|
| 1965 | + */ |
|
| 1966 | + public static function update_user_data( $data_key, $post_id, $value = '' , $user_id = 0 ){ |
|
| 1967 | + |
|
| 1968 | + if( ! ( $user_id > 0 ) ){ |
|
| 1969 | + $user_id = get_current_user_id(); |
|
| 1970 | + } |
|
| 1971 | + |
|
| 1972 | + $supported_post_types = array( 'course', 'lesson' ); |
|
| 1973 | + $post_type = get_post_type( $post_id ); |
|
| 1974 | + if( empty( $post_id ) || empty( $data_key ) |
|
| 1975 | + || ! is_int( $post_id ) || ! ( intval( $post_id ) > 0 ) || ! ( intval( $user_id ) > 0 ) |
|
| 1976 | + || !get_userdata( $user_id ) |
|
| 1977 | + || ! in_array( $post_type, $supported_post_types ) ){ |
|
| 2061 | 1978 | |
| 2062 | - if( ! ( $user_id > 0 ) ){ |
|
| 2063 | - $user_id = get_current_user_id(); |
|
| 2064 | - } |
|
| 1979 | + return false; |
|
| 1980 | + } |
|
| 2065 | 1981 | |
| 2066 | - $supported_post_types = array( 'course', 'lesson' ); |
|
| 2067 | - $post_type = get_post_type( $post_id ); |
|
| 2068 | - if( empty( $post_id ) || empty( $data_key ) |
|
| 2069 | - || ! is_int( $post_id ) || ! ( intval( $post_id ) > 0 ) || ! ( intval( $user_id ) > 0 ) |
|
| 2070 | - || ! get_userdata( $user_id ) |
|
| 2071 | - || !in_array( $post_type, $supported_post_types ) ){ |
|
| 1982 | + // check if there and existing Sensei status on this post type if not create it |
|
| 1983 | + // and get the activity ID |
|
| 1984 | + $status_function = 'user_'.$post_type.'_status'; |
|
| 1985 | + $sensei_user_status = self::$status_function( $post_id ,$user_id ); |
|
| 1986 | + if( ! isset( $sensei_user_status->comment_ID ) ){ |
|
| 2072 | 1987 | |
| 2073 | - return false; |
|
| 2074 | - } |
|
| 1988 | + $start_function = 'user_start_'.$post_type; |
|
| 1989 | + $sensei_user_activity_id = self::$start_function( $user_id, $post_id ); |
|
| 2075 | 1990 | |
| 2076 | - // check if there and existing Sensei status on this post type if not create it |
|
| 2077 | - // and get the activity ID |
|
| 2078 | - $status_function = 'user_'.$post_type.'_status'; |
|
| 2079 | - $sensei_user_status = self::$status_function( $post_id ,$user_id ); |
|
| 2080 | - if( ! isset( $sensei_user_status->comment_ID ) ){ |
|
| 2081 | - return false; |
|
| 2082 | - } |
|
| 1991 | + }else{ |
|
| 2083 | 1992 | |
| 2084 | - $sensei_user_activity_id = $sensei_user_status->comment_ID; |
|
| 2085 | - $deleted = delete_comment_meta( $sensei_user_activity_id , $data_key ); |
|
| 1993 | + $sensei_user_activity_id = $sensei_user_status->comment_ID; |
|
| 2086 | 1994 | |
| 2087 | - return $deleted; |
|
| 1995 | + } |
|
| 2088 | 1996 | |
| 2089 | - }// end delete_user_data |
|
| 1997 | + // store the data |
|
| 1998 | + $success = update_comment_meta( $sensei_user_activity_id, $data_key, $value ); |
|
| 2090 | 1999 | |
| 2000 | + return $success; |
|
| 2091 | 2001 | |
| 2092 | - /** |
|
| 2093 | - * The function creates a drop down. Never write up a Sensei select statement again. |
|
| 2094 | - * |
|
| 2095 | - * @since 1.8.0 |
|
| 2096 | - * |
|
| 2097 | - * @param string $selected_value |
|
| 2098 | - * @param $options{ |
|
| 2099 | - * @type string $value the value saved in the database |
|
| 2100 | - * @type string $option what the user will see in the list of items |
|
| 2101 | - * } |
|
| 2102 | - * @param array $attributes{ |
|
| 2103 | - * @type string $attribute type such name or id etc. |
|
| 2104 | - * @type string $value |
|
| 2105 | - * } |
|
| 2106 | - * @param bool $enable_none_option |
|
| 2107 | - * |
|
| 2108 | - * @return string $drop_down_element |
|
| 2109 | - */ |
|
| 2110 | - public static function generate_drop_down( $selected_value, $options = array() , $attributes = array(), $enable_none_option = true ) { |
|
| 2002 | + }//update_user_data |
|
| 2111 | 2003 | |
| 2112 | - $drop_down_element = ''; |
|
| 2004 | + /** |
|
| 2005 | + * Get the user data stored on the passed in post type |
|
| 2006 | + * |
|
| 2007 | + * This function gets the comment meta on the lesson or course status |
|
| 2008 | + * |
|
| 2009 | + * @since 1.7.4 |
|
| 2010 | + * |
|
| 2011 | + * @param $data_key |
|
| 2012 | + * @param $post_id |
|
| 2013 | + * @param int $user_id |
|
| 2014 | + * |
|
| 2015 | + * @return mixed $user_data_value |
|
| 2016 | + */ |
|
| 2017 | + public static function get_user_data( $data_key, $post_id, $user_id = 0 ){ |
|
| 2113 | 2018 | |
| 2114 | - // setup the basic attributes |
|
| 2115 | - if( !isset( $attributes['name'] ) || empty( $attributes['name'] ) ) { |
|
| 2019 | + $user_data_value = true; |
|
| 2116 | 2020 | |
| 2117 | - $attributes['name'] = 'sensei-options'; |
|
| 2021 | + if( ! ( $user_id > 0 ) ){ |
|
| 2022 | + $user_id = get_current_user_id(); |
|
| 2023 | + } |
|
| 2118 | 2024 | |
| 2119 | - } |
|
| 2025 | + $supported_post_types = array( 'course', 'lesson' ); |
|
| 2026 | + $post_type = get_post_type( $post_id ); |
|
| 2027 | + if( empty( $post_id ) || empty( $data_key ) |
|
| 2028 | + || ! ( intval( $post_id ) > 0 ) || ! ( intval( $user_id ) > 0 ) |
|
| 2029 | + || ! get_userdata( $user_id ) |
|
| 2030 | + || !in_array( $post_type, $supported_post_types ) ){ |
|
| 2120 | 2031 | |
| 2121 | - if( !isset( $attributes['id'] ) || empty( $attributes['id'] ) ) { |
|
| 2032 | + return false; |
|
| 2033 | + } |
|
| 2122 | 2034 | |
| 2123 | - $attributes['id'] = 'sensei-options'; |
|
| 2035 | + // check if there and existing Sensei status on this post type if not create it |
|
| 2036 | + // and get the activity ID |
|
| 2037 | + $status_function = 'user_'.$post_type.'_status'; |
|
| 2038 | + $sensei_user_status = self::$status_function( $post_id ,$user_id ); |
|
| 2039 | + if( ! isset( $sensei_user_status->comment_ID ) ){ |
|
| 2040 | + return false; |
|
| 2041 | + } |
|
| 2124 | 2042 | |
| 2125 | - } |
|
| 2043 | + $sensei_user_activity_id = $sensei_user_status->comment_ID; |
|
| 2044 | + $user_data_value = get_comment_meta( $sensei_user_activity_id , $data_key, true ); |
|
| 2126 | 2045 | |
| 2127 | - if( !isset( $attributes['class'] ) || empty( $attributes['class'] ) ) { |
|
| 2046 | + return $user_data_value; |
|
| 2128 | 2047 | |
| 2129 | - $attributes['class'] ='chosen_select widefat'; |
|
| 2048 | + }// end get_user_data |
|
| 2130 | 2049 | |
| 2131 | - } |
|
| 2050 | + /** |
|
| 2051 | + * Delete the Sensei user data for the given key, Sensei post type and user combination. |
|
| 2052 | + * |
|
| 2053 | + * @param int $data_key |
|
| 2054 | + * @param int $post_id |
|
| 2055 | + * @param int $user_id |
|
| 2056 | + * |
|
| 2057 | + * @return bool $deleted |
|
| 2058 | + */ |
|
| 2059 | + public static function delete_user_data( $data_key, $post_id , $user_id ){ |
|
| 2060 | + $deleted = true; |
|
| 2132 | 2061 | |
| 2133 | - // create element attributes |
|
| 2134 | - $combined_attributes = ''; |
|
| 2135 | - foreach( $attributes as $attribute => $value ){ |
|
| 2062 | + if( ! ( $user_id > 0 ) ){ |
|
| 2063 | + $user_id = get_current_user_id(); |
|
| 2064 | + } |
|
| 2136 | 2065 | |
| 2137 | - $combined_attributes .= $attribute . '="'.$value.'"' . ' '; |
|
| 2066 | + $supported_post_types = array( 'course', 'lesson' ); |
|
| 2067 | + $post_type = get_post_type( $post_id ); |
|
| 2068 | + if( empty( $post_id ) || empty( $data_key ) |
|
| 2069 | + || ! is_int( $post_id ) || ! ( intval( $post_id ) > 0 ) || ! ( intval( $user_id ) > 0 ) |
|
| 2070 | + || ! get_userdata( $user_id ) |
|
| 2071 | + || !in_array( $post_type, $supported_post_types ) ){ |
|
| 2138 | 2072 | |
| 2139 | - }// end for each |
|
| 2073 | + return false; |
|
| 2074 | + } |
|
| 2140 | 2075 | |
| 2076 | + // check if there and existing Sensei status on this post type if not create it |
|
| 2077 | + // and get the activity ID |
|
| 2078 | + $status_function = 'user_'.$post_type.'_status'; |
|
| 2079 | + $sensei_user_status = self::$status_function( $post_id ,$user_id ); |
|
| 2080 | + if( ! isset( $sensei_user_status->comment_ID ) ){ |
|
| 2081 | + return false; |
|
| 2082 | + } |
|
| 2141 | 2083 | |
| 2142 | - // create the select element |
|
| 2143 | - $drop_down_element .= '<select '. $combined_attributes . ' >' . "\n"; |
|
| 2084 | + $sensei_user_activity_id = $sensei_user_status->comment_ID; |
|
| 2085 | + $deleted = delete_comment_meta( $sensei_user_activity_id , $data_key ); |
|
| 2144 | 2086 | |
| 2145 | - // show the none option if the client requested |
|
| 2146 | - if( $enable_none_option ) { |
|
| 2147 | - $drop_down_element .= '<option value="">' . __('None', 'woothemes-sensei') . '</option>'; |
|
| 2148 | - } |
|
| 2087 | + return $deleted; |
|
| 2149 | 2088 | |
| 2150 | - if ( count( $options ) > 0 ) { |
|
| 2089 | + }// end delete_user_data |
|
| 2151 | 2090 | |
| 2152 | - foreach ($options as $value => $option ){ |
|
| 2153 | 2091 | |
| 2154 | - $element = ''; |
|
| 2155 | - $element.= '<option value="' . esc_attr( $value ) . '"'; |
|
| 2156 | - $element .= selected( $value, $selected_value, false ) . '>'; |
|
| 2157 | - $element .= esc_html( $option ) . '</option>' . "\n"; |
|
| 2092 | + /** |
|
| 2093 | + * The function creates a drop down. Never write up a Sensei select statement again. |
|
| 2094 | + * |
|
| 2095 | + * @since 1.8.0 |
|
| 2096 | + * |
|
| 2097 | + * @param string $selected_value |
|
| 2098 | + * @param $options{ |
|
| 2099 | + * @type string $value the value saved in the database |
|
| 2100 | + * @type string $option what the user will see in the list of items |
|
| 2101 | + * } |
|
| 2102 | + * @param array $attributes{ |
|
| 2103 | + * @type string $attribute type such name or id etc. |
|
| 2104 | + * @type string $value |
|
| 2105 | + * } |
|
| 2106 | + * @param bool $enable_none_option |
|
| 2107 | + * |
|
| 2108 | + * @return string $drop_down_element |
|
| 2109 | + */ |
|
| 2110 | + public static function generate_drop_down( $selected_value, $options = array() , $attributes = array(), $enable_none_option = true ) { |
|
| 2158 | 2111 | |
| 2159 | - // add the element to the select html |
|
| 2160 | - $drop_down_element.= $element; |
|
| 2161 | - } // End For Loop |
|
| 2112 | + $drop_down_element = ''; |
|
| 2162 | 2113 | |
| 2163 | - } // End If Statement |
|
| 2114 | + // setup the basic attributes |
|
| 2115 | + if( !isset( $attributes['name'] ) || empty( $attributes['name'] ) ) { |
|
| 2164 | 2116 | |
| 2165 | - $drop_down_element .= '</select>' . "\n"; |
|
| 2117 | + $attributes['name'] = 'sensei-options'; |
|
| 2166 | 2118 | |
| 2167 | - return $drop_down_element; |
|
| 2119 | + } |
|
| 2168 | 2120 | |
| 2169 | - }// generate_drop_down |
|
| 2121 | + if( !isset( $attributes['id'] ) || empty( $attributes['id'] ) ) { |
|
| 2170 | 2122 | |
| 2171 | - /** |
|
| 2172 | - * Wrapper for the default php round() function. |
|
| 2173 | - * This allows us to give more control to a user on how they can round Sensei |
|
| 2174 | - * decimals passed through this function. |
|
| 2175 | - * |
|
| 2176 | - * @since 1.8.5 |
|
| 2177 | - * |
|
| 2178 | - * @param double $val |
|
| 2179 | - * @param int $precision |
|
| 2180 | - * @param $mode |
|
| 2181 | - * @param string $context |
|
| 2182 | - * |
|
| 2183 | - * @return double $val |
|
| 2184 | - */ |
|
| 2185 | - public static function round( $val, $precision = 0, $mode = PHP_ROUND_HALF_UP, $context = '' ){ |
|
| 2123 | + $attributes['id'] = 'sensei-options'; |
|
| 2186 | 2124 | |
| 2187 | - /**å |
|
| 2125 | + } |
|
| 2126 | + |
|
| 2127 | + if( !isset( $attributes['class'] ) || empty( $attributes['class'] ) ) { |
|
| 2128 | + |
|
| 2129 | + $attributes['class'] ='chosen_select widefat'; |
|
| 2130 | + |
|
| 2131 | + } |
|
| 2132 | + |
|
| 2133 | + // create element attributes |
|
| 2134 | + $combined_attributes = ''; |
|
| 2135 | + foreach( $attributes as $attribute => $value ){ |
|
| 2136 | + |
|
| 2137 | + $combined_attributes .= $attribute . '="'.$value.'"' . ' '; |
|
| 2138 | + |
|
| 2139 | + }// end for each |
|
| 2140 | + |
|
| 2141 | + |
|
| 2142 | + // create the select element |
|
| 2143 | + $drop_down_element .= '<select '. $combined_attributes . ' >' . "\n"; |
|
| 2144 | + |
|
| 2145 | + // show the none option if the client requested |
|
| 2146 | + if( $enable_none_option ) { |
|
| 2147 | + $drop_down_element .= '<option value="">' . __('None', 'woothemes-sensei') . '</option>'; |
|
| 2148 | + } |
|
| 2149 | + |
|
| 2150 | + if ( count( $options ) > 0 ) { |
|
| 2151 | + |
|
| 2152 | + foreach ($options as $value => $option ){ |
|
| 2153 | + |
|
| 2154 | + $element = ''; |
|
| 2155 | + $element.= '<option value="' . esc_attr( $value ) . '"'; |
|
| 2156 | + $element .= selected( $value, $selected_value, false ) . '>'; |
|
| 2157 | + $element .= esc_html( $option ) . '</option>' . "\n"; |
|
| 2158 | + |
|
| 2159 | + // add the element to the select html |
|
| 2160 | + $drop_down_element.= $element; |
|
| 2161 | + } // End For Loop |
|
| 2162 | + |
|
| 2163 | + } // End If Statement |
|
| 2164 | + |
|
| 2165 | + $drop_down_element .= '</select>' . "\n"; |
|
| 2166 | + |
|
| 2167 | + return $drop_down_element; |
|
| 2168 | + |
|
| 2169 | + }// generate_drop_down |
|
| 2170 | + |
|
| 2171 | + /** |
|
| 2172 | + * Wrapper for the default php round() function. |
|
| 2173 | + * This allows us to give more control to a user on how they can round Sensei |
|
| 2174 | + * decimals passed through this function. |
|
| 2175 | + * |
|
| 2176 | + * @since 1.8.5 |
|
| 2177 | + * |
|
| 2178 | + * @param double $val |
|
| 2179 | + * @param int $precision |
|
| 2180 | + * @param $mode |
|
| 2181 | + * @param string $context |
|
| 2182 | + * |
|
| 2183 | + * @return double $val |
|
| 2184 | + */ |
|
| 2185 | + public static function round( $val, $precision = 0, $mode = PHP_ROUND_HALF_UP, $context = '' ){ |
|
| 2186 | + |
|
| 2187 | + /**å |
|
| 2188 | 2188 | * Change the precision for the Sensei_Utils::round function. |
| 2189 | 2189 | * the precision given will be passed into the php round function |
| 2190 | 2190 | * @since 1.8.5 |
| 2191 | 2191 | */ |
| 2192 | - $precision = apply_filters( 'sensei_round_precision', $precision , $val, $context, $mode ); |
|
| 2193 | - |
|
| 2194 | - /** |
|
| 2195 | - * Change the mode for the Sensei_Utils::round function. |
|
| 2196 | - * the mode given will be passed into the php round function |
|
| 2197 | - * |
|
| 2198 | - * This applies only to PHP version 5.3.0 and greater |
|
| 2199 | - * |
|
| 2200 | - * @since 1.8.5 |
|
| 2201 | - */ |
|
| 2202 | - $mode = apply_filters( 'sensei_round_mode', $mode , $val, $context, $precision ); |
|
| 2192 | + $precision = apply_filters( 'sensei_round_precision', $precision , $val, $context, $mode ); |
|
| 2193 | + |
|
| 2194 | + /** |
|
| 2195 | + * Change the mode for the Sensei_Utils::round function. |
|
| 2196 | + * the mode given will be passed into the php round function |
|
| 2197 | + * |
|
| 2198 | + * This applies only to PHP version 5.3.0 and greater |
|
| 2199 | + * |
|
| 2200 | + * @since 1.8.5 |
|
| 2201 | + */ |
|
| 2202 | + $mode = apply_filters( 'sensei_round_mode', $mode , $val, $context, $precision ); |
|
| 2203 | 2203 | |
| 2204 | - if ( version_compare(PHP_VERSION, '5.3.0') >= 0 ) { |
|
| 2204 | + if ( version_compare(PHP_VERSION, '5.3.0') >= 0 ) { |
|
| 2205 | 2205 | |
| 2206 | - return round( $val, $precision, $mode ); |
|
| 2206 | + return round( $val, $precision, $mode ); |
|
| 2207 | 2207 | |
| 2208 | - }else{ |
|
| 2208 | + }else{ |
|
| 2209 | 2209 | |
| 2210 | - return round( $val, $precision ); |
|
| 2210 | + return round( $val, $precision ); |
|
| 2211 | 2211 | |
| 2212 | - } |
|
| 2212 | + } |
|
| 2213 | 2213 | |
| 2214 | - } |
|
| 2214 | + } |
|
| 2215 | 2215 | |
| 2216 | - /** |
|
| 2217 | - * Returns the current url with all the query vars |
|
| 2218 | - * |
|
| 2219 | - * @since 1.9.0 |
|
| 2220 | - * @return string $url |
|
| 2221 | - */ |
|
| 2222 | - public static function get_current_url(){ |
|
| 2216 | + /** |
|
| 2217 | + * Returns the current url with all the query vars |
|
| 2218 | + * |
|
| 2219 | + * @since 1.9.0 |
|
| 2220 | + * @return string $url |
|
| 2221 | + */ |
|
| 2222 | + public static function get_current_url(){ |
|
| 2223 | 2223 | |
| 2224 | - global $wp; |
|
| 2225 | - $current_url = trailingslashit( home_url( $wp->request ) ); |
|
| 2226 | - if ( isset( $_GET ) ) { |
|
| 2224 | + global $wp; |
|
| 2225 | + $current_url = trailingslashit( home_url( $wp->request ) ); |
|
| 2226 | + if ( isset( $_GET ) ) { |
|
| 2227 | 2227 | |
| 2228 | - foreach ($_GET as $param => $val ) { |
|
| 2228 | + foreach ($_GET as $param => $val ) { |
|
| 2229 | 2229 | |
| 2230 | - $current_url = add_query_arg( $param, $val , $current_url ); |
|
| 2230 | + $current_url = add_query_arg( $param, $val , $current_url ); |
|
| 2231 | 2231 | |
| 2232 | - } |
|
| 2233 | - } |
|
| 2232 | + } |
|
| 2233 | + } |
|
| 2234 | 2234 | |
| 2235 | - return $current_url; |
|
| 2236 | - } |
|
| 2235 | + return $current_url; |
|
| 2236 | + } |
|
| 2237 | 2237 | |
| 2238 | - /** |
|
| 2239 | - * Restore the global WP_Query |
|
| 2240 | - * |
|
| 2241 | - * @since 1.9.0 |
|
| 2242 | - */ |
|
| 2243 | - public static function restore_wp_query() { |
|
| 2238 | + /** |
|
| 2239 | + * Restore the global WP_Query |
|
| 2240 | + * |
|
| 2241 | + * @since 1.9.0 |
|
| 2242 | + */ |
|
| 2243 | + public static function restore_wp_query() { |
|
| 2244 | 2244 | |
| 2245 | - wp_reset_query(); |
|
| 2245 | + wp_reset_query(); |
|
| 2246 | 2246 | |
| 2247 | - } |
|
| 2247 | + } |
|
| 2248 | 2248 | |
| 2249 | - /** |
|
| 2250 | - * Merge two arrays in a zip like fashion. |
|
| 2251 | - * If one array is longer than the other the elements will be apended |
|
| 2252 | - * to the end of the resulting array. |
|
| 2253 | - * |
|
| 2254 | - * @since 1.9.0 |
|
| 2255 | - * |
|
| 2256 | - * @param array $array_a |
|
| 2257 | - * @param array $array_b |
|
| 2258 | - * @return array $merged_array |
|
| 2259 | - */ |
|
| 2260 | - public static function array_zip_merge( $array_a, $array_b ){ |
|
| 2249 | + /** |
|
| 2250 | + * Merge two arrays in a zip like fashion. |
|
| 2251 | + * If one array is longer than the other the elements will be apended |
|
| 2252 | + * to the end of the resulting array. |
|
| 2253 | + * |
|
| 2254 | + * @since 1.9.0 |
|
| 2255 | + * |
|
| 2256 | + * @param array $array_a |
|
| 2257 | + * @param array $array_b |
|
| 2258 | + * @return array $merged_array |
|
| 2259 | + */ |
|
| 2260 | + public static function array_zip_merge( $array_a, $array_b ){ |
|
| 2261 | 2261 | |
| 2262 | - if( ! is_array( $array_a ) || ! is_array( $array_b ) ){ |
|
| 2263 | - trigger_error('array_zip_merge requires both arrays to be indexed arrays '); |
|
| 2264 | - } |
|
| 2262 | + if( ! is_array( $array_a ) || ! is_array( $array_b ) ){ |
|
| 2263 | + trigger_error('array_zip_merge requires both arrays to be indexed arrays '); |
|
| 2264 | + } |
|
| 2265 | 2265 | |
| 2266 | - $merged_array = array(); |
|
| 2267 | - $total_elements = count( $array_a ) + count( $array_b ); |
|
| 2266 | + $merged_array = array(); |
|
| 2267 | + $total_elements = count( $array_a ) + count( $array_b ); |
|
| 2268 | 2268 | |
| 2269 | - // Zip arrays |
|
| 2270 | - for ( $i = 0; $i < $total_elements; $i++) { |
|
| 2269 | + // Zip arrays |
|
| 2270 | + for ( $i = 0; $i < $total_elements; $i++) { |
|
| 2271 | 2271 | |
| 2272 | - // if has an element at current index push a on top |
|
| 2273 | - if( isset( $array_a[ $i ] ) ){ |
|
| 2274 | - $merged_array[] = $array_a[ $i ] ; |
|
| 2275 | - } |
|
| 2272 | + // if has an element at current index push a on top |
|
| 2273 | + if( isset( $array_a[ $i ] ) ){ |
|
| 2274 | + $merged_array[] = $array_a[ $i ] ; |
|
| 2275 | + } |
|
| 2276 | 2276 | |
| 2277 | - // next if $array_b has an element at current index push a on top of the element |
|
| 2278 | - // from a if there was one, if not the element before that. |
|
| 2279 | - if( isset( $array_b[ $i ] ) ){ |
|
| 2280 | - $merged_array[] = $array_b[ $i ] ; |
|
| 2281 | - } |
|
| 2277 | + // next if $array_b has an element at current index push a on top of the element |
|
| 2278 | + // from a if there was one, if not the element before that. |
|
| 2279 | + if( isset( $array_b[ $i ] ) ){ |
|
| 2280 | + $merged_array[] = $array_b[ $i ] ; |
|
| 2281 | + } |
|
| 2282 | 2282 | |
| 2283 | - } |
|
| 2283 | + } |
|
| 2284 | 2284 | |
| 2285 | - return $merged_array; |
|
| 2286 | - } |
|
| 2285 | + return $merged_array; |
|
| 2286 | + } |
|
| 2287 | 2287 | |
| 2288 | 2288 | } // End Class |
| 2289 | 2289 | |
@@ -13,756 +13,756 @@ discard block |
||
| 13 | 13 | |
| 14 | 14 | Class Sensei_WC{ |
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Load the files needed for the woocommerce integration. |
|
| 18 | - * |
|
| 19 | - * @since 1.9.0 |
|
| 20 | - */ |
|
| 21 | - public static function load_woocommerce_integration_hooks(){ |
|
| 16 | + /** |
|
| 17 | + * Load the files needed for the woocommerce integration. |
|
| 18 | + * |
|
| 19 | + * @since 1.9.0 |
|
| 20 | + */ |
|
| 21 | + public static function load_woocommerce_integration_hooks(){ |
|
| 22 | 22 | |
| 23 | - $woocommerce_hooks_file_path = Sensei()->plugin_path() . 'includes/hooks/woocommerce.php'; |
|
| 24 | - require_once( $woocommerce_hooks_file_path ); |
|
| 23 | + $woocommerce_hooks_file_path = Sensei()->plugin_path() . 'includes/hooks/woocommerce.php'; |
|
| 24 | + require_once( $woocommerce_hooks_file_path ); |
|
| 25 | 25 | |
| 26 | - } |
|
| 27 | - /** |
|
| 28 | - * check if WooCommerce plugin is loaded and allowed by Sensei |
|
| 29 | - * |
|
| 30 | - * @since 1.9.0 |
|
| 31 | - * @return bool |
|
| 32 | - */ |
|
| 33 | - public static function is_woocommerce_active(){ |
|
| 26 | + } |
|
| 27 | + /** |
|
| 28 | + * check if WooCommerce plugin is loaded and allowed by Sensei |
|
| 29 | + * |
|
| 30 | + * @since 1.9.0 |
|
| 31 | + * @return bool |
|
| 32 | + */ |
|
| 33 | + public static function is_woocommerce_active(){ |
|
| 34 | 34 | |
| 35 | - $is_woocommerce_enabled_in_settings = isset( Sensei()->settings->settings['woocommerce_enabled'] ) && Sensei()->settings->settings['woocommerce_enabled']; |
|
| 36 | - return self::is_woocommerce_present() && $is_woocommerce_enabled_in_settings; |
|
| 35 | + $is_woocommerce_enabled_in_settings = isset( Sensei()->settings->settings['woocommerce_enabled'] ) && Sensei()->settings->settings['woocommerce_enabled']; |
|
| 36 | + return self::is_woocommerce_present() && $is_woocommerce_enabled_in_settings; |
|
| 37 | 37 | |
| 38 | - } // end is_woocommerce_active |
|
| 38 | + } // end is_woocommerce_active |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Checks if the WooCommerce plugin is installed and activation. |
|
| 42 | - * |
|
| 43 | - * If you need to check if WooCommerce is activated use Sensei_Utils::is_woocommerce_active(). |
|
| 44 | - * This function does nott check to see if the Sensei setting for WooCommerce is enabled. |
|
| 45 | - * |
|
| 46 | - * @since 1.9.0 |
|
| 47 | - * |
|
| 48 | - * @return bool |
|
| 49 | - */ |
|
| 50 | - public static function is_woocommerce_present(){ |
|
| 40 | + /** |
|
| 41 | + * Checks if the WooCommerce plugin is installed and activation. |
|
| 42 | + * |
|
| 43 | + * If you need to check if WooCommerce is activated use Sensei_Utils::is_woocommerce_active(). |
|
| 44 | + * This function does nott check to see if the Sensei setting for WooCommerce is enabled. |
|
| 45 | + * |
|
| 46 | + * @since 1.9.0 |
|
| 47 | + * |
|
| 48 | + * @return bool |
|
| 49 | + */ |
|
| 50 | + public static function is_woocommerce_present(){ |
|
| 51 | 51 | |
| 52 | - $active_plugins = (array) get_option( 'active_plugins', array() ); |
|
| 52 | + $active_plugins = (array) get_option( 'active_plugins', array() ); |
|
| 53 | 53 | |
| 54 | - if ( is_multisite() ){ |
|
| 54 | + if ( is_multisite() ){ |
|
| 55 | 55 | |
| 56 | - $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) ); |
|
| 56 | + $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) ); |
|
| 57 | 57 | |
| 58 | - } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - $is_woocommerce_plugin_present_and_activated = in_array( 'woocommerce/woocommerce.php', $active_plugins ) || array_key_exists( 'woocommerce/woocommerce.php', $active_plugins ); |
|
| 60 | + $is_woocommerce_plugin_present_and_activated = in_array( 'woocommerce/woocommerce.php', $active_plugins ) || array_key_exists( 'woocommerce/woocommerce.php', $active_plugins ); |
|
| 61 | 61 | |
| 62 | - return class_exists( 'Woocommerce' ) || $is_woocommerce_plugin_present_and_activated; |
|
| 62 | + return class_exists( 'Woocommerce' ) || $is_woocommerce_plugin_present_and_activated; |
|
| 63 | 63 | |
| 64 | - }// end is_woocommerce_present |
|
| 64 | + }// end is_woocommerce_present |
|
| 65 | 65 | |
| 66 | - /** |
|
| 67 | - * Find the order active number (completed or processing ) for a given user on a course. It will return the latest order. |
|
| 68 | - * |
|
| 69 | - * If multiple exist we will return the latest order. |
|
| 70 | - * |
|
| 71 | - * @param $user_id |
|
| 72 | - * @param $course_id |
|
| 73 | - * @return array $user_course_orders |
|
| 74 | - */ |
|
| 75 | - public static function get_learner_course_active_order_id( $user_id, $course_id ){ |
|
| 66 | + /** |
|
| 67 | + * Find the order active number (completed or processing ) for a given user on a course. It will return the latest order. |
|
| 68 | + * |
|
| 69 | + * If multiple exist we will return the latest order. |
|
| 70 | + * |
|
| 71 | + * @param $user_id |
|
| 72 | + * @param $course_id |
|
| 73 | + * @return array $user_course_orders |
|
| 74 | + */ |
|
| 75 | + public static function get_learner_course_active_order_id( $user_id, $course_id ){ |
|
| 76 | + |
|
| 77 | + $course_product_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 78 | + |
|
| 79 | + $orders_query = new WP_Query( array( |
|
| 80 | + 'post_type' => 'shop_order', |
|
| 81 | + 'posts_per_page' => -1, |
|
| 82 | + 'post_status' => array( 'wc-processing', 'wc-completed' ), |
|
| 83 | + 'meta_key'=> '_customer_user', |
|
| 84 | + 'meta_value'=> $user_id, |
|
| 85 | + ) ); |
|
| 86 | + |
|
| 87 | + if( $orders_query->post_count == 0 ){ |
|
| 88 | + |
|
| 89 | + return false; |
|
| 90 | + |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + foreach( $orders_query->get_posts() as $order ){ |
|
| 94 | + |
|
| 95 | + $order = new WC_Order( $order->ID ); |
|
| 96 | + $items = $order->get_items(); |
|
| 97 | + |
|
| 98 | + $user_orders = array(); |
|
| 76 | 99 | |
| 77 | - $course_product_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 100 | + foreach( $items as $item ){ |
|
| 78 | 101 | |
| 79 | - $orders_query = new WP_Query( array( |
|
| 80 | - 'post_type' => 'shop_order', |
|
| 81 | - 'posts_per_page' => -1, |
|
| 82 | - 'post_status' => array( 'wc-processing', 'wc-completed' ), |
|
| 83 | - 'meta_key'=> '_customer_user', |
|
| 84 | - 'meta_value'=> $user_id, |
|
| 85 | - ) ); |
|
| 102 | + // if the product id on the order and the one given to this function |
|
| 103 | + // this order has been placed by the given user on the given course. |
|
| 104 | + $product = wc_get_product( $item['product_id'] ); |
|
| 86 | 105 | |
| 87 | - if( $orders_query->post_count == 0 ){ |
|
| 106 | + if ( is_object( $product ) && $product->is_type( 'variable' )) { |
|
| 88 | 107 | |
| 89 | - return false; |
|
| 108 | + $item_product_id = $item['variation_id']; |
|
| 90 | 109 | |
| 91 | - } |
|
| 110 | + } else { |
|
| 92 | 111 | |
| 93 | - foreach( $orders_query->get_posts() as $order ){ |
|
| 112 | + $item_product_id = $item['product_id']; |
|
| 94 | 113 | |
| 95 | - $order = new WC_Order( $order->ID ); |
|
| 96 | - $items = $order->get_items(); |
|
| 114 | + } |
|
| 97 | 115 | |
| 98 | - $user_orders = array(); |
|
| 116 | + if( $course_product_id == $item_product_id ){ |
|
| 99 | 117 | |
| 100 | - foreach( $items as $item ){ |
|
| 118 | + return $order->id; |
|
| 101 | 119 | |
| 102 | - // if the product id on the order and the one given to this function |
|
| 103 | - // this order has been placed by the given user on the given course. |
|
| 104 | - $product = wc_get_product( $item['product_id'] ); |
|
| 120 | + } |
|
| 105 | 121 | |
| 106 | - if ( is_object( $product ) && $product->is_type( 'variable' )) { |
|
| 107 | 122 | |
| 108 | - $item_product_id = $item['variation_id']; |
|
| 123 | + }//end for each order item |
|
| 109 | 124 | |
| 110 | - } else { |
|
| 125 | + } // end for each order |
|
| 111 | 126 | |
| 112 | - $item_product_id = $item['product_id']; |
|
| 127 | + // if we reach this place we found no order |
|
| 128 | + return false; |
|
| 113 | 129 | |
| 114 | - } |
|
| 130 | + } // end get_learner_course_active_order_ids |
|
| 115 | 131 | |
| 116 | - if( $course_product_id == $item_product_id ){ |
|
| 132 | + /** |
|
| 133 | + * Output WooCommerce specific course filters |
|
| 134 | + * Removing the paged argument |
|
| 135 | + * |
|
| 136 | + * @since 1.9.0 |
|
| 137 | + * @param $filter_links |
|
| 138 | + * @return mixed |
|
| 139 | + */ |
|
| 140 | + public static function add_course_archive_wc_filter_links( $filter_links ){ |
|
| 141 | + |
|
| 142 | + $free_courses = self::get_free_courses(); |
|
| 143 | + $paid_courses = self::get_paid_courses(); |
|
| 144 | + |
|
| 145 | + if ( empty( $free_courses ) || empty( $paid_courses ) ){ |
|
| 146 | + // do not show any WooCommerce filters if all courses are |
|
| 147 | + // free or if all courses are paid |
|
| 148 | + return $filter_links; |
|
| 117 | 149 | |
| 118 | - return $order->id; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + $filter_links[] = array( |
|
| 153 | + 'id'=>'paid' , |
|
| 154 | + 'url'=> add_query_arg( array( 'course_filter'=>'paid'), Sensei_Course::get_courses_page_url() ), |
|
| 155 | + 'title'=>__( 'Paid', 'woothemes-sensei' ) |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + $filter_links[] = array( |
|
| 159 | + 'id'=>'free', |
|
| 160 | + 'url'=> add_query_arg( array( 'course_filter'=>'free'), Sensei_Course::get_courses_page_url() ), |
|
| 161 | + 'title'=>__( 'Free', 'woothemes-sensei' ) |
|
| 162 | + ); |
|
| 119 | 163 | |
| 120 | - } |
|
| 164 | + return $filter_links; |
|
| 121 | 165 | |
| 166 | + }// end add_course_archive_wc_filter_links |
|
| 122 | 167 | |
| 123 | - }//end for each order item |
|
| 168 | + /** |
|
| 169 | + * Apply the free filter the the course query |
|
| 170 | + * getting all course with no products or products with zero price |
|
| 171 | + * |
|
| 172 | + * hooked into pre_get_posts |
|
| 173 | + * |
|
| 174 | + * @since 1.9.0 |
|
| 175 | + * @param WP_Query $query |
|
| 176 | + * @return WP_Query $query |
|
| 177 | + */ |
|
| 178 | + public static function course_archive_wc_filter_free( $query ){ |
|
| 124 | 179 | |
| 125 | - } // end for each order |
|
| 180 | + if( isset( $_GET['course_filter'] ) && 'free' == $_GET['course_filter'] |
|
| 181 | + && 'course' == $query->get( 'post_type') && $query->is_main_query() ){ |
|
| 126 | 182 | |
| 127 | - // if we reach this place we found no order |
|
| 128 | - return false; |
|
| 183 | + // setup the course meta query |
|
| 184 | + $meta_query = self::get_free_courses_meta_query_args(); |
|
| 129 | 185 | |
| 130 | - } // end get_learner_course_active_order_ids |
|
| 186 | + // manipulate the query to return free courses |
|
| 187 | + $query->set('meta_query', $meta_query ); |
|
| 131 | 188 | |
| 132 | - /** |
|
| 133 | - * Output WooCommerce specific course filters |
|
| 134 | - * Removing the paged argument |
|
| 135 | - * |
|
| 136 | - * @since 1.9.0 |
|
| 137 | - * @param $filter_links |
|
| 138 | - * @return mixed |
|
| 139 | - */ |
|
| 140 | - public static function add_course_archive_wc_filter_links( $filter_links ){ |
|
| 189 | + // don't show any paid courses |
|
| 190 | + $courses = self::get_paid_courses(); |
|
| 191 | + $ids = array(); |
|
| 192 | + foreach( $courses as $course ){ |
|
| 193 | + $ids[] = $course->ID; |
|
| 194 | + } |
|
| 195 | + $query->set( 'post__not_in', $ids ); |
|
| 141 | 196 | |
| 142 | - $free_courses = self::get_free_courses(); |
|
| 143 | - $paid_courses = self::get_paid_courses(); |
|
| 197 | + }// end if course_filter |
|
| 144 | 198 | |
| 145 | - if ( empty( $free_courses ) || empty( $paid_courses ) ){ |
|
| 146 | - // do not show any WooCommerce filters if all courses are |
|
| 147 | - // free or if all courses are paid |
|
| 148 | - return $filter_links; |
|
| 199 | + return $query; |
|
| 149 | 200 | |
| 150 | - } |
|
| 201 | + }// course_archive_wc_filter_free |
|
| 151 | 202 | |
| 152 | - $filter_links[] = array( |
|
| 153 | - 'id'=>'paid' , |
|
| 154 | - 'url'=> add_query_arg( array( 'course_filter'=>'paid'), Sensei_Course::get_courses_page_url() ), |
|
| 155 | - 'title'=>__( 'Paid', 'woothemes-sensei' ) |
|
| 156 | - ); |
|
| 203 | + /** |
|
| 204 | + * Apply the paid filter to the course query on the courses page |
|
| 205 | + * will include all course with a product attached with a price |
|
| 206 | + * more than 0 |
|
| 207 | + * |
|
| 208 | + * hooked into pre_get_posts |
|
| 209 | + * |
|
| 210 | + * @since 1.9.0 |
|
| 211 | + * @param WP_Query $query |
|
| 212 | + * @return WP_Query $query |
|
| 213 | + */ |
|
| 214 | + public static function course_archive_wc_filter_paid( $query ){ |
|
| 157 | 215 | |
| 158 | - $filter_links[] = array( |
|
| 159 | - 'id'=>'free', |
|
| 160 | - 'url'=> add_query_arg( array( 'course_filter'=>'free'), Sensei_Course::get_courses_page_url() ), |
|
| 161 | - 'title'=>__( 'Free', 'woothemes-sensei' ) |
|
| 162 | - ); |
|
| 216 | + if( isset( $_GET['course_filter'] ) && 'paid' == $_GET['course_filter'] |
|
| 217 | + && 'course' == $query->get( 'post_type') && $query->is_main_query() ){ |
|
| 163 | 218 | |
| 164 | - return $filter_links; |
|
| 219 | + // setup the course meta query |
|
| 220 | + $meta_query = self::get_paid_courses_meta_query_args(); |
|
| 165 | 221 | |
| 166 | - }// end add_course_archive_wc_filter_links |
|
| 222 | + // manipulate the query to return free courses |
|
| 223 | + $query->set('meta_query', $meta_query ); |
|
| 167 | 224 | |
| 168 | - /** |
|
| 169 | - * Apply the free filter the the course query |
|
| 170 | - * getting all course with no products or products with zero price |
|
| 171 | - * |
|
| 172 | - * hooked into pre_get_posts |
|
| 173 | - * |
|
| 174 | - * @since 1.9.0 |
|
| 175 | - * @param WP_Query $query |
|
| 176 | - * @return WP_Query $query |
|
| 177 | - */ |
|
| 178 | - public static function course_archive_wc_filter_free( $query ){ |
|
| 225 | + } |
|
| 179 | 226 | |
| 180 | - if( isset( $_GET['course_filter'] ) && 'free' == $_GET['course_filter'] |
|
| 181 | - && 'course' == $query->get( 'post_type') && $query->is_main_query() ){ |
|
| 227 | + return $query; |
|
| 182 | 228 | |
| 183 | - // setup the course meta query |
|
| 184 | - $meta_query = self::get_free_courses_meta_query_args(); |
|
| 229 | + } |
|
| 185 | 230 | |
| 186 | - // manipulate the query to return free courses |
|
| 187 | - $query->set('meta_query', $meta_query ); |
|
| 231 | + /** |
|
| 232 | + * Load the WooCommerce single product actions above |
|
| 233 | + * single courses if woocommerce is active allowing purchase |
|
| 234 | + * information and actions to be hooked from WooCommerce. |
|
| 235 | + */ |
|
| 236 | + public static function do_single_course_wc_single_product_action(){ |
|
| 188 | 237 | |
| 189 | - // don't show any paid courses |
|
| 190 | - $courses = self::get_paid_courses(); |
|
| 191 | - $ids = array(); |
|
| 192 | - foreach( $courses as $course ){ |
|
| 193 | - $ids[] = $course->ID; |
|
| 194 | - } |
|
| 195 | - $query->set( 'post__not_in', $ids ); |
|
| 238 | + /** |
|
| 239 | + * this hooks is documented within the WooCommerce plugin. |
|
| 240 | + */ |
|
| 241 | + if ( Sensei_WC::is_woocommerce_active() ) { |
|
| 196 | 242 | |
| 197 | - }// end if course_filter |
|
| 243 | + do_action( 'woocommerce_before_single_product' ); |
|
| 198 | 244 | |
| 199 | - return $query; |
|
| 245 | + } // End If Statement |
|
| 200 | 246 | |
| 201 | - }// course_archive_wc_filter_free |
|
| 247 | + }// end do_single_course_wc_single_product_action |
|
| 202 | 248 | |
| 203 | - /** |
|
| 204 | - * Apply the paid filter to the course query on the courses page |
|
| 205 | - * will include all course with a product attached with a price |
|
| 206 | - * more than 0 |
|
| 207 | - * |
|
| 208 | - * hooked into pre_get_posts |
|
| 209 | - * |
|
| 210 | - * @since 1.9.0 |
|
| 211 | - * @param WP_Query $query |
|
| 212 | - * @return WP_Query $query |
|
| 213 | - */ |
|
| 214 | - public static function course_archive_wc_filter_paid( $query ){ |
|
| 249 | + /** |
|
| 250 | + * Hooking into the single lesson page to alter the |
|
| 251 | + * user access permissions based on if they have purchased the |
|
| 252 | + * course the lesson belongs to. |
|
| 253 | + * |
|
| 254 | + * This function will only return false or the passed in user_access value. |
|
| 255 | + * It doesn't return true in order to avoid altering other options. |
|
| 256 | + * |
|
| 257 | + * @since 1.9.0 |
|
| 258 | + * |
|
| 259 | + * @param $can_user_view_lesson |
|
| 260 | + * @param $lesson_id |
|
| 261 | + * @param $user_id |
|
| 262 | + * @return bool |
|
| 263 | + */ |
|
| 264 | + public static function alter_can_user_view_lesson ( $can_user_view_lesson, $lesson_id, $user_id ){ |
|
| 215 | 265 | |
| 216 | - if( isset( $_GET['course_filter'] ) && 'paid' == $_GET['course_filter'] |
|
| 217 | - && 'course' == $query->get( 'post_type') && $query->is_main_query() ){ |
|
| 218 | - |
|
| 219 | - // setup the course meta query |
|
| 220 | - $meta_query = self::get_paid_courses_meta_query_args(); |
|
| 221 | - |
|
| 222 | - // manipulate the query to return free courses |
|
| 223 | - $query->set('meta_query', $meta_query ); |
|
| 224 | - |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - return $query; |
|
| 228 | - |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - /** |
|
| 232 | - * Load the WooCommerce single product actions above |
|
| 233 | - * single courses if woocommerce is active allowing purchase |
|
| 234 | - * information and actions to be hooked from WooCommerce. |
|
| 235 | - */ |
|
| 236 | - public static function do_single_course_wc_single_product_action(){ |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * this hooks is documented within the WooCommerce plugin. |
|
| 240 | - */ |
|
| 241 | - if ( Sensei_WC::is_woocommerce_active() ) { |
|
| 242 | - |
|
| 243 | - do_action( 'woocommerce_before_single_product' ); |
|
| 244 | - |
|
| 245 | - } // End If Statement |
|
| 246 | - |
|
| 247 | - }// end do_single_course_wc_single_product_action |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Hooking into the single lesson page to alter the |
|
| 251 | - * user access permissions based on if they have purchased the |
|
| 252 | - * course the lesson belongs to. |
|
| 253 | - * |
|
| 254 | - * This function will only return false or the passed in user_access value. |
|
| 255 | - * It doesn't return true in order to avoid altering other options. |
|
| 256 | - * |
|
| 257 | - * @since 1.9.0 |
|
| 258 | - * |
|
| 259 | - * @param $can_user_view_lesson |
|
| 260 | - * @param $lesson_id |
|
| 261 | - * @param $user_id |
|
| 262 | - * @return bool |
|
| 263 | - */ |
|
| 264 | - public static function alter_can_user_view_lesson ( $can_user_view_lesson, $lesson_id, $user_id ){ |
|
| 265 | - |
|
| 266 | - // do not override access to admins |
|
| 267 | - $course_id = Sensei()->lesson->get_course_id( $lesson_id ); |
|
| 268 | - if ( sensei_all_access() || Sensei_Utils::is_preview_lesson( $lesson_id ) |
|
| 269 | - || Sensei_Utils::user_started_course( $course_id, $user_id ) ){ |
|
| 266 | + // do not override access to admins |
|
| 267 | + $course_id = Sensei()->lesson->get_course_id( $lesson_id ); |
|
| 268 | + if ( sensei_all_access() || Sensei_Utils::is_preview_lesson( $lesson_id ) |
|
| 269 | + || Sensei_Utils::user_started_course( $course_id, $user_id ) ){ |
|
| 270 | 270 | |
| 271 | 271 | return true; |
| 272 | 272 | |
| 273 | - } |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - // check if the course has a valid product attached to it |
|
| 276 | - // which the user should have purchased if they want to access |
|
| 277 | - // the current lesson |
|
| 278 | - $course_id = get_post_meta( $lesson_id , '_lesson_course', true); |
|
| 279 | - $wc_post_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 280 | - $product = Sensei()->sensei_get_woocommerce_product_object($wc_post_id); |
|
| 281 | - if( isset ($product) && is_object($product) ){ |
|
| 275 | + // check if the course has a valid product attached to it |
|
| 276 | + // which the user should have purchased if they want to access |
|
| 277 | + // the current lesson |
|
| 278 | + $course_id = get_post_meta( $lesson_id , '_lesson_course', true); |
|
| 279 | + $wc_post_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 280 | + $product = Sensei()->sensei_get_woocommerce_product_object($wc_post_id); |
|
| 281 | + if( isset ($product) && is_object($product) ){ |
|
| 282 | 282 | |
| 283 | - // valid product found |
|
| 284 | - $order_id = self::get_learner_course_active_order_id( $user_id, $course_id ); |
|
| 283 | + // valid product found |
|
| 284 | + $order_id = self::get_learner_course_active_order_id( $user_id, $course_id ); |
|
| 285 | 285 | |
| 286 | - // product has a successful order so this user may access the content |
|
| 287 | - // this function may only return false or the default |
|
| 288 | - // returning true may override other negatives which we don't want |
|
| 289 | - if( ! $order_id ){ |
|
| 286 | + // product has a successful order so this user may access the content |
|
| 287 | + // this function may only return false or the default |
|
| 288 | + // returning true may override other negatives which we don't want |
|
| 289 | + if( ! $order_id ){ |
|
| 290 | 290 | |
| 291 | - return false; |
|
| 291 | + return false; |
|
| 292 | 292 | |
| 293 | - } |
|
| 293 | + } |
|
| 294 | 294 | |
| 295 | - } |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | - // return the passed in value |
|
| 298 | - return $can_user_view_lesson; |
|
| 297 | + // return the passed in value |
|
| 298 | + return $can_user_view_lesson; |
|
| 299 | 299 | |
| 300 | - } |
|
| 300 | + } |
|
| 301 | 301 | |
| 302 | - /** |
|
| 303 | - * Add course link to order thank you and details pages. |
|
| 304 | - * |
|
| 305 | - * @since 1.4.5 |
|
| 306 | - * @access public |
|
| 307 | - * |
|
| 308 | - * @return void |
|
| 309 | - */ |
|
| 310 | - public static function course_link_from_order( ) { |
|
| 302 | + /** |
|
| 303 | + * Add course link to order thank you and details pages. |
|
| 304 | + * |
|
| 305 | + * @since 1.4.5 |
|
| 306 | + * @access public |
|
| 307 | + * |
|
| 308 | + * @return void |
|
| 309 | + */ |
|
| 310 | + public static function course_link_from_order( ) { |
|
| 311 | 311 | |
| 312 | - if( ! is_order_received_page() ){ |
|
| 313 | - return; |
|
| 314 | - } |
|
| 312 | + if( ! is_order_received_page() ){ |
|
| 313 | + return; |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - $order_id = get_query_var( 'order-received' ); |
|
| 316 | + $order_id = get_query_var( 'order-received' ); |
|
| 317 | 317 | $order = new WC_Order( $order_id ); |
| 318 | 318 | |
| 319 | 319 | // exit early if not wc-completed or wc-processing |
| 320 | 320 | if( 'wc-completed' != $order->post_status |
| 321 | - && 'wc-processing' != $order->post_status ) { |
|
| 322 | - return; |
|
| 323 | - } |
|
| 321 | + && 'wc-processing' != $order->post_status ) { |
|
| 322 | + return; |
|
| 323 | + } |
|
| 324 | 324 | |
| 325 | - $course_links = array(); // store the for links for courses purchased |
|
| 325 | + $course_links = array(); // store the for links for courses purchased |
|
| 326 | 326 | foreach ( $order->get_items() as $item ) { |
| 327 | 327 | |
| 328 | - if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 328 | + if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 329 | 329 | |
| 330 | - // If item has variation_id then its a variation of the product |
|
| 331 | - $item_id = $item['variation_id']; |
|
| 330 | + // If item has variation_id then its a variation of the product |
|
| 331 | + $item_id = $item['variation_id']; |
|
| 332 | 332 | |
| 333 | - } else { |
|
| 333 | + } else { |
|
| 334 | 334 | |
| 335 | - //If not its real product set its id to item_id |
|
| 336 | - $item_id = $item['product_id']; |
|
| 335 | + //If not its real product set its id to item_id |
|
| 336 | + $item_id = $item['product_id']; |
|
| 337 | 337 | |
| 338 | - } // End If Statement |
|
| 338 | + } // End If Statement |
|
| 339 | 339 | |
| 340 | - $user_id = get_post_meta( $order->id, '_customer_user', true ); |
|
| 340 | + $user_id = get_post_meta( $order->id, '_customer_user', true ); |
|
| 341 | 341 | |
| 342 | - if( $user_id ) { |
|
| 342 | + if( $user_id ) { |
|
| 343 | 343 | |
| 344 | - // Get all courses for product |
|
| 345 | - $args = Sensei_Course::get_default_query_args(); |
|
| 346 | - $args['meta_query'] = array( array( |
|
| 347 | - 'key' => '_course_woocommerce_product', |
|
| 348 | - 'value' => $item_id |
|
| 349 | - ) ); |
|
| 350 | - $args['orderby'] = 'menu_order date'; |
|
| 351 | - $args['order'] = 'ASC'; |
|
| 344 | + // Get all courses for product |
|
| 345 | + $args = Sensei_Course::get_default_query_args(); |
|
| 346 | + $args['meta_query'] = array( array( |
|
| 347 | + 'key' => '_course_woocommerce_product', |
|
| 348 | + 'value' => $item_id |
|
| 349 | + ) ); |
|
| 350 | + $args['orderby'] = 'menu_order date'; |
|
| 351 | + $args['order'] = 'ASC'; |
|
| 352 | 352 | |
| 353 | - // loop through courses |
|
| 354 | - $courses = get_posts( $args ); |
|
| 355 | - if( $courses && count( $courses ) > 0 ) { |
|
| 353 | + // loop through courses |
|
| 354 | + $courses = get_posts( $args ); |
|
| 355 | + if( $courses && count( $courses ) > 0 ) { |
|
| 356 | 356 | |
| 357 | - foreach( $courses as $course ) { |
|
| 357 | + foreach( $courses as $course ) { |
|
| 358 | 358 | |
| 359 | - $title = $course->post_title; |
|
| 360 | - $permalink = get_permalink( $course->ID ); |
|
| 361 | - $course_links[] .= '<a href="' . esc_url( $permalink ) . '" >' . $title . '</a> '; |
|
| 359 | + $title = $course->post_title; |
|
| 360 | + $permalink = get_permalink( $course->ID ); |
|
| 361 | + $course_links[] .= '<a href="' . esc_url( $permalink ) . '" >' . $title . '</a> '; |
|
| 362 | 362 | |
| 363 | - } // end for each |
|
| 363 | + } // end for each |
|
| 364 | 364 | |
| 365 | - // close the message div |
|
| 365 | + // close the message div |
|
| 366 | 366 | |
| 367 | - }// end if $courses check |
|
| 368 | - } |
|
| 369 | - }// end loop through orders |
|
| 367 | + }// end if $courses check |
|
| 368 | + } |
|
| 369 | + }// end loop through orders |
|
| 370 | 370 | |
| 371 | - // add the courses to the WooCommerce notice |
|
| 372 | - if( ! empty( $course_links) ){ |
|
| 371 | + // add the courses to the WooCommerce notice |
|
| 372 | + if( ! empty( $course_links) ){ |
|
| 373 | 373 | |
| 374 | - $courses_html = _nx( |
|
| 375 | - 'You have purchased the following course:', |
|
| 376 | - 'You have purchased the following courses:', |
|
| 377 | - count( $course_links ), |
|
| 378 | - 'Purchase thank you note on Checkout page. The course link(s) will be show', 'woothemes-sensei' |
|
| 379 | - ); |
|
| 374 | + $courses_html = _nx( |
|
| 375 | + 'You have purchased the following course:', |
|
| 376 | + 'You have purchased the following courses:', |
|
| 377 | + count( $course_links ), |
|
| 378 | + 'Purchase thank you note on Checkout page. The course link(s) will be show', 'woothemes-sensei' |
|
| 379 | + ); |
|
| 380 | 380 | |
| 381 | - foreach( $course_links as $link ){ |
|
| 381 | + foreach( $course_links as $link ){ |
|
| 382 | 382 | |
| 383 | - $courses_html .= '<li>' . $link . '</li>'; |
|
| 383 | + $courses_html .= '<li>' . $link . '</li>'; |
|
| 384 | 384 | |
| 385 | - } |
|
| 385 | + } |
|
| 386 | 386 | |
| 387 | - $courses_html .= ' </ul>'; |
|
| 387 | + $courses_html .= ' </ul>'; |
|
| 388 | 388 | |
| 389 | - wc_add_notice( $courses_html, 'success' ); |
|
| 390 | - } |
|
| 389 | + wc_add_notice( $courses_html, 'success' ); |
|
| 390 | + } |
|
| 391 | 391 | |
| 392 | 392 | } // end course_link_order_form |
| 393 | 393 | |
| 394 | - /** |
|
| 395 | - * Show the message that a user should complete |
|
| 396 | - * their purchase if the course is in the cart |
|
| 397 | - * |
|
| 398 | - * This should be used within the course loop or single course page |
|
| 399 | - * |
|
| 400 | - * @since 1.9.0 |
|
| 401 | - */ |
|
| 402 | - public static function course_in_cart_message(){ |
|
| 394 | + /** |
|
| 395 | + * Show the message that a user should complete |
|
| 396 | + * their purchase if the course is in the cart |
|
| 397 | + * |
|
| 398 | + * This should be used within the course loop or single course page |
|
| 399 | + * |
|
| 400 | + * @since 1.9.0 |
|
| 401 | + */ |
|
| 402 | + public static function course_in_cart_message(){ |
|
| 403 | 403 | |
| 404 | - global $post; |
|
| 404 | + global $post; |
|
| 405 | 405 | |
| 406 | - if( self::is_course_in_cart( $post->ID ) ){ ?> |
|
| 406 | + if( self::is_course_in_cart( $post->ID ) ){ ?> |
|
| 407 | 407 | |
| 408 | 408 | <div class="sensei-message info"> |
| 409 | 409 | <?php |
| 410 | 410 | |
| 411 | - $cart_link = '<a class="cart-complete" href="' . WC()->cart->get_checkout_url() |
|
| 412 | - . '" title="' . __('complete purchase', 'woothemes-sensei') . '">' |
|
| 413 | - . __('complete the purchase', 'woothemes-sensei') . '</a>'; |
|
| 411 | + $cart_link = '<a class="cart-complete" href="' . WC()->cart->get_checkout_url() |
|
| 412 | + . '" title="' . __('complete purchase', 'woothemes-sensei') . '">' |
|
| 413 | + . __('complete the purchase', 'woothemes-sensei') . '</a>'; |
|
| 414 | 414 | |
| 415 | - echo sprintf( __('You have already added this Course to your cart. Please %1$s to access the course.', 'woothemes-sensei'), $cart_link ); |
|
| 415 | + echo sprintf( __('You have already added this Course to your cart. Please %1$s to access the course.', 'woothemes-sensei'), $cart_link ); |
|
| 416 | 416 | |
| 417 | - ?> |
|
| 417 | + ?> |
|
| 418 | 418 | </div> |
| 419 | 419 | <?php } |
| 420 | 420 | |
| 421 | - } // End sensei_woocommerce_in_cart_message() |
|
| 421 | + } // End sensei_woocommerce_in_cart_message() |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Checks the cart to see if a course is in the cart. |
|
| 425 | + * |
|
| 426 | + * @param $course_id |
|
| 427 | + * @return bool |
|
| 428 | + */ |
|
| 429 | + public static function is_course_in_cart( $course_id ){ |
|
| 430 | + |
|
| 431 | + $wc_post_id = absint( get_post_meta( $course_id, '_course_woocommerce_product', true ) ); |
|
| 432 | + $user_course_status_id = Sensei_Utils::user_started_course( $course_id , get_current_user_id() ); |
|
| 433 | + |
|
| 434 | + if ( 0 < intval( $wc_post_id ) && ! $user_course_status_id ) { |
|
| 435 | + |
|
| 436 | + if ( self::is_product_in_cart( $wc_post_id ) ) { |
|
| 437 | + |
|
| 438 | + return true; |
|
| 439 | + |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + return false; |
|
| 445 | + |
|
| 446 | + }// is_course_in_cart |
|
| 447 | + |
|
| 448 | + /** |
|
| 449 | + * Check the cart to see if the product is in the cart |
|
| 450 | + * |
|
| 451 | + * @param $product_id |
|
| 452 | + * @return bool |
|
| 453 | + */ |
|
| 454 | + public static function is_product_in_cart( $product_id ){ |
|
| 455 | + |
|
| 456 | + if ( 0 < $product_id ) { |
|
| 457 | + |
|
| 458 | + $product = wc_get_product( $product_id ); |
|
| 459 | + |
|
| 460 | + $parent_id = ''; |
|
| 461 | + if( isset( $product->variation_id ) && 0 < intval( $product->variation_id ) ) { |
|
| 462 | + $wc_product_id = $product->parent->id; |
|
| 463 | + } |
|
| 464 | + foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 465 | + |
|
| 466 | + $cart_product = $values['data']; |
|
| 467 | + if( $product_id == $cart_product->id ) { |
|
| 468 | + |
|
| 469 | + return true; |
|
| 470 | + |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + } |
|
| 474 | + } // End If Statement |
|
| 475 | + |
|
| 476 | + return false; |
|
| 477 | + |
|
| 478 | + } // end is_product_in_car |
|
| 479 | + |
|
| 480 | + /** |
|
| 481 | + * Get all free WooCommerce products |
|
| 482 | + * |
|
| 483 | + * @since 1.9.0 |
|
| 484 | + * |
|
| 485 | + * @return array $free_products{ |
|
| 486 | + * @type int $wp_post_id |
|
| 487 | + * } |
|
| 488 | + */ |
|
| 489 | + public static function get_free_product_ids(){ |
|
| 490 | + |
|
| 491 | + return get_posts( array( |
|
| 492 | + 'post_type' => 'product', |
|
| 493 | + 'posts_per_page' => '1000', |
|
| 494 | + 'fields' => 'ids', |
|
| 495 | + 'meta_query'=> array( |
|
| 496 | + 'relation' => 'OR', |
|
| 497 | + array( |
|
| 498 | + 'key'=> '_regular_price', |
|
| 499 | + 'value' => 0, |
|
| 500 | + ), |
|
| 501 | + array( |
|
| 502 | + 'key'=> '_sale_price', |
|
| 503 | + 'value' => 0, |
|
| 504 | + ), |
|
| 505 | + ), |
|
| 506 | + )); |
|
| 507 | + |
|
| 508 | + }// end get free product query |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * The metat query for courses that are free |
|
| 512 | + * |
|
| 513 | + * @since 1.9.0 |
|
| 514 | + * @return array $wp_meta_query_param |
|
| 515 | + */ |
|
| 516 | + public static function get_free_courses_meta_query_args(){ |
|
| 517 | + |
|
| 518 | + return array( |
|
| 519 | + 'relation' => 'OR', |
|
| 520 | + array( |
|
| 521 | + 'key' => '_course_woocommerce_product', |
|
| 522 | + 'value' => '-', |
|
| 523 | + 'compare' => '=', |
|
| 524 | + ), |
|
| 525 | + array( |
|
| 526 | + 'key' => '_course_woocommerce_product', |
|
| 527 | + 'value' => self::get_free_product_ids(), |
|
| 528 | + 'compare' => 'IN', |
|
| 529 | + ), |
|
| 530 | + ); |
|
| 422 | 531 | |
| 423 | - /** |
|
| 424 | - * Checks the cart to see if a course is in the cart. |
|
| 425 | - * |
|
| 426 | - * @param $course_id |
|
| 427 | - * @return bool |
|
| 428 | - */ |
|
| 429 | - public static function is_course_in_cart( $course_id ){ |
|
| 430 | - |
|
| 431 | - $wc_post_id = absint( get_post_meta( $course_id, '_course_woocommerce_product', true ) ); |
|
| 432 | - $user_course_status_id = Sensei_Utils::user_started_course( $course_id , get_current_user_id() ); |
|
| 433 | - |
|
| 434 | - if ( 0 < intval( $wc_post_id ) && ! $user_course_status_id ) { |
|
| 435 | - |
|
| 436 | - if ( self::is_product_in_cart( $wc_post_id ) ) { |
|
| 437 | - |
|
| 438 | - return true; |
|
| 439 | - |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - return false; |
|
| 445 | - |
|
| 446 | - }// is_course_in_cart |
|
| 447 | - |
|
| 448 | - /** |
|
| 449 | - * Check the cart to see if the product is in the cart |
|
| 450 | - * |
|
| 451 | - * @param $product_id |
|
| 452 | - * @return bool |
|
| 453 | - */ |
|
| 454 | - public static function is_product_in_cart( $product_id ){ |
|
| 455 | - |
|
| 456 | - if ( 0 < $product_id ) { |
|
| 457 | - |
|
| 458 | - $product = wc_get_product( $product_id ); |
|
| 459 | - |
|
| 460 | - $parent_id = ''; |
|
| 461 | - if( isset( $product->variation_id ) && 0 < intval( $product->variation_id ) ) { |
|
| 462 | - $wc_product_id = $product->parent->id; |
|
| 463 | - } |
|
| 464 | - foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { |
|
| 465 | - |
|
| 466 | - $cart_product = $values['data']; |
|
| 467 | - if( $product_id == $cart_product->id ) { |
|
| 468 | - |
|
| 469 | - return true; |
|
| 470 | - |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - } |
|
| 474 | - } // End If Statement |
|
| 475 | - |
|
| 476 | - return false; |
|
| 477 | - |
|
| 478 | - } // end is_product_in_car |
|
| 479 | - |
|
| 480 | - /** |
|
| 481 | - * Get all free WooCommerce products |
|
| 482 | - * |
|
| 483 | - * @since 1.9.0 |
|
| 484 | - * |
|
| 485 | - * @return array $free_products{ |
|
| 486 | - * @type int $wp_post_id |
|
| 487 | - * } |
|
| 488 | - */ |
|
| 489 | - public static function get_free_product_ids(){ |
|
| 490 | - |
|
| 491 | - return get_posts( array( |
|
| 492 | - 'post_type' => 'product', |
|
| 493 | - 'posts_per_page' => '1000', |
|
| 494 | - 'fields' => 'ids', |
|
| 495 | - 'meta_query'=> array( |
|
| 496 | - 'relation' => 'OR', |
|
| 497 | - array( |
|
| 498 | - 'key'=> '_regular_price', |
|
| 499 | - 'value' => 0, |
|
| 500 | - ), |
|
| 501 | - array( |
|
| 502 | - 'key'=> '_sale_price', |
|
| 503 | - 'value' => 0, |
|
| 504 | - ), |
|
| 505 | - ), |
|
| 506 | - )); |
|
| 507 | - |
|
| 508 | - }// end get free product query |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * The metat query for courses that are free |
|
| 512 | - * |
|
| 513 | - * @since 1.9.0 |
|
| 514 | - * @return array $wp_meta_query_param |
|
| 515 | - */ |
|
| 516 | - public static function get_free_courses_meta_query_args(){ |
|
| 517 | - |
|
| 518 | - return array( |
|
| 519 | - 'relation' => 'OR', |
|
| 520 | - array( |
|
| 521 | - 'key' => '_course_woocommerce_product', |
|
| 522 | - 'value' => '-', |
|
| 523 | - 'compare' => '=', |
|
| 524 | - ), |
|
| 525 | - array( |
|
| 526 | - 'key' => '_course_woocommerce_product', |
|
| 527 | - 'value' => self::get_free_product_ids(), |
|
| 528 | - 'compare' => 'IN', |
|
| 529 | - ), |
|
| 530 | - ); |
|
| 531 | - |
|
| 532 | - }// get_free_courses_meta_query |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * The metat query for courses that are free |
|
| 536 | - * |
|
| 537 | - * @since 1.9.0 |
|
| 538 | - * @return array $wp_query_meta_query_args_param |
|
| 539 | - */ |
|
| 540 | - public static function get_paid_courses_meta_query_args(){ |
|
| 541 | - |
|
| 542 | - $paid_product_ids = self::get_paid_product_ids(); |
|
| 543 | - |
|
| 544 | - return array( |
|
| 545 | - array( |
|
| 546 | - 'key' => '_course_woocommerce_product', |
|
| 547 | - // when empty we give a false post_id to ensure the caller doesn't get any courses for their |
|
| 548 | - // query |
|
| 549 | - 'value' => empty( $paid_product_ids )? '-1000' : $paid_product_ids, |
|
| 550 | - 'compare' => 'IN', |
|
| 551 | - ), |
|
| 552 | - ); |
|
| 553 | - |
|
| 554 | - }// get_free_courses_meta_query |
|
| 555 | - |
|
| 556 | - /** |
|
| 557 | - * The WordPress Query args |
|
| 558 | - * for paid products on sale |
|
| 559 | - * |
|
| 560 | - * @since 1.9.0 |
|
| 561 | - * @return array $product_query_args |
|
| 562 | - */ |
|
| 563 | - public static function get_paid_products_on_sale_query_args(){ |
|
| 564 | - |
|
| 565 | - $args = array( |
|
| 566 | - 'post_type' => 'product', |
|
| 567 | - 'posts_per_page' => 1000, |
|
| 568 | - 'orderby' => 'date', |
|
| 569 | - 'order' => 'DESC', |
|
| 570 | - 'suppress_filters' => 0 |
|
| 571 | - ); |
|
| 572 | - |
|
| 573 | - $args[ 'fields' ] = 'ids'; |
|
| 574 | - |
|
| 575 | - $args[ 'meta_query' ] = array( |
|
| 576 | - 'relation' => 'AND', |
|
| 577 | - array( |
|
| 578 | - 'key'=> '_regular_price', |
|
| 579 | - 'compare' => '>', |
|
| 580 | - 'value' => 0, |
|
| 581 | - ), |
|
| 582 | - array( |
|
| 583 | - 'key'=> '_sale_price', |
|
| 584 | - 'compare' => '>', |
|
| 585 | - 'value' => 0, |
|
| 586 | - ), |
|
| 587 | - ); |
|
| 588 | - |
|
| 589 | - return $args; |
|
| 590 | - |
|
| 591 | - } // get_paid_products_on_sale_query_args |
|
| 592 | - |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * Return the WordPress query args for |
|
| 596 | - * products not on sale but that is not a free |
|
| 597 | - * |
|
| 598 | - * @since 1.9.0 |
|
| 599 | - * |
|
| 600 | - * @return array |
|
| 601 | - */ |
|
| 602 | - public static function get_paid_products_not_on_sale_query_args(){ |
|
| 603 | - |
|
| 604 | - $args = array( |
|
| 605 | - 'post_type' => 'product', |
|
| 606 | - 'posts_per_page' => 1000, |
|
| 607 | - 'orderby' => 'date', |
|
| 608 | - 'order' => 'DESC', |
|
| 609 | - 'suppress_filters' => 0 |
|
| 610 | - ); |
|
| 611 | - |
|
| 612 | - $args[ 'fields' ] = 'ids'; |
|
| 613 | - $args[ 'meta_query' ] = array( |
|
| 614 | - 'relation' => 'AND', |
|
| 615 | - array( |
|
| 616 | - 'key'=> '_regular_price', |
|
| 617 | - 'compare' => '>', |
|
| 618 | - 'value' => 0, |
|
| 619 | - ), |
|
| 620 | - array( |
|
| 621 | - 'key'=> '_sale_price', |
|
| 622 | - 'compare' => '=', |
|
| 623 | - 'value' => '', |
|
| 624 | - ), |
|
| 625 | - ); |
|
| 626 | - |
|
| 627 | - return $args; |
|
| 628 | - |
|
| 629 | - |
|
| 630 | - } // get_paid_courses_meta_query |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * Get all WooCommerce non-free product id's |
|
| 634 | - * |
|
| 635 | - * @since 1.9.0 |
|
| 636 | - * |
|
| 637 | - * @return array $woocommerce_paid_product_ids |
|
| 638 | - */ |
|
| 639 | - public static function get_paid_product_ids(){ |
|
| 640 | - |
|
| 641 | - // get all the paid WooCommerce products that has regular |
|
| 642 | - // and sale price greater than 0 |
|
| 643 | - // will be used later to check for course with the id as meta |
|
| 644 | - $paid_product_ids_with_sale = get_posts( self::get_paid_products_on_sale_query_args() ); |
|
| 645 | - |
|
| 646 | - // get all the paid WooCommerce products that has regular price |
|
| 647 | - // greater than 0 without a sale price |
|
| 648 | - // will be used later to check for course with the id as meta |
|
| 649 | - $paid_product_ids_without_sale = get_posts( self::get_paid_products_not_on_sale_query_args() ); |
|
| 650 | - |
|
| 651 | - // combine products ID's with regular and sale price grater than zero and those without |
|
| 652 | - // sale but regular price greater than zero |
|
| 653 | - $woocommerce_paid_product_ids = array_merge( $paid_product_ids_with_sale, $paid_product_ids_without_sale ); |
|
| 654 | - |
|
| 655 | - // if |
|
| 656 | - if( empty($woocommerce_paid_product_ids) ){ |
|
| 657 | - return array( ); |
|
| 658 | - } |
|
| 659 | - return $woocommerce_paid_product_ids; |
|
| 660 | - |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Get all free courses. |
|
| 665 | - * |
|
| 666 | - * This course that have a WC product attached |
|
| 667 | - * that has a price or sale price of zero and |
|
| 668 | - * other courses with no WooCommerce products |
|
| 669 | - * attached. |
|
| 670 | - * |
|
| 671 | - * @since 1.9.0 |
|
| 672 | - * |
|
| 673 | - * @param array $args |
|
| 674 | - * @return array |
|
| 675 | - */ |
|
| 676 | - public static function get_free_courses( $args = array() ){ |
|
| 677 | - |
|
| 678 | - $free_course_query_args = Sensei_Course::get_default_query_args(); |
|
| 679 | - $free_course_query_args[ 'meta_query' ] = self::get_free_courses_meta_query_args(); |
|
| 680 | - |
|
| 681 | - if( !empty( $args ) ){ |
|
| 682 | - wp_parse_args( $args, $free_course_query_args ); |
|
| 683 | - } |
|
| 684 | - |
|
| 685 | - // don't show any paid courses |
|
| 686 | - $courses = self::get_paid_courses(); |
|
| 687 | - $ids = array(); |
|
| 688 | - foreach( $courses as $course ){ |
|
| 689 | - $ids[] = $course->ID; |
|
| 690 | - } |
|
| 691 | - $free_course_query_args[ 'post__not_in' ] = $ids; |
|
| 692 | - |
|
| 693 | - return get_posts( $free_course_query_args ); |
|
| 694 | - |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - /** |
|
| 698 | - * Return all products that are not free |
|
| 699 | - * |
|
| 700 | - * @since 1.9.0 |
|
| 701 | - * @param array $args override default arg values |
|
| 702 | - * |
|
| 703 | - * @return array |
|
| 704 | - */ |
|
| 705 | - public static function get_paid_courses( $args = array() ){ |
|
| 706 | - |
|
| 707 | - $paid_course_query_args = Sensei_Course::get_default_query_args(); |
|
| 708 | - |
|
| 709 | - $paid_course_query_args[ 'meta_query' ] = self::get_paid_courses_meta_query_args(); |
|
| 710 | - |
|
| 711 | - if( !empty( $args ) ){ |
|
| 712 | - wp_parse_args( $args, $paid_course_query_args ); |
|
| 713 | - } |
|
| 714 | - |
|
| 715 | - return get_posts( $paid_course_query_args ); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - /** |
|
| 719 | - * Show the WooCommerce add to cart button for the current course |
|
| 720 | - * |
|
| 721 | - * The function will only show the button if |
|
| 722 | - * 1- the user can buy the course |
|
| 723 | - * 2- if they have completed their pre-requisite |
|
| 724 | - * 3- if the course has a valid product attached |
|
| 725 | - * |
|
| 726 | - * @since 1.9.0 |
|
| 727 | - * @param int $course_id |
|
| 728 | - * @return string $html markup for the button or nothing if user not allowed to buy |
|
| 729 | - */ |
|
| 730 | - public static function the_add_to_cart_button_html( $course_id ){ |
|
| 731 | - |
|
| 732 | - if ( ! Sensei_Course::is_prerequisite_complete( $course_id ) || self::is_course_in_cart( $course_id ) ) { |
|
| 733 | - return ''; |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - $wc_post_id = self::get_course_product_id( $course_id ); |
|
| 737 | - |
|
| 738 | - // Check if customer purchased the product |
|
| 739 | - if ( self::has_customer_bought_product( get_current_user_id(), $wc_post_id ) |
|
| 740 | - || empty( $wc_post_id ) ) { |
|
| 741 | - |
|
| 742 | - return ''; |
|
| 743 | - |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - // based on simple.php in WC templates/single-product/add-to-cart/ |
|
| 747 | - // Get the product |
|
| 748 | - $product = self::get_product_object( $wc_post_id ); |
|
| 749 | - |
|
| 750 | - // do not show the button for invalid products, non purchasable products, out |
|
| 751 | - // of stock product or if course is already in cart |
|
| 752 | - if ( ! isset ( $product ) |
|
| 753 | - || ! is_object( $product ) |
|
| 754 | - || ! $product->is_purchasable() |
|
| 755 | - || ! $product->is_in_stock() |
|
| 756 | - || self::is_course_in_cart( $wc_post_id ) ) { |
|
| 757 | - |
|
| 758 | - return ''; |
|
| 759 | - |
|
| 760 | - } |
|
| 761 | - |
|
| 762 | - // |
|
| 763 | - // button output: |
|
| 764 | - // |
|
| 765 | - ?> |
|
| 532 | + }// get_free_courses_meta_query |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * The metat query for courses that are free |
|
| 536 | + * |
|
| 537 | + * @since 1.9.0 |
|
| 538 | + * @return array $wp_query_meta_query_args_param |
|
| 539 | + */ |
|
| 540 | + public static function get_paid_courses_meta_query_args(){ |
|
| 541 | + |
|
| 542 | + $paid_product_ids = self::get_paid_product_ids(); |
|
| 543 | + |
|
| 544 | + return array( |
|
| 545 | + array( |
|
| 546 | + 'key' => '_course_woocommerce_product', |
|
| 547 | + // when empty we give a false post_id to ensure the caller doesn't get any courses for their |
|
| 548 | + // query |
|
| 549 | + 'value' => empty( $paid_product_ids )? '-1000' : $paid_product_ids, |
|
| 550 | + 'compare' => 'IN', |
|
| 551 | + ), |
|
| 552 | + ); |
|
| 553 | + |
|
| 554 | + }// get_free_courses_meta_query |
|
| 555 | + |
|
| 556 | + /** |
|
| 557 | + * The WordPress Query args |
|
| 558 | + * for paid products on sale |
|
| 559 | + * |
|
| 560 | + * @since 1.9.0 |
|
| 561 | + * @return array $product_query_args |
|
| 562 | + */ |
|
| 563 | + public static function get_paid_products_on_sale_query_args(){ |
|
| 564 | + |
|
| 565 | + $args = array( |
|
| 566 | + 'post_type' => 'product', |
|
| 567 | + 'posts_per_page' => 1000, |
|
| 568 | + 'orderby' => 'date', |
|
| 569 | + 'order' => 'DESC', |
|
| 570 | + 'suppress_filters' => 0 |
|
| 571 | + ); |
|
| 572 | + |
|
| 573 | + $args[ 'fields' ] = 'ids'; |
|
| 574 | + |
|
| 575 | + $args[ 'meta_query' ] = array( |
|
| 576 | + 'relation' => 'AND', |
|
| 577 | + array( |
|
| 578 | + 'key'=> '_regular_price', |
|
| 579 | + 'compare' => '>', |
|
| 580 | + 'value' => 0, |
|
| 581 | + ), |
|
| 582 | + array( |
|
| 583 | + 'key'=> '_sale_price', |
|
| 584 | + 'compare' => '>', |
|
| 585 | + 'value' => 0, |
|
| 586 | + ), |
|
| 587 | + ); |
|
| 588 | + |
|
| 589 | + return $args; |
|
| 590 | + |
|
| 591 | + } // get_paid_products_on_sale_query_args |
|
| 592 | + |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * Return the WordPress query args for |
|
| 596 | + * products not on sale but that is not a free |
|
| 597 | + * |
|
| 598 | + * @since 1.9.0 |
|
| 599 | + * |
|
| 600 | + * @return array |
|
| 601 | + */ |
|
| 602 | + public static function get_paid_products_not_on_sale_query_args(){ |
|
| 603 | + |
|
| 604 | + $args = array( |
|
| 605 | + 'post_type' => 'product', |
|
| 606 | + 'posts_per_page' => 1000, |
|
| 607 | + 'orderby' => 'date', |
|
| 608 | + 'order' => 'DESC', |
|
| 609 | + 'suppress_filters' => 0 |
|
| 610 | + ); |
|
| 611 | + |
|
| 612 | + $args[ 'fields' ] = 'ids'; |
|
| 613 | + $args[ 'meta_query' ] = array( |
|
| 614 | + 'relation' => 'AND', |
|
| 615 | + array( |
|
| 616 | + 'key'=> '_regular_price', |
|
| 617 | + 'compare' => '>', |
|
| 618 | + 'value' => 0, |
|
| 619 | + ), |
|
| 620 | + array( |
|
| 621 | + 'key'=> '_sale_price', |
|
| 622 | + 'compare' => '=', |
|
| 623 | + 'value' => '', |
|
| 624 | + ), |
|
| 625 | + ); |
|
| 626 | + |
|
| 627 | + return $args; |
|
| 628 | + |
|
| 629 | + |
|
| 630 | + } // get_paid_courses_meta_query |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * Get all WooCommerce non-free product id's |
|
| 634 | + * |
|
| 635 | + * @since 1.9.0 |
|
| 636 | + * |
|
| 637 | + * @return array $woocommerce_paid_product_ids |
|
| 638 | + */ |
|
| 639 | + public static function get_paid_product_ids(){ |
|
| 640 | + |
|
| 641 | + // get all the paid WooCommerce products that has regular |
|
| 642 | + // and sale price greater than 0 |
|
| 643 | + // will be used later to check for course with the id as meta |
|
| 644 | + $paid_product_ids_with_sale = get_posts( self::get_paid_products_on_sale_query_args() ); |
|
| 645 | + |
|
| 646 | + // get all the paid WooCommerce products that has regular price |
|
| 647 | + // greater than 0 without a sale price |
|
| 648 | + // will be used later to check for course with the id as meta |
|
| 649 | + $paid_product_ids_without_sale = get_posts( self::get_paid_products_not_on_sale_query_args() ); |
|
| 650 | + |
|
| 651 | + // combine products ID's with regular and sale price grater than zero and those without |
|
| 652 | + // sale but regular price greater than zero |
|
| 653 | + $woocommerce_paid_product_ids = array_merge( $paid_product_ids_with_sale, $paid_product_ids_without_sale ); |
|
| 654 | + |
|
| 655 | + // if |
|
| 656 | + if( empty($woocommerce_paid_product_ids) ){ |
|
| 657 | + return array( ); |
|
| 658 | + } |
|
| 659 | + return $woocommerce_paid_product_ids; |
|
| 660 | + |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * Get all free courses. |
|
| 665 | + * |
|
| 666 | + * This course that have a WC product attached |
|
| 667 | + * that has a price or sale price of zero and |
|
| 668 | + * other courses with no WooCommerce products |
|
| 669 | + * attached. |
|
| 670 | + * |
|
| 671 | + * @since 1.9.0 |
|
| 672 | + * |
|
| 673 | + * @param array $args |
|
| 674 | + * @return array |
|
| 675 | + */ |
|
| 676 | + public static function get_free_courses( $args = array() ){ |
|
| 677 | + |
|
| 678 | + $free_course_query_args = Sensei_Course::get_default_query_args(); |
|
| 679 | + $free_course_query_args[ 'meta_query' ] = self::get_free_courses_meta_query_args(); |
|
| 680 | + |
|
| 681 | + if( !empty( $args ) ){ |
|
| 682 | + wp_parse_args( $args, $free_course_query_args ); |
|
| 683 | + } |
|
| 684 | + |
|
| 685 | + // don't show any paid courses |
|
| 686 | + $courses = self::get_paid_courses(); |
|
| 687 | + $ids = array(); |
|
| 688 | + foreach( $courses as $course ){ |
|
| 689 | + $ids[] = $course->ID; |
|
| 690 | + } |
|
| 691 | + $free_course_query_args[ 'post__not_in' ] = $ids; |
|
| 692 | + |
|
| 693 | + return get_posts( $free_course_query_args ); |
|
| 694 | + |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + /** |
|
| 698 | + * Return all products that are not free |
|
| 699 | + * |
|
| 700 | + * @since 1.9.0 |
|
| 701 | + * @param array $args override default arg values |
|
| 702 | + * |
|
| 703 | + * @return array |
|
| 704 | + */ |
|
| 705 | + public static function get_paid_courses( $args = array() ){ |
|
| 706 | + |
|
| 707 | + $paid_course_query_args = Sensei_Course::get_default_query_args(); |
|
| 708 | + |
|
| 709 | + $paid_course_query_args[ 'meta_query' ] = self::get_paid_courses_meta_query_args(); |
|
| 710 | + |
|
| 711 | + if( !empty( $args ) ){ |
|
| 712 | + wp_parse_args( $args, $paid_course_query_args ); |
|
| 713 | + } |
|
| 714 | + |
|
| 715 | + return get_posts( $paid_course_query_args ); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + /** |
|
| 719 | + * Show the WooCommerce add to cart button for the current course |
|
| 720 | + * |
|
| 721 | + * The function will only show the button if |
|
| 722 | + * 1- the user can buy the course |
|
| 723 | + * 2- if they have completed their pre-requisite |
|
| 724 | + * 3- if the course has a valid product attached |
|
| 725 | + * |
|
| 726 | + * @since 1.9.0 |
|
| 727 | + * @param int $course_id |
|
| 728 | + * @return string $html markup for the button or nothing if user not allowed to buy |
|
| 729 | + */ |
|
| 730 | + public static function the_add_to_cart_button_html( $course_id ){ |
|
| 731 | + |
|
| 732 | + if ( ! Sensei_Course::is_prerequisite_complete( $course_id ) || self::is_course_in_cart( $course_id ) ) { |
|
| 733 | + return ''; |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + $wc_post_id = self::get_course_product_id( $course_id ); |
|
| 737 | + |
|
| 738 | + // Check if customer purchased the product |
|
| 739 | + if ( self::has_customer_bought_product( get_current_user_id(), $wc_post_id ) |
|
| 740 | + || empty( $wc_post_id ) ) { |
|
| 741 | + |
|
| 742 | + return ''; |
|
| 743 | + |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + // based on simple.php in WC templates/single-product/add-to-cart/ |
|
| 747 | + // Get the product |
|
| 748 | + $product = self::get_product_object( $wc_post_id ); |
|
| 749 | + |
|
| 750 | + // do not show the button for invalid products, non purchasable products, out |
|
| 751 | + // of stock product or if course is already in cart |
|
| 752 | + if ( ! isset ( $product ) |
|
| 753 | + || ! is_object( $product ) |
|
| 754 | + || ! $product->is_purchasable() |
|
| 755 | + || ! $product->is_in_stock() |
|
| 756 | + || self::is_course_in_cart( $wc_post_id ) ) { |
|
| 757 | + |
|
| 758 | + return ''; |
|
| 759 | + |
|
| 760 | + } |
|
| 761 | + |
|
| 762 | + // |
|
| 763 | + // button output: |
|
| 764 | + // |
|
| 765 | + ?> |
|
| 766 | 766 | |
| 767 | 767 | <form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" |
| 768 | 768 | class="cart" |
@@ -791,83 +791,83 @@ discard block |
||
| 791 | 791 | <button type="submit" class="single_add_to_cart_button button alt"> |
| 792 | 792 | <?php $button_text = $product->get_price_html() . ' - ' . __( 'Purchase this Course', 'woothemes-sensei' ); ?> |
| 793 | 793 | <?php |
| 794 | - /** |
|
| 795 | - * Filter Add to Cart button text |
|
| 796 | - * |
|
| 797 | - * @since 1.9.1 |
|
| 798 | - * |
|
| 799 | - * @param string $button_text |
|
| 800 | - */ |
|
| 801 | - echo apply_filters( 'sensei_wc_single_add_to_cart_button_text', $button_text ); |
|
| 802 | - ?> |
|
| 794 | + /** |
|
| 795 | + * Filter Add to Cart button text |
|
| 796 | + * |
|
| 797 | + * @since 1.9.1 |
|
| 798 | + * |
|
| 799 | + * @param string $button_text |
|
| 800 | + */ |
|
| 801 | + echo apply_filters( 'sensei_wc_single_add_to_cart_button_text', $button_text ); |
|
| 802 | + ?> |
|
| 803 | 803 | </button> |
| 804 | 804 | |
| 805 | 805 | </form> |
| 806 | 806 | |
| 807 | 807 | <?php |
| 808 | - } // end the_add_to_cart_button_html |
|
| 808 | + } // end the_add_to_cart_button_html |
|
| 809 | 809 | |
| 810 | - /** |
|
| 811 | - * Alter the no permissions message on the single course page |
|
| 812 | - * Changes the message to a WooCommerce specific message. |
|
| 813 | - * |
|
| 814 | - * @since 1.9.0 |
|
| 815 | - * |
|
| 816 | - * @param $message |
|
| 817 | - * @param $post_id |
|
| 818 | - * |
|
| 819 | - * @return string $message |
|
| 820 | - */ |
|
| 821 | - public static function alter_no_permissions_message( $message, $post_id ){ |
|
| 810 | + /** |
|
| 811 | + * Alter the no permissions message on the single course page |
|
| 812 | + * Changes the message to a WooCommerce specific message. |
|
| 813 | + * |
|
| 814 | + * @since 1.9.0 |
|
| 815 | + * |
|
| 816 | + * @param $message |
|
| 817 | + * @param $post_id |
|
| 818 | + * |
|
| 819 | + * @return string $message |
|
| 820 | + */ |
|
| 821 | + public static function alter_no_permissions_message( $message, $post_id ){ |
|
| 822 | 822 | |
| 823 | - if( empty( $post_id ) || 'course'!=get_post_type( $post_id ) ){ |
|
| 824 | - return $message; |
|
| 825 | - } |
|
| 823 | + if( empty( $post_id ) || 'course'!=get_post_type( $post_id ) ){ |
|
| 824 | + return $message; |
|
| 825 | + } |
|
| 826 | 826 | |
| 827 | - $product_id = self::get_course_product_id( $post_id ); |
|
| 827 | + $product_id = self::get_course_product_id( $post_id ); |
|
| 828 | 828 | |
| 829 | - if( ! $product_id |
|
| 830 | - || ! self::has_customer_bought_product( get_current_user_id(),$product_id ) ){ |
|
| 829 | + if( ! $product_id |
|
| 830 | + || ! self::has_customer_bought_product( get_current_user_id(),$product_id ) ){ |
|
| 831 | 831 | |
| 832 | - return $message; |
|
| 832 | + return $message; |
|
| 833 | 833 | |
| 834 | - } |
|
| 834 | + } |
|
| 835 | 835 | |
| 836 | - ob_start(); |
|
| 837 | - self::the_course_no_permissions_message( $post_id ); |
|
| 838 | - $woocommerce_course_no_permissions_message = ob_get_clean(); |
|
| 836 | + ob_start(); |
|
| 837 | + self::the_course_no_permissions_message( $post_id ); |
|
| 838 | + $woocommerce_course_no_permissions_message = ob_get_clean(); |
|
| 839 | 839 | |
| 840 | - return $woocommerce_course_no_permissions_message ; |
|
| 840 | + return $woocommerce_course_no_permissions_message ; |
|
| 841 | 841 | |
| 842 | - } |
|
| 843 | - /** |
|
| 844 | - * Show the no permissions message when a user is logged in |
|
| 845 | - * and have not yet purchased the current course |
|
| 846 | - * |
|
| 847 | - * @since 1.9.0 |
|
| 848 | - */ |
|
| 849 | - public static function the_course_no_permissions_message( $course_id ){ |
|
| 842 | + } |
|
| 843 | + /** |
|
| 844 | + * Show the no permissions message when a user is logged in |
|
| 845 | + * and have not yet purchased the current course |
|
| 846 | + * |
|
| 847 | + * @since 1.9.0 |
|
| 848 | + */ |
|
| 849 | + public static function the_course_no_permissions_message( $course_id ){ |
|
| 850 | 850 | |
| 851 | - // login link |
|
| 852 | - $my_courses_page_id = intval( Sensei()->settings->settings[ 'my_course_page' ] ); |
|
| 853 | - $login_link = '<a href="' . esc_url( get_permalink( $my_courses_page_id ) ) . '">' . __( 'log in', 'woothemes-sensei' ) . '</a>'; |
|
| 854 | - $wc_product_id = self::get_course_product_id( $course_id ); |
|
| 851 | + // login link |
|
| 852 | + $my_courses_page_id = intval( Sensei()->settings->settings[ 'my_course_page' ] ); |
|
| 853 | + $login_link = '<a href="' . esc_url( get_permalink( $my_courses_page_id ) ) . '">' . __( 'log in', 'woothemes-sensei' ) . '</a>'; |
|
| 854 | + $wc_product_id = self::get_course_product_id( $course_id ); |
|
| 855 | 855 | |
| 856 | - if ( self::is_product_in_cart( $wc_product_id ) ) { |
|
| 856 | + if ( self::is_product_in_cart( $wc_product_id ) ) { |
|
| 857 | 857 | |
| 858 | - $cart_link = '<a href="' . wc_get_checkout_url() . '" title="' . __( 'Checkout','woocommerce' ) . '">' . __( 'checkout', 'woocommerce' ) . '</a>'; |
|
| 858 | + $cart_link = '<a href="' . wc_get_checkout_url() . '" title="' . __( 'Checkout','woocommerce' ) . '">' . __( 'checkout', 'woocommerce' ) . '</a>'; |
|
| 859 | 859 | |
| 860 | - $message = sprintf( __( 'This course is already in your cart, please proceed to %1$s, to gain access.', 'woothemes-sensei' ), $cart_link ); |
|
| 861 | - ?> |
|
| 860 | + $message = sprintf( __( 'This course is already in your cart, please proceed to %1$s, to gain access.', 'woothemes-sensei' ), $cart_link ); |
|
| 861 | + ?> |
|
| 862 | 862 | <span class="add-to-cart-login"> |
| 863 | 863 | <?php echo $message; ?> |
| 864 | 864 | </span> |
| 865 | 865 | |
| 866 | 866 | <?php |
| 867 | 867 | |
| 868 | - } elseif ( is_user_logged_in() ) { |
|
| 868 | + } elseif ( is_user_logged_in() ) { |
|
| 869 | 869 | |
| 870 | - ?> |
|
| 870 | + ?> |
|
| 871 | 871 | <style> |
| 872 | 872 | .sensei-message.alert { |
| 873 | 873 | display: none; |
@@ -876,345 +876,345 @@ discard block |
||
| 876 | 876 | |
| 877 | 877 | <?php |
| 878 | 878 | |
| 879 | - } else { |
|
| 880 | - $message = sprintf( __( 'Or %1$s to access your purchased courses', 'woothemes-sensei' ), $login_link ); |
|
| 881 | - ?> |
|
| 879 | + } else { |
|
| 880 | + $message = sprintf( __( 'Or %1$s to access your purchased courses', 'woothemes-sensei' ), $login_link ); |
|
| 881 | + ?> |
|
| 882 | 882 | <span class="add-to-cart-login"> |
| 883 | 883 | <?php echo $message; ?> |
| 884 | 884 | </span> |
| 885 | 885 | |
| 886 | 886 | <?php |
| 887 | - } |
|
| 888 | - } |
|
| 887 | + } |
|
| 888 | + } |
|
| 889 | 889 | |
| 890 | - /** |
|
| 891 | - * Checks if a user has bought a product item. |
|
| 892 | - * |
|
| 893 | - * @since 1.9.0 |
|
| 894 | - * |
|
| 895 | - * @param int $user_id |
|
| 896 | - * @param int $product_id |
|
| 897 | - * |
|
| 898 | - * @return bool |
|
| 899 | - */ |
|
| 900 | - public static function has_customer_bought_product ( $user_id, $product_id ){ |
|
| 901 | - |
|
| 902 | - $orders = self::get_user_product_orders( $user_id, $product_id ); |
|
| 903 | - |
|
| 904 | - foreach ( $orders as $order_id ) { |
|
| 905 | - |
|
| 906 | - $order = new WC_Order( $order_id->ID ); |
|
| 907 | - |
|
| 908 | - // wc-active is the subscriptions complete status |
|
| 909 | - if ( ! in_array( $order->post_status, array( 'wc-processing', 'wc-completed' ) ) |
|
| 910 | - || ! ( 0 < sizeof( $order->get_items() ) ) ){ |
|
| 911 | - |
|
| 912 | - continue; |
|
| 890 | + /** |
|
| 891 | + * Checks if a user has bought a product item. |
|
| 892 | + * |
|
| 893 | + * @since 1.9.0 |
|
| 894 | + * |
|
| 895 | + * @param int $user_id |
|
| 896 | + * @param int $product_id |
|
| 897 | + * |
|
| 898 | + * @return bool |
|
| 899 | + */ |
|
| 900 | + public static function has_customer_bought_product ( $user_id, $product_id ){ |
|
| 913 | 901 | |
| 914 | - } |
|
| 902 | + $orders = self::get_user_product_orders( $user_id, $product_id ); |
|
| 915 | 903 | |
| 916 | - foreach( $order->get_items() as $item ) { |
|
| 904 | + foreach ( $orders as $order_id ) { |
|
| 917 | 905 | |
| 918 | - // Check if user has bought product |
|
| 919 | - if ( $item['product_id'] == $product_id || $item['variation_id'] == $product_id ) { |
|
| 906 | + $order = new WC_Order( $order_id->ID ); |
|
| 920 | 907 | |
| 921 | - // Check if user has an active subscription for product |
|
| 922 | - if( class_exists( 'WC_Subscriptions_Manager' ) ) { |
|
| 923 | - $sub_key = wcs_get_subscription( $order ); |
|
| 924 | - if( $sub_key ) { |
|
| 925 | - $sub = wcs_get_subscription( $sub_key ); |
|
| 926 | - if( $sub && isset( $sub['status'] ) ) { |
|
| 927 | - if( 'active' == $sub['status'] ) { |
|
| 928 | - return true; |
|
| 929 | - } else { |
|
| 930 | - return false; |
|
| 931 | - } |
|
| 932 | - } |
|
| 933 | - } |
|
| 934 | - } |
|
| 908 | + // wc-active is the subscriptions complete status |
|
| 909 | + if ( ! in_array( $order->post_status, array( 'wc-processing', 'wc-completed' ) ) |
|
| 910 | + || ! ( 0 < sizeof( $order->get_items() ) ) ){ |
|
| 935 | 911 | |
| 936 | - // Customer has bought product |
|
| 937 | - return true; |
|
| 938 | - } // End If Statement |
|
| 912 | + continue; |
|
| 939 | 913 | |
| 940 | - } // End For each item |
|
| 914 | + } |
|
| 941 | 915 | |
| 942 | - } // End For each order |
|
| 916 | + foreach( $order->get_items() as $item ) { |
|
| 943 | 917 | |
| 944 | - // default is no order |
|
| 945 | - return false; |
|
| 918 | + // Check if user has bought product |
|
| 919 | + if ( $item['product_id'] == $product_id || $item['variation_id'] == $product_id ) { |
|
| 946 | 920 | |
| 947 | - } // end has customer bought product |
|
| 921 | + // Check if user has an active subscription for product |
|
| 922 | + if( class_exists( 'WC_Subscriptions_Manager' ) ) { |
|
| 923 | + $sub_key = wcs_get_subscription( $order ); |
|
| 924 | + if( $sub_key ) { |
|
| 925 | + $sub = wcs_get_subscription( $sub_key ); |
|
| 926 | + if( $sub && isset( $sub['status'] ) ) { |
|
| 927 | + if( 'active' == $sub['status'] ) { |
|
| 928 | + return true; |
|
| 929 | + } else { |
|
| 930 | + return false; |
|
| 931 | + } |
|
| 932 | + } |
|
| 933 | + } |
|
| 934 | + } |
|
| 948 | 935 | |
| 949 | - /** |
|
| 950 | - * Return the product id for the given course |
|
| 951 | - * |
|
| 952 | - * @since 1.9.0 |
|
| 953 | - * |
|
| 954 | - * @param int $course_id |
|
| 955 | - * |
|
| 956 | - * @return string $woocommerce_product_id or false if none exist |
|
| 957 | - * |
|
| 958 | - */ |
|
| 959 | - public static function get_course_product_id( $course_id ){ |
|
| 936 | + // Customer has bought product |
|
| 937 | + return true; |
|
| 938 | + } // End If Statement |
|
| 960 | 939 | |
| 961 | - $product_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 940 | + } // End For each item |
|
| 962 | 941 | |
| 963 | - if( empty( $product_id ) || 'product' != get_post_type( $product_id ) ){ |
|
| 964 | - return false; |
|
| 965 | - } |
|
| 942 | + } // End For each order |
|
| 966 | 943 | |
| 967 | - return $product_id; |
|
| 944 | + // default is no order |
|
| 945 | + return false; |
|
| 968 | 946 | |
| 969 | - } |
|
| 947 | + } // end has customer bought product |
|
| 970 | 948 | |
| 971 | - /** |
|
| 972 | - * Alter the body classes adding WooCommerce to the body |
|
| 973 | - * |
|
| 974 | - * Speciall cases where this is needed is template no-permissions.php |
|
| 975 | - * |
|
| 976 | - * @param array $classes |
|
| 977 | - * @return array |
|
| 978 | - */ |
|
| 979 | - public static function add_woocommerce_body_class( $classes ){ |
|
| 949 | + /** |
|
| 950 | + * Return the product id for the given course |
|
| 951 | + * |
|
| 952 | + * @since 1.9.0 |
|
| 953 | + * |
|
| 954 | + * @param int $course_id |
|
| 955 | + * |
|
| 956 | + * @return string $woocommerce_product_id or false if none exist |
|
| 957 | + * |
|
| 958 | + */ |
|
| 959 | + public static function get_course_product_id( $course_id ){ |
|
| 980 | 960 | |
| 981 | - if( ! in_array( 'woocommerce', $classes ) && defined( 'SENSEI_NO_PERMISSION' ) && SENSEI_NO_PERMISSION ){ |
|
| 961 | + $product_id = get_post_meta( $course_id, '_course_woocommerce_product', true ); |
|
| 982 | 962 | |
| 983 | - $classes[] ='woocommerce'; |
|
| 963 | + if( empty( $product_id ) || 'product' != get_post_type( $product_id ) ){ |
|
| 964 | + return false; |
|
| 965 | + } |
|
| 984 | 966 | |
| 985 | - } |
|
| 967 | + return $product_id; |
|
| 986 | 968 | |
| 987 | - return $classes; |
|
| 969 | + } |
|
| 988 | 970 | |
| 989 | - } |
|
| 971 | + /** |
|
| 972 | + * Alter the body classes adding WooCommerce to the body |
|
| 973 | + * |
|
| 974 | + * Speciall cases where this is needed is template no-permissions.php |
|
| 975 | + * |
|
| 976 | + * @param array $classes |
|
| 977 | + * @return array |
|
| 978 | + */ |
|
| 979 | + public static function add_woocommerce_body_class( $classes ){ |
|
| 990 | 980 | |
| 991 | - /** |
|
| 992 | - * Responds to when a subscription product is purchased |
|
| 993 | - * |
|
| 994 | - * @since 1.2.0 |
|
| 995 | - * @since 1.9.0 move to class Sensei_WC |
|
| 996 | - * |
|
| 997 | - * @param WC_Order $order |
|
| 998 | - * |
|
| 999 | - * @return void |
|
| 1000 | - */ |
|
| 1001 | - public static function activate_subscription( $order ) { |
|
| 981 | + if( ! in_array( 'woocommerce', $classes ) && defined( 'SENSEI_NO_PERMISSION' ) && SENSEI_NO_PERMISSION ){ |
|
| 1002 | 982 | |
| 1003 | - $order_user = get_user_by('id', $order->user_id); |
|
| 1004 | - $user['ID'] = $order_user->ID; |
|
| 1005 | - $user['user_login'] = $order_user->user_login; |
|
| 1006 | - $user['user_email'] = $order_user->user_email; |
|
| 1007 | - $user['user_url'] = $order_user->user_url; |
|
| 983 | + $classes[] ='woocommerce'; |
|
| 1008 | 984 | |
| 1009 | - // Run through each product ordered |
|
| 1010 | - if ( ! sizeof($order->get_items() )>0 ) { |
|
| 985 | + } |
|
| 1011 | 986 | |
| 1012 | - return; |
|
| 987 | + return $classes; |
|
| 1013 | 988 | |
| 1014 | - } |
|
| 989 | + } |
|
| 1015 | 990 | |
| 1016 | - foreach($order->get_items() as $item) { |
|
| 991 | + /** |
|
| 992 | + * Responds to when a subscription product is purchased |
|
| 993 | + * |
|
| 994 | + * @since 1.2.0 |
|
| 995 | + * @since 1.9.0 move to class Sensei_WC |
|
| 996 | + * |
|
| 997 | + * @param WC_Order $order |
|
| 998 | + * |
|
| 999 | + * @return void |
|
| 1000 | + */ |
|
| 1001 | + public static function activate_subscription( $order ) { |
|
| 1017 | 1002 | |
| 1018 | - $product_type = ''; |
|
| 1003 | + $order_user = get_user_by('id', $order->user_id); |
|
| 1004 | + $user['ID'] = $order_user->ID; |
|
| 1005 | + $user['user_login'] = $order_user->user_login; |
|
| 1006 | + $user['user_email'] = $order_user->user_email; |
|
| 1007 | + $user['user_url'] = $order_user->user_url; |
|
| 1019 | 1008 | |
| 1020 | - if (isset($item['variation_id']) && $item['variation_id'] > 0) { |
|
| 1009 | + // Run through each product ordered |
|
| 1010 | + if ( ! sizeof($order->get_items() )>0 ) { |
|
| 1021 | 1011 | |
| 1022 | - $item_id = $item['variation_id']; |
|
| 1023 | - $product_type = 'subscription_variation'; |
|
| 1012 | + return; |
|
| 1024 | 1013 | |
| 1025 | - } else { |
|
| 1014 | + } |
|
| 1026 | 1015 | |
| 1027 | - $item_id = $item['product_id']; |
|
| 1016 | + foreach($order->get_items() as $item) { |
|
| 1028 | 1017 | |
| 1029 | - } // End If Statement |
|
| 1018 | + $product_type = ''; |
|
| 1030 | 1019 | |
| 1031 | - $_product = self::get_product_object( $item_id, $product_type ); |
|
| 1020 | + if (isset($item['variation_id']) && $item['variation_id'] > 0) { |
|
| 1032 | 1021 | |
| 1033 | - // Get courses that use the WC product |
|
| 1034 | - $courses = array(); |
|
| 1022 | + $item_id = $item['variation_id']; |
|
| 1023 | + $product_type = 'subscription_variation'; |
|
| 1035 | 1024 | |
| 1036 | - if ( ! in_array( $product_type, self::get_subscription_types() ) ) { |
|
| 1025 | + } else { |
|
| 1037 | 1026 | |
| 1038 | - $courses = Sensei()->course->get_product_courses( $item_id ); |
|
| 1027 | + $item_id = $item['product_id']; |
|
| 1039 | 1028 | |
| 1040 | - } // End If Statement |
|
| 1029 | + } // End If Statement |
|
| 1041 | 1030 | |
| 1042 | - // Loop and add the user to the course. |
|
| 1043 | - foreach ( $courses as $course_item ){ |
|
| 1031 | + $_product = self::get_product_object( $item_id, $product_type ); |
|
| 1044 | 1032 | |
| 1045 | - Sensei_Utils::user_start_course( intval( $user['ID'] ), $course_item->ID ); |
|
| 1033 | + // Get courses that use the WC product |
|
| 1034 | + $courses = array(); |
|
| 1046 | 1035 | |
| 1047 | - } // End For Loop |
|
| 1036 | + if ( ! in_array( $product_type, self::get_subscription_types() ) ) { |
|
| 1048 | 1037 | |
| 1049 | - } // End For Loop |
|
| 1038 | + $courses = Sensei()->course->get_product_courses( $item_id ); |
|
| 1050 | 1039 | |
| 1051 | - } // End activate_subscription() |
|
| 1040 | + } // End If Statement |
|
| 1052 | 1041 | |
| 1053 | - /** |
|
| 1054 | - * Adds detail to to the WooCommerce order |
|
| 1055 | - * |
|
| 1056 | - * @since 1.4.5 |
|
| 1057 | - * @since 1.9.0 function moved to class Sensei_WC and renamed from sensei_woocommerce_email_course_details to email_course_details |
|
| 1058 | - * |
|
| 1059 | - * @param WC_Order $order |
|
| 1060 | - * |
|
| 1061 | - * @return void |
|
| 1062 | - */ |
|
| 1063 | - public static function email_course_details( $order ){ |
|
| 1042 | + // Loop and add the user to the course. |
|
| 1043 | + foreach ( $courses as $course_item ){ |
|
| 1064 | 1044 | |
| 1065 | - global $woocommerce; |
|
| 1045 | + Sensei_Utils::user_start_course( intval( $user['ID'] ), $course_item->ID ); |
|
| 1066 | 1046 | |
| 1067 | - // exit early if not wc-completed or wc-processing |
|
| 1068 | - if( 'wc-completed' != $order->post_status |
|
| 1069 | - && 'wc-processing' != $order->post_status ) { |
|
| 1070 | - return; |
|
| 1071 | - } |
|
| 1047 | + } // End For Loop |
|
| 1072 | 1048 | |
| 1073 | - $order_items = $order->get_items(); |
|
| 1074 | - $order_id = $order->id; |
|
| 1049 | + } // End For Loop |
|
| 1075 | 1050 | |
| 1076 | - //If object have items go through them all to find course |
|
| 1077 | - if ( 0 < sizeof( $order_items ) ) { |
|
| 1051 | + } // End activate_subscription() |
|
| 1078 | 1052 | |
| 1079 | - $course_details_html = '<h2>' . __( 'Course details', 'woothemes-sensei' ) . '</h2>'; |
|
| 1080 | - $order_contains_courses = false; |
|
| 1053 | + /** |
|
| 1054 | + * Adds detail to to the WooCommerce order |
|
| 1055 | + * |
|
| 1056 | + * @since 1.4.5 |
|
| 1057 | + * @since 1.9.0 function moved to class Sensei_WC and renamed from sensei_woocommerce_email_course_details to email_course_details |
|
| 1058 | + * |
|
| 1059 | + * @param WC_Order $order |
|
| 1060 | + * |
|
| 1061 | + * @return void |
|
| 1062 | + */ |
|
| 1063 | + public static function email_course_details( $order ){ |
|
| 1081 | 1064 | |
| 1065 | + global $woocommerce; |
|
| 1082 | 1066 | |
| 1083 | - foreach ( $order_items as $item ) { |
|
| 1067 | + // exit early if not wc-completed or wc-processing |
|
| 1068 | + if( 'wc-completed' != $order->post_status |
|
| 1069 | + && 'wc-processing' != $order->post_status ) { |
|
| 1070 | + return; |
|
| 1071 | + } |
|
| 1084 | 1072 | |
| 1085 | - $product_type = ''; |
|
| 1086 | - if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1087 | - // If item has variation_id then its from variation |
|
| 1088 | - $item_id = $item['variation_id']; |
|
| 1089 | - $product_type = 'variation'; |
|
| 1090 | - } else { |
|
| 1091 | - // If not its real product set its id to item_id |
|
| 1092 | - $item_id = $item['product_id']; |
|
| 1093 | - } // End If Statement |
|
| 1073 | + $order_items = $order->get_items(); |
|
| 1074 | + $order_id = $order->id; |
|
| 1094 | 1075 | |
| 1095 | - $user_id = get_post_meta( $order_id, '_customer_user', true ); |
|
| 1076 | + //If object have items go through them all to find course |
|
| 1077 | + if ( 0 < sizeof( $order_items ) ) { |
|
| 1096 | 1078 | |
| 1097 | - if( $user_id ) { |
|
| 1079 | + $course_details_html = '<h2>' . __( 'Course details', 'woothemes-sensei' ) . '</h2>'; |
|
| 1080 | + $order_contains_courses = false; |
|
| 1098 | 1081 | |
| 1099 | - // Get all courses for product |
|
| 1100 | - $args = array( |
|
| 1101 | - 'posts_per_page' => -1, |
|
| 1102 | - 'post_type' => 'course', |
|
| 1103 | - 'meta_query' => array( |
|
| 1104 | - array( |
|
| 1105 | - 'key' => '_course_woocommerce_product', |
|
| 1106 | - 'value' => $item_id |
|
| 1107 | - ) |
|
| 1108 | - ), |
|
| 1109 | - 'orderby' => 'menu_order date', |
|
| 1110 | - 'order' => 'ASC', |
|
| 1111 | - ); |
|
| 1112 | - $courses = get_posts( $args ); |
|
| 1113 | 1082 | |
| 1114 | - if( $courses && count( $courses ) > 0 ) { |
|
| 1083 | + foreach ( $order_items as $item ) { |
|
| 1115 | 1084 | |
| 1116 | - foreach( $courses as $course ) { |
|
| 1085 | + $product_type = ''; |
|
| 1086 | + if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1087 | + // If item has variation_id then its from variation |
|
| 1088 | + $item_id = $item['variation_id']; |
|
| 1089 | + $product_type = 'variation'; |
|
| 1090 | + } else { |
|
| 1091 | + // If not its real product set its id to item_id |
|
| 1092 | + $item_id = $item['product_id']; |
|
| 1093 | + } // End If Statement |
|
| 1117 | 1094 | |
| 1118 | - $title = $course->post_title; |
|
| 1119 | - $permalink = get_permalink( $course->ID ); |
|
| 1120 | - $order_contains_courses = true; |
|
| 1121 | - $course_details_html .= '<p><strong>' . sprintf( __( 'View course: %1$s', 'woothemes-sensei' ), '</strong><a href="' . esc_url( $permalink ) . '">' . $title . '</a>' ) . '</p>'; |
|
| 1122 | - } |
|
| 1095 | + $user_id = get_post_meta( $order_id, '_customer_user', true ); |
|
| 1123 | 1096 | |
| 1097 | + if( $user_id ) { |
|
| 1124 | 1098 | |
| 1125 | - } // end if has courses |
|
| 1099 | + // Get all courses for product |
|
| 1100 | + $args = array( |
|
| 1101 | + 'posts_per_page' => -1, |
|
| 1102 | + 'post_type' => 'course', |
|
| 1103 | + 'meta_query' => array( |
|
| 1104 | + array( |
|
| 1105 | + 'key' => '_course_woocommerce_product', |
|
| 1106 | + 'value' => $item_id |
|
| 1107 | + ) |
|
| 1108 | + ), |
|
| 1109 | + 'orderby' => 'menu_order date', |
|
| 1110 | + 'order' => 'ASC', |
|
| 1111 | + ); |
|
| 1112 | + $courses = get_posts( $args ); |
|
| 1126 | 1113 | |
| 1127 | - } // end if $userPid |
|
| 1114 | + if( $courses && count( $courses ) > 0 ) { |
|
| 1128 | 1115 | |
| 1129 | - } // end for each order item |
|
| 1116 | + foreach( $courses as $course ) { |
|
| 1130 | 1117 | |
| 1131 | - // Output Course details |
|
| 1132 | - if( $order_contains_courses ){ |
|
| 1118 | + $title = $course->post_title; |
|
| 1119 | + $permalink = get_permalink( $course->ID ); |
|
| 1120 | + $order_contains_courses = true; |
|
| 1121 | + $course_details_html .= '<p><strong>' . sprintf( __( 'View course: %1$s', 'woothemes-sensei' ), '</strong><a href="' . esc_url( $permalink ) . '">' . $title . '</a>' ) . '</p>'; |
|
| 1122 | + } |
|
| 1133 | 1123 | |
| 1134 | - echo $course_details_html; |
|
| 1135 | 1124 | |
| 1136 | - } |
|
| 1125 | + } // end if has courses |
|
| 1137 | 1126 | |
| 1127 | + } // end if $userPid |
|
| 1138 | 1128 | |
| 1139 | - } // end if order items not empty |
|
| 1129 | + } // end for each order item |
|
| 1140 | 1130 | |
| 1141 | - }// end email_course_details |
|
| 1131 | + // Output Course details |
|
| 1132 | + if( $order_contains_courses ){ |
|
| 1142 | 1133 | |
| 1143 | - /** |
|
| 1144 | - * sensei_woocommerce_complete_order description |
|
| 1145 | - * @since 1.0.3 |
|
| 1146 | - * @access public |
|
| 1147 | - * @param int $order_id WC order ID |
|
| 1148 | - * @return void |
|
| 1149 | - */ |
|
| 1150 | - public static function complete_order ( $order_id = 0 ) { |
|
| 1134 | + echo $course_details_html; |
|
| 1151 | 1135 | |
| 1152 | - $order_user = array(); |
|
| 1136 | + } |
|
| 1153 | 1137 | |
| 1154 | - // Check for WooCommerce |
|
| 1155 | - if ( Sensei_WC::is_woocommerce_active() && ( 0 < $order_id ) ) { |
|
| 1156 | - // Get order object |
|
| 1157 | - $order = new WC_Order( $order_id ); |
|
| 1158 | 1138 | |
| 1159 | - if ( ! in_array( $order->get_status(), array( 'complete', 'processing' ) ) ) { |
|
| 1139 | + } // end if order items not empty |
|
| 1160 | 1140 | |
| 1161 | - return; |
|
| 1141 | + }// end email_course_details |
|
| 1162 | 1142 | |
| 1163 | - } |
|
| 1143 | + /** |
|
| 1144 | + * sensei_woocommerce_complete_order description |
|
| 1145 | + * @since 1.0.3 |
|
| 1146 | + * @access public |
|
| 1147 | + * @param int $order_id WC order ID |
|
| 1148 | + * @return void |
|
| 1149 | + */ |
|
| 1150 | + public static function complete_order ( $order_id = 0 ) { |
|
| 1151 | + |
|
| 1152 | + $order_user = array(); |
|
| 1164 | 1153 | |
| 1165 | - $user = get_user_by( 'id', $order->get_user_id() ); |
|
| 1166 | - $order_user['ID'] = $user->ID; |
|
| 1167 | - $order_user['user_login'] = $user->user_login; |
|
| 1168 | - $order_user['user_email'] = $user->user_email; |
|
| 1169 | - $order_user['user_url'] = $user->user_url; |
|
| 1170 | - // Run through each product ordered |
|
| 1171 | - if ( 0 < sizeof( $order->get_items() ) ) { |
|
| 1154 | + // Check for WooCommerce |
|
| 1155 | + if ( Sensei_WC::is_woocommerce_active() && ( 0 < $order_id ) ) { |
|
| 1156 | + // Get order object |
|
| 1157 | + $order = new WC_Order( $order_id ); |
|
| 1172 | 1158 | |
| 1173 | - foreach( $order->get_items() as $item ) { |
|
| 1159 | + if ( ! in_array( $order->get_status(), array( 'complete', 'processing' ) ) ) { |
|
| 1174 | 1160 | |
| 1175 | - $product_type = ''; |
|
| 1176 | - if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1161 | + return; |
|
| 1162 | + |
|
| 1163 | + } |
|
| 1177 | 1164 | |
| 1178 | - $item_id = $item['variation_id']; |
|
| 1179 | - $product_type = 'variation'; |
|
| 1165 | + $user = get_user_by( 'id', $order->get_user_id() ); |
|
| 1166 | + $order_user['ID'] = $user->ID; |
|
| 1167 | + $order_user['user_login'] = $user->user_login; |
|
| 1168 | + $order_user['user_email'] = $user->user_email; |
|
| 1169 | + $order_user['user_url'] = $user->user_url; |
|
| 1170 | + // Run through each product ordered |
|
| 1171 | + if ( 0 < sizeof( $order->get_items() ) ) { |
|
| 1180 | 1172 | |
| 1181 | - } else { |
|
| 1173 | + foreach( $order->get_items() as $item ) { |
|
| 1182 | 1174 | |
| 1183 | - $item_id = $item['product_id']; |
|
| 1175 | + $product_type = ''; |
|
| 1176 | + if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1184 | 1177 | |
| 1185 | - } // End If Statement |
|
| 1178 | + $item_id = $item['variation_id']; |
|
| 1179 | + $product_type = 'variation'; |
|
| 1186 | 1180 | |
| 1187 | - $_product = Sensei_WC::get_product_object( $item_id, $product_type ); |
|
| 1181 | + } else { |
|
| 1188 | 1182 | |
| 1189 | - // Get courses that use the WC product |
|
| 1190 | - $courses = Sensei()->course->get_product_courses( $_product->id ); |
|
| 1183 | + $item_id = $item['product_id']; |
|
| 1191 | 1184 | |
| 1192 | - // Loop and update those courses |
|
| 1193 | - foreach ( $courses as $course_item ) { |
|
| 1185 | + } // End If Statement |
|
| 1194 | 1186 | |
| 1195 | - $update_course = self::course_update( $course_item->ID, $order_user ); |
|
| 1187 | + $_product = Sensei_WC::get_product_object( $item_id, $product_type ); |
|
| 1196 | 1188 | |
| 1197 | - } // End For Loop |
|
| 1189 | + // Get courses that use the WC product |
|
| 1190 | + $courses = Sensei()->course->get_product_courses( $_product->id ); |
|
| 1198 | 1191 | |
| 1199 | - } // End For Loop |
|
| 1192 | + // Loop and update those courses |
|
| 1193 | + foreach ( $courses as $course_item ) { |
|
| 1200 | 1194 | |
| 1201 | - } // End If Statement |
|
| 1202 | - // Add meta to indicate that payment has been completed successfully |
|
| 1203 | - update_post_meta( $order_id, 'sensei_payment_complete', '1' ); |
|
| 1195 | + $update_course = self::course_update( $course_item->ID, $order_user ); |
|
| 1204 | 1196 | |
| 1205 | - } // End If Statement |
|
| 1197 | + } // End For Loop |
|
| 1206 | 1198 | |
| 1207 | - } // End sensei_woocommerce_complete_order() |
|
| 1199 | + } // End For Loop |
|
| 1208 | 1200 | |
| 1209 | - /** |
|
| 1210 | - * Responds to when an order is cancelled. |
|
| 1211 | - * |
|
| 1212 | - * @since 1.2.0 |
|
| 1213 | - * @since 1.9.0 Move function to the Sensei_WC class |
|
| 1214 | - * @param integer| WC_Order $order_id order ID |
|
| 1215 | - * @return void |
|
| 1216 | - */ |
|
| 1217 | - public static function cancel_order ( $order_id ) { |
|
| 1201 | + } // End If Statement |
|
| 1202 | + // Add meta to indicate that payment has been completed successfully |
|
| 1203 | + update_post_meta( $order_id, 'sensei_payment_complete', '1' ); |
|
| 1204 | + |
|
| 1205 | + } // End If Statement |
|
| 1206 | + |
|
| 1207 | + } // End sensei_woocommerce_complete_order() |
|
| 1208 | + |
|
| 1209 | + /** |
|
| 1210 | + * Responds to when an order is cancelled. |
|
| 1211 | + * |
|
| 1212 | + * @since 1.2.0 |
|
| 1213 | + * @since 1.9.0 Move function to the Sensei_WC class |
|
| 1214 | + * @param integer| WC_Order $order_id order ID |
|
| 1215 | + * @return void |
|
| 1216 | + */ |
|
| 1217 | + public static function cancel_order ( $order_id ) { |
|
| 1218 | 1218 | |
| 1219 | 1219 | // Get order object |
| 1220 | 1220 | if( is_object( $order_id ) ){ |
@@ -1232,345 +1232,345 @@ discard block |
||
| 1232 | 1232 | |
| 1233 | 1233 | } |
| 1234 | 1234 | |
| 1235 | - // Run through each product ordered |
|
| 1236 | - if ( 0 < sizeof( $order->get_items() ) ) { |
|
| 1235 | + // Run through each product ordered |
|
| 1236 | + if ( 0 < sizeof( $order->get_items() ) ) { |
|
| 1237 | 1237 | |
| 1238 | - // Get order user |
|
| 1239 | - $user_id = $order->__get( 'user_id' ); |
|
| 1238 | + // Get order user |
|
| 1239 | + $user_id = $order->__get( 'user_id' ); |
|
| 1240 | 1240 | |
| 1241 | - foreach( $order->get_items() as $item ) { |
|
| 1241 | + foreach( $order->get_items() as $item ) { |
|
| 1242 | 1242 | |
| 1243 | - $product_type = ''; |
|
| 1244 | - if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1243 | + $product_type = ''; |
|
| 1244 | + if ( isset( $item['variation_id'] ) && ( 0 < $item['variation_id'] ) ) { |
|
| 1245 | 1245 | |
| 1246 | - $item_id = $item['variation_id']; |
|
| 1247 | - $product_type = 'variation'; |
|
| 1246 | + $item_id = $item['variation_id']; |
|
| 1247 | + $product_type = 'variation'; |
|
| 1248 | 1248 | |
| 1249 | - } else { |
|
| 1249 | + } else { |
|
| 1250 | 1250 | |
| 1251 | - $item_id = $item['product_id']; |
|
| 1251 | + $item_id = $item['product_id']; |
|
| 1252 | 1252 | |
| 1253 | - } // End If Statement |
|
| 1253 | + } // End If Statement |
|
| 1254 | 1254 | |
| 1255 | - $_product = Sensei_WC::get_product_object( $item_id, $product_type ); |
|
| 1255 | + $_product = Sensei_WC::get_product_object( $item_id, $product_type ); |
|
| 1256 | 1256 | |
| 1257 | - // Get courses that use the WC product |
|
| 1258 | - $courses = array(); |
|
| 1259 | - $courses = Sensei()->course->get_product_courses( $item_id ); |
|
| 1257 | + // Get courses that use the WC product |
|
| 1258 | + $courses = array(); |
|
| 1259 | + $courses = Sensei()->course->get_product_courses( $item_id ); |
|
| 1260 | 1260 | |
| 1261 | - // Loop and update those courses |
|
| 1262 | - foreach ($courses as $course_item){ |
|
| 1261 | + // Loop and update those courses |
|
| 1262 | + foreach ($courses as $course_item){ |
|
| 1263 | 1263 | |
| 1264 | - if( self::has_customer_bought_product( $user_id, $course_item->ID ) ){ |
|
| 1265 | - continue; |
|
| 1266 | - } |
|
| 1267 | - // Check and Remove course from courses user meta |
|
| 1268 | - $dataset_changes = Sensei_Utils::sensei_remove_user_from_course( $course_item->ID, $user_id ); |
|
| 1264 | + if( self::has_customer_bought_product( $user_id, $course_item->ID ) ){ |
|
| 1265 | + continue; |
|
| 1266 | + } |
|
| 1267 | + // Check and Remove course from courses user meta |
|
| 1268 | + $dataset_changes = Sensei_Utils::sensei_remove_user_from_course( $course_item->ID, $user_id ); |
|
| 1269 | 1269 | |
| 1270 | - } // End For Loop |
|
| 1270 | + } // End For Loop |
|
| 1271 | 1271 | |
| 1272 | - } // End For Loop |
|
| 1272 | + } // End For Loop |
|
| 1273 | 1273 | |
| 1274 | - } // End If Statement |
|
| 1274 | + } // End If Statement |
|
| 1275 | 1275 | |
| 1276 | - } // End sensei_woocommerce_cancel_order() |
|
| 1276 | + } // End sensei_woocommerce_cancel_order() |
|
| 1277 | 1277 | |
| 1278 | - /** |
|
| 1279 | - * Returns the WooCommerce Product Object |
|
| 1280 | - * |
|
| 1281 | - * The code caters for pre and post WooCommerce 2.2 installations. |
|
| 1282 | - * |
|
| 1283 | - * @since 1.1.1 |
|
| 1284 | - * @access public |
|
| 1285 | - * @param integer $wc_product_id Product ID or Variation ID |
|
| 1286 | - * @param string $product_type '' or 'variation' |
|
| 1287 | - * @return WC_Product $wc_product_object |
|
| 1288 | - */ |
|
| 1289 | - public static function get_product_object ( $wc_product_id = 0, $product_type = '' ) { |
|
| 1278 | + /** |
|
| 1279 | + * Returns the WooCommerce Product Object |
|
| 1280 | + * |
|
| 1281 | + * The code caters for pre and post WooCommerce 2.2 installations. |
|
| 1282 | + * |
|
| 1283 | + * @since 1.1.1 |
|
| 1284 | + * @access public |
|
| 1285 | + * @param integer $wc_product_id Product ID or Variation ID |
|
| 1286 | + * @param string $product_type '' or 'variation' |
|
| 1287 | + * @return WC_Product $wc_product_object |
|
| 1288 | + */ |
|
| 1289 | + public static function get_product_object ( $wc_product_id = 0, $product_type = '' ) { |
|
| 1290 | 1290 | |
| 1291 | - $wc_product_object = false; |
|
| 1292 | - if ( 0 < intval( $wc_product_id ) ) { |
|
| 1291 | + $wc_product_object = false; |
|
| 1292 | + if ( 0 < intval( $wc_product_id ) ) { |
|
| 1293 | 1293 | |
| 1294 | - // Get the product |
|
| 1295 | - if ( function_exists( 'wc_get_product' ) ) { |
|
| 1294 | + // Get the product |
|
| 1295 | + if ( function_exists( 'wc_get_product' ) ) { |
|
| 1296 | 1296 | |
| 1297 | - $wc_product_object = wc_get_product( $wc_product_id ); // Post WC 2.3 |
|
| 1297 | + $wc_product_object = wc_get_product( $wc_product_id ); // Post WC 2.3 |
|
| 1298 | 1298 | |
| 1299 | - } elseif ( function_exists( 'get_product' ) ) { |
|
| 1299 | + } elseif ( function_exists( 'get_product' ) ) { |
|
| 1300 | 1300 | |
| 1301 | - $wc_product_object = get_product( $wc_product_id ); // Post WC 2.0 |
|
| 1301 | + $wc_product_object = get_product( $wc_product_id ); // Post WC 2.0 |
|
| 1302 | 1302 | |
| 1303 | - } else { |
|
| 1303 | + } else { |
|
| 1304 | 1304 | |
| 1305 | - // Pre WC 2.0 |
|
| 1306 | - if ( 'variation' == $product_type || 'subscription_variation' == $product_type ) { |
|
| 1305 | + // Pre WC 2.0 |
|
| 1306 | + if ( 'variation' == $product_type || 'subscription_variation' == $product_type ) { |
|
| 1307 | 1307 | |
| 1308 | - $wc_product_object = new WC_Product_Variation( $wc_product_id ); |
|
| 1308 | + $wc_product_object = new WC_Product_Variation( $wc_product_id ); |
|
| 1309 | 1309 | |
| 1310 | - } else { |
|
| 1310 | + } else { |
|
| 1311 | 1311 | |
| 1312 | - $wc_product_object = new WC_Product( $wc_product_id ); |
|
| 1312 | + $wc_product_object = new WC_Product( $wc_product_id ); |
|
| 1313 | 1313 | |
| 1314 | - } // End If Statement |
|
| 1314 | + } // End If Statement |
|
| 1315 | 1315 | |
| 1316 | - } // End If Statement |
|
| 1316 | + } // End If Statement |
|
| 1317 | 1317 | |
| 1318 | - } // End If Statement |
|
| 1318 | + } // End If Statement |
|
| 1319 | 1319 | |
| 1320 | - return $wc_product_object; |
|
| 1320 | + return $wc_product_object; |
|
| 1321 | 1321 | |
| 1322 | - } // End sensei_get_woocommerce_product_object() |
|
| 1322 | + } // End sensei_get_woocommerce_product_object() |
|
| 1323 | 1323 | |
| 1324 | - /** |
|
| 1325 | - * If customer has purchased the course, update Sensei to indicate that they are taking the course. |
|
| 1326 | - * |
|
| 1327 | - * @since 1.0.0 |
|
| 1328 | - * @since 1.9.0 move to class Sensei_WC |
|
| 1329 | - * |
|
| 1330 | - * @param int $course_id (default: 0) |
|
| 1331 | - * @param array/Object $order_user (default: array()) Specific user's data. |
|
| 1332 | - * |
|
| 1333 | - * @return bool|int |
|
| 1334 | - */ |
|
| 1335 | - public static function course_update ( $course_id = 0, $order_user = array() ) { |
|
| 1324 | + /** |
|
| 1325 | + * If customer has purchased the course, update Sensei to indicate that they are taking the course. |
|
| 1326 | + * |
|
| 1327 | + * @since 1.0.0 |
|
| 1328 | + * @since 1.9.0 move to class Sensei_WC |
|
| 1329 | + * |
|
| 1330 | + * @param int $course_id (default: 0) |
|
| 1331 | + * @param array/Object $order_user (default: array()) Specific user's data. |
|
| 1332 | + * |
|
| 1333 | + * @return bool|int |
|
| 1334 | + */ |
|
| 1335 | + public static function course_update ( $course_id = 0, $order_user = array() ) { |
|
| 1336 | 1336 | |
| 1337 | - global $current_user; |
|
| 1337 | + global $current_user; |
|
| 1338 | 1338 | |
| 1339 | - if ( ! isset( $current_user ) || !$current_user->ID > 0 ) return false; |
|
| 1339 | + if ( ! isset( $current_user ) || !$current_user->ID > 0 ) return false; |
|
| 1340 | 1340 | |
| 1341 | - $data_update = false; |
|
| 1341 | + $data_update = false; |
|
| 1342 | 1342 | |
| 1343 | - // Get the product ID |
|
| 1344 | - $wc_post_id = get_post_meta( intval( $course_id ), '_course_woocommerce_product', true ); |
|
| 1343 | + // Get the product ID |
|
| 1344 | + $wc_post_id = get_post_meta( intval( $course_id ), '_course_woocommerce_product', true ); |
|
| 1345 | 1345 | |
| 1346 | - // Check if in the admin |
|
| 1347 | - if ( is_admin() ) { |
|
| 1346 | + // Check if in the admin |
|
| 1347 | + if ( is_admin() ) { |
|
| 1348 | 1348 | |
| 1349 | - $user_login = $order_user['user_login']; |
|
| 1350 | - $user_email = $order_user['user_email']; |
|
| 1351 | - $user_url = $order_user['user_url']; |
|
| 1352 | - $user_id = $order_user['ID']; |
|
| 1349 | + $user_login = $order_user['user_login']; |
|
| 1350 | + $user_email = $order_user['user_email']; |
|
| 1351 | + $user_url = $order_user['user_url']; |
|
| 1352 | + $user_id = $order_user['ID']; |
|
| 1353 | 1353 | |
| 1354 | - } else { |
|
| 1354 | + } else { |
|
| 1355 | 1355 | |
| 1356 | - $user_login = $current_user->user_login; |
|
| 1357 | - $user_email = $current_user->user_email; |
|
| 1358 | - $user_url = $current_user->user_url; |
|
| 1359 | - $user_id = $current_user->ID; |
|
| 1356 | + $user_login = $current_user->user_login; |
|
| 1357 | + $user_email = $current_user->user_email; |
|
| 1358 | + $user_url = $current_user->user_url; |
|
| 1359 | + $user_id = $current_user->ID; |
|
| 1360 | 1360 | |
| 1361 | - } // End If Statement |
|
| 1361 | + } // End If Statement |
|
| 1362 | 1362 | |
| 1363 | - // This doesn't appear to be purely WooCommerce related. Should it be in a separate function? |
|
| 1364 | - $course_prerequisite_id = (int) get_post_meta( $course_id, '_course_prerequisite', true ); |
|
| 1365 | - if( 0 < absint( $course_prerequisite_id ) ) { |
|
| 1363 | + // This doesn't appear to be purely WooCommerce related. Should it be in a separate function? |
|
| 1364 | + $course_prerequisite_id = (int) get_post_meta( $course_id, '_course_prerequisite', true ); |
|
| 1365 | + if( 0 < absint( $course_prerequisite_id ) ) { |
|
| 1366 | 1366 | |
| 1367 | - $prereq_course_complete = Sensei_Utils::user_completed_course( $course_prerequisite_id, intval( $user_id ) ); |
|
| 1368 | - if ( ! $prereq_course_complete ) { |
|
| 1367 | + $prereq_course_complete = Sensei_Utils::user_completed_course( $course_prerequisite_id, intval( $user_id ) ); |
|
| 1368 | + if ( ! $prereq_course_complete ) { |
|
| 1369 | 1369 | |
| 1370 | - // Remove all course user meta |
|
| 1371 | - return Sensei_Utils::sensei_remove_user_from_course( $course_id, $user_id ); |
|
| 1370 | + // Remove all course user meta |
|
| 1371 | + return Sensei_Utils::sensei_remove_user_from_course( $course_id, $user_id ); |
|
| 1372 | 1372 | |
| 1373 | - } |
|
| 1374 | - } |
|
| 1373 | + } |
|
| 1374 | + } |
|
| 1375 | 1375 | |
| 1376 | - $is_user_taking_course = Sensei_Utils::user_started_course( intval( $course_id ), intval( $user_id ) ); |
|
| 1376 | + $is_user_taking_course = Sensei_Utils::user_started_course( intval( $course_id ), intval( $user_id ) ); |
|
| 1377 | 1377 | |
| 1378 | - if ( ! $is_user_taking_course |
|
| 1379 | - && Sensei_WC::is_woocommerce_active() |
|
| 1380 | - && 0 < $wc_post_id |
|
| 1381 | - && Sensei_WC::has_customer_bought_product( $user_id, $wc_post_id ) ) { |
|
| 1378 | + if ( ! $is_user_taking_course |
|
| 1379 | + && Sensei_WC::is_woocommerce_active() |
|
| 1380 | + && 0 < $wc_post_id |
|
| 1381 | + && Sensei_WC::has_customer_bought_product( $user_id, $wc_post_id ) ) { |
|
| 1382 | 1382 | |
| 1383 | - $activity_logged = Sensei_Utils::user_start_course( intval( $user_id ), intval( $course_id ) ); |
|
| 1383 | + $activity_logged = Sensei_Utils::user_start_course( intval( $user_id ), intval( $course_id ) ); |
|
| 1384 | 1384 | |
| 1385 | - if ( true == $activity_logged ) { |
|
| 1385 | + if ( true == $activity_logged ) { |
|
| 1386 | 1386 | |
| 1387 | - $is_user_taking_course = true; |
|
| 1387 | + $is_user_taking_course = true; |
|
| 1388 | 1388 | |
| 1389 | - } // End If Statement |
|
| 1389 | + } // End If Statement |
|
| 1390 | 1390 | |
| 1391 | - }// end if is user taking course |
|
| 1391 | + }// end if is user taking course |
|
| 1392 | 1392 | |
| 1393 | - return $is_user_taking_course; |
|
| 1393 | + return $is_user_taking_course; |
|
| 1394 | 1394 | |
| 1395 | - } // End course_update() |
|
| 1395 | + } // End course_update() |
|
| 1396 | 1396 | |
| 1397 | - /** |
|
| 1398 | - * Disable guest checkout if a course product is in the cart |
|
| 1399 | - * |
|
| 1400 | - * @since 1.1.0 |
|
| 1401 | - * @since 1.9.0 move to class Sensei_WC |
|
| 1402 | - * |
|
| 1403 | - * @param boolean $guest_checkout Current guest checkout setting |
|
| 1404 | - * |
|
| 1405 | - * @return boolean Modified guest checkout setting |
|
| 1406 | - */ |
|
| 1407 | - public static function disable_guest_checkout( $guest_checkout ) { |
|
| 1397 | + /** |
|
| 1398 | + * Disable guest checkout if a course product is in the cart |
|
| 1399 | + * |
|
| 1400 | + * @since 1.1.0 |
|
| 1401 | + * @since 1.9.0 move to class Sensei_WC |
|
| 1402 | + * |
|
| 1403 | + * @param boolean $guest_checkout Current guest checkout setting |
|
| 1404 | + * |
|
| 1405 | + * @return boolean Modified guest checkout setting |
|
| 1406 | + */ |
|
| 1407 | + public static function disable_guest_checkout( $guest_checkout ) { |
|
| 1408 | 1408 | |
| 1409 | - if( ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 1409 | + if( ! is_admin() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
|
| 1410 | 1410 | |
| 1411 | - if( isset( WC()->cart->cart_contents ) && count( WC()->cart->cart_contents ) > 0 ) { |
|
| 1411 | + if( isset( WC()->cart->cart_contents ) && count( WC()->cart->cart_contents ) > 0 ) { |
|
| 1412 | 1412 | |
| 1413 | - foreach( WC()->cart->cart_contents as $cart_key => $product ) { |
|
| 1414 | - if( isset( $product['product_id'] ) ) { |
|
| 1413 | + foreach( WC()->cart->cart_contents as $cart_key => $product ) { |
|
| 1414 | + if( isset( $product['product_id'] ) ) { |
|
| 1415 | 1415 | |
| 1416 | - $args = array( |
|
| 1417 | - 'posts_per_page' => -1, |
|
| 1418 | - 'post_type' => 'course', |
|
| 1419 | - 'meta_query' => array( |
|
| 1420 | - array( |
|
| 1421 | - 'key' => '_course_woocommerce_product', |
|
| 1422 | - 'value' => $product['product_id'] |
|
| 1423 | - ) |
|
| 1424 | - ) |
|
| 1425 | - ); |
|
| 1416 | + $args = array( |
|
| 1417 | + 'posts_per_page' => -1, |
|
| 1418 | + 'post_type' => 'course', |
|
| 1419 | + 'meta_query' => array( |
|
| 1420 | + array( |
|
| 1421 | + 'key' => '_course_woocommerce_product', |
|
| 1422 | + 'value' => $product['product_id'] |
|
| 1423 | + ) |
|
| 1424 | + ) |
|
| 1425 | + ); |
|
| 1426 | 1426 | |
| 1427 | - $posts = get_posts( $args ); |
|
| 1427 | + $posts = get_posts( $args ); |
|
| 1428 | 1428 | |
| 1429 | - if( $posts && count( $posts ) > 0 ) { |
|
| 1429 | + if( $posts && count( $posts ) > 0 ) { |
|
| 1430 | 1430 | |
| 1431 | - foreach( $posts as $course ) { |
|
| 1432 | - $guest_checkout = ''; |
|
| 1433 | - break; |
|
| 1431 | + foreach( $posts as $course ) { |
|
| 1432 | + $guest_checkout = ''; |
|
| 1433 | + break; |
|
| 1434 | 1434 | |
| 1435 | - } |
|
| 1436 | - } |
|
| 1435 | + } |
|
| 1436 | + } |
|
| 1437 | 1437 | |
| 1438 | - } |
|
| 1438 | + } |
|
| 1439 | 1439 | |
| 1440 | - } |
|
| 1440 | + } |
|
| 1441 | 1441 | |
| 1442 | - } |
|
| 1443 | - } |
|
| 1442 | + } |
|
| 1443 | + } |
|
| 1444 | 1444 | |
| 1445 | - return $guest_checkout; |
|
| 1445 | + return $guest_checkout; |
|
| 1446 | 1446 | |
| 1447 | - }// end disable_guest_checkout |
|
| 1447 | + }// end disable_guest_checkout |
|
| 1448 | 1448 | |
| 1449 | - /** |
|
| 1450 | - * Change order status with virtual products to completed |
|
| 1451 | - * |
|
| 1452 | - * @since 1.1.0 |
|
| 1453 | - * @since 1.9.0 move to class Sensei_WC |
|
| 1454 | - * |
|
| 1455 | - * @param string $order_status |
|
| 1456 | - * @param int $order_id |
|
| 1457 | - * |
|
| 1458 | - * @return string |
|
| 1459 | - **/ |
|
| 1460 | - public static function virtual_order_payment_complete( $order_status, $order_id ) { |
|
| 1449 | + /** |
|
| 1450 | + * Change order status with virtual products to completed |
|
| 1451 | + * |
|
| 1452 | + * @since 1.1.0 |
|
| 1453 | + * @since 1.9.0 move to class Sensei_WC |
|
| 1454 | + * |
|
| 1455 | + * @param string $order_status |
|
| 1456 | + * @param int $order_id |
|
| 1457 | + * |
|
| 1458 | + * @return string |
|
| 1459 | + **/ |
|
| 1460 | + public static function virtual_order_payment_complete( $order_status, $order_id ) { |
|
| 1461 | 1461 | |
| 1462 | - $order = new WC_Order( $order_id ); |
|
| 1462 | + $order = new WC_Order( $order_id ); |
|
| 1463 | 1463 | |
| 1464 | - if ( ! isset ( $order ) ) return ''; |
|
| 1464 | + if ( ! isset ( $order ) ) return ''; |
|
| 1465 | 1465 | |
| 1466 | - if ( $order_status == 'wc-processing' && ( $order->post_status == 'wc-on-hold' || $order->post_status == 'wc-pending' || $order->post_status == 'wc-failed' ) ) { |
|
| 1466 | + if ( $order_status == 'wc-processing' && ( $order->post_status == 'wc-on-hold' || $order->post_status == 'wc-pending' || $order->post_status == 'wc-failed' ) ) { |
|
| 1467 | 1467 | |
| 1468 | - $virtual_order = true; |
|
| 1468 | + $virtual_order = true; |
|
| 1469 | 1469 | |
| 1470 | - if ( count( $order->get_items() ) > 0 ) { |
|
| 1470 | + if ( count( $order->get_items() ) > 0 ) { |
|
| 1471 | 1471 | |
| 1472 | - foreach( $order->get_items() as $item ) { |
|
| 1472 | + foreach( $order->get_items() as $item ) { |
|
| 1473 | 1473 | |
| 1474 | - if ( $item['product_id'] > 0 ) { |
|
| 1475 | - $_product = $order->get_product_from_item( $item ); |
|
| 1476 | - if ( ! $_product->is_virtual() ) { |
|
| 1474 | + if ( $item['product_id'] > 0 ) { |
|
| 1475 | + $_product = $order->get_product_from_item( $item ); |
|
| 1476 | + if ( ! $_product->is_virtual() ) { |
|
| 1477 | 1477 | |
| 1478 | - $virtual_order = false; |
|
| 1479 | - break; |
|
| 1478 | + $virtual_order = false; |
|
| 1479 | + break; |
|
| 1480 | 1480 | |
| 1481 | - } // End If Statement |
|
| 1481 | + } // End If Statement |
|
| 1482 | 1482 | |
| 1483 | - } // End If Statement |
|
| 1483 | + } // End If Statement |
|
| 1484 | 1484 | |
| 1485 | - } // End For Loop |
|
| 1485 | + } // End For Loop |
|
| 1486 | 1486 | |
| 1487 | - } // End If Statement |
|
| 1487 | + } // End If Statement |
|
| 1488 | 1488 | |
| 1489 | - // virtual order, mark as completed |
|
| 1490 | - if ( $virtual_order ) { |
|
| 1489 | + // virtual order, mark as completed |
|
| 1490 | + if ( $virtual_order ) { |
|
| 1491 | 1491 | |
| 1492 | - return 'completed'; |
|
| 1492 | + return 'completed'; |
|
| 1493 | 1493 | |
| 1494 | - } // End If Statement |
|
| 1494 | + } // End If Statement |
|
| 1495 | 1495 | |
| 1496 | - } // End If Statement |
|
| 1496 | + } // End If Statement |
|
| 1497 | 1497 | |
| 1498 | - return $order_status; |
|
| 1498 | + return $order_status; |
|
| 1499 | 1499 | |
| 1500 | - }// end virtual_order_payment_complete |
|
| 1500 | + }// end virtual_order_payment_complete |
|
| 1501 | 1501 | |
| 1502 | 1502 | |
| 1503 | - /** |
|
| 1504 | - * Determine if the user has and active subscription to give them access |
|
| 1505 | - * to the requested resource. |
|
| 1506 | - * |
|
| 1507 | - * @since 1.9.0 |
|
| 1508 | - * |
|
| 1509 | - * @param boolean$user_access_permission |
|
| 1510 | - * @param integer $user_id |
|
| 1511 | - * @return boolean $user_access_permission |
|
| 1512 | - */ |
|
| 1513 | - public static function get_subscription_permission( $user_access_permission , $user_id ){ |
|
| 1503 | + /** |
|
| 1504 | + * Determine if the user has and active subscription to give them access |
|
| 1505 | + * to the requested resource. |
|
| 1506 | + * |
|
| 1507 | + * @since 1.9.0 |
|
| 1508 | + * |
|
| 1509 | + * @param boolean$user_access_permission |
|
| 1510 | + * @param integer $user_id |
|
| 1511 | + * @return boolean $user_access_permission |
|
| 1512 | + */ |
|
| 1513 | + public static function get_subscription_permission( $user_access_permission , $user_id ){ |
|
| 1514 | 1514 | |
| 1515 | - global $post; |
|
| 1515 | + global $post; |
|
| 1516 | 1516 | |
| 1517 | - // ignore the current case if the following conditions are met |
|
| 1518 | - if ( ! class_exists( 'WC_Subscriptions' ) || empty( $user_id ) |
|
| 1519 | - || ! in_array( $post->post_type, array( 'course','lesson','quiz' ) ) |
|
| 1520 | - || ! wcs_user_has_subscription( $user_id) ){ |
|
| 1517 | + // ignore the current case if the following conditions are met |
|
| 1518 | + if ( ! class_exists( 'WC_Subscriptions' ) || empty( $user_id ) |
|
| 1519 | + || ! in_array( $post->post_type, array( 'course','lesson','quiz' ) ) |
|
| 1520 | + || ! wcs_user_has_subscription( $user_id) ){ |
|
| 1521 | 1521 | |
| 1522 | - return $user_access_permission; |
|
| 1522 | + return $user_access_permission; |
|
| 1523 | 1523 | |
| 1524 | - } |
|
| 1524 | + } |
|
| 1525 | 1525 | |
| 1526 | - // at this user has a subscription |
|
| 1527 | - // is the subscription on the the current course? |
|
| 1526 | + // at this user has a subscription |
|
| 1527 | + // is the subscription on the the current course? |
|
| 1528 | 1528 | |
| 1529 | - $course_id = 0; |
|
| 1530 | - if ( 'course' == $post->post_type ){ |
|
| 1529 | + $course_id = 0; |
|
| 1530 | + if ( 'course' == $post->post_type ){ |
|
| 1531 | 1531 | |
| 1532 | - $course_id = $post->ID; |
|
| 1532 | + $course_id = $post->ID; |
|
| 1533 | 1533 | |
| 1534 | - } elseif ( 'lesson' == $post->post_type ) { |
|
| 1534 | + } elseif ( 'lesson' == $post->post_type ) { |
|
| 1535 | 1535 | |
| 1536 | - $course_id = Sensei()->lesson->get_course_id( $post->ID ); |
|
| 1536 | + $course_id = Sensei()->lesson->get_course_id( $post->ID ); |
|
| 1537 | 1537 | |
| 1538 | - } else { |
|
| 1538 | + } else { |
|
| 1539 | 1539 | |
| 1540 | - $lesson_id = Sensei()->quiz->get_lesson_id( $post->ID ); |
|
| 1541 | - $course_id = Sensei()->lesson->get_course_id( $lesson_id ); |
|
| 1540 | + $lesson_id = Sensei()->quiz->get_lesson_id( $post->ID ); |
|
| 1541 | + $course_id = Sensei()->lesson->get_course_id( $lesson_id ); |
|
| 1542 | 1542 | |
| 1543 | - } |
|
| 1543 | + } |
|
| 1544 | 1544 | |
| 1545 | - // if the course has no subscription WooCommerce product attached to return the permissions as is |
|
| 1546 | - $product_id = Sensei_WC::get_course_product_id( $course_id ); |
|
| 1547 | - $product = wc_get_product( $product_id ); |
|
| 1548 | - if( ! in_array( $product->get_type(), self::get_subscription_types() ) ){ |
|
| 1545 | + // if the course has no subscription WooCommerce product attached to return the permissions as is |
|
| 1546 | + $product_id = Sensei_WC::get_course_product_id( $course_id ); |
|
| 1547 | + $product = wc_get_product( $product_id ); |
|
| 1548 | + if( ! in_array( $product->get_type(), self::get_subscription_types() ) ){ |
|
| 1549 | 1549 | |
| 1550 | - return $user_access_permission; |
|
| 1550 | + return $user_access_permission; |
|
| 1551 | 1551 | |
| 1552 | - } |
|
| 1552 | + } |
|
| 1553 | 1553 | |
| 1554 | - // give access if user has active subscription on the product otherwise restrict it. |
|
| 1555 | - // also check if the user was added to the course directly after the subscription started. |
|
| 1556 | - if( wcs_user_has_subscription( $user_id, $product_id, 'active' ) |
|
| 1557 | - || wcs_user_has_subscription( $user_id, $product_id, 'pending-cancel' ) |
|
| 1558 | - || self::was_user_added_without_subscription( $user_id, $product_id, $course_id ) ){ |
|
| 1554 | + // give access if user has active subscription on the product otherwise restrict it. |
|
| 1555 | + // also check if the user was added to the course directly after the subscription started. |
|
| 1556 | + if( wcs_user_has_subscription( $user_id, $product_id, 'active' ) |
|
| 1557 | + || wcs_user_has_subscription( $user_id, $product_id, 'pending-cancel' ) |
|
| 1558 | + || self::was_user_added_without_subscription( $user_id, $product_id, $course_id ) ){ |
|
| 1559 | 1559 | |
| 1560 | - $user_access_permission = true; |
|
| 1560 | + $user_access_permission = true; |
|
| 1561 | 1561 | |
| 1562 | - }else{ |
|
| 1562 | + }else{ |
|
| 1563 | 1563 | |
| 1564 | - $user_access_permission = false; |
|
| 1565 | - // do not show the WC permissions message |
|
| 1566 | - remove_filter( 'sensei_the_no_permissions_message', array( 'Sensei_WC', 'alter_no_permissions_message' ), 20, 2 ); |
|
| 1567 | - Sensei()->permissions_message['title'] = __( 'No active subscription', 'woothemes-sensei' ); |
|
| 1568 | - Sensei()->permissions_message['message'] = __( 'Sorry, you do not have an access to this content without an active subscription.', 'woothemes-sensei' ); |
|
| 1569 | - } |
|
| 1564 | + $user_access_permission = false; |
|
| 1565 | + // do not show the WC permissions message |
|
| 1566 | + remove_filter( 'sensei_the_no_permissions_message', array( 'Sensei_WC', 'alter_no_permissions_message' ), 20, 2 ); |
|
| 1567 | + Sensei()->permissions_message['title'] = __( 'No active subscription', 'woothemes-sensei' ); |
|
| 1568 | + Sensei()->permissions_message['message'] = __( 'Sorry, you do not have an access to this content without an active subscription.', 'woothemes-sensei' ); |
|
| 1569 | + } |
|
| 1570 | 1570 | |
| 1571 | - return $user_access_permission; |
|
| 1571 | + return $user_access_permission; |
|
| 1572 | 1572 | |
| 1573 | - } // end get_subscription_permission |
|
| 1573 | + } // end get_subscription_permission |
|
| 1574 | 1574 | |
| 1575 | 1575 | /** |
| 1576 | 1576 | * @since 1.9.0 |
@@ -1619,8 +1619,8 @@ discard block |
||
| 1619 | 1619 | // give access if user has active subscription on the product otherwise restrict it. |
| 1620 | 1620 | // also check if the user was added to the course directly after the subscription started. |
| 1621 | 1621 | if( wcs_user_has_subscription( $user_id, $product_id, 'active' ) |
| 1622 | - || wcs_user_has_subscription( $user_id, $product_id, 'pending-cancel' ) |
|
| 1623 | - || self::was_user_added_without_subscription( $user_id, $product_id, $course_id ) ){ |
|
| 1622 | + || wcs_user_has_subscription( $user_id, $product_id, 'pending-cancel' ) |
|
| 1623 | + || self::was_user_added_without_subscription( $user_id, $product_id, $course_id ) ){ |
|
| 1624 | 1624 | |
| 1625 | 1625 | $has_user_started_course = true; |
| 1626 | 1626 | |
@@ -1635,91 +1635,91 @@ discard block |
||
| 1635 | 1635 | |
| 1636 | 1636 | } |
| 1637 | 1637 | |
| 1638 | - /** |
|
| 1639 | - * Get all the valid subscription types. |
|
| 1640 | - * |
|
| 1641 | - * @since 1.9.0 |
|
| 1642 | - * @return array |
|
| 1643 | - */ |
|
| 1644 | - public static function get_subscription_types(){ |
|
| 1638 | + /** |
|
| 1639 | + * Get all the valid subscription types. |
|
| 1640 | + * |
|
| 1641 | + * @since 1.9.0 |
|
| 1642 | + * @return array |
|
| 1643 | + */ |
|
| 1644 | + public static function get_subscription_types(){ |
|
| 1645 | 1645 | |
| 1646 | - return array( 'subscription','subscription_variation','variable-subscription' ); |
|
| 1646 | + return array( 'subscription','subscription_variation','variable-subscription' ); |
|
| 1647 | 1647 | |
| 1648 | - } |
|
| 1648 | + } |
|
| 1649 | 1649 | |
| 1650 | - /** |
|
| 1651 | - * Compare the user's subscriptions end date with the date |
|
| 1652 | - * the user was added to the course. If the user was added after |
|
| 1653 | - * the subscription ended they were manually added and this will return |
|
| 1654 | - * true. |
|
| 1655 | - * |
|
| 1656 | - * Important to note that all subscriptions for the user is compared. |
|
| 1657 | - * |
|
| 1658 | - * @since 1.9.0 |
|
| 1659 | - * |
|
| 1660 | - * @param $user_id |
|
| 1661 | - * @param $product_id |
|
| 1662 | - * @param $course_id |
|
| 1663 | - * |
|
| 1664 | - * @return bool |
|
| 1665 | - */ |
|
| 1666 | - public static function was_user_added_without_subscription($user_id, $product_id, $course_id ){ |
|
| 1650 | + /** |
|
| 1651 | + * Compare the user's subscriptions end date with the date |
|
| 1652 | + * the user was added to the course. If the user was added after |
|
| 1653 | + * the subscription ended they were manually added and this will return |
|
| 1654 | + * true. |
|
| 1655 | + * |
|
| 1656 | + * Important to note that all subscriptions for the user is compared. |
|
| 1657 | + * |
|
| 1658 | + * @since 1.9.0 |
|
| 1659 | + * |
|
| 1660 | + * @param $user_id |
|
| 1661 | + * @param $product_id |
|
| 1662 | + * @param $course_id |
|
| 1663 | + * |
|
| 1664 | + * @return bool |
|
| 1665 | + */ |
|
| 1666 | + public static function was_user_added_without_subscription($user_id, $product_id, $course_id ){ |
|
| 1667 | 1667 | |
| 1668 | - $course_start_date = ''; |
|
| 1669 | - $subscription_start_date = ''; |
|
| 1670 | - $is_a_subscription =''; |
|
| 1671 | - $was_user_added_without_subscription = false; |
|
| 1668 | + $course_start_date = ''; |
|
| 1669 | + $subscription_start_date = ''; |
|
| 1670 | + $is_a_subscription =''; |
|
| 1671 | + $was_user_added_without_subscription = false; |
|
| 1672 | 1672 | |
| 1673 | - // if user is not on the course they were not added |
|
| 1674 | - remove_filter( 'sensei_user_started_course', array( 'Sensei_WC', 'get_subscription_user_started_course' ), 10, 3 ); |
|
| 1675 | - if( ! Sensei_Utils::user_started_course( $course_id, $user_id ) ){ |
|
| 1673 | + // if user is not on the course they were not added |
|
| 1674 | + remove_filter( 'sensei_user_started_course', array( 'Sensei_WC', 'get_subscription_user_started_course' ), 10, 3 ); |
|
| 1675 | + if( ! Sensei_Utils::user_started_course( $course_id, $user_id ) ){ |
|
| 1676 | 1676 | |
| 1677 | - return false; |
|
| 1677 | + return false; |
|
| 1678 | 1678 | |
| 1679 | - } |
|
| 1679 | + } |
|
| 1680 | 1680 | |
| 1681 | - // if user doesn't have a subscription and is taking the course |
|
| 1682 | - // they were added manually |
|
| 1683 | - if ( ! wcs_user_has_subscription($user_id, $product_id) |
|
| 1684 | - && Sensei_Utils::user_started_course( $course_id, get_current_user_id() ) ){ |
|
| 1681 | + // if user doesn't have a subscription and is taking the course |
|
| 1682 | + // they were added manually |
|
| 1683 | + if ( ! wcs_user_has_subscription($user_id, $product_id) |
|
| 1684 | + && Sensei_Utils::user_started_course( $course_id, get_current_user_id() ) ){ |
|
| 1685 | 1685 | |
| 1686 | - return true; |
|
| 1686 | + return true; |
|
| 1687 | 1687 | |
| 1688 | - } |
|
| 1688 | + } |
|
| 1689 | 1689 | |
| 1690 | - add_filter( 'sensei_user_started_course', array( 'Sensei_WC', 'get_subscription_user_started_course' ), 10, 3 ); |
|
| 1690 | + add_filter( 'sensei_user_started_course', array( 'Sensei_WC', 'get_subscription_user_started_course' ), 10, 3 ); |
|
| 1691 | 1691 | |
| 1692 | - $course_status = Sensei_Utils::user_course_status( $course_id, $user_id ); |
|
| 1692 | + $course_status = Sensei_Utils::user_course_status( $course_id, $user_id ); |
|
| 1693 | 1693 | |
| 1694 | - // comparing dates setup data |
|
| 1695 | - $course_start_date = date_create( $course_status->comment_date ); |
|
| 1696 | - $subscriptions = wcs_get_users_subscriptions( $user_id ); |
|
| 1694 | + // comparing dates setup data |
|
| 1695 | + $course_start_date = date_create( $course_status->comment_date ); |
|
| 1696 | + $subscriptions = wcs_get_users_subscriptions( $user_id ); |
|
| 1697 | 1697 | |
| 1698 | - // comparing every subscription |
|
| 1699 | - foreach( $subscriptions as $subscription ){ |
|
| 1698 | + // comparing every subscription |
|
| 1699 | + foreach( $subscriptions as $subscription ){ |
|
| 1700 | 1700 | |
| 1701 | - // for the following statuses we know the user was not added |
|
| 1702 | - // manually |
|
| 1703 | - $status = $subscription->get_status(); |
|
| 1704 | - if ( in_array( $status, array( 'pending-canceled', 'active', 'on-hold', 'pending' ) ) ) { |
|
| 1701 | + // for the following statuses we know the user was not added |
|
| 1702 | + // manually |
|
| 1703 | + $status = $subscription->get_status(); |
|
| 1704 | + if ( in_array( $status, array( 'pending-canceled', 'active', 'on-hold', 'pending' ) ) ) { |
|
| 1705 | 1705 | |
| 1706 | - continue; |
|
| 1706 | + continue; |
|
| 1707 | 1707 | |
| 1708 | - } |
|
| 1708 | + } |
|
| 1709 | 1709 | |
| 1710 | - $current_subscription_start_date = date_create( $subscription->modified_date ); |
|
| 1710 | + $current_subscription_start_date = date_create( $subscription->modified_date ); |
|
| 1711 | 1711 | |
| 1712 | - // is the last updated subscription date newer than course start date |
|
| 1713 | - if ( $current_subscription_start_date > $course_start_date ) { |
|
| 1712 | + // is the last updated subscription date newer than course start date |
|
| 1713 | + if ( $current_subscription_start_date > $course_start_date ) { |
|
| 1714 | 1714 | |
| 1715 | - return false; |
|
| 1715 | + return false; |
|
| 1716 | 1716 | |
| 1717 | - } |
|
| 1717 | + } |
|
| 1718 | 1718 | |
| 1719 | - } |
|
| 1719 | + } |
|
| 1720 | 1720 | |
| 1721 | - return $was_user_added_without_subscription; |
|
| 1722 | - } |
|
| 1721 | + return $was_user_added_without_subscription; |
|
| 1722 | + } |
|
| 1723 | 1723 | |
| 1724 | 1724 | /** |
| 1725 | 1725 | * Get all the orders for a specific user and product combination |