| 1 |
<?php |
| 2 |
namespace TUTOR; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) |
| 5 |
exit; |
| 6 |
|
| 7 |
class Lesson extends Tutor_Base { |
| 8 |
public function __construct() { |
| 9 |
parent::__construct(); |
| 10 |
|
| 11 |
add_action( 'add_meta_boxes', array($this, 'register_meta_box') ); |
| 12 |
add_action('save_post_'.$this->lesson_post_type, array($this, "save_lesson_meta")); |
| 13 |
|
| 14 |
add_action('wp_ajax_tutor_load_edit_lesson_modal', array($this, "tutor_load_edit_lesson_modal")); |
| 15 |
add_action('wp_ajax_tutor_modal_create_or_update_lesson', array($this, "tutor_modal_create_or_update_lesson")); |
| 16 |
add_action('wp_ajax_tutor_delete_lesson_by_id', array($this, "tutor_delete_lesson_by_id")); |
| 17 |
|
| 18 |
add_filter('get_sample_permalink', array($this, 'change_lesson_permalink'), 10, 2); |
| 19 |
add_action('admin_init', array($this, 'flush_rewrite_rules')); |
| 20 |
|
| 21 |
/** |
| 22 |
* Add Column |
| 23 |
*/ |
| 24 |
|
| 25 |
add_filter( "manage_{$this->lesson_post_type}_posts_columns", array($this, 'add_column'), 10,1 ); |
| 26 |
add_action( "manage_{$this->lesson_post_type}_posts_custom_column" , array($this, 'custom_lesson_column'), 10, 2 ); |
| 27 |
|
| 28 |
//Frontend Action |
| 29 |
add_action('template_redirect', array($this, 'mark_lesson_complete')); |
| 30 |
|
| 31 |
add_action('wp_ajax_tutor_render_lesson_content', array($this, "tutor_render_lesson_content")); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Registering metabox |
| 36 |
*/ |
| 37 |
public function register_meta_box(){ |
| 38 |
$lesson_post_type = $this->lesson_post_type; |
| 39 |
|
| 40 |
add_meta_box( 'tutor-course-select', __( 'Select Course', 'tutor' ), array($this, 'lesson_metabox'), $lesson_post_type ); |
| 41 |
add_meta_box( 'tutor-lesson-videos', __( 'Lesson Video', 'tutor' ), array($this, 'lesson_video_metabox'), $lesson_post_type ); |
| 42 |
add_meta_box( 'tutor-lesson-attachments', __( 'Attachments', 'tutor' ), array($this, 'lesson_attachments_metabox'), $lesson_post_type ); |
| 43 |
} |
| 44 |
|
| 45 |
public function lesson_metabox(){ |
| 46 |
include tutor()->path.'views/metabox/lesson-metabox.php'; |
| 47 |
} |
| 48 |
|
| 49 |
public function lesson_video_metabox(){ |
| 50 |
include tutor()->path.'views/metabox/video-metabox.php'; |
| 51 |
} |
| 52 |
|
| 53 |
public function lesson_attachments_metabox(){ |
| 54 |
include tutor()->path.'views/metabox/lesson-attachments-metabox.php'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @param $post_ID |
| 59 |
* |
| 60 |
* Saving lesson meta and assets |
| 61 |
* |
| 62 |
*/ |
| 63 |
public function save_lesson_meta($post_ID){ |
| 64 |
//Course |
| 65 |
if (isset($_POST['selected_course'])) { |
| 66 |
$course_id = (int) sanitize_text_field( $_POST['selected_course'] ); |
| 67 |
if ( $course_id ) { |
| 68 |
update_post_meta( $post_ID, '_tutor_course_id_for_lesson', $course_id ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
//Video |
| 73 |
if ( ! empty($_POST['video']['source'])){ |
| 74 |
//$video = tutor_utils()->sanitize_array($_POST['video']); |
| 75 |
$video = tutor_utils()->array_get('video', $_POST); |
| 76 |
update_post_meta($post_ID, '_video', $video); |
| 77 |
}else{ |
| 78 |
delete_post_meta($post_ID, '_video'); |
| 79 |
} |
| 80 |
|
| 81 |
//Attachments |
| 82 |
$attachments = array(); |
| 83 |
if ( ! empty($_POST['tutor_attachments'])){ |
| 84 |
$attachments = tutor_utils()->sanitize_array($_POST['tutor_attachments']); |
| 85 |
$attachments = array_unique($attachments); |
| 86 |
} |
| 87 |
update_post_meta($post_ID, '_tutor_attachments', $attachments); |
| 88 |
} |
| 89 |
|
| 90 |
public function tutor_load_edit_lesson_modal(){ |
| 91 |
$lesson_id = (int) tutor_utils()->avalue_dot('lesson_id', $_POST); |
| 92 |
$topic_id = (int) sanitize_text_field( $_POST['topic_id'] ); |
| 93 |
|
| 94 |
/** |
| 95 |
* If Lesson Not Exists, create One |
| 96 |
*/ |
| 97 |
if ( ! $lesson_id){ |
| 98 |
$course_id = (int) sanitize_text_field( $_POST['course_id'] ); |
| 99 |
|
| 100 |
$post_arr = array( |
| 101 |
'post_type' => $this->lesson_post_type, |
| 102 |
'post_title' => __('Draft Lesson', 'tutor'), |
| 103 |
'post_status' => 'publish', |
| 104 |
'post_author' => get_current_user_id(), |
| 105 |
'post_parent' => $topic_id, |
| 106 |
); |
| 107 |
$lesson_id = wp_insert_post( $post_arr ); |
| 108 |
if ($lesson_id ) { |
| 109 |
update_post_meta( $lesson_id, '_tutor_course_id_for_lesson', $course_id ); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
$post = get_post($lesson_id); |
| 114 |
ob_start(); |
| 115 |
include tutor()->path.'views/modal/edit-lesson.php'; |
| 116 |
$output = ob_get_clean(); |
| 117 |
|
| 118 |
wp_send_json_success(array('output' => $output)); |
| 119 |
} |
| 120 |
|
| 121 |
public function tutor_modal_create_or_update_lesson(){ |
| 122 |
$lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST)); |
| 123 |
$_lesson_thumbnail_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('_lesson_thumbnail_id', $_POST)); |
| 124 |
|
| 125 |
$title = sanitize_text_field($_POST['lesson_title']); |
| 126 |
$lesson_content = wp_kses_post($_POST['lesson_content']); |
| 127 |
|
| 128 |
$lesson_data = array( |
| 129 |
'ID' => $lesson_id, |
| 130 |
'post_title' => $title, |
| 131 |
'post_name' => sanitize_title($title), |
| 132 |
'post_content' => $lesson_content, |
| 133 |
); |
| 134 |
|
| 135 |
if ($_lesson_thumbnail_id){ |
| 136 |
$lesson_data['_thumbnail_id'] = $_lesson_thumbnail_id; |
| 137 |
} |
| 138 |
wp_update_post($lesson_data); |
| 139 |
|
| 140 |
$course_id = tutor_utils()->get_course_id_by_lesson($lesson_id); |
| 141 |
|
| 142 |
ob_start(); |
| 143 |
include tutor()->path.'views/metabox/course-contents.php'; |
| 144 |
$course_contents = ob_get_clean(); |
| 145 |
|
| 146 |
wp_send_json_success(array('course_contents' => $course_contents)); |
| 147 |
} |
| 148 |
|
| 149 |
/** |
| 150 |
* Delete Lesson from course builder |
| 151 |
*/ |
| 152 |
public function tutor_delete_lesson_by_id(){ |
| 153 |
$lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST)); |
| 154 |
wp_delete_post($lesson_id, true); |
| 155 |
delete_post_meta($lesson_id, '_tutor_course_id_for_lesson'); |
| 156 |
wp_send_json_success(); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
/** |
| 161 |
* @param $uri |
| 162 |
* @param $lesson_id |
| 163 |
* |
| 164 |
* @return mixed |
| 165 |
* |
| 166 |
* Changed the URI based |
| 167 |
*/ |
| 168 |
|
| 169 |
public function change_lesson_permalink($uri, $lesson_id){ |
| 170 |
$post = get_post($lesson_id); |
| 171 |
|
| 172 |
if ($post && $post->post_type === $this->lesson_post_type){ |
| 173 |
$uri_base = trailingslashit(site_url()); |
| 174 |
|
| 175 |
$sample_course = "sample-course"; |
| 176 |
$is_course = get_post_meta(get_the_ID(), '_tutor_course_id_for_lesson', true); |
| 177 |
if ($is_course){ |
| 178 |
$course = get_post($is_course); |
| 179 |
if ($course){ |
| 180 |
$sample_course = $course->post_name; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
$new_course_base = $uri_base."course/{$sample_course}/lesson/%pagename%/"; |
| 185 |
$uri[0] = $new_course_base; |
| 186 |
} |
| 187 |
|
| 188 |
return $uri; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
public function flush_rewrite_rules(){ |
| 193 |
$is_required_flush = get_option('required_rewrite_flush'); |
| 194 |
if ($is_required_flush){ |
| 195 |
flush_rewrite_rules(); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
public function add_column($columns){ |
| 201 |
$date_col = $columns['date']; |
| 202 |
unset($columns['date']); |
| 203 |
$columns['course'] = __('Course', 'tutor'); |
| 204 |
$columns['date'] = $date_col; |
| 205 |
|
| 206 |
return $columns; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* @param $column |
| 211 |
* @param $post_id |
| 212 |
* |
| 213 |
*/ |
| 214 |
public function custom_lesson_column($column, $post_id ){ |
| 215 |
if ($column === 'course'){ |
| 216 |
|
| 217 |
$course_id = get_post_meta($post_id, '_tutor_course_id_for_lesson', true); |
| 218 |
if ($course_id){ |
| 219 |
echo '<a href="'.admin_url('post.php?post='.$course_id.'&action=edit').'">'.get_the_title($course_id).'</a>'; |
| 220 |
} |
| 221 |
|
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* |
| 227 |
* Mark lesson completed |
| 228 |
* |
| 229 |
* @since v.1.0.0 |
| 230 |
*/ |
| 231 |
public function mark_lesson_complete(){ |
| 232 |
if ( ! isset($_POST['tutor_action']) || $_POST['tutor_action'] !== 'tutor_complete_lesson' ){ |
| 233 |
return; |
| 234 |
} |
| 235 |
//Checking nonce |
| 236 |
tutor_utils()->checking_nonce(); |
| 237 |
|
| 238 |
$user_id = get_current_user_id(); |
| 239 |
|
| 240 |
//TODO: need to show view if not signed_in |
| 241 |
if ( ! $user_id){ |
| 242 |
die(__('Please Sign-In', 'tutor')); |
| 243 |
} |
| 244 |
|
| 245 |
$lesson_id = (int) sanitize_text_field($_POST['lesson_id']); |
| 246 |
|
| 247 |
do_action('tutor_lesson_completed_before', $lesson_id); |
| 248 |
/** |
| 249 |
* Marking lesson at user meta, meta format, _tutor_completed_lesson_id_{id} and value = time(); |
| 250 |
*/ |
| 251 |
tutor_utils()->mark_lesson_complete($lesson_id); |
| 252 |
|
| 253 |
do_action('tutor_lesson_completed_after', $lesson_id); |
| 254 |
|
| 255 |
|
| 256 |
wp_redirect(get_the_permalink($lesson_id)); |
| 257 |
} |
| 258 |
|
| 259 |
/** |
| 260 |
* Render the lesson content |
| 261 |
*/ |
| 262 |
public function tutor_render_lesson_content(){ |
| 263 |
$lesson_id = (int) sanitize_text_field(tutor_utils()->avalue_dot('lesson_id', $_POST)); |
| 264 |
|
| 265 |
ob_start(); |
| 266 |
global $post; |
| 267 |
|
| 268 |
$post = get_post($lesson_id); |
| 269 |
setup_postdata($post); |
| 270 |
tutor_lesson_content(); |
| 271 |
wp_reset_postdata(); |
| 272 |
|
| 273 |
$html = ob_get_clean(); |
| 274 |
wp_send_json_success(array('html' => $html)); |
| 275 |
} |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|