GetSimple Support Forum

Full Version: Function request: queue_script/style 'local' parameter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I keep having trouble due to plugins not loading assets only for their page.
This is because the queue_script and queue_style functions by default load the scripts on any page (which is good, because some plugins offer cross-tab/ front-end functionality). Ideally all plugin devs would be conscious of this, but as that isn't the case, I'd like to request either a new parameter

Consequences are that JS scripts will throw errors, and CSS from plugins messes up CSS from other plugins/ pages.
For example, the excellent ItemManager plugin loads a jquery-ui theme, and messes up the lay-out of the GS Custom Settings datepicker (look at the icons in the orange bar): 

[Image: iXbQCZ4.png]

The simple solution is to modify the queue_ functions in plugin_functions.php:

PHP Code:
<?php 
function queue_style($handle,$where=1,$localID=NULL){
  global $GS_styles$id;
 
 if (array_key_exists($handle$GS_styles)){
 
    if (!$localID || (($id === $localID) || (is_array($localID) && array_search($id$localID) !== FALSE))) { 
        $GS_styles
[$handle]['load']=true;
 
       $GS_styles[$handle]['where']=$GS_styles[$handle]['where'] | $where;
 
    }
  }
?>


With the above solution, one could queue scripts/styles as follows:
PHP Code:
<?php 
  queue_style
('jqui'GSBACK);                 // load on all backend pages
  
queue_style('jqui'GSBACK'imanager');     // only load on backend page with id === 'imanager'
  
$pages = array('imanager','custom_settings');// load on all backend pages with id's in the array
  
queue_style('jqui'GSBACK$pages); 
?>

Alternatively, the $where parameter could be modified to hold id string/ array as well; eg:

PHP Code:
function get_styles_backend(){
    global 
$GS_styles$id;
    foreach (
$GS_styles as $style){
        if ((
$style['where'] && GSBACK && is_numeric($style['where'])) || (($style['where'] === $id) || (is_array($style['where']) && array_search($id$style['where']) !== FALSE))) {
                if (
$style['load']==TRUE){
                    echo 
"\t".'<link href="'.$style['src'].'?v='.$style['ver'].'" rel="stylesheet" media="'.$style['media']."\">\n";
                }
        }
    }



Usage as follows:
PHP Code:
<?php 
  queue_style
('jqui'GSBACK);                 // load on all backend pages
  
queue_style('jqui''imanager');     // only load on backend page with id === 'imanager'
  
$pages = array('imanager','custom_settings');// load on all backend pages with id's in the array
  
queue_style('jqui'$pages); 
?>
I am not sure this is the proper solution.

bad plugins are bad plugins, there is no reason they even need to use asset queues, they could just output their assets whenever they want , they should be written properly to begin with.

we could however make this easier, by making a wrapper function to do this, so authors do not have to wrap their queue calls in a get_filename_id block.

queue_style_conditional($id,GSBACK,$condition)
I am not sure either it's the proper solution and I agree they should write it properly, but in practice... Anyway I don't really mind the implementation, it would just be really really nice if there was a built-in way to narrow down the asset's scope. I'm saying because it sucks having to request modifying other plugins' cores to make them compatible with yours. I've been using this until now:

PHP Code:
<?php
  
if (isset($_GET['id']) && $_GET['id'] === 'custom_settings') {
    
register_script('adminxml-plus'$SITEURL 'plugins/adminxml_plus/adminxml-plus.js''1'TRUE);
    
queue_script('adminxml-plus',GSBACK); 
  }
?>

I'd also thought of a solution similar to the one you suggested. In yours, I suppose $condition is a function that should return either true or false. But if plugin devs can't be bothered with a simple if clause, I doubt that it will help at all. 

Perhaps the documentation is lacking on this part?
Yes the documentation most likely needs to be updated