| 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 |
$section['section_order'] = $section_order_new; |
| 63 |
|
| 64 |
do_action( 'lp/section/created', $section ); |
| 65 |
} catch ( Throwable $e ) { |
| 66 |
error_log( $e->getMessage() ); |
| 67 |
} |
| 68 |
|
| 69 |
return $section; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Update data into database. |
| 74 |
* |
| 75 |
* @param $args |
| 76 |
* |
| 77 |
* @return mixed |
| 78 |
* @since 3.0.0 |
| 79 |
*/ |
| 80 |
public function update( &$args ) { |
| 81 |
|
| 82 |
$section = $this->parse( $args ); |
| 83 |
|
| 84 |
if ( empty( $section['section_id'] ) ) { |
| 85 |
return $this->create( $args ); |
| 86 |
} |
| 87 |
|
| 88 |
$section_id = $section['section_id']; |
| 89 |
$update_data = array( |
| 90 |
'section_name' => $section['section_name'], |
| 91 |
'section_course_id' => $section['section_course_id'], |
| 92 |
'section_order' => $section['section_order'], |
| 93 |
'section_description' => $section['section_description'], |
| 94 |
); |
| 95 |
|
| 96 |
global $wpdb; |
| 97 |
|
| 98 |
$wpdb->update( |
| 99 |
$wpdb->learnpress_sections, |
| 100 |
$update_data, |
| 101 |
array( 'section_id' => $section_id ) |
| 102 |
); |
| 103 |
$section['section_id'] = $section_id; |
| 104 |
|
| 105 |
return $section; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Delete section data from database. |
| 110 |
* |
| 111 |
* @param $id string |
| 112 |
* |
| 113 |
* @return bool |
| 114 |
* @since 3.0.0 |
| 115 |
*/ |
| 116 |
public function delete( &$id ) { |
| 117 |
|
| 118 |
global $wpdb; |
| 119 |
|
| 120 |
// allow hook |
| 121 |
do_action( 'learn-press/before-delete-section', $this->course_id, $id ); |
| 122 |
|
| 123 |
// Remove section items. |
| 124 |
$wpdb->delete( $wpdb->learnpress_section_items, array( 'section_id' => $id ) ); |
| 125 |
learn_press_reset_auto_increment( 'learnpress_section_items' ); |
| 126 |
|
| 127 |
// Remove section |
| 128 |
$result = $wpdb->delete( $wpdb->learnpress_sections, array( 'section_id' => $id ) ); |
| 129 |
learn_press_reset_auto_increment( 'learnpress_sections' ); |
| 130 |
|
| 131 |
return ! ! $result; |
| 132 |
} |
| 133 |
|
| 134 |
public function duplicate( &$section, $args = array() ) { |
| 135 |
// TODO: Implement duplicate() method. |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Load data from database. |
| 140 |
* |
| 141 |
* @param object $object |
| 142 |
* |
| 143 |
* @return mixed |
| 144 |
* @since 3.0.0 |
| 145 |
*/ |
| 146 |
public function load( &$object ) { |
| 147 |
// TODO: Implement load() method. |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Parse input data. |
| 152 |
* |
| 153 |
* @param $args |
| 154 |
* |
| 155 |
* @return array |
| 156 |
* @since 3.0.0 |
| 157 |
*/ |
| 158 |
private function parse( $args ) { |
| 159 |
$data = wp_parse_args( |
| 160 |
$args, |
| 161 |
array( |
| 162 |
'section_name' => '', |
| 163 |
'section_description' => '', |
| 164 |
'section_course_id' => 0, |
| 165 |
'section_order' => 0, |
| 166 |
'items' => array(), |
| 167 |
) |
| 168 |
); |
| 169 |
|
| 170 |
return $data; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Update sort sections. |
| 175 |
* |
| 176 |
* @param string[] $sections_new_order |
| 177 |
* |
| 178 |
* @return array |
| 179 |
* @version 4.0.1 |
| 180 |
*/ |
| 181 |
public function update_sections_order( $sections_new_order ) { |
| 182 |
global $wpdb; |
| 183 |
|
| 184 |
$orders = array(); |
| 185 |
|
| 186 |
foreach ( $sections_new_order as $order_new => $section_id ) { |
| 187 |
$order_new = $order_new + 1; |
| 188 |
|
| 189 |
$wpdb->update( |
| 190 |
$wpdb->learnpress_sections, |
| 191 |
array( 'section_order' => $order_new ), |
| 192 |
array( 'section_id' => $section_id ) |
| 193 |
); |
| 194 |
} |
| 195 |
|
| 196 |
return $orders; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Get list items of section. |
| 201 |
* |
| 202 |
* @param $section_id |
| 203 |
* |
| 204 |
* @return array |
| 205 |
* @since 3.0.0 |
| 206 |
*/ |
| 207 |
public function get_section_items( $section_id ) { |
| 208 |
$course = learn_press_get_course( $this->course_id ); |
| 209 |
|
| 210 |
$sections = $course->get_curriculum_raw(); |
| 211 |
|
| 212 |
$return = array(); |
| 213 |
|
| 214 |
if ( ! empty( $sections ) ) { |
| 215 |
foreach ( $sections as $section ) { |
| 216 |
if ( $section['id'] == $section_id ) { |
| 217 |
if ( isset( $section['items'] ) && is_array( $section['items'] ) ) { |
| 218 |
$return = $section['items']; |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return $return; |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Create new section item and add to course. |
| 229 |
* |
| 230 |
* @param int $section_id |
| 231 |
* @param array $item |
| 232 |
* |
| 233 |
* @return array | bool |
| 234 |
* @since 3.0.0 |
| 235 |
*/ |
| 236 |
public function new_item( $section_id, $item ) { |
| 237 |
// course author, for case co-instructor add new items. |
| 238 |
$author_id = get_current_user_id(); |
| 239 |
|
| 240 |
if ( ! $author_id ) { |
| 241 |
$author_id = get_post_field( 'post_author', $this->course_id ) ? get_post_field( 'post_author', $this->course_id ) : learn_press_get_current_user_id(); |
| 242 |
} |
| 243 |
|
| 244 |
// $item = wp_parse_args( $item, array( 'title' => '', 'type' => '' ) ); |
| 245 |
$item = array_merge( |
| 246 |
array( |
| 247 |
'title' => '', |
| 248 |
'type' => '', |
| 249 |
'content' => '', |
| 250 |
'status' => 'publish', |
| 251 |
), |
| 252 |
$item |
| 253 |
); |
| 254 |
|
| 255 |
$args = array( |
| 256 |
'title' => $item['title'], |
| 257 |
'author' => $author_id, |
| 258 |
'content' => $item['content'], |
| 259 |
'status' => $item['status'], |
| 260 |
); |
| 261 |
|
| 262 |
if ( ! empty( $item['id'] ) ) { |
| 263 |
$item['old_id'] = $item ['id']; |
| 264 |
} |
| 265 |
if ( $item['type'] == LP_LESSON_CPT ) { |
| 266 |
$lesson_curd = new LP_Lesson_CURD(); |
| 267 |
$item['id'] = $lesson_curd->create( $args ); |
| 268 |
} elseif ( $item['type'] == LP_QUIZ_CPT ) { |
| 269 |
$quiz_curd = new LP_Quiz_CURD(); |
| 270 |
$item['id'] = $quiz_curd->create( $args ); |
| 271 |
} else { |
| 272 |
$item['id'] = apply_filters( 'learn-press/new-section-item-data', $item['id'], $item, $args, $this->course_id ); |
| 273 |
} |
| 274 |
|
| 275 |
if ( is_wp_error( $item['id'] ) || ! $item['id'] ) { |
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
$item['preview'] = get_post_meta( $item['id'], '_lp_preview', true ) == 'yes'; |
| 280 |
|
| 281 |
// allow hook |
| 282 |
do_action( 'learn-press/after-new-section-item', $item['id'], $section_id, $this->course_id ); |
| 283 |
|
| 284 |
// add item to section |
| 285 |
return $this->add_items_section( $section_id, array( $item ) ); |
| 286 |
} |
| 287 |
|
| 288 |
/** |
| 289 |
* Add list new items to section. |
| 290 |
* |
| 291 |
* @param $section_id |
| 292 |
* @param array $items |
| 293 |
* |
| 294 |
* @return array |
| 295 |
*/ |
| 296 |
public function add_items_section( $section_id, $items = array() ) { |
| 297 |
// $order = 1; |
| 298 |
$current_items = $this->get_section_items( $section_id ); |
| 299 |
// allow hook |
| 300 |
do_action( 'learn-press/before-add-items-section', $items, $section_id, $this->course_id ); |
| 301 |
|
| 302 |
global $wpdb; |
| 303 |
|
| 304 |
$all_items = array_merge( $current_items, $items ); |
| 305 |
$result = array(); |
| 306 |
foreach ( $all_items as $key => $item ) { |
| 307 |
$key = $key + 1; |
| 308 |
if ( ! isset( $item['id'] ) || ! isset( $item['type'] ) ) { |
| 309 |
continue; |
| 310 |
} |
| 311 |
|
| 312 |
$item = (array) $item; |
| 313 |
$exist = $this->item_section_exist( $section_id, $item['id'] ); |
| 314 |
|
| 315 |
if ( $exist ) { |
| 316 |
$wpdb->update( |
| 317 |
$wpdb->learnpress_section_items, |
| 318 |
array( 'item_order' => $key ), |
| 319 |
array( |
| 320 |
'section_id' => $section_id, |
| 321 |
'item_id' => $item['id'], |
| 322 |
) |
| 323 |
); |
| 324 |
} else { |
| 325 |
$wpdb->insert( |
| 326 |
$wpdb->learnpress_section_items, |
| 327 |
array( |
| 328 |
'section_id' => $section_id, |
| 329 |
'item_id' => $item['id'], |
| 330 |
'item_order' => $key, |
| 331 |
'item_type' => $item['type'], |
| 332 |
) |
| 333 |
); |
| 334 |
} |
| 335 |
// get WP Post |
| 336 |
$post = get_post( $item['id'] ); |
| 337 |
if ( ! $post ) { |
| 338 |
continue; |
| 339 |
} |
| 340 |
|
| 341 |
$item = array_merge( |
| 342 |
$item, |
| 343 |
array( |
| 344 |
'id' => $post->ID, |
| 345 |
'title' => $post->post_title, |
| 346 |
'type' => $post->post_type, |
| 347 |
'preview' => get_post_meta( $post->ID, '_lp_preview', true ) == 'yes', |
| 348 |
) |
| 349 |
); |
| 350 |
|
| 351 |
if ( ! $exist ) { |
| 352 |
do_action( 'learn-press/added-item-to-section', $item, $section_id, $this->course_id ); |
| 353 |
} else { |
| 354 |
do_action( 'learn-press/updated-item-to-section', $item, $section_id, $this->course_id ); |
| 355 |
} |
| 356 |
|
| 357 |
$result[] = $item; |
| 358 |
} |
| 359 |
|
| 360 |
return $result; |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Add item to section. |
| 365 |
* |
| 366 |
* @param int $section_id |
| 367 |
* @param array $item |
| 368 |
* |
| 369 |
* @return void |
| 370 |
* @version 1.0.0 |
| 371 |
* @since 4.1.7 |
| 372 |
*/ |
| 373 |
public function assign_item_section( int $section_id, array $item = array() ) { |
| 374 |
global $wpdb; |
| 375 |
|
| 376 |
if ( ! isset( $item['item_id'] ) && ! isset( $item['item_type'] ) ) { |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
try { |
| 381 |
$exist = $this->item_section_exist( $section_id, $item['item_id'] ); |
| 382 |
|
| 383 |
if ( $exist ) { |
| 384 |
$wpdb->update( |
| 385 |
$wpdb->learnpress_section_items, |
| 386 |
array( 'item_order' => $item['item_order'] ?? 0 ), |
| 387 |
array( |
| 388 |
'section_id' => $section_id, |
| 389 |
'item_id' => $item['item_id'], |
| 390 |
) |
| 391 |
); |
| 392 |
} else { |
| 393 |
$wpdb->insert( |
| 394 |
$wpdb->learnpress_section_items, |
| 395 |
array( |
| 396 |
'section_id' => $section_id, |
| 397 |
'item_id' => $item['item_id'], |
| 398 |
'item_order' => $item['item_order'] ?? 0, |
| 399 |
'item_type' => $item['item_type'], |
| 400 |
) |
| 401 |
); |
| 402 |
} |
| 403 |
} catch ( Throwable $e ) { |
| 404 |
error_log( $e->getMessage() ); |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Check item was been added to any section. |
| 410 |
* |
| 411 |
* @param $section_id |
| 412 |
* @param $item_id |
| 413 |
* |
| 414 |
* @return bool |
| 415 |
* @since 3.0.0 |
| 416 |
*/ |
| 417 |
private function item_section_exist( $section_id, $item_id ) { |
| 418 |
global $wpdb; |
| 419 |
|
| 420 |
$section_id = intval( $section_id ); |
| 421 |
$item_id = intval( $item_id ); |
| 422 |
|
| 423 |
$query = $wpdb->prepare( "SELECT * FROM {$wpdb->learnpress_section_items} WHERE section_id = %d AND item_id = %d", $section_id, $item_id ); |
| 424 |
$item = $wpdb->get_row( $query, ARRAY_A ); |
| 425 |
|
| 426 |
return ! ! $item; |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Get section id of lesson, quiz id in curriculum. |
| 431 |
* |
| 432 |
* @param $item_id |
| 433 |
* |
| 434 |
* @return array|bool|null|object |
| 435 |
*/ |
| 436 |
public function get_item_section( $item_id ) { |
| 437 |
global $wpdb; |
| 438 |
|
| 439 |
$query = $wpdb->prepare( "SELECT section_id, item_order FROM {$wpdb->learnpress_section_items} WHERE item_id = %d", $item_id ); |
| 440 |
$section = $wpdb->get_row( $query, ARRAY_A ); |
| 441 |
|
| 442 |
return $section; |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Remove section item. |
| 447 |
* |
| 448 |
* @param $section_id |
| 449 |
* @param $item_id |
| 450 |
* |
| 451 |
* @return bool |
| 452 |
* @since 3.0.0 |
| 453 |
*/ |
| 454 |
public function remove_section_item( $section_id, $item_id ) { |
| 455 |
global $wpdb; |
| 456 |
|
| 457 |
$result = $wpdb->delete( |
| 458 |
$wpdb->learnpress_section_items, |
| 459 |
array( |
| 460 |
'section_id' => $section_id, |
| 461 |
'item_id' => $item_id, |
| 462 |
), |
| 463 |
array( |
| 464 |
'%d', |
| 465 |
'%d', |
| 466 |
) |
| 467 |
); |
| 468 |
|
| 469 |
return ! ! $result; |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Update section items. |
| 474 |
* |
| 475 |
* @param $section_id |
| 476 |
* @param $items array |
| 477 |
* |
| 478 |
* @return array |
| 479 |
* @since 3.0.1 |
| 480 |
*/ |
| 481 |
public function update_section_items( $section_id, $items ) { |
| 482 |
global $wpdb; |
| 483 |
|
| 484 |
foreach ( $items as $index => $item ) { |
| 485 |
$item_id = 0; |
| 486 |
$item_type = ''; |
| 487 |
if ( is_object( $item ) ) { |
| 488 |
$item_id = $item->id ?? 0; |
| 489 |
$item_type = $item->post_type ?? ''; |
| 490 |
} elseif ( is_array( $item ) ) { |
| 491 |
$item_id = $item['id'] ?? 0; |
| 492 |
$item_type = $item['type'] ?? ''; |
| 493 |
} |
| 494 |
|
| 495 |
$order = $index + 1; |
| 496 |
$exist = $this->item_section_exist( $section_id, $item_id ); |
| 497 |
|
| 498 |
if ( $exist ) { |
| 499 |
$wpdb->update( |
| 500 |
$wpdb->learnpress_section_items, |
| 501 |
array( 'item_order' => $order ), |
| 502 |
array( |
| 503 |
'section_id' => $section_id, |
| 504 |
'item_id' => $item_id, |
| 505 |
) |
| 506 |
); |
| 507 |
} else { |
| 508 |
$wpdb->insert( |
| 509 |
$wpdb->learnpress_section_items, |
| 510 |
array( |
| 511 |
'section_id' => $section_id, |
| 512 |
'item_id' => $item_id, |
| 513 |
'item_order' => $order, |
| 514 |
'item_type' => $item_type, |
| 515 |
) |
| 516 |
); |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
return $items; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Check item exist. |
| 525 |
* |
| 526 |
* @param $items array |
| 527 |
* @param $item_id string |
| 528 |
* |
| 529 |
* @return bool |
| 530 |
* @since 3.0.0 |
| 531 |
*/ |
| 532 |
private function check_item_exist( $items, $item_id ) { |
| 533 |
foreach ( $items as $item ) { |
| 534 |
if ( $item['id'] == $item_id ) { |
| 535 |
return true; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
return false; |
| 540 |
} |
| 541 |
|
| 542 |
/** |
| 543 |
* Update lesson, quiz title in admin course editor. |
| 544 |
* |
| 545 |
* @param $item |
| 546 |
* |
| 547 |
* @return array |
| 548 |
* @since 3.0.0 |
| 549 |
*/ |
| 550 |
public function update_item( $item ) { |
| 551 |
$item = wp_parse_args( |
| 552 |
$item, |
| 553 |
array( |
| 554 |
'id' => '', |
| 555 |
'title' => '', |
| 556 |
) |
| 557 |
); |
| 558 |
|
| 559 |
wp_update_post( |
| 560 |
array( |
| 561 |
'ID' => $item['id'], |
| 562 |
'post_title' => $item['title'], |
| 563 |
) |
| 564 |
); |
| 565 |
|
| 566 |
if ( isset( $item['preview'] ) && $item['preview'] ) { |
| 567 |
update_post_meta( $item['id'], '_lp_preview', 'yes' ); |
| 568 |
} else { |
| 569 |
delete_post_meta( $item['id'], '_lp_preview' ); |
| 570 |
} |
| 571 |
|
| 572 |
return $item; |
| 573 |
} |
| 574 |
} |
| 575 |
|