Constants
19 hours ago
Accordion.php
19 hours ago
Alert.php
19 hours ago
AttachmentCard.php
19 hours ago
Avatar.php
19 hours ago
Badge.php
19 hours ago
BaseComponent.php
19 hours ago
Button.php
19 hours ago
ConfirmationModal.php
19 hours ago
CourseFilter.php
19 hours ago
DateFilter.php
19 hours ago
DropdownFilter.php
19 hours ago
EmptyState.php
19 hours ago
FileUploader.php
19 hours ago
InputField.php
19 hours ago
Modal.php
19 hours ago
Nav.php
19 hours ago
Pagination.php
19 hours ago
Popover.php
19 hours ago
PreviewTrigger.php
19 hours ago
Progress.php
19 hours ago
SearchFilter.php
19 hours ago
Sorting.php
19 hours ago
StarRating.php
19 hours ago
StarRatingInput.php
19 hours ago
StatusSelect.php
19 hours ago
SvgIcon.php
19 hours ago
Table.php
19 hours ago
Tabs.php
19 hours ago
Tooltip.php
19 hours ago
WPEditor.php
19 hours ago
CourseFilter.php
111 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 | * ``` |
| 27 | * CourseFilter::make() |
| 28 | * ->courses( $courses ) |
| 29 | * ->count( $total_announcements ) |
| 30 | * ->render(); |
| 31 | * ``` |
| 32 | * |
| 33 | * @since 4.0.0 |
| 34 | */ |
| 35 | class CourseFilter extends DropdownFilter { |
| 36 | |
| 37 | /** |
| 38 | * Constructor. |
| 39 | * |
| 40 | * Set default values for course filter. |
| 41 | */ |
| 42 | public function __construct() { |
| 43 | $this->query_param( 'course-id' ); |
| 44 | $this->variant( Variant::PRIMARY_SOFT ); |
| 45 | $this->size( Size::X_SMALL ); |
| 46 | $this->popover_size( Size::MEDIUM ); |
| 47 | $this->placeholder( __( 'Search Course', 'tutor' ) ); |
| 48 | $this->search( true ); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Set courses list |
| 53 | * |
| 54 | * @param array|null $courses List of courses. |
| 55 | * |
| 56 | * @return self |
| 57 | */ |
| 58 | public function courses( $courses = null ): self { |
| 59 | $options = self::get_course_filter_options( $courses ); |
| 60 | $this->options( $options ); |
| 61 | return $this; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Get component content. |
| 66 | * |
| 67 | * @return string |
| 68 | */ |
| 69 | public function get(): string { |
| 70 | if ( empty( $this->options ) ) { |
| 71 | $this->courses(); |
| 72 | } |
| 73 | |
| 74 | return parent::get(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Get course filter options for DropdownFilter component. |
| 79 | * |
| 80 | * @since 4.0.0 |
| 81 | * |
| 82 | * @param array|null $courses Optional. Array of course objects. |
| 83 | * If not provided, courses will be fetched based on user permissions. |
| 84 | * |
| 85 | * @return array |
| 86 | */ |
| 87 | public static function get_course_filter_options( $courses = null ) { |
| 88 | $course_options = array( |
| 89 | array( |
| 90 | 'label' => __( 'All Courses', 'tutor' ), |
| 91 | 'value' => '', |
| 92 | ), |
| 93 | ); |
| 94 | |
| 95 | if ( null === $courses ) { |
| 96 | $courses = current_user_can( 'administrator' ) ? CourseModel::get_courses() : CourseModel::get_courses_by_instructor(); |
| 97 | } |
| 98 | |
| 99 | if ( ! empty( $courses ) ) { |
| 100 | foreach ( $courses as $course ) { |
| 101 | $course_options[] = array( |
| 102 | 'label' => $course->post_title, |
| 103 | 'value' => $course->ID, |
| 104 | ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $course_options; |
| 109 | } |
| 110 | } |
| 111 |