PHP Classes
elePHPant
Icontem

WP_Register: Minify and unify CSS and JavaScript for WordPress

Recommend this page to a friend!
  Info   View files Documentation   View files View files (33)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-10-09 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 23 All time: 9,071 This week: 132Up
Version License PHP version Categories
wp_register 1.0Custom (specified...5HTML, PHP 5, Content management
Description Author

This package can minify and unify CSS and JavaScript for WordPress.

It can register JavaScript and CSS files to be used in WordPress.

The class can minify these files and even merge them in a single file.

  Performance   Level  
Name: Josantonius is available for providing paid consulting. Contact Josantonius .
Classes: 15 packages by
Country: Spain Spain
Age: ???
All time rank: 252465 in Spain Spain
Week rank: 5 Up1 in Spain Spain Up
Innovation award
Innovation award
Nominee: 5x

Details

PHP WordPress Register

Latest Stable Version Total Downloads Latest Unstable Version License Travis

Versión en español

Register, minify and unify CSS and JavaScript resources in WordPress.

Installation

The preferred way to install this extension is through composer.

To install PHP Wordpress Register library, simply:

$ composer require Josantonius/WP_Register

The previous command will only install the necessary files, if you prefer to download the entire source code (including tests, vendor folder, exceptions not used, docs...) you can use:

$ composer require Josantonius/WP_Register --prefer-source

Or you can also clone the complete repository with Git:

$ git clone https://github.com/Josantonius/WP_Register.git

Requirements

This library is supported by PHP versions 5.6 or higher and is compatible with HHVM versions 3.0 or higher.

To use this library in HHVM (HipHop Virtual Machine) you will have to activate the scalar types. Add the following line "hhvm.php7.scalar_types = true" in your "/etc/hhvm/php.ini".

Quick Start and Examples

To use this class, simply:

require __DIR__ . '/vendor/autoload.php';

use Josantonius\WP_Register\WP_Register;

Available Methods

Available methods in this library:

add()

Add scripts or styles.

WP_Register::add($type, $data);

| Atttribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $type | 'script' or 'style' | string | Yes | |

| Atttribute | key | Description | Type | Required | Default | --- | --- | --- | --- | --- | --- | | $data | | Settings | array | Yes | | | | name | Unique ID | string | Yes | | | | url | Url to file | string | Yes | | | | place | 'admin' or 'front' | string | No | 'front' | | | deps | Dependences | array | No | [] | | | version | Version | string | No | false | | | footer | Only for scripts - Attach in footer | boolean | No | true | | | params | Only for scripts - Params available in JS | array | Yes | [] | | | media | Only for styles - Media | string | No | '' |

@return ? void

unify()

Sets whether to merge the content of files into a single file.

WP_Register::unify($id, $params, $minify);

| Atttribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $id | Action hook name | string | Yes | | | $params | Path urls | mixed | Yes | | | $minify | Minimize file content | boolean | No | false |

@return ? boolean true

isAdded()

Check if a particular style or script has been added to be enqueued.

WP_Register::isAdded($type, $name);

| Atttribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $type | 'script' or 'style' | string | Yes | | | $name | Script or style ID | string | Yes | |

@return ? boolean

remove()

Remove before script or style have been registered.

WP_Register::isAdded($type, $name);

| Atttribute | Description | Type | Required | Default | --- | --- | --- | --- | --- | | $type | 'script' or 'style' | string | Yes | | | $name | Script or style ID | string | Yes | |

@return ? boolean true

Usage

Example of use for this library:

<?php
require __DIR__ . '/vendor/autoload.php';

use Josantonius\WP_Register\WP_Register;

Add script:

WP_Register::add('script', [

    'name'  => 'HTML_script',
    'url'   => 'http://josantonius.com/js/html5.js'
]);

WP_Register::add('script', [

    'name'    => 'NavigationScript',
    'url'     => 'http://josantonius.com/js/navigation.js',
    'place'   => 'admin',
    'deps'    => ['jquery'],
    'version' => '1.1.3',
    'footer'  => true,
    'params'  => ['date' => date('now')],
]);

Additionally, a nonce is created for each script using its name. In this example, it will be accessible from JavaScript using NavigationScript.nonce.

wp_verify_nonce($nonce, 'NavigationScript');

In the case of scripts created from plugins, the path of the plugin directory is saved as a parameter. In this example, it will be accessible from JavaScript using NavigationScript.pluginUrl.

Add style:

WP_Register::add('style', [

    'name'  => 'EditorStyle',
    'url'   => 'http://josantonius.com/js/editor-style.css'
]);

WP_Register::add('style', [

    'name'    => 'DefaultStyle',
    'url'     => 'http://josantonius.com/js/style.css',
    'place'   => 'admin',
    'deps'    => [],
    'version' => '1.1.3',
    'media'   => 'all'
])

Unify:

WP_Register::unify('UniqueID', 'http://josantonius.com/min/');

Unify and minify:

WP_Register::unify('UniqueID', 'http://josantonius.com/min/', true);

Unify specifying different url paths for styles and scripts:

WP_Register::unify('UniqueID', [

    'styles'  => 'http://josantonius.com/min/css/',
    'scripts' => 'http://josantonius.com/min/js/'
]);

Unify and minify specifying different url paths for styles and scripts:

WP_Register::unify('UniqueID', [

    'styles'  => 'http://josantonius.com/min/css/',
    'scripts' => 'http://josantonius.com/min/js/'
]);

Check if a particular style or script has been added to be registered:


WP_Register::isAdded('script', 'HTML_script');

WP_Register::isAdded('script', 'NavigationScript');

WP_Register::isAdded('style', 'EditorStyle');

WP_Register::isAdded('style', 'DefaultStyle');

Remove before script or style have been enqueued:


WP_Register::remove('script', 'HTML_script');

WP_Register::remove('script', 'NavigationScript');

WP_Register::remove('style', 'EditorStyle');

WP_Register::remove('style', 'DefaultStyle');

Tests

To run tests simply:

$ git clone https://github.com/Josantonius/WP_Register.git

$ cd WP_Register

$ bash bin/install-wp-tests.sh wordpress_test root '' localhost latest

$ phpunit

? TODO

  • [ ] Sort dependencies when unifying parameters
  • [x] Create tests
  • [ ] Improve documentation

Contribute

  1. Check for open issues or open a new issue to start a discussion around a bug or feature.
  2. Fork the repository on GitHub to start making your changes.
  3. Write one or more tests for the new feature or that expose the bug.
  4. Make code changes to implement the feature or fix the bug.
  5. Send a pull request to get your changes merged and published.

This is intended for large and long-lived objects.

Repository

All files in this repository were created and uploaded automatically with Reposgit Creator.

License

This project is licensed under MIT license. See the LICENSE file for more info.

Copyright

2017 Josantonius, josantonius.com

If you find it useful, let me know :wink:

You can contact me on Twitter or through my email.

  Files folder image Files  
File Role Description
Files folder imagebin (1 file)
Files folder imagesrc (1 file, 1 directory)
Files folder imagetests (1 file, 1 directory)
Files folder imagevendor (1 file, 2 directories)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONDUCT.md Data Auxiliary data
Accessible without login Plain text file contributors.txt Doc. Documentation
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml.dist Data Auxiliary data
Accessible without login Plain text file README-ES.md Doc. Documentation
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file _config.yml Data Auxiliary data

  Files folder image Files  /  bin  
File Role Description
  Accessible without login Plain text file install-wp-tests.sh Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageWP_Register (1 file)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script

  Files folder image Files  /  src  /  WP_Register  
File Role Description
  Plain text file WP_Register.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imageWP_Register (1 directory)
  Accessible without login Plain text file bootstrap.php Example Example script

  Files folder image Files  /  tests  /  WP_Register  
File Role Description
Files folder imageTest (6 files)

  Files folder image Files  /  tests  /  WP_Register  /  Test  
File Role Description
  Plain text file RegisterAdminScriptsTest.php Class Class source
  Plain text file RegisterAdminStylesTest.php Class Class source
  Plain text file RegisterScriptsTest.php Class Class source
  Plain text file RegisterStylesTest.php Class Class source
  Plain text file UnifyAdminFiles.php Class Class source
  Plain text file UnifyFiles.php Class Class source

  Files folder image Files  /  vendor  
File Role Description
Files folder imagecomposer (8 files)
Files folder imagejosantonius (1 directory)
  Accessible without login Plain text file autoload.php Aux. Auxiliary script

  Files folder image Files  /  vendor  /  composer  
File Role Description
  Accessible without login Plain text file autoload_classmap.php Aux. Auxiliary script
  Accessible without login Plain text file autoload_namespaces.php Aux. Auxiliary script
  Accessible without login Plain text file autoload_psr4.php Aux. Auxiliary script
  Plain text file autoload_real.php Class Class source
  Plain text file autoload_static.php Class Class source
  Plain text file ClassLoader.php Class Class source
  Accessible without login Plain text file installed.json Data Auxiliary data
  Accessible without login Plain text file LICENSE Lic. License text

  Files folder image Files  /  vendor  /  josantonius  
File Role Description
Files folder imagefile (2 files, 1 directory)

  Files folder image Files  /  vendor  /  josantonius  /  file  
File Role Description
Files folder imagesrc (1 directory)
  Accessible without login Plain text file composer.json Data Auxiliary data
  Accessible without login Plain text file LICENSE Lic. License text

  Files folder image Files  /  vendor  /  josantonius  /  file  /  src  
File Role Description
Files folder imageFile (1 file)

  Files folder image Files  /  vendor  /  josantonius  /  file  /  src  /  File  
File Role Description
  Plain text file File.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:23
This week:0
All time:9,071
This week:132Up