Changeset 3487673
- Timestamp:
- 03/21/2026 09:02:41 AM (7 days ago)
- Location:
- mediahue-media-rename-auto-alt-text
- Files:
-
- 9 added
- 6 edited
-
assets/screenshot-1.png (modified) (previous)
-
assets/screenshot-3.png (modified) (previous)
-
assets/screenshot-4.png (added)
-
tags/1.0.2 (added)
-
tags/1.0.2/assets (added)
-
tags/1.0.2/assets/admin-mediahue-admin.css (added)
-
tags/1.0.2/assets/admin-mediahue-settings.css (added)
-
tags/1.0.2/assets/admin-mediahue-settings.js (added)
-
tags/1.0.2/assets/admin-mediahue.js (added)
-
tags/1.0.2/mediahue-media-rename-auto-alt-text.php (added)
-
tags/1.0.2/readme.txt (added)
-
trunk/assets/admin-mediahue-admin.css (modified) (9 diffs)
-
trunk/assets/admin-mediahue.js (modified) (1 diff)
-
trunk/mediahue-media-rename-auto-alt-text.php (modified) (11 diffs)
-
trunk/readme.txt (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
mediahue-media-rename-auto-alt-text/trunk/assets/admin-mediahue-admin.css
r3454853 r3487673 1 .compat-attachment-fields .compat-field-mediahue_custom_name { 1 .compat-attachment-fields .compat-field-mediahue_custom_name, 2 .compat-attachment-fields .compat-field-mediahue_custom_title { 2 3 display: block; 3 4 margin: 16px 0 20px; … … 10 11 11 12 .compat-attachment-fields .compat-field-mediahue_custom_name th, 12 .compat-attachment-fields .compat-field-mediahue_custom_name td { 13 .compat-attachment-fields .compat-field-mediahue_custom_name td, 14 .compat-attachment-fields .compat-field-mediahue_custom_title th, 15 .compat-attachment-fields .compat-field-mediahue_custom_title td { 13 16 display: block !important; 14 17 width: 100% !important; … … 16 19 } 17 20 18 .compat-attachment-fields .compat-field-mediahue_custom_name th label { 21 .compat-attachment-fields .compat-field-mediahue_custom_name th label, 22 .compat-attachment-fields .compat-field-mediahue_custom_title th label { 19 23 display: block; 20 24 margin: 0 0 8px 0; … … 23 27 } 24 28 25 .compat-attachment-fields .compat-field-mediahue_custom_name th .alignleft { 29 .compat-attachment-fields .compat-field-mediahue_custom_name th .alignleft, 30 .compat-attachment-fields .compat-field-mediahue_custom_title th .alignleft { 26 31 float: left; 27 32 padding-bottom: 3px; 28 33 } 29 34 30 .compat-attachment-fields .compat-field-mediahue_custom_name th .clear { 35 .compat-attachment-fields .compat-field-mediahue_custom_name th .clear, 36 .compat-attachment-fields .compat-field-mediahue_custom_title th .clear { 31 37 display: none; 32 38 } 33 39 34 .compat-attachment-fields .compat-field-mediahue_custom_name td input[type="text"] { 40 .compat-attachment-fields .compat-field-mediahue_custom_name td input[type="text"], 41 .compat-attachment-fields .compat-field-mediahue_custom_title td input[type="text"] { 35 42 width: 100%; 36 43 max-width: 100%; … … 44 51 } 45 52 46 .compat-attachment-fields .compat-field-mediahue_custom_name td input[type="text"]:focus { 53 .compat-attachment-fields .compat-field-mediahue_custom_name td input[type="text"]:focus, 54 .compat-attachment-fields .compat-field-mediahue_custom_title td input[type="text"]:focus { 47 55 border-color: #2271b1; 48 56 box-shadow: 0 0 0 1px #2271b1, 0 0 0 4px rgba(34, 113, 177, 0.15); … … 50 58 } 51 59 52 .compat-attachment-fields .compat-field-mediahue_custom_name td .help { 60 .compat-attachment-fields .compat-field-mediahue_custom_name td .help, 61 .compat-attachment-fields .compat-field-mediahue_custom_title td .help { 53 62 margin-top: 6px; 54 63 color: #6c7781; … … 57 66 } 58 67 59 .wp-list-table .column-mediahue_new_name { 60 width: 240px; 68 .wp-list-table .column-mediahue_new_name, 69 .wp-list-table .column-mediahue_new_title { 70 width: 170px; 71 min-width: 150px; 61 72 } 62 73 63 .wp-list-table .column-mediahue_new_name input.mediahue-new-name { 74 .wp-list-table .column-mediahue_new_name input.mediahue-new-name, 75 .wp-list-table .column-mediahue_new_title input.mediahue-new-title { 64 76 width: 100%; 65 max-width: 240px;77 max-width: 170px; 66 78 height: 32px; 67 79 padding: 4px 8px; … … 70 82 background-color: #fff; 71 83 transition: border-color 0.15s ease, box-shadow 0.15s ease; 84 box-sizing: border-box; 72 85 } 73 86 74 .wp-list-table .column-mediahue_new_name input.mediahue-new-name:focus { 87 .wp-list-table .column-mediahue_new_name input.mediahue-new-name:focus, 88 .wp-list-table .column-mediahue_new_title input.mediahue-new-title:focus { 75 89 border-color: #2271b1; 76 90 box-shadow: 0 0 0 1px #2271b1, 0 0 0 4px rgba(34, 113, 177, 0.15); … … 79 93 80 94 @media (max-width: 782px) { 81 .wp-list-table .column-mediahue_new_name { 95 .wp-list-table .column-mediahue_new_name, 96 .wp-list-table .column-mediahue_new_title { 82 97 width: auto; 83 98 } 84 99 85 .wp-list-table .column-mediahue_new_name input.mediahue-new-name { 100 .wp-list-table .column-mediahue_new_name input.mediahue-new-name, 101 .wp-list-table .column-mediahue_new_title input.mediahue-new-title { 86 102 max-width: 100%; 87 103 } -
mediahue-media-rename-auto-alt-text/trunk/assets/admin-mediahue.js
r3454853 r3487673 60 60 } 61 61 62 $form.find('input.mediahue-hidden-name ').remove();62 $form.find('input.mediahue-hidden-name, input.mediahue-hidden-title').remove(); 63 63 64 64 $form.find('input[name="media[]"]:checked').each(function () { 65 65 var id = $(this).val(); 66 var titleName = 'mediahue_new_title[' + id + ']'; 67 var titleSelector = 'input.mediahue-new-title[name="' + titleName.replace(/\[/g, '\\[').replace(/\]/g, '\\]') + '"]'; 68 var $titleInput = $form.find(titleSelector); 69 if ($titleInput.length) { 70 $('<input type="hidden" class="mediahue-hidden-title" />') 71 .attr('name', titleName) 72 .val($titleInput.val()) 73 .appendTo($form); 74 } 75 66 76 var name = 'mediahue_new_name[' + id + ']'; 67 77 var selector = 'input.mediahue-new-name[name="' + name.replace(/\[/g, '\\[').replace(/\]/g, '\\]') + '"]'; 68 78 var $input = $form.find(selector); 69 if (!$input.length) { 70 return; 79 if ($input.length) { 80 $('<input type="hidden" class="mediahue-hidden-name" />') 81 .attr('name', name) 82 .val($input.val()) 83 .appendTo($form); 71 84 } 72 $('<input type="hidden" class="mediahue-hidden-name" />')73 .attr('name', name)74 .val($input.val())75 .appendTo($form);76 85 }); 77 86 -
mediahue-media-rename-auto-alt-text/trunk/mediahue-media-rename-auto-alt-text.php
r3454853 r3487673 3 3 * Plugin Name: MediaHue – Media Rename & Auto Alt Text 4 4 * Description: Rename media files and automatically generate missing alt text, titles, and captions to improve media SEO and accessibility. 5 * Version: 1.0. 16 * Author: Solanki Software5 * Version: 1.0.2 6 * Author: Kirtikumar Solanki 7 7 * Author URI: https://profiles.wordpress.org/solankisoftware/ 8 8 * License: GPLv2 or later … … 14 14 if ( ! defined( 'ABSPATH' ) ) exit; 15 15 16 define( 'MEDIAHUE_MEDIA_CUSTOMIZER_VERSION', '1.0. 1' );16 define( 'MEDIAHUE_MEDIA_CUSTOMIZER_VERSION', '1.0.2' ); 17 17 define( 'MEDIAHUE_MEDIA_CUSTOMIZER_PATH', plugin_dir_path( __FILE__ ) ); 18 18 define( 'MEDIAHUE_MEDIA_CUSTOMIZER_URL', plugin_dir_url( __FILE__ ) ); … … 32 32 add_filter( 'handle_bulk_actions-upload', 'mediahue_media_customizer_handle_bulk_action', 10, 3 ); 33 33 add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'mediahue_media_customizer_add_settings_link' ); 34 add_filter( 'plugin_row_meta', 'mediahue_media_customizer_add_plugin_row_meta', 10, 2 ); 34 35 35 36 function mediahue_media_customizer_enqueue_admin_assets( $hook ) { … … 129 130 } 130 131 132 function mediahue_media_customizer_add_plugin_row_meta( $links, $file ) { 133 if ( plugin_basename( __FILE__ ) !== $file ) { 134 return $links; 135 } 136 137 $review_url = 'https://wordpress.org/support/plugin/mediahue-media-rename-auto-alt-text/reviews/#new-post'; 138 $links[] = sprintf( 139 '<a href="%1$s" target="_blank" rel="noopener noreferrer">%2$s</a>', 140 esc_url( $review_url ), 141 esc_html__( 'Leave a Review', 'mediahue-media-rename-auto-alt-text' ) 142 ); 143 144 return $links; 145 } 146 131 147 function mediahue_media_customizer_normalize_title( $title ) { 132 148 $title = str_replace( array( '-', '_' ), ' ', (string) $title ); … … 144 160 $base_title = mediahue_media_customizer_normalize_title( $base_title ); 145 161 return $base_title; 162 } 163 164 function mediahue_media_customizer_get_attached_relative_path( $attachment_id ) { 165 $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); 166 return is_string( $file ) ? $file : ''; 167 } 168 169 function mediahue_media_customizer_rename_attachment_file( $attachment_id, $requested_name ) { 170 $requested_name = sanitize_file_name( (string) $requested_name ); 171 $requested_name = trim( $requested_name ); 172 if ( '' === $requested_name ) { 173 return false; 174 } 175 176 $current_path = get_attached_file( $attachment_id ); 177 if ( ! $current_path || ! file_exists( $current_path ) ) { 178 return false; 179 } 180 181 $current_basename = wp_basename( $current_path ); 182 $current_dir = dirname( $current_path ); 183 $current_ext = strtolower( pathinfo( $current_basename, PATHINFO_EXTENSION ) ); 184 $new_ext = strtolower( pathinfo( $requested_name, PATHINFO_EXTENSION ) ); 185 186 if ( '' === $new_ext ) { 187 $requested_name .= '' !== $current_ext ? '.' . $current_ext : ''; 188 } elseif ( '' !== $current_ext && $new_ext !== $current_ext ) { 189 $requested_name = pathinfo( $requested_name, PATHINFO_FILENAME ) . '.' . $current_ext; 190 } 191 192 $requested_name = sanitize_file_name( $requested_name ); 193 if ( '' === $requested_name || $requested_name === $current_basename ) { 194 return false; 195 } 196 197 $unique_name = wp_unique_filename( $current_dir, $requested_name ); 198 $new_path = trailingslashit( $current_dir ) . $unique_name; 199 if ( ! @rename( $current_path, $new_path ) ) { 200 return false; 201 } 202 203 $relative_path = mediahue_media_customizer_get_attached_relative_path( $attachment_id ); 204 if ( '' !== $relative_path ) { 205 $relative_dir = dirname( $relative_path ); 206 $relative_file = ( '.' === $relative_dir || '' === $relative_dir ) ? $unique_name : trailingslashit( $relative_dir ) . $unique_name; 207 update_attached_file( $attachment_id, $relative_file ); 208 } else { 209 update_attached_file( $attachment_id, $new_path ); 210 } 211 212 $metadata = wp_get_attachment_metadata( $attachment_id ); 213 if ( is_array( $metadata ) ) { 214 $old_base = pathinfo( $current_basename, PATHINFO_FILENAME ); 215 $new_base = pathinfo( $unique_name, PATHINFO_FILENAME ); 216 $meta_dir = dirname( $new_path ); 217 218 if ( ! empty( $metadata['sizes'] ) && is_array( $metadata['sizes'] ) ) { 219 foreach ( $metadata['sizes'] as $size_key => $size_data ) { 220 if ( empty( $size_data['file'] ) || ! is_string( $size_data['file'] ) ) { 221 continue; 222 } 223 $size_file = $size_data['file']; 224 if ( 0 !== strpos( $size_file, $old_base . '-' ) ) { 225 continue; 226 } 227 $renamed_size_file = $new_base . substr( $size_file, strlen( $old_base ) ); 228 $old_size_path = trailingslashit( $meta_dir ) . $size_file; 229 $new_size_path = trailingslashit( $meta_dir ) . $renamed_size_file; 230 if ( file_exists( $old_size_path ) ) { 231 if ( file_exists( $new_size_path ) ) { 232 $renamed_size_file = wp_unique_filename( $meta_dir, $renamed_size_file ); 233 $new_size_path = trailingslashit( $meta_dir ) . $renamed_size_file; 234 } 235 @rename( $old_size_path, $new_size_path ); 236 } 237 $metadata['sizes'][ $size_key ]['file'] = $renamed_size_file; 238 } 239 } 240 241 if ( ! empty( $metadata['original_image'] ) && is_string( $metadata['original_image'] ) ) { 242 $original_file = $metadata['original_image']; 243 if ( 0 === strpos( $original_file, $old_base . '-' ) ) { 244 $renamed_original = $new_base . substr( $original_file, strlen( $old_base ) ); 245 $old_original = trailingslashit( $meta_dir ) . $original_file; 246 $new_original = trailingslashit( $meta_dir ) . $renamed_original; 247 if ( file_exists( $old_original ) ) { 248 if ( file_exists( $new_original ) ) { 249 $renamed_original = wp_unique_filename( $meta_dir, $renamed_original ); 250 $new_original = trailingslashit( $meta_dir ) . $renamed_original; 251 } 252 @rename( $old_original, $new_original ); 253 } 254 $metadata['original_image'] = $renamed_original; 255 } 256 } 257 258 if ( ! empty( $metadata['file'] ) && is_string( $metadata['file'] ) ) { 259 $metadata_relative_dir = dirname( $metadata['file'] ); 260 $metadata['file'] = ( '.' === $metadata_relative_dir || '' === $metadata_relative_dir ) 261 ? $unique_name 262 : trailingslashit( $metadata_relative_dir ) . $unique_name; 263 } 264 265 wp_update_attachment_metadata( $attachment_id, $metadata ); 266 } 267 268 return true; 146 269 } 147 270 … … 416 539 } 417 540 418 $form_fields['mediahue_custom_name'] = array( 419 'label' => __( 'New Media Name', 'mediahue-media-rename-auto-alt-text' ), 541 $file_path = get_attached_file( $post->ID ); 542 $file_name = $file_path ? wp_basename( $file_path ) : ''; 543 544 $form_fields['mediahue_custom_title'] = array( 545 'label' => __( 'New Title', 'mediahue-media-rename-auto-alt-text' ), 420 546 'input' => 'text', 421 547 'value' => $post->post_title, 422 'helps' => __( 'Set a new name for this media item.', 'mediahue-media-rename-auto-alt-text' ), 548 'helps' => __( 'Set a new title for this media item.', 'mediahue-media-rename-auto-alt-text' ), 549 'class' => 'mediahue-custom-title', 550 ); 551 552 $form_fields['mediahue_custom_name'] = array( 553 'label' => __( 'New FileName', 'mediahue-media-rename-auto-alt-text' ), 554 'input' => 'text', 555 'value' => $file_name, 556 'helps' => __( 'Rename the media file (example: new-filename.jpg).', 'mediahue-media-rename-auto-alt-text' ), 423 557 'class' => 'mediahue-custom-name', 424 558 ); … … 433 567 434 568 if ( empty( $attachment['mediahue_custom_name'] ) ) { 435 return $post; 436 } 437 438 $new_title = sanitize_text_field( $attachment['mediahue_custom_name'] ); 439 if ( '' === $new_title ) { 440 return $post; 441 } 442 443 $post['post_title'] = $new_title; 444 $post['post_name'] = sanitize_title( $new_title ); 569 $attachment['mediahue_custom_name'] = ''; 570 } 571 572 if ( ! empty( $attachment['mediahue_custom_title'] ) ) { 573 $new_title = sanitize_text_field( $attachment['mediahue_custom_title'] ); 574 if ( '' !== $new_title ) { 575 $post['post_title'] = $new_title; 576 $post['post_name'] = sanitize_title( $new_title ); 577 } 578 } 579 580 $new_file_name = sanitize_file_name( $attachment['mediahue_custom_name'] ); 581 if ( '' !== $new_file_name && ! empty( $post['ID'] ) ) { 582 mediahue_media_customizer_rename_attachment_file( (int) $post['ID'], $new_file_name ); 583 } 445 584 446 585 return $post; … … 448 587 449 588 function mediahue_media_customizer_add_listing_column( $columns ) { 450 $columns['mediahue_new_name'] = __( 'New Media Name', 'mediahue-media-rename-auto-alt-text' ); 589 $columns['mediahue_new_title'] = __( 'New Title', 'mediahue-media-rename-auto-alt-text' ); 590 $columns['mediahue_new_name'] = __( 'New FileName', 'mediahue-media-rename-auto-alt-text' ); 451 591 return $columns; 452 592 } 453 593 454 594 function mediahue_media_customizer_render_listing_column( $column_name, $attachment_id ) { 455 if ( 'mediahue_new_name' !== $column_name) {595 if ( ! in_array( $column_name, array( 'mediahue_new_title', 'mediahue_new_name' ), true ) ) { 456 596 return; 457 597 } … … 461 601 } 462 602 463 $input_id = 'mediahue-new-name-' . $attachment_id; 464 $value = get_the_title( $attachment_id ); 603 if ( 'mediahue_new_title' === $column_name ) { 604 $input_id = 'mediahue-new-title-' . $attachment_id; 605 $value = get_the_title( $attachment_id ); 606 printf( 607 '<label class="screen-reader-text" for="%1$s">%2$s</label><input type="text" class="mediahue-new-title" id="%1$s" name="mediahue_new_title[%3$d]" value="%4$s" />', 608 esc_attr( $input_id ), 609 esc_html__( 'New Title', 'mediahue-media-rename-auto-alt-text' ), 610 (int) $attachment_id, 611 esc_attr( $value ) 612 ); 613 return; 614 } 615 616 $input_id = 'mediahue-new-name-' . $attachment_id; 617 $file_path = get_attached_file( $attachment_id ); 618 $value = $file_path ? wp_basename( $file_path ) : ''; 465 619 printf( 466 620 '<label class="screen-reader-text" for="%1$s">%2$s</label><input type="text" class="mediahue-new-name" id="%1$s" name="mediahue_new_name[%3$d]" value="%4$s" />', 467 621 esc_attr( $input_id ), 468 esc_html__( 'New MediaName', 'mediahue-media-rename-auto-alt-text' ),622 esc_html__( 'New FileName', 'mediahue-media-rename-auto-alt-text' ), 469 623 (int) $attachment_id, 470 624 esc_attr( $value ) … … 493 647 } 494 648 495 $names = isset( $_REQUEST['mediahue_new_name'] ) ? (array) wp_unslash( $_REQUEST['mediahue_new_name'] ) : array(); 649 $titles = isset( $_REQUEST['mediahue_new_title'] ) ? (array) wp_unslash( $_REQUEST['mediahue_new_title'] ) : array(); 650 $names = isset( $_REQUEST['mediahue_new_name'] ) ? (array) wp_unslash( $_REQUEST['mediahue_new_name'] ) : array(); 496 651 if ( empty( $post_ids ) && isset( $_REQUEST['media'] ) ) { 497 652 $post_ids = array_filter( array_map( 'absint', (array) $_REQUEST['media'] ) ); … … 501 656 foreach ( $post_ids as $attachment_id ) { 502 657 $attachment_id = (int) $attachment_id; 503 if ( empty( $names[ $attachment_id ] ) ) { 504 continue; 505 } 506 $new_title = sanitize_text_field( $names[ $attachment_id ] ); 507 if ( '' === $new_title ) { 508 continue; 509 } 510 wp_update_post( 511 array( 512 'ID' => $attachment_id, 513 'post_title' => $new_title, 514 'post_name' => sanitize_title( $new_title ), 515 ) 516 ); 517 $updated++; 658 $did_update = false; 659 if ( isset( $titles[ $attachment_id ] ) ) { 660 $new_title = sanitize_text_field( $titles[ $attachment_id ] ); 661 if ( '' !== $new_title ) { 662 wp_update_post( 663 array( 664 'ID' => $attachment_id, 665 'post_title' => $new_title, 666 'post_name' => sanitize_title( $new_title ), 667 ) 668 ); 669 $did_update = true; 670 } 671 } 672 673 if ( isset( $names[ $attachment_id ] ) ) { 674 $new_file_name = sanitize_file_name( $names[ $attachment_id ] ); 675 if ( '' !== $new_file_name ) { 676 $renamed = mediahue_media_customizer_rename_attachment_file( $attachment_id, $new_file_name ); 677 $did_update = $did_update || $renamed; 678 } 679 } 680 681 if ( $did_update ) { 682 $updated++; 683 } 518 684 } 519 685 -
mediahue-media-rename-auto-alt-text/trunk/readme.txt
r3455806 r3487673 6 6 Tested up to: 6.9 7 7 Requires PHP: 7.4 8 Stable tag: 1.0. 18 Stable tag: 1.0.2 9 9 License: GPLv2 or later 10 10 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 41 41 1. Media listing page for media rename 42 42 2. Media customizer page 43 3. Media details page for media rename 43 3. Media details page for media rename :: List View 44 4. Media details page for media rename :: Grid View 44 45 45 46 … … 64 65 65 66 == Changelog == 67 = 1.0.2 = 68 * Added filename change Feature 69 66 70 = 1.0.1 = 67 71 * Fixed issues
Note: See TracChangeset
for help on using the changeset viewer.