class-activation.php
3 years ago
class-admin-interface.php
3 years ago
class-content-management.php
3 years ago
class-deactivation.php
3 years ago
class-disable-components.php
3 years ago
class-security.php
3 years ago
class-settings-fields-render.php
3 years ago
class-settings-sanitization.php
3 years ago
class-settings-sections-fields.php
3 years ago
class-utilities.php
3 years ago
class-content-management.php
715 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ASENHA\Classes; |
| 4 | |
| 5 | /** |
| 6 | * Class related to Content Management features |
| 7 | * |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | class Content_Management { |
| 11 | |
| 12 | /** |
| 13 | * Current post type. For Content Admin >> Show Custom Taxonomy Filters functionality. |
| 14 | */ |
| 15 | public $post_type; |
| 16 | |
| 17 | /** |
| 18 | * Show featured images column in list tables for pages and post types that support featured image |
| 19 | * |
| 20 | * @since 1.0.0 |
| 21 | */ |
| 22 | public function show_featured_image_column() { |
| 23 | |
| 24 | $post_types = get_post_types( array( 'public' => true ), 'names' ); |
| 25 | |
| 26 | foreach ( $post_types as $post_type_key => $post_type_name ) { |
| 27 | |
| 28 | if ( post_type_supports( $post_type_key, 'thumbnail' ) ) { |
| 29 | |
| 30 | add_filter( "manage_{$post_type_name}_posts_columns",[ $this, 'add_featured_image_column' ] ); |
| 31 | add_action( "manage_{$post_type_name}_posts_custom_column", [ $this, 'add_featured_image' ], 10, 2 ); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | } |
| 36 | |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Add a column called Featured Image as the first column |
| 41 | * |
| 42 | * @param mixed $columns |
| 43 | * @return void |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | public function add_featured_image_column( $columns ) { |
| 47 | |
| 48 | $new_columns = array(); |
| 49 | |
| 50 | foreach ( $columns as $key => $value ) { |
| 51 | |
| 52 | if ( $key == 'title' ) { |
| 53 | |
| 54 | $new_columns['asenha-featured-image'] = 'Featured Image'; |
| 55 | |
| 56 | } |
| 57 | |
| 58 | $new_columns[$key] = $value; |
| 59 | |
| 60 | } |
| 61 | |
| 62 | return $new_columns; |
| 63 | |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Echo featured image's in thumbnail size to a column |
| 68 | * |
| 69 | * @param mixed $column_name |
| 70 | * @param mixed $id |
| 71 | * @since 1.0.0 |
| 72 | */ |
| 73 | public function add_featured_image( $column_name, $id ) { |
| 74 | |
| 75 | if ( 'asenha-featured-image' === $column_name ) { |
| 76 | |
| 77 | if ( has_post_thumbnail( $id ) ) { |
| 78 | |
| 79 | $size = 'thumbnail'; |
| 80 | |
| 81 | echo get_the_post_thumbnail( $id, $size, '' ); |
| 82 | |
| 83 | } else { |
| 84 | |
| 85 | echo '<img src="' . esc_url( plugins_url( 'assets/img/default_featured_image.jpg', __DIR__ ) ) . '" />'; |
| 86 | |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Show excerpt column in list tables for pages and post types that support excerpt. |
| 94 | * |
| 95 | * @since 1.0.0 |
| 96 | */ |
| 97 | public function show_excerpt_column() { |
| 98 | |
| 99 | $post_types = get_post_types( array( 'public' => true ), 'names' ); |
| 100 | |
| 101 | foreach ( $post_types as $post_type_key => $post_type_name ) { |
| 102 | |
| 103 | if ( post_type_supports( $post_type_key, 'excerpt' ) ) { |
| 104 | |
| 105 | add_filter( "manage_{$post_type_name}_posts_columns",[ $this, 'add_excerpt_column' ] ); |
| 106 | add_action( "manage_{$post_type_name}_posts_custom_column", [ $this, 'add_excerpt' ], 10, 2 ); |
| 107 | |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Add a column called Featured Image as the first column |
| 116 | * |
| 117 | * @param mixed $columns |
| 118 | * @return void |
| 119 | * @since 1.0.0 |
| 120 | */ |
| 121 | public function add_excerpt_column( $columns ) { |
| 122 | |
| 123 | $new_columns = array(); |
| 124 | |
| 125 | foreach ( $columns as $key => $value ) { |
| 126 | |
| 127 | $new_columns[$key] = $value; |
| 128 | |
| 129 | if ( $key == 'title' ) { |
| 130 | |
| 131 | $new_columns['asenha-excerpt'] = 'Excerpt'; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | } |
| 136 | |
| 137 | return $new_columns; |
| 138 | |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Echo featured image's in thumbnail size to a column |
| 143 | * |
| 144 | * @param mixed $column_name |
| 145 | * @param mixed $id |
| 146 | * @since 1.0.0 |
| 147 | */ |
| 148 | public function add_excerpt( $column_name, $id ) { |
| 149 | |
| 150 | if ( 'asenha-excerpt' === $column_name ) { |
| 151 | |
| 152 | $excerpt = get_the_excerpt( $id ); // about 310 characters |
| 153 | $excerpt = substr( $excerpt, 0, 160 ); // truncate to 160 characters |
| 154 | $short_excerpt = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) ); |
| 155 | |
| 156 | echo wp_kses_post( $short_excerpt ); |
| 157 | |
| 158 | } |
| 159 | |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Add ID column list table of pages, posts, custom post types, media, taxonomies, custom taxonomies, users amd comments |
| 164 | * |
| 165 | * @since 1.0.0 |
| 166 | */ |
| 167 | public function show_id_column() { |
| 168 | |
| 169 | // For pages and hierarchical post types list table |
| 170 | |
| 171 | add_filter( 'manage_pages_columns', [ $this, 'add_id_column' ] ); |
| 172 | add_action( 'manage_pages_custom_column', [ $this, 'add_id_echo_value' ], 10, 2 ); |
| 173 | |
| 174 | // For posts and non-hierarchical custom posts list table |
| 175 | |
| 176 | add_filter( 'manage_posts_columns', [ $this, 'add_id_column' ] ); |
| 177 | add_action( 'manage_posts_custom_column', [ $this, 'add_id_echo_value' ], 10, 2 ); |
| 178 | |
| 179 | // For media list table |
| 180 | |
| 181 | add_filter( 'manage_media_columns', [ $this, 'add_id_column' ] ); |
| 182 | add_action( 'manage_media_custom_column', [ $this, 'add_id_echo_value' ], 10, 2 ); |
| 183 | |
| 184 | // For list table of all taxonomies |
| 185 | |
| 186 | $taxonomies = get_taxonomies( [ 'public' => true ], 'names' ); |
| 187 | |
| 188 | foreach ( $taxonomies as $taxonomy ) { |
| 189 | |
| 190 | add_filter( 'manage_edit-' . $taxonomy . '_columns', [ $this, 'add_id_column' ] ); |
| 191 | add_action( 'manage_' . $taxonomy . '_custom_column', [ $this, 'add_id_return_value' ], 10, 3 ); |
| 192 | |
| 193 | } |
| 194 | |
| 195 | // For users list table |
| 196 | |
| 197 | add_filter( 'manage_users_columns', [ $this, 'add_id_column' ]); |
| 198 | add_action( 'manage_users_custom_column', [ $this, 'add_id_return_value' ], 10, 3 ); |
| 199 | |
| 200 | // For comments list table |
| 201 | |
| 202 | add_filter( 'manage_edit-comments_columns', [ $this, 'add_id_column' ]); |
| 203 | add_action( 'manage_comments_custom_column', [ $this, 'add_id_echo_value' ], 10, 3 ); |
| 204 | |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Add a column called ID |
| 209 | * |
| 210 | * @param mixed $columns |
| 211 | * @return void |
| 212 | * @since 1.0.0 |
| 213 | */ |
| 214 | public function add_id_column( $columns ) { |
| 215 | |
| 216 | $columns['asenha-id'] = 'ID'; |
| 217 | |
| 218 | return $columns; |
| 219 | |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Echo post ID value to a column |
| 224 | * |
| 225 | * @param mixed $column_name |
| 226 | * @param mixed $id |
| 227 | * @since 1.0.0 |
| 228 | */ |
| 229 | public function add_id_echo_value( $column_name, $id ) { |
| 230 | |
| 231 | if ( 'asenha-id' === $column_name ) { |
| 232 | echo esc_html( $id ); |
| 233 | } |
| 234 | |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Return post ID value to a column |
| 239 | * |
| 240 | * @param mixed $value |
| 241 | * @param mixed $column_name |
| 242 | * @param mixed $id |
| 243 | * @since 1.0.0 |
| 244 | */ |
| 245 | public function add_id_return_value( $value, $column_name, $id ) { |
| 246 | |
| 247 | if ( 'asenha-id' === $column_name ) { |
| 248 | $value = $id; |
| 249 | } |
| 250 | |
| 251 | return $value; |
| 252 | |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Hide comments column in list tables for pages, post types that support comments, and alse media/attachments. |
| 257 | * |
| 258 | * @since 1.0.0 |
| 259 | */ |
| 260 | public function hide_comments_column() { |
| 261 | |
| 262 | $post_types = get_post_types( array( 'public' => true ), 'names' ); |
| 263 | |
| 264 | foreach ( $post_types as $post_type_key => $post_type_name ) { |
| 265 | |
| 266 | if ( post_type_supports( $post_type_key, 'comments' ) ) { |
| 267 | |
| 268 | if ( 'attachment' != $post_type_name ) { |
| 269 | |
| 270 | // For list tables of pages, posts and other post types |
| 271 | add_filter( "manage_{$post_type_name}_posts_columns", [ $this, 'remove_comment_column' ] ); |
| 272 | |
| 273 | } else { |
| 274 | |
| 275 | // For list table of media/attachment |
| 276 | add_filter( 'manage_media_columns', [ $this, 'remove_comment_column' ] ); |
| 277 | |
| 278 | } |
| 279 | |
| 280 | } |
| 281 | |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Add a column called ID |
| 288 | * |
| 289 | * @param mixed $columns |
| 290 | * @return void |
| 291 | * @since 1.0.0 |
| 292 | */ |
| 293 | public function remove_comment_column( $columns ) { |
| 294 | |
| 295 | unset( $columns['comments'] ); |
| 296 | |
| 297 | return $columns; |
| 298 | |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Hide tags column in list tables for posts. |
| 303 | * |
| 304 | * @since 1.0.0 |
| 305 | */ |
| 306 | public function hide_post_tags_column() { |
| 307 | |
| 308 | $post_types = get_post_types( array( 'public' => true ), 'names' ); |
| 309 | |
| 310 | foreach ( $post_types as $post_type_key => $post_type_name ) { |
| 311 | |
| 312 | if ( $post_type_name == 'post' ) { |
| 313 | |
| 314 | add_filter( "manage_posts_columns", [ $this, 'remove_post_tags_column' ] ); |
| 315 | |
| 316 | } |
| 317 | |
| 318 | } |
| 319 | |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Add a column called ID |
| 324 | * |
| 325 | * @param mixed $columns |
| 326 | * @return void |
| 327 | * @since 1.0.0 |
| 328 | */ |
| 329 | public function remove_post_tags_column( $columns ) { |
| 330 | |
| 331 | unset( $columns['tags'] ); |
| 332 | |
| 333 | return $columns; |
| 334 | |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Show custom (hierarchical) taxonomy filter(s) for all post types. |
| 339 | * |
| 340 | * @since 1.0.0 |
| 341 | */ |
| 342 | public function show_custom_taxonomy_filters( $post_type ) { |
| 343 | |
| 344 | $post_taxonomies = get_object_taxonomies( $post_type, 'objects' ); |
| 345 | |
| 346 | // Only show custom taxonomy filters for post types other than 'post' |
| 347 | |
| 348 | if ( 'post' != $post_type ) { |
| 349 | |
| 350 | array_walk( $post_taxonomies, [ $this, 'output_taxonomy_filter' ] ); |
| 351 | |
| 352 | } |
| 353 | |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Output filter on the post type's list table for a taxonomy |
| 358 | * |
| 359 | * @since 1.0.0 |
| 360 | */ |
| 361 | public function output_taxonomy_filter( $post_taxonomy ) { |
| 362 | |
| 363 | // Only show taxonomy filter when the taxonomy is hierarchical |
| 364 | |
| 365 | if ( true === $post_taxonomy->hierarchical ) { |
| 366 | |
| 367 | wp_dropdown_categories( array( |
| 368 | 'show_option_all' => sprintf( 'All %s', $post_taxonomy->label ), |
| 369 | 'orderby' => 'name', |
| 370 | 'order' => 'ASC', |
| 371 | 'hide_empty' => false, |
| 372 | 'hide_if_empty' => true, |
| 373 | 'selected' => filter_input( INPUT_GET, $post_taxonomy->query_var, FILTER_SANITIZE_STRING ), |
| 374 | 'hierarchical' => true, |
| 375 | 'name' => $post_taxonomy->query_var, |
| 376 | 'taxonomy' => $post_taxonomy->name, |
| 377 | 'value_field' => 'slug', |
| 378 | ) ); |
| 379 | |
| 380 | } |
| 381 | |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Enable duplication of pages, posts and custom posts |
| 386 | * |
| 387 | * @since 1.0.0 |
| 388 | */ |
| 389 | public function asenha_enable_duplication() { |
| 390 | |
| 391 | $original_post_id = intval( sanitize_text_field( $_REQUEST['post'] ) ); |
| 392 | $nonce = sanitize_text_field( $_REQUEST['nonce'] ); |
| 393 | |
| 394 | if ( wp_verify_nonce( $nonce, 'asenha-duplicate-' . $original_post_id ) && current_user_can( 'edit_posts' ) ) { |
| 395 | |
| 396 | $original_post = get_post( $original_post_id ); |
| 397 | |
| 398 | // Set some attributes for the duplicate post |
| 399 | |
| 400 | $new_post_title_suffix = ' (DUPLICATE)'; |
| 401 | $new_post_status = 'draft'; |
| 402 | $current_user = wp_get_current_user(); |
| 403 | $new_post_author_id = $current_user->ID; |
| 404 | |
| 405 | // Create the duplicate post and store the ID |
| 406 | |
| 407 | $args = array( |
| 408 | |
| 409 | 'comment_status' => $original_post->comment_status, |
| 410 | 'ping_status' => $original_post->ping_status, |
| 411 | 'post_author' => $new_post_author_id, |
| 412 | 'post_content' => $original_post->post_content, |
| 413 | 'post_excerpt' => $original_post->post_excerpt, |
| 414 | 'post_parent' => $original_post->post_parent, |
| 415 | 'post_password' => $original_post->post_password, |
| 416 | 'post_status' => $new_post_status, |
| 417 | 'post_title' => $original_post->post_title . $new_post_title_suffix, |
| 418 | 'post_type' => $original_post->post_type, |
| 419 | 'to_ping' => $original_post->to_ping, |
| 420 | 'menu_order' => $original_post->menu_order, |
| 421 | |
| 422 | ); |
| 423 | |
| 424 | $new_post_id = wp_insert_post( $args ); |
| 425 | |
| 426 | // Copy over the taxonomies |
| 427 | |
| 428 | $original_taxonomies = get_object_taxonomies( $original_post->post_type ); |
| 429 | |
| 430 | if ( ! empty( $original_taxonomies ) && is_array( $original_taxonomies ) ) { |
| 431 | |
| 432 | foreach( $original_taxonomies as $taxonomy ) { |
| 433 | |
| 434 | $original_post_terms = wp_get_object_terms( $original_post_id, $taxonomy, array( 'fields' => 'slugs' ) ); |
| 435 | |
| 436 | wp_set_object_terms( $new_post_id, $original_post_terms, $taxonomy, false ); |
| 437 | |
| 438 | } |
| 439 | |
| 440 | } |
| 441 | |
| 442 | // Copy over the post meta |
| 443 | |
| 444 | $original_post_metas = get_post_meta( $original_post_id ); // all meta keys and the corresponding values |
| 445 | |
| 446 | if ( ! empty( $original_post_metas ) ) { |
| 447 | |
| 448 | foreach( $original_post_metas as $meta_key => $meta_values ) { |
| 449 | |
| 450 | foreach( $meta_values as $meta_value ) { |
| 451 | |
| 452 | add_post_meta( $new_post_id, $meta_key, $meta_value ); |
| 453 | |
| 454 | } |
| 455 | |
| 456 | } |
| 457 | |
| 458 | } |
| 459 | |
| 460 | // Redirect to list table of the corresponding post type of original post |
| 461 | |
| 462 | $post_type = get_post_type( $original_post_id ); |
| 463 | |
| 464 | if ( 'post' == $post_type ) { |
| 465 | |
| 466 | wp_redirect( admin_url( 'edit.php' ) ); |
| 467 | |
| 468 | } else { |
| 469 | |
| 470 | wp_redirect( admin_url( 'edit.php?post_type=' . $post_type ) ); |
| 471 | |
| 472 | } |
| 473 | |
| 474 | } else { |
| 475 | |
| 476 | wp_die( 'You do not have permission to perform this action.' ); |
| 477 | |
| 478 | } |
| 479 | |
| 480 | } |
| 481 | |
| 482 | /** |
| 483 | * Add row action link to perform duplication in page/post list tables |
| 484 | * |
| 485 | * @since 1.0.0 |
| 486 | */ |
| 487 | public function add_duplication_action_link( $actions, $post ) { |
| 488 | |
| 489 | if ( current_user_can( 'edit_posts' ) ) { |
| 490 | |
| 491 | $actions['asenha-duplicate'] = '<a href="admin.php?action=asenha_enable_duplication&post=' . $post->ID . '&nonce=' . wp_create_nonce( 'asenha-duplicate-' . $post->ID ) . '" title="Duplicate this as draft">Duplicate</a>'; |
| 492 | |
| 493 | } |
| 494 | |
| 495 | return $actions; |
| 496 | |
| 497 | } |
| 498 | |
| 499 | /** |
| 500 | * Modify the 'Edit' link to be 'Edit or Replace' |
| 501 | * |
| 502 | */ |
| 503 | public function modify_media_list_table_edit_link( $actions, $post ) { |
| 504 | |
| 505 | $new_actions = array(); |
| 506 | |
| 507 | foreach( $actions as $key => $value ) { |
| 508 | |
| 509 | if ( $key == 'edit' ) { |
| 510 | |
| 511 | $new_actions['edit'] = '<a href="' . get_edit_post_link( $post ) . '" aria-label="Edit or Replace">Edit or Replace</a>'; |
| 512 | |
| 513 | } else { |
| 514 | |
| 515 | $new_actions[$key] = $value; |
| 516 | |
| 517 | } |
| 518 | |
| 519 | } |
| 520 | |
| 521 | return $new_actions; |
| 522 | |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * Add media replacement button in the edit screen of media/attachment |
| 527 | * |
| 528 | * @since 1.1.0 |
| 529 | */ |
| 530 | public function add_media_replacement_button( $fields ) { |
| 531 | |
| 532 | // Enqueues all scripts, styles, settings, and templates necessary to use all media JS APIs. |
| 533 | // Reference: https://codex.wordpress.org/Javascript_Reference/wp.media |
| 534 | wp_enqueue_media(); |
| 535 | |
| 536 | // Add new field to attachment fields for the media replace functionality |
| 537 | $fields['asenha-media-replace'] = array(); |
| 538 | $fields['asenha-media-replace']['label'] = ''; |
| 539 | $fields['asenha-media-replace']['input'] = 'html'; |
| 540 | $fields['asenha-media-replace']['html'] = ' |
| 541 | <div id="media-replace-div" class="postbox"> |
| 542 | <div class="postbox-header"> |
| 543 | <h2 class="hndle ui-sortable-handle">Replace Media</h2> |
| 544 | </div> |
| 545 | <div class="inside"> |
| 546 | <button type="button" id="asenha-media-replace" class="button-secondary button-large asenha-media-replace-button">Select New Media File</button> |
| 547 | <input type="hidden" id="new-attachment-id" name="new-attachment-id" /> |
| 548 | <div class="asenha-media-replace-notes">The current file will be replaced with the uploaded and/or selected file while retaining the current ID, publish date and file name. Thus, no existing links will break.</div> |
| 549 | </div> |
| 550 | </div> |
| 551 | '; |
| 552 | |
| 553 | return $fields; |
| 554 | |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * Replace existing media with the newly updated file |
| 559 | * |
| 560 | * @link https://plugins.trac.wordpress.org/browser/replace-image/tags/1.1.7/hm-replace-image.php#L55 |
| 561 | * @since 1.1.0 |
| 562 | */ |
| 563 | public function replace_media( $old_attachment_id ) { |
| 564 | |
| 565 | $old_post_meta = get_post( $old_attachment_id, ARRAY_A ); |
| 566 | $old_post_mime = $old_post_meta['post_mime_type']; // e.g. 'image/jpeg' |
| 567 | |
| 568 | // Get the new attachment/media ID, meta and mime type |
| 569 | $new_attachment_id = intval( sanitize_text_field( $_POST['new-attachment-id'] ) ); |
| 570 | $new_post_meta = get_post( $new_attachment_id, ARRAY_A ); |
| 571 | $new_post_mime = $new_post_meta['post_mime_type']; // e.g. 'image/jpeg' |
| 572 | |
| 573 | // Check if the media file ID selected via the media frame and passed on to the #new-attachment-id hidden field |
| 574 | // Ensure the mime type matches too |
| 575 | if ( ( ! empty( $new_attachment_id ) ) && is_numeric( $new_attachment_id ) && ( $old_post_mime == $new_post_mime ) ) { |
| 576 | |
| 577 | $new_attachment_meta = wp_get_attachment_metadata( $new_attachment_id ); |
| 578 | |
| 579 | // If original file is larger than 2560 pixel |
| 580 | // https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/ |
| 581 | if ( array_key_exists( 'original_image', $new_attachment_meta ) ) { |
| 582 | |
| 583 | // Get the original media file path |
| 584 | $new_media_file_path = wp_get_original_image_path( $new_attachment_id ); |
| 585 | |
| 586 | } else { |
| 587 | |
| 588 | // Get the path to newly uploaded media file. An image file name may end with '-scaled'. |
| 589 | $new_attachment_file = get_post_meta( $new_attachment_id, '_wp_attached_file', true ); |
| 590 | $upload_dir = wp_upload_dir(); |
| 591 | $new_media_file_path = $upload_dir['basedir'] . '/' . $new_attachment_file; |
| 592 | |
| 593 | } |
| 594 | |
| 595 | // Check if the new media file exist / was successfully uploaded |
| 596 | if ( ! is_file( $new_media_file_path ) ) { |
| 597 | return false; |
| 598 | } |
| 599 | |
| 600 | // Delete existing/old media files. Post and post meta entries for it are still there in the database. |
| 601 | $this->delete_media_files( $old_attachment_id ); |
| 602 | |
| 603 | // If original file is larger than 2560 pixel |
| 604 | // https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/ |
| 605 | if ( array_key_exists( 'original_image', $new_attachment_meta ) ) { |
| 606 | |
| 607 | // Get the original media file path |
| 608 | $old_media_file_path = wp_get_original_image_path( $old_attachment_id ); |
| 609 | |
| 610 | } else { |
| 611 | |
| 612 | // Get the path to the old/existing media file that will be replaced and deleted. An image file name may end with '-scaled'. |
| 613 | $old_attachment_file = get_post_meta( $old_attachment_id, '_wp_attached_file', true ); |
| 614 | $old_media_file_path = $upload_dir['basedir'] . '/' . $old_attachment_file; |
| 615 | |
| 616 | } |
| 617 | |
| 618 | // Check if the directory path to the old media file is still intact |
| 619 | if ( ! file_exists( dirname( $old_media_file_path ) ) ) { |
| 620 | |
| 621 | // Recreate the directory path |
| 622 | mkdir( dirname( $old_media_file_path ), 0755, true ); |
| 623 | |
| 624 | } |
| 625 | |
| 626 | // Copy the new media file into the old media file's path |
| 627 | copy( $new_media_file_path, $old_media_file_path ); |
| 628 | |
| 629 | // Regenerate attachment meta data and image sub-sizes from the new media file that was just copied to the old path |
| 630 | $old_media_post_meta_updated = wp_generate_attachment_metadata( $old_attachment_id, $old_media_file_path ); |
| 631 | |
| 632 | // Update new media file's meta data with the ones from the old media. i.e. new media file will carry over the post ID and post meta of the old media file. i.e. only the files are replaced for the old media's ID and post meta in the database. |
| 633 | wp_update_attachment_metadata( $old_attachment_id, $old_media_post_meta_updated ); |
| 634 | |
| 635 | // Delete the newly uploaded media file and it's sub-sizes, and also delete post and post meta entries for it in the database. |
| 636 | wp_delete_attachment( $new_attachment_id, true ); |
| 637 | |
| 638 | } |
| 639 | |
| 640 | } |
| 641 | |
| 642 | /** |
| 643 | * Delete the existing/old media files when performing media replacement |
| 644 | * |
| 645 | * @link https://plugins.trac.wordpress.org/browser/replace-image/tags/1.1.7/hm-replace-image.php#L80 |
| 646 | * @since 1.1.0 |
| 647 | */ |
| 648 | public function delete_media_files( $post_id ) { |
| 649 | |
| 650 | $attachment_meta = wp_get_attachment_metadata( $post_id ); |
| 651 | |
| 652 | // Will get '-scaled' version if it exists, e.g. /path/to/uploads/year/month/file-name.jpg |
| 653 | $attachment_file_path = get_attached_file( $post_id ); |
| 654 | |
| 655 | // e.g. file-name.jpg |
| 656 | $attachment_file_basename = basename( $attachment_file_path ); |
| 657 | |
| 658 | // Delete intermediate images if there are any |
| 659 | |
| 660 | if ( isset( $attachment_meta['sizes'] ) && is_array( $attachment_meta['sizes'] ) ) { |
| 661 | |
| 662 | foreach( $attachment_meta['sizes'] as $size => $size_info) { |
| 663 | |
| 664 | // /path/to/uploads/year/month/file-name.jpg --> /path/to/uploads/year/month/file-name-1024x768.jpg |
| 665 | $intermediate_file_path = str_replace( $attachment_file_basename, $size_info['file'], $attachment_file_path ); |
| 666 | wp_delete_file( $intermediate_file_path ); |
| 667 | |
| 668 | } |
| 669 | |
| 670 | } |
| 671 | |
| 672 | // Delete the attachment file, which maybe the '-scaled' version |
| 673 | wp_delete_file( $attachment_file_path ); |
| 674 | |
| 675 | // If original file is larger than 2560 pixel |
| 676 | // https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/ |
| 677 | if ( array_key_exists( 'original_image', $attachment_meta ) ) { |
| 678 | |
| 679 | $attachment_original_file_path = wp_get_original_image_path( $post_id ); |
| 680 | |
| 681 | // Delete the original file |
| 682 | wp_delete_file( $attachment_original_file_path ); |
| 683 | |
| 684 | } |
| 685 | |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Customize the attachment updated message |
| 690 | * |
| 691 | * @link https://github.com/WordPress/wordpress-develop/blob/6.0.2/src/wp-admin/edit-form-advanced.php#L180 |
| 692 | * @since 1.1.0 |
| 693 | */ |
| 694 | public function attachment_updated_custom_message( $messages ) { |
| 695 | |
| 696 | $new_messages = array(); |
| 697 | |
| 698 | foreach( $messages as $post_type => $messages_array ) { |
| 699 | |
| 700 | if ( $post_type == 'attachment' ) { |
| 701 | |
| 702 | // Message ID for successful edit/update of an attachment is 4. e.g. /wp-admin/post.php?post=a&action=edit&classic-editor&message=4 Customize it here. |
| 703 | $messages_array[4] = 'Media file updated. You may need to <a href="https://fabricdigital.co.nz/blog/how-to-hard-refresh-your-browser-and-clear-cache" target="_blank">hard refresh</a> your browser to see the updated media preview image below.'; |
| 704 | |
| 705 | } |
| 706 | |
| 707 | $new_messages[$post_type] = $messages_array; |
| 708 | |
| 709 | } |
| 710 | |
| 711 | return $new_messages; |
| 712 | |
| 713 | } |
| 714 | |
| 715 | } |