Plugin Directory

Changeset 1995174


Ignore:
Timestamp:
12/14/2018 09:31:52 PM (7 years ago)
Author:
waughjai
Message:

Add image directory bar to WP admin

Location:
waj-image
Files:
218 added
12 edited

Legend:

Unmodified
Added
Removed
  • waj-image/trunk/README.md

    r1987319 r1995174  
    22* Contributors: waughjai
    33* Tags: image, loader, html generator
    4 * Requires at least: 4.9.8
    5 * Tested up to: 4.9.8
    6 * Stable tag: 1.2.0
     4* Requires at least: 5.0.0
     5* Tested up to: 5.0.1
     6* Stable tag: 1.3.0
    77* Requires PHP: 7.0
    88* License: GPLv2 or later
     
    5050If "alt" attribute is not set, an empty 1 will automatically be added to the HTML generated, ensuring that all images made through these will have an alt tag.
    5151
    52 To make working with theme image objects with minimal inconvenience for images that are all in the same directory that is not the topmost directory o' the theme directory, you can globally set the inner shared directory using WPThemeImage's static setDefaultSharedDirectory method on the class itself. After that, all initialized WPThemeImage & WPThemePicture instances, including the shortcodes, will automatically use that shared directory if a different 1 isn't provided.
     52To make working with theme image objects with minimal inconvenience for images that are all in the same directory that is not the topmost directory o' the theme directory, you can globally set the inner shared directory in the WordPress admin through Appearances -> Theme -> Directories, or directly in PHP with WPThemeImage's static setDefaultSharedDirectory method on the class itself. After that, all initialized WPThemeImage & WPThemePicture instances, including the shortcodes, will automatically use that shared directory if a different 1 isn't provided.
    5353
    5454To add HTML attributes to WPThemePicture & WPUploadsPicture shortcodes, prefix them with "img-", "picture-", or "source-" depending on what tag you want the attribute given to. For example, to apply a class to the img tag, give the shortcode the attribute "img-class".
     
    9393## Changelog
    9494
     95### 1.3
     96* Add directory bar to admin.
     97
    9598### 1.2
    9699* Add ability to just get source from images.
  • waj-image/trunk/composer.json

    r1987319 r1995174  
    1414        "php": ">=7.0",
    1515        "waughj/file-loader": ">=0.2.0",
    16         "waughj/html-image": ">=0.4.0",
     16        "waughj/html-image": ">=0.3.0",
    1717        "waughj/html-picture": "*",
    1818        "waughj/test-hash-item": "*",
    19         "waughj/wp-get-image-sizes": "*"
     19        "waughj/wp-get-image-sizes": "*",
     20        "waughj/wp-theme-option": "0.3.0"
    2021    },
    2122    "require-dev": {
  • waj-image/trunk/readme.txt

    r1987319 r1995174  
    22Contributors: waughjai
    33Tags: image, loader, html generator
    4 Requires at least: 4.9.8
    5 Tested up to: 4.9.8
    6 Stable tag: 1.2.0
     4Requires at least: 5.0.0
     5Tested up to: 5.0.1
     6Stable tag: 1.3.0
    77Requires PHP: 7.0
    88License: GPLv2 or later
     
    5050If "alt" attribute is not set, an empty 1 will automatically be added to the HTML generated, ensuring that all images made through these will have an alt tag.
    5151
    52 To make working with theme image objects with minimal inconvenience for images that are all in the same directory that is not the topmost directory o' the theme directory, you can globally set the inner shared directory using WPThemeImage's static setDefaultSharedDirectory method on the class itself. After that, all initialized WPThemeImage & WPThemePicture instances, including the shortcodes, will automatically use that shared directory if a different 1 isn't provided.
     52To make working with theme image objects with minimal inconvenience for images that are all in the same directory that is not the topmost directory o' the theme directory, you can globally set the inner shared directory in the WordPress admin through Appearances -> Theme -> Directories, or directly in PHP with WPThemeImage's static setDefaultSharedDirectory method on the class itself. After that, all initialized WPThemeImage & WPThemePicture instances, including the shortcodes, will automatically use that shared directory if a different 1 isn't provided.
    5353
    5454To add HTML attributes to WPThemePicture & WPUploadsPicture shortcodes, prefix them with "img-", "picture-", or "source-" depending on what tag you want the attribute given to. For example, to apply a class to the img tag, give the shortcode the attribute "img-class".
     
    9393== Changelog ==
    9494
     95= 1.3 =
     96* Add directory bar to admin.
     97
    9598= 1.2 =
    9699* Add ability to just get source from images.
  • waj-image/trunk/tests/MockWordPress.php

    r1987123 r1995174  
    8383        return null;
    8484    }
     85
     86    global $settings_fields;
     87    $settings_fields = [];
     88
     89    function __( $name )
     90    {
     91        return $name;
     92    }
     93
     94    function add_action( $hook, $function )
     95    {
     96        $function();
     97    }
     98
     99    function add_settings_field( $slug, $name, $renderer, $group )
     100    {
     101        global $settings_fields;
     102        if ( !isset( $settings_fields[ $group ] ) )
     103        {
     104            $settings_fields[ $group ] = [];
     105        }
     106        $settings_fields[ $group ][] = $renderer;
     107    };
     108
     109    function do_settings_sections( $group )
     110    {
     111        global $settings_fields;
     112        if ( isset( $settings_fields[ $group ] ) )
     113        {
     114            foreach ( $settings_fields[ $group ] as $renderer )
     115            {
     116                echo $renderer();
     117            }
     118        }
     119    };
     120
     121    function add_theme_page() {};
     122    function add_option() {};
     123    function register_setting() {};
     124    function add_settings_section() {};
     125    function settings_errors() {};
     126    function settings_fields() {};
     127    function submit_button() { echo '<input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">'; };
  • waj-image/trunk/vendor/autoload.php

    r1987123 r1995174  
    55require_once __DIR__ . '/composer/autoload_real.php';
    66
    7 return ComposerAutoloaderInit285755ba0587b518d8728f77f33b073b::getLoader();
     7return ComposerAutoloaderInite59dc2f8def671ec8f56cadb899ebf75::getLoader();
  • waj-image/trunk/vendor/composer/autoload_psr4.php

    r1983500 r1995174  
    77
    88return array(
     9    'WaughJ\\WPThemeOption\\' => array($vendorDir . '/waughj/wp-theme-option/src'),
    910    'WaughJ\\WPImage\\' => array($baseDir . '/src'),
    1011    'WaughJ\\VerifiedArguments\\' => array($vendorDir . '/waughj/verified-arguments/src'),
  • waj-image/trunk/vendor/composer/autoload_real.php

    r1987123 r1995174  
    33// autoload_real.php @generated by Composer
    44
    5 class ComposerAutoloaderInit285755ba0587b518d8728f77f33b073b
     5class ComposerAutoloaderInite59dc2f8def671ec8f56cadb899ebf75
    66{
    77    private static $loader;
     
    2020        }
    2121
    22         spl_autoload_register(array('ComposerAutoloaderInit285755ba0587b518d8728f77f33b073b', 'loadClassLoader'), true, true);
     22        spl_autoload_register(array('ComposerAutoloaderInite59dc2f8def671ec8f56cadb899ebf75', 'loadClassLoader'), true, true);
    2323        self::$loader = $loader = new \Composer\Autoload\ClassLoader();
    24         spl_autoload_unregister(array('ComposerAutoloaderInit285755ba0587b518d8728f77f33b073b', 'loadClassLoader'));
     24        spl_autoload_unregister(array('ComposerAutoloaderInite59dc2f8def671ec8f56cadb899ebf75', 'loadClassLoader'));
    2525
    2626        $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
     
    2828            require_once __DIR__ . '/autoload_static.php';
    2929
    30             call_user_func(\Composer\Autoload\ComposerStaticInit285755ba0587b518d8728f77f33b073b::getInitializer($loader));
     30            call_user_func(\Composer\Autoload\ComposerStaticInite59dc2f8def671ec8f56cadb899ebf75::getInitializer($loader));
    3131        } else {
    3232            $map = require __DIR__ . '/autoload_namespaces.php';
     
    4949
    5050        if ($useStaticLoader) {
    51             $includeFiles = Composer\Autoload\ComposerStaticInit285755ba0587b518d8728f77f33b073b::$files;
     51            $includeFiles = Composer\Autoload\ComposerStaticInite59dc2f8def671ec8f56cadb899ebf75::$files;
    5252        } else {
    5353            $includeFiles = require __DIR__ . '/autoload_files.php';
    5454        }
    5555        foreach ($includeFiles as $fileIdentifier => $file) {
    56             composerRequire285755ba0587b518d8728f77f33b073b($fileIdentifier, $file);
     56            composerRequiree59dc2f8def671ec8f56cadb899ebf75($fileIdentifier, $file);
    5757        }
    5858
     
    6161}
    6262
    63 function composerRequire285755ba0587b518d8728f77f33b073b($fileIdentifier, $file)
     63function composerRequiree59dc2f8def671ec8f56cadb899ebf75($fileIdentifier, $file)
    6464{
    6565    if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
  • waj-image/trunk/vendor/composer/autoload_static.php

    r1987123 r1995174  
    55namespace Composer\Autoload;
    66
    7 class ComposerStaticInit285755ba0587b518d8728f77f33b073b
     7class ComposerStaticInite59dc2f8def671ec8f56cadb899ebf75
    88{
    99    public static $files = array (
     
    1616        'W' =>
    1717        array (
     18            'WaughJ\\WPThemeOption\\' => 21,
    1819            'WaughJ\\WPImage\\' => 15,
    1920            'WaughJ\\VerifiedArguments\\' => 25,
     
    3839
    3940    public static $prefixDirsPsr4 = array (
     41        'WaughJ\\WPThemeOption\\' =>
     42        array (
     43            0 => __DIR__ . '/..' . '/waughj/wp-theme-option/src',
     44        ),
    4045        'WaughJ\\WPImage\\' =>
    4146        array (
     
    9196    {
    9297        return \Closure::bind(function () use ($loader) {
    93             $loader->prefixLengthsPsr4 = ComposerStaticInit285755ba0587b518d8728f77f33b073b::$prefixLengthsPsr4;
    94             $loader->prefixDirsPsr4 = ComposerStaticInit285755ba0587b518d8728f77f33b073b::$prefixDirsPsr4;
     98            $loader->prefixLengthsPsr4 = ComposerStaticInite59dc2f8def671ec8f56cadb899ebf75::$prefixLengthsPsr4;
     99            $loader->prefixDirsPsr4 = ComposerStaticInite59dc2f8def671ec8f56cadb899ebf75::$prefixDirsPsr4;
    95100
    96101        }, null, ClassLoader::class);
  • waj-image/trunk/vendor/composer/installed.json

    r1987319 r1995174  
    198198    {
    199199        "name": "waughj/file-loader",
    200         "version": "0.4.0",
    201         "version_normalized": "0.4.0.0",
     200        "version": "0.4.2",
     201        "version_normalized": "0.4.2.0",
    202202        "source": {
    203203            "type": "git",
    204204            "url": "https://github.com/waughjai/file-loader.git",
    205             "reference": "3532fee29005c127dc8c1e9f0ef9228c4898bc7d"
    206         },
    207         "dist": {
    208             "type": "zip",
    209             "url": "https://api.github.com/repos/waughjai/file-loader/zipball/3532fee29005c127dc8c1e9f0ef9228c4898bc7d",
    210             "reference": "3532fee29005c127dc8c1e9f0ef9228c4898bc7d",
     205            "reference": "5ed773b2e739a6e07bbabc3d85f0f86015861b5f"
     206        },
     207        "dist": {
     208            "type": "zip",
     209            "url": "https://api.github.com/repos/waughjai/file-loader/zipball/5ed773b2e739a6e07bbabc3d85f0f86015861b5f",
     210            "reference": "5ed773b2e739a6e07bbabc3d85f0f86015861b5f",
    211211            "shasum": ""
    212212        },
     
    221221            "phpunit/phpunit": "6.*"
    222222        },
    223         "time": "2018-12-04T20:15:24+00:00",
     223        "time": "2018-12-12T20:32:53+00:00",
    224224        "type": "library",
    225225        "installation-source": "dist",
     
    622622            "wordpress"
    623623        ]
     624    },
     625    {
     626        "name": "waughj/wp-theme-option",
     627        "version": "0.3.0",
     628        "version_normalized": "0.3.0.0",
     629        "source": {
     630            "type": "git",
     631            "url": "https://github.com/waughjai/wp-theme-option.git",
     632            "reference": "112d170fe6abdf207c9f81f3f3e57ee6e473388c"
     633        },
     634        "dist": {
     635            "type": "zip",
     636            "url": "https://api.github.com/repos/waughjai/wp-theme-option/zipball/112d170fe6abdf207c9f81f3f3e57ee6e473388c",
     637            "reference": "112d170fe6abdf207c9f81f3f3e57ee6e473388c",
     638            "shasum": ""
     639        },
     640        "require": {
     641            "php": ">=7.0",
     642            "waughj/test-hash-item": ">=1.0.1",
     643            "waughj/verified-arguments-same-type": "*"
     644        },
     645        "require-dev": {
     646            "phpunit/phpunit": "6.*"
     647        },
     648        "time": "2018-12-14T00:46:52+00:00",
     649        "type": "library",
     650        "installation-source": "dist",
     651        "autoload": {
     652            "psr-4": {
     653                "WaughJ\\WPThemeOption\\": "src/"
     654            }
     655        },
     656        "notification-url": "https://packagist.org/downloads/",
     657        "license": [
     658            "GPL-2.0-or-later"
     659        ],
     660        "authors": [
     661            {
     662                "name": "Jaimeson Waugh",
     663                "email": "waughjai@gmail.com"
     664            }
     665        ],
     666        "description": "Classes for simplifying the creation of theme options, option sections, & option pages.",
     667        "keywords": [
     668            "generator",
     669            "option",
     670            "theme",
     671            "wordpress"
     672        ]
    624673    }
    625674]
  • waj-image/trunk/vendor/waughj/file-loader/src/FileLoader.php

    r1987123 r1995174  
    4343            public function getSourceWithVersion( string $local ) : string
    4444            {
    45                 return $this->getSource( $local ) . $this->getVersionString( $local );
     45                $version_string = '';
     46                $source = $this->getSource( $local );
     47                try
     48                {
     49                    $version_string = $this->getVersionString( $local );
     50                }
     51                catch ( \Exception $e )
     52                {
     53                    echo( "Error: could not find modified time for file \"{$source}\"" );
     54                }
     55
     56                return $source . $version_string;
    4657            }
    4758
     
    104115                {
    105116                    $server_location = $this->getServerLocation( $local );
    106                     return filemtime( $server_location->getString([ 'ending-slash' => false ]) );
     117                    $filetime = filemtime( $server_location->getString([ 'ending-slash' => false ]) );
     118                    return ( $filetime !== false ) ? $filetime : 0;
    107119                }
    108120                return 0;
     
    121133                {
    122134                    $server_location = $this->getServerLocation( $local );
    123                     return '?m=' . ( string )( $this->getVersion( $local ) );
     135                    $version = $this->getVersion( $local );
     136                    return '?m=' . ( string )( ( $version > 0 ) ? $version : '' );
    124137                }
    125138                return '';
  • waj-image/trunk/vendor/waughj/file-loader/tests/FileLoaderTest.php

    r1987123 r1995174  
    2929        $loader4 = new FileLoader([ 'directory-url' => null ]);
    3030        $this->assertEquals( $loader4->getSource( 'logo.png' ), 'logo.png' );
     31    }
     32
     33    public function testNonexistentFile()
     34    {
     35        $loader = new FileLoader([ 'directory-url' => 'https://www.jaimeson-waugh.com/', 'directory-server' => getcwd() ]);
     36        $this->assertEquals( 'https://www.jaimeson-waugh.com/bleb', $loader->getSourceWithVersion( 'bleb' ) );
    3137    }
    3238
  • waj-image/trunk/waj-image-loaders.php

    r1987324 r1995174  
    44    Plugin URI:   https://github.com/waughjai/waj-image-loaders
    55    Description:  Classes & shortcodes for making image HTML generation simpler for WordPress.
    6     Version:      1.2.0
     6    Version:      1.3.0
    77    Author:       Jaimeson Waugh
    88    Author URI:   https://www.jaimeson-waugh.com
     
    2424    use WaughJ\HTMLPicture\HTMLPicture;
    2525    use function WaughJ\TestHashItem\TestHashItemString;
     26    use WaughJ\WPThemeOption\WPThemeOptionsPageManager;
     27    use WaughJ\WPThemeOption\WPThemeOptionsSection;
     28    use WaughJ\WPThemeOption\WPThemeOption;
    2629
    27     add_shortcode
    28     (
    29         'theme-image',
    30         ImageShortcodeFunctionGenerator( WPThemeImage::class )
    31     );
    3230
    33     add_shortcode
    34     (
    35         'upload-image',
    36         function ( $atts )
     31
     32    //
     33    //  ADMIN THEME IMAGE DIRECTORY
     34    //
     35    //////////////////////////////////////////////////////////
     36
     37        $page = WPThemeOptionsPageManager::initializeIfNotAlreadyInitialized( 'directories', 'Directories' );
     38        $section = new WPThemeOptionsSection( $page, 'theme-image', 'Theme Image' );
     39        $option = new WPThemeOption( $section, 'theme-image-directory', 'Theme Image Directory' );
     40        WPThemeImage::setDefaultSharedDirectory( $option->getOptionValue() );
     41
     42
     43
     44    //
     45    //  SHORTCODES
     46    //
     47    //////////////////////////////////////////////////////////
     48
     49        add_shortcode
     50        (
     51            'theme-image',
     52            ImageShortcodeFunctionGenerator( WPThemeImage::class )
     53        );
     54
     55        add_shortcode
     56        (
     57            'upload-image',
     58            function ( $atts )
     59            {
     60                $id = TestHashItemString( $atts, 'id' );
     61                $size = TestHashItemString( $atts, 'size' );
     62                if ( $id )
     63                {
     64                    // Make sure we don't propagate this to the HTML Attributes list.
     65                    unset( $atts[ 'id' ], $atts[ 'size' ] );
     66                    return ( string )( new WPUploadImage( intval( $id ), $size, $atts ) );
     67                }
     68                return '';
     69            }
     70        );
     71
     72        add_shortcode
     73        (
     74            'image',
     75            function ( $atts )
     76            {
     77                $src = TestHashItemString( $atts, 'src' );
     78                if ( $src )
     79                {
     80                    // Make sure we don't propagate this to the HTML Attributes list.
     81                    unset( $atts[ 'src' ] );
     82                    return ( string )( new HTMLImage( $src, null, $atts ) );
     83                }
     84                return '';
     85            }
     86        );
     87
     88        add_shortcode
     89        (
     90            'theme-picture',
     91            PictureShortcodeFunctionGenerator( WPThemePicture::class )
     92        );
     93
     94        add_shortcode
     95        (
     96            'upload-picture',
     97            function ( $atts )
     98            {
     99                $id = TestHashItemString( $atts, 'id' );
     100                if ( $id )
     101                {
     102                    $atts = TransformShortcodeAttributesToElementAttributes( $atts );
     103                    // Make sure we don't propagate these to the HTML Attributes list.
     104                    unset( $atts[ 'id' ] );
     105                    return ( string )( new WPUploadPicture( intval( $id ), $atts ) );
     106                }
     107                return '';
     108            }
     109        );
     110
     111        add_shortcode
     112        (
     113            'picture',
     114            PictureShortcodeFunctionGenerator( HTMLPicture::class )
     115        );
     116
     117        function ImageShortcodeFunctionGenerator( string $class )
    37118        {
    38             $id = TestHashItemString( $atts, 'id' );
    39             $size = TestHashItemString( $atts, 'size' );
    40             if ( $id )
     119            return function ( $atts ) use ( $class )
    41120            {
    42                 // Make sure we don't propagate this to the HTML Attributes list.
    43                 unset( $atts[ 'id' ], $atts[ 'size' ] );
    44                 return ( string )( new WPUploadImage( intval( $id ), $size, $atts ) );
    45             }
    46             return '';
    47         }
    48     );
    49 
    50     add_shortcode
    51     (
    52         'image',
    53         function ( $atts )
    54         {
    55             $src = TestHashItemString( $atts, 'src' );
    56             if ( $src )
    57             {
    58                 // Make sure we don't propagate this to the HTML Attributes list.
    59                 unset( $atts[ 'src' ] );
    60                 return ( string )( new HTMLImage( $src, null, $atts ) );
    61             }
    62             return '';
    63         }
    64     );
    65 
    66     add_shortcode
    67     (
    68         'theme-picture',
    69         PictureShortcodeFunctionGenerator( WPThemePicture::class )
    70     );
    71 
    72     add_shortcode
    73     (
    74         'upload-picture',
    75         function ( $atts )
    76         {
    77             $id = TestHashItemString( $atts, 'id' );
    78             if ( $id )
    79             {
    80                 $atts = TransformShortcodeAttributesToElementAttributes( $atts );
    81                 // Make sure we don't propagate these to the HTML Attributes list.
    82                 unset( $atts[ 'id' ] );
    83                 return ( string )( new WPUploadPicture( intval( $id ), $atts ) );
    84             }
    85             return '';
    86         }
    87     );
    88 
    89     add_shortcode
    90     (
    91         'picture',
    92         PictureShortcodeFunctionGenerator( HTMLPicture::class )
    93     );
    94 
    95     function ImageShortcodeFunctionGenerator( string $class )
    96     {
    97         return function ( $atts ) use ( $class )
    98         {
    99             $src = TestHashItemString( $atts, 'src' );
    100             if ( $src )
    101             {
    102                 // Make sure we don't propagate this to the HTML Attributes list.
    103                 unset( $atts[ 'src' ] );
    104                 return ( string )( new $class( $src, $atts ) );
    105             }
    106             return '';
    107         };
    108     }
    109 
    110     function PictureShortcodeFunctionGenerator( string $class )
    111     {
    112         return function ( $atts ) use ( $class )
    113         {
    114             $src = TestHashItemString( $atts, 'src' );
    115             $ext = TestHashItemString( $atts, 'ext' );
    116             $sizes = TestHashItemString( $atts, 'sizes' );
    117             if ( $src && $ext && $sizes )
    118             {
    119                 $atts = TransformShortcodeAttributesToElementAttributes( $atts );
    120                 // Make sure we don't propagate these to the HTML Attributes list.
    121                 unset( $atts[ 'src' ], $atts[ 'ext' ], $atts[ 'sizes' ] );
    122                 return ( string )( new $class( $src, $ext, $sizes, $atts ) );
    123             }
    124             return '';
    125         };
    126     }
    127 
    128     function TransformShortcodeAttributesToElementAttributes( array $atts ) : array
    129     {
    130         // Initialize
    131         $element_atts = [];
    132         $prefixes = [];
    133         $prefix_lengths = [];
    134 
    135         foreach ( PICTURE_ELEMENTS as $element )
    136         {
    137             // Where we will put the new versions o' the attributes for each element.
    138             $element_atts[ $element ] = [];
    139             // We need these for the lengths, so we might as well save these.
    140             $prefixes[ $element ] = "{$element}-";
    141             // Optimization: save string lengths so we don't recalculate these for every attribute x element, but can just reference them directly.
    142             $prefix_lengths[ $element ] = strlen( $prefixes[ $element ] );
     121                $src = TestHashItemString( $atts, 'src' );
     122                if ( $src )
     123                {
     124                    // Make sure we don't propagate this to the HTML Attributes list.
     125                    unset( $atts[ 'src' ] );
     126                    return ( string )( new $class( $src, $atts ) );
     127                }
     128                return '';
     129            };
    143130        }
    144131
    145         // Convert attributes
    146         foreach ( $atts as $attribute_key => $attribute_value )
     132        function PictureShortcodeFunctionGenerator( string $class )
    147133        {
     134            return function ( $atts ) use ( $class )
     135            {
     136                $src = TestHashItemString( $atts, 'src' );
     137                $ext = TestHashItemString( $atts, 'ext' );
     138                $sizes = TestHashItemString( $atts, 'sizes' );
     139                if ( $src && $ext && $sizes )
     140                {
     141                    $atts = TransformShortcodeAttributesToElementAttributes( $atts );
     142                    // Make sure we don't propagate these to the HTML Attributes list.
     143                    unset( $atts[ 'src' ], $atts[ 'ext' ], $atts[ 'sizes' ] );
     144                    return ( string )( new $class( $src, $ext, $sizes, $atts ) );
     145                }
     146                return '';
     147            };
     148        }
     149
     150        function TransformShortcodeAttributesToElementAttributes( array $atts ) : array
     151        {
     152            // Initialize
     153            $element_atts = [];
     154            $prefixes = [];
     155            $prefix_lengths = [];
     156
    148157            foreach ( PICTURE_ELEMENTS as $element )
    149158            {
    150                 $prefix = $prefixes[ $element ];
    151                 $prefix_length = $prefix_lengths[ $element ];
    152                 $starts_with_prefix = ( strpos( $attribute_key, $prefix ) === 0 );
    153                 if ( $starts_with_prefix )
     159                // Where we will put the new versions o' the attributes for each element.
     160                $element_atts[ $element ] = [];
     161                // We need these for the lengths, so we might as well save these.
     162                $prefixes[ $element ] = "{$element}-";
     163                // Optimization: save string lengths so we don't recalculate these for every attribute x element, but can just reference them directly.
     164                $prefix_lengths[ $element ] = strlen( $prefixes[ $element ] );
     165            }
     166
     167            // Convert attributes
     168            foreach ( $atts as $attribute_key => $attribute_value )
     169            {
     170                foreach ( PICTURE_ELEMENTS as $element )
    154171                {
    155                     $attribute_key_without_prefix = substr( $attribute_key, $prefix_length );
    156                     $element_atts[ $element ][ $attribute_key_without_prefix ] = $attribute_value; // Set new version o' attribute.
    157                     unset( $atts[ $attribute_key ] ); // Get rid of ol' version o' attribute.
     172                    $prefix = $prefixes[ $element ];
     173                    $prefix_length = $prefix_lengths[ $element ];
     174                    $starts_with_prefix = ( strpos( $attribute_key, $prefix ) === 0 );
     175                    if ( $starts_with_prefix )
     176                    {
     177                        $attribute_key_without_prefix = substr( $attribute_key, $prefix_length );
     178                        $element_atts[ $element ][ $attribute_key_without_prefix ] = $attribute_value; // Set new version o' attribute.
     179                        unset( $atts[ $attribute_key ] ); // Get rid of ol' version o' attribute.
     180                    }
    158181                }
    159182            }
     183
     184            // Finally, add all new versions o' attributes to original attributes.
     185            foreach ( PICTURE_ELEMENTS as $element )
     186            {
     187                $atts[ "{$element}-attributes" ] = $element_atts[ $element ];
     188            }
     189
     190            return $atts;
    160191        }
    161192
    162         // Finally, add all new versions o' attributes to original attributes.
    163         foreach ( PICTURE_ELEMENTS as $element )
    164         {
    165             $atts[ "{$element}-attributes" ] = $element_atts[ $element ];
    166         }
    167 
    168         return $atts;
     193        const PICTURE_ELEMENTS = [ 'img', 'picture', 'source' ];
    169194    }
    170 
    171     const PICTURE_ELEMENTS = [ 'img', 'picture', 'source' ];
    172 }
Note: See TracChangeset for help on using the changeset viewer.