ElementGenerator
10 months ago
Ajax.php
10 months ago
Backend.php
1 year ago
Editor.php
1 year ago
Frontend.php
1 year ago
Helper.php
1 year ago
Hooks.php
11 months ago
Iframe.php
1 year ago
Pages.php
11 months ago
VisibilityCondition.php
1 year ago
Hooks.php
472 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 | use Tutor\Ecommerce\CartController; |
| 14 | |
| 15 | if (! defined('ABSPATH')) { |
| 16 | exit; // Exit if accessed directly. |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Class Forntend |
| 21 | */ |
| 22 | class Hooks |
| 23 | { |
| 24 | |
| 25 | use CourseMetaGenerator; |
| 26 | use ThumbnailGenerator; |
| 27 | |
| 28 | public function __construct() |
| 29 | { |
| 30 | add_filter('droip_post_types', [$this, 'droip_post_types'], 10, 1); |
| 31 | add_filter('droip_collection_TUTOR_LMS_COURSES', [$this, 'droip_collection_TUTOR_LMS_COURSES'], 10, 2); |
| 32 | add_filter('droip_collection_TUTOR_LMS_CURRICULUM', [$this, 'droip_collection_TUTOR_LMS_CURRICULUM'], 10, 2); |
| 33 | add_filter('droip_collection_TUTOR_LMS_CART', [$this, 'droip_collection_TUTOR_LMS_CART'], 10, 2); |
| 34 | add_filter('droip_dynamic_content', [$this, 'droip_dynamic_content'], 10, 2); |
| 35 | add_filter('droip_comment-TUTOR_LMS-tutor_q_and_a', [$this, 'modify_qna_comment_data']); |
| 36 | add_filter('droip_comment-TUTOR_LMS-tutor_course_rating', [$this, 'modify_rating_comment_data']); |
| 37 | add_filter('droip_comment_added-TUTOR_LMS-tutor_q_and_a', [$this, 'qna_comment_added']); |
| 38 | add_filter('droip_comment_added-TUTOR_LMS-tutor_course_rating', [$this, 'rating_comment_added']); |
| 39 | add_filter('droip_visibility_condition_fields', [VisibilityCondition::class, 'visibility_condition_fields'], 10, 2); |
| 40 | add_filter('droip_visibility_condition_check_' . TDE_APP_PREFIX, [VisibilityCondition::class, 'element_visibility_condition_check'], 10, 3); |
| 41 | |
| 42 | add_filter('droip_dynamic_content_fields', [$this, 'modify_droip_dynamic_content_fields'], 10, 2); |
| 43 | add_filter('droip_external_collection_options', [$this, 'modify_external_collection_options'], 10, 2); |
| 44 | add_filter('droip_external_collection_item_type', [$this, 'get_tutor_item_types'], 10, 2); |
| 45 | } |
| 46 | |
| 47 | public function modify_droip_dynamic_content_fields($fields, $collection_data) |
| 48 | { |
| 49 | if (isset($collection_data['collectionType'], $collection_data['type']) && $collection_data['collectionType'] === 'posts' && $collection_data['type'] === 'courses') { |
| 50 | if (isset($fields['typeValues'], $fields['typeValues']['content'])) { |
| 51 | foreach ($fields['typeValues']['content'] as $key => $value) { |
| 52 | if ($value['value'] === 'post') { |
| 53 | $fields['typeValues']['content'][$key]['title'] = 'Course'; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | return $fields; |
| 59 | } |
| 60 | |
| 61 | public function modify_external_collection_options($options, $args) |
| 62 | { |
| 63 | $type = $args['type']; |
| 64 | $collectionType = $args['collectionType']; |
| 65 | |
| 66 | $courses_group = [ |
| 67 | 'title' => 'Course', |
| 68 | 'value' => 'TUTOR_LMS_COURSES', |
| 69 | 'inherit' => true, |
| 70 | 'default_select_type' => "TUTOR_LMS-topics", |
| 71 | 'group' => [ |
| 72 | ['title' => 'Curriculum', 'value' => "TUTOR_LMS-topics", 'itemType' => 'post'], |
| 73 | ['title' => 'Reviews', 'value' => "TUTOR_LMS-tutor_course_rating", 'itemType' => 'comment'], |
| 74 | ['title' => 'Q & A', 'value' => "TUTOR_LMS-tutor_q_and_a", 'itemType' => 'comment'], |
| 75 | ['title' => 'Announcements', 'value' => "TUTOR_LMS-announcements", 'itemType' => 'announcement'], |
| 76 | ['title' => 'Resources', 'value' => "TUTOR_LMS-resources", 'itemType' => 'resources'], |
| 77 | ['title' => 'Gradebooks', 'value' => "TUTOR_LMS-gradebooks", 'itemType' => 'post'], |
| 78 | ['title' => 'Instructors', 'value' => "TUTOR_LMS-instructors", 'itemType' => 'user'], |
| 79 | // ['title' => 'Cart', 'value' => "TUTOR_LMS-cart", 'itemType' => 'post'] //temporary commented. |
| 80 | ], |
| 81 | ]; |
| 82 | |
| 83 | $curriculum_group = [ |
| 84 | 'title' => 'Curriculum', |
| 85 | 'value' => 'TUTOR_LMS_CURRICULUM', |
| 86 | 'inherit' => true, |
| 87 | 'default_select_type' => "TUTOR_LMS-materials", |
| 88 | 'group' => [ |
| 89 | ['title' => 'Materials', 'value' => "TUTOR_LMS-materials", 'itemType' => 'material'], |
| 90 | ], |
| 91 | ]; |
| 92 | |
| 93 | $cart_group = [ |
| 94 | 'title' => 'Course', |
| 95 | 'value' => 'TUTOR_LMS_COURSES', |
| 96 | 'inherit' => true, |
| 97 | 'default_select_type' => "TUTOR_LMS-cart", |
| 98 | 'group' => [ |
| 99 | ['title' => 'Cart', 'value' => "TUTOR_LMS-cart", 'itemType' => 'post'], |
| 100 | ], |
| 101 | ]; |
| 102 | |
| 103 | if ($collectionType === 'posts' && $type === 'courses') { |
| 104 | $options[] = $courses_group; |
| 105 | } else if ($collectionType === 'TUTOR_LMS_COURSES' && $type === 'TUTOR_LMS-topics') { |
| 106 | $options[] = $curriculum_group; |
| 107 | } else { |
| 108 | // $options[] = $cart_group; //temporary commented. |
| 109 | } |
| 110 | return $options; |
| 111 | } |
| 112 | |
| 113 | public function get_tutor_item_types($oldItemType, $collectionName) |
| 114 | { |
| 115 | switch ($collectionName) { |
| 116 | case 'TUTOR_LMS-tutor_course_rating': |
| 117 | return 'comment'; |
| 118 | case 'TUTOR_LMS-materials': |
| 119 | return 'material'; |
| 120 | case 'TUTOR_LMS-tutor_q_and_a': |
| 121 | return 'comment'; |
| 122 | case 'TUTOR_LMS-announcements': |
| 123 | return 'announcement'; |
| 124 | case 'TUTOR_LMS-resources': |
| 125 | return 'resources'; |
| 126 | default: |
| 127 | return $oldItemType; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | public function modify_rating_comment_data($value) |
| 132 | { |
| 133 | $moderation = tutor_utils()->get_option('enable_course_review_moderation', false, true, true); |
| 134 | $value['comment_agent'] = 'TutorLMSPlugin'; |
| 135 | $value['comment_type'] = 'tutor_course_rating'; |
| 136 | $value['comment_approved'] = $moderation ? 'hold' : 'approved'; |
| 137 | return $value; |
| 138 | } |
| 139 | |
| 140 | public function modify_qna_comment_data($value) |
| 141 | { |
| 142 | $value['comment_agent'] = 'TutorLMSPlugin'; |
| 143 | $value['comment_type'] = 'tutor_q_and_a'; |
| 144 | $value['comment_approved'] = 'approved'; |
| 145 | return $value; |
| 146 | } |
| 147 | |
| 148 | public function rating_comment_added($value) |
| 149 | { |
| 150 | $comment_ID = $value['comment_ID']; |
| 151 | $form_data = $value['form_data']; |
| 152 | $rating = isset($form_data['rating']) ? sanitize_text_field($form_data['rating']) : 0; |
| 153 | if (! $comment_ID) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | global $wpdb; |
| 158 | $rating_info = $wpdb->get_row( |
| 159 | $wpdb->prepare( |
| 160 | "SELECT * FROM {$wpdb->commentmeta} |
| 161 | WHERE comment_id = %d |
| 162 | AND meta_key = 'tutor_rating'; ", |
| 163 | $comment_ID |
| 164 | ) |
| 165 | ); |
| 166 | if ($rating_info) { |
| 167 | $wpdb->update( |
| 168 | $wpdb->commentmeta, |
| 169 | ['meta_value' => $rating], |
| 170 | [ |
| 171 | 'comment_id' => $comment_ID, |
| 172 | 'meta_key' => 'tutor_rating', |
| 173 | ] |
| 174 | ); |
| 175 | } else { |
| 176 | $wpdb->insert( |
| 177 | $wpdb->commentmeta, |
| 178 | [ |
| 179 | 'comment_id' => $comment_ID, |
| 180 | 'meta_key' => 'tutor_rating', |
| 181 | 'meta_value' => $rating, |
| 182 | ] |
| 183 | ); |
| 184 | } |
| 185 | do_action('tutor_after_rating_placed', $comment_ID); |
| 186 | } |
| 187 | |
| 188 | public function qna_comment_added($value) |
| 189 | { |
| 190 | $form_data = $value['form_data']; |
| 191 | $comment_parent = isset($form_data['comment_parent']) ? sanitize_text_field($form_data['comment_parent']) : 0; |
| 192 | if (! $comment_parent) { |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | global $wpdb; |
| 197 | $parent_q_author_id = $wpdb->get_var( |
| 198 | $wpdb->prepare( |
| 199 | "SELECT user_id |
| 200 | FROM {$wpdb->comments} |
| 201 | WHERE comment_ID = %d |
| 202 | ", |
| 203 | $comment_parent |
| 204 | ) |
| 205 | ); |
| 206 | |
| 207 | $asker_id = $value['user_id']; |
| 208 | $self = $asker_id == $parent_q_author_id; |
| 209 | update_comment_meta($parent_q_author_id, 'tutor_qna_read' . ($self ? '' : '_' . $asker_id), 0); |
| 210 | } |
| 211 | |
| 212 | public function droip_post_types($post_types) |
| 213 | { |
| 214 | $post_types[] = [ |
| 215 | 'title' => 'Course Topics', |
| 216 | 'value' => 'topics', |
| 217 | ]; |
| 218 | return $post_types; |
| 219 | } |
| 220 | public function droip_collection_TUTOR_LMS_COURSES($value, $args) |
| 221 | { |
| 222 | $context = isset($args['context']) ? $args['context'] : false; |
| 223 | if ($context && isset($context['collectionType'])) { |
| 224 | $collectionType = $context['collectionType']; // post/tags/users |
| 225 | if ($collectionType === 'post') { |
| 226 | if ($args['name'] === 'TUTOR_LMS-topics') { |
| 227 | $args['post_parent'] = $context['id']; |
| 228 | $args['inherit'] = true; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | if ($args['name'] === 'TUTOR_LMS-topics') { |
| 233 | $args['name'] = 'topics'; |
| 234 | $args['item_per_page'] = -1; |
| 235 | // $obj = HelperFunctions::get_posts($args); |
| 236 | // $obj['itemType'] = 'post'; |
| 237 | // return $obj; |
| 238 | return [ |
| 239 | 'data' => tutor_utils()->get_topics($args['post_parent'])->posts, |
| 240 | 'pagination' => null, |
| 241 | 'itemType' => 'post', |
| 242 | ]; |
| 243 | } elseif ($args['name'] === 'TUTOR_LMS-tutor_course_rating') { |
| 244 | $reviews = tutor_utils()->get_course_reviews($args['post_parent'], 0, 100, false, ['approved'], get_current_user_id()); |
| 245 | $reviews = $this->add_author_image_to_data($reviews); |
| 246 | return [ |
| 247 | 'data' => $reviews, |
| 248 | 'pagination' => [], |
| 249 | 'itemType' => 'comment', |
| 250 | ]; |
| 251 | } elseif ($args['name'] === 'TUTOR_LMS-materials') { |
| 252 | $topic_contents = tutor_utils()->get_course_contents_by_topic($args['post_parent'], -1); |
| 253 | $obj['data'] = $topic_contents->posts; |
| 254 | $obj['pagination'] = []; |
| 255 | $obj['itemType'] = 'post'; |
| 256 | return $obj; |
| 257 | } elseif ($args['name'] === 'TUTOR_LMS-tutor_q_and_a') { |
| 258 | if ($args['post_parent'] == 0) { |
| 259 | return [ |
| 260 | 'data' => [], |
| 261 | 'pagination' => [], |
| 262 | 'itemType' => 'comment', |
| 263 | ]; |
| 264 | } |
| 265 | |
| 266 | if (isset($args['context']) && isset($args['context']['comment_ID'])) { |
| 267 | $q_id = $args['context']['comment_ID']; |
| 268 | $answer = tutor_utils()->get_qa_answer_by_question($q_id); |
| 269 | $filteredAnswers = array_values( |
| 270 | array_filter( |
| 271 | $answer, |
| 272 | function ($obj) use ($q_id) { |
| 273 | return $obj->comment_ID !== $q_id; |
| 274 | } |
| 275 | ) |
| 276 | ); |
| 277 | $filteredAnswers = $this->add_author_image_to_data($filteredAnswers); |
| 278 | $filteredAnswers = $this->add_qna_reply_flag($filteredAnswers); |
| 279 | return [ |
| 280 | 'data' => $filteredAnswers, |
| 281 | 'pagination' => [], |
| 282 | 'itemType' => 'comment', |
| 283 | ]; |
| 284 | } |
| 285 | |
| 286 | $questions = tutor_utils()->get_qa_questions(0, 100, '', null, null, null, null, false, ['course_id' => $args['post_parent']]); |
| 287 | $questions = $this->add_author_image_to_data($questions); |
| 288 | return [ |
| 289 | 'data' => $questions, |
| 290 | 'pagination' => [], |
| 291 | 'itemType' => 'comment', |
| 292 | ]; |
| 293 | } elseif ($args['name'] === 'TUTOR_LMS-announcements') { |
| 294 | $announcements = tutor_utils()->get_announcements($args['post_parent']); |
| 295 | return [ |
| 296 | 'data' => $announcements, |
| 297 | 'pagination' => [], |
| 298 | 'itemType' => 'announcement', |
| 299 | ]; |
| 300 | } elseif ($args['name'] === 'TUTOR_LMS-resources') { |
| 301 | $resources = tutor_utils()->get_attachments($args['post_parent']); |
| 302 | return [ |
| 303 | 'data' => $resources, |
| 304 | 'pagination' => [], |
| 305 | 'itemType' => 'resources', |
| 306 | ]; |
| 307 | } elseif ($args['name'] === 'TUTOR_LMS-instructors') { |
| 308 | $instructors = tutor_utils()->get_instructors_by_course($args['post_parent']); |
| 309 | $instructors = array_map(fn($user) => (array) array_merge((array) $user, [ |
| 310 | 'user_url' => get_author_posts_url($user->ID), |
| 311 | 'profile_image' => get_avatar_url($user->ID), |
| 312 | ]), $instructors); |
| 313 | |
| 314 | return [ |
| 315 | 'data' => $instructors, |
| 316 | 'pagination' => [], |
| 317 | 'itemType' => 'user', |
| 318 | ]; |
| 319 | } elseif ($args['name'] === 'TUTOR_LMS-cart') { |
| 320 | $cart = new CartController(); |
| 321 | $cart_items = $cart->get_cart_items(); |
| 322 | $courses = isset($cart_items['courses'], $cart_items['courses']['results']) ? $cart_items['courses']['results'] : []; |
| 323 | return [ |
| 324 | 'data' => $courses, |
| 325 | 'pagination' => null, |
| 326 | 'itemType' => 'post', |
| 327 | ]; |
| 328 | } else { |
| 329 | return [ |
| 330 | 'data' => [], |
| 331 | 'pagination' => [], |
| 332 | 'itemType' => false, |
| 333 | ]; |
| 334 | } |
| 335 | |
| 336 | return $value; |
| 337 | } |
| 338 | |
| 339 | public function droip_collection_TUTOR_LMS_CURRICULUM($value, $args) |
| 340 | { |
| 341 | if ($args['name'] === 'TUTOR_LMS-materials' && isset($args['post_parent'])) { |
| 342 | $topic_contents = tutor_utils()->get_course_contents_by_topic($args['post_parent'], -1); |
| 343 | $obj['data'] = $topic_contents->posts; |
| 344 | $obj['pagination'] = []; |
| 345 | $obj['itemType'] = 'post'; |
| 346 | return $obj; |
| 347 | } else { |
| 348 | return [ |
| 349 | 'data' => [], |
| 350 | 'pagination' => [], |
| 351 | 'itemType' => false, |
| 352 | ]; |
| 353 | } |
| 354 | |
| 355 | return $value; |
| 356 | } |
| 357 | |
| 358 | public function droip_dynamic_content($value, $args) |
| 359 | { |
| 360 | if (isset($args['dynamicContent'])) { |
| 361 | $dynamicContent = $args['dynamicContent']; |
| 362 | $collectionItem = $args['collectionItem']; |
| 363 | |
| 364 | if ($collectionItem && isset($collectionItem['ID'])) { |
| 365 | $course_id = $collectionItem['ID']; |
| 366 | } elseif (isset($args['post_id'])) { |
| 367 | $course_id = $args['post_id']; |
| 368 | } |
| 369 | |
| 370 | if ($dynamicContent['type'] === 'course') { |
| 371 | if ($dynamicContent['value'] === 'thumbnail_image') { |
| 372 | $tutor_course_img = get_tutor_course_thumbnail_src('post-thumbnail', $course_id); |
| 373 | if ($tutor_course_img) { |
| 374 | return $tutor_course_img; |
| 375 | } |
| 376 | } elseif ($dynamicContent['value'] === 'thumbnail_video') { |
| 377 | $video_info = tutor_utils()->get_video_info($course_id); |
| 378 | $source_key = is_object($video_info) ? 'source_' . $video_info->source : null; |
| 379 | if ($source_key) { |
| 380 | return ['url' => $video_info->$source_key]; |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | } elseif (isset($args['settings'])) { |
| 385 | $settings = $args['settings']; |
| 386 | $options = []; |
| 387 | switch ($args['collectionProperties']['type']) { |
| 388 | case 'TUTOR_LMS-tutor_course_rating': |
| 389 | $reviews = tutor_utils()->get_course_reviews($args['collectionItem']['comment_ID'], 0, 100, false, ['approved'], get_current_user_id(), false); |
| 390 | $options['comment'] = count($reviews) > 0 ? $reviews[0] : []; |
| 391 | break; |
| 392 | case 'TUTOR_LMS-materials': |
| 393 | $options['material'] = $args['collectionItem']; |
| 394 | break; |
| 395 | case 'TUTOR_LMS-tutor_q_and_a': |
| 396 | $options['comment'] = tutor_utils()->get_qa_question($args['collectionItem']['comment_ID']); |
| 397 | break; |
| 398 | case 'TUTOR_LMS-announcements': |
| 399 | $options['announcement'] = get_post($args['collectionItem']['ID']); |
| 400 | break; |
| 401 | case 'TUTOR_LMS-resources': |
| 402 | $resource_id = $args['collectionItem']['id'] ?? null; |
| 403 | if ($resource_id) { |
| 404 | $resource = tutor_utils()->get_attachment_data($resource_id); |
| 405 | if ($resource) { |
| 406 | $options['resources'] = $resource; |
| 407 | } |
| 408 | } |
| 409 | break; |
| 410 | case 'TUTOR_LMS-cart': |
| 411 | $cart = new CartController(); |
| 412 | $cart_items = $cart->get_cart_items(); |
| 413 | $course_id = $args['collectionItem']['ID']; |
| 414 | $options['post'] = []; |
| 415 | |
| 416 | if ($cart_items && isset($cart_items['courses']['results']) && is_array($cart_items['courses']['results'])) { |
| 417 | foreach ($cart_items['courses']['results'] as $course) { |
| 418 | if ($course->ID === $course_id) { |
| 419 | $options['post'] = $course; |
| 420 | break; |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | default: |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | $collectionItem = $args['collectionItem']; |
| 429 | if ($collectionItem && isset($collectionItem['ID'])) { |
| 430 | $course_id = $collectionItem['ID']; |
| 431 | } elseif (isset($args['post_id'])) { |
| 432 | $course_id = $args['post_id']; |
| 433 | } |
| 434 | |
| 435 | $is_instructor = false; |
| 436 | if (! $collectionItem && isset($args['templateEditContext']['collectionType'])) { |
| 437 | $is_instructor = $args['templateEditContext']['collectionType'] === 'user'; |
| 438 | } else if ( |
| 439 | $collectionItem && $collectionItem['collectionType'] === 'users' || |
| 440 | $args['collectionProperties']['type'] === 'TUTOR_LMS-instructors' |
| 441 | ) { |
| 442 | $is_instructor = true; |
| 443 | } |
| 444 | |
| 445 | if (isset($settings['course_meta_type'])) { |
| 446 | $meta = $this->get_course_meta($settings['course_meta_type'], $course_id, $options, $settings, $is_instructor); |
| 447 | return $this->wrap_if_meta_has_label($meta, $settings); |
| 448 | } |
| 449 | if (isset($settings['thumbnail_type'])) { |
| 450 | return $this->get_course_thumbnail($settings['thumbnail_type'], $settings['thumbnail_type'], $course_id, $options); |
| 451 | } |
| 452 | } |
| 453 | return $value; |
| 454 | } |
| 455 | |
| 456 | public function add_author_image_to_data($list) |
| 457 | { |
| 458 | foreach ($list as $item) { |
| 459 | $item->author_profile_picture = ['src' => get_avatar_url($item->user_id)]; |
| 460 | } |
| 461 | return $list; |
| 462 | } |
| 463 | |
| 464 | public function add_qna_reply_flag($list) |
| 465 | { |
| 466 | foreach ($list as $item) { |
| 467 | $item->reply = true; |
| 468 | } |
| 469 | return $list; |
| 470 | } |
| 471 | } |
| 472 |