ElementGenerator
5 months ago
Ajax.php
5 months ago
Backend.php
1 year ago
Editor.php
1 year ago
Frontend.php
1 year ago
Helper.php
9 months ago
Hooks.php
5 months ago
Iframe.php
1 year ago
Pages.php
9 months ago
VisibilityCondition.php
5 months ago
Hooks.php
876 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 Tutor\Ecommerce\CheckoutController; |
| 12 | use TutorLMSDroip\ElementGenerator\CourseMetaGenerator; |
| 13 | use TutorLMSDroip\ElementGenerator\ThumbnailGenerator; |
| 14 | use Tutor\Ecommerce\CartController; |
| 15 | use TutorPro\Subscription\Models\PlanModel; |
| 16 | use TutorPro\Subscription\Subscription; |
| 17 | |
| 18 | if (! defined('ABSPATH')) { |
| 19 | exit; // Exit if accessed directly. |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Class Forntend |
| 24 | */ |
| 25 | class Hooks |
| 26 | { |
| 27 | |
| 28 | use CourseMetaGenerator; |
| 29 | use ThumbnailGenerator; |
| 30 | |
| 31 | public function __construct() |
| 32 | { |
| 33 | add_filter('droip_post_types', [$this, 'droip_post_types'], 10, 1); |
| 34 | add_filter('droip_collection_TUTOR_LMS_COURSES', [$this, 'droip_collection_TUTOR_LMS_COURSES'], 10, 2); |
| 35 | add_filter('droip_collection_TUTOR_LMS_CURRICULUM', [$this, 'droip_collection_TUTOR_LMS_CURRICULUM'], 10, 2); |
| 36 | add_filter('droip_collection_TUTOR_LMS_CART', [$this, 'droip_collection_TUTOR_LMS_CART'], 10, 2); |
| 37 | add_filter('droip_collection_TUTOR_LMS_MEMBERSHIP', [$this, 'droip_collection_TUTOR_LMS_MEMBERSHIP'], 10, 2); |
| 38 | add_filter('droip_dynamic_content', [$this, 'droip_dynamic_content'], 10, 2); |
| 39 | add_filter('droip_comment-TUTOR_LMS-tutor_q_and_a', [$this, 'modify_qna_comment_data']); |
| 40 | add_filter('droip_comment-TUTOR_LMS-tutor_course_rating', [$this, 'modify_rating_comment_data']); |
| 41 | add_filter('droip_comment_added-TUTOR_LMS-tutor_q_and_a', [$this, 'qna_comment_added']); |
| 42 | add_filter('droip_comment_added-TUTOR_LMS-tutor_course_rating', [$this, 'rating_comment_added']); |
| 43 | add_filter('droip_visibility_condition_fields', [VisibilityCondition::class, 'visibility_condition_fields'], 10, 2); |
| 44 | add_filter('droip_visibility_condition_check_' . TDE_APP_PREFIX, [VisibilityCondition::class, 'element_visibility_condition_check'], 10, 3); |
| 45 | |
| 46 | add_filter('droip_dynamic_content_fields', [$this, 'modify_droip_dynamic_content_fields'], 10, 2); |
| 47 | add_filter('droip_external_collection_options', [$this, 'modify_external_collection_options'], 10, 2); |
| 48 | add_filter('droip_external_collection_item_type', [$this, 'get_tutor_item_types'], 10, 2); |
| 49 | add_filter('droip_element_generator_radio-button', [$this, 'droip_element_generator_radio_buttons'], 10, 2); |
| 50 | |
| 51 | add_filter('droip_import_should_create_page', [$this, 'droip_import_should_create_page'], 10, 2); |
| 52 | add_action('droip_import_page_created', [$this, 'droip_import_page_created'], 10, 2); |
| 53 | |
| 54 | // $show = apply_filters('droip_show_custom_section_' . $type, true); |
| 55 | add_filter('droip_show_custom_section_header', [$this, 'show_droip_header'], 10, 1); |
| 56 | add_filter('droip_show_custom_section_footer', [$this, 'show_droip_footer'], 10, 1); |
| 57 | } |
| 58 | |
| 59 | public function show_droip_header($show) |
| 60 | { |
| 61 | $is_frontend_builder = tutor_utils()->is_tutor_frontend_dashboard( 'create-course' ); |
| 62 | if ( $is_frontend_builder ) { |
| 63 | $show = false; |
| 64 | } |
| 65 | if( $this->if_spotlight_mode_for_learning_page_enabled() ) { |
| 66 | $show = false; |
| 67 | } |
| 68 | return $show; |
| 69 | } |
| 70 | |
| 71 | public function show_droip_footer($show) |
| 72 | { |
| 73 | $is_frontend_builder = tutor_utils()->is_tutor_frontend_dashboard( 'create-course' ); |
| 74 | if ( $is_frontend_builder ) { |
| 75 | $show = false; |
| 76 | } |
| 77 | if( $this->if_spotlight_mode_for_learning_page_enabled() ) { |
| 78 | $show = false; |
| 79 | } |
| 80 | return $show; |
| 81 | } |
| 82 | |
| 83 | private function if_spotlight_mode_for_learning_page_enabled(){ |
| 84 | global $wp_query; |
| 85 | if($wp_query->is_single && ! empty( $wp_query->query_vars['post_type'] ) && in_array( $wp_query->query_vars['post_type'], ['lesson', 'tutor_quiz', 'tutor_assignments', 'tutor-google-meet', 'tutor_zoom_meeting']) ) { |
| 86 | $enable_spotlight_mode = tutor_utils()->get_option( 'enable_spotlight_mode' ); |
| 87 | if ( $enable_spotlight_mode ) { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | public function droip_import_should_create_page($flag, $old_page_data) |
| 96 | { |
| 97 | if ($old_page_data['post_type'] === 'page') { |
| 98 | $page_slug = $old_page_data['post_name']; |
| 99 | if(in_array($page_slug, ['instructor-registration', 'student-registration'])){ |
| 100 | return false; |
| 101 | } |
| 102 | if($page_slug === 'cart' && get_post(CartController::get_page_id())){ |
| 103 | return false; |
| 104 | } |
| 105 | if($page_slug === 'checkout' && get_post(CheckoutController::get_page_id())){ |
| 106 | return false; |
| 107 | } |
| 108 | |
| 109 | $dashboard_page_id = (int) tutor_utils()->dashboard_page_id(); |
| 110 | if($page_slug === 'dashboard' && get_post($dashboard_page_id)){ |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | $certificate_page_id = (int) tutor_utils()->get_option('tutor_certificate_page'); |
| 115 | if($page_slug === 'tutor-certificate' && get_post($certificate_page_id)){ |
| 116 | return false; |
| 117 | } |
| 118 | |
| 119 | $membership_page_id = (int) tutor_utils()->get_option('membership_pricing_page_id'); |
| 120 | if($page_slug === 'membership-pricing' && get_post($membership_page_id)){ |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return $flag; |
| 126 | } |
| 127 | |
| 128 | public function droip_import_page_created($new_page_id, $old_page_data) |
| 129 | { |
| 130 | // Clear Tutor LMS Cache after importing a page |
| 131 | if ($old_page_data['post_type'] === 'page') { |
| 132 | $page_slug = $old_page_data['post_name']; |
| 133 | if($page_slug === 'cart'){ |
| 134 | tutor_utils()->update_option( CartController::PAGE_ID_OPTION_NAME, $new_page_id ); |
| 135 | } |
| 136 | if($page_slug === 'checkout'){ |
| 137 | tutor_utils()->update_option( CheckoutController::PAGE_ID_OPTION_NAME, $new_page_id ); |
| 138 | } |
| 139 | if($page_slug === 'dashboard'){ |
| 140 | tutor_utils()->update_option( 'tutor_dashboard_page_id', $new_page_id ); |
| 141 | } |
| 142 | if($page_slug === 'tutor-certificate'){ |
| 143 | tutor_utils()->update_option( 'tutor_certificate_page', $new_page_id ); |
| 144 | } |
| 145 | if($page_slug === 'membership-pricing'){ |
| 146 | tutor_utils()->update_option( 'membership_pricing_page_id', $new_page_id ); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | public function modify_droip_dynamic_content_fields($fields, $collection_data) |
| 152 | { |
| 153 | if (isset($collection_data['collectionType'], $collection_data['type']) && $collection_data['collectionType'] === 'posts' && $collection_data['type'] === 'courses') { |
| 154 | if (isset($fields['typeValues'], $fields['typeValues']['content'])) { |
| 155 | foreach ($fields['typeValues']['content'] as $key => $value) { |
| 156 | if ($value['value'] === 'post') { |
| 157 | $fields['typeValues']['content'][$key]['title'] = 'Course'; |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | } else if (isset($collection_data['collectionType'], $collection_data['type']) && $collection_data['collectionType'] === 'TUTOR_LMS_COURSES' && $collection_data['type'] === 'TUTOR_LMS-subscriptions') { |
| 162 | if ($collection_data['elementContentType'] === 'content') { |
| 163 | $fields['typeValues']['content'][] = ['title' => 'Subscription', 'value' => 'TUTOR_LMS-subscriptions']; |
| 164 | $fields['typeValuesAttr']['content']['TUTOR_LMS-subscriptions'] = [ |
| 165 | ['title' => 'Plan name', 'value' => 'TUTOR_LMS-subscriptions-plan-name'], |
| 166 | ['title' => 'Plan price', 'value' => 'TUTOR_LMS-subscriptions-plan-price'], |
| 167 | ['title' => 'Plan sale price', 'value' => 'TUTOR_LMS-subscriptions-plan-sale-price'], |
| 168 | ['title' => 'Plan Recurring Interval', 'value' => 'TUTOR_LMS-subscriptions-plan-recurring-interval'], |
| 169 | ['title' => 'Enrollment Fee', 'value' => 'TUTOR_LMS-subscriptions-enrollment-fee'], |
| 170 | ['title' => 'Plan Short Description', 'value' => 'TUTOR_LMS-subscriptions-plan-short-description'], |
| 171 | ]; |
| 172 | } |
| 173 | } else if (isset($collection_data['collectionType'], $collection_data['type']) && $collection_data['collectionType'] === 'TUTOR_LMS_MEMBERSHIP' && $collection_data['type'] === 'TUTOR_LMS-membership-plans') { |
| 174 | if ($collection_data['elementContentType'] === 'content') { |
| 175 | $fields['typeValues']['content'][] = ['title' => 'Membership Plan', 'value' => 'TUTOR_LMS-membership-plans']; |
| 176 | $fields['typeValuesAttr']['content']['TUTOR_LMS-membership-plans'] = [ |
| 177 | ['title' => 'Plan name', 'value' => 'TUTOR_LMS-membership-plans-plan-name'], |
| 178 | ['title' => 'Plan price', 'value' => 'TUTOR_LMS-membership-plans-plan-price'], |
| 179 | ['title' => 'Plan sale price', 'value' => 'TUTOR_LMS-membership-plans-plan-sale-price'], |
| 180 | ['title' => 'Plan Recurring Interval', 'value' => 'TUTOR_LMS-membership-plans-plan-recurring-interval'], |
| 181 | ['title' => 'Enrollment Fee', 'value' => 'TUTOR_LMS-membership-plans-enrollment-fee'], |
| 182 | ['title' => 'Plan Short Description', 'value' => 'TUTOR_LMS-membership-plans-plan-short-description'], |
| 183 | ['title' => 'Featured Text', 'value' => 'TUTOR_LMS-membership-plans-featured-text'], |
| 184 | ['title' => 'Trial Fee', 'value' => 'TUTOR_LMS-membership-plans-trial-fee'], |
| 185 | ['title' => 'Trial Interval', 'value' => 'TUTOR_LMS-membership-plans-trial-interval'], |
| 186 | ['title' => 'Trial Value', 'value' => 'TUTOR_LMS-membership-plans-trial-value'], |
| 187 | |
| 188 | ]; |
| 189 | } |
| 190 | |
| 191 | if (isset($collection_data['elementContentType']) && $collection_data['elementContentType'] === 'anchor') { |
| 192 | $plan_model = new PlanModel(); |
| 193 | $active_membership_plans = $plan_model->get_membership_plans(PlanModel::STATUS_ACTIVE); |
| 194 | |
| 195 | if (count($active_membership_plans) > 0) { |
| 196 | $fields['typeValues']['anchor'][] = ['title' => 'Membership', 'value' => 'membership-anchor']; |
| 197 | |
| 198 | // get all active membership plans names |
| 199 | $fields['typeValuesAttr']['anchor']['membership-anchor'] = [['title' => 'Link', 'value' => 'membership-link']]; |
| 200 | } |
| 201 | } |
| 202 | } else if (isset($collection_data['collectionType'], $collection_data['type']) && $collection_data['collectionType'] === 'TUTOR_LMS_MEMBERSHIP' && $collection_data['type'] === 'TUTOR_LMS-membership-features') { |
| 203 | if ($collection_data['elementContentType'] === 'content') { |
| 204 | $fields['typeValues']['content'][] = ['title' => 'Membership Features', 'value' => 'TUTOR_LMS-membership-features']; |
| 205 | $fields['typeValuesAttr']['content']['TUTOR_LMS-membership-features'] = [ |
| 206 | ['title' => 'Content', 'value' => 'TUTOR_LMS-membership-features-feature-content'], |
| 207 | ]; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | return $fields; |
| 212 | } |
| 213 | |
| 214 | public function modify_external_collection_options($options, $args) |
| 215 | { |
| 216 | $type = $args['type']; |
| 217 | $collectionType = $args['collectionType']; |
| 218 | |
| 219 | $courses_group = [ |
| 220 | 'title' => 'Course', |
| 221 | 'value' => 'TUTOR_LMS_COURSES', |
| 222 | 'inherit' => true, |
| 223 | 'default_select_type' => "TUTOR_LMS-topics", |
| 224 | 'group' => [ |
| 225 | ['title' => 'Curriculum', 'value' => "TUTOR_LMS-topics", 'itemType' => 'post'], |
| 226 | ['title' => 'Reviews', 'value' => "TUTOR_LMS-tutor_course_rating", 'itemType' => 'comment'], |
| 227 | ['title' => 'Q & A', 'value' => "TUTOR_LMS-tutor_q_and_a", 'itemType' => 'comment'], |
| 228 | ['title' => 'Announcements', 'value' => "TUTOR_LMS-announcements", 'itemType' => 'announcement'], |
| 229 | ['title' => 'Resources', 'value' => "TUTOR_LMS-resources", 'itemType' => 'resources'], |
| 230 | ['title' => 'Gradebooks', 'value' => "TUTOR_LMS-gradebooks", 'itemType' => 'post'], |
| 231 | ['title' => 'Instructors', 'value' => "TUTOR_LMS-instructors", 'itemType' => 'user'], |
| 232 | ['title' => 'Subscriptions', 'value' => "TUTOR_LMS-subscriptions", 'itemType' => 'subscriptions'], |
| 233 | // ['title' => 'Cart', 'value' => "TUTOR_LMS-cart", 'itemType' => 'post'] //temporary commented. |
| 234 | ], |
| 235 | ]; |
| 236 | |
| 237 | $curriculum_group = [ |
| 238 | 'title' => 'Curriculum', |
| 239 | 'value' => 'TUTOR_LMS_CURRICULUM', |
| 240 | 'inherit' => true, |
| 241 | 'default_select_type' => "TUTOR_LMS-materials", |
| 242 | 'group' => [ |
| 243 | ['title' => 'Materials', 'value' => "TUTOR_LMS-materials", 'itemType' => 'material'], |
| 244 | ], |
| 245 | ]; |
| 246 | |
| 247 | $cart_group = [ |
| 248 | 'title' => 'Course', |
| 249 | 'value' => 'TUTOR_LMS_COURSES', |
| 250 | 'inherit' => true, |
| 251 | 'default_select_type' => "TUTOR_LMS-cart", |
| 252 | 'group' => [ |
| 253 | ['title' => 'Cart', 'value' => "TUTOR_LMS-cart", 'itemType' => 'post'], |
| 254 | ], |
| 255 | ]; |
| 256 | |
| 257 | $membership_group = [ |
| 258 | 'title' => 'Membership', |
| 259 | 'value' => 'TUTOR_LMS_MEMBERSHIP', |
| 260 | 'inherit' => true, |
| 261 | 'default_select_type' => "TUTOR_LMS-membership-plans", |
| 262 | 'group' => [ |
| 263 | ['title' => 'Plans', 'value' => "TUTOR_LMS-membership-plans", 'itemType' => 'membership-plan'], |
| 264 | ['title' => 'Features', 'value' => "TUTOR_LMS-membership-features", 'itemType' => 'membership-feature'], |
| 265 | ], |
| 266 | ]; |
| 267 | |
| 268 | if ($collectionType === 'posts' && $type === 'courses') { |
| 269 | $options[] = $courses_group; |
| 270 | } else if ($collectionType === 'TUTOR_LMS_COURSES' && $type === 'TUTOR_LMS-topics') { |
| 271 | $options[] = $curriculum_group; |
| 272 | } else { |
| 273 | // $options[] = $cart_group; //temporary commented. |
| 274 | } |
| 275 | |
| 276 | $options[] = $membership_group; |
| 277 | |
| 278 | return $options; |
| 279 | } |
| 280 | |
| 281 | public function get_tutor_item_types($oldItemType, $collectionName) |
| 282 | { |
| 283 | switch ($collectionName) { |
| 284 | case 'TUTOR_LMS-tutor_course_rating': |
| 285 | return 'comment'; |
| 286 | case 'TUTOR_LMS-materials': |
| 287 | return 'material'; |
| 288 | case 'TUTOR_LMS-tutor_q_and_a': |
| 289 | return 'comment'; |
| 290 | case 'TUTOR_LMS-announcements': |
| 291 | return 'announcement'; |
| 292 | case 'TUTOR_LMS-resources': |
| 293 | return 'resources'; |
| 294 | default: |
| 295 | return $oldItemType; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | public function modify_rating_comment_data($value) |
| 300 | { |
| 301 | $moderation = tutor_utils()->get_option('enable_course_review_moderation', false, true, true); |
| 302 | $value['comment_agent'] = 'TutorLMSPlugin'; |
| 303 | $value['comment_type'] = 'tutor_course_rating'; |
| 304 | $value['comment_approved'] = $moderation ? 'hold' : 'approved'; |
| 305 | return $value; |
| 306 | } |
| 307 | |
| 308 | public function modify_qna_comment_data($value) |
| 309 | { |
| 310 | $value['comment_agent'] = 'TutorLMSPlugin'; |
| 311 | $value['comment_type'] = 'tutor_q_and_a'; |
| 312 | $value['comment_approved'] = 'approved'; |
| 313 | return $value; |
| 314 | } |
| 315 | |
| 316 | public function rating_comment_added($value) |
| 317 | { |
| 318 | $comment_ID = $value['comment_ID']; |
| 319 | $form_data = $value['form_data']; |
| 320 | $rating = isset($form_data['rating']) ? sanitize_text_field($form_data['rating']) : 0; |
| 321 | if (! $comment_ID) { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | global $wpdb; |
| 326 | $rating_info = $wpdb->get_row( |
| 327 | $wpdb->prepare( |
| 328 | "SELECT * FROM {$wpdb->commentmeta} |
| 329 | WHERE comment_id = %d |
| 330 | AND meta_key = 'tutor_rating'; ", |
| 331 | $comment_ID |
| 332 | ) |
| 333 | ); |
| 334 | if ($rating_info) { |
| 335 | $wpdb->update( |
| 336 | $wpdb->commentmeta, |
| 337 | ['meta_value' => $rating], |
| 338 | [ |
| 339 | 'comment_id' => $comment_ID, |
| 340 | 'meta_key' => 'tutor_rating', |
| 341 | ] |
| 342 | ); |
| 343 | } else { |
| 344 | $wpdb->insert( |
| 345 | $wpdb->commentmeta, |
| 346 | [ |
| 347 | 'comment_id' => $comment_ID, |
| 348 | 'meta_key' => 'tutor_rating', |
| 349 | 'meta_value' => $rating, |
| 350 | ] |
| 351 | ); |
| 352 | } |
| 353 | do_action('tutor_after_rating_placed', $comment_ID); |
| 354 | } |
| 355 | |
| 356 | public function qna_comment_added($value) |
| 357 | { |
| 358 | $form_data = $value['form_data']; |
| 359 | $comment_parent = isset($form_data['comment_parent']) ? sanitize_text_field($form_data['comment_parent']) : 0; |
| 360 | if (! $comment_parent) { |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | global $wpdb; |
| 365 | $parent_q_author_id = $wpdb->get_var( |
| 366 | $wpdb->prepare( |
| 367 | "SELECT user_id |
| 368 | FROM {$wpdb->comments} |
| 369 | WHERE comment_ID = %d |
| 370 | ", |
| 371 | $comment_parent |
| 372 | ) |
| 373 | ); |
| 374 | |
| 375 | $asker_id = $value['user_id']; |
| 376 | $self = $asker_id == $parent_q_author_id; |
| 377 | update_comment_meta($parent_q_author_id, 'tutor_qna_read' . ($self ? '' : '_' . $asker_id), 0); |
| 378 | } |
| 379 | |
| 380 | public function droip_post_types($post_types) |
| 381 | { |
| 382 | $post_types[] = [ |
| 383 | 'title' => 'Course Topics', |
| 384 | 'value' => 'topics', |
| 385 | ]; |
| 386 | return $post_types; |
| 387 | } |
| 388 | public function droip_collection_TUTOR_LMS_COURSES($value, $args) |
| 389 | { |
| 390 | $context = isset($args['context']) ? $args['context'] : false; |
| 391 | if ($context && isset($context['collectionType'])) { |
| 392 | $collectionType = $context['collectionType']; // post/tags/users |
| 393 | if ($collectionType === 'post') { |
| 394 | if ($args['name'] === 'TUTOR_LMS-topics') { |
| 395 | $args['post_parent'] = $context['id']; |
| 396 | $args['inherit'] = true; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | if ($args['name'] === 'TUTOR_LMS-topics') { |
| 401 | $args['name'] = 'topics'; |
| 402 | $args['item_per_page'] = -1; |
| 403 | // $obj = HelperFunctions::get_posts($args); |
| 404 | // $obj['itemType'] = 'post'; |
| 405 | // return $obj; |
| 406 | return [ |
| 407 | 'data' => tutor_utils()->get_topics($args['post_parent'])->posts, |
| 408 | 'pagination' => null, |
| 409 | 'itemType' => 'post', |
| 410 | ]; |
| 411 | } elseif ($args['name'] === 'TUTOR_LMS-tutor_course_rating') { |
| 412 | $reviews = tutor_utils()->get_course_reviews($args['post_parent'], 0, 100, false, ['approved'], get_current_user_id()); |
| 413 | $reviews = $this->add_author_image_to_data($reviews); |
| 414 | return [ |
| 415 | 'data' => $reviews, |
| 416 | 'pagination' => [], |
| 417 | 'itemType' => 'comment', |
| 418 | ]; |
| 419 | } elseif ($args['name'] === 'TUTOR_LMS-materials') { |
| 420 | $topic_contents = tutor_utils()->get_course_contents_by_topic($args['post_parent'], -1); |
| 421 | $obj['data'] = $topic_contents->posts; |
| 422 | $obj['pagination'] = []; |
| 423 | $obj['itemType'] = 'post'; |
| 424 | return $obj; |
| 425 | } elseif ($args['name'] === 'TUTOR_LMS-tutor_q_and_a') { |
| 426 | if ($args['post_parent'] == 0) { |
| 427 | return [ |
| 428 | 'data' => [], |
| 429 | 'pagination' => [], |
| 430 | 'itemType' => 'comment', |
| 431 | ]; |
| 432 | } |
| 433 | |
| 434 | if (isset($args['context']) && isset($args['context']['comment_ID'])) { |
| 435 | $q_id = $args['context']['comment_ID']; |
| 436 | $answer = tutor_utils()->get_qa_answer_by_question($q_id); |
| 437 | $filteredAnswers = array_values( |
| 438 | array_filter( |
| 439 | $answer, |
| 440 | function ($obj) use ($q_id) { |
| 441 | return $obj->comment_ID !== $q_id; |
| 442 | } |
| 443 | ) |
| 444 | ); |
| 445 | $filteredAnswers = $this->add_author_image_to_data($filteredAnswers); |
| 446 | $filteredAnswers = $this->add_qna_reply_flag($filteredAnswers); |
| 447 | return [ |
| 448 | 'data' => $filteredAnswers, |
| 449 | 'pagination' => [], |
| 450 | 'itemType' => 'comment', |
| 451 | ]; |
| 452 | } |
| 453 | |
| 454 | $questions = tutor_utils()->get_qa_questions(0, 100, '', null, null, null, null, false, ['course_id' => $args['post_parent']]); |
| 455 | $questions = $this->add_author_image_to_data($questions); |
| 456 | return [ |
| 457 | 'data' => $questions, |
| 458 | 'pagination' => [], |
| 459 | 'itemType' => 'comment', |
| 460 | ]; |
| 461 | } elseif ($args['name'] === 'TUTOR_LMS-announcements') { |
| 462 | $announcements = tutor_utils()->get_announcements($args['post_parent']); |
| 463 | return [ |
| 464 | 'data' => $announcements, |
| 465 | 'pagination' => [], |
| 466 | 'itemType' => 'announcement', |
| 467 | ]; |
| 468 | } elseif ($args['name'] === 'TUTOR_LMS-resources') { |
| 469 | $resources = tutor_utils()->get_attachments($args['post_parent']); |
| 470 | return [ |
| 471 | 'data' => $resources, |
| 472 | 'pagination' => [], |
| 473 | 'itemType' => 'resources', |
| 474 | ]; |
| 475 | } elseif ($args['name'] === 'TUTOR_LMS-instructors') { |
| 476 | $instructors = tutor_utils()->get_instructors_by_course($args['post_parent']); |
| 477 | $instructors = array_map(fn($user) => (array) array_merge((array) $user, [ |
| 478 | 'user_url' => get_author_posts_url($user->ID), |
| 479 | 'profile_image' => get_avatar_url($user->ID), |
| 480 | ]), $instructors); |
| 481 | |
| 482 | return [ |
| 483 | 'data' => $instructors, |
| 484 | 'pagination' => [], |
| 485 | 'itemType' => 'user', |
| 486 | ]; |
| 487 | } elseif ($args['name'] === 'TUTOR_LMS-cart') { |
| 488 | $cart = new CartController(); |
| 489 | $cart_items = $cart->get_cart_items(); |
| 490 | $courses = isset($cart_items['courses'], $cart_items['courses']['results']) ? $cart_items['courses']['results'] : []; |
| 491 | return [ |
| 492 | 'data' => $courses, |
| 493 | 'pagination' => null, |
| 494 | 'itemType' => 'post', |
| 495 | ]; |
| 496 | } else if ($args['name'] === 'TUTOR_LMS-subscriptions') { |
| 497 | if (tutor()->has_pro && Subscription::is_enabled()) { |
| 498 | $plan_model = new PlanModel(); |
| 499 | $items = $plan_model->get_subscription_plans($args['post_parent'], PlanModel::STATUS_ACTIVE); // get all active subscription plans |
| 500 | return [ |
| 501 | 'data' => $items, |
| 502 | 'pagination' => null, |
| 503 | 'itemType' => 'TUTOR_LMS-subscriptions', |
| 504 | ]; |
| 505 | } |
| 506 | } else { |
| 507 | return [ |
| 508 | 'data' => [], |
| 509 | 'pagination' => [], |
| 510 | 'itemType' => false, |
| 511 | ]; |
| 512 | } |
| 513 | |
| 514 | return $value; |
| 515 | } |
| 516 | |
| 517 | public function droip_collection_TUTOR_LMS_CURRICULUM($value, $args) |
| 518 | { |
| 519 | if ($args['name'] === 'TUTOR_LMS-materials' && isset($args['post_parent'])) { |
| 520 | $topic_contents = tutor_utils()->get_course_contents_by_topic($args['post_parent'], -1); |
| 521 | $obj['data'] = $topic_contents->posts; |
| 522 | $obj['pagination'] = []; |
| 523 | $obj['itemType'] = 'post'; |
| 524 | return $obj; |
| 525 | } else { |
| 526 | return [ |
| 527 | 'data' => [], |
| 528 | 'pagination' => [], |
| 529 | 'itemType' => false, |
| 530 | ]; |
| 531 | } |
| 532 | |
| 533 | return $value; |
| 534 | } |
| 535 | |
| 536 | public function droip_collection_TUTOR_LMS_MEMBERSHIP($value, $args) |
| 537 | { |
| 538 | if ($args['name'] === 'TUTOR_LMS-membership-plans') { |
| 539 | $plan_model = new PlanModel(); |
| 540 | $active_membership_plans = $plan_model->get_membership_plans(PlanModel::STATUS_ACTIVE); |
| 541 | |
| 542 | return [ |
| 543 | 'data' => $active_membership_plans, |
| 544 | 'pagination' => null, |
| 545 | 'itemType' => 'membership-plan', |
| 546 | ]; |
| 547 | } elseif ($args['name'] === 'TUTOR_LMS-membership-features' && isset($args['post_parent'])) { |
| 548 | $plan_id = $args['post_parent']; |
| 549 | |
| 550 | if (isset($args['parent_item_type']) && $args['parent_item_type'] === 'membership-plan') { |
| 551 | if (isset($args['parent_item'], $args['parent_item']->id)) { |
| 552 | $plan_id = $args['parent_item']->id; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | $plan_model = new PlanModel(); |
| 557 | $active_membership_plans = $plan_model->get_membership_plans(PlanModel::STATUS_ACTIVE); |
| 558 | |
| 559 | $plan_details = null; |
| 560 | foreach ($active_membership_plans as $plan) { |
| 561 | if ($plan->id === $plan_id) { |
| 562 | $plan_details = $plan; |
| 563 | break; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | if (! $plan_details) { |
| 568 | return [ |
| 569 | 'data' => [], |
| 570 | 'pagination' => [], |
| 571 | 'itemType' => false, |
| 572 | ]; |
| 573 | } |
| 574 | |
| 575 | $features = isset($plan_details->description) ? json_decode($plan_details->description, true) : []; |
| 576 | |
| 577 | |
| 578 | return [ |
| 579 | 'data' => $features, |
| 580 | 'pagination' => null, |
| 581 | 'itemType' => 'membership-feature', |
| 582 | ]; |
| 583 | } else { |
| 584 | return [ |
| 585 | 'data' => [], |
| 586 | 'pagination' => [], |
| 587 | 'itemType' => false, |
| 588 | ]; |
| 589 | } |
| 590 | |
| 591 | return $value; |
| 592 | } |
| 593 | |
| 594 | public function droip_dynamic_content($value, $args) |
| 595 | { |
| 596 | if (isset($args['dynamicContent'])) { |
| 597 | $dynamicContent = $args['dynamicContent']; |
| 598 | |
| 599 | if ($dynamicContent['type'] === 'course') { |
| 600 | $collectionItem = $args['collectionItem']; |
| 601 | if ($collectionItem && isset($collectionItem['ID'])) { |
| 602 | $course_id = $collectionItem['ID']; |
| 603 | } elseif (isset($args['post_id'])) { |
| 604 | $course_id = $args['post_id']; |
| 605 | } |
| 606 | |
| 607 | if ($dynamicContent['value'] === 'thumbnail_image') { |
| 608 | $tutor_course_img = get_tutor_course_thumbnail_src('post-thumbnail', $course_id); |
| 609 | if ($tutor_course_img) { |
| 610 | return $tutor_course_img; |
| 611 | } |
| 612 | } elseif ($dynamicContent['value'] === 'thumbnail_video') { |
| 613 | $video_info = tutor_utils()->get_video_info($course_id); |
| 614 | $source_key = is_object($video_info) ? 'source_' . $video_info->source : null; |
| 615 | if ($source_key) { |
| 616 | return ['url' => $video_info->$source_key]; |
| 617 | } |
| 618 | } |
| 619 | } else if ($dynamicContent['type'] === 'TUTOR_LMS-subscriptions') { |
| 620 | $collectionItem = isset($args['collectionItem']) ? $args['collectionItem'] : false; |
| 621 | $plan_id = false; |
| 622 | if ($collectionItem && isset($collectionItem['id'])) { |
| 623 | $plan_id = $collectionItem['id']; |
| 624 | } elseif (isset($args['options'], $args['options']['TUTOR_LMS-subscriptions'])) { |
| 625 | $plan_id = $args['options']['TUTOR_LMS-subscriptions']->id; |
| 626 | } |
| 627 | |
| 628 | $plan_model = new PlanModel(); |
| 629 | $plan_details = $plan_model->get_plan($plan_id); |
| 630 | if (!$plan_details) { |
| 631 | return "No Plan"; |
| 632 | } |
| 633 | if ($dynamicContent['value'] === 'TUTOR_LMS-subscriptions-plan-name') { |
| 634 | if ($plan_details) { |
| 635 | return $plan_details->plan_name; |
| 636 | } |
| 637 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-subscriptions-plan-price') { |
| 638 | if ($plan_details) { |
| 639 | return tutor_get_formatted_price($plan_details->regular_price); |
| 640 | } |
| 641 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-subscriptions-plan-sale-price') { |
| 642 | if ($plan_details) { |
| 643 | return tutor_get_formatted_price($plan_details->sale_price); |
| 644 | } |
| 645 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-subscriptions-plan-recurring-interval') { |
| 646 | if ($plan_details) { |
| 647 | return isset($plan_details->recurring_interval) ? $plan_details->recurring_interval : 'N/A'; |
| 648 | } |
| 649 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-subscriptions-enrollment-fee') { |
| 650 | if ($plan_details) { |
| 651 | return tutor_get_formatted_price($plan_details->enrollment_fee); |
| 652 | } |
| 653 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-subscriptions-plan-short-description') { |
| 654 | if ($plan_details) { |
| 655 | return wp_kses_post($plan_details->short_description); |
| 656 | } |
| 657 | } |
| 658 | } else if ($dynamicContent['type'] === 'TUTOR_LMS-membership-plans') { |
| 659 | $collectionItem = isset($args['collectionItem']) ? $args['collectionItem'] : false; |
| 660 | $plan_id = false; |
| 661 | |
| 662 | if ($collectionItem && isset($collectionItem['id'])) { |
| 663 | $plan_id = $collectionItem['id']; |
| 664 | } elseif (isset($args['options'], $args['options']['membership-plan'])) { |
| 665 | $plan_id = $args['options']['membership-plan']->id; |
| 666 | } |
| 667 | |
| 668 | $plan_model = new PlanModel(); |
| 669 | $plan_details = $plan_model->get_plan($plan_id); |
| 670 | |
| 671 | if (!$plan_details) { |
| 672 | return "No Plan"; |
| 673 | } |
| 674 | |
| 675 | if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-plan-name') { |
| 676 | if ($plan_details) { |
| 677 | return $plan_details->plan_name; |
| 678 | } |
| 679 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-plan-price') { |
| 680 | if ($plan_details) { |
| 681 | return tutor_get_formatted_price($plan_details->regular_price); |
| 682 | } |
| 683 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-plan-sale-price') { |
| 684 | if ($plan_details) { |
| 685 | return tutor_get_formatted_price($plan_details->sale_price); |
| 686 | } |
| 687 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-plan-recurring-interval') { |
| 688 | if ($plan_details) { |
| 689 | return isset($plan_details->recurring_interval) ? $plan_details->recurring_interval : 'N/A'; |
| 690 | } |
| 691 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-enrollment-fee') { |
| 692 | if ($plan_details) { |
| 693 | return tutor_get_formatted_price($plan_details->enrollment_fee); |
| 694 | } |
| 695 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-plan-short-description') { |
| 696 | if ($plan_details) { |
| 697 | return wp_kses_post($plan_details->short_description); |
| 698 | } |
| 699 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-featured-text') { |
| 700 | if ($plan_details) { |
| 701 | return sanitize_text_field($plan_details->featured_text); |
| 702 | } |
| 703 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-trial-fee') { |
| 704 | if ($plan_details) { |
| 705 | return tutor_get_formatted_price($plan_details->trial_fee); |
| 706 | } |
| 707 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-trial-interval') { |
| 708 | if ($plan_details) { |
| 709 | return isset($plan_details->trial_interval) ? $plan_details->trial_interval : 'N/A'; |
| 710 | } |
| 711 | } else if ($dynamicContent['value'] === 'TUTOR_LMS-membership-plans-trial-value') { |
| 712 | if ($plan_details) { |
| 713 | return isset($plan_details->trial_value) ? intval($plan_details->trial_value) : 'N/A'; |
| 714 | } |
| 715 | } |
| 716 | } else if ($dynamicContent['type'] === 'TUTOR_LMS-membership-features') { |
| 717 | $collectionItem = isset($args['collectionItem']) ? $args['collectionItem'] : false; |
| 718 | $feature_id = false; |
| 719 | |
| 720 | if ($collectionItem && isset($collectionItem['id'])) { |
| 721 | $feature_id = $collectionItem['id']; |
| 722 | } elseif (isset($args['options'], $args['options']['membership-feature'])) { |
| 723 | $feature_id = $args['options']['membership-feature']['id']; |
| 724 | } |
| 725 | |
| 726 | $plan_model = new PlanModel(); |
| 727 | $active_membership_plans = $plan_model->get_membership_plans(PlanModel::STATUS_ACTIVE); |
| 728 | |
| 729 | foreach ($active_membership_plans as $plan) { |
| 730 | $features = json_decode($plan->description, true); |
| 731 | foreach ($features as $feature) { |
| 732 | if ($feature['id'] == $feature_id) { |
| 733 | if ($dynamicContent['value'] === 'TUTOR_LMS-membership-features-feature-content') { |
| 734 | return isset($feature['content']) ? wp_kses_post($feature['content']) : ''; |
| 735 | } |
| 736 | } |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | return ''; |
| 741 | } else if ($dynamicContent['type'] === 'membership-anchor') { |
| 742 | if ($dynamicContent['value'] === 'membership-link') { |
| 743 | $collectionItem = isset($args['collectionItem']) ? $args['collectionItem'] : false; |
| 744 | |
| 745 | $plan_id = false; |
| 746 | $url = '#'; |
| 747 | |
| 748 | if (is_user_logged_in() && isset($args['options'], $args['options']['membership-plan'])) { |
| 749 | $plan_id = $args['options']['membership-plan']->id; |
| 750 | $checkout_link = CheckoutController::get_page_url(); |
| 751 | |
| 752 | if ($checkout_link) { |
| 753 | $url = add_query_arg('plan', $plan_id, $checkout_link); |
| 754 | } |
| 755 | } else if (!is_user_logged_in()) { |
| 756 | $url = wp_login_url(wp_get_referer()); |
| 757 | } |
| 758 | |
| 759 | return $url; |
| 760 | } |
| 761 | } |
| 762 | } elseif (isset($args['settings'])) { |
| 763 | $settings = $args['settings']; |
| 764 | $options = []; |
| 765 | switch ($args['collectionProperties']['type']) { |
| 766 | case 'TUTOR_LMS-tutor_course_rating': |
| 767 | $reviews = tutor_utils()->get_course_reviews($args['collectionItem']['comment_ID'], 0, 100, false, ['approved'], get_current_user_id(), false); |
| 768 | $options['comment'] = count($reviews) > 0 ? $reviews[0] : []; |
| 769 | break; |
| 770 | case 'TUTOR_LMS-materials': |
| 771 | $options['material'] = $args['collectionItem']; |
| 772 | break; |
| 773 | case 'TUTOR_LMS-tutor_q_and_a': |
| 774 | $options['comment'] = tutor_utils()->get_qa_question($args['collectionItem']['comment_ID']); |
| 775 | break; |
| 776 | case 'TUTOR_LMS-announcements': |
| 777 | $options['announcement'] = get_post($args['collectionItem']['ID']); |
| 778 | break; |
| 779 | case 'TUTOR_LMS-resources': |
| 780 | $resource_id = $args['collectionItem']['id'] ?? null; |
| 781 | if ($resource_id) { |
| 782 | $resource = tutor_utils()->get_attachment_data($resource_id); |
| 783 | if ($resource) { |
| 784 | $options['resources'] = $resource; |
| 785 | } |
| 786 | } |
| 787 | break; |
| 788 | case 'TUTOR_LMS-cart': |
| 789 | $cart = new CartController(); |
| 790 | $cart_items = $cart->get_cart_items(); |
| 791 | $course_id = $args['collectionItem']['ID']; |
| 792 | $options['post'] = []; |
| 793 | |
| 794 | if ($cart_items && isset($cart_items['courses']['results']) && is_array($cart_items['courses']['results'])) { |
| 795 | foreach ($cart_items['courses']['results'] as $course) { |
| 796 | if ($course->ID === $course_id) { |
| 797 | $options['post'] = $course; |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | default: |
| 803 | break; |
| 804 | } |
| 805 | |
| 806 | $collectionItem = $args['collectionItem']; |
| 807 | if ($collectionItem && isset($collectionItem['ID'])) { |
| 808 | $course_id = $collectionItem['ID']; |
| 809 | } elseif (isset($args['post_id'])) { |
| 810 | $course_id = $args['post_id']; |
| 811 | } |
| 812 | |
| 813 | $is_instructor = false; |
| 814 | if (! $collectionItem && isset($args['templateEditContext']['collectionType'])) { |
| 815 | $is_instructor = $args['templateEditContext']['collectionType'] === 'user'; |
| 816 | } else if ( |
| 817 | $collectionItem && $collectionItem['collectionType'] === 'users' || |
| 818 | $args['collectionProperties']['type'] === 'TUTOR_LMS-instructors' |
| 819 | ) { |
| 820 | $is_instructor = true; |
| 821 | } |
| 822 | |
| 823 | if (isset($settings['course_meta_type'])) { |
| 824 | $meta = $this->get_course_meta($settings['course_meta_type'], $course_id, $options, $settings, $is_instructor); |
| 825 | return $this->wrap_if_meta_has_label($meta, $settings); |
| 826 | } |
| 827 | if (isset($settings['thumbnail_type'])) { |
| 828 | return $this->get_course_thumbnail($settings['thumbnail_type'], $settings['thumbnail_type'], $course_id, $options); |
| 829 | } |
| 830 | } |
| 831 | return $value; |
| 832 | } |
| 833 | |
| 834 | public function add_author_image_to_data($list) |
| 835 | { |
| 836 | foreach ($list as $item) { |
| 837 | $item->author_profile_picture = ['src' => get_avatar_url($item->user_id)]; |
| 838 | } |
| 839 | return $list; |
| 840 | } |
| 841 | |
| 842 | public function add_qna_reply_flag($list) |
| 843 | { |
| 844 | foreach ($list as $item) { |
| 845 | $item->reply = true; |
| 846 | } |
| 847 | return $list; |
| 848 | } |
| 849 | |
| 850 | public function droip_element_generator_radio_buttons($value, $options) |
| 851 | { |
| 852 | if (isset($options['options'], $options['options']['TUTOR_LMS-subscriptions'])) { |
| 853 | |
| 854 | $course_id = isset($options['options']['post']) ? $options['options']['post']->ID : get_the_ID(); |
| 855 | $attributes = isset($options['attributes']) ? $options['attributes'] : ''; |
| 856 | |
| 857 | $tag = $options['element']['properties']['tag'] ?? 'div'; |
| 858 | |
| 859 | $name = 'name="course_' . $course_id . '_subscription_plan"'; |
| 860 | |
| 861 | $extra_attributes = 'data-subscription_id="' . $options['options']['TUTOR_LMS-subscriptions']->id . '"'; |
| 862 | |
| 863 | |
| 864 | // Set checked for first item |
| 865 | $checked = ''; |
| 866 | if (isset($options['options']['item_index']) && $options['options']['item_index'] === 1) { |
| 867 | $checked = 'checked="checked"'; |
| 868 | } |
| 869 | |
| 870 | return "<$tag $name $attributes $extra_attributes $checked />"; |
| 871 | } |
| 872 | |
| 873 | return $value; |
| 874 | } |
| 875 | } |
| 876 |