Plugin Directory

Changeset 2082860


Ignore:
Timestamp:
05/07/2019 09:03:55 PM (7 years ago)
Author:
waughjai
Message:

Add Ability to make mo' complex registration functions for sheet managers & ability to easily register absolute URLs.

Location:
waj-scripts
Files:
158 added
14 edited

Legend:

Unmodified
Added
Removed
  • waj-scripts/trunk/composer.json

    r1995163 r2082860  
    1818    },
    1919    "require-dev": {
    20         "phpunit/phpunit": "6.*"
     20        "phpunit/phpunit": "*"
    2121    },
    2222    "autoload": {
  • waj-scripts/trunk/readme.txt

    r2058442 r2082860  
    33Tags: scripts
    44Requires at least: 5.0.0
    5 Tested up to: 5.1
    6 Stable tag: 1.0.2
     5Tested up to: 5.1.1
     6Stable tag: 1.1.0
    77Requires PHP: 7.0
    88License: GPLv2 or later
     
    3838== Changelog ==
    3939
     40= 1.1.0 =
     41* Add Ability to make mo' complex registration functions for sheet managers & ability to easily register absolute URLs.
     42
    4043= 1.0.2 =
    4144* Test in WordPress 5.1
     
    4447* Fix bug causing meta boxes to try loading empty filename for empty meta boxes.
    4548
    46 = 1.0 =
     49= 1.0.0 =
    4750* Initial stable version.
  • waj-scripts/trunk/src/WPScripts.php

    r1995163 r2082860  
    2727                    'Page JavaScript'
    2828                ),
    29                 'main_js',
    30                 'Main JS',
     29                new WPScriptThemeOption( 'main_js', 'Main JS' ),
    3130                'wp_footer'
    3231            );
     
    3433            self::$no_jquery_checkbox = new WPThemeOption
    3534            (
    36                 WPSheetManager::getWPThemeOptionSection(),
     35                WPScriptThemeOption::getSection(),
    3736                'remove_jquery',
    38                 '¿Remove jQuery?',
     37                'Remove jQuery?',
    3938                [ 'input_type' => 'checkbox' ]
    4039            );
     
    5655        {
    5756            self::$sheet_manager->register( $name, self::getWPHook( $load_in_header ) );
     57        }
     58
     59        public static function registerRaw( string $name, string $src, bool $load_in_header = false, string $version = null ) : void
     60        {
     61            self::$sheet_manager->registerRaw( $name, $src, self::getWPHook( $load_in_header ), $version );
     62        }
     63
     64        public static function addRegistrator( callable $function, bool $load_in_header = false ) : void
     65        {
     66            self::$sheet_manager->addRegistrator( $function, self::getWPHook( $load_in_header ) );
    5867        }
    5968
  • waj-scripts/trunk/src/WPSheetManager.php

    r1996660 r2082860  
    66    use WaughJ\FileLoader\FileLoader;
    77    use WaughJ\WPMetaBox\WPMetaBox;
    8     use WaughJ\WPThemeOption\WPThemeOption;
    9     use WaughJ\WPThemeOption\WPThemeOptionsPage;
    10     use WaughJ\WPThemeOption\WPThemeOptionsPageManager;
    11     use WaughJ\WPThemeOption\WPThemeOptionsSection;
    128
    139    class WPSheetManager
    1410    {
    15         public function __construct( FileLoader $loader, string $wp_action, WPMetaBox $meta_box, string $option_slug, string $option_name, string $default_wp_hook = 'wp_enqueue_scripts' )
     11        public function __construct( FileLoader $loader, string $wp_action, WPMetaBox $meta_box, WPScriptThemeOption $option, string $default_wp_hook = 'wp_enqueue_scripts' )
    1612        {
    1713            $this->loader = $loader;
     
    1915            $this->meta_box = $meta_box;
    2016            $this->default_wp_hook = $default_wp_hook;
    21 
    22             $section = self::getWPThemeOptionSection();
    23             $this->option = new WPThemeOption
    24             (
    25                 $section,
    26                 $option_slug,
    27                 $option_name
    28             );
     17            $this->option = $option;
    2918
    3019            add_action
     
    3322                function()
    3423                {
    35                     $main_sheet = $this->option->getOptionValue();
     24                    $main_sheet = $this->option->getValue();
    3625                    if ( $main_sheet !== '' )
    3726                    {
     
    5948        public function register( string $name, string $wp_hook = null ) : void
    6049        {
    61             if ( !$wp_hook )
    62             {
    63                 $wp_hook = $this->default_wp_hook;
    64             }
    65             add_action( $wp_hook, self::generateRegistrar( $name ) );
     50            add_action( $this->getHook( $wp_hook ), self::generateRegistrar( $name ) );
     51        }
     52
     53        public function registerRaw( string $name, string $src, string $wp_hook = null, string $version = null ) : void
     54        {
     55            add_action
     56            (
     57                $this->getHook( $wp_hook ),
     58                function() use ( $name, $src, $version )
     59                {
     60                    call_user_func( $this->wp_action, $name, $src, [], $version );
     61                }
     62            );
     63        }
     64
     65        public function addRegistrator( callable $function, string $wp_hook = null ) : void
     66        {
     67            add_action
     68            (
     69                $this->getHook( $wp_hook ),
     70                function() use ( $function )
     71                {
     72                    $sheets = $function();
     73                    foreach ( $sheets as $sheet )
     74                    {
     75                        $this->enqueue( $sheet );
     76                    }
     77                }
     78            );
    6679        }
    6780
     
    7487        {
    7588            return ( string )( $this->loader->getVersion( $name ) );
    76         }
    77 
    78         public static function getWPThemeOptionSection() : WPThemeOptionsSection
    79         {
    80             if ( self::$theme_options_section === null )
    81             {
    82                 if ( self::$theme_options_page === null )
    83                 {
    84                     self::$theme_options_page = WPThemeOptionsPageManager::initializeIfNotAlreadyInitialized( 'directories', 'Directories' );
    85                 }
    86                 self::$theme_options_section = new WPThemeOptionsSection( self::$theme_options_page, 'main_scripts', 'Main Scripts' );
    87             }
    88             return self::$theme_options_section;
    8989        }
    9090
     
    102102        }
    103103
     104        private function getHook( string $hook = null ) : string
     105        {
     106            return ( $hook ) ? $hook : $this->default_wp_hook;
     107        }
     108
    104109        private $loader;
    105110        private $wp_action;
     
    107112        private $default_wp_hook;
    108113        private $option;
    109 
    110         private static $theme_options_page = null;
    111         private static $theme_options_section = null;
    112114    }
    113115}
  • waj-scripts/trunk/src/WPStylesheets.php

    r1995163 r2082860  
    2626                        'Page Stylesheets'
    2727                    ),
    28                 'main_css',
    29                 'Main CSS'
     28                new WPScriptThemeOption( 'main_css', 'Main CSS' )
    3029            );
    3130        }
     
    3635        }
    3736
     37        public static function registerRaw( string $name, string $src, string $version = null ) : void
     38        {
     39            self::$sheet_manager->registerRaw( $name, $src, 'wp_enqueue_scripts', $version );
     40        }
     41
     42        public static function addRegistrator( callable $function ) : void
     43        {
     44            self::$sheet_manager->addRegistrator( $function, 'wp_enqueue_scripts' );
     45        }
     46
    3847        private static $sheet_manager;
    3948    }
  • waj-scripts/trunk/tests/MockWordPress.php

    r1995163 r2082860  
    4848        [
    4949            'name' => $name,
    50             'url' => $url,
     50            'url' => $url . ( ( $version ) ? '?m=' . $version : '' ),
    5151            'version' => $version
    5252        ];
     
    5959        [
    6060            'name' => $name,
    61             'url' => $url,
     61            'url' => $url . ( ( $version ) ? '?m=' . $version : '' ),
    6262            'version' => $version
    6363        ];
     
    7373        if ( array_key_exists( $name, $enqueued_stylesheets ) )
    7474        {
    75             return $enqueued_stylesheets[ $name ][ 'url' ] . '?m=' . $enqueued_stylesheets[ $name ][ 'version' ];
     75            return $enqueued_stylesheets[ $name ][ 'url' ];
    7676        }
    7777        return null;
     
    8383        if ( array_key_exists( $name, $enqueued_scripts ) )
    8484        {
    85             return $enqueued_scripts[ $name ][ 'url' ] . '?m=' . $enqueued_scripts[ $name ][ 'version' ];
     85            return $enqueued_scripts[ $name ][ 'url' ];
    8686        }
    8787        return null;
  • waj-scripts/trunk/tests/WPScriptsTest.php

    r1995163 r2082860  
    2020        $this->assertEquals( get_script_action( 'jquery' ), 'wp_enqueue_scripts' );
    2121    }
     22
     23    public function testAddRegistrator()
     24    {
     25        WPScripts::addRegistrator
     26        (
     27            function() : array
     28            {
     29                return ( true ) ? [ 'page' ] : [ 'nopage' ];
     30            },
     31            true
     32        );
     33        $this->assertTrue( is_script_registered( 'page' ) );
     34        $this->assertFalse( is_script_registered( 'nopage' ) );
     35        $this->assertEquals( get_script_action( 'page' ), 'wp_enqueue_scripts' );
     36    }
     37
     38    public function testRegisterRaw()
     39    {
     40        WPScripts::registerRaw( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js' );
     41        $this->assertTrue( is_script_registered( 'jquery' ) );
     42        $this->assertEquals( get_script_url( 'jquery' ), 'https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js' );
     43    }
    2244}
  • waj-scripts/trunk/tests/WPStylesheetsTest.php

    r1995163 r2082860  
    1717        $this->assertEquals( get_stylesheet_url( 'main' ), 'https://www.example.com/css/main.css?m=' . filemtime( getcwd() . '/tests/css/main.css' ) );
    1818    }
     19
     20    public function testAddRegistrator()
     21    {
     22        WPStylesheets::addRegistrator
     23        (
     24            function() : array
     25            {
     26                return ( true ) ? [ 'page' ] : [ 'nopage' ];
     27            }
     28        );
     29        $this->assertTrue( is_stylesheet_registered( 'page' ) );
     30        $this->assertFalse( is_stylesheet_registered( 'nopage' ) );
     31        $this->assertEquals( get_stylesheet_url( 'page' ), 'https://www.example.com/css/page.css?m=' . filemtime( getcwd() . '/tests/css/page.css' ) );
     32    }
     33
     34    public function testRegisterRaw()
     35    {
     36        WPStylesheets::registerRaw( 'rubik', 'https://fonts.googleapis.com/css?family=Rubik:400,500,700' );
     37        $this->assertTrue( is_stylesheet_registered( 'rubik' ) );
     38        $this->assertEquals( get_stylesheet_url( 'rubik' ), 'https://fonts.googleapis.com/css?family=Rubik:400,500,700' );
     39        WPStylesheets::registerRaw( 'roboto', 'https://fonts.googleapis.com/css?family=Roboto', '20190507' );
     40        $this->assertTrue( is_stylesheet_registered( 'roboto' ) );
     41        $this->assertEquals( get_stylesheet_url( 'roboto' ), 'https://fonts.googleapis.com/css?family=Roboto?m=20190507' );
     42    }
    1943}
  • waj-scripts/trunk/vendor/composer/ClassLoader.php

    r1995163 r2082860  
    280280    public function setApcuPrefix($apcuPrefix)
    281281    {
    282         $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
     282        $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null;
    283283    }
    284284
     
    378378            while (false !== $lastPos = strrpos($subPath, '\\')) {
    379379                $subPath = substr($subPath, 0, $lastPos);
    380                 $search = $subPath.'\\';
     380                $search = $subPath . '\\';
    381381                if (isset($this->prefixDirsPsr4[$search])) {
    382382                    $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1);
  • waj-scripts/trunk/vendor/composer/installed.json

    r2058442 r2082860  
    152152    {
    153153        "name": "waughj/directory",
    154         "version": "0.2.0",
    155         "version_normalized": "0.2.0.0",
     154        "version": "0.3.0",
     155        "version_normalized": "0.3.0.0",
    156156        "source": {
    157157            "type": "git",
    158158            "url": "https://github.com/waughjai/directory.git",
    159             "reference": "b95e2acbb9e62c47332f8736b3325330c45b8d55"
    160         },
    161         "dist": {
    162             "type": "zip",
    163             "url": "https://api.github.com/repos/waughjai/directory/zipball/b95e2acbb9e62c47332f8736b3325330c45b8d55",
    164             "reference": "b95e2acbb9e62c47332f8736b3325330c45b8d55",
     159            "reference": "ee52acc01d4660a383337b2bf761af2bd153a084"
     160        },
     161        "dist": {
     162            "type": "zip",
     163            "url": "https://api.github.com/repos/waughjai/directory/zipball/ee52acc01d4660a383337b2bf761af2bd153a084",
     164            "reference": "ee52acc01d4660a383337b2bf761af2bd153a084",
    165165            "shasum": ""
    166166        },
     
    170170        },
    171171        "require-dev": {
    172             "phpunit/phpunit": "6.*"
    173         },
    174         "time": "2018-11-27T17:55:13+00:00",
     172            "phpunit/phpunit": "*"
     173        },
     174        "time": "2019-04-18T20:50:42+00:00",
    175175        "type": "libraryt",
    176176        "installation-source": "dist",
  • waj-scripts/trunk/vendor/waughj/directory/composer.json

    r1995163 r2082860  
    1616    },
    1717    "require-dev": {
    18         "phpunit/phpunit": "6.*"
     18        "phpunit/phpunit": "*"
    1919    },
    2020    "autoload": {
  • waj-scripts/trunk/vendor/waughj/directory/src/Directory.php

    r1995163 r2082860  
    88    class Directory
    99    {
    10         public function __construct( $directories )
     10        public function __construct( $directories, $protocol = null )
    1111        {
    1212            if ( is_array( $directories ) )
    1313            {
    14                 $this->directories = $directories;
     14                $this->directories = self::breakDownDirectoryString( implode( '/', $directories ) );
    1515            }
    1616            else if ( is_string( $directories ) )
     
    3434                throw new \Exception( "Invalid type: " . gettype( $directories ) );
    3535            }
     36
     37            if ( $protocol )
     38            {
     39                $this->protocol = $protocol;
     40            }
     41            else
     42            {
     43                $matches = [];
     44                preg_match( '/^([a-z]+):/', $this->directories[ 0 ], $matches );
     45                if ( count( $matches ) > 1 )
     46                {
     47                    $this->protocol = $matches[ 1 ];
     48                    array_shift( $this->directories );
     49                }
     50                else
     51                {
     52                    $this->protocol = null;
     53                }
     54            }
    3655        }
    3756
     
    4665            return
    4766                ( ( $settings->get( 'starting-slash' ) ) ? $settings->get( 'divider' ) : '' ) .
     67                $this->getFormattedProtocol() .
    4868                implode( $settings->get( 'divider' ), $this->directories ) .
    4969                ( ( $settings->get( 'ending-slash' ) ) ? $settings->get( 'divider' ) : '' );
     
    5777        public function getStringURL() : string
    5878        {
    59             return implode( '/', $this->directories );
     79            return $this->getString( [ 'starting-slash' => false, 'ending-slash' => false, 'divider' => '/' ] );
    6080        }
    6181
     
    7595        {
    7696            $directory = new Directory( $directory );
    77             return new Directory( array_merge( $this->directories, $directory->getDirectoryChain() ) );
     97            return new Directory( array_merge( $this->directories, $directory->getDirectoryChain() ), $this->protocol );
    7898        }
    7999
     
    87107                // Remove last directory o' list ( since this function mutates array, we need to use a copy )
    88108                array_pop( $new_array );
    89                 return new Directory( $new_array );
     109                return new Directory( $new_array, $this->protocol );
    90110            }
    91             return new Directory( '/' );
     111            return new Directory( '/', $this->protocol );
    92112        }
    93113
     
    100120            }
    101121            return "/";
     122        }
     123
     124        private function getFormattedProtocol() : string
     125        {
     126            return ( $this->protocol !== null ) ? $this->protocol . '://' : '';
    102127        }
    103128
     
    118143                foreach ( $directory_list as $single_directory )
    119144                {
    120                     array_push( $directories, $single_directory );
     145                    if ( !empty( $single_directory ) )
     146                    {
     147                        array_push( $directories, $single_directory );
     148                    }
    121149                }
    122150            }
     
    125153
    126154        private $directories;
     155        private $protocol;
    127156
    128157        const DEFAULT_ARGUMENTS =
  • waj-scripts/trunk/vendor/waughj/directory/tests/DirectoryTest.php

    r1995163 r2082860  
    9595        $this->assertEquals( $dir1, new Directory([ 'var', 'www', 'html' ]) );
    9696    }
     97
     98    public function testExcessSlashes()
     99    {
     100        $dir = new Directory([ '/en.wikipedia.org/', '//w/index.php?title=PHP&action=edit&section=6' ]);
     101        $this->assertEquals( 'en.wikipedia.org/w/index.php?title=PHP&action=edit&section=6', $dir->getStringURL() );
     102    }
    97103}
  • waj-scripts/trunk/waj-scripts.php

    r2058442 r2082860  
    55    Plugin URI:   https://github.com/waughjai/waj-scripts
    66    Description:  WordPress plugin for easily adding CSS stylesheets & JavaScript files.
    7     Version:      1.0.2
     7    Version:      1.1.0
    88    Author:       Jaimeson Waugh
    99    Author URI:   https://www.jaimeson-waugh.com
     
    1616    {
    1717        require_once( 'vendor/autoload.php' );
    18 
    1918        WPStylesheets::init();
    2019        WPScripts::init();
Note: See TracChangeset for help on using the changeset viewer.