| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class LP_Lesson_CURD |
| 4 |
* |
| 5 |
* @author ThimPress |
| 6 |
* @package LearnPress/Classes/CURD |
| 7 |
* @since 3.0.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Prevent loading this file directly |
| 12 |
*/ |
| 13 |
defined( 'ABSPATH' ) || exit(); |
| 14 |
|
| 15 |
if ( ! class_exists( 'LP_Lesson_CURD' ) ) { |
| 16 |
|
| 17 |
/** |
| 18 |
* Class LP_Lesson_CURD |
| 19 |
*/ |
| 20 |
class LP_Lesson_CURD extends LP_Object_Data_CURD implements LP_Interface_CURD { |
| 21 |
|
| 22 |
/** |
| 23 |
* Create lesson, with default meta. |
| 24 |
* |
| 25 |
* @param $args |
| 26 |
* |
| 27 |
* @return int|WP_Error |
| 28 |
*/ |
| 29 |
public function create( &$args ) { |
| 30 |
$args = wp_parse_args( |
| 31 |
$args, |
| 32 |
array( |
| 33 |
'id' => '', |
| 34 |
'status' => 'publish', |
| 35 |
'title' => esc_html__( 'New Lesson', 'learnpress' ), |
| 36 |
'content' => '', |
| 37 |
'author' => learn_press_get_current_user_id(), |
| 38 |
) |
| 39 |
); |
| 40 |
|
| 41 |
$lesson_id = wp_insert_post( |
| 42 |
array( |
| 43 |
'ID' => $args['id'], |
| 44 |
'post_type' => LP_LESSON_CPT, |
| 45 |
'post_status' => $args['status'], |
| 46 |
'post_title' => $args['title'], |
| 47 |
'post_content' => $args['content'], |
| 48 |
'post_author' => $args['author'], |
| 49 |
) |
| 50 |
); |
| 51 |
|
| 52 |
if ( $lesson_id ) { |
| 53 |
// add default meta for new lesson |
| 54 |
$default_meta = LP_Lesson::get_default_meta(); |
| 55 |
|
| 56 |
if ( is_array( $default_meta ) ) { |
| 57 |
foreach ( $default_meta as $key => $value ) { |
| 58 |
update_post_meta( $lesson_id, '_lp_' . $key, $value ); |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return $lesson_id; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* @param object $lesson |
| 68 |
*/ |
| 69 |
public function update( &$lesson ) { |
| 70 |
// TODO: Implement update() method. |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Delete lesson. |
| 75 |
* |
| 76 |
* @since 3.0.0 |
| 77 |
* |
| 78 |
* @param object $lesson_id |
| 79 |
*/ |
| 80 |
public function delete( &$lesson_id ) { |
| 81 |
// course curd |
| 82 |
$curd = new LP_Course_CURD(); |
| 83 |
|
| 84 |
// remove lesson from course items |
| 85 |
$curd->remove_item( $lesson_id ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Duplicate lesson. |
| 90 |
* |
| 91 |
* @since 3.0.0 |
| 92 |
* |
| 93 |
* @param $lesson_id |
| 94 |
* @param array $args |
| 95 |
* |
| 96 |
* @return mixed|WP_Error |
| 97 |
*/ |
| 98 |
public function duplicate( &$lesson_id, $args = array() ) { |
| 99 |
if ( ! $lesson_id ) { |
| 100 |
return new WP_Error( '0', 'Oops! ID not found' ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( get_post_type( $lesson_id ) != LP_LESSON_CPT ) { |
| 104 |
return new WP_Error( '1', 'Op! The lesson does not exist' ); |
| 105 |
} |
| 106 |
|
| 107 |
$user_id = $args['meta_input']['_lp_user'] ?? get_current_user_id(); |
| 108 |
// ensure that user can create lesson |
| 109 |
if ( ! user_can( $user_id, 'edit_posts' ) ) { |
| 110 |
return new WP_Error( '2', 'Sorry! You don\'t have permission to duplicate this lesson' ); |
| 111 |
} |
| 112 |
|
| 113 |
// duplicate lesson |
| 114 |
$new_lesson_id = learn_press_duplicate_post( $lesson_id, $args ); |
| 115 |
|
| 116 |
if ( ! $new_lesson_id || is_wp_error( $new_lesson_id ) ) { |
| 117 |
return new WP_Error( '3', 'Sorry! Failed to duplicate lesson!' ); |
| 118 |
} |
| 119 |
|
| 120 |
do_action( 'learn-press/item/after-duplicate', $lesson_id, $new_lesson_id, $args ); |
| 121 |
return $new_lesson_id; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Load lesson data. |
| 126 |
* |
| 127 |
* @since 3.0.0 |
| 128 |
* |
| 129 |
* @param object $lesson |
| 130 |
* |
| 131 |
* @return object |
| 132 |
* @throws Exception |
| 133 |
*/ |
| 134 |
public function load( &$lesson ) { |
| 135 |
// lesson id |
| 136 |
$id = $lesson->get_id(); |
| 137 |
|
| 138 |
if ( ! $id || get_post_type( $id ) !== LP_LESSON_CPT ) { |
| 139 |
throw new Exception( sprintf( __( 'Invalid lesson with ID "%d".', 'learnpress' ), $id ) ); |
| 140 |
} |
| 141 |
|
| 142 |
$lesson->set_data( |
| 143 |
array( |
| 144 |
'preview' => get_post_meta( $id, '_lp_preview', true ), |
| 145 |
) |
| 146 |
); |
| 147 |
|
| 148 |
return $lesson; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|