From 2978bbed8c1678437a465dfdacbb577f4748dd3e Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 13 Mar 2023 00:42:58 +0530 Subject: [PATCH 001/125] Added Object (Lesson) Favorite functionality --- includes/class-llms-loader.php | 2 + .../class.llms.controller.favorites.php | 160 +++++++++++++++++ ...ass.llms.controller.lesson.progression.php | 2 +- includes/functions/llms.functions.person.php | 18 ++ includes/llms.template.functions.php | 13 ++ includes/llms.template.hooks.php | 3 +- includes/models/model.llms.student.php | 163 ++++++++++++++++++ templates/course/favorite.php | 58 +++++++ 8 files changed, 417 insertions(+), 2 deletions(-) create mode 100644 includes/controllers/class.llms.controller.favorites.php create mode 100644 templates/course/favorite.php diff --git a/includes/class-llms-loader.php b/includes/class-llms-loader.php index eae5d74761..e8d79430d0 100644 --- a/includes/class-llms-loader.php +++ b/includes/class-llms-loader.php @@ -232,6 +232,7 @@ public function autoload( $class ) { * Removed loading of class files that don't instantiate their class in favor of autoloading. * @since 6.4.0 Included `LLMS_Shortcodes` before `LLMS_Controller_Orders`. * @since 7.0.0 Include `LLMS_Controller_Checkout`. + * @since [version] Include `LLMS_Controller_Favorite`. * * @return void */ @@ -285,6 +286,7 @@ public function includes() { require_once LLMS_PLUGIN_DIR . 'includes/controllers/class-llms-controller-awards.php'; require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.certificates.php'; require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.lesson.progression.php'; + require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.favorites.php'; require_once LLMS_PLUGIN_DIR . 'includes/controllers/class-llms-controller-checkout.php'; // Added out of alpha order to preserve action load order. require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.orders.php'; require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.quizzes.php'; diff --git a/includes/controllers/class.llms.controller.favorites.php b/includes/controllers/class.llms.controller.favorites.php new file mode 100644 index 0000000000..5ec7d4661f --- /dev/null +++ b/includes/controllers/class.llms.controller.favorites.php @@ -0,0 +1,160 @@ +get_object_id_from_form_data( 'favorite' ); + $object_type = llms_filter_input( INPUT_POST, 'type' ); + + if ( is_null( $object_id ) ) { + return; + } + + /** + * Filter to modify the user id instead of current logged in user id. + * + * @param int $user_id User id to mark lesson as favorite. + * + * @since [version] + */ + $user_id = apply_filters( 'llms_object_favorite_user_id', get_current_user_id() ); + + do_action( 'llms_trigger_object_favorite', $user_id, $object_id, $object_type ); + + } + + /** + * Mark Object as unfavorite + * + * + Unfavorite Object form post. + * + Marks Object as Unfavorite. + * + * @since [version] + * + * @return void + */ + public function handle_unfavorite_form() { + + $lesson_id = $this->get_object_id_from_form_data( 'unfavorite' ); + $object_type = llms_filter_input( INPUT_POST, 'type' ); + + if ( is_null( $lesson_id ) ) { + return; + } + + /** + * Filter to modify the user id instead of current logged in user id. + * + * @param int $user_id User id to mark object as unfavorite. + * + * @since [version] + */ + $user_id = apply_filters( 'llms_object_unfavorite_user_id', get_current_user_id() ); + + // Mark unfavorite. + llms_mark_unfavorite( $user_id, $object_id, $object_type ); + + } + + /** + * Handle favoriting of object via `llms_trigger_object_favorite` action + * + * @since [version] + * + * @param int $user_id User ID. + * @param int $object_id Object ID. + * @param string $object_type Object description string (Lesson, Course or Instructor). + * @param array $args Optional arguments. + * @return void + */ + public function mark_favorite( $user_id, $object_id, $object_type = '', $args = array() ) { + + if ( llms_allow_lesson_completion( $user_id, $object_id, $object_type, $args ) ) { + + llms_mark_favorite( $user_id, $object_id, $object_type ); + + } + + } + +} + +return new LLMS_Controller_Favorite(); diff --git a/includes/controllers/class.llms.controller.lesson.progression.php b/includes/controllers/class.llms.controller.lesson.progression.php index a5f19d1fd0..de703a995f 100644 --- a/includes/controllers/class.llms.controller.lesson.progression.php +++ b/includes/controllers/class.llms.controller.lesson.progression.php @@ -44,7 +44,7 @@ public function __construct() { * * @since 3.29.0 * - * @param tring $action Form action, either "complete" or "incomplete". + * @param String $action Form action, either "complete" or "incomplete". * @return int|null Returns `null` when either required post fields are missing or if the lesson_id is non-numeric, int (lesson id) on success. */ private function get_lesson_id_from_form_data( $action ) { diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index 4044c66447..e73f93b791 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -330,10 +330,28 @@ function llms_is_user_enrolled( $user_id, $product_id, $relation = 'all', $use_c * @return boolean */ function llms_mark_complete( $user_id, $object_id, $object_type, $trigger = 'unspecified' ) { + // die('minus one'); $student = new LLMS_Student( $user_id ); return $student->mark_complete( $object_id, $object_type, $trigger ); } +/** + * Mark an object as favorite + * + * @since [version] + * + * @see LLMS_Student->mark_favorite() + * + * @param int $user_id WP User ID. + * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @param int $object_type Object type [lesson|section|course|track]. + * @return boolean + */ +function llms_mark_favorite( $user_id, $object_id, $object_type ) { + $student = new LLMS_Student( $user_id ); + return $student->mark_favorite( $object_id, $object_type ); +} + /** * Mark a lesson, section, course, or track as incomplete * diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index f85b10d3c2..c663b1d87c 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -385,6 +385,19 @@ function lifterlms_template_single_parent_course() { } } +/** + * Favorite Lesson Template Include + * + * @return void + */ +if ( ! function_exists( 'lifterlms_template_single_favorite' ) ) { + + function lifterlms_template_single_favorite() { + + llms_get_template( 'course/favorite.php' ); + } +} + /** * Complete Lesson Link Template Include * diff --git a/includes/llms.template.hooks.php b/includes/llms.template.hooks.php index ca205fb744..2203ccd750 100644 --- a/includes/llms.template.hooks.php +++ b/includes/llms.template.hooks.php @@ -7,7 +7,7 @@ * @package LifterLMS/Hooks * * @since 1.0.0 - * @version 6.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -48,6 +48,7 @@ * @since Unknown */ add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_parent_course', 10 ); +add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_favorite', 10 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_video', 20 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_audio', 20 ); diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index f72f838faa..f18dfcca1c 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1253,6 +1253,30 @@ private function insert_incompletion_postmeta( $object_id, $trigger = 'unspecifi } + /** + * Add student postmeta data when lesson is favorited + * + * @param int $object_id WP Post ID of the lesson + * @param string $trigger String describing the reason for mark completion + * @return boolean + * @since [version] + * @version [version] + */ + private function insert_favorite_postmeta( $object_id, $trigger = 'unspecified' ) { + + // Add info to the user postmeta table. + $user_metadatas = array( + '_is_favorite' => 'yes', + '_completion_trigger' => $trigger, + ); + + $update = llms_update_user_postmeta( $this->get_id(), $object_id, '_favorite', true ); + + // Returns an array with errored keys or true on success. + return is_array( $update ) ? false : true; + + } + /** * Add student postmeta data for enrollment into a course or membership * @@ -1841,4 +1865,143 @@ private function update_completion_status( $status, $object_id, $object_type, $t } + /** + * Determine if the student has favorited a lesson + * + * @param int $object_id WP Post ID of a course or lesson or section or the term id of the track + * @param string $type Object type (course, lesson, section, or track) + * @return boolean + * @since [version] + * @version [version] + */ + public function is_favorite( $object_id, $object_type = 'course' ) { + + $query = new LLMS_Query_User_Postmeta( + array( + 'types' => 'favorites', + 'include_post_children' => false, + 'user_id' => $this->get_id(), + 'post_id' => $object_id, + 'per_page' => 1, + ) + ); + + $ret = $query->has_results(); + + return apply_filters( 'llms_is_' . $object_type . '_favorite', $ret, $object_id, $object_type, $this ); + + } + + /** + * Mark a lesson, section, course, or track complete for the given user + * + * @param int $object_id WP Post ID of the lesson, section, course, or track + * @param string $object_type object type [lesson|section|course|track] + * @param string $trigger String describing the reason for marking complete + * @return boolean + * + * @see llms_mark_favorite() calls this function without having to instantiate the LLMS_Student class first + * + * @since [version] + * @version [version] + */ + public function mark_favorite( $object_id, $object_type ) { + + // Short circuit if it's already favorited. + if ( $this->is_favorite( $object_id, $object_type ) ) { + return true; + } + + return $this->update_favorite_status( 'favorite', $object_id, $object_type ); + + } + + /** + * Update the favorite status of a track, course, section, or lesson for the current student + * + * Triggers actions for favorite/unfavorite. + * + * Inserts / updates necessary user postmeta data. + * + * @since [version] + * @since 4.2.0 Use filterable functions to determine if the object is completable. + * Added filter to allow customization of object parent data. + * + * @param string $status New status to update to, either "complete" or "incomplete". + * @param int $object_id WP_Post ID of the object. + * @param string $object_type The type of object. A lesson, section, course, or course_track. + * @param string $trigger String describing the reason for the status change. + * @return boolean + */ + private function update_favorite_status( $status, $object_id, $object_type, $trigger = 'unspecified' ) { + + $student_id = $this->get_id(); + + /** + * Fires before a student's object completion status is updated. + * + * The dynamic portion of this hook, `$status`, refers to the new completion status of the object, + * either "complete" or "incomplete" + * + * @since [version] + * + * @param int $student_id WP_User ID of the student. + * @param int $object_id WP_Post ID of the object. + * @param string $object_type The type of object. A lesson, section, course, or course_track. + * @param string $trigger String describing the reason for the status change. + */ + do_action( "before_llms_mark_{$status}", $student_id, $object_id, $object_type, $trigger ); + + // Retrieve an instance of the object we're acting on. + if ( in_array( $object_type, llms_get_completable_post_types(), true ) ) { + $object = llms_get_post( $object_id ); + } elseif ( in_array( $object_type, llms_get_completable_taxonomies(), true ) ) { + $object = get_term( $object_id, $object_type ); + } else { + return false; + } + + // Insert meta data. + if ( 'favorite' === $status ) { + $this->insert_favorite_postmeta( $object_id, $trigger ); + } elseif ( 'unfavorite' === $status ) { + // TODO + $this->insert_unfavorite_postmeta( $object_id, $trigger ); + } + + /** + * Hook that fires when a student's completion status is updated for any object. + * + * The dynamic portion of this hook, `$status`, refers to the new favorite status of the object, + * either "favorite" or "unfavorite" + * + * @since [version] + * + * @param int $student_id WP_User ID of the student. + * @param int $object_id WP_Post ID of the object. + * @param string $object_type The type of object. A lesson, section, course, or course_track. + * @param string $trigger String describing the reason for the status change. + */ + do_action( "llms_mark_{$status}", $student_id, $object_id, $object_type, $trigger ); + + /** + * Hook that fires when a student's favorite status is updated for a specific object type. + * + * The dynamic portion of this hook, `$object_type` refers to the WP_Post post_type of the object + * which the student's completion status is being updated for. + * + * The dynamic portion of this hook, `$status`, refers to the new completion status of the object, + * either "favorite" or "unfavorite" + * + * @since [version] + * + * @param int $student_id WP_User ID of the student. + * @param int $object_id WP_Post ID of the object. + */ + do_action( "lifterlms_{$object_type}_{$status}d", $student_id, $object_id ); + + return true; + + } + } diff --git a/templates/course/favorite.php b/templates/course/favorite.php new file mode 100644 index 0000000000..8bbfaeab8b --- /dev/null +++ b/templates/course/favorite.php @@ -0,0 +1,58 @@ +ID ); +$student = llms_get_student( get_current_user_id() ); +?> + +
+ + + + is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> + + + + + +
+ + + + + + + + + + 1, + 'classes' => 'auto button llms-favorite', + 'id' => 'llms_mark_favorite', + 'value' => apply_filters( 'lifterlms_mark_lesson_favorite_button_text', __( '', 'lifterlms' ), $lesson ), + 'last_column' => true, + 'name' => 'mark_favorite', + 'required' => false, + 'type' => 'submit', + ) + ); + ?> + + + +
+ + + + + +
From 77886e0b3936fa2e33f4908c3e71b9b560dbcdf0 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 13 Mar 2023 01:02:54 +0530 Subject: [PATCH 002/125] Fixed PHPCS errors --- .../class.llms.controller.favorites.php | 14 ++-- includes/functions/llms.functions.person.php | 6 +- includes/models/model.llms.student.php | 10 +-- templates/course/favorite.php | 64 +++++++++---------- 4 files changed, 47 insertions(+), 47 deletions(-) diff --git a/includes/controllers/class.llms.controller.favorites.php b/includes/controllers/class.llms.controller.favorites.php index 5ec7d4661f..37c7d516dd 100644 --- a/includes/controllers/class.llms.controller.favorites.php +++ b/includes/controllers/class.llms.controller.favorites.php @@ -81,8 +81,8 @@ private function get_object_id_from_form_data( $action ) { */ public function handle_favorite_form() { - $object_id = $this->get_object_id_from_form_data( 'favorite' ); - $object_type = llms_filter_input( INPUT_POST, 'type' ); + $object_id = $this->get_object_id_from_form_data( 'favorite' ); + $object_type = llms_filter_input( INPUT_POST, 'type' ); if ( is_null( $object_id ) ) { return; @@ -113,8 +113,8 @@ public function handle_favorite_form() { */ public function handle_unfavorite_form() { - $lesson_id = $this->get_object_id_from_form_data( 'unfavorite' ); - $object_type = llms_filter_input( INPUT_POST, 'type' ); + $lesson_id = $this->get_object_id_from_form_data( 'unfavorite' ); + $object_type = llms_filter_input( INPUT_POST, 'type' ); if ( is_null( $lesson_id ) ) { return; @@ -139,10 +139,10 @@ public function handle_unfavorite_form() { * * @since [version] * - * @param int $user_id User ID. - * @param int $object_id Object ID. + * @param int $user_id User ID. + * @param int $object_id Object ID. * @param string $object_type Object description string (Lesson, Course or Instructor). - * @param array $args Optional arguments. + * @param array $args Optional arguments. * @return void */ public function mark_favorite( $user_id, $object_id, $object_type = '', $args = array() ) { diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index e73f93b791..7ec1b968b3 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -342,9 +342,9 @@ function llms_mark_complete( $user_id, $object_id, $object_type, $trigger = 'uns * * @see LLMS_Student->mark_favorite() * - * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param int $object_type Object type [lesson|section|course|track]. + * @param int $user_id WP User ID. + * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @param int $object_type Object type [lesson|section|course|track]. * @return boolean */ function llms_mark_favorite( $user_id, $object_id, $object_type ) { diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index f18dfcca1c..771b945e6c 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1906,7 +1906,7 @@ public function is_favorite( $object_id, $object_type = 'course' ) { * @version [version] */ public function mark_favorite( $object_id, $object_type ) { - + // Short circuit if it's already favorited. if ( $this->is_favorite( $object_id, $object_type ) ) { return true; @@ -1934,7 +1934,7 @@ public function mark_favorite( $object_id, $object_type ) { * @return boolean */ private function update_favorite_status( $status, $object_id, $object_type, $trigger = 'unspecified' ) { - + $student_id = $this->get_id(); /** @@ -1951,7 +1951,7 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * @param string $trigger String describing the reason for the status change. */ do_action( "before_llms_mark_{$status}", $student_id, $object_id, $object_type, $trigger ); - + // Retrieve an instance of the object we're acting on. if ( in_array( $object_type, llms_get_completable_post_types(), true ) ) { $object = llms_get_post( $object_id ); @@ -1960,7 +1960,7 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri } else { return false; } - + // Insert meta data. if ( 'favorite' === $status ) { $this->insert_favorite_postmeta( $object_id, $trigger ); @@ -1999,7 +1999,7 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * @param int $object_id WP_Post ID of the object. */ do_action( "lifterlms_{$object_type}_{$status}d", $student_id, $object_id ); - + return true; } diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 8bbfaeab8b..4c7c277df6 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -8,51 +8,51 @@ global $post; -$lesson = new LLMS_Lesson( $post->ID ); +$lesson = new LLMS_Lesson( $post->ID ); $student = llms_get_student( get_current_user_id() ); ?>
- - - is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> - - + + + is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> + + - + -
+ - + - - - - - + + + + + - 1, - 'classes' => 'auto button llms-favorite', - 'id' => 'llms_mark_favorite', - 'value' => apply_filters( 'lifterlms_mark_lesson_favorite_button_text', __( '', 'lifterlms' ), $lesson ), - 'last_column' => true, - 'name' => 'mark_favorite', - 'required' => false, - 'type' => 'submit', - ) - ); - ?> + 1, + 'classes' => 'auto button llms-favorite', + 'id' => 'llms_mark_favorite', + 'value' => apply_filters( 'lifterlms_mark_lesson_favorite_button_text', __( '', 'lifterlms' ), $lesson ), + 'last_column' => true, + 'name' => 'mark_favorite', + 'required' => false, + 'type' => 'submit', + ) + ); + ?> - + -
+ - + - +
From d8d1ff4b85bfda3f69e7a6b72223093aedb5d4b9 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 13 Mar 2023 14:01:55 +0530 Subject: [PATCH 003/125] Added Object (Lesson) Unfavorite functionality --- .../class.llms.controller.favorites.php | 4 +- includes/functions/llms.functions.person.php | 29 ++++++++--- includes/models/model.llms.student.php | 51 +++++++++++++++++-- templates/course/favorite.php | 29 ++++++++++- 4 files changed, 99 insertions(+), 14 deletions(-) diff --git a/includes/controllers/class.llms.controller.favorites.php b/includes/controllers/class.llms.controller.favorites.php index 37c7d516dd..2681fdddd5 100644 --- a/includes/controllers/class.llms.controller.favorites.php +++ b/includes/controllers/class.llms.controller.favorites.php @@ -113,10 +113,10 @@ public function handle_favorite_form() { */ public function handle_unfavorite_form() { - $lesson_id = $this->get_object_id_from_form_data( 'unfavorite' ); + $object_id = $this->get_object_id_from_form_data( 'unfavorite' ); $object_type = llms_filter_input( INPUT_POST, 'type' ); - if ( is_null( $lesson_id ) ) { + if ( is_null( $object_id ) ) { return; } diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index 7ec1b968b3..9bbe21a92f 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -335,6 +335,24 @@ function llms_mark_complete( $user_id, $object_id, $object_type, $trigger = 'uns return $student->mark_complete( $object_id, $object_type, $trigger ); } +/** + * Mark a lesson, section, course, or track as incomplete + * + * @since 3.5.0 + * + * @see LLMS_Student->mark_incomplete() + * + * @param int $user_id WP User ID. + * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @param int $object_type Object type [lesson|section|course|track]. + * @param string $trigger String describing the event that triggered marking the object as incomplete. + * @return boolean + */ +function llms_mark_incomplete( $user_id, $object_id, $object_type, $trigger = 'unspecified' ) { + $student = new LLMS_Student( $user_id ); + return $student->mark_incomplete( $object_id, $object_type, $trigger ); +} + /** * Mark an object as favorite * @@ -353,21 +371,20 @@ function llms_mark_favorite( $user_id, $object_id, $object_type ) { } /** - * Mark a lesson, section, course, or track as incomplete + * Mark a lesson as unfavorite * - * @since 3.5.0 + * @since [version] * - * @see LLMS_Student->mark_incomplete() + * @see LLMS_Student->mark_unfavorite() * * @param int $user_id WP User ID. * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. * @param int $object_type Object type [lesson|section|course|track]. - * @param string $trigger String describing the event that triggered marking the object as incomplete. * @return boolean */ -function llms_mark_incomplete( $user_id, $object_id, $object_type, $trigger = 'unspecified' ) { +function llms_mark_unfavorite( $user_id, $object_id, $object_type ) { $student = new LLMS_Student( $user_id ); - return $student->mark_incomplete( $object_id, $object_type, $trigger ); + return $student->mark_unfavorite( $object_id, $object_type ); } /** diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 771b945e6c..bdb5861c2b 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -27,6 +27,7 @@ * Added new filter to allow customization of object completion data. * @since 5.2.0 Changed the date to be relative to the local time zone in `get_registration_date`. * @since 6.0.0 Removed the deprecated `llms_user_removed_from_membership_level` action hook from the `LLMS_Student::unenroll()` method. + * @since [version] Added the logic to add and remove lesson favoritism. */ class LLMS_Student extends LLMS_Abstract_User_Data { @@ -1277,6 +1278,24 @@ private function insert_favorite_postmeta( $object_id, $trigger = 'unspecified' } + /** + * Remove student postmeta data when lesson is unfavorited + * + * @param int $object_id WP Post ID of the lesson + * @param string $trigger String describing the reason for mark completion + * @return boolean + * @since [version] + * @version [version] + */ + private function insert_unfavorite_postmeta( $object_id, $trigger = 'unspecified' ) { + + $update = llms_delete_user_postmeta( $this->get_id(), $object_id, '_favorite', true ); + + // Returns an array with errored keys or true on success. + return is_array( $update ) ? false : true; + + } + /** * Add student postmeta data for enrollment into a course or membership * @@ -1893,11 +1912,10 @@ public function is_favorite( $object_id, $object_type = 'course' ) { } /** - * Mark a lesson, section, course, or track complete for the given user + * Mark a lesson, section, course, or track favorite for the given user * * @param int $object_id WP Post ID of the lesson, section, course, or track * @param string $object_type object type [lesson|section|course|track] - * @param string $trigger String describing the reason for marking complete * @return boolean * * @see llms_mark_favorite() calls this function without having to instantiate the LLMS_Student class first @@ -1916,6 +1934,29 @@ public function mark_favorite( $object_id, $object_type ) { } + /** + * Mark a lesson, section, course, or track unfavorite for the given user + * + * @param int $object_id WP Post ID of the lesson, section, course, or track + * @param string $object_type object type [lesson|section|course|track] + * @return boolean + * + * @see llms_mark_unfavorite() calls this function without having to instantiate the LLMS_Student class first + * + * @since [version] + * @version [version] + */ + public function mark_unfavorite( $object_id, $object_type ) { + + // Short circuit if it's not favorited. + if ( ! $this->is_favorite( $object_id, $object_type ) ) { + return true; + } + + return $this->update_favorite_status( 'unfavorite', $object_id, $object_type ); + + } + /** * Update the favorite status of a track, course, section, or lesson for the current student * @@ -1933,15 +1974,16 @@ public function mark_favorite( $object_id, $object_type ) { * @param string $trigger String describing the reason for the status change. * @return boolean */ + private function update_favorite_status( $status, $object_id, $object_type, $trigger = 'unspecified' ) { $student_id = $this->get_id(); /** - * Fires before a student's object completion status is updated. + * Fires before a student's object favorite status is updated. * * The dynamic portion of this hook, `$status`, refers to the new completion status of the object, - * either "complete" or "incomplete" + * either "favorite" or "unfavorite" * * @since [version] * @@ -1965,7 +2007,6 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri if ( 'favorite' === $status ) { $this->insert_favorite_postmeta( $object_id, $trigger ); } elseif ( 'unfavorite' === $status ) { - // TODO $this->insert_unfavorite_postmeta( $object_id, $trigger ); } diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 4c7c277df6..50eb167e84 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -18,7 +18,34 @@ is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> - +
+ + + + + + + + + + 12, + 'classes' => 'llms-button-secondary auto button', + 'id' => 'llms_mark_unfavorite', + 'value' => apply_filters( 'lifterlms_mark_lesson_unfavorite_button_text', __( '', 'lifterlms' ), $lesson ), + 'last_column' => true, + 'name' => 'mark_unfavorite', + 'required' => false, + 'type' => 'submit', + ) + ); + ?> + + + +
From fd8bab9202a4eea2a78a2411304175019ca57f5a Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 13 Mar 2023 17:07:55 +0530 Subject: [PATCH 004/125] Added favorite object (Lesson) count --- includes/functions/llms.functions.course.php | 31 +++++++++++++++++++- includes/models/model.llms.student.php | 2 +- templates/course/favorite.php | 4 +++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/includes/functions/llms.functions.course.php b/includes/functions/llms.functions.course.php index f987c74f24..c8136c5046 100644 --- a/includes/functions/llms.functions.course.php +++ b/includes/functions/llms.functions.course.php @@ -5,7 +5,7 @@ * @package LifterLMS/Functions * * @since Unknown - * @version 3.37.13 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -51,3 +51,32 @@ function get_lesson( $the_lesson = false, $args = array() ) { return new LLMS_Lesson( $the_lesson, $args ); } + +/** + * Get Favorites Count + * + * @since [version] + * + * @param WP_Post|int|false $object_id Lesson post object or id. If `false` uses the global `$post` object. + * @param array $args Arguments to pass to the LLMS_Lesson Constructor. + * @return Favorites Count + */ +function get_total_favorites( $object_id = false, $meta_key, $meta_value = '', $args = array() ) { + + global $wpdb; + + $key = $meta_key ? $wpdb->prepare( 'AND meta_key = %s', $meta_key ) : ''; + + // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared + $res = $wpdb->get_results( + $wpdb->prepare( + "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta + WHERE post_id = %d {$key} ORDER BY updated_date DESC", + $object_id + ) + ); + // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared + + return count( $res ); + +} diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index bdb5861c2b..847d2a6d2a 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1268,7 +1268,7 @@ private function insert_favorite_postmeta( $object_id, $trigger = 'unspecified' // Add info to the user postmeta table. $user_metadatas = array( '_is_favorite' => 'yes', - '_completion_trigger' => $trigger, + '_favorite_trigger' => $trigger, ); $update = llms_update_user_postmeta( $this->get_id(), $object_id, '_favorite', true ); diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 50eb167e84..047a5a1a6f 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -80,6 +80,10 @@ +
+ get( 'id' ), '_favorite' ); ?> +
+ From 45b98c2ff4f396ca04921720ed80d72fe2827b43 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 13 Mar 2023 19:06:59 +0530 Subject: [PATCH 005/125] PHPCS fixes --- includes/functions/llms.functions.course.php | 2 +- includes/functions/llms.functions.person.php | 9 ++++--- includes/models/model.llms.student.php | 25 ++++++++++---------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/includes/functions/llms.functions.course.php b/includes/functions/llms.functions.course.php index c8136c5046..e01766acf8 100644 --- a/includes/functions/llms.functions.course.php +++ b/includes/functions/llms.functions.course.php @@ -62,7 +62,7 @@ function get_lesson( $the_lesson = false, $args = array() ) { * @return Favorites Count */ function get_total_favorites( $object_id = false, $meta_key, $meta_value = '', $args = array() ) { - + global $wpdb; $key = $meta_key ? $wpdb->prepare( 'AND meta_key = %s', $meta_key ) : ''; diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index 9bbe21a92f..cfa62b54a1 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -330,7 +330,6 @@ function llms_is_user_enrolled( $user_id, $product_id, $relation = 'all', $use_c * @return boolean */ function llms_mark_complete( $user_id, $object_id, $object_type, $trigger = 'unspecified' ) { - // die('minus one'); $student = new LLMS_Student( $user_id ); return $student->mark_complete( $object_id, $object_type, $trigger ); } @@ -354,7 +353,7 @@ function llms_mark_incomplete( $user_id, $object_id, $object_type, $trigger = 'u } /** - * Mark an object as favorite + * Mark an object as favorite. * * @since [version] * @@ -377,9 +376,9 @@ function llms_mark_favorite( $user_id, $object_id, $object_type ) { * * @see LLMS_Student->mark_unfavorite() * - * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param int $object_type Object type [lesson|section|course|track]. + * @param int $user_id WP User ID. + * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @param int $object_type Object type [lesson|section|course|track]. * @return boolean */ function llms_mark_unfavorite( $user_id, $object_id, $object_type ) { diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 847d2a6d2a..e5ff280fa8 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1255,19 +1255,21 @@ private function insert_incompletion_postmeta( $object_id, $trigger = 'unspecifi } /** - * Add student postmeta data when lesson is favorited + * Add student postmeta data when lesson is favorited. * - * @param int $object_id WP Post ID of the lesson - * @param string $trigger String describing the reason for mark completion + * @since [version] + * + * @see LLMS_Student->mark_favorite() + * + * @param int $object_id WP User ID. + * @param int $trigger WP Post ID of the Lesson, Section, Track, or Course. * @return boolean - * @since [version] - * @version [version] */ private function insert_favorite_postmeta( $object_id, $trigger = 'unspecified' ) { // Add info to the user postmeta table. $user_metadatas = array( - '_is_favorite' => 'yes', + '_is_favorite' => 'yes', '_favorite_trigger' => $trigger, ); @@ -1279,10 +1281,10 @@ private function insert_favorite_postmeta( $object_id, $trigger = 'unspecified' } /** - * Remove student postmeta data when lesson is unfavorited + * Remove student postmeta data when lesson is unfavorited. * - * @param int $object_id WP Post ID of the lesson - * @param string $trigger String describing the reason for mark completion + * @param int $object_id WP Post ID of the lesson. + * @param string $trigger String describing the reason for mark completion. * @return boolean * @since [version] * @version [version] @@ -1887,8 +1889,8 @@ private function update_completion_status( $status, $object_id, $object_type, $t /** * Determine if the student has favorited a lesson * - * @param int $object_id WP Post ID of a course or lesson or section or the term id of the track - * @param string $type Object type (course, lesson, section, or track) + * @param int $object_id WP Post ID of a course or lesson or section or the term id of the track + * @param string $object_type Object type (course, lesson, section, or track) * @return boolean * @since [version] * @version [version] @@ -1974,7 +1976,6 @@ public function mark_unfavorite( $object_id, $object_type ) { * @param string $trigger String describing the reason for the status change. * @return boolean */ - private function update_favorite_status( $status, $object_id, $object_type, $trigger = 'unspecified' ) { $student_id = $this->get_id(); From f9663e5b58215b83cc2b0121d81951f882af6a71 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Tue, 14 Mar 2023 14:40:21 +0530 Subject: [PATCH 006/125] Doc Blocks and PHPCS fixes --- .../class.llms.controller.favorites.php | 17 ++++-- ...ass.llms.controller.lesson.progression.php | 2 +- includes/functions/llms.functions.course.php | 5 +- includes/llms.template.functions.php | 4 +- includes/models/model.llms.student.php | 55 +++++++++++-------- templates/course/favorite.php | 13 ++++- 6 files changed, 61 insertions(+), 35 deletions(-) diff --git a/includes/controllers/class.llms.controller.favorites.php b/includes/controllers/class.llms.controller.favorites.php index 2681fdddd5..d81cecb254 100644 --- a/includes/controllers/class.llms.controller.favorites.php +++ b/includes/controllers/class.llms.controller.favorites.php @@ -91,12 +91,21 @@ public function handle_favorite_form() { /** * Filter to modify the user id instead of current logged in user id. * - * @param int $user_id User id to mark lesson as favorite. - * * @since [version] + * + * @param int $user_id User id to mark lesson as favorite. */ $user_id = apply_filters( 'llms_object_favorite_user_id', get_current_user_id() ); + /** + * Action triggered for saving the favorite object to it's own postmeta. + * + * @since [version] + * + * @param int $user_id User ID who is marking object as favorite. + * @param int $object_id Object ID (Lesson, Course or Instructor). + * @param string $object_type Object description string (Lesson, Course or Instructor). + */ do_action( 'llms_trigger_object_favorite', $user_id, $object_id, $object_type ); } @@ -123,9 +132,9 @@ public function handle_unfavorite_form() { /** * Filter to modify the user id instead of current logged in user id. * - * @param int $user_id User id to mark object as unfavorite. - * * @since [version] + * + * @param int $user_id User id to mark lesson as favorite. */ $user_id = apply_filters( 'llms_object_unfavorite_user_id', get_current_user_id() ); diff --git a/includes/controllers/class.llms.controller.lesson.progression.php b/includes/controllers/class.llms.controller.lesson.progression.php index de703a995f..c138644720 100644 --- a/includes/controllers/class.llms.controller.lesson.progression.php +++ b/includes/controllers/class.llms.controller.lesson.progression.php @@ -44,7 +44,7 @@ public function __construct() { * * @since 3.29.0 * - * @param String $action Form action, either "complete" or "incomplete". + * @param string $action Form action, either "complete" or "incomplete". * @return int|null Returns `null` when either required post fields are missing or if the lesson_id is non-numeric, int (lesson id) on success. */ private function get_lesson_id_from_form_data( $action ) { diff --git a/includes/functions/llms.functions.course.php b/includes/functions/llms.functions.course.php index e01766acf8..c4b86e9387 100644 --- a/includes/functions/llms.functions.course.php +++ b/includes/functions/llms.functions.course.php @@ -57,11 +57,12 @@ function get_lesson( $the_lesson = false, $args = array() ) { * * @since [version] * - * @param WP_Post|int|false $object_id Lesson post object or id. If `false` uses the global `$post` object. + * @param WP_Post|int|false $object_id Lesson post object or id. If `false` uses the global `$post` object. + * @param string $meta_key Optional meta key to use in the WHERE condition. * @param array $args Arguments to pass to the LLMS_Lesson Constructor. * @return Favorites Count */ -function get_total_favorites( $object_id = false, $meta_key, $meta_value = '', $args = array() ) { +function get_total_favorites( $object_id = false, $meta_key, $args = array() ) { global $wpdb; diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index c663b1d87c..618ba013ea 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -386,7 +386,9 @@ function lifterlms_template_single_parent_course() { } /** - * Favorite Lesson Template Include + * Favorite Lesson Template Include. + * + * @since [version] * * @return void */ diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index e5ff280fa8..04fa07d1fa 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1283,13 +1283,13 @@ private function insert_favorite_postmeta( $object_id, $trigger = 'unspecified' /** * Remove student postmeta data when lesson is unfavorited. * + * @since [version] + * * @param int $object_id WP Post ID of the lesson. * @param string $trigger String describing the reason for mark completion. * @return boolean - * @since [version] - * @version [version] */ - private function insert_unfavorite_postmeta( $object_id, $trigger = 'unspecified' ) { + private function remove_unfavorite_postmeta( $object_id, $trigger = 'unspecified' ) { $update = llms_delete_user_postmeta( $this->get_id(), $object_id, '_favorite', true ); @@ -1889,11 +1889,11 @@ private function update_completion_status( $status, $object_id, $object_type, $t /** * Determine if the student has favorited a lesson * + * @since [version] + * * @param int $object_id WP Post ID of a course or lesson or section or the term id of the track * @param string $object_type Object type (course, lesson, section, or track) * @return boolean - * @since [version] - * @version [version] */ public function is_favorite( $object_id, $object_type = 'course' ) { @@ -1909,6 +1909,18 @@ public function is_favorite( $object_id, $object_type = 'course' ) { $ret = $query->has_results(); + /** + * Filter object favorite boolean value prior to returning + * + * The dynamic portion of this filter, `{$object_type}`, refers to the Lesson, Course or Instructor. + * + * @since [version] + * + * @param array|false $ret Array of favorite data or `false` if no favorite is found. + * @param int $object_id Object ID (Lesson, Course or Instructor). + * @param string $object_type Object description string (Lesson, Course or Instructor). + * @param LLMS_Student $instance The Student Instance + */ return apply_filters( 'llms_is_' . $object_type . '_favorite', $ret, $object_id, $object_type, $this ); } @@ -1916,14 +1928,13 @@ public function is_favorite( $object_id, $object_type = 'course' ) { /** * Mark a lesson, section, course, or track favorite for the given user * + * @since [version] + * + * @see llms_mark_favorite() calls this function without having to instantiate the LLMS_Student class first + * * @param int $object_id WP Post ID of the lesson, section, course, or track * @param string $object_type object type [lesson|section|course|track] * @return boolean - * - * @see llms_mark_favorite() calls this function without having to instantiate the LLMS_Student class first - * - * @since [version] - * @version [version] */ public function mark_favorite( $object_id, $object_type ) { @@ -1939,14 +1950,13 @@ public function mark_favorite( $object_id, $object_type ) { /** * Mark a lesson, section, course, or track unfavorite for the given user * - * @param int $object_id WP Post ID of the lesson, section, course, or track - * @param string $object_type object type [lesson|section|course|track] - * @return boolean + * @since [version] * * @see llms_mark_unfavorite() calls this function without having to instantiate the LLMS_Student class first * - * @since [version] - * @version [version] + * @param int $object_id WP Post ID of the lesson, section, course, or track + * @param string $object_type object type [lesson|section|course|track] + * @return boolean */ public function mark_unfavorite( $object_id, $object_type ) { @@ -1960,17 +1970,14 @@ public function mark_unfavorite( $object_id, $object_type ) { } /** - * Update the favorite status of a track, course, section, or lesson for the current student - * * Triggers actions for favorite/unfavorite. * + * Update the favorite status of a track, course, section, or lesson for the current student * Inserts / updates necessary user postmeta data. * - * @since [version] - * @since 4.2.0 Use filterable functions to determine if the object is completable. - * Added filter to allow customization of object parent data. + * @since [version] Use filterable functions to determine if the object can be marked favorite. * - * @param string $status New status to update to, either "complete" or "incomplete". + * @param string $status New status to update to, either "favorite" or "unfavorite". * @param int $object_id WP_Post ID of the object. * @param string $object_type The type of object. A lesson, section, course, or course_track. * @param string $trigger String describing the reason for the status change. @@ -2004,15 +2011,15 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri return false; } - // Insert meta data. + // Insert / Remove meta data. if ( 'favorite' === $status ) { $this->insert_favorite_postmeta( $object_id, $trigger ); } elseif ( 'unfavorite' === $status ) { - $this->insert_unfavorite_postmeta( $object_id, $trigger ); + $this->remove_unfavorite_postmeta( $object_id, $trigger ); } /** - * Hook that fires when a student's completion status is updated for any object. + * Hook that fires when a student's favorite status is updated for any object. * * The dynamic portion of this hook, `$status`, refers to the new favorite status of the object, * either "favorite" or "unfavorite" diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 047a5a1a6f..368c776f2b 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -1,9 +1,16 @@ Date: Tue, 14 Mar 2023 15:01:59 +0530 Subject: [PATCH 007/125] Fixed PHPCS tabs errors --- includes/models/model.llms.student.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 04fa07d1fa..d6c5f8dfca 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1916,10 +1916,10 @@ public function is_favorite( $object_id, $object_type = 'course' ) { * * @since [version] * - * @param array|false $ret Array of favorite data or `false` if no favorite is found. - * @param int $object_id Object ID (Lesson, Course or Instructor). - * @param string $object_type Object description string (Lesson, Course or Instructor). - * @param LLMS_Student $instance The Student Instance + * @param array|false $ret Array of favorite data or `false` if no favorite is found. + * @param int $object_id Object ID (Lesson, Course or Instructor). + * @param string $object_type Object description string (Lesson, Course or Instructor). + * @param LLMS_Student $instance The Student Instance */ return apply_filters( 'llms_is_' . $object_type . '_favorite', $ret, $object_id, $object_type, $this ); From 761452165d418e4cde23a39924f814fc031c02fc Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 16 Mar 2023 16:53:28 +0530 Subject: [PATCH 008/125] Added AJAX handler for favorite/unfavorite --- assets/js/llms-favorites.js | 80 +++++++++++++++++++++++++ includes/assets/llms-assets-scripts.php | 4 ++ includes/class.llms.ajax.php | 43 +++++++++++++ includes/class.llms.frontend.assets.php | 5 ++ templates/course/favorite.php | 23 +++---- 5 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 assets/js/llms-favorites.js diff --git a/assets/js/llms-favorites.js b/assets/js/llms-favorites.js new file mode 100644 index 0000000000..98ec13e8b7 --- /dev/null +++ b/assets/js/llms-favorites.js @@ -0,0 +1,80 @@ +;/* global LLMS, $ */ +/* jshint strict: true */ + +/** + * Front End Favorite Class + * + * @type {Object} + * @since [version] + * @version [version] + */ +( function( $ ) { + + var favorite = { + + /** + * Bind DOM events + * + * @return void + * @since [version] + * @version [version] + */ + bind: function() { + + var self = this; + + // Favorite clicked + $( '.llms-favorite-wrapper .llms-favorite-action' ).on( 'click', function( e ) { + e.preventDefault(); + self.favorite( $( this ) ); + } ); + + }, + + /** + * Favorite / Unfavorite an object + * + * @param obj $btn jQuery object for the "Favorite / Unfavorite" button + * @return void + * @since [version] + * @version [version] + */ + favorite: function( $btn ) { + + var self = this, + object_id = $btn.attr( 'data-id' ), + object_type = $btn.attr( 'data-type' ) + user_action = $btn.attr( 'data-action' ); + + console.log( 'before ajax', object_id, object_type, user_action ); + + LLMS.Ajax.call( { + data: { + action: 'favorite_object', + object_id: object_id, + object_type: object_type, + user_action: user_action + }, + beforeSend: function() { + + console.log('before send'); + + }, + success: function( r ) { + + console.log('result', r ); + + } + + } ); + + } + + }; + + favorite.bind(); + + window.llms = window.llms || {}; + window.llms.favorites = favorite; + +} )( jQuery ); diff --git a/includes/assets/llms-assets-scripts.php b/includes/assets/llms-assets-scripts.php index c8bf2773d8..434485c026 100644 --- a/includes/assets/llms-assets-scripts.php +++ b/includes/assets/llms-assets-scripts.php @@ -33,6 +33,7 @@ * @since 6.0.0 Added llms-admin-certificate-editor. * @since 6.10.0 Added llms-quill-wordcount. * @since 7.0.0 Added llms-spinner. + * @since [version] Added llms-favorites. */ return array( @@ -49,6 +50,9 @@ 'llms-quiz' => array( 'dependencies' => array( 'jquery', 'llms', 'wp-mediaelement' ), ), + 'llms-favorites' => array( + 'dependencies' => array( 'jquery', 'llms' ), + ), // Admin. 'llms-addons' => array( diff --git a/includes/class.llms.ajax.php b/includes/class.llms.ajax.php index 1a3bdde781..49000ebb4e 100644 --- a/includes/class.llms.ajax.php +++ b/includes/class.llms.ajax.php @@ -38,6 +38,7 @@ class LLMS_AJAX { * @since 4.0.0 Stop registering previously deprecated actions. * @since 5.9.0 Move `check_voucher_duplicate()` to `LLMS_AJAX_Handler`. * @since 6.0.0 Removed loading of class files that don't instantiate their class in favor of autoloading. + * @since [version] Added `favorite_object` ajax event. * * @return void */ @@ -45,6 +46,7 @@ public function __construct() { $ajax_events = array( 'query_quiz_questions' => false, + 'favorite_object' => false, ); foreach ( $ajax_events as $ajax_event => $nopriv ) { @@ -207,6 +209,47 @@ public function query_quiz_questions() { } + /** + * Add Favorite / Unfavorite Postmeta for an object. + * + * @since [version] + * + * @return void + */ + public function favorite_object() { + + // Grab the data if it exists. + $user_action = array_key_exists( 'user_action', $_REQUEST ) ? llms_filter_input_sanitize_string( INPUT_POST, 'user_action' ) : ''; + $object_id = array_key_exists( 'object_id', $_REQUEST ) ? llms_filter_input( INPUT_POST, 'object_id', FILTER_SANITIZE_NUMBER_INT ) : 0; + $object_type = array_key_exists( 'object_type', $_REQUEST ) ? llms_filter_input_sanitize_string( INPUT_POST, 'object_type' ) : ''; + + if ( is_null( $object_id ) ) { + return; + } + + /** + * Filter to modify the user id instead of current logged in user id. + * + * @since [version] + * + * @param int $user_id User id to mark lesson as favorite. + */ + $user_id = apply_filters( 'llms_object_favorite_user_id', get_current_user_id() ); + + if ( 'favorite' === $user_action ) { + + $r = llms_mark_favorite( $user_id, $object_id, $object_type ); + + } elseif ( 'unfavorite' === $user_action ) { + + $r = llms_mark_unfavorite( $user_id, $object_id, $object_type ); + + } + + wp_die(); + + } + } new LLMS_AJAX(); diff --git a/includes/class.llms.frontend.assets.php b/includes/class.llms.frontend.assets.php index a8eed0a3ab..9635eac0dd 100644 --- a/includes/class.llms.frontend.assets.php +++ b/includes/class.llms.frontend.assets.php @@ -166,6 +166,7 @@ public static function enqueue_styles() { * Moved inline scripts to `enqueue_inline_scripts()`. * @since 5.0.0 Enqueue locale data and dependencies on account and checkout pages for searchable dropdowns for country & state. * Remove password strength inline enqueue. + * @since [version] Enqueue `llms-favorites` script. * * @return void */ @@ -200,6 +201,10 @@ public static function enqueue_scripts() { llms()->assets->enqueue_script( 'llms-quiz' ); } + if ( is_lesson() ) { // TODO: check if favorites is enabled and enqueue script on right places + llms()->assets->enqueue_script( 'llms-favorites' ); + } + llms()->assets->register_script( 'llms-iziModal' ); if ( is_llms_account_page() ) { llms()->assets->enqueue_script( 'llms-iziModal' ); diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 368c776f2b..c4bdc75814 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -17,6 +17,7 @@ $lesson = new LLMS_Lesson( $post->ID ); $student = llms_get_student( get_current_user_id() ); +$total_favorites = get_total_favorites( $lesson->get( 'id' ) ); ?>
@@ -24,7 +25,10 @@ is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> - + + + +
@@ -39,9 +43,9 @@ llms_form_field( array( 'columns' => 12, - 'classes' => 'llms-button-secondary auto button', + 'classes' => 'llms-button-secondary', 'id' => 'llms_mark_unfavorite', - 'value' => apply_filters( 'lifterlms_mark_lesson_unfavorite_button_text', __( '', 'lifterlms' ), $lesson ), + 'value' => apply_filters( 'lifterlms_mark_lesson_unfavorite_button_text', sprintf( ' %d', $total_favorites ), $lesson ), 'last_column' => true, 'name' => 'mark_unfavorite', 'required' => false, @@ -55,6 +59,9 @@
+ + +
@@ -69,10 +76,10 @@ 1, - 'classes' => 'auto button llms-favorite', + 'columns' => 12, + 'classes' => 'llms-button-primary button llms-favorite', 'id' => 'llms_mark_favorite', - 'value' => apply_filters( 'lifterlms_mark_lesson_favorite_button_text', __( '', 'lifterlms' ), $lesson ), + 'value' => apply_filters( 'lifterlms_mark_lesson_favorite_button_text', sprintf( ' %d', $total_favorites ), $lesson ), 'last_column' => true, 'name' => 'mark_favorite', 'required' => false, @@ -87,10 +94,6 @@ -
- get( 'id' ), '_favorite' ); ?> -
-
From 8ea82eccf0ae71eecfb3ed1b5e447aaf31e219c2 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 16 Mar 2023 16:57:48 +0530 Subject: [PATCH 009/125] =?UTF-8?q?Minor=20fixes,=20form=20handler=20repla?= =?UTF-8?q?ced=20with=20AJAX=20=F0=9F=92=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/class-llms-loader.php | 1 - .../class.llms.controller.favorites.php | 169 ------------------ includes/functions/llms.functions.course.php | 10 +- 3 files changed, 4 insertions(+), 176 deletions(-) delete mode 100644 includes/controllers/class.llms.controller.favorites.php diff --git a/includes/class-llms-loader.php b/includes/class-llms-loader.php index e8d79430d0..a952d0ce3e 100644 --- a/includes/class-llms-loader.php +++ b/includes/class-llms-loader.php @@ -286,7 +286,6 @@ public function includes() { require_once LLMS_PLUGIN_DIR . 'includes/controllers/class-llms-controller-awards.php'; require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.certificates.php'; require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.lesson.progression.php'; - require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.favorites.php'; require_once LLMS_PLUGIN_DIR . 'includes/controllers/class-llms-controller-checkout.php'; // Added out of alpha order to preserve action load order. require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.orders.php'; require_once LLMS_PLUGIN_DIR . 'includes/controllers/class.llms.controller.quizzes.php'; diff --git a/includes/controllers/class.llms.controller.favorites.php b/includes/controllers/class.llms.controller.favorites.php deleted file mode 100644 index d81cecb254..0000000000 --- a/includes/controllers/class.llms.controller.favorites.php +++ /dev/null @@ -1,169 +0,0 @@ -get_object_id_from_form_data( 'favorite' ); - $object_type = llms_filter_input( INPUT_POST, 'type' ); - - if ( is_null( $object_id ) ) { - return; - } - - /** - * Filter to modify the user id instead of current logged in user id. - * - * @since [version] - * - * @param int $user_id User id to mark lesson as favorite. - */ - $user_id = apply_filters( 'llms_object_favorite_user_id', get_current_user_id() ); - - /** - * Action triggered for saving the favorite object to it's own postmeta. - * - * @since [version] - * - * @param int $user_id User ID who is marking object as favorite. - * @param int $object_id Object ID (Lesson, Course or Instructor). - * @param string $object_type Object description string (Lesson, Course or Instructor). - */ - do_action( 'llms_trigger_object_favorite', $user_id, $object_id, $object_type ); - - } - - /** - * Mark Object as unfavorite - * - * + Unfavorite Object form post. - * + Marks Object as Unfavorite. - * - * @since [version] - * - * @return void - */ - public function handle_unfavorite_form() { - - $object_id = $this->get_object_id_from_form_data( 'unfavorite' ); - $object_type = llms_filter_input( INPUT_POST, 'type' ); - - if ( is_null( $object_id ) ) { - return; - } - - /** - * Filter to modify the user id instead of current logged in user id. - * - * @since [version] - * - * @param int $user_id User id to mark lesson as favorite. - */ - $user_id = apply_filters( 'llms_object_unfavorite_user_id', get_current_user_id() ); - - // Mark unfavorite. - llms_mark_unfavorite( $user_id, $object_id, $object_type ); - - } - - /** - * Handle favoriting of object via `llms_trigger_object_favorite` action - * - * @since [version] - * - * @param int $user_id User ID. - * @param int $object_id Object ID. - * @param string $object_type Object description string (Lesson, Course or Instructor). - * @param array $args Optional arguments. - * @return void - */ - public function mark_favorite( $user_id, $object_id, $object_type = '', $args = array() ) { - - if ( llms_allow_lesson_completion( $user_id, $object_id, $object_type, $args ) ) { - - llms_mark_favorite( $user_id, $object_id, $object_type ); - - } - - } - -} - -return new LLMS_Controller_Favorite(); diff --git a/includes/functions/llms.functions.course.php b/includes/functions/llms.functions.course.php index c4b86e9387..087e722a13 100644 --- a/includes/functions/llms.functions.course.php +++ b/includes/functions/llms.functions.course.php @@ -58,22 +58,20 @@ function get_lesson( $the_lesson = false, $args = array() ) { * @since [version] * * @param WP_Post|int|false $object_id Lesson post object or id. If `false` uses the global `$post` object. - * @param string $meta_key Optional meta key to use in the WHERE condition. * @param array $args Arguments to pass to the LLMS_Lesson Constructor. * @return Favorites Count */ -function get_total_favorites( $object_id = false, $meta_key, $args = array() ) { +function get_total_favorites( $object_id = false, $args = array() ) { global $wpdb; - $key = $meta_key ? $wpdb->prepare( 'AND meta_key = %s', $meta_key ) : ''; - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $res = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta - WHERE post_id = %d {$key} ORDER BY updated_date DESC", - $object_id + WHERE post_id = %d AND meta_key = %s ORDER BY updated_date DESC", + $object_id, + '_favorite' ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared From e38a0050274de4543292f774f3d0cc451558ff4a Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 16 Mar 2023 17:32:28 +0530 Subject: [PATCH 010/125] Added AJAX function to update data --- assets/js/llms-favorites.js | 41 ++++++++++++++----- templates/course/favorite.php | 74 +++++------------------------------ 2 files changed, 41 insertions(+), 74 deletions(-) diff --git a/assets/js/llms-favorites.js b/assets/js/llms-favorites.js index 98ec13e8b7..4119b89b5e 100644 --- a/assets/js/llms-favorites.js +++ b/assets/js/llms-favorites.js @@ -12,6 +12,13 @@ var favorite = { + /** + * Main Favorite Container Element + * + * @type obj + */ + $container: null, + /** * Bind DOM events * @@ -24,7 +31,7 @@ var self = this; // Favorite clicked - $( '.llms-favorite-wrapper .llms-favorite-action' ).on( 'click', function( e ) { + $( '.llms-favorite-wrapper' ).on( 'click', '.llms-heart-btn', function( e ) { e.preventDefault(); self.favorite( $( this ) ); } ); @@ -41,13 +48,13 @@ */ favorite: function( $btn ) { - var self = this, + this.$container = $btn.closest( '.llms-favorite-wrapper' ); + + var self = this, object_id = $btn.attr( 'data-id' ), - object_type = $btn.attr( 'data-type' ) + object_type = $btn.attr( 'data-type' ), user_action = $btn.attr( 'data-action' ); - console.log( 'before ajax', object_id, object_type, user_action ); - LLMS.Ajax.call( { data: { action: 'favorite_object', @@ -55,14 +62,28 @@ object_type: object_type, user_action: user_action }, - beforeSend: function() { + beforeSend: function() {}, + success: function( r ) { + + if( r.success ) { - console.log('before send'); + if( 'favorite' === user_action ) { - }, - success: function( r ) { + $btn.removeClass( 'fa-heart-o' ).addClass( 'fa-heart' ); + $( $btn ).attr( 'data-action', 'unfavorite' ); + + } else if ( 'unfavorite' === user_action ) { + + $btn.removeClass( 'fa-heart' ).addClass( 'fa-heart-o' ); + $( $btn ).attr( 'data-action', 'favorite' ); + + } + + // Updating count. + self.$container.find( '.llms-favorites-count' ).text( r.total_favorites ); + - console.log('result', r ); + } } diff --git a/templates/course/favorite.php b/templates/course/favorite.php index c4bdc75814..7d56563424 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -15,8 +15,8 @@ global $post; -$lesson = new LLMS_Lesson( $post->ID ); -$student = llms_get_student( get_current_user_id() ); +$lesson = new LLMS_Lesson( $post->ID ); +$student = llms_get_student( get_current_user_id() ); $total_favorites = get_total_favorites( $lesson->get( 'id' ) ); ?> @@ -26,74 +26,20 @@ is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> - - - - - - - - - - - - - - 12, - 'classes' => 'llms-button-secondary', - 'id' => 'llms_mark_unfavorite', - 'value' => apply_filters( 'lifterlms_mark_lesson_unfavorite_button_text', sprintf( ' %d', $total_favorites ), $lesson ), - 'last_column' => true, - 'name' => 'mark_unfavorite', - 'required' => false, - 'type' => 'submit', - ) - ); - ?> - - - - + + - - - -
- - - - - - - - - - 12, - 'classes' => 'llms-button-primary button llms-favorite', - 'id' => 'llms_mark_favorite', - 'value' => apply_filters( 'lifterlms_mark_lesson_favorite_button_text', sprintf( ' %d', $total_favorites ), $lesson ), - 'last_column' => true, - 'name' => 'mark_favorite', - 'required' => false, - 'type' => 'submit', - ) - ); - ?> - - - -
+ + + + + + From 771adbe963ed10a6e1500d871491d968c1a696ff Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Fri, 17 Mar 2023 00:58:55 +0530 Subject: [PATCH 011/125] =?UTF-8?q?Minor=20fixes=20=F0=9F=9B=A0=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/js/llms-favorites.js | 10 ++--- includes/class-llms-loader.php | 1 - includes/class.llms.ajax.php | 23 +++++----- includes/class.llms.frontend.assets.php | 2 +- includes/functions/llms.functions.course.php | 7 ++-- includes/functions/llms.functions.person.php | 2 +- includes/models/model.llms.student.php | 44 ++++++++------------ templates/course/favorite.php | 2 +- 8 files changed, 40 insertions(+), 51 deletions(-) diff --git a/assets/js/llms-favorites.js b/assets/js/llms-favorites.js index 4119b89b5e..bc29a013aa 100644 --- a/assets/js/llms-favorites.js +++ b/assets/js/llms-favorites.js @@ -2,7 +2,7 @@ /* jshint strict: true */ /** - * Front End Favorite Class + * Front End Favorite Class. * * @type {Object} * @since [version] @@ -13,14 +13,14 @@ var favorite = { /** - * Main Favorite Container Element + * Main Favorite Container Element. * * @type obj */ $container: null, /** - * Bind DOM events + * Bind DOM events. * * @return void * @since [version] @@ -39,9 +39,9 @@ }, /** - * Favorite / Unfavorite an object + * Favorite / Unfavorite an object. * - * @param obj $btn jQuery object for the "Favorite / Unfavorite" button + * @param obj $btn jQuery object for the "Favorite / Unfavorite" button. * @return void * @since [version] * @version [version] diff --git a/includes/class-llms-loader.php b/includes/class-llms-loader.php index a952d0ce3e..eae5d74761 100644 --- a/includes/class-llms-loader.php +++ b/includes/class-llms-loader.php @@ -232,7 +232,6 @@ public function autoload( $class ) { * Removed loading of class files that don't instantiate their class in favor of autoloading. * @since 6.4.0 Included `LLMS_Shortcodes` before `LLMS_Controller_Orders`. * @since 7.0.0 Include `LLMS_Controller_Checkout`. - * @since [version] Include `LLMS_Controller_Favorite`. * * @return void */ diff --git a/includes/class.llms.ajax.php b/includes/class.llms.ajax.php index 49000ebb4e..96bb3bd45c 100644 --- a/includes/class.llms.ajax.php +++ b/includes/class.llms.ajax.php @@ -219,23 +219,15 @@ public function query_quiz_questions() { public function favorite_object() { // Grab the data if it exists. - $user_action = array_key_exists( 'user_action', $_REQUEST ) ? llms_filter_input_sanitize_string( INPUT_POST, 'user_action' ) : ''; - $object_id = array_key_exists( 'object_id', $_REQUEST ) ? llms_filter_input( INPUT_POST, 'object_id', FILTER_SANITIZE_NUMBER_INT ) : 0; - $object_type = array_key_exists( 'object_type', $_REQUEST ) ? llms_filter_input_sanitize_string( INPUT_POST, 'object_type' ) : ''; + $user_action = llms_filter_input_sanitize_string( INPUT_POST, 'user_action' ); + $object_id = llms_filter_input( INPUT_POST, 'object_id', FILTER_SANITIZE_NUMBER_INT ); + $object_type = llms_filter_input_sanitize_string( INPUT_POST, 'object_type' ); + $user_id = get_current_user_id(); if ( is_null( $object_id ) ) { return; } - /** - * Filter to modify the user id instead of current logged in user id. - * - * @since [version] - * - * @param int $user_id User id to mark lesson as favorite. - */ - $user_id = apply_filters( 'llms_object_favorite_user_id', get_current_user_id() ); - if ( 'favorite' === $user_action ) { $r = llms_mark_favorite( $user_id, $object_id, $object_type ); @@ -246,6 +238,13 @@ public function favorite_object() { } + echo json_encode( + array( + 'total_favorites' => get_total_favorites( $object_id ), + 'success' => true, + ) + ); + wp_die(); } diff --git a/includes/class.llms.frontend.assets.php b/includes/class.llms.frontend.assets.php index 9635eac0dd..be9f2761e3 100644 --- a/includes/class.llms.frontend.assets.php +++ b/includes/class.llms.frontend.assets.php @@ -201,7 +201,7 @@ public static function enqueue_scripts() { llms()->assets->enqueue_script( 'llms-quiz' ); } - if ( is_lesson() ) { // TODO: check if favorites is enabled and enqueue script on right places + if ( is_lesson() ) { // TODO: check if favorites is enabled and enqueue script on right places. llms()->assets->enqueue_script( 'llms-favorites' ); } diff --git a/includes/functions/llms.functions.course.php b/includes/functions/llms.functions.course.php index 087e722a13..7484d50fca 100644 --- a/includes/functions/llms.functions.course.php +++ b/includes/functions/llms.functions.course.php @@ -53,15 +53,14 @@ function get_lesson( $the_lesson = false, $args = array() ) { } /** - * Get Favorites Count + * Get Favorites Count. * * @since [version] * * @param WP_Post|int|false $object_id Lesson post object or id. If `false` uses the global `$post` object. - * @param array $args Arguments to pass to the LLMS_Lesson Constructor. - * @return Favorites Count + * @return int */ -function get_total_favorites( $object_id = false, $args = array() ) { +function get_total_favorites( $object_id = false ) { global $wpdb; diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index cfa62b54a1..b06313e17b 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -370,7 +370,7 @@ function llms_mark_favorite( $user_id, $object_id, $object_type ) { } /** - * Mark a lesson as unfavorite + * Mark a lesson as unfavorite. * * @since [version] * diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index d6c5f8dfca..bf9a3d2c8e 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1262,16 +1262,9 @@ private function insert_incompletion_postmeta( $object_id, $trigger = 'unspecifi * @see LLMS_Student->mark_favorite() * * @param int $object_id WP User ID. - * @param int $trigger WP Post ID of the Lesson, Section, Track, or Course. * @return boolean */ - private function insert_favorite_postmeta( $object_id, $trigger = 'unspecified' ) { - - // Add info to the user postmeta table. - $user_metadatas = array( - '_is_favorite' => 'yes', - '_favorite_trigger' => $trigger, - ); + private function insert_favorite_postmeta( $object_id ) { $update = llms_update_user_postmeta( $this->get_id(), $object_id, '_favorite', true ); @@ -1285,11 +1278,10 @@ private function insert_favorite_postmeta( $object_id, $trigger = 'unspecified' * * @since [version] * - * @param int $object_id WP Post ID of the lesson. - * @param string $trigger String describing the reason for mark completion. + * @param int $object_id WP Post ID of the lesson * @return boolean */ - private function remove_unfavorite_postmeta( $object_id, $trigger = 'unspecified' ) { + private function remove_unfavorite_postmeta( $object_id ) { $update = llms_delete_user_postmeta( $this->get_id(), $object_id, '_favorite', true ); @@ -1887,15 +1879,15 @@ private function update_completion_status( $status, $object_id, $object_type, $t } /** - * Determine if the student has favorited a lesson + * Determine if the student has favorited a lesson. * * @since [version] * - * @param int $object_id WP Post ID of a course or lesson or section or the term id of the track - * @param string $object_type Object type (course, lesson, section, or track) + * @param int $object_id WP Post ID of a course or lesson or section or the term id of the track. + * @param string $object_type Object type (course, lesson, section, or track). * @return boolean */ - public function is_favorite( $object_id, $object_type = 'course' ) { + public function is_favorite( $object_id, $object_type = 'lesson' ) { $query = new LLMS_Query_User_Postmeta( array( @@ -1926,14 +1918,14 @@ public function is_favorite( $object_id, $object_type = 'course' ) { } /** - * Mark a lesson, section, course, or track favorite for the given user + * Mark a lesson, section, course, or track favorite for the given user. * * @since [version] * - * @see llms_mark_favorite() calls this function without having to instantiate the LLMS_Student class first + * @see llms_mark_favorite() calls this function without having to instantiate the LLMS_Student class first. * - * @param int $object_id WP Post ID of the lesson, section, course, or track - * @param string $object_type object type [lesson|section|course|track] + * @param int $object_id WP Post ID of the lesson, section, course, or track. + * @param string $object_type object type [lesson|section|course|track]. * @return boolean */ public function mark_favorite( $object_id, $object_type ) { @@ -1948,14 +1940,14 @@ public function mark_favorite( $object_id, $object_type ) { } /** - * Mark a lesson, section, course, or track unfavorite for the given user + * Mark a lesson, section, course, or track unfavorite for the given user. * * @since [version] * - * @see llms_mark_unfavorite() calls this function without having to instantiate the LLMS_Student class first + * @see llms_mark_unfavorite() calls this function without having to instantiate the LLMS_Student class first. * - * @param int $object_id WP Post ID of the lesson, section, course, or track - * @param string $object_type object type [lesson|section|course|track] + * @param int $object_id WP Post ID of the lesson, section, course, or track. + * @param string $object_type object type [lesson|section|course|track]. * @return boolean */ public function mark_unfavorite( $object_id, $object_type ) { @@ -1991,7 +1983,7 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * Fires before a student's object favorite status is updated. * * The dynamic portion of this hook, `$status`, refers to the new completion status of the object, - * either "favorite" or "unfavorite" + * either "favorite" or "unfavorite". * * @since [version] * @@ -2022,7 +2014,7 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * Hook that fires when a student's favorite status is updated for any object. * * The dynamic portion of this hook, `$status`, refers to the new favorite status of the object, - * either "favorite" or "unfavorite" + * either "favorite" or "unfavorite". * * @since [version] * @@ -2040,7 +2032,7 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * which the student's completion status is being updated for. * * The dynamic portion of this hook, `$status`, refers to the new completion status of the object, - * either "favorite" or "unfavorite" + * either "favorite" or "unfavorite". * * @since [version] * diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 7d56563424..ab81e46f16 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -1,7 +1,7 @@ Date: Fri, 17 Mar 2023 10:49:19 +0530 Subject: [PATCH 012/125] Added helper for favorites --- includes/functions/llms.functions.course.php | 27 ------------- .../functions/llms.functions.favorite.php | 38 +++++++++++++++++++ includes/llms.functions.core.php | 3 +- 3 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 includes/functions/llms.functions.favorite.php diff --git a/includes/functions/llms.functions.course.php b/includes/functions/llms.functions.course.php index 7484d50fca..61c4fefa8c 100644 --- a/includes/functions/llms.functions.course.php +++ b/includes/functions/llms.functions.course.php @@ -51,30 +51,3 @@ function get_lesson( $the_lesson = false, $args = array() ) { return new LLMS_Lesson( $the_lesson, $args ); } - -/** - * Get Favorites Count. - * - * @since [version] - * - * @param WP_Post|int|false $object_id Lesson post object or id. If `false` uses the global `$post` object. - * @return int - */ -function get_total_favorites( $object_id = false ) { - - global $wpdb; - - // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared - $res = $wpdb->get_results( - $wpdb->prepare( - "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta - WHERE post_id = %d AND meta_key = %s ORDER BY updated_date DESC", - $object_id, - '_favorite' - ) - ); - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared - - return count( $res ); - -} diff --git a/includes/functions/llms.functions.favorite.php b/includes/functions/llms.functions.favorite.php new file mode 100644 index 0000000000..87b3b7b075 --- /dev/null +++ b/includes/functions/llms.functions.favorite.php @@ -0,0 +1,38 @@ +get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching + $wpdb->prepare( + "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta + WHERE post_id = %d AND meta_key = %s ORDER BY updated_date DESC", + $object_id, + '_favorite' + ) + ); + // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared + + return count( $res ); + +} diff --git a/includes/llms.functions.core.php b/includes/llms.functions.core.php index 330578bf54..44024e3cea 100644 --- a/includes/llms.functions.core.php +++ b/includes/llms.functions.core.php @@ -5,7 +5,7 @@ * @package LifterLMS/Functions * * @since 1.0.0 - * @version 6.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -32,6 +32,7 @@ require_once 'functions/llms.functions.quiz.php'; require_once 'functions/llms.functions.template.php'; require_once 'functions/llms.functions.user.postmeta.php'; +require_once 'functions/llms.functions.favorite.php'; if ( ! function_exists( 'llms_anonymize_string' ) ) { /** From 78ac233598e945f0423100caf5f4aee75a82d64f Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Fri, 17 Mar 2023 15:54:04 +0530 Subject: [PATCH 013/125] Added Favorite Design --- assets/scss/frontend/_main.scss | 14 ++++++++++++++ includes/llms.template.functions.php | 15 --------------- includes/llms.template.hooks.php | 1 - includes/models/model.llms.student.php | 4 ++-- templates/course/parent-course.php | 11 ++++++++++- 5 files changed, 26 insertions(+), 19 deletions(-) diff --git a/assets/scss/frontend/_main.scss b/assets/scss/frontend/_main.scss index eee0ceffe3..134ec56cbe 100644 --- a/assets/scss/frontend/_main.scss +++ b/assets/scss/frontend/_main.scss @@ -473,3 +473,17 @@ svg .llms-animated-circle { .llms-lesson-tooltip.active { display: inline-block; } + +// Favorites. +.llms-parent-course-wrapper { + display: flex; + justify-content: space-between; + + .llms-favorite-wrapper { + cursor: pointer; + + .fa-heart { + color: #FF2E2B; + } + } +} diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index 618ba013ea..f85b10d3c2 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -385,21 +385,6 @@ function lifterlms_template_single_parent_course() { } } -/** - * Favorite Lesson Template Include. - * - * @since [version] - * - * @return void - */ -if ( ! function_exists( 'lifterlms_template_single_favorite' ) ) { - - function lifterlms_template_single_favorite() { - - llms_get_template( 'course/favorite.php' ); - } -} - /** * Complete Lesson Link Template Include * diff --git a/includes/llms.template.hooks.php b/includes/llms.template.hooks.php index 2203ccd750..10dc69c517 100644 --- a/includes/llms.template.hooks.php +++ b/includes/llms.template.hooks.php @@ -48,7 +48,6 @@ * @since Unknown */ add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_parent_course', 10 ); -add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_favorite', 10 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_video', 20 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_audio', 20 ); diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index bf9a3d2c8e..82d0d9c158 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1276,9 +1276,9 @@ private function insert_favorite_postmeta( $object_id ) { /** * Remove student postmeta data when lesson is unfavorited. * - * @since [version] + * @since [version] * - * @param int $object_id WP Post ID of the lesson + * @param int $object_id WP Post ID of the lesson * @return boolean */ private function remove_unfavorite_postmeta( $object_id ) { diff --git a/templates/course/parent-course.php b/templates/course/parent-course.php index 83e8d32f24..be3007d94c 100644 --- a/templates/course/parent-course.php +++ b/templates/course/parent-course.php @@ -14,5 +14,14 @@ global $post; $lesson = new LLMS_Lesson( $post ); +?> -printf( __( '', 'lifterlms' ), get_permalink( $lesson->get( 'parent_course' ) ), get_the_title( $lesson->get( 'parent_course' ) ) ); +
+ + Back to: %2$s

', 'lifterlms' ), get_permalink( $lesson->get( 'parent_course' ) ), get_the_title( $lesson->get( 'parent_course' ) ) ); + + llms_get_template( 'course/favorite.php' ); + ?> + +
From 5af16a9a3b1685d43ddf6e1286c16803c8353802 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Fri, 17 Mar 2023 17:10:26 +0530 Subject: [PATCH 014/125] =?UTF-8?q?Fixed=20favorite=20template=20design=20?= =?UTF-8?q?=F0=9F=9B=A0=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit when used with another template. Previous commit reverted and fixed. --- assets/scss/frontend/_main.scss | 22 +++++++++++++++------- includes/llms.template.functions.php | 15 +++++++++++++++ includes/llms.template.hooks.php | 1 + templates/course/parent-course.php | 11 +---------- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/assets/scss/frontend/_main.scss b/assets/scss/frontend/_main.scss index 134ec56cbe..cff5dd21f1 100644 --- a/assets/scss/frontend/_main.scss +++ b/assets/scss/frontend/_main.scss @@ -475,15 +475,23 @@ svg .llms-animated-circle { } // Favorites. -.llms-parent-course-wrapper { - display: flex; - justify-content: space-between; +.llms-parent-course-link { + display: inline; - .llms-favorite-wrapper { - cursor: pointer; + + .llms-favorite-wrapper { + display: inline; + float: right; - .fa-heart { - color: #FF2E2B; + + .llms-video-wrapper { + margin-top: 10px; } } } + +.llms-favorite-wrapper { + cursor: pointer; + + .fa-heart { + color: #FF2E2B; + } +} diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index f85b10d3c2..618ba013ea 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -385,6 +385,21 @@ function lifterlms_template_single_parent_course() { } } +/** + * Favorite Lesson Template Include. + * + * @since [version] + * + * @return void + */ +if ( ! function_exists( 'lifterlms_template_single_favorite' ) ) { + + function lifterlms_template_single_favorite() { + + llms_get_template( 'course/favorite.php' ); + } +} + /** * Complete Lesson Link Template Include * diff --git a/includes/llms.template.hooks.php b/includes/llms.template.hooks.php index 10dc69c517..2203ccd750 100644 --- a/includes/llms.template.hooks.php +++ b/includes/llms.template.hooks.php @@ -48,6 +48,7 @@ * @since Unknown */ add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_parent_course', 10 ); +add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_favorite', 10 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_video', 20 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_audio', 20 ); diff --git a/templates/course/parent-course.php b/templates/course/parent-course.php index be3007d94c..83e8d32f24 100644 --- a/templates/course/parent-course.php +++ b/templates/course/parent-course.php @@ -14,14 +14,5 @@ global $post; $lesson = new LLMS_Lesson( $post ); -?> -
- - Back to: %2$s

', 'lifterlms' ), get_permalink( $lesson->get( 'parent_course' ) ), get_the_title( $lesson->get( 'parent_course' ) ) ); - - llms_get_template( 'course/favorite.php' ); - ?> - -
+printf( __( '', 'lifterlms' ), get_permalink( $lesson->get( 'parent_course' ) ), get_the_title( $lesson->get( 'parent_course' ) ) ); From 3aa835aac0b6b831eb1b53d74524836d3770950d Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Sat, 18 Mar 2023 00:48:34 +0530 Subject: [PATCH 015/125] Fixed CSS issue with favorite button when used with another element. Now a parent class is available to modify CSS. --- assets/js/llms-favorites.js | 3 +++ assets/scss/frontend/_main.scss | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/assets/js/llms-favorites.js b/assets/js/llms-favorites.js index bc29a013aa..7aa6c9c4b9 100644 --- a/assets/js/llms-favorites.js +++ b/assets/js/llms-favorites.js @@ -36,6 +36,9 @@ self.favorite( $( this ) ); } ); + // Adding class in Favorite's parent + $( '.llms-favorite-wrapper' ).parent().addClass( 'llms-has-favorite' ); + }, /** diff --git a/assets/scss/frontend/_main.scss b/assets/scss/frontend/_main.scss index cff5dd21f1..d1623f280f 100644 --- a/assets/scss/frontend/_main.scss +++ b/assets/scss/frontend/_main.scss @@ -475,7 +475,7 @@ svg .llms-animated-circle { } // Favorites. -.llms-parent-course-link { +.llms-has-favorite .llms-parent-course-link { display: inline; + .llms-favorite-wrapper { From adbf385158e12ec6548582dc96ff162d67173efd Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 20 Mar 2023 14:34:40 +0530 Subject: [PATCH 016/125] Added filter to enable/disable the feature --- class-lifterlms.php | 12 ++++++++++++ includes/class.llms.frontend.assets.php | 2 +- includes/llms.template.functions.php | 4 +++- templates/content-single-lesson-before.php | 1 + 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/class-lifterlms.php b/class-lifterlms.php index 79db0f5d0d..18e6555e3c 100644 --- a/class-lifterlms.php +++ b/class-lifterlms.php @@ -24,6 +24,7 @@ * Remove deprecated class files and variables. * Move includes (file loading) into the LLMS_Loader class. * @since 5.3.0 Replace singleton code with `LLMS_Trait_Singleton`. + * @since [version] Include the Favorite filter to enable the feature. */ final class LifterLMS { @@ -418,4 +419,15 @@ public function localize() { } + /** + * Filter Hook to enable the Favorite feature. + * + * @since [version] + * + * @return boolean + */ + public function is_favorites_enabled() { + return apply_filters( 'llms_favorites_enabled', true ); + } + } diff --git a/includes/class.llms.frontend.assets.php b/includes/class.llms.frontend.assets.php index be9f2761e3..f3b66e9695 100644 --- a/includes/class.llms.frontend.assets.php +++ b/includes/class.llms.frontend.assets.php @@ -201,7 +201,7 @@ public static function enqueue_scripts() { llms()->assets->enqueue_script( 'llms-quiz' ); } - if ( is_lesson() ) { // TODO: check if favorites is enabled and enqueue script on right places. + if ( is_lesson() && true === llms()->is_favorites_enabled() ) { llms()->assets->enqueue_script( 'llms-favorites' ); } diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index 618ba013ea..59fa647e64 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -396,7 +396,9 @@ function lifterlms_template_single_parent_course() { function lifterlms_template_single_favorite() { - llms_get_template( 'course/favorite.php' ); + if ( llms()->is_favorites_enabled() ) { + llms_get_template( 'course/favorite.php' ); + } } } diff --git a/templates/content-single-lesson-before.php b/templates/content-single-lesson-before.php index ef0eeebf83..393793401c 100644 --- a/templates/content-single-lesson-before.php +++ b/templates/content-single-lesson-before.php @@ -12,6 +12,7 @@ /** * @hooked - lifterlms_template_single_parent_course - 10 + * @hooked - lifterlms_template_single_favorite - 10 * @hooked - lifterlms_template_single_lesson_video - 20 * @hooked - lifterlms_template_single_lesson_audio - 20 */ From c23ad1153473835110e44ebc67ec008c67b10e42 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Tue, 21 Mar 2023 02:34:20 +0530 Subject: [PATCH 017/125] Added favorites shortcode [wip] --- .../class.llms.shortcode.favorites.php | 109 ++++++++++++++++++ includes/shortcodes/class.llms.shortcodes.php | 2 +- 2 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 includes/shortcodes/class.llms.shortcode.favorites.php diff --git a/includes/shortcodes/class.llms.shortcode.favorites.php b/includes/shortcodes/class.llms.shortcode.favorites.php new file mode 100644 index 0000000000..21ef3a03a1 --- /dev/null +++ b/includes/shortcodes/class.llms.shortcode.favorites.php @@ -0,0 +1,109 @@ +get_results( + $wpdb->prepare( + "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta + WHERE meta_key = %s AND user_id = %d ORDER BY updated_date DESC", + '_favorite', + get_current_user_id() + ) + ); + + return empty( $res ) ? false : $res; + + } + + /** + * Retrieve the actual content of the shortcode. + * + * $atts & $content are both filtered before being passed to get_output() + * output is filtered so the return of get_output() doesn't need its own filter. + * + * @since [version] + * + * @return string + */ + protected function get_output() { + + $this->enqueue_script( 'llms-jquery-matchheight' ); + + ob_start(); + + // If we're outputting a "My Favorites" list and we don't have a student output login info. + if ( ! llms_get_student() ) { + + printf( + __( 'You must be logged in to view this information. Click %1$shere%2$s to login.', 'lifterlms' ), + '', + '' + ); + + } else { + + $favorites = $this->get_favorites(); + + if ( $favorites ) { + + foreach ( $favorites as $favorite ) { + + $lesson = new LLMS_Lesson( $favorite->post_id ); + + llms_get_template( + 'course/lesson-preview.php', + array( + 'lesson' => $lesson, + ) + ); + + } + } else { + + printf( '

%s

', __( 'No favorites found.', 'lifterlms' ) ); + + } + } + + return ob_get_clean(); + + } + +} + +return LLMS_Shortcode_Favorites::instance(); diff --git a/includes/shortcodes/class.llms.shortcodes.php b/includes/shortcodes/class.llms.shortcodes.php index c2387429cf..8bf2068b7b 100644 --- a/includes/shortcodes/class.llms.shortcodes.php +++ b/includes/shortcodes/class.llms.shortcodes.php @@ -68,6 +68,7 @@ public static function init() { 'LLMS_Shortcode_My_Achievements', 'LLMS_Shortcode_Registration', 'LLMS_Shortcode_User_Info', + 'LLMS_Shortcode_Favorites', ) ); @@ -79,7 +80,6 @@ public static function init() { $separator = in_array( $class, $hyphenated_file_classes, true ) ? '-' : '.'; $filename = "class{$separator}" . strtolower( str_replace( '_', $separator, $class ) ); - /** * Filters the path of the shortcode class file. * From 7dbee61573c89b7845e9d3285f07eee8b1f1d763 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Tue, 21 Mar 2023 16:08:40 +0530 Subject: [PATCH 018/125] Added shortcode attributes --- .../class.llms.shortcode.favorites.php | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/includes/shortcodes/class.llms.shortcode.favorites.php b/includes/shortcodes/class.llms.shortcode.favorites.php index 21ef3a03a1..83b4731c98 100644 --- a/includes/shortcodes/class.llms.shortcode.favorites.php +++ b/includes/shortcodes/class.llms.shortcode.favorites.php @@ -26,6 +26,25 @@ class LLMS_Shortcode_Favorites extends LLMS_Shortcode { */ public $tag = 'lifterlms_favorites'; + /** + * Get shortcode attributes + * + * Retrieves an array of default attributes which are automatically merged + * with the user submitted attributes and passed to $this->get_output(). + * + * @since [version] + * + * @return array + */ + protected function get_default_attributes() { + + return array( + 'orderby' => 'updated_date', + 'order' => 'ASC', + 'limit' => '', + ); + } + /** * Retrieve an array of Favorites from `lifterlms_user_postmeta`. * @@ -37,10 +56,14 @@ protected function get_favorites() { global $wpdb; + $order_by = ( '' === $this->get_attribute( 'orderby' ) ) ? '' : 'ORDER BY ' . $this->get_attribute( 'orderby' ); + $order = ( '' === $this->get_attribute( 'order' ) ) ? '' : $this->get_attribute( 'order' ); + $limit = ( '' === $this->get_attribute( 'limit' ) ) ? '' : 'LIMIT ' . $this->get_attribute( 'limit' ); + $res = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta - WHERE meta_key = %s AND user_id = %d ORDER BY updated_date DESC", + WHERE meta_key = %s AND user_id = %d $order_by $order $limit", '_favorite', get_current_user_id() ) From e2791e4ca1cdbe27ee0fc143b75b1bdb25eb86be Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Tue, 21 Mar 2023 16:45:11 +0530 Subject: [PATCH 019/125] Hiding favorites when user not logged in --- templates/course/favorite.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/templates/course/favorite.php b/templates/course/favorite.php index ab81e46f16..88169281cf 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -15,6 +15,10 @@ global $post; +if ( ! is_user_logged_in() ) { + return; +} + $lesson = new LLMS_Lesson( $post->ID ); $student = llms_get_student( get_current_user_id() ); $total_favorites = get_total_favorites( $lesson->get( 'id' ) ); @@ -23,7 +27,7 @@
- + is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> From b28a3a4c2daa9a3ea23b6ea6ab10d4e5284f7e42 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 22 Mar 2023 18:22:05 +0530 Subject: [PATCH 020/125] Added favorite event for checking existing favs *fixes bug in favorites when admin opens any completed lesson, the lesson is already favorited --- includes/class.llms.query.user.postmeta.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/class.llms.query.user.postmeta.php b/includes/class.llms.query.user.postmeta.php index aa7e3cc83a..951a54a498 100644 --- a/includes/class.llms.query.user.postmeta.php +++ b/includes/class.llms.query.user.postmeta.php @@ -145,6 +145,10 @@ protected function parse_args() { 'key' => '_enrollment_trigger', 'value' => 'order_%', ), + 'favorites' => array( + 'key' => '_favorite', + 'compare' => 'IS NOT NULL', + ), ); if ( is_string( $this->arguments['types'] ) && 'all' === $this->arguments['types'] ) { From b274bef33c0f76a4b01ca0b4e0c25e34fa245980 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 29 Mar 2023 14:13:33 +0530 Subject: [PATCH 021/125] Added favorites helper in student class --- includes/models/model.llms.student.php | 30 +++++++++++++++++++ .../class.llms.shortcode.favorites.php | 21 ++++--------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 82d0d9c158..12064b0c93 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -265,6 +265,36 @@ public function get_courses( $args = array() ) { } + /** + * Retrieve user's favorites based on supplied criteria. + * + * @since [version] + * + * @param string $order_by Result set ordering field. Default "date". + * @param string $order Result set order. Default "DESC". Accepts "DESC" or "ASC". + * @param integer $limit Number of favorites to return. + * @return array + */ + public function get_favorites( $order_by = '', $order = '', $limit = '' ) { + + global $wpdb; + + $order_by = ( '' === $order_by ) ? '' : 'ORDER BY ' . $order_by; + $limit = ( '' === $limit ) ? '' : 'LIMIT ' . $limit; + + $res = $wpdb->get_results( + $wpdb->prepare( + "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta + WHERE meta_key = %s AND user_id = %d $order_by $order $limit", + '_favorite', + get_current_user_id() + ) + ); + + return empty( $res ) ? false : $res; + + } + /** * Retrieve IDs of courses a user has completed * diff --git a/includes/shortcodes/class.llms.shortcode.favorites.php b/includes/shortcodes/class.llms.shortcode.favorites.php index 83b4731c98..9f590238cb 100644 --- a/includes/shortcodes/class.llms.shortcode.favorites.php +++ b/includes/shortcodes/class.llms.shortcode.favorites.php @@ -54,22 +54,13 @@ protected function get_default_attributes() { */ protected function get_favorites() { - global $wpdb; - - $order_by = ( '' === $this->get_attribute( 'orderby' ) ) ? '' : 'ORDER BY ' . $this->get_attribute( 'orderby' ); - $order = ( '' === $this->get_attribute( 'order' ) ) ? '' : $this->get_attribute( 'order' ); - $limit = ( '' === $this->get_attribute( 'limit' ) ) ? '' : 'LIMIT ' . $this->get_attribute( 'limit' ); - - $res = $wpdb->get_results( - $wpdb->prepare( - "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta - WHERE meta_key = %s AND user_id = %d $order_by $order $limit", - '_favorite', - get_current_user_id() - ) - ); + $student = llms_get_student(); + + $order_by = $this->get_attribute( 'orderby' ); + $order = $this->get_attribute( 'order' ); + $limit = $this->get_attribute( 'limit' ); - return empty( $res ) ? false : $res; + return $student->get_favorites( $order_by, $order, $limit ); } From d383ce94f81e22c5d3bbfcb49337199717a5b77f Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 29 Mar 2023 16:57:44 +0530 Subject: [PATCH 022/125] Added Favorites subpage/endpoint in student dashboard --- .../settings/class.llms.settings.accounts.php | 9 ++ includes/class.llms.student.dashboard.php | 8 ++ .../llms.functions.templates.dashboard.php | 84 ++++++++++++++++++- includes/llms.template.hooks.php | 1 + templates/myaccount/dashboard.php | 3 +- 5 files changed, 103 insertions(+), 2 deletions(-) diff --git a/includes/admin/settings/class.llms.settings.accounts.php b/includes/admin/settings/class.llms.settings.accounts.php index 86a6d629f8..02f504e53d 100644 --- a/includes/admin/settings/class.llms.settings.accounts.php +++ b/includes/admin/settings/class.llms.settings.accounts.php @@ -46,6 +46,7 @@ class LLMS_Settings_Accounts extends LLMS_Settings_Page { * Reorganized open registration setting. * Renamed "User Information Options" to "User Privacy Options". * @since 5.6.0 Added options to disable concurrent logins. + * @since [version] Added settings to view favorites. * * @return array */ @@ -180,6 +181,14 @@ public function get_settings() { 'default' => 'my-certificates', 'sanitize' => 'slug', ), + array( + 'title' => __( 'View Favorites', 'lifterlms' ), + 'desc' => __( 'List of all the student\'s favorites', 'lifterlms' ), + 'id' => 'lifterlms_myaccount_favorites_endpoint', + 'type' => 'text', + 'default' => 'my-favorites', + 'sanitize' => 'slug', + ), array( 'title' => __( 'Notifications', 'lifterlms' ), 'desc' => __( 'View Notifications and adjust notification settings', 'lifterlms' ), diff --git a/includes/class.llms.student.dashboard.php b/includes/class.llms.student.dashboard.php index 99313c1198..ab6b1555cc 100644 --- a/includes/class.llms.student.dashboard.php +++ b/includes/class.llms.student.dashboard.php @@ -140,6 +140,7 @@ public static function get_current_tab( $return = 'data' ) { * @since 3.0.0 * @since 3.28.2 Unknown. * @since 6.0.0 Add pagination to the view-achievements and view-certificates tabs. + * @since [version] Add view-favorites tab. * * @return array */ @@ -189,6 +190,13 @@ public static function get_tabs() { 'nav_item' => true, 'title' => __( 'My Certificates', 'lifterlms' ), ), + 'view-favorites' => array( + 'content' => 'lifterlms_template_student_dashboard_my_favorites', + 'endpoint' => get_option( 'lifterlms_myaccount_favorites_endpoint', 'view-favorites' ), + 'paginate' => true, + 'nav_item' => true, + 'title' => __( 'My Favorites', 'lifterlms' ), + ), 'notifications' => array( 'content' => 'lifterlms_template_student_dashboard_my_notifications', 'endpoint' => get_option( 'lifterlms_myaccount_notifications_endpoint', 'notifications' ), diff --git a/includes/functions/llms.functions.templates.dashboard.php b/includes/functions/llms.functions.templates.dashboard.php index ed491bbab5..2e30d549b6 100644 --- a/includes/functions/llms.functions.templates.dashboard.php +++ b/includes/functions/llms.functions.templates.dashboard.php @@ -5,7 +5,7 @@ * @package LifterLMS/Functions * * @since 3.0.0 - * @version 6.3.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -275,6 +275,47 @@ function lifterlms_template_my_courses_loop( $student = null, $preview = false ) } } +if ( ! function_exists( 'lifterlms_template_my_favorites_loop' ) ) { + + /** + * Get student's favorites. + * + * @since [version] + * + * @param LLMS_Student $student Optional. LLMS_Student (current student if none supplied). Default `null`. + * @return void + */ + function lifterlms_template_my_favorites_loop( $student = null ) { + + $student = llms_get_student( $student ); + if ( ! $student ) { + return; + } + + $favorites = $student->get_favorites(); + + if ( ! $favorites ) { + + printf( '

%s

', __( 'No favorites found.', 'lifterlms' ) ); + + } else { + + foreach ( $favorites as $favorite ) { + + $lesson = new LLMS_Lesson( $favorite->post_id ); + + llms_get_template( + 'course/lesson-preview.php', + array( + 'lesson' => $lesson, + ) + ); + + } + } + + } +} if ( ! function_exists( 'lifterlms_template_my_memberships_loop' ) ) { @@ -503,6 +544,47 @@ function lifterlms_template_student_dashboard_my_courses( $preview = false ) { } } +if ( ! function_exists( 'lifterlms_template_student_dashboard_my_favorites' ) ) { + + /** + * Template for My Favorites section on dashboard index. + * + * @since [version] + * + * @param bool $preview Optional. If true, outputs a short list of favorites. Default `false`. + * @return void + */ + function lifterlms_template_student_dashboard_my_favorites( $preview = false ) { + + $student = llms_get_student(); + if ( ! $student ) { + return; + } + + $more = false; + if ( $preview && LLMS_Student_Dashboard::is_endpoint_enabled( 'view-favorites' ) ) { + $more = array( + 'url' => llms_get_endpoint_url( 'view-favorites', '', llms_get_page_url( 'myaccount' ) ), + 'text' => __( 'View All My Favorites', 'lifterlms' ), + ); + } + + ob_start(); + lifterlms_template_my_favorites_loop( $student, $preview ); + + llms_get_template( + 'myaccount/dashboard-section.php', + array( + 'action' => 'my_favorites', + 'slug' => 'llms-my-favorites', + 'title' => $preview ? __( 'My Favorites', 'lifterlms' ) : '', + 'content' => ob_get_clean(), + 'more' => $more, + ) + ); + + } +} if ( ! function_exists( 'lifterlms_template_student_dashboard_my_grades' ) ) { diff --git a/includes/llms.template.hooks.php b/includes/llms.template.hooks.php index 2203ccd750..112a29d8dc 100644 --- a/includes/llms.template.hooks.php +++ b/includes/llms.template.hooks.php @@ -169,6 +169,7 @@ add_action( 'llms_achievement_content', 'llms_the_achievement', 10 ); add_action( 'llms_certificate_preview', 'llms_the_certificate_preview', 10 ); add_action( 'lifterlms_student_dashboard_index', 'lifterlms_template_student_dashboard_my_memberships', 40 ); +add_action( 'lifterlms_student_dashboard_index', 'lifterlms_template_student_dashboard_my_favorites', 50 ); add_action( 'llms_my_grades_course_table', 'lifterlms_template_student_dashboard_my_grades_table', 10, 2 ); diff --git a/templates/myaccount/dashboard.php b/templates/myaccount/dashboard.php index ee06da4894..a889e06fe0 100644 --- a/templates/myaccount/dashboard.php +++ b/templates/myaccount/dashboard.php @@ -5,7 +5,7 @@ * @package LifterLMS/Templates * * @since 1.0.0 - * @version 3.14.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -26,6 +26,7 @@ * @hooked lifterlms_template_student_dashboard_my_achievements - 20 * @hooked lifterlms_template_student_dashboard_my_certificates - 30 * @hooked lifterlms_template_student_dashboard_my_memberships - 40 + * @hooked lifterlms_template_student_dashboard_my_favorites - 50 */ do_action( 'lifterlms_student_dashboard_index', true ); From 88cd9cbbedcd7b8fab4bbb4a2c944ad631d14aac Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 29 Mar 2023 17:22:51 +0530 Subject: [PATCH 023/125] Disabling favorites endpoint if favorites disabled --- includes/class.llms.student.dashboard.php | 2 +- includes/functions/llms.functions.templates.dashboard.php | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/class.llms.student.dashboard.php b/includes/class.llms.student.dashboard.php index ab6b1555cc..a71808a25a 100644 --- a/includes/class.llms.student.dashboard.php +++ b/includes/class.llms.student.dashboard.php @@ -192,7 +192,7 @@ public static function get_tabs() { ), 'view-favorites' => array( 'content' => 'lifterlms_template_student_dashboard_my_favorites', - 'endpoint' => get_option( 'lifterlms_myaccount_favorites_endpoint', 'view-favorites' ), + 'endpoint' => llms()->is_favorites_enabled() ? get_option( 'lifterlms_myaccount_favorites_endpoint', 'view-favorites' ) : '', 'paginate' => true, 'nav_item' => true, 'title' => __( 'My Favorites', 'lifterlms' ), diff --git a/includes/functions/llms.functions.templates.dashboard.php b/includes/functions/llms.functions.templates.dashboard.php index 2e30d549b6..f3928b5f24 100644 --- a/includes/functions/llms.functions.templates.dashboard.php +++ b/includes/functions/llms.functions.templates.dashboard.php @@ -557,7 +557,8 @@ function lifterlms_template_student_dashboard_my_courses( $preview = false ) { function lifterlms_template_student_dashboard_my_favorites( $preview = false ) { $student = llms_get_student(); - if ( ! $student ) { + + if ( ! $student || ! llms()->is_favorites_enabled() ) { return; } From 37ee724d5092bd75f2d6461485aebc0a0846cc2a Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 29 Mar 2023 17:39:41 +0530 Subject: [PATCH 024/125] Disabling admin favorite settings via filter. --- includes/admin/settings/class.llms.settings.accounts.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/includes/admin/settings/class.llms.settings.accounts.php b/includes/admin/settings/class.llms.settings.accounts.php index 02f504e53d..c50332a7e1 100644 --- a/includes/admin/settings/class.llms.settings.accounts.php +++ b/includes/admin/settings/class.llms.settings.accounts.php @@ -46,7 +46,7 @@ class LLMS_Settings_Accounts extends LLMS_Settings_Page { * Reorganized open registration setting. * Renamed "User Information Options" to "User Privacy Options". * @since 5.6.0 Added options to disable concurrent logins. - * @since [version] Added settings to view favorites. + * @since [version] Added settings for favorites endpoint. * * @return array */ @@ -188,6 +188,7 @@ public function get_settings() { 'type' => 'text', 'default' => 'my-favorites', 'sanitize' => 'slug', + 'disabled' => ! llms()->is_favorites_enabled() ? true : false, ), array( 'title' => __( 'Notifications', 'lifterlms' ), From 389945cfde929c1c921729f783a7ff5f04ad44f8 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 30 Mar 2023 16:46:51 +0530 Subject: [PATCH 025/125] Added favorites in lesson view --- assets/scss/frontend/_main.scss | 4 ++++ includes/class.llms.frontend.assets.php | 4 ++-- includes/functions/llms.functions.person.php | 16 ++++++++-------- includes/llms.template.functions.php | 13 +++++++++++-- templates/course/favorite.php | 8 ++++---- templates/course/lesson-preview.php | 4 +++- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/assets/scss/frontend/_main.scss b/assets/scss/frontend/_main.scss index d1623f280f..804b905812 100644 --- a/assets/scss/frontend/_main.scss +++ b/assets/scss/frontend/_main.scss @@ -495,3 +495,7 @@ svg .llms-animated-circle { color: #FF2E2B; } } + +.llms-syllabus-wrapper .llms-favorite-wrapper { + text-align: left; +} diff --git a/includes/class.llms.frontend.assets.php b/includes/class.llms.frontend.assets.php index f3b66e9695..37939eb2f6 100644 --- a/includes/class.llms.frontend.assets.php +++ b/includes/class.llms.frontend.assets.php @@ -166,7 +166,7 @@ public static function enqueue_styles() { * Moved inline scripts to `enqueue_inline_scripts()`. * @since 5.0.0 Enqueue locale data and dependencies on account and checkout pages for searchable dropdowns for country & state. * Remove password strength inline enqueue. - * @since [version] Enqueue `llms-favorites` script. + * @since [version] Enqueue `llms-favorites` script on lesson and course page. * * @return void */ @@ -201,7 +201,7 @@ public static function enqueue_scripts() { llms()->assets->enqueue_script( 'llms-quiz' ); } - if ( is_lesson() && true === llms()->is_favorites_enabled() ) { + if ( ( is_lesson() || is_course() ) && true === llms()->is_favorites_enabled() ) { llms()->assets->enqueue_script( 'llms-favorites' ); } diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index b06313e17b..664cb949f5 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -325,7 +325,7 @@ function llms_is_user_enrolled( $user_id, $product_id, $relation = 'all', $use_c * * @param int $user_id WP User ID. * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param int $object_type Object type [lesson|section|course|track]. + * @param string $object_type Object type [lesson|section|course|track]. * @param string $trigger String describing the event that triggered marking the object as complete. * @return boolean */ @@ -343,7 +343,7 @@ function llms_mark_complete( $user_id, $object_id, $object_type, $trigger = 'uns * * @param int $user_id WP User ID. * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param int $object_type Object type [lesson|section|course|track]. + * @param string $object_type Object type [lesson|section|course|track]. * @param string $trigger String describing the event that triggered marking the object as incomplete. * @return boolean */ @@ -359,9 +359,9 @@ function llms_mark_incomplete( $user_id, $object_id, $object_type, $trigger = 'u * * @see LLMS_Student->mark_favorite() * - * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param int $object_type Object type [lesson|section|course|track]. + * @param int $user_id WP User ID. + * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @param string $object_type Object type [lesson|section|course|track]. * @return boolean */ function llms_mark_favorite( $user_id, $object_id, $object_type ) { @@ -376,9 +376,9 @@ function llms_mark_favorite( $user_id, $object_id, $object_type ) { * * @see LLMS_Student->mark_unfavorite() * - * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param int $object_type Object type [lesson|section|course|track]. + * @param int $user_id WP User ID. + * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @param string $object_type Object type [lesson|section|course|track]. * @return boolean */ function llms_mark_unfavorite( $user_id, $object_id, $object_type ) { diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index 59fa647e64..19e1a0d156 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -390,14 +390,23 @@ function lifterlms_template_single_parent_course() { * * @since [version] * + * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @param string $object_type Object type [lesson|section|course|track]. * @return void */ if ( ! function_exists( 'lifterlms_template_single_favorite' ) ) { - function lifterlms_template_single_favorite() { + function lifterlms_template_single_favorite( $object_id = null, $object_type = 'lesson' ) { if ( llms()->is_favorites_enabled() ) { - llms_get_template( 'course/favorite.php' ); + + llms_get_template( + 'course/favorite.php', + array( + 'object_id' => $object_id, + 'object_type' => $object_type, + ) + ); } } } diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 88169281cf..5899c2c01b 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -8,8 +8,8 @@ * * @since [version] * - * @var LLMS_Lesson $lesson The lesson object. - * @var LLMS_Student $student A LLMS_Student object. + * @var int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @var string $object_type Object type [lesson|section|course|track]. */ defined( 'ABSPATH' ) || exit; @@ -19,7 +19,7 @@ return; } -$lesson = new LLMS_Lesson( $post->ID ); +$lesson = new LLMS_Lesson( ( null === $object_id || '' === $object_id ) ? $post->ID : $object_id ); $student = llms_get_student( get_current_user_id() ); $total_favorites = get_total_favorites( $lesson->get( 'id' ) ); ?> @@ -34,7 +34,7 @@ - + diff --git a/templates/course/lesson-preview.php b/templates/course/lesson-preview.php index aa1bd906a7..698396e6b5 100644 --- a/templates/course/lesson-preview.php +++ b/templates/course/lesson-preview.php @@ -9,7 +9,8 @@ * @since 3.19.2 Unknown. * @since 4.4.0 Use the passed `$order` param if available, in favor of retrieving the lesson's order post meta. * @since 5.7.0 Replaced the call to the deprecated `LLMS_Lesson::get_order()` method with `LLMS_Lesson::get( 'order' )`. - * @version 5.7.0 + * @since [version] Added Favorites function `lifterlms_template_single_favorite()`. + * @version [version] * * @var LLMS_Lesson $lesson The lesson object. * @var string $pre_text The text to display before the lesson. @@ -46,6 +47,7 @@
get( 'id' ) ); ?>
+ get( 'id' ) ); ?> get( 'id' ) ) ) : ?>
get( 'id' ) ); ?>
From 70b599530218cafdc39c13aadec6f4d9ea215986 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 30 Mar 2023 16:57:40 +0530 Subject: [PATCH 026/125] Added dummy favorite for logged out user --- templates/course/favorite.php | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 5899c2c01b..4db37d5e51 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -15,10 +15,6 @@ global $post; -if ( ! is_user_logged_in() ) { - return; -} - $lesson = new LLMS_Lesson( ( null === $object_id || '' === $object_id ) ? $post->ID : $object_id ); $student = llms_get_student( get_current_user_id() ); $total_favorites = get_total_favorites( $lesson->get( 'id' ) ); @@ -28,17 +24,25 @@ - is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> + + + + + + + is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> + + + - - + - + + - - + - + From afd5b4e431e25b257cf216e62b9bfdc2b3a94e20 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 12 Apr 2023 14:51:08 +0530 Subject: [PATCH 027/125] Action validation for LifterLMS Labs --- includes/llms.template.functions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index 19e1a0d156..2dc0f31e53 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -398,7 +398,7 @@ function lifterlms_template_single_parent_course() { function lifterlms_template_single_favorite( $object_id = null, $object_type = 'lesson' ) { - if ( llms()->is_favorites_enabled() ) { + if ( llms()->is_favorites_enabled() && has_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_favorite' ) ) { llms_get_template( 'course/favorite.php', From 7784735ea2e03dbdd3419f636109da5e12816cea Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 20 Apr 2023 17:09:36 +0530 Subject: [PATCH 028/125] Minor fixes --- includes/llms.template.functions.php | 6 +++--- includes/llms.template.hooks.php | 2 +- templates/content-single-lesson-before.php | 2 +- templates/course/lesson-preview.php | 6 ++++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index 2dc0f31e53..1760c4ca08 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -394,11 +394,11 @@ function lifterlms_template_single_parent_course() { * @param string $object_type Object type [lesson|section|course|track]. * @return void */ -if ( ! function_exists( 'lifterlms_template_single_favorite' ) ) { +if ( ! function_exists( 'lifterlms_template_favorite' ) ) { - function lifterlms_template_single_favorite( $object_id = null, $object_type = 'lesson' ) { + function lifterlms_template_favorite( $object_id = null, $object_type = 'lesson' ) { - if ( llms()->is_favorites_enabled() && has_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_favorite' ) ) { + if ( llms()->is_favorites_enabled() ) { llms_get_template( 'course/favorite.php', diff --git a/includes/llms.template.hooks.php b/includes/llms.template.hooks.php index 112a29d8dc..eb8b01193c 100644 --- a/includes/llms.template.hooks.php +++ b/includes/llms.template.hooks.php @@ -48,7 +48,7 @@ * @since Unknown */ add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_parent_course', 10 ); -add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_favorite', 10 ); +add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_favorite', 10 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_video', 20 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_audio', 20 ); diff --git a/templates/content-single-lesson-before.php b/templates/content-single-lesson-before.php index 393793401c..9610309e7a 100644 --- a/templates/content-single-lesson-before.php +++ b/templates/content-single-lesson-before.php @@ -12,7 +12,7 @@ /** * @hooked - lifterlms_template_single_parent_course - 10 - * @hooked - lifterlms_template_single_favorite - 10 + * @hooked - lifterlms_template_favorite - 10 * @hooked - lifterlms_template_single_lesson_video - 20 * @hooked - lifterlms_template_single_lesson_audio - 20 */ diff --git a/templates/course/lesson-preview.php b/templates/course/lesson-preview.php index 698396e6b5..9b0bab6d24 100644 --- a/templates/course/lesson-preview.php +++ b/templates/course/lesson-preview.php @@ -9,7 +9,7 @@ * @since 3.19.2 Unknown. * @since 4.4.0 Use the passed `$order` param if available, in favor of retrieving the lesson's order post meta. * @since 5.7.0 Replaced the call to the deprecated `LLMS_Lesson::get_order()` method with `LLMS_Lesson::get( 'order' )`. - * @since [version] Added Favorites function `lifterlms_template_single_favorite()`. + * @since [version] Added Favorites function `lifterlms_template_favorite()`. * @version [version] * * @var LLMS_Lesson $lesson The lesson object. @@ -47,7 +47,9 @@
get( 'id' ) ); ?>
- get( 'id' ) ); ?> + + get( 'id' ) ); ?> + get( 'id' ) ) ) : ?>
get( 'id' ) ); ?>
From 1ed588582722634839fba186838e85a52141ad66 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 20 Apr 2023 18:29:02 +0530 Subject: [PATCH 029/125] Added support for LifterLabs switch --- templates/course/lesson-preview.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/course/lesson-preview.php b/templates/course/lesson-preview.php index 9b0bab6d24..ef3fe8dd4b 100644 --- a/templates/course/lesson-preview.php +++ b/templates/course/lesson-preview.php @@ -47,7 +47,7 @@
get( 'id' ) ); ?>
- + get( 'id' ) ); ?> get( 'id' ) ) ) : ?> From a6f76f81fa0a26c28b2f5eb843bb3b7ca9abf95b Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Tue, 25 Apr 2023 18:43:09 +0530 Subject: [PATCH 030/125] CS Fixes --- includes/class.llms.query.user.postmeta.php | 4 ++-- includes/models/model.llms.student.php | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/includes/class.llms.query.user.postmeta.php b/includes/class.llms.query.user.postmeta.php index 951a54a498..2ec0baa229 100644 --- a/includes/class.llms.query.user.postmeta.php +++ b/includes/class.llms.query.user.postmeta.php @@ -145,8 +145,8 @@ protected function parse_args() { 'key' => '_enrollment_trigger', 'value' => 'order_%', ), - 'favorites' => array( - 'key' => '_favorite', + 'favorites' => array( + 'key' => '_favorite', 'compare' => 'IS NOT NULL', ), ); diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 12064b0c93..7eeee5c50f 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -282,6 +282,7 @@ public function get_favorites( $order_by = '', $order = '', $limit = '' ) { $order_by = ( '' === $order_by ) ? '' : 'ORDER BY ' . $order_by; $limit = ( '' === $limit ) ? '' : 'LIMIT ' . $limit; + // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $res = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}lifterlms_user_postmeta From 09760e6ab8e85add43cdf0f2a395f4d32dc31ab5 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 26 Apr 2023 16:36:51 +0530 Subject: [PATCH 031/125] Fixed Unit Tests for favorites --- tests/phpunit/unit-tests/class-llms-test-query.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/unit-tests/class-llms-test-query.php b/tests/phpunit/unit-tests/class-llms-test-query.php index 1a01e2572d..09463d96e1 100644 --- a/tests/phpunit/unit-tests/class-llms-test-query.php +++ b/tests/phpunit/unit-tests/class-llms-test-query.php @@ -81,6 +81,7 @@ public function test_add_endpoints() { 'view-memberships' => 'lifterlms_myaccount_memberships_endpoint', 'view-achievements' => 'lifterlms_myaccount_achievements_endpoint', 'view-certificates' => 'lifterlms_myaccount_certificates_endpoint', + 'view-favorites' => 'lifterlms_myaccount_favorites_endpoint', 'notifications' => 'lifterlms_myaccount_notifications_endpoint', 'edit-account' => 'lifterlms_myaccount_edit_account_endpoint', 'redeem-voucher' => 'lifterlms_myaccount_redeem_vouchers_endpoint', @@ -93,6 +94,7 @@ public function test_add_endpoints() { 'view-memberships' => 'ਵੇਖੋ-ਸਦੱਸਤਾ', // Punjabi. 'view-achievements' => 'nailiyyətlər', // Azerbaijani. 'view-certificates' => 'ເບິ່ງໃບຢັ້ງຢືນ', // Lao. + 'view-favorites' => '즐겨찾기', // Korean. 'notifications' => '通知', // Chinese (Simplified). 'edit-account' => 'חשבון-עריכה', // Hebrew. 'redeem-voucher' => 'چھڑانا', // Urdu. From 29984c6bd1b8ffa83ab5bd36fbbc240cc954cab3 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 26 Apr 2023 23:16:33 +0530 Subject: [PATCH 032/125] unit test fix --- .../admin/settings/class-llms-test-settings-accounts.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/phpunit/unit-tests/admin/settings/class-llms-test-settings-accounts.php b/tests/phpunit/unit-tests/admin/settings/class-llms-test-settings-accounts.php index e4131568d3..dd31b8af71 100644 --- a/tests/phpunit/unit-tests/admin/settings/class-llms-test-settings-accounts.php +++ b/tests/phpunit/unit-tests/admin/settings/class-llms-test-settings-accounts.php @@ -87,6 +87,10 @@ protected function get_mock_settings() { 'my-certificates', 'custom-endpoint-certificates', ), + 'lifterlms_myaccount_favorites_endpoint' => array( + 'my-favorites', + 'custom-endpoint-favorites', + ), 'lifterlms_myaccount_notifications_endpoint' => array( 'notifications', 'custom-endpoint-notifications', From 381c51feb76870787d0d39e55288a302bd9e5f88 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 18 May 2023 17:15:33 +0530 Subject: [PATCH 033/125] Showing number of lessons in Course Catalog and My Courses. --- .../llms.functions.templates.loop.php | 18 ++++++++++- includes/llms.template.hooks.php | 3 +- templates/course/length.php | 1 - templates/course/lesson-count.php | 31 +++++++++++++++++++ 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 templates/course/lesson-count.php diff --git a/includes/functions/llms.functions.templates.loop.php b/includes/functions/llms.functions.templates.loop.php index 4710aa0ce0..22f4520795 100644 --- a/includes/functions/llms.functions.templates.loop.php +++ b/includes/functions/llms.functions.templates.loop.php @@ -5,7 +5,7 @@ * @package LifterLMS/Functions * * @since 1.0.0 - * @version 7.1.3 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -346,6 +346,22 @@ function lifterlms_template_loop_difficulty() { } } +/** + * Count of Total Lessons in a Course. + * + * @since [version] + * + * @return void. + */ +if ( ! function_exists( 'lifterlms_template_loop_lesson_count' ) ) { + + function lifterlms_template_loop_lesson_count() { + if ( 'course' === get_post_type( get_the_ID() ) ) { + llms_get_template( 'course/lesson-count.php' ); + } + } +} + /** * Show enrollment date meta * used on Dashboard only diff --git a/includes/llms.template.hooks.php b/includes/llms.template.hooks.php index ca205fb744..d1f6df2b14 100644 --- a/includes/llms.template.hooks.php +++ b/includes/llms.template.hooks.php @@ -7,7 +7,7 @@ * @package LifterLMS/Hooks * * @since 1.0.0 - * @version 6.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -79,6 +79,7 @@ add_action( 'lifterlms_after_loop_item_title', 'lifterlms_template_loop_author', 10 ); add_action( 'lifterlms_after_loop_item_title', 'lifterlms_template_loop_length', 15 ); add_action( 'lifterlms_after_loop_item_title', 'lifterlms_template_loop_difficulty', 20 ); +add_action( 'lifterlms_after_loop_item_title', 'lifterlms_template_loop_lesson_count', 25 ); add_action( 'lifterlms_after_loop_item', 'lifterlms_loop_link_end', 5 ); diff --git a/templates/course/length.php b/templates/course/length.php index d2db3d77a7..24d8e3dbc2 100644 --- a/templates/course/length.php +++ b/templates/course/length.php @@ -19,4 +19,3 @@

%s', 'lifterlms' ), $course->get( 'length' ) ); ?>

- diff --git a/templates/course/lesson-count.php b/templates/course/lesson-count.php new file mode 100644 index 0000000000..a8d9ab6efa --- /dev/null +++ b/templates/course/lesson-count.php @@ -0,0 +1,31 @@ +get_sections(); +$lessons = array(); +foreach ( $sections as $section ) { + $lessons = array_merge( $lessons, $section->get_lessons() ); +} +$lessons_count = count( $lessons ); +?> + +
+

%u', 'lifterlms' ), $lessons_count ); ?>

+
From aface06429606136fd3831e7d823ba3035520a1f Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 18 May 2023 17:24:09 +0530 Subject: [PATCH 034/125] added changelog --- .changelogs/issues_2434.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changelogs/issues_2434.yml diff --git a/.changelogs/issues_2434.yml b/.changelogs/issues_2434.yml new file mode 100644 index 0000000000..95aee54a5e --- /dev/null +++ b/.changelogs/issues_2434.yml @@ -0,0 +1,6 @@ +significance: minor +type: added +links: + - "#2434" +entry: Added a paragraph to show Number of lessons in a course at Course Catalog + and My Courses. From d8da2151dfdc1c7d47aee3af11076b636bd86e6b Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 24 May 2023 16:24:20 +0530 Subject: [PATCH 035/125] code optimized --- templates/course/lesson-count.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/templates/course/lesson-count.php b/templates/course/lesson-count.php index a8d9ab6efa..c48fc82cb5 100644 --- a/templates/course/lesson-count.php +++ b/templates/course/lesson-count.php @@ -16,14 +16,7 @@ global $post; $course = new LLMS_Course( $post ); - -// Get Total number of lessons. -$sections = $course->get_sections(); -$lessons = array(); -foreach ( $sections as $section ) { - $lessons = array_merge( $lessons, $section->get_lessons() ); -} -$lessons_count = count( $lessons ); +$lessons_count = $course->get_lessons_count(); ?>
From e2db3a28cb09a347ae0fad52e8fe9079783c3876 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 31 May 2023 14:59:31 +0530 Subject: [PATCH 036/125] minor fixes --- templates/course/lesson-count.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/templates/course/lesson-count.php b/templates/course/lesson-count.php index c48fc82cb5..09a7b2703b 100644 --- a/templates/course/lesson-count.php +++ b/templates/course/lesson-count.php @@ -1,24 +1,21 @@ get_lessons_count(); ?>
-

%u', 'lifterlms' ), $lessons_count ); ?>

+ %1$s%2$s

', esc_html__( 'Number of Lessons: ', 'lifterlms' ), $lessons_count ); ?>
From 591d6f22725b5b9d8ff1e265e1b4c07eb258130c Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 31 May 2023 16:37:35 +0530 Subject: [PATCH 037/125] minor fixes --- templates/course/lesson-count.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/templates/course/lesson-count.php b/templates/course/lesson-count.php index 09a7b2703b..4e02dc7041 100644 --- a/templates/course/lesson-count.php +++ b/templates/course/lesson-count.php @@ -17,5 +17,10 @@ ?>
- %1$s%2$s

', esc_html__( 'Number of Lessons: ', 'lifterlms' ), $lessons_count ); ?> +

+ ' . $lessons_count . '' ); + ?> +

From 579e2af19cfcd607f8b3116eed8eb5830fa485c4 Mon Sep 17 00:00:00 2001 From: Rocco Aliberti Date: Fri, 2 Jun 2023 11:57:25 +0200 Subject: [PATCH 038/125] Update templates/course/lesson-count.php --- templates/course/lesson-count.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/course/lesson-count.php b/templates/course/lesson-count.php index 4e02dc7041..9339330fa2 100644 --- a/templates/course/lesson-count.php +++ b/templates/course/lesson-count.php @@ -20,7 +20,7 @@

' . $lessons_count . '' ); + printf( esc_html__( 'Number of lessons: %1$s', 'lifterlms' ), '' . $lessons_count . '' ); ?>

From 7bb81fd8e04330d6fee5dbd93eed22a640e43271 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 21 Jun 2023 16:42:53 +0530 Subject: [PATCH 039/125] whole wrapper clickable for favorites --- assets/js/llms-favorites.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/assets/js/llms-favorites.js b/assets/js/llms-favorites.js index 7aa6c9c4b9..c694daee9f 100644 --- a/assets/js/llms-favorites.js +++ b/assets/js/llms-favorites.js @@ -31,9 +31,10 @@ var self = this; // Favorite clicked - $( '.llms-favorite-wrapper' ).on( 'click', '.llms-heart-btn', function( e ) { + $( '.llms-favorite-wrapper' ).on( 'click', function( e ) { e.preventDefault(); - self.favorite( $( this ) ); + let $btn = $( this ).find( '.llms-heart-btn' ); + self.favorite( $btn ); } ); // Adding class in Favorite's parent From 09fbb232d64e43f94fc14930d1bf337906b112dc Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 22 Jun 2023 12:52:23 +0530 Subject: [PATCH 040/125] favs grouped under respective course --- .../llms.functions.templates.dashboard.php | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/includes/functions/llms.functions.templates.dashboard.php b/includes/functions/llms.functions.templates.dashboard.php index a771411ef6..27f337adda 100644 --- a/includes/functions/llms.functions.templates.dashboard.php +++ b/includes/functions/llms.functions.templates.dashboard.php @@ -313,9 +313,27 @@ function lifterlms_template_my_favorites_loop( $student = null ) { } else { - foreach ( $favorites as $favorite ) { + // Adding Parent Course IDs in Favorites for each lesson. + foreach ( $favorites as $key => $favorite ) { + $parent_course = get_post_meta( $favorite->post_id, '_llms_parent_course', true ); + $favorite->parent_course = $parent_course; + } + + // Grouping Favorites by Parent Course ID. + $favorites = array_reduce( + $favorites, + function ( $carry, $item ) { + $carry[ $item->parent_course ][] = $item; + return $carry; + }, + array() + ); - $lesson = new LLMS_Lesson( $favorite->post_id ); + // Printing Favorite Lessons under each Parent Course. + foreach ( $favorites as $course => $lessons ) { + + // Get Course Name. + $lesson = new LLMS_Lesson( $course ); llms_get_template( 'course/lesson-preview.php', @@ -324,6 +342,21 @@ function lifterlms_template_my_favorites_loop( $student = null ) { ) ); + echo '
    '; + foreach ( $lessons as $lesson ) { + + $lesson = new LLMS_Lesson( $lesson->post_id ); + echo '
  • '; + llms_get_template( + 'course/lesson-preview.php', + array( + 'lesson' => $lesson, + ) + ); + echo '
  • '; + } + echo '
'; + } } From b001128e7824e59f9d73bba11929a13ce985813f Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 22 Jun 2023 13:21:28 +0530 Subject: [PATCH 041/125] removed TODOs --- templates/course/favorite.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 4db37d5e51..da6fe95d91 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -32,12 +32,10 @@ is_favorite( $lesson->get( 'id' ), 'lesson' ) ) : ?> - - From efeef1aa3db255040e48382458c4fa75510a387c Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Thu, 22 Jun 2023 13:37:36 +0530 Subject: [PATCH 042/125] equalize dashboard and shortcode output --- .../llms.functions.templates.dashboard.php | 6 ++--- .../class.llms.shortcode.favorites.php | 22 +------------------ 2 files changed, 4 insertions(+), 24 deletions(-) diff --git a/includes/functions/llms.functions.templates.dashboard.php b/includes/functions/llms.functions.templates.dashboard.php index 27f337adda..0139690cac 100644 --- a/includes/functions/llms.functions.templates.dashboard.php +++ b/includes/functions/llms.functions.templates.dashboard.php @@ -298,14 +298,14 @@ function lifterlms_template_my_courses_loop( $student = null, $preview = false ) * @param LLMS_Student $student Optional. LLMS_Student (current student if none supplied). Default `null`. * @return void */ - function lifterlms_template_my_favorites_loop( $student = null ) { + function lifterlms_template_my_favorites_loop( $student = null, $favorites = null ) { $student = llms_get_student( $student ); if ( ! $student ) { return; } - $favorites = $student->get_favorites(); + $favorites = $favorites ?? $student->get_favorites(); if ( ! $favorites ) { @@ -630,7 +630,7 @@ function lifterlms_template_student_dashboard_my_favorites( $preview = false ) { } ob_start(); - lifterlms_template_my_favorites_loop( $student, $preview ); + lifterlms_template_my_favorites_loop( $student ); llms_get_template( 'myaccount/dashboard-section.php', diff --git a/includes/shortcodes/class.llms.shortcode.favorites.php b/includes/shortcodes/class.llms.shortcode.favorites.php index 9f590238cb..2657d975e5 100644 --- a/includes/shortcodes/class.llms.shortcode.favorites.php +++ b/includes/shortcodes/class.llms.shortcode.favorites.php @@ -76,8 +76,6 @@ protected function get_favorites() { */ protected function get_output() { - $this->enqueue_script( 'llms-jquery-matchheight' ); - ob_start(); // If we're outputting a "My Favorites" list and we don't have a student output login info. @@ -93,25 +91,7 @@ protected function get_output() { $favorites = $this->get_favorites(); - if ( $favorites ) { - - foreach ( $favorites as $favorite ) { - - $lesson = new LLMS_Lesson( $favorite->post_id ); - - llms_get_template( - 'course/lesson-preview.php', - array( - 'lesson' => $lesson, - ) - ); - - } - } else { - - printf( '

%s

', __( 'No favorites found.', 'lifterlms' ) ); - - } + lifterlms_template_my_favorites_loop( get_current_user_id(), $favorites ); } return ob_get_clean(); From 9b55935c4c294d6cdcc7242b6ebaf4eb7c46f4c1 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 26 Jul 2023 15:29:49 +0530 Subject: [PATCH 043/125] Added enable/disable control for favs --- class-lifterlms.php | 7 +++++-- .../admin/settings/class.llms.settings.general.php | 11 ++++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/class-lifterlms.php b/class-lifterlms.php index f265346cd4..408ab94e80 100644 --- a/class-lifterlms.php +++ b/class-lifterlms.php @@ -5,7 +5,7 @@ * @package LifterLMS/Main * * @since 1.0.0 - * @version 7.2.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -429,7 +429,10 @@ public function localize() { * @return boolean */ public function is_favorites_enabled() { - return apply_filters( 'llms_favorites_enabled', true ); + + $favorite_enabled = llms_parse_bool( get_option( 'lifterlms_favorites', 'no' ) ); + + return apply_filters( 'llms_favorites_enabled', $favorite_enabled ); } } diff --git a/includes/admin/settings/class.llms.settings.general.php b/includes/admin/settings/class.llms.settings.general.php index b93ed7790a..f1a5f897d9 100644 --- a/includes/admin/settings/class.llms.settings.general.php +++ b/includes/admin/settings/class.llms.settings.general.php @@ -5,7 +5,7 @@ * @package LifterLMS/Admin/Settings/Classes * * @since 1.0.0 - * @version 6.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -45,6 +45,7 @@ public function __construct() { * @since 3.13.0 Unknown. * @since 5.6.0 use LLMS_Roles::get_all_role_names() to retrieve the list of roles who can bypass enrollments. * Add content protection setting. + * @since [version] Added settings for enabling/disabling favorites. * * @return array */ @@ -72,6 +73,14 @@ public function get_settings() { ), ); + $settings[] = array( + 'title' => __( 'Lesson Favorites', 'lifterlms' ), + 'desc' => __( 'Enabling this setting allows students to mark a lesson as "favorite".', 'lifterlms' ), + 'id' => 'lifterlms_favorites', + 'default' => 'no', + 'type' => 'checkbox', + ); + $settings[] = array( 'id' => 'section_features', 'type' => 'sectionend', From 5621a21547d60170a55586ce88ac5d711ac30fdd Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Wed, 26 Jul 2023 19:16:46 +0530 Subject: [PATCH 044/125] Added changelogs and docblock fixes --- .changelogs/feature_favorite-1.yml | 3 +++ .changelogs/feature_favorite-2.yml | 4 ++++ .changelogs/feature_favorite-3.yml | 4 ++++ .changelogs/feature_favorite.yml | 3 +++ class-lifterlms.php | 9 ++++++++- includes/admin/settings/class.llms.settings.accounts.php | 2 +- includes/assets/llms-assets-scripts.php | 2 +- includes/class.llms.ajax.php | 3 ++- includes/class.llms.frontend.assets.php | 2 +- includes/class.llms.query.user.postmeta.php | 7 ++++--- includes/class.llms.student.dashboard.php | 2 +- includes/functions/llms.functions.course.php | 2 +- includes/functions/llms.functions.person.php | 2 +- includes/llms.template.functions.php | 2 +- includes/models/model.llms.student.php | 6 +++--- includes/shortcodes/class.llms.shortcode.favorites.php | 4 ++-- includes/shortcodes/class.llms.shortcodes.php | 3 ++- templates/myaccount/dashboard.php | 2 +- 18 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 .changelogs/feature_favorite-1.yml create mode 100644 .changelogs/feature_favorite-2.yml create mode 100644 .changelogs/feature_favorite-3.yml create mode 100644 .changelogs/feature_favorite.yml diff --git a/.changelogs/feature_favorite-1.yml b/.changelogs/feature_favorite-1.yml new file mode 100644 index 0000000000..8f8159c56c --- /dev/null +++ b/.changelogs/feature_favorite-1.yml @@ -0,0 +1,3 @@ +significance: minor +type: dev +entry: Added 'favorites' in User postmeta for getting all user's favorites. diff --git a/.changelogs/feature_favorite-2.yml b/.changelogs/feature_favorite-2.yml new file mode 100644 index 0000000000..a86c9048c8 --- /dev/null +++ b/.changelogs/feature_favorite-2.yml @@ -0,0 +1,4 @@ +significance: minor +type: dev +entry: Added filter `lifterlms_single_course_syllabus_lesson_favorite` for + disabling favorites in syllabus view. diff --git a/.changelogs/feature_favorite-3.yml b/.changelogs/feature_favorite-3.yml new file mode 100644 index 0000000000..ee9fdfd87c --- /dev/null +++ b/.changelogs/feature_favorite-3.yml @@ -0,0 +1,4 @@ +significance: minor +type: dev +entry: Added filter `llms_is_$object_type_favorite` to change object's (lesson, + student, course) favorite boolean value. diff --git a/.changelogs/feature_favorite.yml b/.changelogs/feature_favorite.yml new file mode 100644 index 0000000000..55f9764807 --- /dev/null +++ b/.changelogs/feature_favorite.yml @@ -0,0 +1,3 @@ +significance: minor +type: dev +entry: Added filter `llms_favorites_enabled` to enable/disable Favorites feature. diff --git a/class-lifterlms.php b/class-lifterlms.php index 408ab94e80..6462d75cc7 100644 --- a/class-lifterlms.php +++ b/class-lifterlms.php @@ -24,7 +24,7 @@ * Remove deprecated class files and variables. * Move includes (file loading) into the LLMS_Loader class. * @since 5.3.0 Replace singleton code with `LLMS_Trait_Singleton`. - * @since [version] Include the Favorite filter to enable the feature. + * @since [version] Added `is_favorites_enabled` to check favorites status. */ final class LifterLMS { @@ -432,6 +432,13 @@ public function is_favorites_enabled() { $favorite_enabled = llms_parse_bool( get_option( 'lifterlms_favorites', 'no' ) ); + /** + * Filter to enable/disable the Favorite feature. + * + * @since [version] + * + * @param boolean $favorite_enabled True if favorites are enabled, false otherwise. + */ return apply_filters( 'llms_favorites_enabled', $favorite_enabled ); } diff --git a/includes/admin/settings/class.llms.settings.accounts.php b/includes/admin/settings/class.llms.settings.accounts.php index c50332a7e1..abfcb4a686 100644 --- a/includes/admin/settings/class.llms.settings.accounts.php +++ b/includes/admin/settings/class.llms.settings.accounts.php @@ -5,7 +5,7 @@ * @package LifterLMS/Admin/Settings/Classes * * @since 1.0.0 - * @version 5.6.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; diff --git a/includes/assets/llms-assets-scripts.php b/includes/assets/llms-assets-scripts.php index 434485c026..f9c9046816 100644 --- a/includes/assets/llms-assets-scripts.php +++ b/includes/assets/llms-assets-scripts.php @@ -18,7 +18,7 @@ * @package LifterLMS/Assets * * @since 4.4.0 - * @version 7.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; diff --git a/includes/class.llms.ajax.php b/includes/class.llms.ajax.php index 96bb3bd45c..e4e21b3034 100644 --- a/includes/class.llms.ajax.php +++ b/includes/class.llms.ajax.php @@ -5,7 +5,7 @@ * @package LifterLMS/Classes * * @since 1.0.0 - * @version 5.9.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -20,6 +20,7 @@ * - `LLMS_AJAX::check_voucher_duplicate()` method. * - `LLMS_AJAX::get_ajax_data()` method. * - `LLMS_AJAX::register_script()` method. + * @since [version] Added method `favorite_object` to Add Favorite / Unfavorite Postmeta for an object. */ class LLMS_AJAX { diff --git a/includes/class.llms.frontend.assets.php b/includes/class.llms.frontend.assets.php index 37939eb2f6..8e08b0d1c3 100644 --- a/includes/class.llms.frontend.assets.php +++ b/includes/class.llms.frontend.assets.php @@ -5,7 +5,7 @@ * @package LifterLMS/Classes * * @since 1.0.0 - * @version 7.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; diff --git a/includes/class.llms.query.user.postmeta.php b/includes/class.llms.query.user.postmeta.php index 2ec0baa229..b4cc0759bf 100644 --- a/includes/class.llms.query.user.postmeta.php +++ b/includes/class.llms.query.user.postmeta.php @@ -5,7 +5,7 @@ * @package LifterLMS/Classes * * @since 3.15.0 - * @version 6.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -14,7 +14,7 @@ * LifterLMS User Postmeta Query * * @since 3.15.0 - * @version 3.15.0 + * @version [version] */ class LLMS_Query_User_Postmeta extends LLMS_Database_Query { @@ -87,7 +87,8 @@ public function get_metas() { * * @return void * @since 3.15.0 - * @version 3.15.0 + * @since [version] Added 'Favorites' event. + * @version [version] */ protected function parse_args() { diff --git a/includes/class.llms.student.dashboard.php b/includes/class.llms.student.dashboard.php index a71808a25a..f6660f9f98 100644 --- a/includes/class.llms.student.dashboard.php +++ b/includes/class.llms.student.dashboard.php @@ -5,7 +5,7 @@ * @package LifterLMS/Classes * * @since 3.0.0 - * @version 6.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; diff --git a/includes/functions/llms.functions.course.php b/includes/functions/llms.functions.course.php index 61c4fefa8c..f987c74f24 100644 --- a/includes/functions/llms.functions.course.php +++ b/includes/functions/llms.functions.course.php @@ -5,7 +5,7 @@ * @package LifterLMS/Functions * * @since Unknown - * @version [version] + * @version 3.37.13 */ defined( 'ABSPATH' ) || exit; diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index 16c1d2aa3e..3d470bf550 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -7,7 +7,7 @@ * @package LifterLMS/Functions * * @since 1.0.0 - * @version 7.1.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index a40436c56c..5460244580 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -5,7 +5,7 @@ * @package LifterLMS/Functions/Templates * * @since 1.0.0 - * @version 7.1.2 + * @version [version] */ defined( 'ABSPATH' ) || exit; diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 7eeee5c50f..1497d12b56 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -5,7 +5,7 @@ * @package LifterLMS/Models/Classes * * @since 2.2.3 - * @version 6.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -27,7 +27,7 @@ * Added new filter to allow customization of object completion data. * @since 5.2.0 Changed the date to be relative to the local time zone in `get_registration_date`. * @since 6.0.0 Removed the deprecated `llms_user_removed_from_membership_level` action hook from the `LLMS_Student::unenroll()` method. - * @since [version] Added the logic to add and remove lesson favoritism. + * @since [version] Added the logic to add and remove lesson favorite. */ class LLMS_Student extends LLMS_Abstract_User_Data { @@ -1933,7 +1933,7 @@ public function is_favorite( $object_id, $object_type = 'lesson' ) { $ret = $query->has_results(); /** - * Filter object favorite boolean value prior to returning + * Filter object favorite boolean value prior to returning. * * The dynamic portion of this filter, `{$object_type}`, refers to the Lesson, Course or Instructor. * diff --git a/includes/shortcodes/class.llms.shortcode.favorites.php b/includes/shortcodes/class.llms.shortcode.favorites.php index 2657d975e5..9463e6a91b 100644 --- a/includes/shortcodes/class.llms.shortcode.favorites.php +++ b/includes/shortcodes/class.llms.shortcode.favorites.php @@ -20,14 +20,14 @@ class LLMS_Shortcode_Favorites extends LLMS_Shortcode { /** - * Shortcode tag + * Shortcode tag. * * @var string */ public $tag = 'lifterlms_favorites'; /** - * Get shortcode attributes + * Get shortcode attributes. * * Retrieves an array of default attributes which are automatically merged * with the user submitted attributes and passed to $this->get_output(). diff --git a/includes/shortcodes/class.llms.shortcodes.php b/includes/shortcodes/class.llms.shortcodes.php index 9b49b6f61f..e58a24ca48 100644 --- a/includes/shortcodes/class.llms.shortcodes.php +++ b/includes/shortcodes/class.llms.shortcodes.php @@ -5,7 +5,7 @@ * @package LifterLMS/Classes/Shortcodes * * @since 1.0.0 - * @version 7.2.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -37,6 +37,7 @@ public function __construct() { * @since 4.0.0 Stop registering previously deprecated shortcode `[courses]` and `[lifterlms_user_statistics]`. * @since 6.0.0 Removed loading of class files that don't instantiate their class in favor of autoloading. * @since 6.4.0 Allowed `LLMS_Shortcode_User_Info` class to be filtered. + * @since [version] Added `LLMS_Shortcode_Favorites` class in shortcodes array. * * @return void */ diff --git a/templates/myaccount/dashboard.php b/templates/myaccount/dashboard.php index a889e06fe0..8cd85c0f6e 100644 --- a/templates/myaccount/dashboard.php +++ b/templates/myaccount/dashboard.php @@ -1,6 +1,6 @@ Date: Fri, 28 Jul 2023 13:34:45 +0530 Subject: [PATCH 045/125] cs and code fixes --- .changelogs/feature_favorite-2.yml | 2 +- assets/js/llms-favorites.js | 6 +- class-lifterlms.php | 24 +---- .../settings/class.llms.settings.accounts.php | 2 +- includes/class.llms.ajax.php | 2 +- includes/class.llms.frontend.assets.php | 2 +- includes/class.llms.student.dashboard.php | 2 +- .../functions/llms.functions.favorite.php | 30 +++++- includes/functions/llms.functions.person.php | 16 +-- .../llms.functions.templates.dashboard.php | 21 ++-- includes/llms.template.functions.php | 10 +- includes/llms.template.hooks.php | 2 +- includes/models/model.llms.student.php | 99 +++++++++---------- .../class.llms.shortcode.favorites.php | 2 +- templates/content-single-lesson-before.php | 2 +- templates/course/favorite.php | 30 +++++- templates/course/lesson-preview.php | 18 +++- templates/myaccount/dashboard.php | 5 +- .../unit-tests/class-llms-test-query.php | 4 +- 19 files changed, 153 insertions(+), 126 deletions(-) diff --git a/.changelogs/feature_favorite-2.yml b/.changelogs/feature_favorite-2.yml index a86c9048c8..bbaf6927ef 100644 --- a/.changelogs/feature_favorite-2.yml +++ b/.changelogs/feature_favorite-2.yml @@ -1,4 +1,4 @@ significance: minor type: dev -entry: Added filter `lifterlms_single_course_syllabus_lesson_favorite` for +entry: Added filter `llms_course_syllabus_lesson_favorite_visibility` for disabling favorites in syllabus view. diff --git a/assets/js/llms-favorites.js b/assets/js/llms-favorites.js index c694daee9f..ce503376fe 100644 --- a/assets/js/llms-favorites.js +++ b/assets/js/llms-favorites.js @@ -4,9 +4,9 @@ /** * Front End Favorite Class. * - * @type {Object} - * @since [version] - * @version [version] + * @type {Object} + * @since [version] + * @version [version] */ ( function( $ ) { diff --git a/class-lifterlms.php b/class-lifterlms.php index 6462d75cc7..c4356a60a3 100644 --- a/class-lifterlms.php +++ b/class-lifterlms.php @@ -5,7 +5,7 @@ * @package LifterLMS/Main * * @since 1.0.0 - * @version [version] + * @version 7.2.0 */ defined( 'ABSPATH' ) || exit; @@ -24,7 +24,6 @@ * Remove deprecated class files and variables. * Move includes (file loading) into the LLMS_Loader class. * @since 5.3.0 Replace singleton code with `LLMS_Trait_Singleton`. - * @since [version] Added `is_favorites_enabled` to check favorites status. */ final class LifterLMS { @@ -421,25 +420,4 @@ public function localize() { } - /** - * Filter Hook to enable the Favorite feature. - * - * @since [version] - * - * @return boolean - */ - public function is_favorites_enabled() { - - $favorite_enabled = llms_parse_bool( get_option( 'lifterlms_favorites', 'no' ) ); - - /** - * Filter to enable/disable the Favorite feature. - * - * @since [version] - * - * @param boolean $favorite_enabled True if favorites are enabled, false otherwise. - */ - return apply_filters( 'llms_favorites_enabled', $favorite_enabled ); - } - } diff --git a/includes/admin/settings/class.llms.settings.accounts.php b/includes/admin/settings/class.llms.settings.accounts.php index abfcb4a686..d3f86f3c06 100644 --- a/includes/admin/settings/class.llms.settings.accounts.php +++ b/includes/admin/settings/class.llms.settings.accounts.php @@ -188,7 +188,7 @@ public function get_settings() { 'type' => 'text', 'default' => 'my-favorites', 'sanitize' => 'slug', - 'disabled' => ! llms()->is_favorites_enabled() ? true : false, + 'disabled' => ! llms_is_favorites_enabled() ? true : false, ), array( 'title' => __( 'Notifications', 'lifterlms' ), diff --git a/includes/class.llms.ajax.php b/includes/class.llms.ajax.php index e4e21b3034..1be5de105c 100644 --- a/includes/class.llms.ajax.php +++ b/includes/class.llms.ajax.php @@ -241,7 +241,7 @@ public function favorite_object() { echo json_encode( array( - 'total_favorites' => get_total_favorites( $object_id ), + 'total_favorites' => llms_get_object_total_favorites( $object_id ), 'success' => true, ) ); diff --git a/includes/class.llms.frontend.assets.php b/includes/class.llms.frontend.assets.php index 8e08b0d1c3..b91b7b1ef3 100644 --- a/includes/class.llms.frontend.assets.php +++ b/includes/class.llms.frontend.assets.php @@ -201,7 +201,7 @@ public static function enqueue_scripts() { llms()->assets->enqueue_script( 'llms-quiz' ); } - if ( ( is_lesson() || is_course() ) && true === llms()->is_favorites_enabled() ) { + if ( ( is_lesson() || is_course() ) && true === llms_is_favorites_enabled() ) { llms()->assets->enqueue_script( 'llms-favorites' ); } diff --git a/includes/class.llms.student.dashboard.php b/includes/class.llms.student.dashboard.php index f6660f9f98..d3f5a4f597 100644 --- a/includes/class.llms.student.dashboard.php +++ b/includes/class.llms.student.dashboard.php @@ -192,7 +192,7 @@ public static function get_tabs() { ), 'view-favorites' => array( 'content' => 'lifterlms_template_student_dashboard_my_favorites', - 'endpoint' => llms()->is_favorites_enabled() ? get_option( 'lifterlms_myaccount_favorites_endpoint', 'view-favorites' ) : '', + 'endpoint' => llms_is_favorites_enabled() ? get_option( 'lifterlms_myaccount_favorites_endpoint', 'view-favorites' ) : '', 'paginate' => true, 'nav_item' => true, 'title' => __( 'My Favorites', 'lifterlms' ), diff --git a/includes/functions/llms.functions.favorite.php b/includes/functions/llms.functions.favorite.php index 87b3b7b075..e25b47bdbc 100644 --- a/includes/functions/llms.functions.favorite.php +++ b/includes/functions/llms.functions.favorite.php @@ -15,13 +15,18 @@ * * @since [version] * - * @param WP_Post|int|false $object_id Lesson post object or id. If `false` uses the global `$post` object. + * @param int $object_id WP Post ID of the Lesson. * @return int */ -function get_total_favorites( $object_id = false ) { +function llms_get_object_total_favorites( $object_id = false ) { global $wpdb; + // Getting ID from Global Post object. + if ( ! $object_id ) { + $object_id = get_the_ID(); + } + // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $res = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->prepare( @@ -36,3 +41,24 @@ function get_total_favorites( $object_id = false ) { return count( $res ); } + +/** + * Filter Hook to enable the Favorite feature. + * + * @since [version] + * + * @return bool True if favorites are enabled, false otherwise. + */ +function llms_is_favorites_enabled() { + + $favorite_enabled = llms_parse_bool( get_option( 'lifterlms_favorites', 'no' ) ); + + /** + * Filter to enable/disable the Favorite feature. + * + * @since [version] + * + * @param bool $favorite_enabled True if favorites are enabled, false otherwise. + */ + return apply_filters( 'llms_favorites_enabled', $favorite_enabled ); +} diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index 3d470bf550..44a8c6e01f 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -324,8 +324,8 @@ function llms_is_user_enrolled( $user_id, $product_id, $relation = 'all', $use_c * @see LLMS_Student->mark_complete() * * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param string $object_type Object type [lesson|section|course|track]. + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. * @param string $trigger String describing the event that triggered marking the object as complete. * @return boolean */ @@ -342,8 +342,8 @@ function llms_mark_complete( $user_id, $object_id, $object_type, $trigger = 'uns * @see LLMS_Student->mark_incomplete() * * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param string $object_type Object type [lesson|section|course|track]. + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. * @param string $trigger String describing the event that triggered marking the object as incomplete. * @return boolean */ @@ -360,8 +360,8 @@ function llms_mark_incomplete( $user_id, $object_id, $object_type, $trigger = 'u * @see LLMS_Student->mark_favorite() * * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param string $object_type Object type [lesson|section|course|track]. + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. * @return boolean */ function llms_mark_favorite( $user_id, $object_id, $object_type ) { @@ -377,8 +377,8 @@ function llms_mark_favorite( $user_id, $object_id, $object_type ) { * @see LLMS_Student->mark_unfavorite() * * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param string $object_type Object type [lesson|section|course|track]. + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. * @return boolean */ function llms_mark_unfavorite( $user_id, $object_id, $object_type ) { diff --git a/includes/functions/llms.functions.templates.dashboard.php b/includes/functions/llms.functions.templates.dashboard.php index 0139690cac..5fc0edb9a6 100644 --- a/includes/functions/llms.functions.templates.dashboard.php +++ b/includes/functions/llms.functions.templates.dashboard.php @@ -153,7 +153,7 @@ function lifterlms_student_dashboard( $options = array() ) { } -if ( ! function_exists( 'lifterlms_template_my_courses_loop' ) ) { +if ( ! function_exists( 'llms_template_my_courses_loop' ) ) { /** * Get course tiles for a student's courses @@ -168,7 +168,7 @@ function lifterlms_student_dashboard( $options = array() ) { * @param bool $preview Optional. If true, outputs a short list of courses (based on dashboard_recent_courses filter). Default `false`. * @return void */ - function lifterlms_template_my_courses_loop( $student = null, $preview = false ) { + function llms_template_my_courses_loop( $student = null, $preview = false ) { $student = llms_get_student( $student ); if ( ! $student ) { @@ -288,17 +288,18 @@ function lifterlms_template_my_courses_loop( $student = null, $preview = false ) } } -if ( ! function_exists( 'lifterlms_template_my_favorites_loop' ) ) { +if ( ! function_exists( 'llms_template_my_favorites_loop' ) ) { /** * Get student's favorites. * * @since [version] * - * @param LLMS_Student $student Optional. LLMS_Student (current student if none supplied). Default `null`. + * @param LLMS_Student $student Optional. LLMS_Student (current student if none supplied). Default `null`. + * @param array $favorites Optional. Array of favorites (current student's favorites if none supplied). Default `null`. * @return void */ - function lifterlms_template_my_favorites_loop( $student = null, $favorites = null ) { + function llms_template_my_favorites_loop( $student = null, $favorites = null ) { $student = llms_get_student( $student ); if ( ! $student ) { @@ -315,8 +316,8 @@ function lifterlms_template_my_favorites_loop( $student = null, $favorites = nul // Adding Parent Course IDs in Favorites for each lesson. foreach ( $favorites as $key => $favorite ) { - $parent_course = get_post_meta( $favorite->post_id, '_llms_parent_course', true ); - $favorite->parent_course = $parent_course; + $lesson = new LLMS_Lesson( $favorite->post_id ); + $favorite->parent_course = $lesson->get( 'parent_course' ); } // Grouping Favorites by Parent Course ID. @@ -587,7 +588,7 @@ function lifterlms_template_student_dashboard_my_courses( $preview = false ) { } ob_start(); - lifterlms_template_my_courses_loop( $student, $preview ); + llms_template_my_courses_loop( $student, $preview ); llms_get_template( 'myaccount/dashboard-section.php', @@ -617,7 +618,7 @@ function lifterlms_template_student_dashboard_my_favorites( $preview = false ) { $student = llms_get_student(); - if ( ! $student || ! llms()->is_favorites_enabled() ) { + if ( ! $student || ! llms_is_favorites_enabled() ) { return; } @@ -630,7 +631,7 @@ function lifterlms_template_student_dashboard_my_favorites( $preview = false ) { } ob_start(); - lifterlms_template_my_favorites_loop( $student ); + llms_template_my_favorites_loop( $student ); llms_get_template( 'myaccount/dashboard-section.php', diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index 5460244580..508e11216b 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -390,15 +390,15 @@ function lifterlms_template_single_parent_course() { * * @since [version] * - * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @param string $object_type Object type [lesson|section|course|track]. + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. * @return void */ -if ( ! function_exists( 'lifterlms_template_favorite' ) ) { +if ( ! function_exists( 'llms_template_favorite' ) ) { - function lifterlms_template_favorite( $object_id = null, $object_type = 'lesson' ) { + function llms_template_favorite( $object_id = null, $object_type = 'lesson' ) { - if ( llms()->is_favorites_enabled() ) { + if ( llms_is_favorites_enabled() ) { llms_get_template( 'course/favorite.php', diff --git a/includes/llms.template.hooks.php b/includes/llms.template.hooks.php index eb8b01193c..a9c43a9490 100644 --- a/includes/llms.template.hooks.php +++ b/includes/llms.template.hooks.php @@ -48,7 +48,7 @@ * @since Unknown */ add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_parent_course', 10 ); -add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_favorite', 10 ); +add_action( 'lifterlms_single_lesson_before_summary', 'llms_template_favorite', 10 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_video', 20 ); add_action( 'lifterlms_single_lesson_before_summary', 'lifterlms_template_single_lesson_audio', 20 ); diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 1497d12b56..b3cc840f19 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -99,7 +99,7 @@ private function add_membership_level( $membership_id ) { * * @param int $product_id WP Post ID of the course or membership * @param string $trigger String describing the reason for enrollment - * @return boolean + * @return bool */ public function enroll( $product_id, $trigger = 'unspecified' ) { @@ -268,12 +268,12 @@ public function get_courses( $args = array() ) { /** * Retrieve user's favorites based on supplied criteria. * - * @since [version] + * @since [version] * - * @param string $order_by Result set ordering field. Default "date". - * @param string $order Result set order. Default "DESC". Accepts "DESC" or "ASC". - * @param integer $limit Number of favorites to return. - * @return array + * @param string $order_by Result set ordering field. Default "date". + * @param string $order Result set order. Default "DESC". Accepts "DESC" or "ASC". + * @param int $limit Number of favorites to return. + * @return array */ public function get_favorites( $order_by = '', $order = '', $limit = '' ) { @@ -1125,7 +1125,7 @@ public function get_registration_date( $format = '' ) { * * @since 3.14.0 * - * @return boolean + * @return bool */ public function is_active() { @@ -1208,7 +1208,7 @@ public function is_instructor() { * * @param int $object_id WP Post ID of the lesson, section, course or track * @param string $trigger String describing the reason for mark completion - * @return boolean + * @return bool * @since 3.3.1 * @version 3.21.0 */ @@ -1293,13 +1293,13 @@ private function insert_incompletion_postmeta( $object_id, $trigger = 'unspecifi * @see LLMS_Student->mark_favorite() * * @param int $object_id WP User ID. - * @return boolean + * @return bool */ private function insert_favorite_postmeta( $object_id ) { $update = llms_update_user_postmeta( $this->get_id(), $object_id, '_favorite', true ); - // Returns an array with errored keys or true on success. + // Returns boolean if postmeta update is successful. return is_array( $update ) ? false : true; } @@ -1310,13 +1310,13 @@ private function insert_favorite_postmeta( $object_id ) { * @since [version] * * @param int $object_id WP Post ID of the lesson - * @return boolean + * @return bool */ private function remove_unfavorite_postmeta( $object_id ) { $update = llms_delete_user_postmeta( $this->get_id(), $object_id, '_favorite', true ); - // Returns an array with errored keys or true on success. + // Returns boolean if postmeta update is successful. return is_array( $update ) ? false : true; } @@ -1353,7 +1353,7 @@ private function insert_enrollment_postmeta( $product_id, $trigger = 'unspecifie * * @param int $product_id WP Post ID of the course or membership. * @param string $trigger Optional. String the reason for enrollment. Default `null` - * @return boolean Whether or not the enrollment records have been succesfully removed. + * @return bool Whether or not the enrollment records have been succesfully removed. */ private function delete_enrollment_postmeta( $product_id, $trigger = null ) { @@ -1444,12 +1444,12 @@ public function is_enrolled( $product_ids = null, $relation = 'all', $use_cache } /** - * Mark a lesson, section, course, or track complete for the given user + * Mark a lesson, section, course, or track complete for the given user. * * @param int $object_id WP Post ID of the lesson, section, course, or track * @param string $object_type object type [lesson|section|course|track] * @param string $trigger String describing the reason for marking complete - * @return boolean + * @return bool * * @see llms_mark_complete() calls this function without having to instantiate the LLMS_Student class first * @@ -1474,7 +1474,7 @@ public function mark_complete( $object_id, $object_type, $trigger = 'unspecified * @param int $object_id WP Post ID of the lesson, section, course, or track * @param string $object_type object type [lesson|section|course|track] * @param string $trigger String describing the reason for marking incomplete - * @return boolean + * @return bool * * @see llms_mark_incomplete() calls this function without having to instantiate the LLMS_Student class first * @@ -1552,7 +1552,7 @@ private function remove_membership_level( $membership_id, $status = 'expired', $ * @param string $trigger Only remove the student if the original enrollment trigger matches the submitted value. * Passing `any` will remove regardless of enrollment trigger. * @param string $new_status the value to update the new status with after removal is complete. - * @return boolean + * @return bool */ public function unenroll( $product_id, $trigger = 'any', $new_status = 'expired' ) { @@ -1656,7 +1656,7 @@ public function unenroll( $product_id, $trigger = 'any', $new_status = 'expired' * @param int $product_id WP Post ID of the course or membership. * @param string $trigger Optional. Only delete the student's enrollment if the original enrollment trigger matches the submitted value. * "any" will remove regardless of enrollment trigger. Default "any". - * @return boolean Whether or not the enrollment records have been successfully removed. + * @return bool Whether or not the enrollment records have been successfully removed. */ public function delete_enrollment( $product_id, $trigger = 'any' ) { @@ -1744,7 +1744,7 @@ public function delete_enrollment( $product_id, $trigger = 'any' ) { * @param int $object_id WP_Post ID of the object. * @param string $object_type The type of object. A lesson, section, course, or course_track. * @param string $trigger String describing the reason for the status change. - * @return boolean + * @return bool */ private function update_completion_status( $status, $object_id, $object_type, $trigger = 'unspecified' ) { @@ -1914,9 +1914,9 @@ private function update_completion_status( $status, $object_id, $object_type, $t * * @since [version] * - * @param int $object_id WP Post ID of a course or lesson or section or the term id of the track. - * @param string $object_type Object type (course, lesson, section, or track). - * @return boolean + * @param int $object_id WP Post ID of a lesson. + * @param string $object_type Type, 'Lesson'. + * @return bool */ public function is_favorite( $object_id, $object_type = 'lesson' ) { @@ -1935,29 +1935,29 @@ public function is_favorite( $object_id, $object_type = 'lesson' ) { /** * Filter object favorite boolean value prior to returning. * - * The dynamic portion of this filter, `{$object_type}`, refers to the Lesson, Course or Instructor. + * The dynamic portion of this filter, `{$object_type}`, refers to the Lesson. * * @since [version] * - * @param array|false $ret Array of favorite data or `false` if no favorite is found. - * @param int $object_id Object ID (Lesson, Course or Instructor). - * @param string $object_type Object description string (Lesson, Course or Instructor). - * @param LLMS_Student $instance The Student Instance + * @param array|false $ret Array of favorite data or `false` if no favorite is found. + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. + * @param LLMS_Student $instance The Student Instance */ return apply_filters( 'llms_is_' . $object_type . '_favorite', $ret, $object_id, $object_type, $this ); } /** - * Mark a lesson, section, course, or track favorite for the given user. + * Mark a lesson favorite for the given user. * * @since [version] * * @see llms_mark_favorite() calls this function without having to instantiate the LLMS_Student class first. * - * @param int $object_id WP Post ID of the lesson, section, course, or track. - * @param string $object_type object type [lesson|section|course|track]. - * @return boolean + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. + * @return bool */ public function mark_favorite( $object_id, $object_type ) { @@ -1971,15 +1971,15 @@ public function mark_favorite( $object_id, $object_type ) { } /** - * Mark a lesson, section, course, or track unfavorite for the given user. + * Mark a lesson unfavorite for the given user. * - * @since [version] + * @since [version] * - * @see llms_mark_unfavorite() calls this function without having to instantiate the LLMS_Student class first. + * @see llms_mark_unfavorite() calls this function without having to instantiate the LLMS_Student class first. * - * @param int $object_id WP Post ID of the lesson, section, course, or track. - * @param string $object_type object type [lesson|section|course|track]. - * @return boolean + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. + * @return bool */ public function mark_unfavorite( $object_id, $object_type ) { @@ -1995,16 +1995,16 @@ public function mark_unfavorite( $object_id, $object_type ) { /** * Triggers actions for favorite/unfavorite. * - * Update the favorite status of a track, course, section, or lesson for the current student + * Update the favorite status of a lesson for the current student. * Inserts / updates necessary user postmeta data. * * @since [version] Use filterable functions to determine if the object can be marked favorite. * * @param string $status New status to update to, either "favorite" or "unfavorite". - * @param int $object_id WP_Post ID of the object. - * @param string $object_type The type of object. A lesson, section, course, or course_track. + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. * @param string $trigger String describing the reason for the status change. - * @return boolean + * @return bool */ private function update_favorite_status( $status, $object_id, $object_type, $trigger = 'unspecified' ) { @@ -2019,21 +2019,12 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * @since [version] * * @param int $student_id WP_User ID of the student. - * @param int $object_id WP_Post ID of the object. - * @param string $object_type The type of object. A lesson, section, course, or course_track. + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. * @param string $trigger String describing the reason for the status change. */ do_action( "before_llms_mark_{$status}", $student_id, $object_id, $object_type, $trigger ); - // Retrieve an instance of the object we're acting on. - if ( in_array( $object_type, llms_get_completable_post_types(), true ) ) { - $object = llms_get_post( $object_id ); - } elseif ( in_array( $object_type, llms_get_completable_taxonomies(), true ) ) { - $object = get_term( $object_id, $object_type ); - } else { - return false; - } - // Insert / Remove meta data. if ( 'favorite' === $status ) { $this->insert_favorite_postmeta( $object_id, $trigger ); @@ -2050,8 +2041,8 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * @since [version] * * @param int $student_id WP_User ID of the student. - * @param int $object_id WP_Post ID of the object. - * @param string $object_type The type of object. A lesson, section, course, or course_track. + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. * @param string $trigger String describing the reason for the status change. */ do_action( "llms_mark_{$status}", $student_id, $object_id, $object_type, $trigger ); diff --git a/includes/shortcodes/class.llms.shortcode.favorites.php b/includes/shortcodes/class.llms.shortcode.favorites.php index 9463e6a91b..11399734a4 100644 --- a/includes/shortcodes/class.llms.shortcode.favorites.php +++ b/includes/shortcodes/class.llms.shortcode.favorites.php @@ -91,7 +91,7 @@ protected function get_output() { $favorites = $this->get_favorites(); - lifterlms_template_my_favorites_loop( get_current_user_id(), $favorites ); + llms_template_my_favorites_loop( get_current_user_id(), $favorites ); } return ob_get_clean(); diff --git a/templates/content-single-lesson-before.php b/templates/content-single-lesson-before.php index 9610309e7a..007fabe67c 100644 --- a/templates/content-single-lesson-before.php +++ b/templates/content-single-lesson-before.php @@ -12,7 +12,7 @@ /** * @hooked - lifterlms_template_single_parent_course - 10 - * @hooked - lifterlms_template_favorite - 10 + * @hooked - llms_template_favorite - 10 * @hooked - lifterlms_template_single_lesson_video - 20 * @hooked - lifterlms_template_single_lesson_audio - 20 */ diff --git a/templates/course/favorite.php b/templates/course/favorite.php index da6fe95d91..2d6b6d4b83 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -8,8 +8,8 @@ * * @since [version] * - * @var int $object_id WP Post ID of the Lesson, Section, Track, or Course. - * @var string $object_type Object type [lesson|section|course|track]. + * @var int $object_id WP Post ID of the Lesson. + * @var string $object_type Type, 'Lesson'. */ defined( 'ABSPATH' ) || exit; @@ -17,12 +17,22 @@ $lesson = new LLMS_Lesson( ( null === $object_id || '' === $object_id ) ? $post->ID : $object_id ); $student = llms_get_student( get_current_user_id() ); -$total_favorites = get_total_favorites( $lesson->get( 'id' ) ); +$total_favorites = llms_get_object_total_favorites( $lesson->get( 'id' ) ); ?>
- + @@ -46,6 +56,16 @@ - +
diff --git a/templates/course/lesson-preview.php b/templates/course/lesson-preview.php index ef3fe8dd4b..a3fa37a396 100644 --- a/templates/course/lesson-preview.php +++ b/templates/course/lesson-preview.php @@ -9,7 +9,7 @@ * @since 3.19.2 Unknown. * @since 4.4.0 Use the passed `$order` param if available, in favor of retrieving the lesson's order post meta. * @since 5.7.0 Replaced the call to the deprecated `LLMS_Lesson::get_order()` method with `LLMS_Lesson::get( 'order' )`. - * @since [version] Added Favorites function `lifterlms_template_favorite()`. + * @since [version] Added Favorites function `llms_template_favorite()`. * @version [version] * * @var LLMS_Lesson $lesson The lesson object. @@ -47,9 +47,19 @@
get( 'id' ) ); ?>
- - get( 'id' ) ); ?> - + get( 'id' ) ); + endif; + ?> get( 'id' ) ) ) : ?>
get( 'id' ) ); ?>
diff --git a/templates/myaccount/dashboard.php b/templates/myaccount/dashboard.php index 8cd85c0f6e..9674094234 100644 --- a/templates/myaccount/dashboard.php +++ b/templates/myaccount/dashboard.php @@ -4,8 +4,9 @@ * * @package LifterLMS/Templates * - * @since 1.0.0 - * @version [version] + * @since 1.0.0 + * @since [version] Hooked my_favorites function. + * @version [version] */ defined( 'ABSPATH' ) || exit; diff --git a/tests/phpunit/unit-tests/class-llms-test-query.php b/tests/phpunit/unit-tests/class-llms-test-query.php index 09463d96e1..a592986fcb 100644 --- a/tests/phpunit/unit-tests/class-llms-test-query.php +++ b/tests/phpunit/unit-tests/class-llms-test-query.php @@ -81,7 +81,7 @@ public function test_add_endpoints() { 'view-memberships' => 'lifterlms_myaccount_memberships_endpoint', 'view-achievements' => 'lifterlms_myaccount_achievements_endpoint', 'view-certificates' => 'lifterlms_myaccount_certificates_endpoint', - 'view-favorites' => 'lifterlms_myaccount_favorites_endpoint', + 'view-favorites' => 'lifterlms_myaccount_favorites_endpoint', 'notifications' => 'lifterlms_myaccount_notifications_endpoint', 'edit-account' => 'lifterlms_myaccount_edit_account_endpoint', 'redeem-voucher' => 'lifterlms_myaccount_redeem_vouchers_endpoint', @@ -94,7 +94,7 @@ public function test_add_endpoints() { 'view-memberships' => 'ਵੇਖੋ-ਸਦੱਸਤਾ', // Punjabi. 'view-achievements' => 'nailiyyətlər', // Azerbaijani. 'view-certificates' => 'ເບິ່ງໃບຢັ້ງຢືນ', // Lao. - 'view-favorites' => '즐겨찾기', // Korean. + 'view-favorites' => '즐겨찾기', // Korean. 'notifications' => '通知', // Chinese (Simplified). 'edit-account' => 'חשבון-עריכה', // Hebrew. 'redeem-voucher' => 'چھڑانا', // Urdu. From 63fada045d71d8e7587ec598bdc8037881957da7 Mon Sep 17 00:00:00 2001 From: Akash <18614782+actuallyakash@users.noreply.github.com> Date: Fri, 28 Jul 2023 13:40:20 +0530 Subject: [PATCH 046/125] Apply suggestions from code review Co-authored-by: Rocco Aliberti --- assets/js/llms-favorites.js | 16 ++++++++-------- .../llms.functions.templates.dashboard.php | 6 +++--- .../class.llms.shortcode.favorites.php | 2 +- templates/course/favorite.php | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/assets/js/llms-favorites.js b/assets/js/llms-favorites.js index ce503376fe..9018ed80a1 100644 --- a/assets/js/llms-favorites.js +++ b/assets/js/llms-favorites.js @@ -15,16 +15,16 @@ /** * Main Favorite Container Element. * - * @type obj + * @type {Object} */ $container: null, /** * Bind DOM events. * - * @return void - * @since [version] - * @version [version] + * @since [version] + * + * @return {Void} */ bind: function() { @@ -45,10 +45,10 @@ /** * Favorite / Unfavorite an object. * - * @param obj $btn jQuery object for the "Favorite / Unfavorite" button. - * @return void - * @since [version] - * @version [version] + * @since [version] + * + * @param {Object} $btn jQuery object for the "Favorite / Unfavorite" button. + * @return {Void} */ favorite: function( $btn ) { diff --git a/includes/functions/llms.functions.templates.dashboard.php b/includes/functions/llms.functions.templates.dashboard.php index 5fc0edb9a6..65cf85e4ad 100644 --- a/includes/functions/llms.functions.templates.dashboard.php +++ b/includes/functions/llms.functions.templates.dashboard.php @@ -310,7 +310,7 @@ function llms_template_my_favorites_loop( $student = null, $favorites = null ) { if ( ! $favorites ) { - printf( '

%s

', __( 'No favorites found.', 'lifterlms' ) ); + printf( '

%s

', esc_html__( 'No favorites found.', 'lifterlms' ) ); } else { @@ -626,7 +626,7 @@ function lifterlms_template_student_dashboard_my_favorites( $preview = false ) { if ( $preview && LLMS_Student_Dashboard::is_endpoint_enabled( 'view-favorites' ) ) { $more = array( 'url' => llms_get_endpoint_url( 'view-favorites', '', llms_get_page_url( 'myaccount' ) ), - 'text' => __( 'View All My Favorites', 'lifterlms' ), + 'text' => esc_html__( 'View All My Favorites', 'lifterlms' ), ); } @@ -638,7 +638,7 @@ function lifterlms_template_student_dashboard_my_favorites( $preview = false ) { array( 'action' => 'my_favorites', 'slug' => 'llms-my-favorites', - 'title' => $preview ? __( 'My Favorites', 'lifterlms' ) : '', + 'title' => $preview ? esc_html__( 'My Favorites', 'lifterlms' ) : '', 'content' => ob_get_clean(), 'more' => $more, ) diff --git a/includes/shortcodes/class.llms.shortcode.favorites.php b/includes/shortcodes/class.llms.shortcode.favorites.php index 11399734a4..f382cc8456 100644 --- a/includes/shortcodes/class.llms.shortcode.favorites.php +++ b/includes/shortcodes/class.llms.shortcode.favorites.php @@ -82,7 +82,7 @@ protected function get_output() { if ( ! llms_get_student() ) { printf( - __( 'You must be logged in to view this information. Click %1$shere%2$s to login.', 'lifterlms' ), + esc_html__( 'You must be logged in to view this information. Click %1$shere%2$s to login.', 'lifterlms' ), '', '' ); diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 2d6b6d4b83..2ca3ade996 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -15,7 +15,7 @@ global $post; -$lesson = new LLMS_Lesson( ( null === $object_id || '' === $object_id ) ? $post->ID : $object_id ); +$lesson = new LLMS_Lesson( empty( $object_id ) ? $post->ID : $object_id ); $student = llms_get_student( get_current_user_id() ); $total_favorites = llms_get_object_total_favorites( $lesson->get( 'id' ) ); ?> From 0ba96ba2ca07403d1869bd38b5fe2821a1e877ed Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Fri, 28 Jul 2023 13:46:28 +0530 Subject: [PATCH 047/125] minor fixes --- assets/js/llms-favorites.js | 3 +-- includes/class.llms.student.dashboard.php | 2 +- includes/functions/llms.functions.templates.dashboard.php | 4 ++-- includes/llms.template.hooks.php | 2 +- templates/myaccount/dashboard.php | 2 +- 5 files changed, 6 insertions(+), 7 deletions(-) diff --git a/assets/js/llms-favorites.js b/assets/js/llms-favorites.js index 9018ed80a1..3b200c3b10 100644 --- a/assets/js/llms-favorites.js +++ b/assets/js/llms-favorites.js @@ -1,4 +1,4 @@ -;/* global LLMS, $ */ +/* global LLMS, $ */ /* jshint strict: true */ /** @@ -85,7 +85,6 @@ // Updating count. self.$container.find( '.llms-favorites-count' ).text( r.total_favorites ); - } diff --git a/includes/class.llms.student.dashboard.php b/includes/class.llms.student.dashboard.php index d3f5a4f597..2a135ed16b 100644 --- a/includes/class.llms.student.dashboard.php +++ b/includes/class.llms.student.dashboard.php @@ -191,7 +191,7 @@ public static function get_tabs() { 'title' => __( 'My Certificates', 'lifterlms' ), ), 'view-favorites' => array( - 'content' => 'lifterlms_template_student_dashboard_my_favorites', + 'content' => 'llms_template_student_dashboard_my_favorites', 'endpoint' => llms_is_favorites_enabled() ? get_option( 'lifterlms_myaccount_favorites_endpoint', 'view-favorites' ) : '', 'paginate' => true, 'nav_item' => true, diff --git a/includes/functions/llms.functions.templates.dashboard.php b/includes/functions/llms.functions.templates.dashboard.php index 65cf85e4ad..9990e7c794 100644 --- a/includes/functions/llms.functions.templates.dashboard.php +++ b/includes/functions/llms.functions.templates.dashboard.php @@ -604,7 +604,7 @@ function lifterlms_template_student_dashboard_my_courses( $preview = false ) { } } -if ( ! function_exists( 'lifterlms_template_student_dashboard_my_favorites' ) ) { +if ( ! function_exists( 'llms_template_student_dashboard_my_favorites' ) ) { /** * Template for My Favorites section on dashboard index. @@ -614,7 +614,7 @@ function lifterlms_template_student_dashboard_my_courses( $preview = false ) { * @param bool $preview Optional. If true, outputs a short list of favorites. Default `false`. * @return void */ - function lifterlms_template_student_dashboard_my_favorites( $preview = false ) { + function llms_template_student_dashboard_my_favorites( $preview = false ) { $student = llms_get_student(); diff --git a/includes/llms.template.hooks.php b/includes/llms.template.hooks.php index a9c43a9490..fd5af4c2bf 100644 --- a/includes/llms.template.hooks.php +++ b/includes/llms.template.hooks.php @@ -169,7 +169,7 @@ add_action( 'llms_achievement_content', 'llms_the_achievement', 10 ); add_action( 'llms_certificate_preview', 'llms_the_certificate_preview', 10 ); add_action( 'lifterlms_student_dashboard_index', 'lifterlms_template_student_dashboard_my_memberships', 40 ); -add_action( 'lifterlms_student_dashboard_index', 'lifterlms_template_student_dashboard_my_favorites', 50 ); +add_action( 'lifterlms_student_dashboard_index', 'llms_template_student_dashboard_my_favorites', 50 ); add_action( 'llms_my_grades_course_table', 'lifterlms_template_student_dashboard_my_grades_table', 10, 2 ); diff --git a/templates/myaccount/dashboard.php b/templates/myaccount/dashboard.php index 9674094234..0a282174ce 100644 --- a/templates/myaccount/dashboard.php +++ b/templates/myaccount/dashboard.php @@ -27,7 +27,7 @@ * @hooked lifterlms_template_student_dashboard_my_achievements - 20 * @hooked lifterlms_template_student_dashboard_my_certificates - 30 * @hooked lifterlms_template_student_dashboard_my_memberships - 40 - * @hooked lifterlms_template_student_dashboard_my_favorites - 50 + * @hooked llms_template_student_dashboard_my_favorites - 50 */ do_action( 'lifterlms_student_dashboard_index', true ); From f596c5dda80c02c1f29f1f187f76555741ea2f25 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Fri, 28 Jul 2023 14:02:24 +0530 Subject: [PATCH 048/125] minor cs fixes --- includes/models/model.llms.student.php | 2 +- templates/course/favorite.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index b3cc840f19..0ea7890fa4 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1309,7 +1309,7 @@ private function insert_favorite_postmeta( $object_id ) { * * @since [version] * - * @param int $object_id WP Post ID of the lesson + * @param int $object_id WP Post ID of the lesson * @return bool */ private function remove_unfavorite_postmeta( $object_id ) { diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 2ca3ade996..7bd88b96f1 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -15,7 +15,7 @@ global $post; -$lesson = new LLMS_Lesson( empty( $object_id ) ? $post->ID : $object_id ); +$lesson = new LLMS_Lesson( empty( $object_id ) ? $post->ID : $object_id ); $student = llms_get_student( get_current_user_id() ); $total_favorites = llms_get_object_total_favorites( $lesson->get( 'id' ) ); ?> From ef2b482ae886c2f2f00166e9fd0cbfa77ba001d8 Mon Sep 17 00:00:00 2001 From: Akash <18614782+actuallyakash@users.noreply.github.com> Date: Fri, 28 Jul 2023 18:06:09 +0530 Subject: [PATCH 049/125] Apply suggestions from code review Co-authored-by: Rocco Aliberti --- assets/js/llms-favorites.js | 9 +++++---- includes/class.llms.query.user.postmeta.php | 4 +--- includes/functions/llms.functions.favorite.php | 2 +- includes/models/model.llms.student.php | 4 ++-- templates/course/favorite.php | 5 ++--- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/assets/js/llms-favorites.js b/assets/js/llms-favorites.js index 3b200c3b10..548292eb2e 100644 --- a/assets/js/llms-favorites.js +++ b/assets/js/llms-favorites.js @@ -4,8 +4,9 @@ /** * Front End Favorite Class. * - * @type {Object} - * @since [version] + * @type {Object} + * + * @since [version] * @version [version] */ ( function( $ ) { @@ -30,14 +31,14 @@ var self = this; - // Favorite clicked + // Favorite clicked. $( '.llms-favorite-wrapper' ).on( 'click', function( e ) { e.preventDefault(); let $btn = $( this ).find( '.llms-heart-btn' ); self.favorite( $btn ); } ); - // Adding class in Favorite's parent + // Adding class in Favorite's parent. $( '.llms-favorite-wrapper' ).parent().addClass( 'llms-has-favorite' ); }, diff --git a/includes/class.llms.query.user.postmeta.php b/includes/class.llms.query.user.postmeta.php index b4cc0759bf..84acc8c2ca 100644 --- a/includes/class.llms.query.user.postmeta.php +++ b/includes/class.llms.query.user.postmeta.php @@ -14,7 +14,6 @@ * LifterLMS User Postmeta Query * * @since 3.15.0 - * @version [version] */ class LLMS_Query_User_Postmeta extends LLMS_Database_Query { @@ -87,8 +86,7 @@ public function get_metas() { * * @return void * @since 3.15.0 - * @since [version] Added 'Favorites' event. - * @version [version] + * @since [version] Added 'Favorites' event. */ protected function parse_args() { diff --git a/includes/functions/llms.functions.favorite.php b/includes/functions/llms.functions.favorite.php index e25b47bdbc..19c965f94c 100644 --- a/includes/functions/llms.functions.favorite.php +++ b/includes/functions/llms.functions.favorite.php @@ -15,7 +15,7 @@ * * @since [version] * - * @param int $object_id WP Post ID of the Lesson. + * @param bool|int $object_id WP Post ID of the Lesson. If not supplied it will default to the current post ID. * @return int */ function llms_get_object_total_favorites( $object_id = false ) { diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 0ea7890fa4..1e649de968 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -273,7 +273,7 @@ public function get_courses( $args = array() ) { * @param string $order_by Result set ordering field. Default "date". * @param string $order Result set order. Default "DESC". Accepts "DESC" or "ASC". * @param int $limit Number of favorites to return. - * @return array + * @return bool|array */ public function get_favorites( $order_by = '', $order = '', $limit = '' ) { @@ -1309,7 +1309,7 @@ private function insert_favorite_postmeta( $object_id ) { * * @since [version] * - * @param int $object_id WP Post ID of the lesson + * @param int $object_id WP Post ID of the lesson. * @return bool */ private function remove_unfavorite_postmeta( $object_id ) { diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 7bd88b96f1..4d15eb7cfa 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -1,5 +1,4 @@ Date: Fri, 28 Jul 2023 18:15:33 +0530 Subject: [PATCH 050/125] minor fixes --- includes/class.llms.ajax.php | 1 - includes/functions/llms.functions.person.php | 10 +++++----- .../llms.functions.templates.dashboard.php | 6 +++--- includes/llms.template.functions.php | 18 +++++++++--------- includes/models/model.llms.student.php | 2 +- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/includes/class.llms.ajax.php b/includes/class.llms.ajax.php index 1be5de105c..692c6049a3 100644 --- a/includes/class.llms.ajax.php +++ b/includes/class.llms.ajax.php @@ -20,7 +20,6 @@ * - `LLMS_AJAX::check_voucher_duplicate()` method. * - `LLMS_AJAX::get_ajax_data()` method. * - `LLMS_AJAX::register_script()` method. - * @since [version] Added method `favorite_object` to Add Favorite / Unfavorite Postmeta for an object. */ class LLMS_AJAX { diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index 44a8c6e01f..dc68e2f3c5 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -324,9 +324,9 @@ function llms_is_user_enrolled( $user_id, $product_id, $relation = 'all', $use_c * @see LLMS_Student->mark_complete() * * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. - * @param string $trigger String describing the event that triggered marking the object as complete. + * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @param string $object_type Object type [lesson|section|course|track]. + * @param string $trigger String describing the event that triggered marking the object as incomplete. * @return boolean */ function llms_mark_complete( $user_id, $object_id, $object_type, $trigger = 'unspecified' ) { @@ -342,8 +342,8 @@ function llms_mark_complete( $user_id, $object_id, $object_type, $trigger = 'uns * @see LLMS_Student->mark_incomplete() * * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. + * @param string $object_type Object type [lesson|section|course|track]. * @param string $trigger String describing the event that triggered marking the object as incomplete. * @return boolean */ diff --git a/includes/functions/llms.functions.templates.dashboard.php b/includes/functions/llms.functions.templates.dashboard.php index 9990e7c794..3c6301ae2d 100644 --- a/includes/functions/llms.functions.templates.dashboard.php +++ b/includes/functions/llms.functions.templates.dashboard.php @@ -153,7 +153,7 @@ function lifterlms_student_dashboard( $options = array() ) { } -if ( ! function_exists( 'llms_template_my_courses_loop' ) ) { +if ( ! function_exists( 'lifterlms_template_my_courses_loop' ) ) { /** * Get course tiles for a student's courses @@ -168,7 +168,7 @@ function lifterlms_student_dashboard( $options = array() ) { * @param bool $preview Optional. If true, outputs a short list of courses (based on dashboard_recent_courses filter). Default `false`. * @return void */ - function llms_template_my_courses_loop( $student = null, $preview = false ) { + function lifterlms_template_my_courses_loop( $student = null, $preview = false ) { $student = llms_get_student( $student ); if ( ! $student ) { @@ -588,7 +588,7 @@ function lifterlms_template_student_dashboard_my_courses( $preview = false ) { } ob_start(); - llms_template_my_courses_loop( $student, $preview ); + lifterlms_template_my_courses_loop( $student, $preview ); llms_get_template( 'myaccount/dashboard-section.php', diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index 508e11216b..8e759e158f 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -385,17 +385,17 @@ function lifterlms_template_single_parent_course() { } } -/** - * Favorite Lesson Template Include. - * - * @since [version] - * - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. - * @return void - */ if ( ! function_exists( 'llms_template_favorite' ) ) { + /** + * Favorite Lesson Template Include. + * + * @since [version] + * + * @param int $object_id WP Post ID of the Lesson. + * @param string $object_type Type, 'Lesson'. + * @return void + */ function llms_template_favorite( $object_id = null, $object_type = 'lesson' ) { if ( llms_is_favorites_enabled() ) { diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 1e649de968..95eb20de29 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1292,7 +1292,7 @@ private function insert_incompletion_postmeta( $object_id, $trigger = 'unspecifi * * @see LLMS_Student->mark_favorite() * - * @param int $object_id WP User ID. + * @param int $object_id WP Post ID of the lesson. * @return bool */ private function insert_favorite_postmeta( $object_id ) { From ceffca68c75981060e43cca72baeb9c2f1d7f9a6 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Fri, 28 Jul 2023 20:51:04 +0530 Subject: [PATCH 051/125] fix failing tests --- includes/class.llms.student.dashboard.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class.llms.student.dashboard.php b/includes/class.llms.student.dashboard.php index 2a135ed16b..395be15b5a 100644 --- a/includes/class.llms.student.dashboard.php +++ b/includes/class.llms.student.dashboard.php @@ -192,7 +192,7 @@ public static function get_tabs() { ), 'view-favorites' => array( 'content' => 'llms_template_student_dashboard_my_favorites', - 'endpoint' => llms_is_favorites_enabled() ? get_option( 'lifterlms_myaccount_favorites_endpoint', 'view-favorites' ) : '', + 'endpoint' => get_option( 'lifterlms_myaccount_favorites_endpoint', 'view-favorites' ), 'paginate' => true, 'nav_item' => true, 'title' => __( 'My Favorites', 'lifterlms' ), From e282976a3ee9f043493f1d3fe6d26a61045ca5b2 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 31 Jul 2023 12:33:57 +0530 Subject: [PATCH 052/125] updated docblock comments --- includes/functions/llms.functions.person.php | 8 ++--- includes/llms.template.functions.php | 4 +-- includes/models/model.llms.student.php | 32 ++++++++++---------- templates/course/favorite.php | 2 +- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index dc68e2f3c5..4ddfa85c00 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -360,8 +360,8 @@ function llms_mark_incomplete( $user_id, $object_id, $object_type, $trigger = 'u * @see LLMS_Student->mark_favorite() * * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @return boolean */ function llms_mark_favorite( $user_id, $object_id, $object_type ) { @@ -377,8 +377,8 @@ function llms_mark_favorite( $user_id, $object_id, $object_type ) { * @see LLMS_Student->mark_unfavorite() * * @param int $user_id WP User ID. - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @return boolean */ function llms_mark_unfavorite( $user_id, $object_id, $object_type ) { diff --git a/includes/llms.template.functions.php b/includes/llms.template.functions.php index 8e759e158f..f85b4c12f5 100644 --- a/includes/llms.template.functions.php +++ b/includes/llms.template.functions.php @@ -392,8 +392,8 @@ function lifterlms_template_single_parent_course() { * * @since [version] * - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @return void */ function llms_template_favorite( $object_id = null, $object_type = 'lesson' ) { diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index 95eb20de29..edc639d779 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1292,7 +1292,7 @@ private function insert_incompletion_postmeta( $object_id, $trigger = 'unspecifi * * @see LLMS_Student->mark_favorite() * - * @param int $object_id WP Post ID of the lesson. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. * @return bool */ private function insert_favorite_postmeta( $object_id ) { @@ -1309,7 +1309,7 @@ private function insert_favorite_postmeta( $object_id ) { * * @since [version] * - * @param int $object_id WP Post ID of the lesson. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. * @return bool */ private function remove_unfavorite_postmeta( $object_id ) { @@ -1914,8 +1914,8 @@ private function update_completion_status( $status, $object_id, $object_type, $t * * @since [version] * - * @param int $object_id WP Post ID of a lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @return bool */ public function is_favorite( $object_id, $object_type = 'lesson' ) { @@ -1940,8 +1940,8 @@ public function is_favorite( $object_id, $object_type = 'lesson' ) { * @since [version] * * @param array|false $ret Array of favorite data or `false` if no favorite is found. - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @param LLMS_Student $instance The Student Instance */ return apply_filters( 'llms_is_' . $object_type . '_favorite', $ret, $object_id, $object_type, $this ); @@ -1955,8 +1955,8 @@ public function is_favorite( $object_id, $object_type = 'lesson' ) { * * @see llms_mark_favorite() calls this function without having to instantiate the LLMS_Student class first. * - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @return bool */ public function mark_favorite( $object_id, $object_type ) { @@ -1977,8 +1977,8 @@ public function mark_favorite( $object_id, $object_type ) { * * @see llms_mark_unfavorite() calls this function without having to instantiate the LLMS_Student class first. * - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @return bool */ public function mark_unfavorite( $object_id, $object_type ) { @@ -2001,8 +2001,8 @@ public function mark_unfavorite( $object_id, $object_type ) { * @since [version] Use filterable functions to determine if the object can be marked favorite. * * @param string $status New status to update to, either "favorite" or "unfavorite". - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @param string $trigger String describing the reason for the status change. * @return bool */ @@ -2019,8 +2019,8 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * @since [version] * * @param int $student_id WP_User ID of the student. - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @param string $trigger String describing the reason for the status change. */ do_action( "before_llms_mark_{$status}", $student_id, $object_id, $object_type, $trigger ); @@ -2041,8 +2041,8 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * @since [version] * * @param int $student_id WP_User ID of the student. - * @param int $object_id WP Post ID of the Lesson. - * @param string $object_type Type, 'Lesson'. + * @param int $object_id WP Post ID of the object to mark/unmark as favorite. + * @param string $object_type The object type, currently only 'lesson'. * @param string $trigger String describing the reason for the status change. */ do_action( "llms_mark_{$status}", $student_id, $object_id, $object_type, $trigger ); diff --git a/templates/course/favorite.php b/templates/course/favorite.php index 4d15eb7cfa..176bca85db 100644 --- a/templates/course/favorite.php +++ b/templates/course/favorite.php @@ -8,7 +8,7 @@ * @since [version] * * @param int $object_id WP Post ID of the object to mark/unmark as favorite. - * @param string $object_type The object type. Currently only the 'lesson' type is possible. + * @param string $object_type The object type, currently only 'lesson'. */ defined( 'ABSPATH' ) || exit; From 65238b5d42495e337a5d921f595adf39d6299549 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 31 Jul 2023 14:07:26 +0530 Subject: [PATCH 053/125] removed $trigger --- includes/models/model.llms.student.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/includes/models/model.llms.student.php b/includes/models/model.llms.student.php index edc639d779..da0557287e 100644 --- a/includes/models/model.llms.student.php +++ b/includes/models/model.llms.student.php @@ -1312,7 +1312,7 @@ private function insert_favorite_postmeta( $object_id ) { * @param int $object_id WP Post ID of the object to mark/unmark as favorite. * @return bool */ - private function remove_unfavorite_postmeta( $object_id ) { + private function remove_favorite_postmeta( $object_id ) { $update = llms_delete_user_postmeta( $this->get_id(), $object_id, '_favorite', true ); @@ -2003,10 +2003,9 @@ public function mark_unfavorite( $object_id, $object_type ) { * @param string $status New status to update to, either "favorite" or "unfavorite". * @param int $object_id WP Post ID of the object to mark/unmark as favorite. * @param string $object_type The object type, currently only 'lesson'. - * @param string $trigger String describing the reason for the status change. * @return bool */ - private function update_favorite_status( $status, $object_id, $object_type, $trigger = 'unspecified' ) { + private function update_favorite_status( $status, $object_id, $object_type ) { $student_id = $this->get_id(); @@ -2021,15 +2020,14 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * @param int $student_id WP_User ID of the student. * @param int $object_id WP Post ID of the object to mark/unmark as favorite. * @param string $object_type The object type, currently only 'lesson'. - * @param string $trigger String describing the reason for the status change. */ - do_action( "before_llms_mark_{$status}", $student_id, $object_id, $object_type, $trigger ); + do_action( "before_llms_mark_{$status}", $student_id, $object_id, $object_type ); // Insert / Remove meta data. if ( 'favorite' === $status ) { - $this->insert_favorite_postmeta( $object_id, $trigger ); + $this->insert_favorite_postmeta( $object_id ); } elseif ( 'unfavorite' === $status ) { - $this->remove_unfavorite_postmeta( $object_id, $trigger ); + $this->remove_favorite_postmeta( $object_id ); } /** @@ -2043,9 +2041,8 @@ private function update_favorite_status( $status, $object_id, $object_type, $tri * @param int $student_id WP_User ID of the student. * @param int $object_id WP Post ID of the object to mark/unmark as favorite. * @param string $object_type The object type, currently only 'lesson'. - * @param string $trigger String describing the reason for the status change. */ - do_action( "llms_mark_{$status}", $student_id, $object_id, $object_type, $trigger ); + do_action( "llms_mark_{$status}", $student_id, $object_id, $object_type ); /** * Hook that fires when a student's favorite status is updated for a specific object type. From 560dd80d64e5ad6380a4b975ec36935b9a638e34 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 31 Jul 2023 16:40:32 +0530 Subject: [PATCH 054/125] updated heart color and favorites shortcode design --- assets/scss/frontend/_main.scss | 2 +- .../llms.functions.templates.dashboard.php | 23 ++++++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/assets/scss/frontend/_main.scss b/assets/scss/frontend/_main.scss index 804b905812..c956ae043c 100644 --- a/assets/scss/frontend/_main.scss +++ b/assets/scss/frontend/_main.scss @@ -492,7 +492,7 @@ svg .llms-animated-circle { cursor: pointer; .fa-heart { - color: #FF2E2B; + color: #EF476F; } } diff --git a/includes/functions/llms.functions.templates.dashboard.php b/includes/functions/llms.functions.templates.dashboard.php index 3c6301ae2d..b6cb750e7e 100644 --- a/includes/functions/llms.functions.templates.dashboard.php +++ b/includes/functions/llms.functions.templates.dashboard.php @@ -330,37 +330,34 @@ function ( $carry, $item ) { array() ); + echo '
'; + // Printing Favorite Lessons under each Parent Course. foreach ( $favorites as $course => $lessons ) { // Get Course Name. - $lesson = new LLMS_Lesson( $course ); + $course = new LLMS_Course( $course ); - llms_get_template( - 'course/lesson-preview.php', - array( - 'lesson' => $lesson, - ) - ); + echo '

'; + echo $course->get( 'title' ); + echo '

'; - echo '
    '; foreach ( $lessons as $lesson ) { $lesson = new LLMS_Lesson( $lesson->post_id ); - echo '
  • '; + llms_get_template( 'course/lesson-preview.php', array( 'lesson' => $lesson, ) ); - echo '
  • '; - } - echo '
'; + } } - } + echo '
'; + } } } From c6c8a1024b879859d6b6ca9f43653a248a68cb92 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Mon, 31 Jul 2023 19:19:07 +0530 Subject: [PATCH 055/125] fixed typo due to copy/paste --- includes/functions/llms.functions.person.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/functions/llms.functions.person.php b/includes/functions/llms.functions.person.php index 4ddfa85c00..faa776281b 100644 --- a/includes/functions/llms.functions.person.php +++ b/includes/functions/llms.functions.person.php @@ -326,7 +326,7 @@ function llms_is_user_enrolled( $user_id, $product_id, $relation = 'all', $use_c * @param int $user_id WP User ID. * @param int $object_id WP Post ID of the Lesson, Section, Track, or Course. * @param string $object_type Object type [lesson|section|course|track]. - * @param string $trigger String describing the event that triggered marking the object as incomplete. + * @param string $trigger String describing the event that triggered marking the object as complete. * @return boolean */ function llms_mark_complete( $user_id, $object_id, $object_type, $trigger = 'unspecified' ) { From 35ca2364fb1ab114e39dc5b657aa59f9831888f3 Mon Sep 17 00:00:00 2001 From: actuallyakash Date: Tue, 1 Aug 2023 12:39:27 +0530 Subject: [PATCH 056/125] show favorites endpoint only when enabled --- includes/class.llms.student.dashboard.php | 166 ++++++++++++---------- 1 file changed, 88 insertions(+), 78 deletions(-) diff --git a/includes/class.llms.student.dashboard.php b/includes/class.llms.student.dashboard.php index 395be15b5a..ebc8ae9ab5 100644 --- a/includes/class.llms.student.dashboard.php +++ b/includes/class.llms.student.dashboard.php @@ -146,89 +146,99 @@ public static function get_current_tab( $return = 'data' ) { */ public static function get_tabs() { - return apply_filters( - 'llms_get_student_dashboard_tabs', - array( - 'dashboard' => array( - 'content' => 'lifterlms_template_student_dashboard_home', - 'endpoint' => false, - 'nav_item' => true, - 'title' => __( 'Dashboard', 'lifterlms' ), - 'url' => llms_get_page_url( 'myaccount' ), - ), - 'view-courses' => array( - 'content' => 'lifterlms_template_student_dashboard_my_courses', - 'endpoint' => get_option( 'lifterlms_myaccount_courses_endpoint', 'view-courses' ), - 'paginate' => true, - 'nav_item' => true, - 'title' => __( 'My Courses', 'lifterlms' ), - ), - 'my-grades' => array( - 'content' => 'lifterlms_template_student_dashboard_my_grades', - 'endpoint' => get_option( 'lifterlms_myaccount_grades_endpoint', 'my-grades' ), - 'paginate' => true, - 'nav_item' => true, - 'title' => __( 'My Grades', 'lifterlms' ), - ), - 'view-memberships' => array( - 'content' => 'lifterlms_template_student_dashboard_my_memberships', - 'endpoint' => get_option( 'lifterlms_myaccount_memberships_endpoint', 'view-memberships' ), - 'nav_item' => true, - 'title' => __( 'My Memberships', 'lifterlms' ), - ), - 'view-achievements' => array( - 'content' => 'lifterlms_template_student_dashboard_my_achievements', - 'endpoint' => get_option( 'lifterlms_myaccount_achievements_endpoint', 'view-achievements' ), - 'paginate' => true, - 'nav_item' => true, - 'title' => __( 'My Achievements', 'lifterlms' ), - ), - 'view-certificates' => array( - 'content' => 'lifterlms_template_student_dashboard_my_certificates', - 'endpoint' => get_option( 'lifterlms_myaccount_certificates_endpoint', 'view-certificates' ), - 'paginate' => true, - 'nav_item' => true, - 'title' => __( 'My Certificates', 'lifterlms' ), - ), - 'view-favorites' => array( + $tabs = array( + 'dashboard' => array( + 'content' => 'lifterlms_template_student_dashboard_home', + 'endpoint' => false, + 'nav_item' => true, + 'title' => __( 'Dashboard', 'lifterlms' ), + 'url' => llms_get_page_url( 'myaccount' ), + ), + 'view-courses' => array( + 'content' => 'lifterlms_template_student_dashboard_my_courses', + 'endpoint' => get_option( 'lifterlms_myaccount_courses_endpoint', 'view-courses' ), + 'paginate' => true, + 'nav_item' => true, + 'title' => __( 'My Courses', 'lifterlms' ), + ), + 'my-grades' => array( + 'content' => 'lifterlms_template_student_dashboard_my_grades', + 'endpoint' => get_option( 'lifterlms_myaccount_grades_endpoint', 'my-grades' ), + 'paginate' => true, + 'nav_item' => true, + 'title' => __( 'My Grades', 'lifterlms' ), + ), + 'view-memberships' => array( + 'content' => 'lifterlms_template_student_dashboard_my_memberships', + 'endpoint' => get_option( 'lifterlms_myaccount_memberships_endpoint', 'view-memberships' ), + 'nav_item' => true, + 'title' => __( 'My Memberships', 'lifterlms' ), + ), + 'view-achievements' => array( + 'content' => 'lifterlms_template_student_dashboard_my_achievements', + 'endpoint' => get_option( 'lifterlms_myaccount_achievements_endpoint', 'view-achievements' ), + 'paginate' => true, + 'nav_item' => true, + 'title' => __( 'My Achievements', 'lifterlms' ), + ), + 'view-certificates' => array( + 'content' => 'lifterlms_template_student_dashboard_my_certificates', + 'endpoint' => get_option( 'lifterlms_myaccount_certificates_endpoint', 'view-certificates' ), + 'paginate' => true, + 'nav_item' => true, + 'title' => __( 'My Certificates', 'lifterlms' ), + ), + 'notifications' => array( + 'content' => 'lifterlms_template_student_dashboard_my_notifications', + 'endpoint' => get_option( 'lifterlms_myaccount_notifications_endpoint', 'notifications' ), + 'paginate' => true, + 'nav_item' => true, + 'title' => __( 'Notifications', 'lifterlms' ), + ), + 'edit-account' => array( + 'content' => array( __CLASS__, 'output_edit_account_content' ), + 'endpoint' => get_option( 'lifterlms_myaccount_edit_account_endpoint', 'edit-account' ), + 'nav_item' => true, + 'title' => __( 'Edit Account', 'lifterlms' ), + ), + 'redeem-voucher' => array( + 'content' => array( __CLASS__, 'output_redeem_voucher_content' ), + 'endpoint' => get_option( 'lifterlms_myaccount_redeem_vouchers_endpoint', 'redeem-voucher' ), + 'nav_item' => true, + 'title' => __( 'Redeem a Voucher', 'lifterlms' ), + ), + 'orders' => array( + 'content' => array( __CLASS__, 'output_orders_content' ), + 'endpoint' => get_option( 'lifterlms_myaccount_orders_endpoint', 'orders' ), + 'nav_item' => true, + 'title' => __( 'Order History', 'lifterlms' ), + ), + 'signout' => array( + 'endpoint' => false, + 'title' => __( 'Sign Out', 'lifterlms' ), + 'nav_item' => false, + 'url' => wp_logout_url( llms_get_page_url( 'myaccount' ) ), + ), + ); + + if ( llms_is_favorites_enabled() ) { + $tabs = llms_assoc_array_insert( + $tabs, + 'view-certificates', + 'view-favorites', + array( 'content' => 'llms_template_student_dashboard_my_favorites', 'endpoint' => get_option( 'lifterlms_myaccount_favorites_endpoint', 'view-favorites' ), 'paginate' => true, 'nav_item' => true, 'title' => __( 'My Favorites', 'lifterlms' ), - ), - 'notifications' => array( - 'content' => 'lifterlms_template_student_dashboard_my_notifications', - 'endpoint' => get_option( 'lifterlms_myaccount_notifications_endpoint', 'notifications' ), - 'paginate' => true, - 'nav_item' => true, - 'title' => __( 'Notifications', 'lifterlms' ), - ), - 'edit-account' => array( - 'content' => array( __CLASS__, 'output_edit_account_content' ), - 'endpoint' => get_option( 'lifterlms_myaccount_edit_account_endpoint', 'edit-account' ), - 'nav_item' => true, - 'title' => __( 'Edit Account', 'lifterlms' ), - ), - 'redeem-voucher' => array( - 'content' => array( __CLASS__, 'output_redeem_voucher_content' ), - 'endpoint' => get_option( 'lifterlms_myaccount_redeem_vouchers_endpoint', 'redeem-voucher' ), - 'nav_item' => true, - 'title' => __( 'Redeem a Voucher', 'lifterlms' ), - ), - 'orders' => array( - 'content' => array( __CLASS__, 'output_orders_content' ), - 'endpoint' => get_option( 'lifterlms_myaccount_orders_endpoint', 'orders' ), - 'nav_item' => true, - 'title' => __( 'Order History', 'lifterlms' ), - ), - 'signout' => array( - 'endpoint' => false, - 'title' => __( 'Sign Out', 'lifterlms' ), - 'nav_item' => false, - 'url' => wp_logout_url( llms_get_page_url( 'myaccount' ) ), - ), - ) + ) + ); + } + + return apply_filters( + 'llms_get_student_dashboard_tabs', + $tabs ); } From eeed27eb19a04686d3dddf1bd9fb7c61996a8753 Mon Sep 17 00:00:00 2001 From: Rocco Aliberti Date: Sun, 16 Jul 2023 14:04:26 +0200 Subject: [PATCH 057/125] Handle access plan not processable by gateways --- .../abstract.llms.payment.gateway.php | 29 +++++++++++++- includes/functions/llms.functions.order.php | 12 ++++-- templates/checkout/form-gateways.php | 39 +++++++++++++++---- templates/checkout/form-switch-source.php | 4 +- 4 files changed, 70 insertions(+), 14 deletions(-) diff --git a/includes/abstracts/abstract.llms.payment.gateway.php b/includes/abstracts/abstract.llms.payment.gateway.php index ba6e74ed84..fc14382656 100644 --- a/includes/abstracts/abstract.llms.payment.gateway.php +++ b/includes/abstracts/abstract.llms.payment.gateway.php @@ -5,7 +5,7 @@ * @package LifterLMS/Abstracts/Classes * * @since 3.0.0 - * @version 7.1.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -1033,7 +1033,7 @@ public function retrieve_secure_strings() { } /** - * Determine if a feature is supported by the gateway + * Determine if a feature is supported by the gateway. * * Looks at the $this->supports and ensures the submitted feature exists and is true. * @@ -1056,4 +1056,29 @@ public function supports( $feature, $order = null ) { } + /** + * Determine if an access plan can be processed by the gateway. + * + * @since [version] + * + * @param LLMS_Access_Plan $plan Instance of an LLMS_Access_Plan. + * @param LLMS_Order $order Instance of an LLMS_Order. Used to check whether a payment can be switched using this gateway. + * In that case, in fact, we have to rely on the access plan information contained in the order + * at the moment of its creation. + * @return boolean + */ + public function can_process_access_plan( $plan, $order = null ) { + /** + * Filters whether or not a gateway can process a specific access plan. + * + * @since [version] + * + * @param bool $can_process_plan Whether or not the gateway can process a specific access plan. + * @param LLMS_Access_Plan $plan Access plan object. + * @param LLMS_Order $plan Order object. + * @param string $id The gateway ID. + */ + return apply_filters( 'llms_can_gateway_process_access_plan', $plan || $order, $plan, $order, $this->id ); + } + } diff --git a/includes/functions/llms.functions.order.php b/includes/functions/llms.functions.order.php index 501ee508a9..c9d6afcb3a 100644 --- a/includes/functions/llms.functions.order.php +++ b/includes/functions/llms.functions.order.php @@ -5,7 +5,7 @@ * @package LifterLMS/Functions * * @since 3.29.0 - * @version 7.0.0 + * @version [version] */ defined( 'ABSPATH' ) || exit; @@ -48,6 +48,7 @@ function llms_can_gateway_be_used_for_plan( $gateway_id, $plan ) { * + The gateway must support the order/plan's type (recurring or single). * * @since 7.0.0 + * @since [version] Added check on whether a gateway can process a plan. * * @param string $gateway_id Payment gateway ID. * @param LLMS_Order|LLMS_Access_Plan|int $plan_or_order The `WP_Post` id of a plan or order, a plan object, or an order object. @@ -64,16 +65,21 @@ function llms_can_gateway_be_used_for_plan_or_order( $gateway_id, $plan_or_order $plan_or_order = is_numeric( $plan_or_order ) ? llms_get_post( $plan_or_order ) : $plan_or_order; $err_data = compact( 'gateway_id', 'plan_or_order' ); - if ( ! is_a( $plan_or_order, 'LLMS_Order' ) && ! is_a( $plan_or_order, 'LLMS_Access_Plan' ) ) { + $order = is_a( $plan_or_order, 'LLMS_Order' ) ? $plan_or_order : null; + $plan = ! $order && is_a( $plan_or_order, 'LLMS_Access_Plan' ) ? $plan_or_order : null; + + if ( is_null( $order ) && isset( $plan ) ) { $can_use = new WP_Error( 'post-invalid', __( 'A valid order or access plan must be supplied.', 'lifterlms' ), $err_data ); } else { $gateway = llms()->payment_gateways()->get_gateway_by_id( $gateway_id ); - if ( ! $gateway ) { $can_use = new WP_Error( 'gateway-invalid', __( 'The selected payment gateway is not valid.', 'lifterlms' ), $err_data ); } elseif ( $enabled_only && ! $gateway->is_enabled() ) { $can_use = new WP_Error( 'gateway-disabled', __( 'The selected payment gateway is not available.', 'lifterlms' ), $err_data ); + } elseif ( ! $gateway->can_process_access_plan( $plan, $order ) ) { + // Check whether the gateway can process the plan or the order's plan (which is the plan at the moment of the order's creation). + $can_use = new WP_Error( 'gateway-support-plan', __( 'The selected payment gateway is not available for the given plan.', 'lifterlms' ), $err_data ); } elseif ( $plan_or_order->is_recurring() && ! $gateway->supports( 'recurring_payments' ) ) { $can_use = new WP_Error( 'gateway-support-recurring', __( 'The selected payment gateway does not support recurring payments.', 'lifterlms' ), $err_data ); } elseif ( ! $plan_or_order->is_recurring() && ! $gateway->supports( 'single_payments' ) ) { diff --git a/templates/checkout/form-gateways.php b/templates/checkout/form-gateways.php index 0ff0c600a2..45dc16adbf 100644 --- a/templates/checkout/form-gateways.php +++ b/templates/checkout/form-gateways.php @@ -7,12 +7,14 @@ * @since Unknown * @since 5.0.0 Update form field to utilize "checked" attribute of "selected" and removed superfluous values. * @since 7.0.0 Disable data-source loading for gateway radio fields. - * @version 7.0.0 + * @since [version] Added check on whether a gateway can or cannot process a plan, or an order's plan (source switching). + * @version [version] * - * @param LLMS_Payment_Gateway[] $gateways Array of enabled payment gateway instances. - * @param string $selected_gateway ID of the currently selected/default payment gateway. - * @param LLMS_Coupon|false $coupon Coupon currently applied to the session or `false` when none found. - * @param LLMS_Access_Plan $plan Access plan object. + * @param LLMS_Payment_Gateway[] $gateways Array of enabled payment gateway instances. + * @param string $selected_gateway ID of the currently selected/default payment gateway. + * @param LLMS_Coupon|false $coupon Coupon currently applied to the session or `false` when none found. + * @param LLMS_Access_Plan|null $plan Access plan object. + * @param LLMS_Order|null $order Order object. */ defined( 'ABSPATH' ) || exit; @@ -20,20 +22,41 @@ $show_gateways = true; // Don't display for free plans or plans which do not require any payment. -if ( isset( $plan ) && $plan->is_free() || ! $plan->requires_payment( $coupon ) ) { +if ( isset( $plan ) && ( $plan->is_free() || ! $plan->requires_payment( $coupon ) ) ) { $show_gateways = false; +} elseif ( isset( $plan ) ) { + $supports = $plan->is_recurring() ? 'recurring_payments' : 'single_payments'; +} elseif ( ! isset( $plan ) && isset( $order ) ) { // Switching source. + $supports = $order->is_recurring() ? 'recurring_payments' : 'single_payments'; } -$supports = $plan->is_recurring() ? 'recurring_payments' : 'single_payments'; $supporting_gateways = 0; +$gateways_array = array_values( $gateways ); ?>