Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook to conditionally show the lesson actions block in the frontend #7677

Merged
merged 9 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: development

Added a hook to conditionally render the lesson actions in the frontend
19 changes: 17 additions & 2 deletions includes/class-sensei-lesson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 );
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/unit-tests/test-class-lesson.php
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,21 @@ public function testShouldShowLessonActions_WhenPrerequisiteFoundAndWasNotLesson
self::assertFalse( $result );
}

public function testShouldShowLessonActions_WhenHookSetsFalce_ReturnsFalse(): void {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test case name is somewhat misleading. In particular, the WhenHookSetsFalse part because in this test case we're testing both true and false. We could split these into 2 separate use cases to test each since there isn't much repetitive setup code.

/* 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();
Expand Down
Loading