| 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( 'MywpControllerModuleAdminUploads' ) ) : |
| 12 |
|
| 13 |
final class MywpControllerModuleAdminUploads extends MywpControllerAbstractModule { |
| 14 |
|
| 15 |
static protected $id = 'admin_uploads'; |
| 16 |
|
| 17 |
public static function mywp_controller_initial_data( $initial_data ) { |
| 18 |
|
| 19 |
$initial_data['list_columns'] = array(); |
| 20 |
|
| 21 |
$initial_data['per_page_num'] = ''; |
| 22 |
$initial_data['hide_add_new'] = ''; |
| 23 |
$initial_data['hide_search_box'] = ''; |
| 24 |
|
| 25 |
return $initial_data; |
| 26 |
|
| 27 |
} |
| 28 |
|
| 29 |
public static function mywp_controller_default_data( $default_data ) { |
| 30 |
|
| 31 |
$default_data['list_columns'] = array(); |
| 32 |
|
| 33 |
$default_data['per_page_num'] = 20; |
| 34 |
$default_data['hide_add_new'] = false; |
| 35 |
$default_data['hide_search_box'] = false; |
| 36 |
|
| 37 |
return $default_data; |
| 38 |
|
| 39 |
} |
| 40 |
|
| 41 |
public static function mywp_wp_loaded() { |
| 42 |
|
| 43 |
if( ! is_admin() ) { |
| 44 |
|
| 45 |
return false; |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
if( is_network_admin() ) { |
| 50 |
|
| 51 |
return false; |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
if( ! self::is_do_controller() ) { |
| 56 |
|
| 57 |
return false; |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
add_action( 'load-upload.php' , array( __CLASS__ , 'load_uploads' ) , 1000 ); |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
public static function load_uploads() { |
| 66 |
|
| 67 |
add_action( 'admin_print_styles' , array( __CLASS__ , 'hide_add_new' ) ); |
| 68 |
|
| 69 |
add_action( 'admin_print_styles' , array( __CLASS__ , 'hide_search_box' ) ); |
| 70 |
|
| 71 |
add_action( 'admin_print_styles' , array( __CLASS__ , 'change_column_width' ) ); |
| 72 |
|
| 73 |
add_filter( 'request' , array( __CLASS__ , 'sortable_request' ) ); |
| 74 |
|
| 75 |
add_filter( 'posts_orderby', array( __CLASS__ , 'sortable_posts_orderby' ) ); |
| 76 |
|
| 77 |
add_filter( 'upload_per_page' , array( __CLASS__ , 'upload_per_page' ) ); |
| 78 |
|
| 79 |
add_filter( 'manage_media_columns' , array( __CLASS__ , 'manage_columns' ) ); |
| 80 |
|
| 81 |
add_filter( 'manage_media_custom_column' , array( __CLASS__ , 'manage_column_body' ) , 10 , 2 ); |
| 82 |
|
| 83 |
add_filter( 'manage_upload_sortable_columns', array( __CLASS__ , 'manage_columns_sortable' ) ); |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
public static function hide_add_new() { |
| 88 |
|
| 89 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 90 |
|
| 91 |
return false; |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
$setting_data = self::get_setting_data(); |
| 96 |
|
| 97 |
if( empty( $setting_data['hide_add_new'] ) ) { |
| 98 |
|
| 99 |
return false; |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
echo '<style>'; |
| 104 |
|
| 105 |
echo 'body.wp-admin .wrap h1 a { display: none; }'; |
| 106 |
echo 'body.wp-admin .wrap .page-title-action { display: none; }'; |
| 107 |
|
| 108 |
echo '</style>'; |
| 109 |
|
| 110 |
self::after_do_function( __FUNCTION__ ); |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
public static function hide_search_box() { |
| 115 |
|
| 116 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 117 |
|
| 118 |
return false; |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
$setting_data = self::get_setting_data(); |
| 123 |
|
| 124 |
if( empty( $setting_data['hide_search_box'] ) ) { |
| 125 |
|
| 126 |
return false; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
echo '<style>'; |
| 131 |
|
| 132 |
echo 'body.wp-admin #media-search-input { display: none; }'; |
| 133 |
|
| 134 |
echo '</style>'; |
| 135 |
|
| 136 |
self::after_do_function( __FUNCTION__ ); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
public static function change_column_width() { |
| 141 |
|
| 142 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 143 |
|
| 144 |
return false; |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
$setting_data = self::get_setting_data(); |
| 149 |
|
| 150 |
if( empty( $setting_data['list_columns'] ) ) { |
| 151 |
|
| 152 |
return false; |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
$columns = array(); |
| 157 |
|
| 158 |
foreach( $setting_data['list_columns'] as $column_id => $column_setting ) { |
| 159 |
|
| 160 |
if( empty( $column_setting['width'] ) ) { |
| 161 |
|
| 162 |
continue; |
| 163 |
|
| 164 |
} |
| 165 |
|
| 166 |
$columns[ $column_id ] = $column_setting['width']; |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
if( empty( $columns ) ) { |
| 171 |
|
| 172 |
return false; |
| 173 |
|
| 174 |
} |
| 175 |
|
| 176 |
echo '<style>'; |
| 177 |
|
| 178 |
foreach( $columns as $column_id => $width ) { |
| 179 |
|
| 180 |
echo 'body.wp-admin .wp-list-table.widefat thead th.column-' . esc_attr( $column_id ) . ' { width: ' . esc_attr( $width ) . '; display: table-cell; }'; |
| 181 |
echo 'body.wp-admin .wp-list-table.widefat thead td.column-' . esc_attr( $column_id ) . ' { width: ' . esc_attr( $width ) . '; display: table-cell; }'; |
| 182 |
|
| 183 |
echo 'body.wp-admin .wp-list-table.widefat thead th#' . esc_attr( $column_id ) . ' { width: ' . esc_attr( $width ) . '; display: table-cell; }'; |
| 184 |
echo 'body.wp-admin .wp-list-table.widefat thead td#' . esc_attr( $column_id ) . ' { width: ' . esc_attr( $width ) . '; display: table-cell; }'; |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
echo '</style>'; |
| 189 |
|
| 190 |
self::after_do_function( __FUNCTION__ ); |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
public static function sortable_request( $request ) { |
| 195 |
|
| 196 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 197 |
|
| 198 |
return $request; |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
if( empty( $request['orderby'] ) ) { |
| 203 |
|
| 204 |
return $request; |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
if( $request['orderby'] === 'image_alt') { |
| 209 |
|
| 210 |
$request['meta_key'] = '_wp_attachment_image_alt'; |
| 211 |
$request['orderby'] = 'meta_value'; |
| 212 |
|
| 213 |
} |
| 214 |
|
| 215 |
self::after_do_function( __FUNCTION__ ); |
| 216 |
|
| 217 |
return $request; |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
public static function sortable_posts_orderby( $orderby_statement ) { |
| 222 |
|
| 223 |
global $wpdb; |
| 224 |
global $wp_query; |
| 225 |
|
| 226 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 227 |
|
| 228 |
return $orderby_statement; |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
if( strpos( $orderby_statement , 'post_date' ) !== false ) { |
| 233 |
|
| 234 |
$orderby = $wp_query->get( 'orderby' ); |
| 235 |
|
| 236 |
if( empty( $orderby ) ) { |
| 237 |
|
| 238 |
return $orderby_statement; |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
if( $orderby === 'post_excerpt' ) { |
| 243 |
|
| 244 |
$order = $wp_query->get( 'order' ); |
| 245 |
|
| 246 |
$orderby_statement = sprintf( '%1$s.%2$s %3$s' , $wpdb->posts , $orderby , $order ); |
| 247 |
|
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
self::after_do_function( __FUNCTION__ ); |
| 253 |
|
| 254 |
return $orderby_statement; |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
public static function upload_per_page( $per_page ) { |
| 259 |
|
| 260 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 261 |
|
| 262 |
return $per_page; |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
$setting_data = self::get_setting_data(); |
| 267 |
|
| 268 |
if( empty( $setting_data['per_page_num'] ) ) { |
| 269 |
|
| 270 |
return $per_page; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
$per_page = $setting_data['per_page_num']; |
| 275 |
|
| 276 |
self::after_do_function( __FUNCTION__ ); |
| 277 |
|
| 278 |
return $per_page; |
| 279 |
|
| 280 |
} |
| 281 |
|
| 282 |
public static function manage_columns( $columns ) { |
| 283 |
|
| 284 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 285 |
|
| 286 |
return $columns; |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
$setting_data = self::get_setting_data(); |
| 291 |
|
| 292 |
if( empty( $setting_data['list_columns'] ) ) { |
| 293 |
|
| 294 |
return $columns; |
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
$columns = array(); |
| 299 |
|
| 300 |
foreach( $setting_data['list_columns'] as $column_id => $column_setting ) { |
| 301 |
|
| 302 |
$columns[ $column_id ] = $column_setting['title']; |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
self::after_do_function( __FUNCTION__ ); |
| 307 |
|
| 308 |
return $columns; |
| 309 |
|
| 310 |
} |
| 311 |
|
| 312 |
public static function manage_column_body( $column_id , $post_id ) { |
| 313 |
|
| 314 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 315 |
|
| 316 |
return false; |
| 317 |
|
| 318 |
} |
| 319 |
|
| 320 |
$post = get_post( $post_id ); |
| 321 |
|
| 322 |
if( $column_id === 'id' ) { |
| 323 |
|
| 324 |
echo $post_id; |
| 325 |
|
| 326 |
} elseif( $column_id === 'media_title' ) { |
| 327 |
|
| 328 |
echo _draft_or_post_title( $post_id ); |
| 329 |
|
| 330 |
} elseif( $column_id === 'image_alt' ) { |
| 331 |
|
| 332 |
$image_alt = get_post_meta( $post_id , '_wp_attachment_image_alt' , true ); |
| 333 |
|
| 334 |
echo wp_strip_all_tags( stripslashes( $image_alt ) ); |
| 335 |
|
| 336 |
} elseif( $column_id === 'post_excerpt' ) { |
| 337 |
|
| 338 |
if( ! empty( $post->post_excerpt ) ) { |
| 339 |
|
| 340 |
if( function_exists( 'mb_substr' ) ) { |
| 341 |
|
| 342 |
echo mb_substr( strip_tags( $post->post_excerpt ) , 0 , 20 ) . '.'; |
| 343 |
|
| 344 |
} else { |
| 345 |
|
| 346 |
echo substr( strip_tags( $post->post_excerpt ) , 0 , 20 ) . '.'; |
| 347 |
|
| 348 |
} |
| 349 |
|
| 350 |
} |
| 351 |
|
| 352 |
} elseif( $column_id === 'post_content' ) { |
| 353 |
|
| 354 |
if( ! empty( $post->post_content ) ) { |
| 355 |
|
| 356 |
if( function_exists( 'mb_substr' ) ) { |
| 357 |
|
| 358 |
echo mb_substr( strip_tags( $post->post_content ) , 0 , 20 ) . '.'; |
| 359 |
|
| 360 |
} else { |
| 361 |
|
| 362 |
echo substr( strip_tags( $post->post_content ) , 0 , 20 ) . '.'; |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
} |
| 367 |
|
| 368 |
} elseif( $column_id === 'file_url' ) { |
| 369 |
|
| 370 |
printf( '<input type="text" readonly="readonly" value="%s" class="large-text" />' , esc_url( wp_get_attachment_url( $post_id ) ) ); |
| 371 |
|
| 372 |
} |
| 373 |
|
| 374 |
self::after_do_function( __FUNCTION__ ); |
| 375 |
|
| 376 |
} |
| 377 |
|
| 378 |
public static function manage_columns_sortable( $sortables ) { |
| 379 |
|
| 380 |
if( ! self::is_do_function( __FUNCTION__ ) ) { |
| 381 |
|
| 382 |
return $sortables; |
| 383 |
|
| 384 |
} |
| 385 |
|
| 386 |
$setting_data = self::get_setting_data(); |
| 387 |
|
| 388 |
if( empty( $setting_data['list_columns'] ) ) { |
| 389 |
|
| 390 |
return $sortables; |
| 391 |
|
| 392 |
} |
| 393 |
|
| 394 |
$sortables = array(); |
| 395 |
|
| 396 |
foreach( $setting_data['list_columns'] as $column_id => $column_setting ) { |
| 397 |
|
| 398 |
if( ! empty( $column_setting['sort'] ) ) { |
| 399 |
|
| 400 |
$sortables[ $column_id ] = $column_setting['orderby']; |
| 401 |
|
| 402 |
} |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
self::after_do_function( __FUNCTION__ ); |
| 407 |
|
| 408 |
return $sortables; |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
MywpControllerModuleAdminUploads::init(); |
| 415 |
|
| 416 |
endif; |
| 417 |
|