| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Section_CURD |
| 5 |
* |
| 6 |
* @since 3.0.0 |
| 7 |
*/ |
| 8 |
class LP_Section_CURD extends LP_Object_Data_CURD implements LP_Interface_CURD { |
| 9 |
/** |
| 10 |
* @var int |
| 11 |
* |
| 12 |
* @since 3.0.0 |
| 13 |
*/ |
| 14 |
private $course_id = null; |
| 15 |
|
| 16 |
/** |
| 17 |
* LP_Section_CURD constructor. |
| 18 |
* |
| 19 |
* @param $course_id |
| 20 |
* |
| 21 |
* @since 3.0.0 |
| 22 |
*/ |
| 23 |
public function __construct( $course_id ) { |
| 24 |
$this->course_id = $course_id; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Create item and insert to database. |
| 29 |
* |
| 30 |
* @param $section_origin array |
| 31 |
* |
| 32 |
* @return array |
| 33 |
* @since 3.0.0 |
| 34 |
* @version 3.0.1 |
| 35 |
*/ |
| 36 |
public function create( &$section_origin ) { |
| 37 |
global $wpdb; |
| 38 |
$section = []; |
| 39 |
|
| 40 |
try { |
| 41 |
$section = $this->parse( $section_origin ); |
| 42 |
//$section = stripslashes_deep( $section ); |
| 43 |
$last_section_order_number = LP_Section_DB::getInstance()->get_last_number_order( $section['section_course_id'] ); |
| 44 |
$section_order_new = $last_section_order_number + 1; |
| 45 |
$insert_data = apply_filters( |
| 46 |
'lp/section/data-insert', |
| 47 |
array( |
| 48 |
'section_course_id' => $this->course_id, |
| 49 |
'section_name' => $section['section_name'], |
| 50 |
'section_order' => $section_order_new, |
| 51 |
'section_description' => $section['section_description'], |
| 52 |
), |
| 53 |
$section_origin |
| 54 |
); |
| 55 |
|
| 56 |
$wpdb->insert( |
| 57 |
$wpdb->learnpress_sections, |
| 58 |
$insert_data, |
| 59 |
array( '%d', '%s', '%d', '%s' ) |
| 60 |
); |
| 61 |
$section['section_id'] = $wpdb->insert_id; |
| 62 |
|
| 63 |
do_action( 'lp/section/created', $section ); |
| 64 |
} catch ( Throwable $e ) { |
| 65 |
error_log( $e->getMessage() ); |
| 66 |
} |
| 67 |
|
| 68 |
return $section; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Update data into database. |
| 73 |
* |
| 74 |
* @param $args |
| 75 |
* |
| 76 |
* @return mixed |
| 77 |
* @since 3.0.0 |
| 78 |
*/ |
| 79 |
public function update( &$args ) { |
| 80 |
|
| 81 |
$section = $this->parse( $args ); |
| 82 |
|
| 83 |
if ( empty( $section['section_id'] ) ) { |
| 84 |
return $this->create( $args ); |
| 85 |
} |
| 86 |
|
| 87 |
$section_id = $section['section_id']; |
| 88 |
$update_data = array( |
| 89 |
'section_name' => $section['section_name'], |
| 90 |
'section_course_id' => $section['section_course_id'], |
| 91 |
'section_order' => $section['section_order'], |
| 92 |
'section_description' => $section['section_description'], |
| 93 |
); |
| 94 |
|
| 95 |
global $wpdb; |
| 96 |
|
| 97 |
$wpdb->update( |
| 98 |
$wpdb->learnpress_sections, |
| 99 |
$update_data, |
| 100 |
array( 'section_id' => $section_id ) |
| 101 |
); |
| 102 |
$section['section_id'] = $section_id; |
| 103 |
|
| 104 |
return $section; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Delete section data from database. |
| 109 |
* |
| 110 |
* @param $id string |
| 111 |
* |
| 112 |
* @return bool |
| 113 |
* @since 3.0.0 |
| 114 |
*/ |
| 115 |
public function delete( &$id ) { |
| 116 |
|
| 117 |
global $wpdb; |
| 118 |
|
| 119 |
// allow hook |
| 120 |
do_action( 'learn-press/before-delete-section', $this->course_id, $id ); |
| 121 |
|
| 122 |
// Remove section items. |
| 123 |
$wpdb->delete( $wpdb->learnpress_section_items, array( 'section_id' => $id ) ); |
| 124 |
learn_press_reset_auto_increment( 'learnpress_section_items' ); |
| 125 |
|
| 126 |
// Remove section |
| 127 |
$result = $wpdb->delete( $wpdb->learnpress_sections, array( 'section_id' => $id ) ); |
| 128 |
learn_press_reset_auto_increment( 'learnpress_sections' ); |
| 129 |
|
| 130 |
return ! ! $result; |
| 131 |
} |
| 132 |
|
| 133 |
public function duplicate( &$section, $args = array() ) { |
| 134 |
// TODO: Implement duplicate() method. |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Remove all items from each section and delete course's sections. |
| 139 |
* |
| 140 |
* @return bool |
| 141 |
* @since 3.0.0 |
| 142 |
* @editor tungnx |
| 143 |
* @modify 4.1.4.1 - comment - not use |
| 144 |
*/ |
| 145 |
/*public function clear() { |
| 146 |
|
| 147 |
$sections_ids = LP_Object_Cache::get( 'course-' . $this->course_id, 'learn-press/course-sections-ids' ); |
| 148 |
|
| 149 |
if ( ! $sections_ids ) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
global $wpdb; |
| 154 |
|
| 155 |
do_action( 'learn-press/before-clear-section', $this->course_id ); |
| 156 |
|
| 157 |
// Remove all items in course's sections |
| 158 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}learnpress_section_items WHERE %d AND section_id IN(" . join( ',', $sections_ids ) . ')', 1 ) ); |
| 159 |
learn_press_reset_auto_increment( 'learnpress_section_items' ); |
| 160 |
// delete sections ids cache |
| 161 |
// wp_cache_delete( 'course-' . $this->course_id, 'learn-press/course-sections-ids' ); |
| 162 |
|
| 163 |
// delete sections in course |
| 164 |
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}learnpress_sections WHERE section_course_id = %d", $this->course_id ) ); |
| 165 |
learn_press_reset_auto_increment( 'learnpress_sections' ); |
| 166 |
// delete sections cache |
| 167 |
// wp_cache_delete( 'course-' . $this->course_id, 'learn-press/course-sections' ); |
| 168 |
|
| 169 |
LP_Course_Utils::set_cache_db_sections( $this->course_id, false ); |
| 170 |
|
| 171 |
return true; |
| 172 |
}*/ |
| 173 |
|
| 174 |
/** |
| 175 |
* Load data from database. |
| 176 |
* |
| 177 |
* @param object $object |
| 178 |
* |
| 179 |
* @return mixed |
| 180 |
* @since 3.0.0 |
| 181 |
*/ |
| 182 |
public function load( &$object ) { |
| 183 |
// TODO: Implement load() method. |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Parse input data. |
| 188 |
* |
| 189 |
* @param $args |
| 190 |
* |
| 191 |
* @return array |
| 192 |
* @since 3.0.0 |
| 193 |
*/ |
| 194 |
private function parse( $args ) { |
| 195 |
$data = wp_parse_args( |
| 196 |
$args, |
| 197 |
array( |
| 198 |
'section_name' => '', |
| 199 |
'section_description' => '', |
| 200 |
'section_course_id' => 0, |
| 201 |
'section_order' => 0, |
| 202 |
'items' => array(), |
| 203 |
) |
| 204 |
); |
| 205 |
|
| 206 |
return $data; |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Get course sections ids and set data to cache. |
| 211 |
* |
| 212 |
* @return array |
| 213 |
* @since 3.0.0 |
| 214 |
* @deprecated 4.1.6.3 |
| 215 |
*/ |
| 216 |
/*public function read_sections_ids() { |
| 217 |
|
| 218 |
// Get course's sections id data from cache |
| 219 |
$ids = LP_Object_Cache::get( 'course-' . $this->course_id, 'learn-press/course-sections-ids' ); |
| 220 |
|
| 221 |
if ( ! $ids ) { |
| 222 |
global $wpdb; |
| 223 |
// get sections id |
| 224 |
$ids = $wpdb->get_col( $wpdb->prepare( "SELECT section_id FROM {$wpdb->prefix}learnpress_sections WHERE section_course_id = %d", $this->course_id ) ); |
| 225 |
// Set cache |
| 226 |
LP_Object_Cache::set( 'course-' . $this->course_id, $ids, 'learn-press/course-sections-ids' ); |
| 227 |
} |
| 228 |
|
| 229 |
return $ids; |
| 230 |
}*/ |
| 231 |
|
| 232 |
/** |
| 233 |
* Read all items from DB |
| 234 |
* |
| 235 |
* @param int $section_id |
| 236 |
* |
| 237 |
* @return array |
| 238 |
* @depecated 4.1.6.9 Remove when release Addon Frontend Editor 4.0.1 |
| 239 |
*/ |
| 240 |
public function read_items( $section_id ) { |
| 241 |
global $wpdb; |
| 242 |
|
| 243 |
$query = $wpdb->prepare( |
| 244 |
" |
| 245 |
SELECT item_id id |
| 246 |
FROM {$wpdb->learnpress_section_items} si |
| 247 |
INNER JOIN {$wpdb->learnpress_sections} s ON si.section_id = s.section_id |
| 248 |
INNER JOIN {$wpdb->posts} c ON c.ID = s.section_course_id |
| 249 |
INNER JOIN {$wpdb->posts} it ON it.ID = si.item_id |
| 250 |
WHERE s.section_id = %d |
| 251 |
AND it.post_status = %s |
| 252 |
ORDER BY si.item_order, si.section_item_id ASC |
| 253 |
", |
| 254 |
$section_id, |
| 255 |
'publish' |
| 256 |
); |
| 257 |
|
| 258 |
return $wpdb->get_col( $query ); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* Update sort sections. |
| 263 |
* |
| 264 |
* @param string[] $sections_new_order |
| 265 |
* |
| 266 |
* @return array |
| 267 |
* @version 4.0.1 |
| 268 |
*/ |
| 269 |
public function update_sections_order( $sections_new_order ) { |
| 270 |
global $wpdb; |
| 271 |
|
| 272 |
//$current_sections = LP_Course_Utils::get_cached_db_sections( $this->course_id ); |
| 273 |
//$new_sections = array(); |
| 274 |
//$course = learn_press_get_course( $this->course_id ); |
| 275 |
//$sections_items = $course->get_full_sections_and_items_course(); |
| 276 |
|
| 277 |
$orders = array(); |
| 278 |
|
| 279 |
foreach ( $sections_new_order as $order_new => $section_id ) { |
| 280 |
$order_new = $order_new + 1; |
| 281 |
|
| 282 |
$wpdb->update( |
| 283 |
$wpdb->learnpress_sections, |
| 284 |
array( 'section_order' => $order_new ), |
| 285 |
array( 'section_id' => $section_id ) |
| 286 |
); |
| 287 |
|
| 288 |
// $this->get_section_items( $section_id ); |
| 289 |
} |
| 290 |
|
| 291 |
/*foreach ( $sections as $index => $section_id ) { |
| 292 |
$order = $index + 1; |
| 293 |
|
| 294 |
$orders[ $section_id ] = $order; |
| 295 |
|
| 296 |
$wpdb->update( |
| 297 |
$wpdb->learnpress_sections, |
| 298 |
array( 'section_order' => $order ), |
| 299 |
array( 'section_id' => $section_id ) |
| 300 |
); |
| 301 |
|
| 302 |
foreach ( $current_sections as $current_section ) { |
| 303 |
if ( $current_section->section_id == $section_id ) { |
| 304 |
$new_sections[ $index ] = $current_section; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
$this->get_section_items( $section_id ); |
| 309 |
}*/ |
| 310 |
|
| 311 |
//LP_Course_Utils::set_cache_db_sections( $this->course_id, $new_sections ); |
| 312 |
|
| 313 |
return $orders; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Get list items of section. |
| 318 |
* |
| 319 |
* @param $section_id |
| 320 |
* |
| 321 |
* @return array |
| 322 |
* @since 3.0.0 |
| 323 |
*/ |
| 324 |
public function get_section_items( $section_id ) { |
| 325 |
$course = learn_press_get_course( $this->course_id ); |
| 326 |
|
| 327 |
$sections = $course->get_curriculum_raw(); |
| 328 |
|
| 329 |
$return = array(); |
| 330 |
|
| 331 |
if ( ! empty( $sections ) ) { |
| 332 |
foreach ( $sections as $section ) { |
| 333 |
if ( $section['id'] == $section_id ) { |
| 334 |
if ( isset( $section['items'] ) && is_array( $section['items'] ) ) { |
| 335 |
$return = $section['items']; |
| 336 |
} |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
// LP_Object_Cache::set( 'course-' . $this->course_id . '-' . $section_id, $return, 'learn-press/course-section-items' ); |
| 342 |
|
| 343 |
return $return; |
| 344 |
} |
| 345 |
|
| 346 |
/** |
| 347 |
* Create new section item and add to course. |
| 348 |
* |
| 349 |
* @param int $section_id |
| 350 |
* @param array $item |
| 351 |
* |
| 352 |
* @return array | bool |
| 353 |
* @since 3.0.0 |
| 354 |
*/ |
| 355 |
public function new_item( $section_id, $item ) { |
| 356 |
// course author, for case co-instructor add new items. |
| 357 |
$author_id = get_current_user_id(); |
| 358 |
|
| 359 |
if ( ! $author_id ) { |
| 360 |
$author_id = get_post_field( 'post_author', $this->course_id ) ? get_post_field( 'post_author', $this->course_id ) : learn_press_get_current_user_id(); |
| 361 |
} |
| 362 |
|
| 363 |
// $item = wp_parse_args( $item, array( 'title' => '', 'type' => '' ) ); |
| 364 |
$item = array_merge( |
| 365 |
array( |
| 366 |
'title' => '', |
| 367 |
'type' => '', |
| 368 |
), |
| 369 |
$item |
| 370 |
); |
| 371 |
|
| 372 |
$args = array( |
| 373 |
'title' => $item['title'], |
| 374 |
'author' => $author_id, |
| 375 |
); |
| 376 |
|
| 377 |
if ( ! empty( $item['id'] ) ) { |
| 378 |
$item['old_id'] = $item ['id']; |
| 379 |
} |
| 380 |
if ( $item['type'] == LP_LESSON_CPT ) { |
| 381 |
$lesson_curd = new LP_Lesson_CURD(); |
| 382 |
$item['id'] = $lesson_curd->create( $args ); |
| 383 |
} elseif ( $item['type'] == LP_QUIZ_CPT ) { |
| 384 |
$quiz_curd = new LP_Quiz_CURD(); |
| 385 |
$item['id'] = $quiz_curd->create( $args ); |
| 386 |
} else { |
| 387 |
$item['id'] = apply_filters( 'learn-press/new-section-item-data', $item['id'], $item, $args, $this->course_id ); |
| 388 |
} |
| 389 |
|
| 390 |
if ( is_wp_error( $item['id'] ) || ! $item['id'] ) { |
| 391 |
return false; |
| 392 |
} |
| 393 |
|
| 394 |
$item['preview'] = get_post_meta( $item['id'], '_lp_preview', true ) == 'yes'; |
| 395 |
|
| 396 |
// allow hook |
| 397 |
do_action( 'learn-press/after-new-section-item', $item['id'], $section_id, $this->course_id ); |
| 398 |
|
| 399 |
// add item to section |
| 400 |
return $this->add_items_section( $section_id, array( $item ) ); |
| 401 |
} |
| 402 |
|
| 403 |
/** |
| 404 |
* Add list new items to section. |
| 405 |
* |
| 406 |
* @param $section_id |
| 407 |
* @param array $items |
| 408 |
* |
| 409 |
* @return array |
| 410 |
*/ |
| 411 |
public function add_items_section( $section_id, $items = array() ) { |
| 412 |
// $order = 1; |
| 413 |
$current_items = $this->get_section_items( $section_id ); |
| 414 |
// allow hook |
| 415 |
do_action( 'learn-press/before-add-items-section', $items, $section_id, $this->course_id ); |
| 416 |
|
| 417 |
global $wpdb; |
| 418 |
|
| 419 |
$all_items = array_merge( $current_items, $items ); |
| 420 |
$result = array(); |
| 421 |
foreach ( $all_items as $key => $item ) { |
| 422 |
if ( ! isset( $item['id'] ) || ! isset( $item['type'] ) ) { |
| 423 |
continue; |
| 424 |
} |
| 425 |
|
| 426 |
$item = (array) $item; |
| 427 |
$exist = $this->item_section_exist( $section_id, $item['id'] ); |
| 428 |
|
| 429 |
if ( $exist ) { |
| 430 |
$wpdb->update( |
| 431 |
$wpdb->learnpress_section_items, |
| 432 |
array( 'item_order' => $key ), |
| 433 |
array( |
| 434 |
'section_id' => $section_id, |
| 435 |
'item_id' => $item['id'], |
| 436 |
) |
| 437 |
); |
| 438 |
} else { |
| 439 |
$wpdb->insert( |
| 440 |
$wpdb->learnpress_section_items, |
| 441 |
array( |
| 442 |
'section_id' => $section_id, |
| 443 |
'item_id' => $item['id'], |
| 444 |
'item_order' => $key, |
| 445 |
'item_type' => $item['type'], |
| 446 |
) |
| 447 |
); |
| 448 |
} |
| 449 |
// get WP Post |
| 450 |
$post = get_post( $item['id'] ); |
| 451 |
if ( ! $post ) { |
| 452 |
continue; |
| 453 |
} |
| 454 |
|
| 455 |
$item = array_merge( |
| 456 |
$item, |
| 457 |
array( |
| 458 |
'id' => $post->ID, |
| 459 |
'title' => $post->post_title, |
| 460 |
'type' => $post->post_type, |
| 461 |
'preview' => get_post_meta( $post->ID, '_lp_preview', true ) == 'yes', |
| 462 |
) |
| 463 |
); |
| 464 |
|
| 465 |
if ( ! $exist ) { |
| 466 |
do_action( 'learn-press/added-item-to-section', $item, $section_id, $this->course_id ); |
| 467 |
} else { |
| 468 |
do_action( 'learn-press/updated-item-to-section', $item, $section_id, $this->course_id ); |
| 469 |
} |
| 470 |
|
| 471 |
$result[] = $item; |
| 472 |
} |
| 473 |
|
| 474 |
return $result; |
| 475 |
} |
| 476 |
|
| 477 |
/** |
| 478 |
* Add item to section. |
| 479 |
* |
| 480 |
* @param int $section_id |
| 481 |
* @param array $item |
| 482 |
* |
| 483 |
* @return void |
| 484 |
* @version 1.0.0 |
| 485 |
* @since 4.1.7 |
| 486 |
*/ |
| 487 |
public function assign_item_section( int $section_id, array $item = array() ) { |
| 488 |
global $wpdb; |
| 489 |
|
| 490 |
if ( ! isset( $item['item_id'] ) && ! isset( $item['item_type'] ) ) { |
| 491 |
return; |
| 492 |
} |
| 493 |
|
| 494 |
try { |
| 495 |
$exist = $this->item_section_exist( $section_id, $item['item_id'] ); |
| 496 |
|
| 497 |
if ( $exist ) { |
| 498 |
$wpdb->update( |
| 499 |
$wpdb->learnpress_section_items, |
| 500 |
array( 'item_order' => $item['item_order'] ?? 0 ), |
| 501 |
array( |
| 502 |
'section_id' => $section_id, |
| 503 |
'item_id' => $item['item_id'], |
| 504 |
) |
| 505 |
); |
| 506 |
} else { |
| 507 |
$wpdb->insert( |
| 508 |
$wpdb->learnpress_section_items, |
| 509 |
array( |
| 510 |
'section_id' => $section_id, |
| 511 |
'item_id' => $item['item_id'], |
| 512 |
'item_order' => $item['item_order'] ?? 0, |
| 513 |
'item_type' => $item['item_type'], |
| 514 |
) |
| 515 |
); |
| 516 |
} |
| 517 |
} catch ( Throwable $e ) { |
| 518 |
error_log( $e->getMessage() ); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
/** |
| 523 |
* Check item was been added to any section. |
| 524 |
* |
| 525 |
* @param $section_id |
| 526 |
* @param $item_id |
| 527 |
* |
| 528 |
* @return bool |
| 529 |
* @since 3.0.0 |
| 530 |
*/ |
| 531 |
private function item_section_exist( $section_id, $item_id ) { |
| 532 |
global $wpdb; |
| 533 |
|
| 534 |
$section_id = intval( $section_id ); |
| 535 |
$item_id = intval( $item_id ); |
| 536 |
|
| 537 |
$query = $wpdb->prepare( "SELECT * FROM {$wpdb->learnpress_section_items} WHERE section_id = %d AND item_id = %d", $section_id, $item_id ); |
| 538 |
$item = $wpdb->get_row( $query, ARRAY_A ); |
| 539 |
|
| 540 |
return ! ! $item; |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Get section id of lesson, quiz id in curriculum. |
| 545 |
* |
| 546 |
* @param $item_id |
| 547 |
* |
| 548 |
* @return array|bool|null|object |
| 549 |
*/ |
| 550 |
public function get_item_section( $item_id ) { |
| 551 |
global $wpdb; |
| 552 |
|
| 553 |
$query = $wpdb->prepare( "SELECT section_id, item_order FROM {$wpdb->learnpress_section_items} WHERE item_id = %d", $item_id ); |
| 554 |
$section = $wpdb->get_row( $query, ARRAY_A ); |
| 555 |
|
| 556 |
return $section; |
| 557 |
} |
| 558 |
|
| 559 |
/** |
| 560 |
* Update course final item. |
| 561 |
* |
| 562 |
* @return bool |
| 563 |
* @version 4.0.0 |
| 564 |
* @depecated 4.1.6.9 |
| 565 |
*/ |
| 566 |
public function update_final_item() { |
| 567 |
_deprecated_function( __FUNCTION__, '4.1.6.9' ); |
| 568 |
/*$sections = LP_Course_Utils::get_cached_db_sections( $this->course_id ); |
| 569 |
|
| 570 |
if ( ! $sections ) { |
| 571 |
return false; |
| 572 |
} |
| 573 |
|
| 574 |
$last_section = end( $sections ); |
| 575 |
$section_id = $last_section->section_id; |
| 576 |
|
| 577 |
// get last section items |
| 578 |
$section_items = LP_Object_Cache::get( 'course-' . $this->course_id . '-' . $section_id, 'learn-press/course-section-items' ); |
| 579 |
|
| 580 |
$types = apply_filters( 'learn-press/post-types-support-assessment-by-final-item', array( LP_QUIZ_CPT ) ); |
| 581 |
|
| 582 |
if ( $section_items ) { |
| 583 |
// last item in last section |
| 584 |
$final = end( $section_items ); |
| 585 |
|
| 586 |
if ( is_array( $types ) && in_array( $final['type'], $types ) ) { |
| 587 |
update_post_meta( $this->course_id, '_' . substr_replace( $final['type'], '_final_', 2, 1 ), $final['id'] ); |
| 588 |
$diff = array_diff( $types, array( $final['type'] ) ); |
| 589 |
foreach ( $diff as $type ) { |
| 590 |
// delete all other final meta |
| 591 |
delete_post_meta( $this->course_id, '_' . substr_replace( $type, '_final_', 2, 1 ) ); |
| 592 |
} |
| 593 |
} else { |
| 594 |
// for last item is not post type need check final item |
| 595 |
foreach ( $types as $type ) { |
| 596 |
delete_post_meta( $this->course_id, '_' . substr_replace( $type, '_final_', 2, 1 ) ); |
| 597 |
} |
| 598 |
} |
| 599 |
} else { |
| 600 |
// for last section does not has any item |
| 601 |
foreach ( $types as $type ) { |
| 602 |
delete_post_meta( $this->course_id, '_' . substr_replace( $type, '_final_', 2, 1 ) ); |
| 603 |
} |
| 604 |
} |
| 605 |
|
| 606 |
return true;*/ |
| 607 |
|
| 608 |
} |
| 609 |
|
| 610 |
/** |
| 611 |
* Remove section item. |
| 612 |
* |
| 613 |
* @param $section_id |
| 614 |
* @param $item_id |
| 615 |
* |
| 616 |
* @return bool |
| 617 |
* @since 3.0.0 |
| 618 |
*/ |
| 619 |
public function remove_section_item( $section_id, $item_id ) { |
| 620 |
global $wpdb; |
| 621 |
|
| 622 |
$result = $wpdb->delete( |
| 623 |
$wpdb->learnpress_section_items, |
| 624 |
array( |
| 625 |
'section_id' => $section_id, |
| 626 |
'item_id' => $item_id, |
| 627 |
), |
| 628 |
array( |
| 629 |
'%d', |
| 630 |
'%d', |
| 631 |
) |
| 632 |
); |
| 633 |
|
| 634 |
return ! ! $result; |
| 635 |
} |
| 636 |
|
| 637 |
/** |
| 638 |
* Update section items. |
| 639 |
* |
| 640 |
* @param $section_id |
| 641 |
* @param $items array |
| 642 |
* |
| 643 |
* @return array |
| 644 |
* @since 3.0.0 |
| 645 |
*/ |
| 646 |
public function update_section_items( $section_id, $items ) { |
| 647 |
global $wpdb; |
| 648 |
$current_items = $this->get_section_items( $section_id ); |
| 649 |
|
| 650 |
foreach ( $items as $index => $item ) { |
| 651 |
$order = $index + 1; |
| 652 |
$exist = $this->item_section_exist( $section_id, $item['id'] ); |
| 653 |
|
| 654 |
if ( $exist ) { |
| 655 |
$wpdb->update( |
| 656 |
$wpdb->learnpress_section_items, |
| 657 |
array( 'item_order' => $order ), |
| 658 |
array( |
| 659 |
'section_id' => $section_id, |
| 660 |
'item_id' => $item['id'], |
| 661 |
) |
| 662 |
); |
| 663 |
} else { |
| 664 |
$wpdb->insert( |
| 665 |
$wpdb->learnpress_section_items, |
| 666 |
array( |
| 667 |
'section_id' => $section_id, |
| 668 |
'item_id' => $item['id'], |
| 669 |
'item_order' => $order, |
| 670 |
'item_type' => $item['type'], |
| 671 |
) |
| 672 |
); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* Remove non-existent items. |
| 678 |
*/ |
| 679 |
/*foreach ( $current_items as $item ) { |
| 680 |
$find = $this->check_item_exist( $items, $item['id'] ); |
| 681 |
|
| 682 |
if ( ! $find ) { |
| 683 |
$wpdb->delete( |
| 684 |
$wpdb->learnpress_section_items, |
| 685 |
array( |
| 686 |
'section_id' => $section_id, |
| 687 |
'item_id' => $item['id'], |
| 688 |
) |
| 689 |
); |
| 690 |
} |
| 691 |
}*/ |
| 692 |
|
| 693 |
return $items; |
| 694 |
} |
| 695 |
|
| 696 |
/** |
| 697 |
* Check item exist. |
| 698 |
* |
| 699 |
* @param $items array |
| 700 |
* @param $item_id string |
| 701 |
* |
| 702 |
* @return bool |
| 703 |
* @since 3.0.0 |
| 704 |
*/ |
| 705 |
private function check_item_exist( $items, $item_id ) { |
| 706 |
foreach ( $items as $item ) { |
| 707 |
if ( $item['id'] == $item_id ) { |
| 708 |
return true; |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
return false; |
| 713 |
} |
| 714 |
|
| 715 |
/** |
| 716 |
* Update lesson, quiz title in admin course editor. |
| 717 |
* |
| 718 |
* @param $item |
| 719 |
* |
| 720 |
* @return array |
| 721 |
* @since 3.0.0 |
| 722 |
*/ |
| 723 |
public function update_item( $item ) { |
| 724 |
$item = wp_parse_args( |
| 725 |
$item, |
| 726 |
array( |
| 727 |
'id' => '', |
| 728 |
'title' => '', |
| 729 |
) |
| 730 |
); |
| 731 |
|
| 732 |
wp_update_post( |
| 733 |
array( |
| 734 |
'ID' => $item['id'], |
| 735 |
'post_title' => $item['title'], |
| 736 |
) |
| 737 |
); |
| 738 |
|
| 739 |
if ( isset( $item['preview'] ) && $item['preview'] ) { |
| 740 |
update_post_meta( $item['id'], '_lp_preview', 'yes' ); |
| 741 |
} else { |
| 742 |
delete_post_meta( $item['id'], '_lp_preview' ); |
| 743 |
} |
| 744 |
|
| 745 |
return $item; |
| 746 |
} |
| 747 |
} |
| 748 |
|