Addons.php
2 years ago
Admin.php
2 years ago
Ajax.php
2 years ago
Announcements.php
3 years ago
Assets.php
2 years ago
Backend_Page_Trait.php
3 years ago
Course.php
2 years ago
Course_Embed.php
3 years ago
Course_Filter.php
2 years ago
Course_List.php
2 years ago
Course_Settings_Tabs.php
3 years ago
Course_Widget.php
3 years ago
Custom_Validation.php
3 years ago
Dashboard.php
3 years ago
FormHandler.php
2 years ago
Frontend.php
2 years ago
Gutenberg.php
3 years ago
Input.php
3 years ago
Instructor.php
2 years ago
Instructors_List.php
2 years ago
Lesson.php
2 years ago
Options_V2.php
2 years ago
Permalink.php
2 years ago
Post_types.php
2 years ago
Private_Course_Access.php
3 years ago
Q_And_A.php
2 years ago
Question_Answers_List.php
3 years ago
Quiz.php
2 years ago
Quiz_Attempts_List.php
2 years ago
RestAPI.php
2 years ago
Reviews.php
3 years ago
Rewrite_Rules.php
2 years ago
Shortcode.php
2 years ago
Student.php
2 years ago
Students_List.php
3 years ago
Taxonomies.php
3 years ago
Template.php
2 years ago
Theme_Compatibility.php
3 years ago
Tools.php
3 years ago
Tools_V2.php
2 years ago
Tutor.php
2 years ago
TutorEDD.php
2 years ago
Tutor_Base.php
2 years ago
Tutor_Setup.php
2 years ago
Upgrader.php
2 years ago
User.php
2 years ago
Utils.php
2 years ago
Video_Stream.php
3 years ago
WhatsNew.php
2 years ago
Withdraw.php
2 years ago
Withdraw_Requests_List.php
3 years ago
WooCommerce.php
2 years ago
Lesson.php
638 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Manage Lesson |
| 4 | * |
| 5 | * @package Tutor |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace TUTOR; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | use Tutor\Models\LessonModel; |
| 18 | |
| 19 | /** |
| 20 | * Lesson class |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | class Lesson extends Tutor_Base { |
| 25 | |
| 26 | /** |
| 27 | * Register hooks |
| 28 | * |
| 29 | * @since 1.0.0 |
| 30 | * |
| 31 | * @return void |
| 32 | */ |
| 33 | public function __construct() { |
| 34 | parent::__construct(); |
| 35 | |
| 36 | add_action( 'add_meta_boxes', array( $this, 'register_meta_box' ) ); |
| 37 | add_action( 'save_post_' . $this->lesson_post_type, array( $this, 'save_lesson_meta' ) ); |
| 38 | |
| 39 | add_action( 'wp_ajax_tutor_load_edit_lesson_modal', array( $this, 'tutor_load_edit_lesson_modal' ) ); |
| 40 | add_action( 'wp_ajax_tutor_modal_create_or_update_lesson', array( $this, 'tutor_modal_create_or_update_lesson' ) ); |
| 41 | add_action( 'wp_ajax_tutor_delete_lesson_by_id', array( $this, 'tutor_delete_lesson_by_id' ) ); |
| 42 | |
| 43 | add_filter( 'get_sample_permalink', array( $this, 'change_lesson_permalink' ), 10, 2 ); |
| 44 | |
| 45 | /** |
| 46 | * Add Column |
| 47 | */ |
| 48 | add_filter( "manage_{$this->lesson_post_type}_posts_columns", array( $this, 'add_column' ), 10, 1 ); |
| 49 | add_action( "manage_{$this->lesson_post_type}_posts_custom_column", array( $this, 'custom_lesson_column' ), 10, 2 ); |
| 50 | |
| 51 | /** |
| 52 | * Frontend Action |
| 53 | */ |
| 54 | add_action( 'template_redirect', array( $this, 'mark_lesson_complete' ) ); |
| 55 | |
| 56 | add_action( 'wp_ajax_tutor_render_lesson_content', array( $this, 'tutor_render_lesson_content' ) ); |
| 57 | |
| 58 | /** |
| 59 | * For public course access |
| 60 | */ |
| 61 | add_action( 'wp_ajax_nopriv_tutor_render_lesson_content', array( $this, 'tutor_render_lesson_content' ) ); |
| 62 | |
| 63 | /** |
| 64 | * Autoplay next video |
| 65 | * |
| 66 | * @since 1.4.9 |
| 67 | */ |
| 68 | add_action( 'wp_ajax_autoload_next_course_content', array( $this, 'autoload_next_course_content' ) ); |
| 69 | |
| 70 | /** |
| 71 | * Load next course item after click complete button |
| 72 | * |
| 73 | * @since 1.5.3 |
| 74 | */ |
| 75 | add_action( 'tutor_lesson_completed_after', array( $this, 'tutor_lesson_completed_after' ), 999 ); |
| 76 | |
| 77 | /** |
| 78 | * Lesson comment & reply ajax handler |
| 79 | * |
| 80 | * @since 2.0.0 |
| 81 | */ |
| 82 | add_action( 'wp_ajax_tutor_single_course_lesson_load_more', array( $this, 'tutor_single_course_lesson_load_more' ) ); |
| 83 | add_action( 'wp_ajax_tutor_create_lesson_comment', array( $this, 'tutor_single_course_lesson_load_more' ) ); |
| 84 | add_action( 'wp_ajax_tutor_reply_lesson_comment', array( $this, 'reply_lesson_comment' ) ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Manage load more & comment create |
| 89 | * |
| 90 | * @since 2.0.6 |
| 91 | * @return void send wp json data |
| 92 | */ |
| 93 | public function tutor_single_course_lesson_load_more() { |
| 94 | tutor_utils()->checking_nonce(); |
| 95 | $comment = Input::post( 'comment', '', Input::TYPE_KSES_POST ); |
| 96 | if ( 'tutor_create_lesson_comment' === Input::post( 'action' ) && strlen( $comment ) > 0 ) { |
| 97 | $comment_data = array( |
| 98 | 'comment_content' => $comment, |
| 99 | 'comment_post_ID' => Input::post( 'comment_post_ID', 0, Input::TYPE_INT ), |
| 100 | 'comment_parent' => Input::post( 'comment_parent', 0, Input::TYPE_INT ), |
| 101 | ); |
| 102 | self::create_comment( $comment_data ); |
| 103 | do_action( 'tutor_new_comment_added', $comment_data ); |
| 104 | } |
| 105 | ob_start(); |
| 106 | tutor_load_template( 'single.lesson.comment' ); |
| 107 | $html = ob_get_clean(); |
| 108 | wp_send_json_success( array( 'html' => $html ) ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Registering metabox |
| 113 | * |
| 114 | * @since 1.0.0 |
| 115 | * @return void |
| 116 | */ |
| 117 | public function register_meta_box() { |
| 118 | $lesson_post_type = $this->lesson_post_type; |
| 119 | |
| 120 | tutor_meta_box_wrapper( 'tutor-course-select', __( 'Select Course', 'tutor' ), array( $this, 'lesson_metabox' ), $lesson_post_type, 'advanced', 'default', 'tutor-admin-post-meta' ); |
| 121 | |
| 122 | tutor_meta_box_wrapper( 'tutor-lesson-videos', __( 'Lesson Video', 'tutor' ), array( $this, 'lesson_video_metabox' ), $lesson_post_type, 'advanced', 'default', 'tutor-admin-post-meta' ); |
| 123 | |
| 124 | tutor_meta_box_wrapper( 'tutor-lesson-attachments', __( 'Attachments', 'tutor' ), array( $this, 'lesson_attachments_metabox' ), $lesson_post_type, 'advanced', 'default', 'tutor-admin-post-meta' ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Lesson metabox |
| 129 | * |
| 130 | * @since 1.0.0 |
| 131 | * |
| 132 | * @return void |
| 133 | */ |
| 134 | public function lesson_metabox() { |
| 135 | include tutor()->path . 'views/metabox/lesson-metabox.php'; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Video metabox |
| 140 | * |
| 141 | * @since 1.0.0 |
| 142 | * |
| 143 | * @return void |
| 144 | */ |
| 145 | public function lesson_video_metabox() { |
| 146 | include tutor()->path . 'views/metabox/video-metabox.php'; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Attachment metabox |
| 151 | * |
| 152 | * @since 1.0.0 |
| 153 | * |
| 154 | * @return void |
| 155 | */ |
| 156 | public function lesson_attachments_metabox() { |
| 157 | include tutor()->path . 'views/metabox/lesson-attachments-metabox.php'; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Saving lesson meta and assets |
| 162 | * |
| 163 | * @since 1.0.0 |
| 164 | * |
| 165 | * @param integer $post_ID post ID. |
| 166 | * @return void |
| 167 | */ |
| 168 | public function save_lesson_meta( $post_ID ) { |
| 169 | $video_source = sanitize_text_field( tutor_utils()->array_get( 'video.source', $_POST ) ); //phpcs:ignore |
| 170 | if ( '-1' === $video_source ) { |
| 171 | delete_post_meta( $post_ID, '_video' ); |
| 172 | } elseif ( $video_source ) { |
| 173 | |
| 174 | // Sanitize data through helper method. |
| 175 | $video = Input::sanitize_array( |
| 176 | $_POST['video'] ?? array(), //phpcs:ignore |
| 177 | array( |
| 178 | 'source_external_url' => 'esc_url', |
| 179 | 'source_embedded' => 'wp_kses_post', |
| 180 | ), |
| 181 | true |
| 182 | ); |
| 183 | update_post_meta( $post_ID, '_video', $video ); |
| 184 | } |
| 185 | |
| 186 | // Attachments. |
| 187 | $attachments = array(); |
| 188 | // phpcs:disable WordPress.Security.NonceVerification.Missing |
| 189 | if ( ! empty( $_POST['tutor_attachments'] ) ) { |
| 190 | //phpcs:ignore -- data sanitized through helper method. |
| 191 | $attachments = tutor_utils()->sanitize_array( wp_unslash( $_POST['tutor_attachments'] ) ); |
| 192 | $attachments = array_unique( $attachments ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * If !empty attachment then update meta else |
| 197 | * delete meta key to prevent empty data in db |
| 198 | * |
| 199 | * @since 1.8.9 |
| 200 | */ |
| 201 | if ( ! empty( $attachments ) ) { |
| 202 | update_post_meta( $post_ID, '_tutor_attachments', $attachments ); |
| 203 | } else { |
| 204 | delete_post_meta( $post_ID, '_tutor_attachments' ); |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Load edit lesson modal |
| 211 | * |
| 212 | * @since 1.0.0 |
| 213 | * |
| 214 | * @return void |
| 215 | */ |
| 216 | public function tutor_load_edit_lesson_modal() { |
| 217 | tutor_utils()->checking_nonce(); |
| 218 | |
| 219 | $lesson_id = Input::post( 'lesson_id', 0, Input::TYPE_INT ); |
| 220 | $topic_id = Input::post( 'topic_id', 0, Input::TYPE_INT ); |
| 221 | |
| 222 | if ( ! tutor_utils()->can_user_manage( 'topic', $topic_id ) ) { |
| 223 | wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) ); |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * If Lesson Not Exists, provide dummy |
| 228 | */ |
| 229 | $post_arr = array( |
| 230 | 'ID' => 0, |
| 231 | 'post_content' => '', |
| 232 | 'post_type' => $this->lesson_post_type, |
| 233 | 'post_title' => __( 'Draft Lesson', 'tutor' ), |
| 234 | 'post_status' => 'publish', |
| 235 | 'post_author' => get_current_user_id(), |
| 236 | 'post_parent' => $topic_id, |
| 237 | ); |
| 238 | |
| 239 | $post = $lesson_id ? get_post( $lesson_id ) : (object) $post_arr; |
| 240 | |
| 241 | ob_start(); |
| 242 | include tutor()->path . 'views/modal/edit-lesson.php'; |
| 243 | $output = ob_get_clean(); |
| 244 | |
| 245 | wp_send_json_success( array( 'output' => $output ) ); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Load lesson modal for create or update lesson |
| 250 | * |
| 251 | * @since 1.0.0 |
| 252 | * @since 1.5.1 updated |
| 253 | * |
| 254 | * @return void |
| 255 | */ |
| 256 | public function tutor_modal_create_or_update_lesson() { |
| 257 | tutor_utils()->checking_nonce(); |
| 258 | |
| 259 | global $wpdb; |
| 260 | |
| 261 | /** |
| 262 | * Allow iframe inside lesson content to support |
| 263 | * embed video & other stuff |
| 264 | * |
| 265 | * @since 2.1.6 |
| 266 | */ |
| 267 | add_filter( 'wp_kses_allowed_html', Input::class . '::allow_iframe', 10, 2 ); |
| 268 | |
| 269 | $lesson_id = Input::post( 'lesson_id', 0, Input::TYPE_INT ); |
| 270 | $topic_id = Input::post( 'current_topic_id', 0, Input::TYPE_INT ); |
| 271 | $current_topic_id = $topic_id; |
| 272 | $course_id = tutor_utils()->get_course_id_by( 'topic', $topic_id ); |
| 273 | |
| 274 | if ( ! tutor_utils()->can_user_manage( 'topic', $topic_id ) ) { |
| 275 | wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) ); |
| 276 | } |
| 277 | |
| 278 | $title = Input::post( 'lesson_title' ); |
| 279 | $_lesson_thumbnail_id = Input::post( '_lesson_thumbnail_id', 0, Input::TYPE_INT ); |
| 280 | $lesson_content = Input::post( 'lesson_content', '', Input::TYPE_KSES_POST ); |
| 281 | $is_html_active = Input::post( 'is_html_active' ) === 'true' ? true : false; |
| 282 | $raw_html_content = Input::post( 'tutor_lesson_modal_editor', '', Input::TYPE_KSES_POST ); |
| 283 | $post_content = $is_html_active ? $raw_html_content : $lesson_content; |
| 284 | |
| 285 | $lesson_data = array( |
| 286 | 'post_type' => $this->lesson_post_type, |
| 287 | 'post_title' => $title, |
| 288 | 'post_name' => sanitize_title( $title ), |
| 289 | 'post_content' => $post_content, |
| 290 | 'post_status' => 'publish', |
| 291 | 'comment_status' => 'open', |
| 292 | 'post_author' => get_current_user_id(), |
| 293 | 'post_parent' => $topic_id, |
| 294 | ); |
| 295 | |
| 296 | if ( 0 === $lesson_id ) { |
| 297 | $lesson_data['menu_order'] = tutor_utils()->get_next_course_content_order_id( $topic_id ); |
| 298 | $lesson_id = wp_insert_post( $lesson_data ); |
| 299 | |
| 300 | if ( $lesson_id ) { |
| 301 | do_action( 'tutor/lesson/created', $lesson_id ); |
| 302 | } else { |
| 303 | wp_send_json_error( array( 'message' => __( 'Couldn\'t create lesson.', 'tutor' ) ) ); |
| 304 | } |
| 305 | } else { |
| 306 | $lesson_data['ID'] = $lesson_id; |
| 307 | |
| 308 | do_action( 'tutor/lesson_update/before', $lesson_id ); |
| 309 | wp_update_post( $lesson_data ); |
| 310 | if ( $_lesson_thumbnail_id ) { |
| 311 | update_post_meta( $lesson_id, '_thumbnail_id', $_lesson_thumbnail_id ); |
| 312 | } else { |
| 313 | delete_post_meta( $lesson_id, '_thumbnail_id' ); |
| 314 | } |
| 315 | |
| 316 | do_action( 'tutor/lesson_update/after', $lesson_id ); |
| 317 | } |
| 318 | |
| 319 | ob_start(); |
| 320 | include tutor()->path . 'views/metabox/course-contents.php'; |
| 321 | $course_contents = ob_get_clean(); |
| 322 | |
| 323 | wp_send_json_success( array( 'course_contents' => $course_contents ) ); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Delete Lesson from course builder by ID |
| 328 | * |
| 329 | * @since 1.0.0 |
| 330 | * |
| 331 | * @return void |
| 332 | */ |
| 333 | public function tutor_delete_lesson_by_id() { |
| 334 | tutor_utils()->checking_nonce(); |
| 335 | |
| 336 | $lesson_id = Input::post( 'lesson_id', 0, Input::TYPE_INT ); |
| 337 | |
| 338 | if ( ! tutor_utils()->can_user_manage( 'lesson', $lesson_id ) ) { |
| 339 | wp_send_json_error( array( 'message' => __( 'Access Denied', 'tutor' ) ) ); |
| 340 | } |
| 341 | |
| 342 | wp_delete_post( $lesson_id, true ); |
| 343 | wp_send_json_success(); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /** |
| 348 | * Changed the URI based |
| 349 | * |
| 350 | * @since 1.0.0 |
| 351 | * |
| 352 | * @param string $uri URI. |
| 353 | * @param integer $lesson_id lesson ID. |
| 354 | * |
| 355 | * @return string |
| 356 | */ |
| 357 | public function change_lesson_permalink( $uri, $lesson_id ) { |
| 358 | $post = get_post( $lesson_id ); |
| 359 | |
| 360 | if ( $post && $post->post_type === $this->lesson_post_type ) { |
| 361 | $uri_base = trailingslashit( site_url() ); |
| 362 | |
| 363 | $sample_course = 'sample-course'; |
| 364 | $is_course = tutor_utils()->get_course_id_by( 'lesson', get_the_ID() ); |
| 365 | if ( $is_course ) { |
| 366 | $course = get_post( $is_course ); |
| 367 | if ( $course ) { |
| 368 | $sample_course = $course->post_name; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | $new_course_base = $uri_base . "course/{$sample_course}/lesson/%pagename%/"; |
| 373 | $uri[0] = $new_course_base; |
| 374 | } |
| 375 | |
| 376 | return $uri; |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | * Add column to lesson HTML table |
| 381 | * |
| 382 | * @since 1.0.0 |
| 383 | * |
| 384 | * @param array $columns columns. |
| 385 | * @return array |
| 386 | */ |
| 387 | public function add_column( $columns ) { |
| 388 | $date_col = $columns['date']; |
| 389 | unset( $columns['date'] ); |
| 390 | $columns['course'] = __( 'Course', 'tutor' ); |
| 391 | $columns['date'] = $date_col; |
| 392 | |
| 393 | return $columns; |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Add custom lesson column. |
| 398 | * |
| 399 | * @since 1.0.0 |
| 400 | * |
| 401 | * @param string $column column name. |
| 402 | * @param integer $post_id post ID. |
| 403 | * |
| 404 | * @return mixed |
| 405 | */ |
| 406 | public function custom_lesson_column( $column, $post_id ) { |
| 407 | if ( 'course' === $column ) { |
| 408 | |
| 409 | $course_id = tutor_utils()->get_course_id_by( 'lesson', $post_id ); |
| 410 | if ( $course_id ) { |
| 411 | echo wp_kses( |
| 412 | '<a href="' . admin_url( 'post.php?post=' . $course_id . '&action=edit' ) . '">' . get_the_title( $course_id ) . '</a>', |
| 413 | array( 'a' => array( 'href' => true ) ) |
| 414 | ); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Mark lesson completed |
| 421 | * |
| 422 | * @since 1.0.0 |
| 423 | * |
| 424 | * @return void |
| 425 | */ |
| 426 | public function mark_lesson_complete() { |
| 427 | if ( 'tutor_complete_lesson' !== Input::post( 'tutor_action' ) ) { |
| 428 | return; |
| 429 | } |
| 430 | // Checking nonce. |
| 431 | tutor_utils()->checking_nonce(); |
| 432 | |
| 433 | $user_id = get_current_user_id(); |
| 434 | |
| 435 | // TODO: need to show view if not signed_in. |
| 436 | if ( ! $user_id ) { |
| 437 | die( esc_html__( 'Please Sign-In', 'tutor' ) ); |
| 438 | } |
| 439 | |
| 440 | $lesson_id = Input::post( 'lesson_id', 0, Input::TYPE_INT ); |
| 441 | |
| 442 | if ( ! $lesson_id ) { |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | $validated = apply_filters( 'tutor_validate_lesson_complete', true, $user_id, $lesson_id ); |
| 447 | if ( ! $validated ) { |
| 448 | return; |
| 449 | } |
| 450 | |
| 451 | do_action( 'tutor_lesson_completed_before', $lesson_id ); |
| 452 | /** |
| 453 | * Marking lesson at user meta, meta format, _tutor_completed_lesson_id_{id} and value = tutor_time(); |
| 454 | */ |
| 455 | LessonModel::mark_lesson_complete( $lesson_id ); |
| 456 | |
| 457 | do_action( 'tutor_lesson_completed_email_after', $lesson_id, $user_id ); |
| 458 | do_action( 'tutor_lesson_completed_after', $lesson_id, $user_id ); |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Render the lesson content |
| 463 | * |
| 464 | * @since 1.0.0 |
| 465 | * |
| 466 | * @return void |
| 467 | */ |
| 468 | public function tutor_render_lesson_content() { |
| 469 | tutor_utils()->checking_nonce(); |
| 470 | |
| 471 | $lesson_id = Input::post( 'lesson_id', 0, Input::TYPE_INT ); |
| 472 | |
| 473 | $ancestors = get_post_ancestors( $lesson_id ); |
| 474 | $course_id = ! empty( $ancestors ) ? array_pop( $ancestors ) : $lesson_id; |
| 475 | |
| 476 | // Course must be public or current user must be enrolled to access this lesson. |
| 477 | if ( get_post_meta( $course_id, '_tutor_is_public_course', true ) !== 'yes' && ! tutor_utils()->is_enrolled( $course_id ) ) { |
| 478 | |
| 479 | $is_admin = tutor_utils()->has_user_role( 'administrator' ); |
| 480 | $allowed = $is_admin ? true : tutor_utils()->is_instructor_of_this_course( get_current_user_id(), $course_id ); |
| 481 | |
| 482 | if ( ! $allowed ) { |
| 483 | http_response_code( 400 ); |
| 484 | exit; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | ob_start(); |
| 489 | global $post; |
| 490 | |
| 491 | $post = get_post( $lesson_id ); //phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 492 | setup_postdata( $post ); |
| 493 | tutor_lesson_content(); |
| 494 | wp_reset_postdata(); |
| 495 | |
| 496 | $html = ob_get_clean(); |
| 497 | |
| 498 | wp_send_json_success( array( 'html' => $html ) ); |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Load next course item automatically |
| 503 | * |
| 504 | * @since 1.4.9 |
| 505 | * |
| 506 | * @return void |
| 507 | */ |
| 508 | public function autoload_next_course_content() { |
| 509 | tutor_utils()->checking_nonce(); |
| 510 | |
| 511 | $post_id = Input::post( 'post_id', 0, Input::TYPE_INT ); |
| 512 | $content_id = tutor_utils()->get_post_id( $post_id ); |
| 513 | $contents = tutor_utils()->get_course_prev_next_contents_by_id( $content_id ); |
| 514 | |
| 515 | $autoload_course_content = (bool) get_tutor_option( 'autoload_next_course_content' ); |
| 516 | $next_url = false; |
| 517 | if ( $autoload_course_content ) { |
| 518 | $next_url = get_the_permalink( $contents->next_id ); |
| 519 | } |
| 520 | wp_send_json_success( array( 'next_url' => $next_url ) ); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Load next course item after click complete button |
| 525 | * |
| 526 | * @since 1.5.3 |
| 527 | * |
| 528 | * @param integer $content_id content ID. |
| 529 | * @return void |
| 530 | */ |
| 531 | public function tutor_lesson_completed_after( $content_id ) { |
| 532 | $contents = tutor_utils()->get_course_prev_next_contents_by_id( $content_id ); |
| 533 | $autoload_course_content = (bool) get_tutor_option( 'autoload_next_course_content' ); |
| 534 | if ( $autoload_course_content ) { |
| 535 | wp_safe_redirect( get_the_permalink( $contents->next_id ) ); |
| 536 | } else { |
| 537 | wp_safe_redirect( get_the_permalink( $content_id ) ); |
| 538 | } |
| 539 | die(); |
| 540 | } |
| 541 | |
| 542 | /** |
| 543 | * Replay lesson comment |
| 544 | * |
| 545 | * @since 1.0.0 |
| 546 | * |
| 547 | * @return void|null |
| 548 | */ |
| 549 | public function reply_lesson_comment() { |
| 550 | tutor_utils()->checking_nonce(); |
| 551 | $comment = Input::post( 'comment', '', Input::TYPE_KSES_POST ); |
| 552 | if ( 0 === strlen( $comment ) ) { |
| 553 | wp_send_json_error(); |
| 554 | return; |
| 555 | } |
| 556 | |
| 557 | $comment_data = array( |
| 558 | 'comment_content' => $comment, |
| 559 | 'comment_post_ID' => Input::post( 'comment_post_ID', 0, Input::TYPE_INT ), |
| 560 | 'comment_parent' => Input::post( 'comment_parent', 0, Input::TYPE_INT ), |
| 561 | ); |
| 562 | $comment_id = self::create_comment( $comment_data ); |
| 563 | if ( false === $comment_id ) { |
| 564 | wp_send_json_error(); |
| 565 | return; |
| 566 | } |
| 567 | $reply = get_comment( $comment_id ); |
| 568 | do_action( 'tutor_reply_lesson_comment_thread', $comment_id, $comment_data ); |
| 569 | |
| 570 | ob_start(); |
| 571 | ?> |
| 572 | <div class="tutor-comments-list tutor-child-comment tutor-mt-32" id="lesson-comment-<?php echo esc_attr( $reply->comment_ID ); ?>"> |
| 573 | <div class="comment-avatar"> |
| 574 | <img src="<?php echo esc_url( get_avatar_url( $reply->user_id ) ); ?>" alt=""> |
| 575 | </div> |
| 576 | <div class="tutor-single-comment"> |
| 577 | <div class="tutor-actual-comment tutor-mb-12"> |
| 578 | <div class="tutor-comment-author"> |
| 579 | <span class="tutor-fs-6 tutor-fw-bold"> |
| 580 | <?php echo esc_html( $reply->comment_author ); ?> |
| 581 | </span> |
| 582 | <span class="tutor-fs-7 tutor-ml-0 tutor-ml-sm-10"> |
| 583 | <?php echo esc_html( human_time_diff( strtotime( $reply->comment_date ), tutor_time() ) . __( ' ago', 'tutor' ) ); ?> |
| 584 | </span> |
| 585 | </div> |
| 586 | <div class="tutor-comment-text tutor-fs-6 tutor-mt-4"> |
| 587 | <?php echo esc_html( $reply->comment_content ); ?> |
| 588 | </div> |
| 589 | </div> |
| 590 | </div> |
| 591 | </div> |
| 592 | <?php |
| 593 | $html = ob_get_clean(); |
| 594 | wp_send_json_success( array( 'html' => $html ) ); |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * Get comments |
| 599 | * |
| 600 | * @since 2.0.6 |
| 601 | * @see Checkout arguments details: https://developer.wordpress.org/reference/classes/wp_comment_query/__construct/ |
| 602 | * |
| 603 | * @param array $args arguments. |
| 604 | * @return mixed based on arguments |
| 605 | */ |
| 606 | public static function get_comments( array $args ) { |
| 607 | $comments = get_comments( $args ); |
| 608 | return $comments; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * Create comment |
| 613 | * |
| 614 | * @since 1.0.0 |
| 615 | * |
| 616 | * @param array $request request. |
| 617 | * @return mixed comment id on success, false on failure |
| 618 | */ |
| 619 | public static function create_comment( array $request ) { |
| 620 | $current_user = wp_get_current_user(); |
| 621 | $default_data = array( |
| 622 | 'comment_content' => '', |
| 623 | 'comment_post_ID' => '', |
| 624 | 'comment_parent' => '', |
| 625 | 'user_id' => $current_user->ID, |
| 626 | 'comment_author' => $current_user->user_login, |
| 627 | 'comment_author_email' => $current_user->user_email, |
| 628 | 'comment_author_url' => $current_user->user_url, |
| 629 | 'comment_agent' => 'Tutor', |
| 630 | ); |
| 631 | $comment_data = wp_parse_args( $request, $default_data ); |
| 632 | return wp_insert_comment( $comment_data ); |
| 633 | } |
| 634 | |
| 635 | } |
| 636 | |
| 637 | |
| 638 |