| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class LP_Abstract_Post_Type |
| 5 |
* |
| 6 |
* @author ThimPress |
| 7 |
* @package LearnPress/Classes |
| 8 |
* @version 1.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit(); |
| 12 |
|
| 13 |
abstract class LP_Abstract_Post_Type { |
| 14 |
/** |
| 15 |
* Type of post |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
protected $_post_type = ''; |
| 20 |
|
| 21 |
/** |
| 22 |
* Metaboxes registered |
| 23 |
* |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
protected $_meta_boxes = array(); |
| 27 |
|
| 28 |
/** |
| 29 |
* @var null |
| 30 |
*/ |
| 31 |
protected $_current_meta_box = null; |
| 32 |
|
| 33 |
/** |
| 34 |
* Columns display on list table |
| 35 |
* |
| 36 |
* @var array |
| 37 |
*/ |
| 38 |
protected $_columns = array(); |
| 39 |
|
| 40 |
/** |
| 41 |
* Sortable columns |
| 42 |
* |
| 43 |
* @var array |
| 44 |
*/ |
| 45 |
protected $_sortable_columns = array(); |
| 46 |
|
| 47 |
/** |
| 48 |
* Map default method to a new method |
| 49 |
* |
| 50 |
* @var array |
| 51 |
*/ |
| 52 |
// protected $_map_methods = array(); |
| 53 |
|
| 54 |
/** |
| 55 |
* @var array |
| 56 |
*/ |
| 57 |
protected $_default_metas = array(); |
| 58 |
|
| 59 |
/** |
| 60 |
* @var array |
| 61 |
*/ |
| 62 |
protected $_remove_features = array(); |
| 63 |
|
| 64 |
/** |
| 65 |
* Constructor |
| 66 |
* |
| 67 |
* @param string |
| 68 |
* @param mixed |
| 69 |
*/ |
| 70 |
public function __construct( $post_type = '', $args = '' ) { |
| 71 |
|
| 72 |
if ( ! empty( $post_type ) ) { |
| 73 |
$this->_post_type = $post_type; |
| 74 |
} |
| 75 |
add_action( 'init', array( $this, '_do_register' ) ); |
| 76 |
add_action( 'save_post', array( $this, '_do_save_post' ), 10, 2 ); |
| 77 |
add_action( 'before_delete_post', array( $this, '_before_delete_post' ) ); |
| 78 |
add_action( 'deleted_post', array( $this, '_deleted_post' ) ); |
| 79 |
add_action( 'wp_trash_post', array( $this, '_before_trash_post' ) ); |
| 80 |
add_action( 'trashed_post', array( $this, '_trashed_post' ) ); |
| 81 |
|
| 82 |
add_filter( 'manage_edit-' . $this->_post_type . '_sortable_columns', array( $this, 'sortable_columns' ) ); |
| 83 |
add_filter( 'manage_' . $this->_post_type . '_posts_columns', array( $this, 'columns_head' ) ); |
| 84 |
add_filter( 'manage_' . $this->_post_type . '_posts_custom_column', array( $this, 'columns_content' ), 10, 2 ); |
| 85 |
|
| 86 |
add_filter( 'posts_fields', array( $this, '_posts_fields' ) ); |
| 87 |
add_filter( 'posts_join_paged', array( $this, '_posts_join_paged' ) ); |
| 88 |
add_filter( 'posts_where_paged', array( $this, '_posts_where_paged' ) ); |
| 89 |
add_filter( 'posts_orderby', array( $this, '_posts_orderby' ) ); |
| 90 |
|
| 91 |
// Show actions link on list post admin. |
| 92 |
add_filter( 'post_row_actions', array( $this, '_post_row_actions' ), 10, 2 ); |
| 93 |
|
| 94 |
// New metabox: Nhamdv |
| 95 |
add_action( 'add_meta_boxes', array( $this, 'render_meta_box' ), 0 ); |
| 96 |
|
| 97 |
// After update h5p and withdraw will remove it. |
| 98 |
add_action( 'load-post.php', array( $this, 'add_meta_boxes' ), 0 ); |
| 99 |
add_action( 'load-post-new.php', array( $this, 'add_meta_boxes' ), 0 ); |
| 100 |
// End |
| 101 |
|
| 102 |
// Comment by tungnx |
| 103 |
// add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) ); |
| 104 |
// add_action( 'admin_enqueue_scripts', array( $this, 'admin_styles' ) ); |
| 105 |
|
| 106 |
// Comment by tungnx |
| 107 |
// add_action( 'admin_footer-post.php', array( $this, 'print_js_template' ) ); |
| 108 |
// add_action( 'admin_footer-post-new.php', array( $this, 'print_js_template' ) ); |
| 109 |
|
| 110 |
// Comment by tungnx - not use |
| 111 |
// add_action( 'pre_get_posts', array( $this, 'update_default_meta' ) ); |
| 112 |
add_action( 'admin_footer', array( $this, 'admin_footer_scripts' ) ); |
| 113 |
|
| 114 |
//add_filter( 'post_updated_messages', array( $this, 'updated_messages' ) ); |
| 115 |
|
| 116 |
$args = wp_parse_args( |
| 117 |
$args, |
| 118 |
array( |
| 119 |
'auto_save' => 'no', |
| 120 |
'default_meta' => false, |
| 121 |
) |
| 122 |
); |
| 123 |
|
| 124 |
if ( $args['auto_save'] == 'no' ) { |
| 125 |
add_action( 'admin_print_scripts', array( $this, 'remove_auto_save_script' ) ); |
| 126 |
} |
| 127 |
|
| 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 ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* This function is invoked along with 'init' action to register |
| 139 |
* new post type with WP. |
| 140 |
* |
| 141 |
* @editor tungnx |
| 142 |
* @since modify 4.1.0 |
| 143 |
*/ |
| 144 |
public function _do_register() { |
| 145 |
$args = $this->args_register_post_type(); |
| 146 |
|
| 147 |
/* |
| 148 |
* Todo: This is function old, still has on some addons, so need replace "register" function to args_register_post_type |
| 149 |
* When replace all will delete this function - long ago will delete, for some user didn't updated new addon version fix |
| 150 |
*/ |
| 151 |
if ( method_exists( $this, 'register' ) ) { |
| 152 |
$args = $this->register(); |
| 153 |
} |
| 154 |
|
| 155 |
if ( $args ) { |
| 156 |
register_post_type( $this->_post_type, $args ); |
| 157 |
|
| 158 |
flush_rewrite_rules(); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Args to register custom post type. |
| 164 |
* |
| 165 |
* @return array |
| 166 |
*/ |
| 167 |
public function args_register_post_type() : array { |
| 168 |
return array(); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Hook save post of WP |
| 173 |
* |
| 174 |
* In child-class use function save() |
| 175 |
* |
| 176 |
* @param int $post_id |
| 177 |
* @param WP_Post $post |
| 178 |
* @editor tungnx |
| 179 |
* @since modify 4.0.9 |
| 180 |
* @version 4.0.1 |
| 181 |
*/ |
| 182 |
final function _do_save_post( int $post_id = 0, WP_Post $post = null ) { |
| 183 |
// Maybe remove |
| 184 |
$this->maybe_remove_assigned( $post ); |
| 185 |
|
| 186 |
if ( ! $this->check_post( $post_id ) ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
$this->save( $post_id, $post ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Function for child class handle when post has just saved |
| 195 |
* |
| 196 |
* @editor tungnx |
| 197 |
* @docs Class post type extend need override this function if want to handle when save |
| 198 |
*/ |
| 199 |
public function save( int $post_id, WP_Post $post ) { |
| 200 |
// Implement from child |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Hook before delete post |
| 205 |
* Only on receiver 1 param $post_id, can't get param $post - don't know why |
| 206 |
* |
| 207 |
* @param int $post_id |
| 208 |
* |
| 209 |
* @editor tungnx |
| 210 |
* @since modify 4.0.9 |
| 211 |
*/ |
| 212 |
final function _before_delete_post( int $post_id ) { |
| 213 |
if ( ! $this->check_post( $post_id ) ) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
$this->before_delete( $post_id ); |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Function for child class handle before post deleted |
| 222 |
* |
| 223 |
* @param int $post_id |
| 224 |
* @editor tungnx |
| 225 |
* @since modify 4.0.9 |
| 226 |
*/ |
| 227 |
public function before_delete( int $post_id ) { |
| 228 |
// Implement from child |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Hook deleted post |
| 233 |
* |
| 234 |
* @param int $post_id |
| 235 |
*/ |
| 236 |
final function _deleted_post( int $post_id ) { |
| 237 |
if ( ! $this->check_post() ) { |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
$this->deleted_post( $post_id ); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Function for child class handle when post has just deleted |
| 246 |
* |
| 247 |
* @editor tungnx |
| 248 |
* @docs Class post type extend need override this function if want to handle when post deleted |
| 249 |
*/ |
| 250 |
public function deleted_post( int $post_id ) { |
| 251 |
// Implement from child |
| 252 |
} |
| 253 |
|
| 254 |
protected $course_of_item_trashed = 0; |
| 255 |
/** |
| 256 |
* Hook before delete post |
| 257 |
* |
| 258 |
* @param int $post_id |
| 259 |
* |
| 260 |
* @author tungnx |
| 261 |
* @since 4.1.6.9 |
| 262 |
* @version 1.0.0 |
| 263 |
*/ |
| 264 |
final function _before_trash_post( int $post_id ) { |
| 265 |
if ( ! $this->check_post() ) { |
| 266 |
return; |
| 267 |
} |
| 268 |
|
| 269 |
$this->before_trash_post( $post_id ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Before trash post |
| 274 |
* |
| 275 |
* @param int $post_id |
| 276 |
* |
| 277 |
* @return void |
| 278 |
* @author tungnx |
| 279 |
* @since 4.1.6.9 |
| 280 |
* @version 1.0.0.0 |
| 281 |
*/ |
| 282 |
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 |
} |
| 289 |
|
| 290 |
try { |
| 291 |
// Set course id of item when item assign on course is trashed |
| 292 |
$course_of_item = LP_Course_DB::getInstance()->get_course_by_item_id( $post_id ); |
| 293 |
if ( $course_of_item ) { |
| 294 |
$this->course_of_item_trashed = $course_of_item; |
| 295 |
} |
| 296 |
} catch ( Throwable $e ) { |
| 297 |
error_log( $e->getMessage() ); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Hook Trashed post |
| 303 |
* |
| 304 |
* @param int $post_id |
| 305 |
* |
| 306 |
* @return void |
| 307 |
* @author tungnx |
| 308 |
* @since 4.1.6.9 |
| 309 |
* @version 1.0.0 |
| 310 |
*/ |
| 311 |
final function _trashed_post( int $post_id ) { |
| 312 |
if ( ! $this->check_post() ) { |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
$this->trashed_post( $post_id ); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* Method handle Trashed post |
| 321 |
* |
| 322 |
* @param int $post_id |
| 323 |
* @author tungnx |
| 324 |
* @since 4.1.6.9 |
| 325 |
* @version 1.0.0 |
| 326 |
* @return void |
| 327 |
*/ |
| 328 |
public function trashed_post( int $post_id ) { |
| 329 |
// Implement from child |
| 330 |
// Check is item type of course |
| 331 |
$course_item_types = learn_press_get_course_item_types(); |
| 332 |
if ( ! in_array( get_post_type( $post_id ), $course_item_types ) ) { |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
if ( $this->course_of_item_trashed ) { |
| 337 |
// 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 ); |
| 341 |
$this->course_of_item_trashed = 0; |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
public function column_instructor( $post_id = 0 ) { |
| 346 |
global $post; |
| 347 |
|
| 348 |
$args = array( |
| 349 |
'post_type' => $post->post_type, |
| 350 |
'author' => get_the_author_meta( 'ID' ), |
| 351 |
); |
| 352 |
|
| 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() ); |
| 355 |
} |
| 356 |
|
| 357 |
public function get_post_type() { |
| 358 |
$post_type = get_post_type(); |
| 359 |
if ( ! $post_type ) { |
| 360 |
$post_type = LP_Request::get_string( 'post_type' ); |
| 361 |
} |
| 362 |
|
| 363 |
return $post_type; |
| 364 |
} |
| 365 |
|
| 366 |
public function admin_footer_scripts() { |
| 367 |
|
| 368 |
global $pagenow; |
| 369 |
|
| 370 |
if ( $this->get_post_type() !== $this->_post_type ) { |
| 371 |
return; |
| 372 |
} |
| 373 |
|
| 374 |
$user = learn_press_get_current_user(); |
| 375 |
|
| 376 |
if ( ! $user->is_admin() ) { |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
// Comment by tungnx - not use on here, wrote on js |
| 381 |
/* |
| 382 |
if ( $pagenow === 'edit.php' ) { |
| 383 |
$option = sprintf( '<option value="">%s</option>', __( 'Search by user', 'learnpress' ) ); |
| 384 |
$user = get_user_by( 'id', LP_Request::get_int( 'author' ) ); |
| 385 |
|
| 386 |
if ( $user ) { |
| 387 |
$option = sprintf( '<option value="%d" selected="selected">%s</option>', $user->ID, $user->user_login ); |
| 388 |
} |
| 389 |
}*/ |
| 390 |
|
| 391 |
// Todo: write this code on file js |
| 392 |
if ( $pagenow === 'post.php' ) { |
| 393 |
?> |
| 394 |
<script> |
| 395 |
jQuery(function ($) { |
| 396 |
var isAssigned = '<?php echo esc_js( $this->is_assigned() ); ?>', |
| 397 |
$postStatus = $('#post_status'), |
| 398 |
$message = $('<p class="learn-press-notice-assigned-item"></p>').html(isAssigned), |
| 399 |
currentStatus = $postStatus.val(); |
| 400 |
|
| 401 |
(currentStatus === 'publish') && isAssigned && $postStatus.on('change', function() { |
| 402 |
if (this.value !== 'publish') { |
| 403 |
$message.insertBefore($('#post-status-select')); |
| 404 |
} else { |
| 405 |
$message.remove(); |
| 406 |
} |
| 407 |
}); |
| 408 |
|
| 409 |
}) |
| 410 |
</script> |
| 411 |
<?php |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
public function is_assigned() { |
| 416 |
global $wpdb; |
| 417 |
$post_type = $this->get_post_type(); |
| 418 |
if ( learn_press_is_support_course_item_type( $post_type ) ) { |
| 419 |
$query = $wpdb->prepare( |
| 420 |
" |
| 421 |
SELECT s.section_course_id |
| 422 |
FROM {$wpdb->learnpress_section_items} si |
| 423 |
INNER JOIN {$wpdb->learnpress_sections} s ON s.section_id = si.section_id |
| 424 |
INNER JOIN {$wpdb->posts} p ON p.ID = si.item_id |
| 425 |
WHERE p.ID = %d |
| 426 |
", |
| 427 |
get_the_ID() |
| 428 |
); |
| 429 |
|
| 430 |
$course_id = $wpdb->get_var( $query ); |
| 431 |
if ( $course_id ) { |
| 432 |
return __( 'This item has already assigned to course. It will be removed from course if it is not published.', 'learnpress' ); |
| 433 |
} |
| 434 |
} elseif ( LP_QUESTION_CPT === $post_type ) { |
| 435 |
$query = $wpdb->prepare( |
| 436 |
" |
| 437 |
SELECT p.ID |
| 438 |
FROM {$wpdb->posts} p |
| 439 |
INNER JOIN {$wpdb->learnpress_quiz_questions} qq ON p.ID = qq.quiz_id |
| 440 |
WHERE qq.question_id = %d |
| 441 |
", |
| 442 |
get_the_ID() |
| 443 |
); |
| 444 |
|
| 445 |
$quiz_id = $wpdb->get_var( $query ); |
| 446 |
if ( $quiz_id ) { |
| 447 |
return __( 'This question has already assigned to quiz. It will be removed from quiz if it is not published.', 'learnpress' ); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
return 0; |
| 452 |
} |
| 453 |
|
| 454 |
public function remove_auto_save_script() { |
| 455 |
global $post; |
| 456 |
|
| 457 |
if ( $post && in_array( get_post_type( $post->ID ), array( $this->_post_type ) ) ) { |
| 458 |
wp_dequeue_script( 'autosave' ); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
/** |
| 463 |
* Maybe remove assigned item |
| 464 |
* |
| 465 |
* @param WP_Post $post |
| 466 |
* @editor tungnx |
| 467 |
* @todo Review and move to place correct |
| 468 |
*/ |
| 469 |
public function maybe_remove_assigned( WP_Post $post = null ) { |
| 470 |
global $wpdb; |
| 471 |
|
| 472 |
if ( ! $post ) { |
| 473 |
return; |
| 474 |
} |
| 475 |
|
| 476 |
$post_type = $post->post_type; |
| 477 |
$post_status = $post->post_status; |
| 478 |
|
| 479 |
// If we are updating question |
| 480 |
if ( LP_QUESTION_CPT === $post_type ) { |
| 481 |
|
| 482 |
// If question is not published then delete it from quizzes |
| 483 |
if ( $post_status !== 'publish' ) { |
| 484 |
$query = $wpdb->prepare( |
| 485 |
" |
| 486 |
DELETE FROM {$wpdb->learnpress_quiz_questions} |
| 487 |
WHERE question_id = %d |
| 488 |
", |
| 489 |
$post->ID |
| 490 |
); |
| 491 |
$wpdb->query( $query ); |
| 492 |
} |
| 493 |
} elseif ( learn_press_is_support_course_item_type( $post_type ) ) { |
| 494 |
|
| 495 |
// If item is not published then delete it from courses |
| 496 |
if ( $post_status !== 'publish' ) { |
| 497 |
$query = $wpdb->prepare( |
| 498 |
" |
| 499 |
DELETE FROM {$wpdb->learnpress_section_items} |
| 500 |
WHERE item_id = %d |
| 501 |
", |
| 502 |
$post->ID |
| 503 |
); |
| 504 |
$wpdb->query( $query ); |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
protected function _get_quizzes_by_question( $question_id ) { |
| 510 |
global $wpdb; |
| 511 |
$query = $wpdb->prepare( |
| 512 |
" |
| 513 |
SELECT quiz_id |
| 514 |
FROM {$wpdb->learnpress_quiz_questions} |
| 515 |
WHERE question_id = %d |
| 516 |
", |
| 517 |
$question_id |
| 518 |
); |
| 519 |
|
| 520 |
return $wpdb->get_col( $query ); |
| 521 |
} |
| 522 |
|
| 523 |
protected function _get_courses_by_item( $item_id ) { |
| 524 |
global $wpdb; |
| 525 |
$query = $wpdb->prepare( |
| 526 |
" |
| 527 |
SELECT section_course_id |
| 528 |
FROM {$wpdb->learnpress_sections} s |
| 529 |
INNER JOIN {$wpdb->learnpress_section_items} si ON s.section_id = si.section_id |
| 530 |
WHERE si.item_id = %d |
| 531 |
", |
| 532 |
$item_id |
| 533 |
); |
| 534 |
|
| 535 |
return $wpdb->get_col( $query ); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Ouput meta boxes. |
| 540 |
* |
| 541 |
* @param WP_Post $post |
| 542 |
* @param mixed $box |
| 543 |
*/ |
| 544 |
public function _do_output_meta_box( $post, $box ) { |
| 545 |
$callback = $this->_meta_boxes[ $box['id'] ][2]; |
| 546 |
if ( is_array( $callback ) ) { |
| 547 |
if ( $callback[0] instanceof LP_Abstract_Post_Type ) { |
| 548 |
if ( $callback[1] != __FUNCTION__ ) { |
| 549 |
call_user_func_array( $callback, array( $post, $box ) ); |
| 550 |
} |
| 551 |
} else { |
| 552 |
call_user_func_array( $callback, array( $post, $box ) ); |
| 553 |
} |
| 554 |
} else { |
| 555 |
if ( is_callable( array( $this, $callback ) ) ) { |
| 556 |
call_user_func_array( array( $this, $callback ), array( $post, $box ) ); |
| 557 |
} else { |
| 558 |
call_user_func_array( $callback, array( $post, $box ) ); |
| 559 |
} |
| 560 |
} |
| 561 |
} |
| 562 |
|
| 563 |
/** |
| 564 |
* @editor tungnx |
| 565 |
* @reason not use |
| 566 |
*/ |
| 567 |
/* |
| 568 |
private function _is_archive() { |
| 569 |
global $pagenow, $post_type; |
| 570 |
if ( ! is_admin() || ( $pagenow != 'edit.php' ) || ( $this->_post_type != LP_Request::get_string( 'post_type' ) ) ) { |
| 571 |
return false; |
| 572 |
} |
| 573 |
|
| 574 |
return true; |
| 575 |
}*/ |
| 576 |
|
| 577 |
protected function _flush_cache() { |
| 578 |
// LP_Hard_Cache::flush(); |
| 579 |
// wp_cache_flush(); |
| 580 |
} |
| 581 |
|
| 582 |
public function _posts_fields( $fields ) { |
| 583 |
if ( ! $this->_check_post() ) { |
| 584 |
return $fields; |
| 585 |
} |
| 586 |
|
| 587 |
return $this->posts_fields( $fields ); |
| 588 |
} |
| 589 |
|
| 590 |
public function _posts_join_paged( $join ) { |
| 591 |
if ( ! $this->_check_post() ) { |
| 592 |
return $join; |
| 593 |
} |
| 594 |
|
| 595 |
return $this->posts_join_paged( $join ); |
| 596 |
} |
| 597 |
|
| 598 |
public function posts_join_paged( $join ) { |
| 599 |
return $join; |
| 600 |
} |
| 601 |
|
| 602 |
final function _posts_where_paged( $where ) { |
| 603 |
if ( ! $this->_check_post() ) { |
| 604 |
return $where; |
| 605 |
} |
| 606 |
|
| 607 |
return $this->posts_where_paged( $where ); |
| 608 |
} |
| 609 |
|
| 610 |
public function posts_where_paged( $where ) { |
| 611 |
return $where; |
| 612 |
} |
| 613 |
|
| 614 |
public function _posts_orderby( $orderby ) { |
| 615 |
if ( ! $this->_check_post() ) { |
| 616 |
return $orderby; |
| 617 |
} |
| 618 |
|
| 619 |
return $this->posts_orderby( $orderby ); |
| 620 |
} |
| 621 |
|
| 622 |
public function posts_orderby( $orderby ) { |
| 623 |
return $orderby; |
| 624 |
} |
| 625 |
|
| 626 |
/** |
| 627 |
* Check post valid |
| 628 |
* |
| 629 |
* @return bool |
| 630 |
*/ |
| 631 |
public function _check_post():bool { |
| 632 |
global $pagenow, $post_type; |
| 633 |
|
| 634 |
if ( ! is_admin() || ( ! in_array( $pagenow, array( 'edit.php', 'post.php' ) ) ) || ( $this->_post_type != $post_type ) ) { |
| 635 |
return false; |
| 636 |
} |
| 637 |
|
| 638 |
return true; |
| 639 |
} |
| 640 |
|
| 641 |
/** |
| 642 |
* Check post is valid to handle |
| 643 |
* |
| 644 |
* @param int $post_id |
| 645 |
* |
| 646 |
* @return bool |
| 647 |
* @since 4.1.6.9 |
| 648 |
* @version 1.0.0 |
| 649 |
*/ |
| 650 |
public function check_post( int $post_id = 0 ):bool { |
| 651 |
$can_save = true; |
| 652 |
|
| 653 |
try { |
| 654 |
$post = get_post( $post_id ); |
| 655 |
|
| 656 |
if ( ! $post ) { |
| 657 |
throw new Exception( 'Post is invalid' ); |
| 658 |
} |
| 659 |
|
| 660 |
if ( $this->_post_type !== $post->post_type ) { |
| 661 |
throw new Exception( 'Post type is invalid' ); |
| 662 |
} |
| 663 |
|
| 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 |
} |
| 668 |
} |
| 669 |
} catch ( Throwable $e ) { |
| 670 |
|
| 671 |
} |
| 672 |
|
| 673 |
return $can_save; |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* Check is page list posts valid |
| 678 |
* |
| 679 |
* @return bool |
| 680 |
*/ |
| 681 |
protected function is_page_list_posts_on_backend():bool { |
| 682 |
global $pagenow, $post_type; |
| 683 |
|
| 684 |
if ( ! is_admin() || $pagenow != 'edit.php' || ( $this->_post_type != $post_type ) ) { |
| 685 |
return false; |
| 686 |
} |
| 687 |
|
| 688 |
return true; |
| 689 |
} |
| 690 |
|
| 691 |
/** |
| 692 |
* New Metabox instance |
| 693 |
* |
| 694 |
* @author Nhamdv |
| 695 |
* |
| 696 |
* @return void |
| 697 |
*/ |
| 698 |
public function meta_boxes() { |
| 699 |
return array(); |
| 700 |
} |
| 701 |
|
| 702 |
/** |
| 703 |
* Render Metabox. |
| 704 |
* |
| 705 |
* @author Nhamdv |
| 706 |
* |
| 707 |
* @return void |
| 708 |
*/ |
| 709 |
public function render_meta_box() { |
| 710 |
$add_meta_box = $this->meta_boxes(); |
| 711 |
$metaboxes = ! empty( $add_meta_box ) && is_array( $add_meta_box ) ? $add_meta_box : array(); |
| 712 |
|
| 713 |
$metaboxes = apply_filters( 'learnpress/custom-post-type/add-meta-box', $metaboxes, $this->_post_type ); |
| 714 |
|
| 715 |
if ( ! empty( $metaboxes ) ) { |
| 716 |
foreach ( $metaboxes as $metabox_id => $metabox ) { |
| 717 |
if ( isset( $metabox['callback'] ) ) { |
| 718 |
add_meta_box( $metabox_id, $metabox['title'] ?? esc_html__( 'Unknown', 'learnpress' ), $metabox['callback'], $metabox['post_type'] ?? $this->_post_type, $metabox['context'] ?? 'normal', $metabox['priority'] ?? 'high' ); |
| 719 |
} |
| 720 |
} |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
// Todo: after update metabox in h5p and withdraw will remove this function |
| 725 |
public function add_meta_box( $id, $title, $callback = null, $context = 'advanced', $priority = 'default', $callback_args = null ) { |
| 726 |
$this->_meta_boxes[ $id ] = func_get_args(); |
| 727 |
|
| 728 |
return $this; |
| 729 |
} |
| 730 |
|
| 731 |
// Todo: after update metabox in h5p and withdraw will remove this function |
| 732 |
public function add_meta_boxes() { |
| 733 |
if ( $this->_post_type != learn_press_get_requested_post_type() ) { |
| 734 |
return; |
| 735 |
} |
| 736 |
|
| 737 |
do_action( 'learn_press_add_meta_boxes', $this->_post_type, $this ); |
| 738 |
do_action( "learn_press_{$this->_post_type}_add_meta_boxes", $this ); |
| 739 |
|
| 740 |
if ( ! $this->_meta_boxes ) { |
| 741 |
return; |
| 742 |
} |
| 743 |
|
| 744 |
foreach ( $this->_meta_boxes as $k => $meta_box ) { |
| 745 |
$size = sizeof( $meta_box ); |
| 746 |
|
| 747 |
if ( ( $size == 2 ) || ( $size == 3 && ! $meta_box[2] ) ) { |
| 748 |
$func = 'output_' . preg_replace( '/[-]+/', '_', $meta_box[0] ); |
| 749 |
$meta_box[2] = array( $this, $func ); |
| 750 |
} |
| 751 |
array_splice( $meta_box, 3, 0, array( $this->_post_type ) ); |
| 752 |
$this->_meta_boxes[ $k ] = $meta_box; |
| 753 |
|
| 754 |
$meta_box[2] = array( $this, '_do_output_meta_box' ); |
| 755 |
call_user_func_array( 'add_meta_box', $meta_box ); |
| 756 |
} |
| 757 |
|
| 758 |
} |
| 759 |
|
| 760 |
/** |
| 761 |
* Filter item by the course selected. |
| 762 |
* |
| 763 |
* @since 3.0.7 |
| 764 |
* |
| 765 |
* @return bool|int |
| 766 |
* @Todo move to course LP_Course_Post_Type |
| 767 |
*/ |
| 768 |
protected function _filter_items_by_course() { |
| 769 |
$course_id = ! empty( $_REQUEST['course'] ) ? absint( $_REQUEST['course'] ) : false; |
| 770 |
|
| 771 |
if ( ! $course_id ) { |
| 772 |
global $post_type; |
| 773 |
if ( ! learn_press_is_support_course_item_type( $post_type ) ) { |
| 774 |
$course_id = false; |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
return $course_id; |
| 779 |
} |
| 780 |
|
| 781 |
/** |
| 782 |
* @return mixed |
| 783 |
* @Todo move to course LP_Course_Post_Type |
| 784 |
*/ |
| 785 |
protected function _get_course_column_title() { |
| 786 |
global $post_type; |
| 787 |
|
| 788 |
if ( ! learn_press_is_support_course_item_type( $post_type ) ) { |
| 789 |
return false; |
| 790 |
} |
| 791 |
|
| 792 |
$title = esc_html__( 'Course', 'learnpress' ); |
| 793 |
$course_id = $this->_filter_items_by_course(); |
| 794 |
|
| 795 |
if ( $course_id ) { |
| 796 |
$course = learn_press_get_course( $course_id ); |
| 797 |
|
| 798 |
if ( $course ) { |
| 799 |
$count = $course->count_items( $this->_post_type ); |
| 800 |
$post_object = get_post_type_object( $post_type ); |
| 801 |
$title = sprintf( _n( 'Course (%1$d %2$s)', 'Course (%1$d %2$s)', $count, 'learnpress' ), $count, $count > 1 ? $post_object->label : $post_object->labels->singular_name ); |
| 802 |
} |
| 803 |
} |
| 804 |
|
| 805 |
return $title; |
| 806 |
} |
| 807 |
|
| 808 |
/** |
| 809 |
* Get course that the items is contained. |
| 810 |
* |
| 811 |
* @param $post_id |
| 812 |
*/ |
| 813 |
protected function _get_item_course( $post_id ) { |
| 814 |
$courses = learn_press_get_item_courses( $post_id ); |
| 815 |
if ( $courses ) { |
| 816 |
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>'; |
| 818 |
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' ) ); |
| 820 |
echo ' | '; |
| 821 |
printf( '<a href="%s">%s</a>', get_the_permalink( $course->ID ), __( 'View', 'learnpress' ) ); |
| 822 |
|
| 823 |
if ( $this->_filter_items_by_course() ) { |
| 824 |
echo ' | '; |
| 825 |
printf( |
| 826 |
'<a href="%s">%s</a>', |
| 827 |
esc_url_raw( remove_query_arg( array( 'course', 'orderby' ) ) ), |
| 828 |
__( 'Remove Filter', 'learnpress' ) |
| 829 |
); |
| 830 |
} |
| 831 |
echo '</div></div>'; |
| 832 |
} |
| 833 |
} else { |
| 834 |
_e( 'Not assigned yet', 'learnpress' ); |
| 835 |
} |
| 836 |
} |
| 837 |
|
| 838 |
/** |
| 839 |
* @param string $fields |
| 840 |
* |
| 841 |
* @return mixed |
| 842 |
*/ |
| 843 |
public function posts_fields( $fields ) { |
| 844 |
return $fields; |
| 845 |
} |
| 846 |
|
| 847 |
/** |
| 848 |
* Get sortable columns for list table |
| 849 |
* |
| 850 |
* @return mixed |
| 851 |
*/ |
| 852 |
public function sortable_columns( $columns ) { |
| 853 |
return $columns; |
| 854 |
} |
| 855 |
|
| 856 |
public function columns_head( $columns ) { |
| 857 |
return $columns; |
| 858 |
} |
| 859 |
|
| 860 |
public function columns_content( $column, $post_id = 0 ) { |
| 861 |
// Implement from child |
| 862 |
} |
| 863 |
|
| 864 |
/** |
| 865 |
* Get string for searching |
| 866 |
* |
| 867 |
* @return string |
| 868 |
*/ |
| 869 |
protected function get_search(): string { |
| 870 |
return LP_Request::get( 's' ); |
| 871 |
} |
| 872 |
|
| 873 |
/** |
| 874 |
* @return string |
| 875 |
*/ |
| 876 |
protected function get_order_sort(): string { |
| 877 |
return strtolower( LP_Request::get( 'order' ) ) === 'desc' ? 'DESC' : 'ASC'; |
| 878 |
} |
| 879 |
|
| 880 |
/** |
| 881 |
* @return mixed |
| 882 |
*/ |
| 883 |
protected function get_order_by(): string { |
| 884 |
return LP_Request::get( 'orderby' ); |
| 885 |
} |
| 886 |
|
| 887 |
/** |
| 888 |
* Show actions on list post |
| 889 |
* |
| 890 |
* @param string[] $actions |
| 891 |
* @param WP_Post $post |
| 892 |
* @return array|false|mixed |
| 893 |
*/ |
| 894 |
public function _post_row_actions( $actions, $post ) { |
| 895 |
if ( ! $this->_check_post() ) { |
| 896 |
return $actions; |
| 897 |
} |
| 898 |
|
| 899 |
return $this->row_actions( $actions, $post ); |
| 900 |
} |
| 901 |
|
| 902 |
public function row_actions( $actions, $post ) { |
| 903 |
return $actions; |
| 904 |
} |
| 905 |
|
| 906 |
/** |
| 907 |
* @depecated 4.1.6.9 |
| 908 |
*/ |
| 909 |
public function updated_messages( $messages ) { |
| 910 |
$post = get_post(); |
| 911 |
$post_type = get_post_type( $post ); |
| 912 |
$post_type_object = get_post_type_object( $this->_post_type ); |
| 913 |
if ( $this->_post_type !== $post_type ) { |
| 914 |
return $messages; |
| 915 |
} |
| 916 |
$messages[ $this->_post_type ] = array( |
| 917 |
0 => '', // Unused. Messages start at index 1. |
| 918 |
1 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'updated.', 'learnpress' ) ), |
| 919 |
2 => __( 'Custom field updated.', 'learnpress' ), |
| 920 |
3 => __( 'Custom field deleted.', 'learnpress' ), |
| 921 |
4 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'updated.', 'learnpress' ) ), |
| 922 |
/* 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, |
| 924 |
6 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'published.', 'learnpress' ) ), |
| 925 |
7 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'saved.', 'learnpress' ) ), |
| 926 |
8 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'submitted.', 'learnpress' ) ), |
| 927 |
9 => sprintf( |
| 928 |
sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'scheduled for: <strong>%1$s</strong>.', 'learnpress' ) ), |
| 929 |
// translators: Publish box date format, see http://php.net/date |
| 930 |
date_i18n( __( 'M j, Y @ G:i', 'learnpress' ), strtotime( $post->post_date ) ) |
| 931 |
), |
| 932 |
10 => sprintf( '%s %s', $post_type_object->labels->singular_name, __( 'draft updated.', 'learnpress' ) ), |
| 933 |
); |
| 934 |
|
| 935 |
if ( $post_type_object->publicly_queryable ) { |
| 936 |
$permalink = get_permalink( $post->ID ); |
| 937 |
|
| 938 |
$view_link = sprintf( ' <a href="%s">%s</a>', esc_url_raw( $permalink ), sprintf( '%s %s', __( 'View', 'learnpress' ), $post_type_object->labels->singular_name ) ); |
| 939 |
switch ( $this->_post_type ) { |
| 940 |
case LP_LESSON_CPT: |
| 941 |
case LP_QUIZ_CPT: |
| 942 |
//$view_link = learn_press_get_item_course_id( $post->ID, $post->post_type ) ? $view_link : ''; |
| 943 |
$view_link = LP_Course_DB::getInstance()->get_course_by_item_id( $post->ID ) ? $view_link : ''; |
| 944 |
break; |
| 945 |
case LP_ORDER_CPT: |
| 946 |
$order = learn_press_get_order( $post->ID ); |
| 947 |
$view_link = $order->get_view_order_url(); |
| 948 |
$view_link = sprintf( ' <a href="%s">%s</a>', esc_url_raw( $view_link ), sprintf( '%s %s', __( 'View', 'learnpress' ), $post_type_object->labels->singular_name ) ); |
| 949 |
break; |
| 950 |
case LP_QUESTION_CPT: |
| 951 |
$view_link = ''; |
| 952 |
break; |
| 953 |
} |
| 954 |
$messages[ $this->_post_type ][1] .= $view_link; |
| 955 |
$messages[ $this->_post_type ][6] .= $view_link; |
| 956 |
$messages[ $this->_post_type ][9] .= $view_link; |
| 957 |
|
| 958 |
$preview_permalink = learn_press_get_preview_url( $post->ID ); |
| 959 |
|
| 960 |
$preview_link = sprintf( ' <a target="_blank" href="%s">%s</a>', esc_url_raw( $preview_permalink ), sprintf( '%s %s', __( 'Preview', 'learnpress' ), $post_type_object->labels->singular_name ) ); |
| 961 |
$messages[ $this->_post_type ][8] .= $preview_link; |
| 962 |
$messages[ $this->_post_type ][10] .= $preview_link; |
| 963 |
} |
| 964 |
|
| 965 |
return $messages; |
| 966 |
} |
| 967 |
} |
| 968 |
|