| @@ -7,8 +7,13 @@ | ||
| 7 | 7 | * @package LearnPress/Classes |
| 8 | 8 | * @version 1.0 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | +use LearnPress\Models\CourseModel; | |
| 12 | +use LearnPress\Models\CourseSectionItemModel; | |
| 13 | +use LearnPress\Models\UserModel; | |
| 14 | +use LearnPress\TemplateHooks\UserTemplate; | |
| 15 | + | |
| 11 | 16 | defined( 'ABSPATH' ) || exit(); |
| 12 | 17 | |
| 13 | 18 | abstract class LP_Abstract_Post_Type { |
| 14 | 19 | /** |
| @@ -18,8 +23,16 @@ | ||
| 18 | 23 | */ |
| 19 | 24 | protected $_post_type = ''; |
| 20 | 25 | |
| 21 | 26 | /** |
| 27 | + * Screen list post type | |
| 28 | + * Ex: edit-{post_type} | |
| 29 | + * | |
| 30 | + * @var string | |
| 31 | + */ | |
| 32 | + protected $_screen_list = ''; | |
| 33 | + | |
| 34 | + /** | |
| 22 | 35 | * Metaboxes registered |
| 23 | 36 | * |
| 24 | 37 | * @var array |
| 25 | 38 | */ |
| @@ -71,15 +84,21 @@ | ||
| 71 | 84 | |
| 72 | 85 | if ( ! empty( $post_type ) ) { |
| 73 | 86 | $this->_post_type = $post_type; |
| 74 | 87 | } |
| 75 | - add_action( 'init', array( $this, '_do_register' ) ); | |
| 76 | - add_action( 'save_post', array( $this, '_do_save_post' ), 10, 2 ); | |
| 88 | + | |
| 89 | + $this->_do_register(); | |
| 90 | + | |
| 91 | + add_filter( 'wp_list_table_class_name', [ $this, 'wp_list_table_class_name' ], 10, 2 ); | |
| 92 | + add_action( 'save_post', array( $this, '_do_save_post' ), - 1, 3 ); | |
| 93 | + add_action( 'wp_after_insert_post', [ $this, 'wp_after_insert_post' ], - 1, 3 ); | |
| 77 | 94 | add_action( 'before_delete_post', array( $this, '_before_delete_post' ) ); |
| 78 | 95 | add_action( 'deleted_post', array( $this, '_deleted_post' ) ); |
| 79 | 96 | add_action( 'wp_trash_post', array( $this, '_before_trash_post' ) ); |
| 80 | 97 | add_action( 'trashed_post', array( $this, '_trashed_post' ) ); |
| 81 | 98 | |
| 99 | + //add_filter( 'manage_posts_columns', array( $this, '_manage_columns_head_title' ), 11, 2 ); | |
| 100 | + //add_action( 'manage_posts_custom_column', array( $this, '_manage_column_value' ), 11, 2 ); | |
| 82 | 101 | add_filter( 'manage_edit-' . $this->_post_type . '_sortable_columns', array( $this, 'sortable_columns' ) ); |
| 83 | 102 | add_filter( 'manage_' . $this->_post_type . '_posts_columns', array( $this, 'columns_head' ) ); |
| 84 | 103 | add_filter( 'manage_' . $this->_post_type . '_posts_custom_column', array( $this, 'columns_content' ), 10, 2 ); |
| 85 | 104 | |
| @@ -111,28 +130,27 @@ | ||
| 111 | 130 | // add_action( 'pre_get_posts', array( $this, 'update_default_meta' ) ); |
| 112 | 131 | add_action( 'admin_footer', array( $this, 'admin_footer_scripts' ) ); |
| 113 | 132 | |
| 114 | 133 | //add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) ); |
| 134 | + add_action( 'admin_print_scripts', array( $this, 'remove_auto_save_script' ) ); | |
| 115 | 135 | |
| 116 | - $args = wp_parse_args( | |
| 117 | - $args, | |
| 118 | - array( | |
| 119 | - 'auto_save' => 'no', | |
| 120 | - 'default_meta' => false, | |
| 121 | - ) | |
| 122 | - ); | |
| 136 | + // Remove wp-auth-check and heartbeat script on post type of LearnPress | |
| 137 | + add_action( | |
| 138 | + 'admin_enqueue_scripts', | |
| 139 | + function ( $page ) { | |
| 140 | + if ( $page !== 'post.php' && $page !== 'post-new.php' ) { | |
| 141 | + return; | |
| 142 | + } | |
| 123 | 143 | |
| 124 | - if ( $args['auto_save'] == 'no' ) { | |
| 125 | - add_action( 'admin_print_scripts', array( $this, 'remove_auto_save_script' ) ); | |
| 126 | - } | |
| 144 | + if ( get_post_type() !== $this->_post_type ) { | |
| 145 | + return; | |
| 146 | + } | |
| 127 | 147 | |
| 128 | - /* | |
| 129 | - if ( $args['default_meta'] ) { | |
| 130 | - $this->_default_metas = $args['default_meta']; | |
| 131 | - }*/ | |
| 132 | - | |
| 133 | - // Comment by tungnx | |
| 134 | - // add_action( 'init', array( $this, 'maybe_remove_features' ), 1000 ); | |
| 148 | + wp_deregister_script( 'wp-auth-check' ); | |
| 149 | + wp_deregister_script( 'heartbeat' ); | |
| 150 | + }, | |
| 151 | + 1 | |
| 152 | + ); | |
| 135 | 153 | } |
| 136 | 154 | |
| 137 | 155 | /** |
| 138 | 156 | * This function is invoked along with 'init' action to register |
| @@ -154,9 +172,10 @@ | ||
| 154 | 172 | |
| 155 | 173 | if ( $args ) { |
| 156 | 174 | register_post_type( $this->_post_type, $args ); |
| 157 | 175 | |
| 158 | - flush_rewrite_rules(); | |
| 176 | + // Todo: tungnx review this code. | |
| 177 | + //flush_rewrite_rules(); | |
| 159 | 178 | } |
| 160 | 179 | } |
| 161 | 180 | |
| 162 | 181 | /** |
| @@ -163,24 +182,54 @@ | ||
| 163 | 182 | * Args to register custom post type. |
| 164 | 183 | * |
| 165 | 184 | * @return array |
| 166 | 185 | */ |
| 167 | - public function args_register_post_type() : array { | |
| 186 | + public function args_register_post_type(): array { | |
| 168 | 187 | return array(); |
| 169 | 188 | } |
| 170 | 189 | |
| 171 | 190 | /** |
| 191 | + * Check screen list post type | |
| 192 | + * | |
| 193 | + * @param WP_Screen|null $screen | |
| 194 | + * | |
| 195 | + * @return bool | |
| 196 | + */ | |
| 197 | + public function check_class_name_handle_table( $screen ): bool { | |
| 198 | + if ( $screen instanceof WP_Screen | |
| 199 | + && $screen->id === $this->_screen_list ) { | |
| 200 | + return true; | |
| 201 | + } | |
| 202 | + | |
| 203 | + return false; | |
| 204 | + } | |
| 205 | + | |
| 206 | + /** | |
| 207 | + * Declare class name of table | |
| 208 | + * | |
| 209 | + * @param $class_name | |
| 210 | + * @param $args | |
| 211 | + * | |
| 212 | + * @return mixed | |
| 213 | + */ | |
| 214 | + public function wp_list_table_class_name( $class_name, $args ) { | |
| 215 | + return $class_name; | |
| 216 | + } | |
| 217 | + | |
| 218 | + /** | |
| 172 | 219 | * Hook save post of WP |
| 173 | 220 | * |
| 174 | 221 | * In child-class use function save() |
| 175 | 222 | * |
| 176 | - * @param int $post_id | |
| 177 | - * @param WP_Post $post | |
| 223 | + * @param int $post_id | |
| 224 | + * @param WP_Post|null|mixed $post | |
| 225 | + * @param bool $is_update | |
| 226 | + * | |
| 178 | 227 | * @editor tungnx |
| 179 | 228 | * @since modify 4.0.9 |
| 180 | - * @version 4.0.1 | |
| 229 | + * @version 4.0.3 | |
| 181 | 230 | */ |
| 182 | - final function _do_save_post( int $post_id = 0, WP_Post $post = null ) { | |
| 231 | + final public function _do_save_post( int $post_id = 0, ?WP_Post $post = null, bool $is_update = false ) { | |
| 183 | 232 | // Maybe remove |
| 184 | 233 | $this->maybe_remove_assigned( $post ); |
| 185 | 234 | |
| 186 | 235 | if ( ! $this->check_post( $post_id ) ) { |
| @@ -187,8 +236,9 @@ | ||
| 187 | 236 | return; |
| 188 | 237 | } |
| 189 | 238 | |
| 190 | 239 | $this->save( $post_id, $post ); |
| 240 | + $this->save_post( $post_id, $post, $is_update ); | |
| 191 | 241 | } |
| 192 | 242 | |
| 193 | 243 | /** |
| 194 | 244 | * Function for child class handle when post has just saved |
| @@ -200,22 +250,80 @@ | ||
| 200 | 250 | // Implement from child |
| 201 | 251 | } |
| 202 | 252 | |
| 203 | 253 | /** |
| 254 | + * Function for child class handle when post has just saved | |
| 255 | + * This function provides the argument `$update` to determine whether a post is updated or new. | |
| 256 | + * Replace for function save only has two args | |
| 257 | + * | |
| 258 | + * @param int $post_id | |
| 259 | + * @param WP_Post|null $post | |
| 260 | + * @param bool $is_update | |
| 261 | + * | |
| 262 | + * @since 4.2.6.9 | |
| 263 | + * @version 1.0.1 | |
| 264 | + */ | |
| 265 | + public function save_post( int $post_id, ?WP_Post $post = null, bool $is_update = false ) { | |
| 266 | + // Implement from child | |
| 267 | + } | |
| 268 | + | |
| 269 | + /** | |
| 270 | + * Callback hook 'wp_after_insert_post' | |
| 271 | + * | |
| 272 | + * @param $post_id | |
| 273 | + * @param $post | |
| 274 | + * @param $update | |
| 275 | + * | |
| 276 | + * @return void | |
| 277 | + * @since 4.2.6.9 | |
| 278 | + * @version 1.0.1 | |
| 279 | + */ | |
| 280 | + final public function wp_after_insert_post( $post_id, $post, $update ) { | |
| 281 | + if ( ! $this->check_post( $post_id ) ) { | |
| 282 | + return; | |
| 283 | + } | |
| 284 | + | |
| 285 | + $this->after_insert_post( $post_id, $post, $update ); | |
| 286 | + } | |
| 287 | + | |
| 288 | + /** | |
| 289 | + * Function for child class handle when post has just saved | |
| 290 | + * | |
| 291 | + * @param int $post_id | |
| 292 | + * @param WP_Post|null $post | |
| 293 | + * @param bool $update | |
| 294 | + */ | |
| 295 | + public function after_insert_post( int $post_id, ?WP_Post $post = null, bool $update = false ) { | |
| 296 | + // Implement from child | |
| 297 | + } | |
| 298 | + | |
| 299 | + /** | |
| 204 | 300 | * Hook before delete post |
| 205 | 301 | * Only on receiver 1 param $post_id, can't get param $post - don't know why |
| 206 | 302 | * |
| 207 | 303 | * @param int $post_id |
| 304 | + * @param WP_Post|null $post | |
| 208 | 305 | * |
| 209 | 306 | * @editor tungnx |
| 210 | 307 | * @since modify 4.0.9 |
| 211 | 308 | */ |
| 212 | - final function _before_delete_post( int $post_id ) { | |
| 213 | - if ( ! $this->check_post( $post_id ) ) { | |
| 214 | - return; | |
| 309 | + final public function _before_delete_post( int $post_id, ?WP_Post $post = null ) { | |
| 310 | + try { | |
| 311 | + // Todo: check is pages of LP | |
| 312 | + if ( 'page' === get_post_type( $post_id ) ) { | |
| 313 | + // Clear cache LP settings | |
| 314 | + $lp_settings_cache = new LP_Settings_Cache( true ); | |
| 315 | + $lp_settings_cache->clean_lp_settings(); | |
| 316 | + } | |
| 317 | + | |
| 318 | + if ( ! $this->check_post( $post_id ) ) { | |
| 319 | + return; | |
| 320 | + } | |
| 321 | + | |
| 322 | + $this->before_delete( $post_id ); | |
| 323 | + } catch ( Throwable $e ) { | |
| 324 | + error_log( __METHOD__ . ': ' . $e->getMessage() ); | |
| 215 | 325 | } |
| 216 | - | |
| 217 | - $this->before_delete( $post_id ); | |
| 218 | 326 | } |
| 219 | 327 | |
| 220 | 328 | /** |
| 221 | 329 | * Function for child class handle before post deleted |
| @@ -220,8 +328,9 @@ | ||
| 220 | 328 | /** |
| 221 | 329 | * Function for child class handle before post deleted |
| 222 | 330 | * |
| 223 | 331 | * @param int $post_id |
| 332 | + * | |
| 224 | 333 | * @editor tungnx |
| 225 | 334 | * @since modify 4.0.9 |
| 226 | 335 | */ |
| 227 | 336 | public function before_delete( int $post_id ) { |
| @@ -232,13 +341,9 @@ | ||
| 232 | 341 | * Hook deleted post |
| 233 | 342 | * |
| 234 | 343 | * @param int $post_id |
| 235 | 344 | */ |
| 236 | - final function _deleted_post( int $post_id ) { | |
| 237 | - if ( ! $this->check_post() ) { | |
| 238 | - return; | |
| 239 | - } | |
| 240 | - | |
| 345 | + final public function _deleted_post( int $post_id ) { | |
| 241 | 346 | $this->deleted_post( $post_id ); |
| 242 | 347 | } |
| 243 | 348 | |
| 244 | 349 | /** |
| @@ -251,8 +356,9 @@ | ||
| 251 | 356 | // Implement from child |
| 252 | 357 | } |
| 253 | 358 | |
| 254 | 359 | protected $course_of_item_trashed = 0; |
| 360 | + | |
| 255 | 361 | /** |
| 256 | 362 | * Hook before delete post |
| 257 | 363 | * |
| 258 | 364 | * @param int $post_id |
| @@ -258,12 +364,12 @@ | ||
| 258 | 364 | * @param int $post_id |
| 259 | 365 | * |
| 260 | 366 | * @author tungnx |
| 261 | 367 | * @since 4.1.6.9 |
| 262 | - * @version 1.0.0 | |
| 368 | + * @version 1.0.1 | |
| 263 | 369 | */ |
| 264 | - final function _before_trash_post( int $post_id ) { | |
| 265 | - if ( ! $this->check_post() ) { | |
| 370 | + final public function _before_trash_post( int $post_id ) { | |
| 371 | + if ( ! $this->check_post( $post_id ) ) { | |
| 266 | 372 | return; |
| 267 | 373 | } |
| 268 | 374 | |
| 269 | 375 | $this->before_trash_post( $post_id ); |
| @@ -276,19 +382,18 @@ | ||
| 276 | 382 | * |
| 277 | 383 | * @return void |
| 278 | 384 | * @author tungnx |
| 279 | 385 | * @since 4.1.6.9 |
| 280 | - * @version 1.0.0.0 | |
| 386 | + * @version 1.0.1 | |
| 281 | 387 | */ |
| 282 | 388 | public function before_trash_post( int $post_id ) { |
| 283 | - // Implement from child | |
| 284 | - // Check is item type of course | |
| 285 | - $course_item_types = learn_press_get_course_item_types(); | |
| 286 | - if ( ! in_array( get_post_type( $post_id ), $course_item_types ) ) { | |
| 287 | - return; | |
| 288 | - } | |
| 389 | + try { | |
| 390 | + // Case move item's course to trash | |
| 391 | + $course_item_types = CourseModel::item_types_support(); | |
| 392 | + if ( ! in_array( get_post_type( $post_id ), $course_item_types ) ) { | |
| 393 | + return; | |
| 394 | + } | |
| 289 | 395 | |
| 290 | - try { | |
| 291 | 396 | // Set course id of item when item assign on course is trashed |
| 292 | 397 | $course_of_item = LP_Course_DB::getInstance()->get_course_by_item_id( $post_id ); |
| 293 | 398 | if ( $course_of_item ) { |
| 294 | 399 | $this->course_of_item_trashed = $course_of_item; |
| @@ -305,12 +410,12 @@ | ||
| 305 | 410 | * |
| 306 | 411 | * @return void |
| 307 | 412 | * @author tungnx |
| 308 | 413 | * @since 4.1.6.9 |
| 309 | - * @version 1.0.0 | |
| 414 | + * @version 1.0.1 | |
| 310 | 415 | */ |
| 311 | - final function _trashed_post( int $post_id ) { | |
| 312 | - if ( ! $this->check_post() ) { | |
| 416 | + final public function _trashed_post( int $post_id ) { | |
| 417 | + if ( ! $this->check_post( $post_id ) ) { | |
| 313 | 418 | return; |
| 314 | 419 | } |
| 315 | 420 | |
| 316 | 421 | $this->trashed_post( $post_id ); |
| @@ -319,12 +424,13 @@ | ||
| 319 | 424 | /** |
| 320 | 425 | * Method handle Trashed post |
| 321 | 426 | * |
| 322 | 427 | * @param int $post_id |
| 323 | - * @author tungnx | |
| 428 | + * | |
| 429 | + * @return void | |
| 324 | 430 | * @since 4.1.6.9 |
| 325 | 431 | * @version 1.0.0 |
| 326 | - * @return void | |
| 432 | + * @author tungnx | |
| 327 | 433 | */ |
| 328 | 434 | public function trashed_post( int $post_id ) { |
| 329 | 435 | // Implement from child |
| 330 | 436 | // Check is item type of course |
| @@ -334,31 +440,89 @@ | ||
| 334 | 440 | } |
| 335 | 441 | |
| 336 | 442 | if ( $this->course_of_item_trashed ) { |
| 337 | 443 | // Save course when item assign on course is trashed |
| 338 | - $course_id = $this->course_of_item_trashed; | |
| 339 | - $course_post = get_post( $course_id ); | |
| 340 | - LP_Course_Post_Type::instance()->save( $course_id, $course_post ); | |
| 444 | + $course_id = $this->course_of_item_trashed; | |
| 445 | + LP_Course_Post_Type::instance()->save_post( $course_id, null, true ); | |
| 341 | 446 | $this->course_of_item_trashed = 0; |
| 342 | 447 | } |
| 343 | 448 | } |
| 344 | 449 | |
| 345 | 450 | public function column_instructor( $post_id = 0 ) { |
| 346 | - global $post; | |
| 451 | + $post = get_post( $post_id ); | |
| 452 | + $user_id = $post->post_author; | |
| 347 | 453 | |
| 454 | + $user = UserModel::find( $user_id, true ); | |
| 455 | + if ( ! $user ) { | |
| 456 | + return; | |
| 457 | + } | |
| 458 | + | |
| 348 | 459 | $args = array( |
| 349 | 460 | 'post_type' => $post->post_type, |
| 350 | - 'author' => get_the_author_meta( 'ID' ), | |
| 461 | + 'author' => $user_id, | |
| 351 | 462 | ); |
| 352 | 463 | |
| 353 | - $author_link = esc_url_raw( add_query_arg( $args, 'edit.php' ) ); | |
| 354 | - echo sprintf( '<span class="post-author">%s<a href="%s">%s</a></span>', get_avatar( get_the_author_meta( 'ID' ), 32 ), $author_link, get_the_author() ); | |
| 464 | + $author_link = esc_url_raw( add_query_arg( $args, 'edit.php' ) ); | |
| 465 | + $userTemplate = new UserTemplate(); | |
| 466 | + echo sprintf( | |
| 467 | + '<span class="post-author">%s<a href="%s">%s</a></span>', | |
| 468 | + $userTemplate->html_avatar( | |
| 469 | + $user, | |
| 470 | + [ | |
| 471 | + 'width' => 32, | |
| 472 | + 'height' => 32, | |
| 473 | + ] | |
| 474 | + ), | |
| 475 | + $author_link, | |
| 476 | + get_the_author() | |
| 477 | + ); | |
| 355 | 478 | } |
| 356 | 479 | |
| 480 | + /** | |
| 481 | + * Get column author | |
| 482 | + * | |
| 483 | + * @param $post | |
| 484 | + * | |
| 485 | + * @return void | |
| 486 | + * @since 4.2.9.5 | |
| 487 | + * @version 1.0.0 | |
| 488 | + */ | |
| 489 | + public static function column_author( $post ) { | |
| 490 | + if ( ! $post instanceof WP_Post ) { | |
| 491 | + return; | |
| 492 | + } | |
| 493 | + | |
| 494 | + $user_id = $post->post_author; | |
| 495 | + $userModel = UserModel::find( $user_id, true ); | |
| 496 | + if ( ! $userModel ) { | |
| 497 | + return; | |
| 498 | + } | |
| 499 | + | |
| 500 | + $args = array( | |
| 501 | + 'post_type' => $post->post_type, | |
| 502 | + 'author' => $user_id, | |
| 503 | + ); | |
| 504 | + | |
| 505 | + $author_link = esc_url_raw( add_query_arg( $args, 'edit.php' ) ); | |
| 506 | + $userTemplate = new UserTemplate(); | |
| 507 | + echo sprintf( | |
| 508 | + '<span class="post-author">%s<a href="%s">%s</a></span>', | |
| 509 | + $userTemplate->html_avatar( | |
| 510 | + $userModel, | |
| 511 | + [ | |
| 512 | + 'width' => 32, | |
| 513 | + 'height' => 32, | |
| 514 | + ] | |
| 515 | + ), | |
| 516 | + $author_link, | |
| 517 | + $userModel->get_display_name() | |
| 518 | + ); | |
| 519 | + } | |
| 520 | + | |
| 357 | 521 | public function get_post_type() { |
| 358 | 522 | $post_type = get_post_type(); |
| 359 | 523 | if ( ! $post_type ) { |
| 360 | - $post_type = LP_Request::get_string( 'post_type' ); | |
| 524 | + $post_type = LP_Request::get_param( 'post_type' ); | |
| 361 | 525 | } |
| 362 | 526 | |
| 363 | 527 | return $post_type; |
| 364 | 528 | } |
| @@ -397,9 +561,9 @@ | ||
| 397 | 561 | $postStatus = $('#post_status'), |
| 398 | 562 | $message = $('<p class="learn-press-notice-assigned-item"></p>').html(isAssigned), |
| 399 | 563 | currentStatus = $postStatus.val(); |
| 400 | 564 | |
| 401 | - (currentStatus === 'publish') && isAssigned && $postStatus.on('change', function() { | |
| 565 | + (currentStatus === 'publish') && isAssigned && $postStatus.on('change', function () { | |
| 402 | 566 | if (this.value !== 'publish') { |
| 403 | 567 | $message.insertBefore($('#post-status-select')); |
| 404 | 568 | } else { |
| 405 | 569 | $message.remove(); |
| @@ -428,9 +592,9 @@ | ||
| 428 | 592 | ); |
| 429 | 593 | |
| 430 | 594 | $course_id = $wpdb->get_var( $query ); |
| 431 | 595 | if ( $course_id ) { |
| 432 | - return __( 'This item has already assigned to course. It will be removed from course if it is not published.', 'learnpress' ); | |
| 596 | + return __( 'This item has already been assigned to the course. It will be removed from the course if it is not published.', 'learnpress' ); | |
| 433 | 597 | } |
| 434 | 598 | } elseif ( LP_QUESTION_CPT === $post_type ) { |
| 435 | 599 | $query = $wpdb->prepare( |
| 436 | 600 | " |
| @@ -443,9 +607,9 @@ | ||
| 443 | 607 | ); |
| 444 | 608 | |
| 445 | 609 | $quiz_id = $wpdb->get_var( $query ); |
| 446 | 610 | if ( $quiz_id ) { |
| 447 | - return __( 'This question has already assigned to quiz. It will be removed from quiz if it is not published.', 'learnpress' ); | |
| 611 | + return __( 'This question has already been assigned to the quiz. It will be removed from the quiz if it is not published.', 'learnpress' ); | |
| 448 | 612 | } |
| 449 | 613 | } |
| 450 | 614 | |
| 451 | 615 | return 0; |
| @@ -452,10 +616,13 @@ | ||
| 452 | 616 | } |
| 453 | 617 | |
| 454 | 618 | public function remove_auto_save_script() { |
| 455 | 619 | global $post; |
| 620 | + if ( ! $post ) { | |
| 621 | + return; | |
| 622 | + } | |
| 456 | 623 | |
| 457 | - if ( $post && in_array( get_post_type( $post->ID ), array( $this->_post_type ) ) ) { | |
| 624 | + if ( $this->check_post( $post->ID ) ) { | |
| 458 | 625 | wp_dequeue_script( 'autosave' ); |
| 459 | 626 | } |
| 460 | 627 | } |
| 461 | 628 | |
| @@ -462,12 +629,13 @@ | ||
| 462 | 629 | /** |
| 463 | 630 | * Maybe remove assigned item |
| 464 | 631 | * |
| 465 | 632 | * @param WP_Post $post |
| 633 | + * | |
| 466 | 634 | * @editor tungnx |
| 467 | 635 | * @todo Review and move to place correct |
| 468 | 636 | */ |
| 469 | - public function maybe_remove_assigned( WP_Post $post = null ) { | |
| 637 | + public function maybe_remove_assigned( $post = null ) { | |
| 470 | 638 | global $wpdb; |
| 471 | 639 | |
| 472 | 640 | if ( ! $post ) { |
| 473 | 641 | return; |
| @@ -505,9 +673,15 @@ | ||
| 505 | 673 | } |
| 506 | 674 | } |
| 507 | 675 | } |
| 508 | 676 | |
| 677 | + /** | |
| 678 | + * @deprecated v4.2.9.4 | |
| 679 | + */ | |
| 509 | 680 | protected function _get_quizzes_by_question( $question_id ) { |
| 681 | + _deprecated_function( __METHOD__, '4.2.9.4' ); | |
| 682 | + return []; | |
| 683 | + | |
| 510 | 684 | global $wpdb; |
| 511 | 685 | $query = $wpdb->prepare( |
| 512 | 686 | " |
| 513 | 687 | SELECT quiz_id |
| @@ -519,9 +693,15 @@ | ||
| 519 | 693 | |
| 520 | 694 | return $wpdb->get_col( $query ); |
| 521 | 695 | } |
| 522 | 696 | |
| 697 | + /** | |
| 698 | + * @deprecated v4.2.9.4 | |
| 699 | + */ | |
| 523 | 700 | protected function _get_courses_by_item( $item_id ) { |
| 701 | + _deprecated_function( __METHOD__, '4.2.9.4' ); | |
| 702 | + return []; | |
| 703 | + | |
| 524 | 704 | global $wpdb; |
| 525 | 705 | $query = $wpdb->prepare( |
| 526 | 706 | " |
| 527 | 707 | SELECT section_course_id |
| @@ -538,9 +718,9 @@ | ||
| 538 | 718 | /** |
| 539 | 719 | * Ouput meta boxes. |
| 540 | 720 | * |
| 541 | 721 | * @param WP_Post $post |
| 542 | - * @param mixed $box | |
| 722 | + * @param mixed $box | |
| 543 | 723 | */ |
| 544 | 724 | public function _do_output_meta_box( $post, $box ) { |
| 545 | 725 | $callback = $this->_meta_boxes[ $box['id'] ][2]; |
| 546 | 726 | if ( is_array( $callback ) ) { |
| @@ -598,9 +778,9 @@ | ||
| 598 | 778 | public function posts_join_paged( $join ) { |
| 599 | 779 | return $join; |
| 600 | 780 | } |
| 601 | 781 | |
| 602 | - final function _posts_where_paged( $where ) { | |
| 782 | + final public function _posts_where_paged( $where ) { | |
| 603 | 783 | if ( ! $this->_check_post() ) { |
| 604 | 784 | return $where; |
| 605 | 785 | } |
| 606 | 786 | |
| @@ -627,12 +807,18 @@ | ||
| 627 | 807 | * Check post valid |
| 628 | 808 | * |
| 629 | 809 | * @return bool |
| 630 | 810 | */ |
| 631 | - public function _check_post():bool { | |
| 811 | + public function _check_post(): bool { | |
| 632 | 812 | global $pagenow, $post_type; |
| 633 | 813 | |
| 634 | - if ( ! is_admin() || ( ! in_array( $pagenow, array( 'edit.php', 'post.php' ) ) ) || ( $this->_post_type != $post_type ) ) { | |
| 814 | + if ( ! is_admin() || ( ! in_array( | |
| 815 | + $pagenow, | |
| 816 | + array( | |
| 817 | + 'edit.php', | |
| 818 | + 'post.php', | |
| 819 | + ) | |
| 820 | + ) ) || ( $this->_post_type != $post_type ) ) { | |
| 635 | 821 | return false; |
| 636 | 822 | } |
| 637 | 823 | |
| 638 | 824 | return true; |
| @@ -644,29 +830,30 @@ | ||
| 644 | 830 | * @param int $post_id |
| 645 | 831 | * |
| 646 | 832 | * @return bool |
| 647 | 833 | * @since 4.1.6.9 |
| 648 | - * @version 1.0.0 | |
| 834 | + * @version 1.0.1 | |
| 649 | 835 | */ |
| 650 | - public function check_post( int $post_id = 0 ):bool { | |
| 836 | + public function check_post( int $post_id = 0 ): bool { | |
| 651 | 837 | $can_save = true; |
| 652 | 838 | |
| 653 | 839 | try { |
| 654 | 840 | $post = get_post( $post_id ); |
| 655 | - | |
| 656 | 841 | if ( ! $post ) { |
| 657 | - throw new Exception( 'Post is invalid' ); | |
| 842 | + return false; | |
| 658 | 843 | } |
| 659 | 844 | |
| 660 | 845 | if ( $this->_post_type !== $post->post_type ) { |
| 661 | - throw new Exception( 'Post type is invalid' ); | |
| 846 | + //throw new Exception( 'Post type is invalid' ); | |
| 847 | + return false; | |
| 662 | 848 | } |
| 663 | 849 | |
| 664 | - if ( ! current_user_can( ADMIN_ROLE ) ) { | |
| 665 | - if ( get_current_user_id() !== $post->post_author ) { | |
| 666 | - $can_save = apply_filters( 'lp/custom-post-type/can-save', false, $post ); | |
| 667 | - } | |
| 850 | + if ( ! current_user_can( ADMIN_ROLE ) && | |
| 851 | + get_current_user_id() !== (int) $post->post_author ) { | |
| 852 | + $can_save = false; | |
| 668 | 853 | } |
| 854 | + | |
| 855 | + $can_save = apply_filters( 'lp/custom-post-type/can-save', $can_save, $post ); | |
| 669 | 856 | } catch ( Throwable $e ) { |
| 670 | 857 | $can_save = false; |
| 671 | 858 | } |
| 672 | 859 | |
| @@ -677,9 +864,9 @@ | ||
| 677 | 864 | * Check is page list posts valid |
| 678 | 865 | * |
| 679 | 866 | * @return bool |
| 680 | 867 | */ |
| 681 | - protected function is_page_list_posts_on_backend():bool { | |
| 868 | + protected function is_page_list_posts_on_backend(): bool { | |
| 682 | 869 | global $pagenow, $post_type; |
| 683 | 870 | |
| 684 | 871 | if ( ! is_admin() || $pagenow != 'edit.php' || ( $this->_post_type != $post_type ) ) { |
| 685 | 872 | return false; |
| @@ -690,11 +877,10 @@ | ||
| 690 | 877 | |
| 691 | 878 | /** |
| 692 | 879 | * New Metabox instance |
| 693 | 880 | * |
| 881 | + * @return array | |
| 694 | 882 | * @author Nhamdv |
| 695 | - * | |
| 696 | - * @return void | |
| 697 | 883 | */ |
| 698 | 884 | public function meta_boxes() { |
| 699 | 885 | return array(); |
| 700 | 886 | } |
| @@ -701,11 +887,10 @@ | ||
| 701 | 887 | |
| 702 | 888 | /** |
| 703 | 889 | * Render Metabox. |
| 704 | 890 | * |
| 891 | + * @return void | |
| 705 | 892 | * @author Nhamdv |
| 706 | - * | |
| 707 | - * @return void | |
| 708 | 893 | */ |
| 709 | 894 | public function render_meta_box() { |
| 710 | 895 | $add_meta_box = $this->meta_boxes(); |
| 711 | 896 | $metaboxes = ! empty( $add_meta_box ) && is_array( $add_meta_box ) ? $add_meta_box : array(); |
| @@ -753,18 +938,16 @@ | ||
| 753 | 938 | |
| 754 | 939 | $meta_box[2] = array( $this, '_do_output_meta_box' ); |
| 755 | 940 | call_user_func_array( 'add_meta_box', $meta_box ); |
| 756 | 941 | } |
| 757 | - | |
| 758 | 942 | } |
| 759 | 943 | |
| 760 | 944 | /** |
| 761 | 945 | * Filter item by the course selected. |
| 762 | 946 | * |
| 763 | - * @since 3.0.7 | |
| 764 | - * | |
| 765 | 947 | * @return bool|int |
| 766 | 948 | * @Todo move to course LP_Course_Post_Type |
| 949 | + * @since 3.0.7 | |
| 767 | 950 | */ |
| 768 | 951 | protected function _filter_items_by_course() { |
| 769 | 952 | $course_id = ! empty( $_REQUEST['course'] ) ? absint( $_REQUEST['course'] ) : false; |
| 770 | 953 | |
| @@ -809,17 +992,20 @@ | ||
| 809 | 992 | * Get course that the items is contained. |
| 810 | 993 | * |
| 811 | 994 | * @param $post_id |
| 812 | 995 | */ |
| 813 | - protected function _get_item_course( $post_id ) { | |
| 814 | - $courses = learn_press_get_item_courses( $post_id ); | |
| 996 | + protected function get_courses_of_item( $post_id ) { | |
| 997 | + $courses = CourseSectionItemModel::get_courses_from_item_id( | |
| 998 | + $post_id, get_post_type( $post_id ) | |
| 999 | + ); | |
| 815 | 1000 | if ( $courses ) { |
| 816 | 1001 | foreach ( $courses as $course ) { |
| 817 | - echo '<div><a href="' . esc_url_raw( remove_query_arg( 'orderby', add_query_arg( array( 'course' => $course->ID ) ) ) ) . '">' . get_the_title( $course->ID ) . '</a>'; | |
| 1002 | + $course_id = $course->section_course_id; | |
| 1003 | + echo '<div><a href="' . esc_url_raw( remove_query_arg( 'orderby', add_query_arg( array( 'course' => $course_id ) ) ) ) . '">' . get_the_title( $course_id ) . '</a>'; | |
| 818 | 1004 | echo '<div class="row-actions">'; |
| 819 | - printf( '<a href="%s">%s</a>', admin_url( sprintf( 'post.php?post=%d&action=edit', $course->ID ) ), __( 'Edit', 'learnpress' ) ); | |
| 1005 | + printf( '<a href="%s">%s</a>', admin_url( sprintf( 'post.php?post=%d&action=edit', $course_id ) ), __( 'Edit', 'learnpress' ) ); | |
| 820 | 1006 | echo ' | '; |
| 821 | - printf( '<a href="%s">%s</a>', get_the_permalink( $course->ID ), __( 'View', 'learnpress' ) ); | |
| 1007 | + printf( '<a href="%s">%s</a>', get_the_permalink( $course_id ), __( 'View', 'learnpress' ) ); | |
| 822 | 1008 | |
| 823 | 1009 | if ( $this->_filter_items_by_course() ) { |
| 824 | 1010 | echo ' | '; |
| 825 | 1011 | printf( |
| @@ -873,9 +1059,9 @@ | ||
| 873 | 1059 | /** |
| 874 | 1060 | * @return string |
| 875 | 1061 | */ |
| 876 | 1062 | protected function get_order_sort(): string { |
| 877 | - return strtolower( LP_Request::get( 'order' ) ) === 'desc' ? 'DESC' : 'ASC'; | |
| 1063 | + return strtolower( LP_Request::get_param( 'order' ) ) === 'desc' ? 'DESC' : 'ASC'; | |
| 878 | 1064 | } |
| 879 | 1065 | |
| 880 | 1066 | /** |
| 881 | 1067 | * @return mixed |
| @@ -880,9 +1066,9 @@ | ||
| 880 | 1066 | /** |
| 881 | 1067 | * @return mixed |
| 882 | 1068 | */ |
| 883 | 1069 | protected function get_order_by(): string { |
| 884 | - return LP_Request::get( 'orderby' ); | |
| 1070 | + return LP_Request::get_param( 'orderby' ); | |
| 885 | 1071 | } |
| 886 | 1072 | |
| 887 | 1073 | /** |
| 888 | 1074 | * Show actions on list post |
| @@ -887,9 +1073,10 @@ | ||
| 887 | 1073 | /** |
| 888 | 1074 | * Show actions on list post |
| 889 | 1075 | * |
| 890 | 1076 | * @param string[] $actions |
| 891 | - * @param WP_Post $post | |
| 1077 | + * @param WP_Post $post | |
| 1078 | + * | |
| 892 | 1079 | * @return array|false|mixed |
| 893 | 1080 | */ |
| 894 | 1081 | public function _post_row_actions( $actions, $post ) { |
| 895 | 1082 | if ( ! $this->_check_post() ) { |
| @@ -903,9 +1090,9 @@ | ||
| 903 | 1090 | return $actions; |
| 904 | 1091 | } |
| 905 | 1092 | |
| 906 | 1093 | /** |
| 907 | - * @depecated 4.1.6.9 | |
| 1094 | + * @deprecated 4.1.6.9 | |
| 908 | 1095 | */ |
| 909 | 1096 | public function updated_messages( $messages ) { |
| 910 | 1097 | $post = get_post(); |
| 911 | 1098 | $post_type = get_post_type( $post ); |
| @@ -919,9 +1106,9 @@ | ||
| 919 | 1106 | 2 => __( 'Custom field updated.', 'learnpress' ), |
| 920 | 1107 | 3 => __( 'Custom field deleted.', 'learnpress' ), |
| 921 | 1108 | 4 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'updated.', 'learnpress' ) ), |
| 922 | 1109 | /* translators: %s: date and time of the revision */ |
| 923 | - 5 => isset( $_GET['revision'] ) ? sprintf( __( 'Lesson restored to revision from %s', 'learnpress' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, | |
| 1110 | + 5 => isset( $_GET['revision'] ) ? sprintf( __( 'The lesson has been restored to revision from %s', 'learnpress' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, | |
| 924 | 1111 | 6 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'published.', 'learnpress' ) ), |
| 925 | 1112 | 7 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'saved.', 'learnpress' ) ), |
| 926 | 1113 | 8 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'submitted.', 'learnpress' ) ), |
| 927 | 1114 | 9 => sprintf( |