| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
if( ! class_exists( 'MywpControllerAbstractModule' ) ) { |
| 8 |
return false; |
| 9 |
} |
| 10 |
|
| 11 |
if ( ! class_exists( 'MywpControllerModuleAdminPosts' ) ) : |
| 12 |
|
| 13 |
final class MywpControllerModuleAdminPosts extends MywpControllerAbstractModule { |
| 14 |
|
| 15 |
static protected $id = 'admin_posts'; |
| 16 |
|
| 17 |
static private $post_type = ''; |
| 18 |
|
| 19 |
public static function mywp_controller_initial_data( $initial_data ) { |
| 20 |
|
| 21 |
$initial_data['list_columns'] = array(); |
| 22 |
|
| 23 |
$initial_data['bulk_post_updated_messages'] = array(); |
| 24 |
|
| 25 |
$initial_data['per_page_num'] = ''; |
| 26 |
$initial_data['hide_add_new'] = ''; |
| 27 |
$initial_data['hide_search_box'] = ''; |
| 28 |
$initial_data['auto_output_column_body'] = ''; |
| 29 |
|
| 30 |
return $initial_data; |
| 31 |
|
| 32 |
} |
| 33 |
|
| 34 |
public static function mywp_controller_default_data( $default_data ) { |
| 35 |
|
| 36 |
$default_data['list_columns'] = array(); |
| 37 |
|
| 38 |
$default_data['bulk_post_updated_messages'] = array(); |
| 39 |
|
| 40 |
$default_data['per_page_num'] = ''; |
| 41 |
$default_data['hide_add_new'] = false; |
| 42 |
$default_data['hide_search_box'] = false; |
| 43 |
$default_data['auto_output_column_body'] = false; |
| 44 |
|
| 45 |
return $default_data; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
public static function get_bulk_update_messages_default() { |
| 50 |
|
| 51 |
$bulk_update_messages_default = array( |
| 52 |
'updated' => _n( '%s post updated.', '%s posts updated.', 0 ), |
| 53 |
'locked' => _n( '%s post not updated, somebody is editing it.', '%s posts not updated, somebody is editing them.', 0 ), |
| 54 |
'deleted' => _n( '%s post permanently deleted.', '%s posts permanently deleted.', 0 ), |
| 55 |
'trashed' => _n( '%s post moved to the Trash.', '%s posts moved to the Trash.', 0 ), |
| 56 |
'untrashed' => _n( '%s post restored from the Trash.', '%s posts restored from the Trash.', 0 ), |
| 57 |
); |
| 58 |
|
| 59 |
return $bulk_update_messages_default; |
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
public static function mywp_wp_loaded() { |
| 64 |
|
| 65 |
if( ! is_admin() ) { |
| 66 |
|
| 67 |
return false; |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
if( is_network_admin() ) { |
| 72 |
|
| 73 |
return false; |
| 74 |
|
| 75 |
} |
| 76 |
|
| 77 |
if( ! self::is_do_controller() ) { |
| 78 |
|
| 79 |
return false; |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
add_action( 'mywp_ajax' , array( __CLASS__ , 'mywp_ajax' ) , 1000 ); |
| 84 |
|
| 85 |
add_action( 'load-edit.php' , array( __CLASS__ , 'load_edit' ) , 1000 ); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
public static function mywp_model_get_option_key( $option_key ) { |
| 90 |
|
| 91 |
if( empty( self::$post_type ) ) { |
| 92 |
|
| 93 |
return $option_key; |
| 94 |
|
| 95 |
} |
| 96 |
|
| 97 |
$option_key .= '_' . self::$post_type; |
| 98 |
|
| 99 |
return $option_key; |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
public static function mywp_ajax() { |
| 104 |
|
| 105 |
if( empty( $_POST['action'] ) or $_POST['action'] !== 'inline-save' ) { |
| 106 |
|
| 107 |
return false; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
if( empty( $_POST['screen'] ) ) { |
| 112 |
|
| 113 |
return false; |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
if( empty( $_POST['post_type'] ) ) { |
| 118 |
|
| 119 |
return false; |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
self::$post_type = strip_tags( $_POST['post_type'] ); |
| 124 |
|
| 125 |
add_filter( 'mywp_model_get_option_key_mywp_' . self::$id , array( __CLASS__ , 'mywp_model_get_option_key' ) ); |
| 126 |
|
| 127 |
add_filter( 'manage_edit-' . self::$post_type . '_columns' , array( __CLASS__ , 'manage_columns' ) ); |
| 128 |
|
| 129 |
add_action( 'manage_' . self::$post_type . '_posts_custom_column' , array( __CLASS__ , 'manage_column_body' ) , 10 , 2 ); |
| 130 |
|
| 131 |
add_filter( 'manage_edit-' . self::$post_type . '_sortable_columns', array( __CLASS__ , 'manage_columns_sortable' ) ); |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
public static function load_edit() { |
| 136 |
|
| 137 |
global $typenow; |
| 138 |
|
| 139 |
if( empty( $typenow ) ) { |
| 140 |
|
| 141 |
return false; |
| 142 |
|
| 143 |
} |
| 144 |
|
| 145 |
self::$post_type = $typenow; |
| 146 |
|
| 147 |
add_filter( 'mywp_model_get_option_key_mywp_' . self::$id , array( __CLASS__ , 'mywp_model_get_option_key' ) ); |
| 148 |
|
| 149 |
add_filter( 'bulk_post_updated_messages' , array( __CLASS__ , 'change_bulk_post_updated_messages' ) ); |
| 150 |
|
| 151 |
add_action( 'admin_print_styles' , array( __CLASS__ , 'hide_add_new' ) ); |
| 152 |
|
| 153 |
add_action( 'admin_print_styles' , array( __CLASS__ , 'hide_search_box' ) ); |
| 154 |
|
| 155 |
add_action( 'admin_print_styles' , array( __CLASS__ , 'change_column_width' ) ); |
| 156 |
|
| 157 |
add_filter( 'request' , array( __CLASS__ , 'sortable_request' ) ); |
| 158 |
|
| 159 |
add_filter( 'posts_orderby', array( __CLASS__ , 'sortable_posts_orderby' ) ); |
| 160 |
|
| 161 |
add_filter( "edit_{$typenow}_per_page" , array( __CLASS__ , 'edit_per_page' ) ); |
| 162 |
|
| 163 |
add_filter( "manage_edit-{$typenow}_columns" , array( __CLASS__ , 'manage_columns' ) ); |
| 164 |
|
| 165 |
add_action( "manage_{$typenow}_posts_custom_column" , array( __CLASS__ , 'manage_column_body' ) , 10 , 2 ); |
| 166 |
|
| 167 |
add_filter( "manage_edit-{$typenow}_sortable_columns", array( __CLASS__ , 'manage_columns_sortable' ) ); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
public static function change_bulk_post_updated_messages( $bulk_post_updated_messages ) { |
| 172 |
|
| 173 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 174 |
|
| 175 |
return $bulk_post_updated_messages; |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
$setting_data = self::get_setting_data(); |
| 180 |
|
| 181 |
if( empty( $setting_data['bulk_post_updated_messages'] ) ) { |
| 182 |
|
| 183 |
return $bulk_post_updated_messages; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
$bulk_post_updated_messages_default = self::get_bulk_update_messages_default(); |
| 188 |
|
| 189 |
foreach( $bulk_post_updated_messages_default as $key => $v ) { |
| 190 |
|
| 191 |
if( ! empty( $setting_data['bulk_post_updated_messages'][ $key ] ) ) { |
| 192 |
|
| 193 |
$bulk_post_updated_messages[ self::$post_type ][ $key ] = $setting_data['bulk_post_updated_messages'][ $key ]; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
self::after_do_function( __FUNCTION__ ); |
| 200 |
|
| 201 |
return $bulk_post_updated_messages; |
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
public static function hide_add_new() { |
| 206 |
|
| 207 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 208 |
|
| 209 |
return false; |
| 210 |
|
| 211 |
} |
| 212 |
|
| 213 |
$setting_data = self::get_setting_data(); |
| 214 |
|
| 215 |
if( empty( $setting_data['hide_add_new'] ) ) { |
| 216 |
|
| 217 |
return false; |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
echo '<style>'; |
| 222 |
|
| 223 |
echo 'body.wp-admin .wrap h1 a { display: none; }'; |
| 224 |
echo 'body.wp-admin .wrap .page-title-action { display: none; }'; |
| 225 |
|
| 226 |
echo '</style>'; |
| 227 |
|
| 228 |
self::after_do_function( __FUNCTION__ ); |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
public static function hide_search_box() { |
| 233 |
|
| 234 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 235 |
|
| 236 |
return false; |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
$setting_data = self::get_setting_data(); |
| 241 |
|
| 242 |
if( empty( $setting_data['hide_search_box'] ) ) { |
| 243 |
|
| 244 |
return false; |
| 245 |
|
| 246 |
} |
| 247 |
|
| 248 |
echo '<style>'; |
| 249 |
|
| 250 |
echo 'body.wp-admin #posts-filter .search-box { display: none; }'; |
| 251 |
|
| 252 |
echo '</style>'; |
| 253 |
|
| 254 |
self::after_do_function( __FUNCTION__ ); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
public static function change_column_width() { |
| 259 |
|
| 260 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 261 |
|
| 262 |
return false; |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
$setting_data = self::get_setting_data(); |
| 267 |
|
| 268 |
if( empty( $setting_data['list_columns'] ) ) { |
| 269 |
|
| 270 |
return false; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
$columns = array(); |
| 275 |
|
| 276 |
foreach( $setting_data['list_columns'] as $column_id => $column_setting ) { |
| 277 |
|
| 278 |
if( empty( $column_setting['width'] ) ) { |
| 279 |
|
| 280 |
continue; |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
$columns[ $column_id ] = $column_setting['width']; |
| 285 |
|
| 286 |
} |
| 287 |
|
| 288 |
if( empty( $columns ) ) { |
| 289 |
|
| 290 |
return false; |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
echo '<style>'; |
| 295 |
|
| 296 |
foreach( $columns as $column_id => $width ) { |
| 297 |
|
| 298 |
echo 'body.wp-admin .wp-list-table.widefat thead th.column-' . esc_attr( $column_id ) . ' { width: ' . esc_attr( $width ) . '; display: table-cell; }'; |
| 299 |
echo 'body.wp-admin .wp-list-table.widefat thead td.column-' . esc_attr( $column_id ) . ' { width: ' . esc_attr( $width ) . '; display: table-cell; }'; |
| 300 |
|
| 301 |
echo 'body.wp-admin .wp-list-table.widefat thead th#' . esc_attr( $column_id ) . ' { width: ' . esc_attr( $width ) . '; display: table-cell; }'; |
| 302 |
echo 'body.wp-admin .wp-list-table.widefat thead td#' . esc_attr( $column_id ) . ' { width: ' . esc_attr( $width ) . '; display: table-cell; }'; |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
echo '</style>'; |
| 307 |
|
| 308 |
self::after_do_function( __FUNCTION__ ); |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
public static function sortable_request( $request ) { |
| 313 |
|
| 314 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 315 |
|
| 316 |
return $request; |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
if( empty( $request['orderby'] ) ) { |
| 321 |
|
| 322 |
return $request; |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
if( $request['orderby'] === 'post-thumbnails') { |
| 327 |
|
| 328 |
$request['meta_key'] = '_thumbnail_id'; |
| 329 |
$request['orderby'] = 'meta_value'; |
| 330 |
|
| 331 |
} elseif( ! empty( $request['post_type'] ) ) { |
| 332 |
|
| 333 |
$posts_all_custom_fields = MywpPostType::get_post_type_posts_all_custom_fields( $request['post_type'] ); |
| 334 |
|
| 335 |
if( isset( $posts_all_custom_fields[ $request['orderby'] ] ) ) { |
| 336 |
|
| 337 |
$request['meta_key'] = $request['orderby']; |
| 338 |
$request['orderby'] = 'meta_value'; |
| 339 |
|
| 340 |
} |
| 341 |
|
| 342 |
} |
| 343 |
|
| 344 |
self::after_do_function( __FUNCTION__ ); |
| 345 |
|
| 346 |
return $request; |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
public static function sortable_posts_orderby( $orderby_statement ) { |
| 351 |
|
| 352 |
global $wpdb; |
| 353 |
global $wp_query; |
| 354 |
|
| 355 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 356 |
|
| 357 |
return $orderby_statement; |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
if( strpos( $orderby_statement , 'post_date' ) !== false ) { |
| 362 |
|
| 363 |
$orderby = $wp_query->get( 'orderby' ); |
| 364 |
|
| 365 |
if( empty( $orderby ) ) { |
| 366 |
|
| 367 |
return $orderby_statement; |
| 368 |
|
| 369 |
} |
| 370 |
|
| 371 |
if( $orderby === 'post_excerpt' ) { |
| 372 |
|
| 373 |
$order = $wp_query->get( 'order' ); |
| 374 |
|
| 375 |
$orderby_statement = sprintf( '%1$s.%2$s %3$s' , $wpdb->posts , $orderby , $order ); |
| 376 |
|
| 377 |
} |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
self::after_do_function( __FUNCTION__ ); |
| 382 |
|
| 383 |
return $orderby_statement; |
| 384 |
|
| 385 |
} |
| 386 |
|
| 387 |
public static function edit_per_page( $per_page ) { |
| 388 |
|
| 389 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 390 |
|
| 391 |
return $per_page; |
| 392 |
|
| 393 |
} |
| 394 |
|
| 395 |
$setting_data = self::get_setting_data(); |
| 396 |
|
| 397 |
if( empty( $setting_data['per_page_num'] ) ) { |
| 398 |
|
| 399 |
return $per_page; |
| 400 |
|
| 401 |
} |
| 402 |
|
| 403 |
$per_page = $setting_data['per_page_num']; |
| 404 |
|
| 405 |
self::after_do_function( __FUNCTION__ ); |
| 406 |
|
| 407 |
return $per_page; |
| 408 |
|
| 409 |
} |
| 410 |
|
| 411 |
public static function manage_columns( $columns ) { |
| 412 |
|
| 413 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 414 |
|
| 415 |
return $columns; |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
$setting_data = self::get_setting_data(); |
| 420 |
|
| 421 |
if( empty( $setting_data['list_columns'] ) ) { |
| 422 |
|
| 423 |
return $columns; |
| 424 |
|
| 425 |
} |
| 426 |
|
| 427 |
$columns = array(); |
| 428 |
|
| 429 |
foreach( $setting_data['list_columns'] as $column_id => $column_setting ) { |
| 430 |
|
| 431 |
$columns[ $column_id ] = do_shortcode( $column_setting['title'] ); |
| 432 |
|
| 433 |
} |
| 434 |
|
| 435 |
self::after_do_function( __FUNCTION__ ); |
| 436 |
|
| 437 |
return $columns; |
| 438 |
|
| 439 |
} |
| 440 |
|
| 441 |
public static function manage_column_body( $column_id , $post_id ) { |
| 442 |
|
| 443 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 444 |
|
| 445 |
return false; |
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
$setting_data = self::get_setting_data(); |
| 450 |
|
| 451 |
if( ! $setting_data['auto_output_column_body'] ) { |
| 452 |
|
| 453 |
return false; |
| 454 |
|
| 455 |
} |
| 456 |
|
| 457 |
$post = get_post( $post_id ); |
| 458 |
|
| 459 |
if( $column_id === 'id' ) { |
| 460 |
|
| 461 |
echo $post_id; |
| 462 |
|
| 463 |
} elseif( $column_id === 'slug' ) { |
| 464 |
|
| 465 |
echo sanitize_title( $post->post_name ); |
| 466 |
|
| 467 |
} elseif( $column_id === 'parent' ) { |
| 468 |
|
| 469 |
echo $post->post_parent; |
| 470 |
|
| 471 |
} elseif( $column_id === 'post-formats' ) { |
| 472 |
|
| 473 |
echo get_post_format_string( get_post_format( $post_id ) ); |
| 474 |
|
| 475 |
} elseif( $column_id === 'excerpt' ) { |
| 476 |
|
| 477 |
if( ! empty( $post->post_excerpt ) ) { |
| 478 |
|
| 479 |
if( function_exists( 'mb_substr' ) ) { |
| 480 |
|
| 481 |
echo mb_substr( strip_tags( $post->post_excerpt ) , 0 , 20 ) . '.'; |
| 482 |
|
| 483 |
} else { |
| 484 |
|
| 485 |
echo substr( strip_tags( $post->post_excerpt ) , 0 , 20 ) . '.'; |
| 486 |
|
| 487 |
} |
| 488 |
|
| 489 |
} |
| 490 |
|
| 491 |
} elseif( $column_id === 'menu_order' ) { |
| 492 |
|
| 493 |
echo $post->menu_order; |
| 494 |
|
| 495 |
} elseif( $column_id === 'post-thumbnails' ) { |
| 496 |
|
| 497 |
if( has_post_thumbnail( $post_id ) ) { |
| 498 |
|
| 499 |
$thumbnail_id = get_post_thumbnail_id( $post_id ); |
| 500 |
|
| 501 |
$thumbnail = wp_get_attachment_image_src( $thumbnail_id , 'post-thumbnail' , true ); |
| 502 |
|
| 503 |
printf( '<img src="%s" style="%s" /></a>' , esc_attr( $thumbnail[0] ) , esc_attr( 'max-width:100%;' ) ); |
| 504 |
|
| 505 |
} |
| 506 |
|
| 507 |
} else { |
| 508 |
|
| 509 |
$post_type_taxonomies = MywpTaxonomy::get_taxonomies( array( 'object_type' => array( self::$post_type ) ) ); |
| 510 |
|
| 511 |
if( ! empty( $post_type_taxonomies[ $column_id ] ) ) { |
| 512 |
|
| 513 |
$post_terms = wp_get_post_terms( $post_id , $column_id , array( 'fields' => 'all' ) ); |
| 514 |
|
| 515 |
if( ! empty( $post_terms ) ) { |
| 516 |
|
| 517 |
foreach( $post_terms as $post_term ) { |
| 518 |
|
| 519 |
printf( '<span class="post-term post-term-%d">[%s]</span> ' , esc_attr( $post_term->term_id ) , $post_term->name ); |
| 520 |
|
| 521 |
} |
| 522 |
|
| 523 |
} |
| 524 |
|
| 525 |
} else { |
| 526 |
|
| 527 |
$post_meta = MywpPostType::get_post_meta( $post_id , $column_id ); |
| 528 |
|
| 529 |
if( ! empty( $post_meta ) ) { |
| 530 |
|
| 531 |
echo $post_meta; |
| 532 |
|
| 533 |
} |
| 534 |
|
| 535 |
} |
| 536 |
|
| 537 |
} |
| 538 |
|
| 539 |
self::after_do_function( __FUNCTION__ ); |
| 540 |
|
| 541 |
} |
| 542 |
|
| 543 |
public static function manage_columns_sortable( $sortables ) { |
| 544 |
|
| 545 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 546 |
|
| 547 |
return $sortables; |
| 548 |
|
| 549 |
} |
| 550 |
|
| 551 |
$setting_data = self::get_setting_data(); |
| 552 |
|
| 553 |
if( empty( $setting_data['list_columns'] ) ) { |
| 554 |
|
| 555 |
return $sortables; |
| 556 |
|
| 557 |
} |
| 558 |
|
| 559 |
$sortables = array(); |
| 560 |
|
| 561 |
foreach( $setting_data['list_columns'] as $column_id => $column_setting ) { |
| 562 |
|
| 563 |
if( ! empty( $column_setting['sort'] ) ) { |
| 564 |
|
| 565 |
$sortables[ $column_id ] = $column_setting['orderby']; |
| 566 |
|
| 567 |
} |
| 568 |
|
| 569 |
} |
| 570 |
|
| 571 |
self::after_do_function( __FUNCTION__ ); |
| 572 |
|
| 573 |
return $sortables; |
| 574 |
|
| 575 |
} |
| 576 |
|
| 577 |
} |
| 578 |
|
| 579 |
MywpControllerModuleAdminPosts::init(); |
| 580 |
|
| 581 |
endif; |
| 582 |
|