Skip to content

Commit

Permalink
Don't alter the todo list
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparsd committed Dec 6, 2015
1 parent 5a9b320 commit 1950450
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions include/minit.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public function init() {
add_filter( 'print_scripts_array', array( $this, 'register_js' ) );
add_filter( 'print_styles_array', array( $this, 'register_css' ) );

// Enqueue all registered Minit styles
// Print our CSS files
add_filter( 'print_styles_array', array( $this, 'minit_css' ), 20 );

// Enqueue all registered Minit scripts in the footer
// Print our JS file
add_filter( 'print_scripts_array', array( $this, 'minit_js' ), 20 );

// Print external scripts asynchronously in the footer
Expand Down Expand Up @@ -96,7 +96,10 @@ function register_assets( $object, $todo ) {
if ( ! in_array( $handle, $this->queue[ $extension ] ) )
$this->queue[ $extension ][] = $handle;

return array_diff( $todo, $this->queue[ $extension ] );
// Mark these as done since we'll take care of them
$object->done = array_merge( $object->done, $this->queue[ $extension ] );

return $todo;

}

Expand Down Expand Up @@ -157,9 +160,9 @@ function minit_assets( $object ) {
'minit_cache_ver-' . get_option( 'minit_cache_ver' ), // Use a global cache version key to purge cache
);

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

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

Expand Down Expand Up @@ -250,8 +253,6 @@ protected function mark_done( $handles, $object ) {
// Mark them as processed by Minit
$this->done[ $extension ] = array_merge( $this->done[ $extension ], $handles );

$object->dequeue( $handles );

}


Expand All @@ -260,12 +261,7 @@ function minit_js( $todo ) {
global $wp_scripts;

// Run this only in the footer
if ( 1 !== $wp_scripts->group )
return $todo;

$queue = $this->get_queue( $wp_scripts );

if ( empty( $queue ) )
if ( 0 !== $wp_scripts->group )
return $todo;

$handle = 'minit-js';
Expand All @@ -276,23 +272,21 @@ function minit_js( $todo ) {
}

// @todo create a fallback for apply_filters( 'minit-js-in-footer', true )
wp_enqueue_script( $handle, $url, null, null, true );
wp_register_script( $handle, $url, null, null, true );

// Add our Minit script since wp_enqueue_script won't do it at this point
$todo[] = $handle;

$done = $this->get_done( $wp_scripts );
$inline_data = array();

// Add inline scripts for all minited scripts
foreach ( $done as $script )
$inline_data[] = $wp_scripts->get_data( $script, 'data' );
foreach ( $done as $script ) {
$inline_js = $wp_scripts->get_data( $script, 'data' );

// Remove empty elements
$inline_data = array_filter( $inline_data );
if ( ! empty( $inline_js ) )
$wp_scripts->add_data( $script, 'data', $inline_js );

if ( ! empty( $inline_data ) )
$wp_scripts->add_data( $handle, 'data', implode( "\n", $inline_data ) );
}

return $todo;

Expand All @@ -316,17 +310,15 @@ function minit_css( $todo ) {
$todo[] = $handle;

$done = $this->get_done( $wp_styles );
$inline_data = array();

// Add inline styles for all minited styles
foreach ( $done as $script )
$inline_data[] = implode( "\n", $wp_styles->get_data( $script, 'after' ) );
foreach ( $done as $script ) {
// Can this return an array instead?
$inline_styles = $wp_styles->get_data( $script, 'after' );

// Remove empty elements
$inline_data = array_filter( $inline_data );

if ( ! empty( $inline_data ) )
$wp_styles->add_inline_style( $handle, implode( "\n", $inline_data ) );
if ( ! empty( $inline_styles ) )
$wp_styles->add_inline_style( $handle, implode( "\n", $inline_styles ) );
}

return $todo;

Expand Down

0 comments on commit 1950450

Please sign in to comment.