Constants
21 hours ago
Accordion.php
21 hours ago
Alert.php
21 hours ago
AttachmentCard.php
21 hours ago
Avatar.php
21 hours ago
Badge.php
21 hours ago
BaseComponent.php
21 hours ago
Button.php
21 hours ago
ConfirmationModal.php
21 hours ago
CourseFilter.php
21 hours ago
DateFilter.php
21 hours ago
DropdownFilter.php
21 hours ago
EmptyState.php
21 hours ago
FileUploader.php
21 hours ago
InputField.php
21 hours ago
Modal.php
21 hours ago
Nav.php
21 hours ago
Pagination.php
21 hours ago
Popover.php
21 hours ago
PreviewTrigger.php
21 hours ago
Progress.php
21 hours ago
SearchFilter.php
21 hours ago
Sorting.php
21 hours ago
StarRating.php
21 hours ago
StarRatingInput.php
21 hours ago
StatusSelect.php
21 hours ago
SvgIcon.php
21 hours ago
Table.php
21 hours ago
Tabs.php
21 hours ago
Tooltip.php
21 hours ago
WPEditor.php
21 hours ago
PreviewTrigger.php
129 lines
| 1 | <?php |
| 2 | /** |
| 3 | * PreviewTrigger Component Class. |
| 4 | * |
| 5 | * Responsible for rendering a preview trigger with a popover card. |
| 6 | * |
| 7 | * @package Tutor\Components |
| 8 | * @author Themeum |
| 9 | * @link https://themeum.com |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Tutor\Components; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Class PreviewTrigger |
| 19 | * |
| 20 | * Example Usage: |
| 21 | * ``` |
| 22 | * PreviewTrigger::make() |
| 23 | * ->id( 123 ) |
| 24 | * ->render(); |
| 25 | * ``` |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | class PreviewTrigger extends BaseComponent { |
| 30 | |
| 31 | /** |
| 32 | * Content ID (Course or Lesson) |
| 33 | * |
| 34 | * @var int |
| 35 | */ |
| 36 | protected $id; |
| 37 | |
| 38 | /** |
| 39 | * Set content ID |
| 40 | * |
| 41 | * @param int $id Content ID. |
| 42 | * |
| 43 | * @return self |
| 44 | */ |
| 45 | public function id( int $id ): self { |
| 46 | $this->id = $id; |
| 47 | return $this; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Get component content |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | public function get(): string { |
| 56 | $id = $this->id; |
| 57 | |
| 58 | if ( ! $id ) { |
| 59 | return ''; |
| 60 | } |
| 61 | |
| 62 | $post = get_post( $id ); |
| 63 | if ( ! $post ) { |
| 64 | return ''; |
| 65 | } |
| 66 | |
| 67 | $post_type = $post->post_type; |
| 68 | $title = get_the_title( $id ); |
| 69 | $thumbnail = get_tutor_course_thumbnail_src( 'medium', $id ); |
| 70 | |
| 71 | // Fallback for thumbnail if empty. |
| 72 | if ( empty( $thumbnail ) ) { |
| 73 | $thumbnail = tutor()->url . 'assets/images/placeholder.svg'; |
| 74 | } |
| 75 | |
| 76 | $instructor_name = ''; |
| 77 | $instructor_url = ''; |
| 78 | |
| 79 | // Determine instructor based on post type logic matching dashboard-notes. |
| 80 | if ( tutor()->course_post_type === $post_type ) { |
| 81 | $author_id = $post->post_author; |
| 82 | $instructor_name = tutor_utils()->display_name( $author_id ); |
| 83 | $instructor_url = tutor_utils()->profile_url( $author_id, true ); |
| 84 | } elseif ( tutor()->lesson_post_type === $post_type ) { |
| 85 | $course_id = tutor_utils()->get_course_id_by_lesson( $id ); |
| 86 | if ( $course_id ) { |
| 87 | $course_post = get_post( $course_id ); |
| 88 | $author_id = $course_post->post_author; |
| 89 | $instructor_name = tutor_utils()->display_name( $author_id ); |
| 90 | $instructor_url = tutor_utils()->profile_url( $author_id, true ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | $preview_data = array( |
| 95 | 'title' => $title, |
| 96 | 'url' => get_permalink( $id ), |
| 97 | 'thumbnail' => $thumbnail, |
| 98 | 'instructor' => $instructor_name, |
| 99 | 'instructor_url' => $instructor_url, |
| 100 | ); |
| 101 | |
| 102 | ob_start(); |
| 103 | ?> |
| 104 | <div |
| 105 | x-data="tutorPreviewTrigger({ data: <?php echo esc_attr( wp_json_encode( $preview_data ) ); ?>, offset: 0 })" |
| 106 | x-ref="trigger" |
| 107 | class="tutor-preview-trigger" |
| 108 | > |
| 109 | <span |
| 110 | tabindex="0" |
| 111 | role="button" |
| 112 | aria-haspopup="true" |
| 113 | :aria-expanded="open ? 'true' : 'false'" |
| 114 | class="tutor-preview-trigger-text" |
| 115 | ><?php echo esc_html( $title ); ?></span> |
| 116 | <div |
| 117 | x-ref="content" |
| 118 | x-show="open" |
| 119 | x-cloak |
| 120 | x-trap="open" |
| 121 | @keydown.escape="hide()" |
| 122 | @click.outside="handleClickOutside()" |
| 123 | class="tutor-popover tutor-preview-card" |
| 124 | ></div> |
| 125 | </div> |
| 126 | <?php |
| 127 | return ob_get_clean(); |
| 128 | } |
| 129 | } |