Constants
3 weeks ago
Accordion.php
6 days ago
Alert.php
6 days ago
AttachmentCard.php
6 days ago
Avatar.php
6 days ago
Badge.php
6 days ago
BaseComponent.php
3 weeks ago
Button.php
6 days ago
ConfirmationModal.php
6 days ago
CourseFilter.php
6 days ago
DateFilter.php
6 days ago
DropdownFilter.php
6 days ago
EmptyState.php
6 days ago
FileUploader.php
6 days ago
InputField.php
6 days ago
Modal.php
6 days ago
Nav.php
6 days ago
Pagination.php
6 days ago
Popover.php
6 days ago
PreviewTrigger.php
6 days ago
Progress.php
6 days ago
SearchFilter.php
6 days ago
Sorting.php
6 days ago
StarRating.php
6 days ago
StarRatingInput.php
6 days ago
StatusSelect.php
6 days ago
SvgIcon.php
6 days ago
Table.php
6 days ago
Tabs.php
6 days ago
Tooltip.php
6 days ago
WPEditor.php
6 days ago
CourseFilter.php
124 lines
| 1 | <?php |
| 2 | /** |
| 3 | * CourseFilter Component Class. |
| 4 | * |
| 5 | * Responsible for rendering a course filtering dropdown |
| 6 | * with a search box to filter the list. |
| 7 | * |
| 8 | * @package Tutor\Components |
| 9 | * @author Themeum |
| 10 | * @link https://themeum.com |
| 11 | * @since 4.0.0 |
| 12 | */ |
| 13 | |
| 14 | namespace Tutor\Components; |
| 15 | |
| 16 | use Tutor\Components\Constants\Size; |
| 17 | use Tutor\Components\Constants\Variant; |
| 18 | use Tutor\Models\CourseModel; |
| 19 | |
| 20 | defined( 'ABSPATH' ) || exit; |
| 21 | |
| 22 | /** |
| 23 | * Class CourseFilter |
| 24 | * |
| 25 | * Example Usage: |
| 26 | * ```php |
| 27 | * // Minimal — auto-fetches courses for the current user |
| 28 | * CourseFilter::make() |
| 29 | * ->render(); |
| 30 | * |
| 31 | * // With explicit courses list and total count |
| 32 | * CourseFilter::make() |
| 33 | * ->courses( $courses ) |
| 34 | * ->count( $total_announcements ) |
| 35 | * ->render(); |
| 36 | * |
| 37 | * // Pre-populate options from a custom courses array |
| 38 | * CourseFilter::make() |
| 39 | * ->courses( CourseModel::get_courses_by_instructor() ) |
| 40 | * ->render(); |
| 41 | * |
| 42 | * // Retrieve HTML without echoing |
| 43 | * $html = CourseFilter::make()->courses( $courses )->get(); |
| 44 | * ``` |
| 45 | * |
| 46 | * @since 4.0.0 |
| 47 | */ |
| 48 | class CourseFilter extends DropdownFilter { |
| 49 | |
| 50 | /** |
| 51 | * Constructor. |
| 52 | * |
| 53 | * Set default values for course filter. |
| 54 | */ |
| 55 | public function __construct() { |
| 56 | $this->query_param( 'course-id' ); |
| 57 | $this->variant( Variant::PRIMARY_SOFT ); |
| 58 | $this->size( Size::X_SMALL ); |
| 59 | $this->popover_size( Size::MEDIUM ); |
| 60 | $this->placeholder( __( 'Search Course', 'tutor' ) ); |
| 61 | $this->search( true ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Set courses list |
| 66 | * |
| 67 | * @param array|null $courses List of courses. |
| 68 | * |
| 69 | * @return self |
| 70 | */ |
| 71 | public function courses( $courses = null ): self { |
| 72 | $options = self::get_course_filter_options( $courses ); |
| 73 | $this->options( $options ); |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get component content. |
| 79 | * |
| 80 | * @return string |
| 81 | */ |
| 82 | public function get(): string { |
| 83 | if ( empty( $this->options ) ) { |
| 84 | $this->courses(); |
| 85 | } |
| 86 | |
| 87 | return parent::get(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Get course filter options for DropdownFilter component. |
| 92 | * |
| 93 | * @since 4.0.0 |
| 94 | * |
| 95 | * @param array|null $courses Optional. Array of course objects. |
| 96 | * If not provided, courses will be fetched based on user permissions. |
| 97 | * |
| 98 | * @return array |
| 99 | */ |
| 100 | public static function get_course_filter_options( $courses = null ) { |
| 101 | $course_options = array( |
| 102 | array( |
| 103 | 'label' => __( 'All Courses', 'tutor' ), |
| 104 | 'value' => '', |
| 105 | ), |
| 106 | ); |
| 107 | |
| 108 | if ( null === $courses ) { |
| 109 | $courses = current_user_can( 'manage_options' ) ? CourseModel::get_courses() : CourseModel::get_courses_by_instructor(); |
| 110 | } |
| 111 | |
| 112 | if ( ! empty( $courses ) ) { |
| 113 | foreach ( $courses as $course ) { |
| 114 | $course_options[] = array( |
| 115 | 'label' => $course->post_title, |
| 116 | 'value' => $course->ID, |
| 117 | ); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return $course_options; |
| 122 | } |
| 123 | } |
| 124 |