From 7625f339f2f86d5c7c7579d48d81f4b6bc1f1898 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Wed, 4 Sep 2024 21:48:42 +0600 Subject: [PATCH 1/7] Add hook to prevent rendering lesson actions --- includes/class-sensei-lesson.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/includes/class-sensei-lesson.php b/includes/class-sensei-lesson.php index 3ffaa788f0..12a90970d9 100755 --- a/includes/class-sensei-lesson.php +++ b/includes/class-sensei-lesson.php @@ -5061,6 +5061,8 @@ public static function footer_quiz_call_to_action( $lesson_id = 0, $user_id = 0 * @return bool */ public static function should_show_lesson_actions( int $lesson_id, int $user_id = 0 ): bool { + $show_actions = true; + $user_id = empty( $user_id ) ? get_current_user_id() : $user_id; $lesson_prerequisite = (int) get_post_meta( $lesson_id, '_lesson_prerequisite', true ); @@ -5069,12 +5071,25 @@ public static function should_show_lesson_actions( int $lesson_id, int $user_id // If the user hasn't completed the prerequisites then hide the current actions. // (If the user is either the lesson creator or admin, show actions). - return Sensei_Utils::user_completed_lesson( $lesson_prerequisite, $user_id ) + $show_actions = Sensei_Utils::user_completed_lesson( $lesson_prerequisite, $user_id ) || Sensei()->lesson->is_lesson_author( $lesson_id, $user_id ) || current_user_can( 'manage_options' ); } - return true; + /** + * Filters if the lesson actions should be shown. + * + * @since $$next-version$$ + * + * @hook sensei_lesson_show_actions + * + * @param {bool} $show_actions Flag if the lesson actions should be shown. + * @param {int} $lesson_id The lesson id. + * @param {int} $user_id The user id. + * + * @return {bool} Filtered flag if the lesson actions should be shown. + */ + return apply_filters( 'sensei_lesson_show_actions', $show_actions, $lesson_id, $user_id ); } /** From fc895505b79fef93f267b4bec78ea106babcfe31 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Wed, 4 Sep 2024 21:49:08 +0600 Subject: [PATCH 2/7] Add tests to make sure the hook is working --- tests/unit-tests/test-class-lesson.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/unit-tests/test-class-lesson.php b/tests/unit-tests/test-class-lesson.php index a97cf5c6c9..4b27ea3d3c 100644 --- a/tests/unit-tests/test-class-lesson.php +++ b/tests/unit-tests/test-class-lesson.php @@ -1358,6 +1358,21 @@ public function testShouldShowLessonActions_WhenPrerequisiteFoundAndWasNotLesson self::assertFalse( $result ); } + public function testShouldShowLessonActions_WhenHookSetsFalce_ReturnsFalse(): void { + /* Arrange */ + $user_id = $this->factory->user->create(); + $lesson_id = $this->factory->lesson->create(); + $result1 = Sensei_Lesson::should_show_lesson_actions( $lesson_id, $user_id ); + add_filter( 'sensei_lesson_show_actions', '__return_false' ); + + /* Act */ + $result2 = Sensei_Lesson::should_show_lesson_actions( $lesson_id, $user_id ); + + /* Assert */ + self::assertTrue( $result1, 'Lesson actions should be shown because there is no filter or prerequisite' ); + self::assertFalse( $result2, 'Lesson actions should not be shown because filter is set to false here' ); + } + public function testFooterQuizCallToAction_WhenCalled_OutputsButton(): void { /* Arrange */ $course = $this->factory->get_course_with_lessons(); From 010d10f0b7fd7ccd1adce85d51dfad66f361b249 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Wed, 4 Sep 2024 22:04:31 +0600 Subject: [PATCH 3/7] Add changelog --- .../fix-prevent-rendering-lesson-actions-in-content-drip | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/fix-prevent-rendering-lesson-actions-in-content-drip diff --git a/changelog/fix-prevent-rendering-lesson-actions-in-content-drip b/changelog/fix-prevent-rendering-lesson-actions-in-content-drip new file mode 100644 index 0000000000..b28679a3fe --- /dev/null +++ b/changelog/fix-prevent-rendering-lesson-actions-in-content-drip @@ -0,0 +1,4 @@ +Significance: minor +Type: development + +Added a hook to conditionally render the lesson actions in the frontend From 438d8cabf4f5f2db521703dcccc7c4e6a010517b Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Tue, 29 Oct 2024 08:55:43 +0600 Subject: [PATCH 4/7] Split a test in two --- tests/unit-tests/test-class-lesson.php | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/tests/unit-tests/test-class-lesson.php b/tests/unit-tests/test-class-lesson.php index 4b27ea3d3c..356641ae22 100644 --- a/tests/unit-tests/test-class-lesson.php +++ b/tests/unit-tests/test-class-lesson.php @@ -1358,18 +1358,29 @@ public function testShouldShowLessonActions_WhenPrerequisiteFoundAndWasNotLesson self::assertFalse( $result ); } - public function testShouldShowLessonActions_WhenHookSetsFalce_ReturnsFalse(): void { + public function testShouldShowLessonActions_WhenHookNotSetAndHasNoPreRequisite_ReturnsTrue(): void { /* Arrange */ $user_id = $this->factory->user->create(); $lesson_id = $this->factory->lesson->create(); - $result1 = Sensei_Lesson::should_show_lesson_actions( $lesson_id, $user_id ); + + /* Act */ + $result = Sensei_Lesson::should_show_lesson_actions( $lesson_id, $user_id ); + + /* Assert */ + self::assertTrue( $result, 'Lesson actions should be shown because there is no filter or prerequisite' ); + } + + public function testShouldShowLessonActions_WhenHookSetToFalse_ReturnsFalse(): void { + /* Arrange */ + $user_id = $this->factory->user->create(); + $lesson_id = $this->factory->lesson->create(); + add_filter( 'sensei_lesson_show_actions', '__return_false' ); /* Act */ - $result2 = Sensei_Lesson::should_show_lesson_actions( $lesson_id, $user_id ); + $result = Sensei_Lesson::should_show_lesson_actions( $lesson_id, $user_id ); /* Assert */ - self::assertTrue( $result1, 'Lesson actions should be shown because there is no filter or prerequisite' ); self::assertFalse( $result2, 'Lesson actions should not be shown because filter is set to false here' ); } From 2918b2b6f185bc2b19ad71ddb2aa10da3efb5875 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Tue, 29 Oct 2024 11:27:41 +0600 Subject: [PATCH 5/7] Fix test --- tests/unit-tests/test-class-lesson.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit-tests/test-class-lesson.php b/tests/unit-tests/test-class-lesson.php index 356641ae22..d4fc9f56d5 100644 --- a/tests/unit-tests/test-class-lesson.php +++ b/tests/unit-tests/test-class-lesson.php @@ -1381,7 +1381,7 @@ public function testShouldShowLessonActions_WhenHookSetToFalse_ReturnsFalse(): v $result = Sensei_Lesson::should_show_lesson_actions( $lesson_id, $user_id ); /* Assert */ - self::assertFalse( $result2, 'Lesson actions should not be shown because filter is set to false here' ); + self::assertFalse( $result, 'Lesson actions should not be shown because filter is set to false here' ); } public function testFooterQuizCallToAction_WhenCalled_OutputsButton(): void { From a4de2509ac41400fbd7f72b4db6ead26d8cb1d01 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Tue, 29 Oct 2024 11:37:00 +0600 Subject: [PATCH 6/7] Test the version --- tests/unit-tests/test-class-sensei-utils.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/unit-tests/test-class-sensei-utils.php b/tests/unit-tests/test-class-sensei-utils.php index dd1288de70..d918817331 100644 --- a/tests/unit-tests/test-class-sensei-utils.php +++ b/tests/unit-tests/test-class-sensei-utils.php @@ -493,6 +493,9 @@ public function testSenseiDeleteQuizAnswers_WhenHasQuizSubmission_DeletesTheQuiz public function lastActivityDateTestingData() { global $wp_version; + echo 'WP Version test: ' . $wp_version . PHP_EOL; + echo json_encode( $wp_version ); + // Account for the string change introduced in https://core.trac.wordpress.org/ticket/61535 $minutes_text = version_compare( $wp_version, '6.6.1', '<=' ) ? 'mins' : 'minutes'; From 14ec3228bcbd2f6719647d4f07da43bcca67fbc6 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Tue, 29 Oct 2024 11:51:19 +0600 Subject: [PATCH 7/7] Remove test lines --- tests/unit-tests/test-class-sensei-utils.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/unit-tests/test-class-sensei-utils.php b/tests/unit-tests/test-class-sensei-utils.php index d918817331..dd1288de70 100644 --- a/tests/unit-tests/test-class-sensei-utils.php +++ b/tests/unit-tests/test-class-sensei-utils.php @@ -493,9 +493,6 @@ public function testSenseiDeleteQuizAnswers_WhenHasQuizSubmission_DeletesTheQuiz public function lastActivityDateTestingData() { global $wp_version; - echo 'WP Version test: ' . $wp_version . PHP_EOL; - echo json_encode( $wp_version ); - // Account for the string change introduced in https://core.trac.wordpress.org/ticket/61535 $minutes_text = version_compare( $wp_version, '6.6.1', '<=' ) ? 'mins' : 'minutes';