ActionsGenerator.php
7 months ago
AddRatingGenerator.php
1 year ago
CourseMetaGenerator.php
9 months ago
ElementGenerator.php
1 year ago
MaterialGenerator.php
1 year ago
Preview.php
1 year ago
PriceGenerator.php
7 months ago
SocialLinkGenerator.php
1 year ago
ThumbnailGenerator.php
1 year ago
ActionsGenerator.php
363 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Preview script for html markup generator |
| 5 | * |
| 6 | * @package tutor-droip-elements |
| 7 | */ |
| 8 | |
| 9 | namespace TutorLMSDroip\ElementGenerator; |
| 10 | |
| 11 | use TUTOR\Course; |
| 12 | use TUTOR_CERT\Certificate; |
| 13 | use TutorPro\Subscription\Subscription; |
| 14 | use TutorPro\Subscription\Models\PlanModel; |
| 15 | use TutorPro\Subscription\Models\SubscriptionModel; |
| 16 | use Tutor\Ecommerce\CheckoutController; |
| 17 | use TutorPro\GiftCourse\GiftCourse; |
| 18 | use TutorPro\Subscription\Settings; |
| 19 | |
| 20 | if (! defined('ABSPATH')) { |
| 21 | exit; // Exit if accessed directly. |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Class ActionsGenerator |
| 26 | * This class is used to define all helper functions. |
| 27 | * |
| 28 | * @package TutorLMSDroip\ElementGenerator |
| 29 | */ |
| 30 | trait ActionsGenerator |
| 31 | { |
| 32 | |
| 33 | /** |
| 34 | * Generate actionbox markup |
| 35 | * |
| 36 | * @return string |
| 37 | */ |
| 38 | private function generate_action_markup() |
| 39 | { |
| 40 | $course_id = isset($this->options['post']) ? $this->options['post']->ID : get_the_ID(); |
| 41 | $ele_name = $this->element['name']; |
| 42 | $entry_box_button_logic = tutor_entry_box_buttons($course_id); |
| 43 | $type = isset($this->properties['type']) ? $this->properties['type'] : 'enroll_btn'; |
| 44 | $extra_attributes = "data-course_id='$course_id' data-action_type='$type'"; |
| 45 | |
| 46 | $selling_option = Course::get_selling_option($course_id); |
| 47 | if (!$selling_option) { |
| 48 | $selling_option = Course::SELLING_OPTION_ALL; |
| 49 | } |
| 50 | |
| 51 | switch ($type) { |
| 52 | case 'wishlist_btn': { |
| 53 | if (! is_user_logged_in()) { |
| 54 | return ''; |
| 55 | } |
| 56 | $is_wish_listed = tutor_utils()->is_wishlisted($course_id, get_current_user_id()); |
| 57 | if ($is_wish_listed) { |
| 58 | return ''; |
| 59 | } |
| 60 | |
| 61 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 62 | } |
| 63 | case 'wishlisted_btn': { |
| 64 | if (! is_user_logged_in()) { |
| 65 | return ''; |
| 66 | } |
| 67 | $is_wish_listed = tutor_utils()->is_wishlisted($course_id, get_current_user_id()); |
| 68 | if (! $is_wish_listed) { |
| 69 | return ''; |
| 70 | } |
| 71 | |
| 72 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | $entry_box_button_logic = $this->update_entry_box_button_logic($entry_box_button_logic, $this->options); |
| 77 | |
| 78 | if (! isset($entry_box_button_logic->{'show_' . $type}) || (isset($entry_box_button_logic->{'show_' . $type}) && $entry_box_button_logic->{'show_' . $type} !== true)) { |
| 79 | return ''; |
| 80 | } |
| 81 | switch ($type) { |
| 82 | case 'enroll_btn': { |
| 83 | if (! $entry_box_button_logic->show_enroll_btn) { |
| 84 | return ''; |
| 85 | } |
| 86 | |
| 87 | if (tutor()->has_pro && Subscription::is_enabled()) { |
| 88 | $subscription_model = new SubscriptionModel(); |
| 89 | $tutor_subscription_enrollment = false; |
| 90 | |
| 91 | // For hybrid mode. |
| 92 | if (Course::PRICE_TYPE_PAID === tutor_utils()->price_type($course_id) && $subscription_model->has_course_access($course_id)) { |
| 93 | $tutor_subscription_enrollment = true; |
| 94 | } |
| 95 | |
| 96 | // For membership only mode. |
| 97 | if (Settings::membership_only_mode_enabled() && $subscription_model->has_course_access($course_id)) { |
| 98 | $tutor_subscription_enrollment = true; |
| 99 | } |
| 100 | |
| 101 | if ($tutor_subscription_enrollment) { |
| 102 | $extra_attributes .= " data-tutor_subscription_enrollment='true'"; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 108 | } |
| 109 | |
| 110 | case 'add_to_cart_btn': { |
| 111 | $is_course_in_user_cart = tutor_is_item_in_cart($course_id); |
| 112 | if ($is_course_in_user_cart) { |
| 113 | return ''; |
| 114 | } |
| 115 | |
| 116 | if ($selling_option === Course::SELLING_OPTION_ALL || $selling_option === Course::SELLING_OPTION_ONE_TIME || $selling_option === Course::SELLING_OPTION_BOTH) { |
| 117 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 118 | } |
| 119 | return ""; |
| 120 | } |
| 121 | |
| 122 | case 'remove_from_cart_btn': { |
| 123 | $is_course_in_user_cart = tutor_is_item_in_cart($course_id); |
| 124 | if (!$is_course_in_user_cart) { |
| 125 | return ''; |
| 126 | } |
| 127 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 128 | } |
| 129 | |
| 130 | case 'view_cart_btn': { |
| 131 | $is_course_in_user_cart = tutor_is_item_in_cart($course_id); |
| 132 | if (! $is_course_in_user_cart) { |
| 133 | return ''; |
| 134 | } |
| 135 | $extra_attributes .= " data-cart_url='" . tutor_get_cart_url() . "'"; |
| 136 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 137 | } |
| 138 | |
| 139 | case 'start_learning_btn': { |
| 140 | if (! $entry_box_button_logic->show_start_learning_btn) { |
| 141 | return ''; |
| 142 | } |
| 143 | $is_course_completed = tutor_utils()->is_completed_course($course_id, get_current_user_id()); |
| 144 | if ($is_course_completed) { |
| 145 | return ''; |
| 146 | } |
| 147 | $lession_url = tutor_utils()->get_course_first_lesson($course_id); |
| 148 | $extra_attributes .= " data-lession_url=$lession_url"; |
| 149 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 150 | } |
| 151 | |
| 152 | case 'continue_learning_btn': { |
| 153 | if (! $entry_box_button_logic->show_continue_learning_btn) { |
| 154 | return ''; |
| 155 | } |
| 156 | $is_course_completed = tutor_utils()->is_completed_course($course_id, get_current_user_id()); |
| 157 | if ($is_course_completed) { |
| 158 | return ''; |
| 159 | } |
| 160 | $extra_attributes .= " data-continue_learning_url='" . tutor_utils()->get_course_first_lesson() . "'"; |
| 161 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 162 | } |
| 163 | |
| 164 | case 'complete_course_btn': { |
| 165 | if (! $entry_box_button_logic->show_complete_course_btn) { |
| 166 | return ''; |
| 167 | } |
| 168 | $is_course_completed = tutor_utils()->is_completed_course($course_id, get_current_user_id()); |
| 169 | if ($is_course_completed) { |
| 170 | return ''; |
| 171 | } |
| 172 | |
| 173 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 174 | } |
| 175 | |
| 176 | case 'retake_course_btn': { |
| 177 | if ($entry_box_button_logic->show_retake_course_btn || ($entry_box_button_logic->show_certificate_view_btn && function_exists('TUTOR_CERT'))) { |
| 178 | $extra_attributes .= " data-continue_learning_url='" . tutor_utils()->get_course_first_lesson() . "'"; |
| 179 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 180 | } |
| 181 | |
| 182 | return ""; |
| 183 | } |
| 184 | case 'certificate_view_btn': { |
| 185 | if (! function_exists('TUTOR_CERT')) { |
| 186 | return ''; |
| 187 | } |
| 188 | if (! $entry_box_button_logic->show_certificate_view_btn) { |
| 189 | return ''; |
| 190 | } |
| 191 | $is_course_completed = tutor_utils()->is_completed_course($course_id, get_current_user_id()); |
| 192 | |
| 193 | if (! $is_course_completed) { |
| 194 | return ''; |
| 195 | } |
| 196 | if (! $course_id) { |
| 197 | return ''; |
| 198 | } |
| 199 | |
| 200 | if (tutils()->is_addon_enabled(TUTOR_CERT()->basename)) { |
| 201 | $has_course_certificate_template = (new Certificate(true))->has_course_certificate_template($course_id); |
| 202 | if (!$has_course_certificate_template) { |
| 203 | return ""; |
| 204 | } |
| 205 | |
| 206 | $certificate_url = ''; |
| 207 | $certificate_url = (new Certificate(true))->get_certificate($course_id); |
| 208 | |
| 209 | $extra_attributes .= " data-certificate_url='" . $certificate_url . "'"; |
| 210 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 211 | } |
| 212 | |
| 213 | return ''; |
| 214 | } |
| 215 | |
| 216 | case 'subscribe_now_btn': { |
| 217 | $checkout_link = CheckoutController::get_page_url(); |
| 218 | |
| 219 | |
| 220 | if (is_user_logged_in()) { |
| 221 | $extra_attributes .= " data-checkout_url='" . $checkout_link . "'"; |
| 222 | } else { |
| 223 | $login_url = wp_login_url(wp_get_referer()); |
| 224 | |
| 225 | $extra_attributes .= " data-login_url='" . $login_url . "'"; |
| 226 | } |
| 227 | |
| 228 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 229 | } |
| 230 | |
| 231 | case 'membership_btn': { |
| 232 | $pricing_page = Settings::get_pricing_page_url(); |
| 233 | if ($pricing_page) { |
| 234 | if (is_user_logged_in()) { |
| 235 | $extra_attributes .= " data-pricing_url='" . $pricing_page . "'"; |
| 236 | } else { |
| 237 | $login_url = wp_login_url(wp_get_referer()); |
| 238 | |
| 239 | $extra_attributes .= " data-login_url='" . $login_url . "'"; |
| 240 | } |
| 241 | |
| 242 | return $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 243 | } |
| 244 | |
| 245 | return ""; |
| 246 | } |
| 247 | |
| 248 | case 'gift_course_btn': { |
| 249 | if (is_user_logged_in()) { |
| 250 | $modal_id = 'tutor-gift-this-course-modal-' . wp_generate_uuid4(); |
| 251 | $extra_attributes .= " data-tutor-modal-target='$modal_id'"; |
| 252 | |
| 253 | $btn = $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 254 | // Capture template output |
| 255 | ob_start(); |
| 256 | tutor_load_template( |
| 257 | 'single.course.gift-this-course-modal', |
| 258 | array('course_id' => $course_id, 'modal_id' => $modal_id), |
| 259 | true |
| 260 | ); |
| 261 | |
| 262 | $modal_html = ob_get_clean(); |
| 263 | |
| 264 | // Return button + modal |
| 265 | return $btn . $modal_html; |
| 266 | } else { |
| 267 | // direct to login page if not logged in |
| 268 | $login_url = wp_login_url(wp_get_referer()); |
| 269 | $extra_attributes .= " data-login_url='" . $login_url . "'"; |
| 270 | $btn = $this->generate_child_element_with_parent_droip_data($extra_attributes); |
| 271 | |
| 272 | // Return button + modal |
| 273 | return $btn; |
| 274 | } |
| 275 | |
| 276 | return ""; |
| 277 | } |
| 278 | |
| 279 | default: { |
| 280 | return ''; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | return ''; |
| 285 | } |
| 286 | |
| 287 | private function update_entry_box_button_logic($entry_box_button_logic, $options) |
| 288 | { |
| 289 | $course_id = isset($options['post']) ? $options['post']->ID : get_the_ID(); |
| 290 | |
| 291 | $is_paid_course = tutor_utils()->is_course_purchasable($course_id); |
| 292 | |
| 293 | if (isset($options['relation_type']) && $options['relation_type'] === 'TUTOR_LMS_CART') { |
| 294 | if ($entry_box_button_logic->show_view_cart_btn) { |
| 295 | $entry_box_button_logic->show_remove_from_cart_btn = true; |
| 296 | } |
| 297 | } |
| 298 | if (isset($options['relation_type']) && $options['relation_type'] === 'TUTOR_LMS_CART') { |
| 299 | $entry_box_button_logic->show_view_cart_btn = false; |
| 300 | } |
| 301 | |
| 302 | // Remove this part. These logic come from tutor_entry_box_buttons function. |
| 303 | // if ($is_paid_course) { |
| 304 | |
| 305 | // if (tutor()->has_pro && Subscription::is_enabled() && $course_id) { |
| 306 | |
| 307 | // $plan_model = new PlanModel(); |
| 308 | // // Checking is course has subscription plan then show buy now button. |
| 309 | // $selling_option = Course::get_selling_option($course_id); |
| 310 | // if (!$selling_option) { |
| 311 | // $selling_option = Course::SELLING_OPTION_ALL; |
| 312 | // } |
| 313 | // if ($selling_option === Course::SELLING_OPTION_SUBSCRIPTION || $selling_option === Course::SELLING_OPTION_BOTH || $selling_option === Course::SELLING_OPTION_ALL) { |
| 314 | |
| 315 | // $items = $plan_model->get_subscription_plans($course_id, PlanModel::STATUS_ACTIVE); |
| 316 | |
| 317 | // if (count($items) > 0) { |
| 318 | // $entry_box_button_logic->show_subscribe_now_btn = true; |
| 319 | // } |
| 320 | // } |
| 321 | |
| 322 | // // Checking is course has membership plan enabled |
| 323 | // $selling_option = Course::get_selling_option($course_id); |
| 324 | // if (!$selling_option) { |
| 325 | // $selling_option = Course::SELLING_OPTION_ALL; |
| 326 | // } |
| 327 | // if ($selling_option === Course::SELLING_OPTION_MEMBERSHIP || $selling_option === Course::SELLING_OPTION_ALL) { |
| 328 | // $active_membership_plans = $plan_model->get_membership_plans(PlanModel::STATUS_ACTIVE); |
| 329 | // if (count($active_membership_plans) > 0) { |
| 330 | // $entry_box_button_logic->show_membership_btn = true; |
| 331 | // } |
| 332 | // } |
| 333 | // } |
| 334 | |
| 335 | // // Checking is course can be gifted then show gift course button. |
| 336 | // if (class_exists('\TutorPro\GiftCourse\InitGift') && class_exists('TutorPro\GiftCourse\GiftCourse')) { |
| 337 | // $init_gift = new \TutorPro\GiftCourse\InitGift(); |
| 338 | // if (tutor()->has_pro && $init_gift->is_enabled() && $course_id) { |
| 339 | // $can_gift_this_course = GiftCourse::can_gift_course($course_id); |
| 340 | |
| 341 | // if ($can_gift_this_course) { |
| 342 | // $entry_box_button_logic->show_gift_course_btn = true; |
| 343 | // } |
| 344 | // } |
| 345 | // } |
| 346 | // } |
| 347 | |
| 348 | return $entry_box_button_logic; |
| 349 | } |
| 350 | |
| 351 | private function generate_child_element_with_parent_droip_data($extra_attributes) |
| 352 | { |
| 353 | $children_html = $this->generate_child_elements(); |
| 354 | // echo "<pre>";var_dump($this->element['parentId'], $this->elements[$this->element['parentId'] ]);die; |
| 355 | if (isset($this->elements[$this->element['parentId']])) { |
| 356 | $encoded_data = $this->get_all_data_and_styles_from_element_id($this->element['parentId']); |
| 357 | $encoded_data = json_encode($encoded_data); |
| 358 | $children_html .= "<textarea style='display: none'>$encoded_data</textarea>"; |
| 359 | } |
| 360 | return $this->generate_common_element(false, $children_html, $extra_attributes); |
| 361 | } |
| 362 | } |
| 363 |