Skip to content

Instantly share code, notes, and snippets.

@RalfAlbert
Created December 16, 2012 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RalfAlbert/4313448 to your computer and use it in GitHub Desktop.
Save RalfAlbert/4313448 to your computer and use it in GitHub Desktop.
A class to safely registering scripts in WordPress
<?php
/**
* Safely registering scripts without script collisions
* @package WordPress
* @author Ralf Albert
*
*/
class Safe_Registering_Scripts
{
/**
* Set to true if registered scripts should be overwritten
* @var bool
*/
public $overwrite_scripts = false;
/**
* Scripts to be registered
* @var unknown
*/
public $scripts = array();
/**
* Array with scripts that have to be registered
* @var array
*/
public $todo_handles = array();
/**
* Scripts to be unregistered when scripts should be overwritten
* @var array
*/
public $deregister_handles = array();
/**
* Constructor setup the arrays and start the tests
* @param array $scripts Scripts to be registered
* @param bool $overwrite Flag if a already registered handle should be overwritten by a handle with the same name
*/
public function __construct( $scripts = array(), $overwrite = false ) {
$this->scripts = $scripts;
$this->overwrite_scripts = $overwrite;
$this->check_scripts();
$this->register_scripts();
}
/**
* Create an array with all handle and sources that can be safely registered without any collision
* @param array $scripts Scripts to be registered if not already set by constructor
* @return array $todo_handles Array with all handles and data that can be safely registered
*/
public function check_scripts( $scripts = array() ) {
global $wp_scripts;
if ( ! empty( $scripts ) )
$this->scripts = array_merge( $this->scripts, $scripts );
$registered_sources = array();
// copy all registered sources to an array
foreach ( $wp_scripts->registered as $script )
$registered_sources[] = $this->extract_source_filename( $script->src );
// check if our source is already registered
foreach ( $this->scripts as $handle => $data ){
// if no source for this handle is available, continue with the next script
if( isset( $data['src'] ) && ! empty( $data['src'] ) )
$src = $this->extract_source_filename( $data['src'] );
else
continue;
// add the handle and data to the todo-array if the script source isn't already registered
if ( ! in_array( $src, $registered_sources ) ) {
$this->todo_handles[ $handle ] = $data;
// remove the handle and data from the todo-array if a handle with the same name is already registered
// and scripts should not be overwritten
if ( false === $this->overwrite_scripts && isset( $wp_scripts->registered[ $handle ] ) )
unset( $this->todo_handles[ $handle ] );
else
$this->deregister_handles[] = $handle;
}
}
return $this->todo_handles;
}
/**
* Registering the tested scripts
* @return array $res Array with the result of each handle (true if the script was successfully registered, else false)
*/
public function register_scripts() {
// deregister registered handles first before re-register this handle
if ( true === $this->overwrite_scripts )
$this->deregister_scripts();
// registering the scripts
foreach ( $this->todo_handles as $handle => $data ) {
$res[ $handle ] = wp_register_script(
$handle,
$data['src'],
$data['deps'],
$data['version'],
$data['in_footer']
);
}
return $res;
}
/**
* Deregister registered scripts
* @return array Result array handle->success [bool]
*/
public function deregister_scripts() {
if ( ! empty( $this->deregister_handles ) && true === $this->overwrite_scripts ) {
foreach ( $this->deregister_handles as $handle )
$res[ $handle ] = wp_deregister_script( $handle );
}
return $res;
}
/**
* Extracting the filename from a give ressource
* @param string $src Complete ressource
* @return string Extracted filename of the ressource
*/
public function extract_source_filename( $src = '' ){
$src_parts = parse_url( $src );
return ( isset( $src_parts['path'] ) && ! empty( $src_parts['path'] ) ) ?
array_pop( explode( '/', $src_parts['path'] ) ):
$src;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment