From 74f26b151dd04ed6bea836dc81cbb9ca1e97fb70 Mon Sep 17 00:00:00 2001 From: Rui Cruz Date: Fri, 31 Jul 2015 12:08:03 +0100 Subject: [PATCH 1/3] Support for script dependencies Implements script dependencies through the use: ``` $enqueue->front([ 'as' => 'ShirtCreatorInterface', 'src' => Helper::assetUrl('/js/app.js'), 'dep' => ['jquery'], 'filter' => [ 'page' => '*' ] ]); ``` --- Herbert/Framework/Enqueue.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Herbert/Framework/Enqueue.php b/Herbert/Framework/Enqueue.php index 4602740..e031aa9 100644 --- a/Herbert/Framework/Enqueue.php +++ b/Herbert/Framework/Enqueue.php @@ -66,13 +66,15 @@ public function buildInclude($attrs, $footer) // $attrs['src'] = ltrim($attrs['src'], '/'); // } + if (!isset($attrs['dep'])) $attrs['dep'] = []; + if (pathinfo($attrs['src'], PATHINFO_EXTENSION) === 'css') { wp_enqueue_style($attrs['as'], $attrs['src']); } else { - wp_enqueue_script($attrs['as'], $attrs['src'], [], false, $footer); + wp_enqueue_script($attrs['as'], $attrs['src'], $attrs['dep'], false, $footer); } } From c171b63cbfa96963567faa3e3667c29e32ddb8ab Mon Sep 17 00:00:00 2001 From: Rui Cruz Date: Sun, 9 Aug 2015 18:02:58 +0100 Subject: [PATCH 2/3] Update Enqueue.php Improve dependency attributes fetching for enqueue. --- Herbert/Framework/Enqueue.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Herbert/Framework/Enqueue.php b/Herbert/Framework/Enqueue.php index e031aa9..3ffe5ba 100644 --- a/Herbert/Framework/Enqueue.php +++ b/Herbert/Framework/Enqueue.php @@ -66,15 +66,13 @@ public function buildInclude($attrs, $footer) // $attrs['src'] = ltrim($attrs['src'], '/'); // } - if (!isset($attrs['dep'])) $attrs['dep'] = []; - if (pathinfo($attrs['src'], PATHINFO_EXTENSION) === 'css') { wp_enqueue_style($attrs['as'], $attrs['src']); } else { - wp_enqueue_script($attrs['as'], $attrs['src'], $attrs['dep'], false, $footer); + wp_enqueue_script($attrs['as'], $attrs['src'], (array) array_get($attrs, 'dep', []), false, $footer); } } From 6c22099283258c2d9af0cd99eb3f65f7f5bb12dd Mon Sep 17 00:00:00 2001 From: Rui Cruz Date: Sun, 9 Aug 2015 18:07:50 +0100 Subject: [PATCH 3/3] Adds support for script localizing Changes the way scripts are called now using wp_register_script first in order to localize scripts. Ex: ``` $enqueue->front([ 'as' => 'MyScript', 'src' => Helper::assetUrl('/js/app.js'), 'dep' => 'jquery', 'filter' => [ 'page' => '*' ], 'localize' => array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ]); ``` --- Herbert/Framework/Enqueue.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Herbert/Framework/Enqueue.php b/Herbert/Framework/Enqueue.php index 3ffe5ba..ad86461 100644 --- a/Herbert/Framework/Enqueue.php +++ b/Herbert/Framework/Enqueue.php @@ -72,7 +72,13 @@ public function buildInclude($attrs, $footer) } else { - wp_enqueue_script($attrs['as'], $attrs['src'], (array) array_get($attrs, 'dep', []), false, $footer); + wp_register_script( $attrs['as'], $attrs['src'], (array) array_get($attrs, 'dep', []), false, $footer ); + + if (isset($attrs['localize'])) { + wp_localize_script( $attrs['as'],$attrs['as'], $attrs['localize'] ); + } + + wp_enqueue_script($attrs['as']); } }