Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Enqueue.php #45

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 85 additions & 19 deletions Herbert/Framework/Enqueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,38 +49,73 @@ public function buildInclude($attrs, $footer)
{
$filterBy = key($attrs['filter']);
$filterWith = reset($attrs['filter']);

if (!is_array($filterWith))
$filterWith = $this->check_array($filterWith);
if (!$this->filterBy($filterBy, $attrs, $filterWith))
{
$filterWith = [$filterWith];
return;
}

if (!$this->filterBy($filterBy, $attrs, $filterWith))

//subpanel filter
$filterWithSub = next($attrs['filter']);
$filterWithSub = $this->check_array($filterWithSub);
$attrs['enqueue_panel'] = array_key_exists('subPanel', $attrs['filter']) === true ? true : false; // if panel define or not defined
if (!$this->filterBy('subpanel', $attrs, $filterWithSub))
{
return;
}
}

$deps = [];
if( isset( $attrs['deps'] ) ){
$deps = (array)$attrs['deps'];
}
$ver = false;
if( isset( $attrs['ver'] ) ){
$ver = (int) $attrs['ver'];
}


// if (substr($attrs['src'], 0, 2) !== "//")
// {
// $attrs['src'] = ltrim($attrs['src'], '/');
// }

if (pathinfo($attrs['src'], PATHINFO_EXTENSION) === 'css')
if ( isset( $attrs['src'] ) && pathinfo($attrs['src'], PATHINFO_EXTENSION) === 'css')
{
wp_enqueue_style($attrs['as'], $attrs['src']);
wp_enqueue_style($attrs['as'], $attrs['src'], $deps, $ver);
}
else if( isset( $attrs['lc'] ) && !empty($attrs['lc']) ){
wp_localize_script( $attrs['as'], $attrs['name'], $this->get_additional_data($attrs['data']) );
}
else
{
wp_enqueue_script($attrs['as'], $attrs['src'], [], false, $footer);

if(isset($attrs['localize'])) {
wp_localize_script( $attrs['as'], $attrs['as'], $attrs['localize'] );
}
wp_enqueue_script($attrs['as'], $attrs['src'], $deps, $ver, $footer);
}
}

/**
* Get additional Constant data
*
* @param type $data
* @return type
*/
private function get_additional_data( $data ){
$page = $this->app['http']->get('page', false);
return array_merge(array(
'base_url' => admin_url( "admin.php?page={$page}" )
), $data);
}

/**
* Check Data is is array
*
* @param type $data
* @return array
*/
private function check_array( $filterWith ){
if (!is_array($filterWith))
{
return [$filterWith];
}
else return $filterWith;
}

/**
* Filters by a specific filter.
*
* @param $by
Expand Down Expand Up @@ -188,7 +223,6 @@ public function filterPanel($attrs, $filterWith)
{
$panels = $this->app['panel']->getPanels();
$page = $this->app['http']->get('page', false);

if (!$page && function_exists('get_current_screen'))
{
$page = object_get(get_current_screen(), 'id', $page);
Expand All @@ -208,6 +242,38 @@ public function filterPanel($attrs, $filterWith)

return false;
}

/**
* Filter Subpanel. Subpanel slug should be 'tab=subpanel'
*
* @param type $attrs
* @param type $filterWith
* @return boolean
*/
public function filterSubpanel($attrs, $filterWith){
$tab = $this->app['http']->get('tab', false);
// foreach ($filterWith as $filter) {
// if( str_is( $filter, $tab ) ){
// return true;
// }
// }
//check all *
$getFirstTab = trim($filterWith[0]);
if(str_is( $getFirstTab, '*')){
return true;
}

if(in_array($tab, $filterWith, true)){
return true;
}

// page has no subpanel but script enqueued
if( empty( $tab) && $attrs['enqueue_panel'] === true){
return true;
}

return false;
}

/**
* Filter by Page, if '*' is provided then
Expand Down Expand Up @@ -327,7 +393,7 @@ public function filterSearch($attrs, $filterWith)
*/
public function filterPostType($attrs, $filterWith)
{
return array_search(get_post_type(), $filterWith) !== FALSE;
return array_search(get_post_type(), $filterWith) !== null;
}

}