ElementGenerator
1 year ago
Ajax.php
1 year ago
Backend.php
1 year ago
Editor.php
1 year ago
Frontend.php
1 year ago
Helper.php
1 year ago
Hooks.php
1 year ago
Iframe.php
1 year ago
Pages.php
1 year ago
VisibilityCondition.php
1 year ago
Hooks.php
361 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Preview script for html markup generator |
| 5 | * |
| 6 | * @package tutor-droip-elements |
| 7 | */ |
| 8 | |
| 9 | namespace TutorLMSDroip; |
| 10 | |
| 11 | use TutorLMSDroip\ElementGenerator\CourseMetaGenerator; |
| 12 | use TutorLMSDroip\ElementGenerator\ThumbnailGenerator; |
| 13 | |
| 14 | if ( ! defined( 'ABSPATH' ) ) { |
| 15 | exit; // Exit if accessed directly. |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * Class Forntend |
| 20 | */ |
| 21 | class Hooks { |
| 22 | |
| 23 | use CourseMetaGenerator; |
| 24 | use ThumbnailGenerator; |
| 25 | |
| 26 | public function __construct() { |
| 27 | add_filter( 'droip_post_types', array( $this, 'droip_post_types' ), 10, 1 ); |
| 28 | add_filter( 'droip_collection_TUTOR_LMS_COURSES', array( $this, 'droip_collection_TUTOR_LMS_COURSES' ), 10, 2 ); |
| 29 | add_filter( 'droip_collection_TUTOR_LMS_CURRICULUM', array( $this, 'droip_collection_TUTOR_LMS_CURRICULUM' ), 10, 2 ); |
| 30 | add_filter( 'droip_dynamic_content', array( $this, 'droip_dynamic_content' ), 10, 2 ); |
| 31 | add_filter( 'droip_comment-TUTOR_LMS-tutor_q_and_a', array( $this, 'modify_qna_comment_data' ) ); |
| 32 | add_filter( 'droip_comment-TUTOR_LMS-tutor_course_rating', array( $this, 'modify_rating_comment_data' ) ); |
| 33 | add_filter( 'droip_comment_added-TUTOR_LMS-tutor_q_and_a', array( $this, 'qna_comment_added' ) ); |
| 34 | add_filter( 'droip_comment_added-TUTOR_LMS-tutor_course_rating', array( $this, 'rating_comment_added' ) ); |
| 35 | add_filter( 'droip_visibility_condition_fields', array( VisibilityCondition::class, 'visibility_condition_fields' ), 10, 2 ); |
| 36 | add_filter( 'droip_visibility_condition_check_' . TDE_APP_PREFIX, array( VisibilityCondition::class, 'element_visibility_condition_check' ), 10, 3 ); |
| 37 | |
| 38 | add_filter( 'droip_dynamic_content_fields', array( $this, 'modify_droip_dynamic_content_fields' ), 10, 2 ); |
| 39 | } |
| 40 | |
| 41 | public function modify_droip_dynamic_content_fields( $fields, $collection_data ) { |
| 42 | if ( isset( $collection_data['collectionType'], $collection_data['type'] ) && $collection_data['collectionType'] === 'posts' && $collection_data['type'] === 'courses' ) { |
| 43 | if ( isset( $fields['typeValues'], $fields['typeValues']['content'] ) ) { |
| 44 | foreach ( $fields['typeValues']['content'] as $key => $value ) { |
| 45 | if ( $value['value'] === 'post' ) { |
| 46 | $fields['typeValues']['content'][ $key ]['title'] = 'Course'; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | return $fields; |
| 52 | } |
| 53 | |
| 54 | public function modify_rating_comment_data( $value ) { |
| 55 | $moderation = tutor_utils()->get_option( 'enable_course_review_moderation', false, true, true ); |
| 56 | $value['comment_agent'] = 'TutorLMSPlugin'; |
| 57 | $value['comment_type'] = 'tutor_course_rating'; |
| 58 | $value['comment_approved'] = $moderation ? 'hold' : 'approved'; |
| 59 | return $value; |
| 60 | } |
| 61 | |
| 62 | public function modify_qna_comment_data( $value ) { |
| 63 | $value['comment_agent'] = 'TutorLMSPlugin'; |
| 64 | $value['comment_type'] = 'tutor_q_and_a'; |
| 65 | $value['comment_approved'] = 'approved'; |
| 66 | return $value; |
| 67 | } |
| 68 | |
| 69 | public function rating_comment_added( $value ) { |
| 70 | $comment_ID = $value['comment_ID']; |
| 71 | $form_data = $value['form_data']; |
| 72 | $rating = isset( $form_data['rating'] ) ? sanitize_text_field( $form_data['rating'] ) : 0; |
| 73 | if ( ! $comment_ID ) { |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | global $wpdb; |
| 78 | $rating_info = $wpdb->get_row( |
| 79 | $wpdb->prepare( |
| 80 | "SELECT * FROM {$wpdb->commentmeta} |
| 81 | WHERE comment_id = %d |
| 82 | AND meta_key = 'tutor_rating'; ", |
| 83 | $comment_ID |
| 84 | ) |
| 85 | ); |
| 86 | if ( $rating_info ) { |
| 87 | $wpdb->update( |
| 88 | $wpdb->commentmeta, |
| 89 | array( 'meta_value' => $rating ), |
| 90 | array( |
| 91 | 'comment_id' => $comment_ID, |
| 92 | 'meta_key' => 'tutor_rating', |
| 93 | ) |
| 94 | ); |
| 95 | } else { |
| 96 | $wpdb->insert( |
| 97 | $wpdb->commentmeta, |
| 98 | array( |
| 99 | 'comment_id' => $comment_ID, |
| 100 | 'meta_key' => 'tutor_rating', |
| 101 | 'meta_value' => $rating, |
| 102 | ) |
| 103 | ); |
| 104 | } |
| 105 | do_action( 'tutor_after_rating_placed', $comment_ID ); |
| 106 | } |
| 107 | |
| 108 | public function qna_comment_added( $value ) { |
| 109 | $form_data = $value['form_data']; |
| 110 | $comment_parent = isset( $form_data['comment_parent'] ) ? sanitize_text_field( $form_data['comment_parent'] ) : 0; |
| 111 | if ( ! $comment_parent ) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | global $wpdb; |
| 116 | $parent_q_author_id = $wpdb->get_var( |
| 117 | $wpdb->prepare( |
| 118 | "SELECT user_id |
| 119 | FROM {$wpdb->comments} |
| 120 | WHERE comment_ID = %d |
| 121 | ", |
| 122 | $comment_parent |
| 123 | ) |
| 124 | ); |
| 125 | |
| 126 | $asker_id = $value['user_id']; |
| 127 | $self = $asker_id == $parent_q_author_id; |
| 128 | update_comment_meta( $parent_q_author_id, 'tutor_qna_read' . ( $self ? '' : '_' . $asker_id ), 0 ); |
| 129 | } |
| 130 | |
| 131 | public function droip_post_types( $post_types ) { |
| 132 | $post_types[] = array( |
| 133 | 'title' => 'Course Topics', |
| 134 | 'value' => 'topics', |
| 135 | ); |
| 136 | return $post_types; |
| 137 | } |
| 138 | public function droip_collection_TUTOR_LMS_COURSES( $value, $args ) { |
| 139 | $context = isset( $args['context'] ) ? $args['context'] : false; |
| 140 | if ( $context && isset( $context['collectionType'] ) ) { |
| 141 | $collectionType = $context['collectionType']; // post/tags/users |
| 142 | if ( $collectionType === 'post' ) { |
| 143 | if ( $args['name'] === 'TUTOR_LMS-topics' ) { |
| 144 | $args['post_parent'] = $context['id']; |
| 145 | $args['inherit'] = true; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | if ( $args['name'] === 'TUTOR_LMS-topics' ) { |
| 150 | $args['name'] = 'topics'; |
| 151 | $args['item_per_page'] = -1; |
| 152 | // $obj = HelperFunctions::get_posts($args); |
| 153 | // $obj['itemType'] = 'post'; |
| 154 | // return $obj; |
| 155 | return array( |
| 156 | 'data' => tutor_utils()->get_topics( $args['post_parent'] )->posts, |
| 157 | 'pagination' => null, |
| 158 | 'itemType' => 'post', |
| 159 | ); |
| 160 | } elseif ( $args['name'] === 'TUTOR_LMS-tutor_course_rating' ) { |
| 161 | $reviews = tutor_utils()->get_course_reviews( $args['post_parent'], 0, 100, false, array( 'approved' ), get_current_user_id() ); |
| 162 | $reviews = $this->add_author_image_to_data( $reviews ); |
| 163 | return array( |
| 164 | 'data' => $reviews, |
| 165 | 'pagination' => array(), |
| 166 | 'itemType' => 'comment', |
| 167 | ); |
| 168 | } elseif ( $args['name'] === 'TUTOR_LMS-materials' ) { |
| 169 | $topic_contents = tutor_utils()->get_course_contents_by_topic( $args['post_parent'], -1 ); |
| 170 | $obj['data'] = $topic_contents->posts; |
| 171 | $obj['pagination'] = array(); |
| 172 | $obj['itemType'] = 'post'; |
| 173 | return $obj; |
| 174 | } elseif ( $args['name'] === 'TUTOR_LMS-tutor_q_and_a' ) { |
| 175 | if ( $args['post_parent'] == 0 ) { |
| 176 | return array( |
| 177 | 'data' => array(), |
| 178 | 'pagination' => array(), |
| 179 | 'itemType' => 'comment', |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | if ( isset( $args['context'] ) && isset( $args['context']['comment_ID'] ) ) { |
| 184 | $q_id = $args['context']['comment_ID']; |
| 185 | $answer = tutor_utils()->get_qa_answer_by_question( $q_id ); |
| 186 | $filteredAnswers = array_values( |
| 187 | array_filter( |
| 188 | $answer, |
| 189 | function ( $obj ) use ( $q_id ) { |
| 190 | return $obj->comment_ID !== $q_id; |
| 191 | } |
| 192 | ) |
| 193 | ); |
| 194 | $filteredAnswers = $this->add_author_image_to_data( $filteredAnswers ); |
| 195 | $filteredAnswers = $this->add_qna_reply_flag( $filteredAnswers ); |
| 196 | return array( |
| 197 | 'data' => $filteredAnswers, |
| 198 | 'pagination' => array(), |
| 199 | 'itemType' => 'comment', |
| 200 | ); |
| 201 | } |
| 202 | |
| 203 | $questions = tutor_utils()->get_qa_questions( 0, 100, '', null, null, null, null, false, array( 'course_id' => $args['post_parent'] ) ); |
| 204 | $questions = $this->add_author_image_to_data( $questions ); |
| 205 | return array( |
| 206 | 'data' => $questions, |
| 207 | 'pagination' => array(), |
| 208 | 'itemType' => 'comment', |
| 209 | ); |
| 210 | } elseif ( $args['name'] === 'TUTOR_LMS-announcements' ) { |
| 211 | $announcements = tutor_utils()->get_announcements( $args['post_parent'] ); |
| 212 | return array( |
| 213 | 'data' => $announcements, |
| 214 | 'pagination' => array(), |
| 215 | 'itemType' => 'announcement', |
| 216 | ); |
| 217 | } elseif ( $args['name'] === 'TUTOR_LMS-resources' ) { |
| 218 | $resources = tutor_utils()->get_attachments( $args['post_parent'] ); |
| 219 | return array( |
| 220 | 'data' => $resources, |
| 221 | 'pagination' => array(), |
| 222 | 'itemType' => 'resources', |
| 223 | ); |
| 224 | } elseif ($args['name'] === 'TUTOR_LMS-instructors') { |
| 225 | $instructors = tutor_utils()->get_instructors_by_course($args['post_parent']); |
| 226 | $instructors = array_map(fn($user) => (array) array_merge((array) $user, [ |
| 227 | 'user_url' => get_author_posts_url($user->ID), |
| 228 | 'profile_image' => get_avatar_url($user->ID) |
| 229 | ]), $instructors); |
| 230 | |
| 231 | return array( |
| 232 | 'data' => $instructors, |
| 233 | 'pagination' => array(), |
| 234 | 'itemType' => 'user', |
| 235 | ); |
| 236 | } else { |
| 237 | return array( |
| 238 | 'data' => array(), |
| 239 | 'pagination' => array(), |
| 240 | 'itemType' => false, |
| 241 | ); |
| 242 | } |
| 243 | |
| 244 | return $value; |
| 245 | } |
| 246 | |
| 247 | public function droip_collection_TUTOR_LMS_CURRICULUM( $value, $args ) { |
| 248 | if ( $args['name'] === 'TUTOR_LMS-materials' && isset( $args['post_parent'] ) ) { |
| 249 | $topic_contents = tutor_utils()->get_course_contents_by_topic( $args['post_parent'], -1 ); |
| 250 | $obj['data'] = $topic_contents->posts; |
| 251 | $obj['pagination'] = array(); |
| 252 | $obj['itemType'] = 'post'; |
| 253 | return $obj; |
| 254 | } else { |
| 255 | return array( |
| 256 | 'data' => array(), |
| 257 | 'pagination' => array(), |
| 258 | 'itemType' => false, |
| 259 | ); |
| 260 | } |
| 261 | |
| 262 | return $value; |
| 263 | } |
| 264 | public function droip_dynamic_content( $value, $args ) { |
| 265 | if ( isset( $args['dynamicContent'] ) ) { |
| 266 | $dynamicContent = $args['dynamicContent']; |
| 267 | $collectionItem = $args['collectionItem']; |
| 268 | |
| 269 | if ( $collectionItem && isset( $collectionItem['ID'] ) ) { |
| 270 | $course_id = $collectionItem['ID']; |
| 271 | } elseif ( isset( $args['post_id'] ) ) { |
| 272 | $course_id = $args['post_id']; |
| 273 | } |
| 274 | |
| 275 | if ( $dynamicContent['type'] === 'course' ) { |
| 276 | if ( $dynamicContent['value'] === 'thumbnail_image' ) { |
| 277 | $tutor_course_img = get_tutor_course_thumbnail_src( 'post-thumbnail', $course_id ); |
| 278 | if ( $tutor_course_img ) { |
| 279 | return $tutor_course_img; |
| 280 | } |
| 281 | } elseif ( $dynamicContent['value'] === 'thumbnail_video' ) { |
| 282 | $video_info = tutor_utils()->get_video_info( $course_id ); |
| 283 | $source_key = is_object( $video_info ) ? 'source_' . $video_info->source : null; |
| 284 | if ( $source_key ) { |
| 285 | return array( 'url' => $video_info->$source_key ); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } elseif ( isset( $args['settings'] ) ) { |
| 290 | $settings = $args['settings']; |
| 291 | $options = array(); |
| 292 | switch ( $args['collectionProperties']['type'] ) { |
| 293 | case 'TUTOR_LMS-tutor_course_rating': |
| 294 | $reviews = tutor_utils()->get_course_reviews( $args['collectionItem']['comment_ID'], 0, 100, false, array( 'approved' ), get_current_user_id(), false ); |
| 295 | $options['comment'] = count( $reviews ) > 0 ? $reviews[0] : array(); |
| 296 | break; |
| 297 | case 'TUTOR_LMS-materials': |
| 298 | $options['material'] = $args['collectionItem']; |
| 299 | break; |
| 300 | case 'TUTOR_LMS-tutor_q_and_a': |
| 301 | $options['comment'] = tutor_utils()->get_qa_question( $args['collectionItem']['comment_ID'] ); |
| 302 | break; |
| 303 | case 'TUTOR_LMS-announcements': |
| 304 | $options['announcement'] = get_post( $args['collectionItem']['ID'] ); |
| 305 | break; |
| 306 | case 'TUTOR_LMS-resources': |
| 307 | $resource_id = $args['collectionItem']['id'] ?? null; |
| 308 | if ( $resource_id ) { |
| 309 | $resource = tutor_utils()->get_attachment_data( $resource_id ); |
| 310 | if ( $resource ) { |
| 311 | $options['resources'] = $resource; |
| 312 | } |
| 313 | } |
| 314 | break; |
| 315 | default: |
| 316 | break; |
| 317 | } |
| 318 | |
| 319 | $collectionItem = $args['collectionItem']; |
| 320 | if ( $collectionItem && isset( $collectionItem['ID'] ) ) { |
| 321 | $course_id = $collectionItem['ID']; |
| 322 | } elseif ( isset( $args['post_id'] ) ) { |
| 323 | $course_id = $args['post_id']; |
| 324 | } |
| 325 | |
| 326 | $is_instructor = false; |
| 327 | if ( ! $collectionItem && isset( $args['templateEditContext']['collectionType'] ) ) { |
| 328 | $is_instructor = $args['templateEditContext']['collectionType'] === 'user'; |
| 329 | } else if ( |
| 330 | $collectionItem && $collectionItem['collectionType'] === 'users' || |
| 331 | $args['collectionProperties']['type'] === 'TUTOR_LMS-instructors' |
| 332 | ) { |
| 333 | $is_instructor = true; |
| 334 | } |
| 335 | |
| 336 | if ( isset( $settings['course_meta_type'] ) ) { |
| 337 | $meta = $this->get_course_meta( $settings['course_meta_type'], $course_id, $options, $settings, $is_instructor ); |
| 338 | return $this->wrap_if_meta_has_label( $meta, $settings ); |
| 339 | } |
| 340 | if ( isset( $settings['thumbnail_type'] ) ) { |
| 341 | return $this->get_course_thumbnail( $settings['thumbnail_type'], $settings['thumbnail_type'], $course_id, $options ); |
| 342 | } |
| 343 | } |
| 344 | return $value; |
| 345 | } |
| 346 | |
| 347 | public function add_author_image_to_data( $list ) { |
| 348 | foreach ( $list as $item ) { |
| 349 | $item->author_profile_picture = array( 'src' => get_avatar_url( $item->user_id ) ); |
| 350 | } |
| 351 | return $list; |
| 352 | } |
| 353 | |
| 354 | public function add_qna_reply_flag( $list ) { |
| 355 | foreach ( $list as $item ) { |
| 356 | $item->reply = true; |
| 357 | } |
| 358 | return $list; |
| 359 | } |
| 360 | } |
| 361 |