Plugin Directory

Changeset 3487673


Ignore:
Timestamp:
03/21/2026 09:02:41 AM (7 days ago)
Author:
solankisoftware
Message:

In V 1.0.2 Added media filename rename feature

Location:
mediahue-media-rename-auto-alt-text
Files:
9 added
6 edited

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 {
    23    display: block;
    34    margin: 16px 0 20px;
     
    1011
    1112.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 {
    1316    display: block !important;
    1417    width: 100% !important;
     
    1619}
    1720
    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 {
    1923    display: block;
    2024    margin: 0 0 8px 0;
     
    2327}
    2428
    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 {
    2631    float: left;
    2732    padding-bottom: 3px;
    2833}
    2934
    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 {
    3137    display: none;
    3238}
    3339
    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"] {
    3542    width: 100%;
    3643    max-width: 100%;
     
    4451}
    4552
    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 {
    4755    border-color: #2271b1;
    4856    box-shadow: 0 0 0 1px #2271b1, 0 0 0 4px rgba(34, 113, 177, 0.15);
     
    5058}
    5159
    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 {
    5362    margin-top: 6px;
    5463    color: #6c7781;
     
    5766}
    5867
    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;
    6172}
    6273
    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 {
    6476    width: 100%;
    65     max-width: 240px;
     77    max-width: 170px;
    6678    height: 32px;
    6779    padding: 4px 8px;
     
    7082    background-color: #fff;
    7183    transition: border-color 0.15s ease, box-shadow 0.15s ease;
     84    box-sizing: border-box;
    7285}
    7386
    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 {
    7589    border-color: #2271b1;
    7690    box-shadow: 0 0 0 1px #2271b1, 0 0 0 4px rgba(34, 113, 177, 0.15);
     
    7993
    8094@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 {
    8297        width: auto;
    8398    }
    8499
    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 {
    86102        max-width: 100%;
    87103    }
  • mediahue-media-rename-auto-alt-text/trunk/assets/admin-mediahue.js

    r3454853 r3487673  
    6060        }
    6161
    62         $form.find('input.mediahue-hidden-name').remove();
     62        $form.find('input.mediahue-hidden-name, input.mediahue-hidden-title').remove();
    6363
    6464        $form.find('input[name="media[]"]:checked').each(function () {
    6565            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
    6676            var name = 'mediahue_new_name[' + id + ']';
    6777            var selector = 'input.mediahue-new-name[name="' + name.replace(/\[/g, '\\[').replace(/\]/g, '\\]') + '"]';
    6878            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);
    7184            }
    72             $('<input type="hidden" class="mediahue-hidden-name" />')
    73                 .attr('name', name)
    74                 .val($input.val())
    75                 .appendTo($form);
    7685        });
    7786
  • mediahue-media-rename-auto-alt-text/trunk/mediahue-media-rename-auto-alt-text.php

    r3454853 r3487673  
    33 * Plugin Name: MediaHue – Media Rename & Auto Alt Text
    44 * Description: Rename media files and automatically generate missing alt text, titles, and captions to improve media SEO and accessibility.
    5  * Version:     1.0.1
    6  * Author:      Solanki Software
     5 * Version:     1.0.2
     6 * Author:      Kirtikumar Solanki
    77 * Author URI:  https://profiles.wordpress.org/solankisoftware/
    88 * License:     GPLv2 or later
     
    1414if ( ! defined( 'ABSPATH' ) ) exit;
    1515
    16 define( 'MEDIAHUE_MEDIA_CUSTOMIZER_VERSION', '1.0.1' );
     16define( 'MEDIAHUE_MEDIA_CUSTOMIZER_VERSION', '1.0.2' );
    1717define( 'MEDIAHUE_MEDIA_CUSTOMIZER_PATH', plugin_dir_path( __FILE__ ) );
    1818define( 'MEDIAHUE_MEDIA_CUSTOMIZER_URL', plugin_dir_url( __FILE__ ) );
     
    3232add_filter( 'handle_bulk_actions-upload', 'mediahue_media_customizer_handle_bulk_action', 10, 3 );
    3333add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'mediahue_media_customizer_add_settings_link' );
     34add_filter( 'plugin_row_meta', 'mediahue_media_customizer_add_plugin_row_meta', 10, 2 );
    3435
    3536function mediahue_media_customizer_enqueue_admin_assets( $hook ) {
     
    129130}
    130131
     132function 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
    131147function mediahue_media_customizer_normalize_title( $title ) {
    132148    $title = str_replace( array( '-', '_' ), ' ', (string) $title );
     
    144160    $base_title = mediahue_media_customizer_normalize_title( $base_title );
    145161    return $base_title;
     162}
     163
     164function 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
     169function 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;
    146269}
    147270
     
    416539    }
    417540
    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' ),
    420546        'input' => 'text',
    421547        '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' ),
    423557        'class' => 'mediahue-custom-name',
    424558    );
     
    433567
    434568    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    }
    445584
    446585    return $post;
     
    448587
    449588function 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' );
    451591    return $columns;
    452592}
    453593
    454594function 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 ) ) {
    456596        return;
    457597    }
     
    461601    }
    462602
    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 ) : '';
    465619    printf(
    466620        '<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" />',
    467621        esc_attr( $input_id ),
    468         esc_html__( 'New Media Name', 'mediahue-media-rename-auto-alt-text' ),
     622        esc_html__( 'New FileName', 'mediahue-media-rename-auto-alt-text' ),
    469623        (int) $attachment_id,
    470624        esc_attr( $value )
     
    493647    }
    494648
    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();
    496651    if ( empty( $post_ids ) && isset( $_REQUEST['media'] ) ) {
    497652        $post_ids = array_filter( array_map( 'absint', (array) $_REQUEST['media'] ) );
     
    501656    foreach ( $post_ids as $attachment_id ) {
    502657        $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        }
    518684    }
    519685
  • mediahue-media-rename-auto-alt-text/trunk/readme.txt

    r3455806 r3487673  
    66Tested up to: 6.9
    77Requires PHP: 7.4
    8 Stable tag: 1.0.1
     8Stable tag: 1.0.2
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    41411. Media listing page for media rename 
    42422. Media customizer page
    43 3. Media details page for media rename
     433. Media details page for media rename :: List View
     444. Media details page for media rename :: Grid View
    4445
    4546
     
    6465
    6566== Changelog ==
     67= 1.0.2 =
     68* Added filename change Feature
     69
    6670= 1.0.1 =
    6771* Fixed issues
Note: See TracChangeset for help on using the changeset viewer.