Plugin Directory

Changeset 3329312


Ignore:
Timestamp:
07/17/2025 12:30:14 AM (5 months ago)
Author:
jchristopher
Message:

Version 0.3.0

Location:
alt-text-tools/trunk
Files:
6 added
2 edited

Legend:

Unmodified
Added
Removed
  • alt-text-tools/trunk/alt-text-tools.php

    r2302160 r3329312  
    33 Plugin Name: Alt Text Tools
    44 Description: Exports a CSV file of all images that are actually used in your content, along with their corresponding alt tags.
    5  Version: 0.2.0
     5 Version: 0.3.0
    66 Author: NerdPress
    77 Author URI: https://www.nerdpress.net
     
    1313
    1414if ( ! defined( 'NP_ATT_VERSION' ) )
    15     define( 'NP_ATT_VERSION', '0.2.0' );
     15    define( 'NP_ATT_VERSION', '0.3.0' );
    1616
    1717class NerdpressAltTextTools {
     
    3434        new $class;
    3535    }
    36    
     36
    3737    /**
    3838     * Constructor
     
    4646        add_action( 'admin_menu', array( $this, 'settingsPage' ) );
    4747        add_action( 'wp_ajax_getCsv', array( $this, 'getCsv' ) );
    48        
    49     }
    50    
     48
     49    }
     50
    5151    /**
    5252     * Inject JS script(s) into the settings page
    5353     *
    5454     * @since 0.0.1
    55      */ 
     55     */
    5656    public function injectScripts() {
    5757        wp_register_script( 'np_alt_tools_js', plugins_url( 'js/np_alt_tools.js', __FILE__ ), array( 'jquery' ),  NP_ATT_VERSION );
     
    6060            'nonce' => wp_create_nonce( 'np_alt_tools_secure_me' )
    6161        ) );
    62         wp_enqueue_script( 'np_alt_tools_js' ); 
     62        wp_enqueue_script( 'np_alt_tools_js' );
    6363    }
    6464
     
    9494    }
    9595
    96     private static function quoteIfComma( $fields_array ) {
     96    private static function escapeCsv( $fields_array ) {
    9797        $result = [];
    9898        foreach( $fields_array as $field => $value ) {
    99             if ( strpos( $value, ',' ) !== FALSE )
    100                 $result[] = '"' . $value . '"';
    101             else
    102                 $result[] = $value;
    103         }
    104         return $result;             
     99            // Escape double quotes by doubling them.
     100            $value = str_replace( '"', '""', $value );
     101
     102            // Enclose in double quotes if it contains special characters
     103            if ( preg_match( '/[,"\r\n]/', $value ) ) {
     104                $value = '"' . $value . '"';
     105            }
     106
     107            $result[] = $value;
     108        }
     109        return $result;
    105110    }
    106111
     
    133138            '_builtin' => FALSE
    134139        );
    135         $postTypes = array_merge( 
    136             array( 'post', 'page' ), 
    137             get_post_types( $opts ) 
     140        $postTypes = array_merge(
     141            array( 'post', 'page' ),
     142            get_post_types( $opts )
    138143        );
    139144
    140145        $posts = [];
    141         foreach( $postTypes as $type ) { 
     146        foreach( $postTypes as $type ) {
    142147            $posts = array_merge( $posts, get_posts( array(
    143148                'posts_per_page' => -1,
    144                 'post_type' => $type 
     149                'post_type' => $type
    145150            ) ) );
    146151        }
     
    152157        $imgElemRegex = '/<img[^>]*>/';
    153158        $site_url = site_url();
    154         $finds = []; 
     159        $finds = [];
    155160        foreach( $posts as $post ) {
    156161            $post_link = get_permalink( $post );
    157162            $post_title = get_the_title( $post );
    158163            $post_edit_link = get_edit_post_link( $post );
    159             $found = preg_match_all( 
    160                 $imgElemRegex, 
     164            $found = preg_match_all(
     165                $imgElemRegex,
    161166                apply_filters( 'the_content', $post->post_content ),
    162                 $matches, 
    163                 PREG_PATTERN_ORDER 
     167                $matches,
     168                PREG_PATTERN_ORDER
    164169            );
    165170
     
    172177                        $media_link = $site_url .'/wp-admin/upload.php?s=' . self::getImageNameFromUrl( $img_url[ 1 ] );
    173178                    else
    174                         $media_link = ''; 
     179                        $media_link = '';
    175180
    176181                    preg_match( '/alt="([^"]*)"/', $submatch, $alt_tag );
     
    193198            }
    194199        }
    195      
     200
    196201        /*================================
    197202        * Create CSV File and AJAX it back
    198203        *=================================*/
    199         $rows = array( implode( ',', [ 
    200               'post id', 
     204        $rows = array( implode( ',', [
     205              'post id',
    201206              'post_type',
    202               'page title', 
    203               'page url', 
    204               'image url', 
    205               'image alt tag', 
     207              'page title',
     208              'page url',
     209              'image url',
     210              'image alt tag',
    206211              'edit link',
    207               'media library link' 
    208             ] 
     212              'media library link'
     213            ]
    209214        ) );
    210215
    211216        foreach( $finds as $find ) {
    212             $rows[] = implode( ',', self::quoteIfComma( $find ) );
     217            $rows[] = implode( ',', self::escapeCsv( $find ) );
    213218        }
    214219
    215220        $csvString = implode( '\r\n', $rows );
    216221        echo $csvString;
    217         die(); 
     222        die();
    218223    }
    219224}
  • alt-text-tools/trunk/readme.txt

    r3056486 r3329312  
    11=== Alt Text Tools ===
    2 Contributors: nerdpressteam, eatingrules
     2Contributors: nerdpressteam, eatingrules, jchristopher
    33Author URI: https://www.nerdpress.net/
    4 Tested Up To: 6.5
    5 Stable tag: trunk
     4Tested Up To: 6.8
     5Stable tag: 0.3.0
    66Tags: alt text, accessibility, alternative text, alt tags, fix alt text
    77License: GPLv2
     
    1515This plugin will provide a CSV (comma separated values) file that lists all of your images used in your content -- and their corresponding Alt attribute.
    1616
    17 To use, go to `Tools > Alt Text Tools`, and click the button. Give it a few moments to scan your site, and your download should begin. 
     17To use, go to `Tools > Alt Text Tools`, and click the button. Give it a few moments to scan your site, and your download should begin.
    1818
    1919You can then open the file into your favorite spreadsheet program, and use it to identify images that are missing Alt Texts, or that need other improvement. The file also includes links to edit the post in which the images appear, so you can quickly and easily edit the attribute.
     
    4141If you add an Alt Text to the image in the media library (_before_ it's inserted into the post content), then it will be inserted with that Alt Text.
    4242
    43 **Heads up!** If you're using the Block Editor, and if you use the "Image" block to insert an image and upload it directly into the block (Selecting an "Image" Block and then clicking the "Upload" button), it  inserts the image and adds it to the media library _before_ you can add the Alt Text. So you'll end up inserting the image, adding the Alt Text in the sidebar in the editor, and later find that there's no Alt Text set on the image in the media library! 
     43**Heads up!** If you're using the Block Editor, and if you use the "Image" block to insert an image and upload it directly into the block (Selecting an "Image" Block and then clicking the "Upload" button), it  inserts the image and adds it to the media library _before_ you can add the Alt Text. So you'll end up inserting the image, adding the Alt Text in the sidebar in the editor, and later find that there's no Alt Text set on the image in the media library!
    4444
    4545The workaround for this is to select the image block, click "Media Library" (instead of "Upload"), and then from the Media Library modal, click the "Upload Files" tab, and upload the image there. Add the Alt Text after you upload the image, and _then_ insert it into the post.
     
    5555= What's the difference between EMPTY and MISSING? =
    5656
    57 An empty (null) alt text, which is expressed like this in the code: `alt=""`, we'll note that they're `EMPTY`. (This is generally considered the best practice for "decorative" images.) 
     57An empty (null) alt text, which is expressed like this in the code: `alt=""`, we'll note that they're `EMPTY`. (This is generally considered the best practice for "decorative" images.)
    5858
    5959If the image has no `alt` attribute at all, then we'll note that it's `MISSING`.
     
    8989== Changelog ==
    9090
     91= 0.3.0 =
     92* Improved CSV formatting when fields contain special characters.
     93
    9194= 0.2.0 =
    9295* First release.
Note: See TracChangeset for help on using the changeset viewer.