| @@ -15,8 +15,9 @@ | ||
| 15 | 15 | use LearnPress; |
| 16 | 16 | use LearnPress\Databases\PostDB; |
| 17 | 17 | use LearnPress\Filters\FilterBase; |
| 18 | 18 | use LearnPress\Filters\PostFilter; |
| 19 | +use LearnPress\Services\CourseService; | |
| 19 | 20 | use LP_Cache; |
| 20 | 21 | use LP_Post_Meta_DB; |
| 21 | 22 | use LP_Post_Meta_Filter; |
| 22 | 23 | use LP_Post_Type_Filter; |
| @@ -232,11 +233,12 @@ | ||
| 232 | 233 | * Check capabilities to create new post. |
| 233 | 234 | * |
| 234 | 235 | * @return bool |
| 235 | 236 | * @since 4.2.9.4 |
| 236 | - * @version 1.0.0 | |
| 237 | + * @version 1.0.1 | |
| 237 | 238 | */ |
| 238 | 239 | public function check_capabilities_create(): bool { |
| 240 | + //return current_user_can( 'edit_' . $this->post_type . 's' ); | |
| 239 | 241 | return true; |
| 240 | 242 | } |
| 241 | 243 | |
| 242 | 244 | /** |
| @@ -243,11 +245,12 @@ | ||
| 243 | 245 | * Check capabilities to update post. |
| 244 | 246 | * |
| 245 | 247 | * @return bool |
| 246 | 248 | * @since 4.2.9.4 |
| 247 | - * @version 1.0.0 | |
| 249 | + * @version 1.0.1 | |
| 248 | 250 | */ |
| 249 | 251 | public function check_capabilities_update(): bool { |
| 252 | + //return current_user_can( 'edit_' . $this->post_type, $this->ID ); | |
| 250 | 253 | return true; |
| 251 | 254 | } |
| 252 | 255 | |
| 253 | 256 | /** |
| @@ -255,19 +258,12 @@ | ||
| 255 | 258 | * Check user current can edit it. |
| 256 | 259 | * |
| 257 | 260 | * @return void |
| 258 | 261 | * @throws Exception |
| 259 | - * @version 1.0.0 | |
| 262 | + * @version 1.0.1 | |
| 260 | 263 | * @since 4.2.9 |
| 261 | 264 | */ |
| 262 | 265 | public function check_capabilities_create_item_course() { |
| 263 | - $course_item_types = CourseModel::item_types_support(); | |
| 264 | - // Questions not type item of course, it is type item of quiz, but need check here. | |
| 265 | - $course_item_types[] = LP_QUESTION_CPT; | |
| 266 | - if ( ! in_array( $this->post_type, $course_item_types, true ) ) { | |
| 267 | - return; | |
| 268 | - } | |
| 269 | - | |
| 270 | 266 | $user = wp_get_current_user(); |
| 271 | 267 | if ( ! user_can( $user, 'edit_' . LP_LESSON_CPT . 's' ) ) { |
| 272 | 268 | throw new Exception( __( 'You do not have permission to create item.', 'learnpress' ) ); |
| 273 | 269 | } |
| @@ -278,19 +274,12 @@ | ||
| 278 | 274 | * Check user current can edit it. |
| 279 | 275 | * |
| 280 | 276 | * @return void |
| 281 | 277 | * @throws Exception |
| 282 | - * @version 1.0.0 | |
| 278 | + * @version 1.0.1 | |
| 283 | 279 | * @since 4.2.9 |
| 284 | 280 | */ |
| 285 | 281 | public function check_capabilities_update_item_course() { |
| 286 | - $course_item_types = CourseModel::item_types_support(); | |
| 287 | - // Questions not type item of course, it is type item of quiz, but need check here. | |
| 288 | - $course_item_types[] = LP_QUESTION_CPT; | |
| 289 | - if ( ! in_array( $this->post_type, $course_item_types, true ) ) { | |
| 290 | - return; | |
| 291 | - } | |
| 292 | - | |
| 293 | 282 | $user = wp_get_current_user(); |
| 294 | 283 | if ( ! user_can( $user, 'edit_' . LP_LESSON_CPT, $this->ID ) ) { |
| 295 | 284 | throw new Exception( __( 'You do not have permission to edit this item.', 'learnpress' ) ); |
| 296 | 285 | } |
| @@ -302,9 +291,9 @@ | ||
| 302 | 291 | * If user_item_id is empty, insert new data, else update data. |
| 303 | 292 | * |
| 304 | 293 | * @throws Exception |
| 305 | 294 | * @since 4.2.5 |
| 306 | - * @version 1.0.4 | |
| 295 | + * @version 1.0.5 | |
| 307 | 296 | */ |
| 308 | 297 | public function save( bool $force_save = false ) { |
| 309 | 298 | $data = get_object_vars( $this ); |
| 310 | 299 | |
| @@ -318,13 +307,14 @@ | ||
| 318 | 307 | // Check if exists course id. |
| 319 | 308 | if ( empty( $this->ID ) ) { // Insert data. |
| 320 | 309 | // Check permission |
| 321 | 310 | if ( ! $force_save ) { |
| 322 | - if ( ! $this->check_capabilities_create() ) { | |
| 311 | + if ( CourseService::check_is_item_type_of_course( $this->post_type ) | |
| 312 | + || LP_QUESTION_CPT === $this->post_type ) { | |
| 313 | + $this->check_capabilities_create_item_course(); | |
| 314 | + } elseif ( ! $this->check_capabilities_create() ) { | |
| 323 | 315 | throw new Exception( __( 'You do not have permission to create item.', 'learnpress' ) ); |
| 324 | 316 | } |
| 325 | - | |
| 326 | - $this->check_capabilities_create_item_course(); | |
| 327 | 317 | } |
| 328 | 318 | |
| 329 | 319 | unset( $data['ID'] ); |
| 330 | 320 | $post_id = wp_insert_post( $data, true ); |
| @@ -330,13 +320,14 @@ | ||
| 330 | 320 | $post_id = wp_insert_post( $data, true ); |
| 331 | 321 | } else { // Update data. |
| 332 | 322 | // Check permission |
| 333 | 323 | if ( ! $force_save ) { |
| 334 | - if ( ! $this->check_capabilities_update() ) { | |
| 335 | - throw new Exception( __( 'You do not have permission to edit this item.', 'learnpress' ) ); | |
| 324 | + if ( courseservice::check_is_item_type_of_course( $this->post_type ) | |
| 325 | + || LP_QUESTION_CPT === $this->post_type ) { | |
| 326 | + $this->check_capabilities_update_item_course(); | |
| 327 | + } elseif ( ! $this->check_capabilities_update() ) { | |
| 328 | + throw new exception( __( 'you do not have permission to edit this item.', 'learnpress' ) ); | |
| 336 | 329 | } |
| 337 | - | |
| 338 | - $this->check_capabilities_update_item_course(); | |
| 339 | 330 | } |
| 340 | 331 | |
| 341 | 332 | $post_id = wp_update_post( $data, true ); |
| 342 | 333 | } |