| @@ -26,27 +26,32 @@ | ||
| 26 | 26 | |
| 27 | 27 | /** |
| 28 | 28 | * Create item and insert to database. |
| 29 | 29 | * |
| 30 | - * @param $args array | |
| 30 | + * @param $section_origin array | |
| 31 | 31 | * |
| 32 | - * @return mixed | |
| 32 | + * @return array | |
| 33 | 33 | * @since 3.0.0 |
| 34 | + * @version 3.0.1 | |
| 34 | 35 | */ |
| 35 | - public function create( &$args ) { | |
| 36 | + public function create( &$section_origin ) { | |
| 36 | 37 | global $wpdb; |
| 37 | 38 | $section = []; |
| 38 | 39 | |
| 39 | 40 | try { |
| 40 | - $section = $this->parse( $args ); | |
| 41 | - $section = stripslashes_deep( $section ); | |
| 41 | + $section = $this->parse( $section_origin ); | |
| 42 | + //$section = stripslashes_deep( $section ); | |
| 42 | 43 | $last_section_order_number = LP_Section_DB::getInstance()->get_last_number_order( $section['section_course_id'] ); |
| 43 | - $section['section_order'] = $last_section_order_number + 1; | |
| 44 | - $insert_data = array( | |
| 45 | - 'section_course_id' => $this->course_id, | |
| 46 | - 'section_name' => $section['section_name'], | |
| 47 | - 'section_order' => $section['section_order'], | |
| 48 | - 'section_description' => $section['section_description'], | |
| 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 | |
| 49 | 54 | ); |
| 50 | 55 | |
| 51 | 56 | $wpdb->insert( |
| 52 | 57 | $wpdb->learnpress_sections, |
| @@ -52,9 +57,12 @@ | ||
| 52 | 57 | $wpdb->learnpress_sections, |
| 53 | 58 | $insert_data, |
| 54 | 59 | array( '%d', '%s', '%d', '%s' ) |
| 55 | 60 | ); |
| 56 | - $section['section_id'] = $wpdb->insert_id; | |
| 61 | + $section['section_id'] = $wpdb->insert_id; | |
| 62 | + $section['section_order'] = $section_order_new; | |
| 63 | + | |
| 64 | + do_action( 'lp/section/created', $section ); | |
| 57 | 65 | } catch ( Throwable $e ) { |
| 58 | 66 | error_log( $e->getMessage() ); |
| 59 | 67 | } |
| 60 | 68 | |
| @@ -127,45 +135,8 @@ | ||
| 127 | 135 | // TODO: Implement duplicate() method. |
| 128 | 136 | } |
| 129 | 137 | |
| 130 | 138 | /** |
| 131 | - * Remove all items from each section and delete course's sections. | |
| 132 | - * | |
| 133 | - * @return bool | |
| 134 | - * @since 3.0.0 | |
| 135 | - * @editor tungnx | |
| 136 | - * @modify 4.1.4.1 - comment - not use | |
| 137 | - */ | |
| 138 | - /*public function clear() { | |
| 139 | - | |
| 140 | - $sections_ids = LP_Object_Cache::get( 'course-' . $this->course_id, 'learn-press/course-sections-ids' ); | |
| 141 | - | |
| 142 | - if ( ! $sections_ids ) { | |
| 143 | - return false; | |
| 144 | - } | |
| 145 | - | |
| 146 | - global $wpdb; | |
| 147 | - | |
| 148 | - do_action( 'learn-press/before-clear-section', $this->course_id ); | |
| 149 | - | |
| 150 | - // Remove all items in course's sections | |
| 151 | - $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}learnpress_section_items WHERE %d AND section_id IN(" . join( ',', $sections_ids ) . ')', 1 ) ); | |
| 152 | - learn_press_reset_auto_increment( 'learnpress_section_items' ); | |
| 153 | - // delete sections ids cache | |
| 154 | - // wp_cache_delete( 'course-' . $this->course_id, 'learn-press/course-sections-ids' ); | |
| 155 | - | |
| 156 | - // delete sections in course | |
| 157 | - $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}learnpress_sections WHERE section_course_id = %d", $this->course_id ) ); | |
| 158 | - learn_press_reset_auto_increment( 'learnpress_sections' ); | |
| 159 | - // delete sections cache | |
| 160 | - // wp_cache_delete( 'course-' . $this->course_id, 'learn-press/course-sections' ); | |
| 161 | - | |
| 162 | - LP_Course_Utils::set_cache_db_sections( $this->course_id, false ); | |
| 163 | - | |
| 164 | - return true; | |
| 165 | - }*/ | |
| 166 | - | |
| 167 | - /** | |
| 168 | 139 | * Load data from database. |
| 169 | 140 | * |
| 170 | 141 | * @param object $object |
| 171 | 142 | * |
| @@ -199,60 +170,8 @@ | ||
| 199 | 170 | return $data; |
| 200 | 171 | } |
| 201 | 172 | |
| 202 | 173 | /** |
| 203 | - * Get course sections ids and set data to cache. | |
| 204 | - * | |
| 205 | - * @return array | |
| 206 | - * @since 3.0.0 | |
| 207 | - * @deprecated 4.1.6.3 | |
| 208 | - */ | |
| 209 | - /*public function read_sections_ids() { | |
| 210 | - | |
| 211 | - // Get course's sections id data from cache | |
| 212 | - $ids = LP_Object_Cache::get( 'course-' . $this->course_id, 'learn-press/course-sections-ids' ); | |
| 213 | - | |
| 214 | - if ( ! $ids ) { | |
| 215 | - global $wpdb; | |
| 216 | - // get sections id | |
| 217 | - $ids = $wpdb->get_col( $wpdb->prepare( "SELECT section_id FROM {$wpdb->prefix}learnpress_sections WHERE section_course_id = %d", $this->course_id ) ); | |
| 218 | - // Set cache | |
| 219 | - LP_Object_Cache::set( 'course-' . $this->course_id, $ids, 'learn-press/course-sections-ids' ); | |
| 220 | - } | |
| 221 | - | |
| 222 | - return $ids; | |
| 223 | - }*/ | |
| 224 | - | |
| 225 | - /** | |
| 226 | - * Read all items from DB | |
| 227 | - * | |
| 228 | - * @param int $section_id | |
| 229 | - * | |
| 230 | - * @return array | |
| 231 | - * @depecated 4.1.6.9 Remove when release Addon Frontend Editor 4.0.1 | |
| 232 | - */ | |
| 233 | - public function read_items( $section_id ) { | |
| 234 | - global $wpdb; | |
| 235 | - | |
| 236 | - $query = $wpdb->prepare( | |
| 237 | - " | |
| 238 | - SELECT item_id id | |
| 239 | - FROM {$wpdb->learnpress_section_items} si | |
| 240 | - INNER JOIN {$wpdb->learnpress_sections} s ON si.section_id = s.section_id | |
| 241 | - INNER JOIN {$wpdb->posts} c ON c.ID = s.section_course_id | |
| 242 | - INNER JOIN {$wpdb->posts} it ON it.ID = si.item_id | |
| 243 | - WHERE s.section_id = %d | |
| 244 | - AND it.post_status = %s | |
| 245 | - ORDER BY si.item_order, si.section_item_id ASC | |
| 246 | - ", | |
| 247 | - $section_id, | |
| 248 | - 'publish' | |
| 249 | - ); | |
| 250 | - | |
| 251 | - return $wpdb->get_col( $query ); | |
| 252 | - } | |
| 253 | - | |
| 254 | - /** | |
| 255 | 174 | * Update sort sections. |
| 256 | 175 | * |
| 257 | 176 | * @param string[] $sections_new_order |
| 258 | 177 | * |
| @@ -261,13 +180,8 @@ | ||
| 261 | 180 | */ |
| 262 | 181 | public function update_sections_order( $sections_new_order ) { |
| 263 | 182 | global $wpdb; |
| 264 | 183 | |
| 265 | - //$current_sections = LP_Course_Utils::get_cached_db_sections( $this->course_id ); | |
| 266 | - //$new_sections = array(); | |
| 267 | - //$course = learn_press_get_course( $this->course_id ); | |
| 268 | - //$sections_items = $course->get_full_sections_and_items_course(); | |
| 269 | - | |
| 270 | 184 | $orders = array(); |
| 271 | 185 | |
| 272 | 186 | foreach ( $sections_new_order as $order_new => $section_id ) { |
| 273 | 187 | $order_new = $order_new + 1; |
| @@ -276,34 +190,10 @@ | ||
| 276 | 190 | $wpdb->learnpress_sections, |
| 277 | 191 | array( 'section_order' => $order_new ), |
| 278 | 192 | array( 'section_id' => $section_id ) |
| 279 | 193 | ); |
| 280 | - | |
| 281 | - // $this->get_section_items( $section_id ); | |
| 282 | 194 | } |
| 283 | 195 | |
| 284 | - /*foreach ( $sections as $index => $section_id ) { | |
| 285 | - $order = $index + 1; | |
| 286 | - | |
| 287 | - $orders[ $section_id ] = $order; | |
| 288 | - | |
| 289 | - $wpdb->update( | |
| 290 | - $wpdb->learnpress_sections, | |
| 291 | - array( 'section_order' => $order ), | |
| 292 | - array( 'section_id' => $section_id ) | |
| 293 | - ); | |
| 294 | - | |
| 295 | - foreach ( $current_sections as $current_section ) { | |
| 296 | - if ( $current_section->section_id == $section_id ) { | |
| 297 | - $new_sections[ $index ] = $current_section; | |
| 298 | - } | |
| 299 | - } | |
| 300 | - | |
| 301 | - $this->get_section_items( $section_id ); | |
| 302 | - }*/ | |
| 303 | - | |
| 304 | - //LP_Course_Utils::set_cache_db_sections( $this->course_id, $new_sections ); | |
| 305 | - | |
| 306 | 196 | return $orders; |
| 307 | 197 | } |
| 308 | 198 | |
| 309 | 199 | /** |
| @@ -330,10 +220,8 @@ | ||
| 330 | 220 | } |
| 331 | 221 | } |
| 332 | 222 | } |
| 333 | 223 | |
| 334 | - // LP_Object_Cache::set( 'course-' . $this->course_id . '-' . $section_id, $return, 'learn-press/course-section-items' ); | |
| 335 | - | |
| 336 | 224 | return $return; |
| 337 | 225 | } |
| 338 | 226 | |
| 339 | 227 | /** |
| @@ -338,9 +226,9 @@ | ||
| 338 | 226 | |
| 339 | 227 | /** |
| 340 | 228 | * Create new section item and add to course. |
| 341 | 229 | * |
| 342 | - * @param int $section_id | |
| 230 | + * @param int $section_id | |
| 343 | 231 | * @param array $item |
| 344 | 232 | * |
| 345 | 233 | * @return array | bool |
| 346 | 234 | * @since 3.0.0 |
| @@ -355,17 +243,21 @@ | ||
| 355 | 243 | |
| 356 | 244 | // $item = wp_parse_args( $item, array( 'title' => '', 'type' => '' ) ); |
| 357 | 245 | $item = array_merge( |
| 358 | 246 | array( |
| 359 | - 'title' => '', | |
| 360 | - 'type' => '', | |
| 247 | + 'title' => '', | |
| 248 | + 'type' => '', | |
| 249 | + 'content' => '', | |
| 250 | + 'status' => 'publish', | |
| 361 | 251 | ), |
| 362 | 252 | $item |
| 363 | 253 | ); |
| 364 | 254 | |
| 365 | 255 | $args = array( |
| 366 | - 'title' => $item['title'], | |
| 367 | - 'author' => $author_id, | |
| 256 | + 'title' => $item['title'], | |
| 257 | + 'author' => $author_id, | |
| 258 | + 'content' => $item['content'], | |
| 259 | + 'status' => $item['status'], | |
| 368 | 260 | ); |
| 369 | 261 | |
| 370 | 262 | if ( ! empty( $item['id'] ) ) { |
| 371 | 263 | $item['old_id'] = $item ['id']; |
| @@ -396,9 +288,9 @@ | ||
| 396 | 288 | /** |
| 397 | 289 | * Add list new items to section. |
| 398 | 290 | * |
| 399 | 291 | * @param $section_id |
| 400 | - * @param array $items | |
| 292 | + * @param array $items | |
| 401 | 293 | * |
| 402 | 294 | * @return array |
| 403 | 295 | */ |
| 404 | 296 | public function add_items_section( $section_id, $items = array() ) { |
| @@ -411,14 +303,18 @@ | ||
| 411 | 303 | |
| 412 | 304 | $all_items = array_merge( $current_items, $items ); |
| 413 | 305 | $result = array(); |
| 414 | 306 | foreach ( $all_items as $key => $item ) { |
| 307 | + $key = $key + 1; | |
| 308 | + if ( ! isset( $item['id'] ) || ! isset( $item['type'] ) ) { | |
| 309 | + continue; | |
| 310 | + } | |
| 415 | 311 | |
| 416 | 312 | $item = (array) $item; |
| 417 | 313 | $exist = $this->item_section_exist( $section_id, $item['id'] ); |
| 418 | 314 | |
| 419 | 315 | if ( $exist ) { |
| 420 | - $a = $wpdb->update( | |
| 316 | + $wpdb->update( | |
| 421 | 317 | $wpdb->learnpress_section_items, |
| 422 | 318 | array( 'item_order' => $key ), |
| 423 | 319 | array( |
| 424 | 320 | 'section_id' => $section_id, |
| @@ -425,9 +321,9 @@ | ||
| 425 | 321 | 'item_id' => $item['id'], |
| 426 | 322 | ) |
| 427 | 323 | ); |
| 428 | 324 | } else { |
| 429 | - $a = $wpdb->insert( | |
| 325 | + $wpdb->insert( | |
| 430 | 326 | $wpdb->learnpress_section_items, |
| 431 | 327 | array( |
| 432 | 328 | 'section_id' => $section_id, |
| 433 | 329 | 'item_id' => $item['id'], |
| @@ -437,8 +333,12 @@ | ||
| 437 | 333 | ); |
| 438 | 334 | } |
| 439 | 335 | // get WP Post |
| 440 | 336 | $post = get_post( $item['id'] ); |
| 337 | + if ( ! $post ) { | |
| 338 | + continue; | |
| 339 | + } | |
| 340 | + | |
| 441 | 341 | $item = array_merge( |
| 442 | 342 | $item, |
| 443 | 343 | array( |
| 444 | 344 | 'id' => $post->ID, |
| @@ -454,14 +354,56 @@ | ||
| 454 | 354 | do_action( 'learn-press/updated-item-to-section', $item, $section_id, $this->course_id ); |
| 455 | 355 | } |
| 456 | 356 | |
| 457 | 357 | $result[] = $item; |
| 458 | - // $order ++; | |
| 459 | 358 | } |
| 460 | 359 | |
| 461 | - // LP_Object_Cache::set( 'course-' . $this->course_id . '-' . $section_id, $all_items, 'learn-press/course-section-items' ); | |
| 360 | + return $result; | |
| 361 | + } | |
| 462 | 362 | |
| 463 | - return $result; | |
| 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 | + } | |
| 464 | 406 | } |
| 465 | 407 | |
| 466 | 408 | /** |
| 467 | 409 | * Check item was been added to any section. |
| @@ -500,59 +442,8 @@ | ||
| 500 | 442 | return $section; |
| 501 | 443 | } |
| 502 | 444 | |
| 503 | 445 | /** |
| 504 | - * Update course final item. | |
| 505 | - * | |
| 506 | - * @return bool | |
| 507 | - * @version 4.0.0 | |
| 508 | - * @depecated 4.1.6.9 | |
| 509 | - */ | |
| 510 | - public function update_final_item() { | |
| 511 | - _deprecated_function( __FUNCTION__, '4.1.6.9' ); | |
| 512 | - /*$sections = LP_Course_Utils::get_cached_db_sections( $this->course_id ); | |
| 513 | - | |
| 514 | - if ( ! $sections ) { | |
| 515 | - return false; | |
| 516 | - } | |
| 517 | - | |
| 518 | - $last_section = end( $sections ); | |
| 519 | - $section_id = $last_section->section_id; | |
| 520 | - | |
| 521 | - // get last section items | |
| 522 | - $section_items = LP_Object_Cache::get( 'course-' . $this->course_id . '-' . $section_id, 'learn-press/course-section-items' ); | |
| 523 | - | |
| 524 | - $types = apply_filters( 'learn-press/post-types-support-assessment-by-final-item', array( LP_QUIZ_CPT ) ); | |
| 525 | - | |
| 526 | - if ( $section_items ) { | |
| 527 | - // last item in last section | |
| 528 | - $final = end( $section_items ); | |
| 529 | - | |
| 530 | - if ( is_array( $types ) && in_array( $final['type'], $types ) ) { | |
| 531 | - update_post_meta( $this->course_id, '_' . substr_replace( $final['type'], '_final_', 2, 1 ), $final['id'] ); | |
| 532 | - $diff = array_diff( $types, array( $final['type'] ) ); | |
| 533 | - foreach ( $diff as $type ) { | |
| 534 | - // delete all other final meta | |
| 535 | - delete_post_meta( $this->course_id, '_' . substr_replace( $type, '_final_', 2, 1 ) ); | |
| 536 | - } | |
| 537 | - } else { | |
| 538 | - // for last item is not post type need check final item | |
| 539 | - foreach ( $types as $type ) { | |
| 540 | - delete_post_meta( $this->course_id, '_' . substr_replace( $type, '_final_', 2, 1 ) ); | |
| 541 | - } | |
| 542 | - } | |
| 543 | - } else { | |
| 544 | - // for last section does not has any item | |
| 545 | - foreach ( $types as $type ) { | |
| 546 | - delete_post_meta( $this->course_id, '_' . substr_replace( $type, '_final_', 2, 1 ) ); | |
| 547 | - } | |
| 548 | - } | |
| 549 | - | |
| 550 | - return true;*/ | |
| 551 | - | |
| 552 | - } | |
| 553 | - | |
| 554 | - /** | |
| 555 | 446 | * Remove section item. |
| 556 | 447 | * |
| 557 | 448 | * @param $section_id |
| 558 | 449 | * @param $item_id |
| @@ -584,18 +475,26 @@ | ||
| 584 | 475 | * @param $section_id |
| 585 | 476 | * @param $items array |
| 586 | 477 | * |
| 587 | 478 | * @return array |
| 588 | - * @since 3.0.0 | |
| 479 | + * @since 3.0.1 | |
| 589 | 480 | */ |
| 590 | 481 | public function update_section_items( $section_id, $items ) { |
| 591 | - $current_items = $this->get_section_items( $section_id ); | |
| 592 | - | |
| 593 | 482 | global $wpdb; |
| 594 | 483 | |
| 595 | 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 | + | |
| 596 | 495 | $order = $index + 1; |
| 597 | - $exist = $this->item_section_exist( $section_id, $item['id'] ); | |
| 496 | + $exist = $this->item_section_exist( $section_id, $item_id ); | |
| 598 | 497 | |
| 599 | 498 | if ( $exist ) { |
| 600 | 499 | $wpdb->update( |
| 601 | 500 | $wpdb->learnpress_section_items, |
| @@ -601,9 +500,9 @@ | ||
| 601 | 500 | $wpdb->learnpress_section_items, |
| 602 | 501 | array( 'item_order' => $order ), |
| 603 | 502 | array( |
| 604 | 503 | 'section_id' => $section_id, |
| 605 | - 'item_id' => $item['id'], | |
| 504 | + 'item_id' => $item_id, | |
| 606 | 505 | ) |
| 607 | 506 | ); |
| 608 | 507 | } else { |
| 609 | 508 | $wpdb->insert( |
| @@ -609,28 +508,11 @@ | ||
| 609 | 508 | $wpdb->insert( |
| 610 | 509 | $wpdb->learnpress_section_items, |
| 611 | 510 | array( |
| 612 | 511 | 'section_id' => $section_id, |
| 613 | - 'item_id' => $item['id'], | |
| 512 | + 'item_id' => $item_id, | |
| 614 | 513 | 'item_order' => $order, |
| 615 | - 'item_type' => $item['type'], | |
| 616 | - ) | |
| 617 | - ); | |
| 618 | - } | |
| 619 | - } | |
| 620 | - | |
| 621 | - /** | |
| 622 | - * Remove non-existent items. | |
| 623 | - */ | |
| 624 | - foreach ( $current_items as $item ) { | |
| 625 | - $find = $this->check_item_exist( $items, $item['id'] ); | |
| 626 | - | |
| 627 | - if ( ! $find ) { | |
| 628 | - $wpdb->delete( | |
| 629 | - $wpdb->learnpress_section_items, | |
| 630 | - array( | |
| 631 | - 'section_id' => $section_id, | |
| 632 | - 'item_id' => $item['id'], | |
| 514 | + 'item_type' => $item_type, | |
| 633 | 515 | ) |
| 634 | 516 | ); |
| 635 | 517 | } |
| 636 | 518 | } |