Skip to content

Commit

Permalink
Merge pull request #52 from kasparsd/feature/restructure-abstract
Browse files Browse the repository at this point in the history
Implement abstract
  • Loading branch information
kasparsd committed Dec 14, 2015
2 parents 2367ba2 + f107e28 commit 331affd
Show file tree
Hide file tree
Showing 6 changed files with 524 additions and 542 deletions.
76 changes: 4 additions & 72 deletions include/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
add_filter( 'minit-item-css', 'minit_comment_combined', 15, 3 );
add_filter( 'minit-item-js', 'minit_comment_combined', 15, 3 );

function minit_comment_combined( $content, $object, $script ) {
function minit_comment_combined( $content, $object, $handle ) {

if ( ! $content )
return $content;

return sprintf(
"\n\n/* Minit: %s */\n",
$object->registered[ $script ]->src
$object->registered[ $handle ]->src
) . $content;

}
Expand All @@ -36,75 +36,6 @@ function minit_add_toc( $content, $items ) {
}


// Turn all local asset URLs into absolute URLs
add_filter( 'minit-item-css', 'minit_resolve_css_urls', 10, 3 );

function minit_resolve_css_urls( $content, $object, $script ) {

if ( ! $content )
return $content;

$src = Minit::get_asset_relative_path(
$object->base_url,
$object->registered[ $script ]->src
);

// Make all local asset URLs absolute
$content = preg_replace(
'/url\(["\' ]?+(?!data:|https?:|\/\/)(.*?)["\' ]?\)/i',
sprintf( "url('%s/$1')", $object->base_url . dirname( $src ) ),
$content
);

return $content;

}


// Add support for relative CSS imports
add_filter( 'minit-item-css', 'minit_resolve_css_imports', 10, 3 );

function minit_resolve_css_imports( $content, $object, $script ) {

if ( ! $content )
return $content;

$src = Minit::get_asset_relative_path(
$object->base_url,
$object->registered[ $script ]->src
);

// Make all import asset URLs absolute
$content = preg_replace(
'/@import\s+(url\()?["\'](?!https?:|\/\/)(.*?)["\'](\)?)/i',
sprintf( "@import url('%s/$2')", $object->base_url . dirname( $src ) ),
$content
);

return $content;

}


// Exclude styles with media queries from being included in Minit
add_filter( 'minit-item-css', 'minit_exclude_css_with_media_query', 10, 3 );

function minit_exclude_css_with_media_query( $content, $object, $script ) {

if ( ! $content )
return $content;

$whitelist = array( '', 'all', 'screen' );

// Exclude from Minit if media query specified
if ( ! in_array( $object->registered[ $script ]->args, $whitelist ) )
return false;

return $content;

}


// Make sure that all Minit files are served from the correct protocol
add_filter( 'minit-url-css', 'minit_maybe_ssl_url' );
add_filter( 'minit-url-js', 'minit_maybe_ssl_url' );
Expand All @@ -118,6 +49,7 @@ function minit_maybe_ssl_url( $url ) {

}


// Exclude handles that are known to cause problems
add_filter( 'minit-exclude-js', 'minit_exclude_defaults' );

Expand All @@ -127,6 +59,6 @@ function minit_exclude_defaults( $handles ) {
'this-is-a-handle-of-a-script-you-want-to-exclude',
);

return array_merge( $handles, $exclude );
return array_merge( $exclude, $handles );

}
228 changes: 228 additions & 0 deletions include/minit-assets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
<?php

abstract class Minit_Assets {

public $queue = array();
public $done = array();
public $handler;
public $extension;


function __construct( $handler, $extension = null ) {

$this->handler = $handler;

if ( ! empty( $extension ) )
$this->extension = $extension;
else
$this->extension = get_class( $handler );

}


abstract function init();


function register( $todo ) {

if ( empty( $todo ) )
return $todo;

// Allow files to be excluded from Minit
$minit_exclude = apply_filters( 'minit-exclude-' . $this->extension, array() );

if ( ! is_array( $minit_exclude ) )
$minit_exclude = array();

$minit_todo = array_diff( $todo, $minit_exclude );

if ( empty( $minit_todo ) )
return $todo;

foreach ( $minit_todo as $handle )
if ( ! in_array( $handle, $this->queue ) )
$this->queue[] = $handle;

// Mark these as done since we'll take care of them
$this->handler->done = array_merge( $this->handler->done, $this->queue );

return $todo;

}


function minit() {

$done = array();

if ( empty( $this->queue ) )
return false;

// Build a cache key
$ver = array(
'is_ssl-' . is_ssl(), // Use different cache key for SSL and non-SSL
'minit_cache_ver-' . get_option( 'minit_cache_ver' ), // Use a global cache version key to purge cache
);

// Include individual scripts versions in the cache key
foreach ( $this->queue as $handle )
$ver[] = sprintf( '%s-%s', $handle, $this->handler->registered[ $handle ]->ver );

$cache_ver = md5( 'minit-' . implode( '-', $ver ) );

// Try to get queue from cache
//$cache = get_transient( 'minit-' . $cache_ver );

if ( ! empty( $cache ) && isset( $cache['url'] ) ) {
$this->mark_done( $cache['done'] );

return $cache['url'];
}

foreach ( $this->queue as $handle ) {

// Ignore pseudo packages such as jquery which return src as empty string
if ( empty( $this->handler->registered[ $handle ]->src ) )
$done[ $handle ] = null;

// Get the relative URL of the asset
$src = $this->get_asset_relative_path( $handle );

// Skip if the file is not hosted locally
if ( empty( $src ) || ! file_exists( ABSPATH . $src ) )
continue;

$item = $this->minit_item( file_get_contents( ABSPATH . $src ), $handle, $src );

$item = apply_filters(
'minit-item-' . $this->extension,
$item,
$this->handler,
$handle
);

if ( false !== $item )
$done[ $handle ] = $item;

}

if ( empty( $done ) )
return false;

$this->mark_done( array_keys( $done ) );

$wp_upload_dir = wp_upload_dir();

// Try to create the folder for cache
if ( ! is_dir( $wp_upload_dir['basedir'] . '/minit' ) )
if ( ! mkdir( $wp_upload_dir['basedir'] . '/minit' ) )
return false;

$combined_file_path = sprintf( '%s/minit/%s.%s', $wp_upload_dir['basedir'], $cache_ver, $this->extension );
$combined_file_url = sprintf( '%s/minit/%s.%s', $wp_upload_dir['baseurl'], $cache_ver, $this->extension );

// Allow other plugins to do something with the resulting URL
$combined_file_url = apply_filters( 'minit-url-' . $this->extension, $combined_file_url, $done );

// Allow other plugins to minify and obfuscate
$done_imploded = apply_filters( 'minit-content-' . $this->extension, implode( "\n\n", $done ), $done );

// Store the combined file on the filesystem
if ( ! file_exists( $combined_file_path ) )
if ( ! file_put_contents( $combined_file_path, $done_imploded ) )
return false;

// Cache this set of scripts, by default for 24 hours
$cache_ttl = apply_filters( 'minit-cache-expiration', 24 * 60 * 60 );
$cache_ttl = apply_filters( 'minit-cache-expiration-' . $this->extension, $cache_ttl );

$result = array(
'done' => array_keys( $done ),
'url' => $combined_file_url,
'file' => $combined_file_path,
);

set_transient( 'minit-' . $cache_ver, $result, $cache_ttl );

return $combined_file_url;

}


abstract function process( $todo );


function minit_item( $source, $handle, $src ) {

return $source;

}


protected function mark_done( $handles ) {

// Remove processed items from the queue
$this->queue = array_diff( $this->queue, $handles );

// Mark them as processed by Minit
$this->done = array_merge( $this->done, $handles );

}


protected function get_asset_relative_path( $handle ) {

if ( ! isset( $this->handler->registered[ $handle ] ) )
return false;

$item_url = $this->handler->registered[ $handle ]->src;

if ( empty( $item_url ) )
return false;

// Remove protocol reference from the local base URL
$base_url = preg_replace( '/^(https?:)/i', '', $this->handler->base_url );

// Check if this is a local asset which we can include
$src_parts = explode( $base_url, $item_url );

if ( empty( $src_parts ) )
return false;

// Get the trailing part of the local URL
$maybe_relative = array_pop( $src_parts );

if ( file_exists( ABSPATH . $maybe_relative ) )
return $maybe_relative;

return false;

}


public static function cache_bump() {

// Use this as a global cache version number
update_option( 'minit_cache_ver', time() );

// Allow other plugins to know that we purged
do_action( 'minit-cache-purged' );

}


public static function cache_delete() {

$wp_upload_dir = wp_upload_dir();
$minit_files = glob( $wp_upload_dir['basedir'] . '/minit/*' );

if ( $minit_files ) {
foreach ( $minit_files as $minit_file ) {
unlink( $minit_file );
}
}

}


}
Loading

0 comments on commit 331affd

Please sign in to comment.