ActionBoxGenerator.php
1 year ago
AnnouncementsGenerator.php
1 year ago
CourseMetaGenerator.php
1 year ago
CourseThumbnailGenerator.php
1 year ago
ElementGenerator.php
1 year ago
GradebookGenerator.php
1 year ago
InstructorGenerator.php
1 year ago
LessonGenerator.php
1 year ago
Preview.php
1 year ago
QnAGenerator.php
1 year ago
RatingGenerator.php
1 year ago
ResourcesGenerator.php
1 year ago
ShareGenerator.php
1 year ago
TopicsGenerator.php
1 year ago
WishListGenerator.php
1 year ago
ActionBoxGenerator.php
311 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\Ecommerce\CartController; |
| 12 | use Tutor\Models\CartModel; |
| 13 | use TUTOR_CERT\Certificate; |
| 14 | |
| 15 | if (! defined('ABSPATH')) { |
| 16 | exit; // Exit if accessed directly. |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * Class ActionboxGenerator |
| 21 | * This class is used to define all helper functions. |
| 22 | * |
| 23 | * @package TutorLMSDroip\ElementGenerator |
| 24 | */ |
| 25 | trait ActionboxGenerator |
| 26 | { |
| 27 | /** |
| 28 | * Generate actionbox markup |
| 29 | * |
| 30 | * @return string |
| 31 | */ |
| 32 | private function generate_actionbox_markup() |
| 33 | { |
| 34 | $course_id = isset($this->options['post']) ? $this->options['post']->ID : get_the_ID(); |
| 35 | $ele_name = $this->element['name']; |
| 36 | switch ($this->element['name']) { |
| 37 | case TDE_APP_PREFIX . '-course-enroll-buttons': |
| 38 | if (tutor_entry_box_buttons($course_id)->show_add_to_cart_btn || tutor_entry_box_buttons($course_id)->show_enroll_btn) { |
| 39 | return $this->generate_common_element(); |
| 40 | } else { |
| 41 | return ''; |
| 42 | } |
| 43 | case TDE_APP_PREFIX . '-free-action-box': |
| 44 | $price = apply_filters('get_tutor_course_price', null, $course_id); |
| 45 | $is_purchasable = tutor_utils()->is_course_purchasable(); |
| 46 | $is_free = $is_purchasable && $price ? false : true; |
| 47 | if (! $is_free) { |
| 48 | return ''; |
| 49 | } |
| 50 | return $this->generate_common_element(); |
| 51 | |
| 52 | case TDE_APP_PREFIX . '-enroll-button': |
| 53 | if (! tutor_entry_box_buttons($course_id)->show_enroll_btn) { |
| 54 | return ''; |
| 55 | } |
| 56 | $children_html = $this->generate_child_elements(); |
| 57 | $children_html .= "<input type='hidden' name='course_id' value='$course_id' />"; |
| 58 | return "<div $this->attributes data-ele_name='$ele_name'>$children_html</div>"; |
| 59 | case TDE_APP_PREFIX . '-paid-action-buttons': |
| 60 | if (tutor_entry_box_buttons($course_id)->show_add_to_cart_btn) { |
| 61 | return $this->generate_common_element(); |
| 62 | } else { |
| 63 | return ''; |
| 64 | } |
| 65 | |
| 66 | case TDE_APP_PREFIX . '-cart-box': |
| 67 | $monetize_by = tutor_utils()->get_option('monetize_by'); |
| 68 | if ('tutor' === $monetize_by) { |
| 69 | $is_course_in_user_cart = CartModel::is_course_in_user_cart(get_current_user_id(), $course_id); |
| 70 | if (!$is_course_in_user_cart) |
| 71 | return ''; |
| 72 | } else if ('wc' === $monetize_by) { |
| 73 | if (! class_exists('WooCommerce')) { |
| 74 | return ''; |
| 75 | } |
| 76 | if (! tutor_entry_box_buttons($course_id)->show_add_to_cart_btn) { |
| 77 | return ''; |
| 78 | } |
| 79 | } |
| 80 | $children_html = $this->generate_child_elements(); |
| 81 | return "<div $this->attributes>$children_html</div>"; |
| 82 | case TDE_APP_PREFIX . '-add-to-cart-button': |
| 83 | $monetize_by = tutor_utils()->get_option('monetize_by'); |
| 84 | if ('tutor' === $monetize_by) { |
| 85 | $is_course_in_user_cart = CartModel::is_course_in_user_cart(get_current_user_id(), $course_id); |
| 86 | if ($is_course_in_user_cart) |
| 87 | return ''; |
| 88 | |
| 89 | $children_html = $this->generate_child_elements(); |
| 90 | $children_html .= "<input type='hidden' name='course_id' value='$course_id' />"; |
| 91 | $children_html .= "<input type='hidden' name='monetize_by' value='$monetize_by' />"; |
| 92 | return $this->generate_common_element(false, $children_html); |
| 93 | } else if ('wc' === $monetize_by) { |
| 94 | if (! class_exists('WooCommerce')) { |
| 95 | return ''; |
| 96 | } |
| 97 | if (! tutor_entry_box_buttons($course_id)->show_add_to_cart_btn) { |
| 98 | return ''; |
| 99 | } |
| 100 | |
| 101 | $product_id = tutor_utils()->get_course_product_id($course_id); |
| 102 | |
| 103 | if (tutor_utils()->is_course_added_to_cart($product_id, true)) { |
| 104 | return ''; |
| 105 | } |
| 106 | |
| 107 | $action_url = esc_url(apply_filters('tutor_course_add_to_cart_form_action', get_permalink($course_id))); |
| 108 | $children_html = $this->generate_child_elements(); |
| 109 | $children_html .= "<input type='hidden' name='action_url' value='$action_url' />"; |
| 110 | $children_html .= "<input type='hidden' name='product_id' value='$product_id' />"; |
| 111 | $children_html .= "<input type='hidden' name='monetize_by' value='$monetize_by' />"; |
| 112 | return $this->generate_common_element(false, $children_html); |
| 113 | } |
| 114 | return ''; |
| 115 | case TDE_APP_PREFIX . '-view-cart-button': |
| 116 | |
| 117 | $monetize_by = tutor_utils()->get_option('monetize_by'); |
| 118 | if ('tutor' === $monetize_by) { |
| 119 | $is_course_in_user_cart = CartModel::is_course_in_user_cart(get_current_user_id(), $course_id); |
| 120 | if (!$is_course_in_user_cart) |
| 121 | return ''; |
| 122 | $cart_page_url = CartController::get_page_url(); |
| 123 | $children_html = $this->generate_child_elements(); |
| 124 | return "<a href='$cart_page_url' $this->attributes>$children_html</a>"; |
| 125 | } else if ('wc' === $monetize_by) { |
| 126 | if (! class_exists('WooCommerce')) { |
| 127 | return ''; |
| 128 | } |
| 129 | $product_id = tutor_utils()->get_course_product_id($course_id); |
| 130 | $product = wc_get_product($product_id); |
| 131 | |
| 132 | if (! tutor_utils()->is_course_added_to_cart($product_id, true)) { |
| 133 | return ''; |
| 134 | } |
| 135 | |
| 136 | $cart_url = esc_url(wc_get_cart_url()); |
| 137 | $children_html = $this->generate_child_elements(); |
| 138 | |
| 139 | return "<a href='$cart_url' $this->attributes>$children_html</a>"; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | case TDE_APP_PREFIX . '-discounted-price': |
| 146 | $monetize_by = tutor_utils()->get_option('monetize_by'); |
| 147 | $sale_price = false; |
| 148 | if ('tutor' === $monetize_by) { |
| 149 | $course_price = tutor_utils()->get_raw_course_price($course_id); |
| 150 | $regular_price = $course_price->regular_price; |
| 151 | $sale_price = $course_price->sale_price; |
| 152 | } else if ('wc' === $monetize_by) { |
| 153 | if (! class_exists('WooCommerce')) { |
| 154 | return ''; |
| 155 | } |
| 156 | $product_id = tutor_utils()->get_course_product_id($course_id); |
| 157 | $product = wc_get_product($product_id); |
| 158 | if (! $product) { |
| 159 | return ''; |
| 160 | } |
| 161 | $sale_price = wc_price($product->get_sale_price()); |
| 162 | } |
| 163 | |
| 164 | if (! $sale_price) { |
| 165 | return ''; |
| 166 | } |
| 167 | return "<span $this->attributes>$sale_price</span>"; |
| 168 | |
| 169 | case TDE_APP_PREFIX . '-regular-price': |
| 170 | $monetize_by = tutor_utils()->get_option('monetize_by'); |
| 171 | if ('tutor' === $monetize_by) { |
| 172 | $course_price = tutor_utils()->get_raw_course_price($course_id); |
| 173 | $regular_price = $course_price->regular_price; |
| 174 | } else if ('wc' === $monetize_by) { |
| 175 | if (! class_exists('WooCommerce')) { |
| 176 | return ''; |
| 177 | } |
| 178 | $product_id = tutor_utils()->get_course_product_id($course_id); |
| 179 | $product = wc_get_product($product_id); |
| 180 | if (! $product) { |
| 181 | return ''; |
| 182 | } |
| 183 | $regular_price = wc_price($product->get_regular_price()); |
| 184 | } |
| 185 | if (! $regular_price) { |
| 186 | return ''; |
| 187 | } |
| 188 | return "<span $this->attributes>$regular_price</span>"; |
| 189 | |
| 190 | case TDE_APP_PREFIX . '-course-action-buttons': |
| 191 | $is_course_completed = tutor_utils()->is_completed_course($course_id, get_current_user_id()); |
| 192 | if ((tutor_entry_box_buttons($course_id)->show_start_learning_btn && ! $is_course_completed) || (tutor_entry_box_buttons($course_id)->show_continue_learning_btn && ! $is_course_completed) || tutor_entry_box_buttons($course_id)->show_retake_course_btn || (tutor_entry_box_buttons($course_id)->show_certificate_view_btn && function_exists('TUTOR_CERT'))) { |
| 193 | return $this->generate_common_element(); |
| 194 | } else { |
| 195 | return ''; |
| 196 | } |
| 197 | |
| 198 | case TDE_APP_PREFIX . '-start-learning': |
| 199 | if (! tutor_entry_box_buttons($course_id)->show_start_learning_btn) { |
| 200 | return ''; |
| 201 | } |
| 202 | $is_course_completed = tutor_utils()->is_completed_course($course_id, get_current_user_id()); |
| 203 | if ($is_course_completed) { |
| 204 | return ''; |
| 205 | } |
| 206 | $lession_url = tutor_utils()->get_course_first_lesson(); |
| 207 | $children_html = $this->generate_child_elements(); |
| 208 | return "<a href='$lession_url' $this->attributes data-ele_name='$ele_name'>$children_html</a>"; |
| 209 | |
| 210 | case TDE_APP_PREFIX . '-continue-learning': |
| 211 | if (! tutor_entry_box_buttons($course_id)->show_continue_learning_btn) { |
| 212 | return ''; |
| 213 | } |
| 214 | $is_course_completed = tutor_utils()->is_completed_course($course_id, get_current_user_id()); |
| 215 | |
| 216 | if ($is_course_completed) { |
| 217 | return ''; |
| 218 | } |
| 219 | $lession_url = tutor_utils()->get_course_first_lesson(); |
| 220 | $children_html = $this->generate_child_elements(); |
| 221 | return "<a href='$lession_url' $this->attributes data-ele_name='$ele_name'>$children_html</a>"; |
| 222 | |
| 223 | case TDE_APP_PREFIX . '-complete-course': |
| 224 | if (! tutor_entry_box_buttons($course_id)->show_complete_course_btn) { |
| 225 | return ''; |
| 226 | } |
| 227 | $is_course_completed = tutor_utils()->is_completed_course($course_id, get_current_user_id()); |
| 228 | if ($is_course_completed) { |
| 229 | return ''; |
| 230 | } |
| 231 | |
| 232 | $children_html = $this->generate_child_elements(); |
| 233 | $children_html .= "<input type='hidden' name='course_id' value='$course_id' />"; |
| 234 | return "<button $this->attributes data-ele_name='$ele_name'>$children_html</button>"; |
| 235 | |
| 236 | case TDE_APP_PREFIX . '-retake-and-certificate': |
| 237 | if (tutor_entry_box_buttons($course_id)->show_retake_course_btn || (tutor_entry_box_buttons($course_id)->show_certificate_view_btn && function_exists('TUTOR_CERT'))) { |
| 238 | return $this->generate_common_element(); |
| 239 | } else { |
| 240 | return ''; |
| 241 | } |
| 242 | case TDE_APP_PREFIX . '-retake-course': |
| 243 | if (! tutor_entry_box_buttons($course_id)->show_retake_course_btn) { |
| 244 | return ''; |
| 245 | } |
| 246 | // TODO: Retake functionality |
| 247 | return $this->generate_common_element(); |
| 248 | |
| 249 | case TDE_APP_PREFIX . '-view-certificate': |
| 250 | if (! function_exists('TUTOR_CERT')) { |
| 251 | return ''; |
| 252 | } |
| 253 | if (! tutor_entry_box_buttons($course_id)->show_certificate_view_btn) { |
| 254 | return ''; |
| 255 | } |
| 256 | $is_course_completed = tutor_utils()->is_completed_course($course_id, get_current_user_id()); |
| 257 | |
| 258 | if (! $is_course_completed) { |
| 259 | return ''; |
| 260 | } |
| 261 | |
| 262 | $certificate_url = ''; |
| 263 | if (tutils()->is_addon_enabled(TUTOR_CERT()->basename)) { |
| 264 | $certificate_url = (new Certificate(true))->get_certificate($course_id); |
| 265 | } |
| 266 | $children_html = $this->generate_child_elements(); |
| 267 | return "<a href='$certificate_url' $this->attributes data-ele_name='$ele_name'>$children_html</a>"; |
| 268 | |
| 269 | case TDE_APP_PREFIX . '-enroll-info': |
| 270 | $is_enrolled = tutor_utils()->is_enrolled($course_id); |
| 271 | if (! $is_enrolled) { |
| 272 | return ''; |
| 273 | } |
| 274 | return $this->generate_common_element(); |
| 275 | |
| 276 | case TDE_APP_PREFIX . '-enroll-date': |
| 277 | $is_enrolled = tutor_utils()->is_enrolled($course_id); |
| 278 | $post_date = is_object($is_enrolled) && isset($is_enrolled->post_date) ? $is_enrolled->post_date : ''; |
| 279 | $post_date = tutor_i18n_get_formated_date($post_date, get_option('date_format')); |
| 280 | return $this->generate_common_element(false, $post_date); |
| 281 | |
| 282 | case TDE_APP_PREFIX . '-lesson-counter': |
| 283 | $course_progress = tutor_utils()->get_course_completed_percent($course_id, 0, true); |
| 284 | $html = $course_progress['completed_count'] . '/' . $course_progress['total_count']; |
| 285 | return $this->generate_common_element(false, $html); |
| 286 | |
| 287 | case TDE_APP_PREFIX . '-course-progress': |
| 288 | $is_enrolled = tutor_utils()->is_enrolled($course_id); |
| 289 | if (! $is_enrolled) { |
| 290 | return ''; |
| 291 | } |
| 292 | return $this->generate_common_element(); |
| 293 | case TDE_APP_PREFIX . '-progress-percent': |
| 294 | $course_progress = tutor_utils()->get_course_completed_percent($course_id, 0, true); |
| 295 | $completed_percent = $course_progress['completed_percent']; |
| 296 | $html = $completed_percent . '%'; |
| 297 | return $this->generate_common_element(false, $html); |
| 298 | |
| 299 | case TDE_APP_PREFIX . '-progress-bar-inner': |
| 300 | $id = $this->element['id']; |
| 301 | $course_progress = tutor_utils()->get_course_completed_percent($course_id, 0, true); |
| 302 | $completed_percent = $course_progress['completed_percent']; |
| 303 | |
| 304 | $html = "<style>[data-droip='$id']{width: $completed_percent% !important;}</style>" . $this->generate_common_element(false); |
| 305 | return $html; |
| 306 | default: |
| 307 | return ''; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 |