Guest User

Untitled

a guest
Nov 28th, 2013
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 44.17 KB | None | 0 0
  1. <?php
  2. /**
  3.  * THERE IS A LOT OF WORK TO DO FOR THE STABLE VERSION OF DEFERRING CAPACITY.
  4.  * RUN SOME TEST WITH ALL THE CAPACITY OF BWP MINIFY TO DO !
  5.  *
  6.  * Copyright (c) 2012 Khang Minh <betterwp.net>
  7.  *
  8.  * This program is free software: you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation, either version 3 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20.  */
  21.  
  22. /**
  23.  * This is a wrapper function to help you convert a normal source to a minified source.
  24.  *
  25.  * Please do not use this function before WordPress has been initialized. Otherwise, you might get a fatal error.
  26.  *
  27.  * @param string $src The source file you would like to convert
  28.  * @see http://betterwp.net/wordpress-plugins/bwp-minify/ for more information
  29.  */
  30. function bwp_minify($src)
  31. {
  32.     global $bwp_minify;
  33.  
  34.     return $bwp_minify->get_minify_src($bwp_minify->process_media_source($src));
  35. }
  36.  
  37. if (!class_exists('BWP_FRAMEWORK'))
  38.     require_once('class-bwp-framework.php');
  39.  
  40. class BWP_MINIFY extends BWP_FRAMEWORK {
  41.  
  42.     /**
  43.      * Hold all scripts to be printed in <head> section
  44.      */
  45.     var $header_scripts = array(array()), $header_l10n = array(), $header_dynamic = array(), $wp_scripts_done = array();
  46.  
  47.     /**
  48.      * Hold all scripts to be printed just before </body>
  49.      */
  50.     var $footer_scripts = array(array()), $footer_l10n = array(), $footer_dynamic = array();
  51.  
  52.     /**
  53.      * Determine positions to manually put scripts in
  54.      */
  55.     var $print_positions = array('header' => array(), 'footer' => array(), 'direct' => array(), 'ignore' => array(), 'ignore_def' => array());
  56.    
  57.     /**
  58.      * Are scripts still queueable?
  59.      */
  60.     var $queueable = true;
  61.    
  62.     /**
  63.      * Queued styles to be printed
  64.      */
  65.     var $styles = array(array()), $media_styles = array('print' => array()), $dynamic_media_styles = array('print' => array()), $dynamic_styles = array(), $inline_styles = array(), $wp_styles_done = array();
  66.      
  67.     /**
  68.      * Are we still able to print styles?
  69.      */
  70.     var $printable = true;
  71.      
  72.     /**
  73.      * Other options
  74.      */
  75.      var $ver = '', $base = '', $cache_time = 1800, $buster = '';
  76.  
  77.     /**
  78.      * Constructor
  79.      */
  80.     function __construct($version = '1.3.6')
  81.     {
  82.         // Plugin's title
  83.         $this->plugin_title = 'BetterWP Minify';
  84.         // Plugin's version
  85.         $this->set_version($version);
  86.         $this->set_version('5.1.6', 'php');
  87.         // Basic version checking
  88.         if (!$this->check_required_versions())
  89.             return;
  90.  
  91.         // The default options
  92.         $options = array(
  93.             'input_minurl' => '',
  94.             'input_cache_dir' => '',
  95.             'input_maxfiles' => 20,
  96.             'input_maxage' => 30,
  97.             'input_ignore' => '',
  98.             'input_header' => '',
  99.             'input_direct' => 'admin-bar',
  100.             'input_footer' => '',
  101.             'input_ignore_def' => '',
  102.             'input_custom_buster' => '',
  103.             'enable_min_js' => 'yes',
  104.             'enable_deferring' => 'no',
  105.             'enable_min_css' => 'yes',
  106.             'enable_bloginfo' => 'yes',
  107.             'select_buster_type' => 'none',
  108.             'select_time_type' => 60
  109.         );
  110.         // Super admin only options
  111.         $this->site_options = array('input_cache_dir');
  112.  
  113.         $this->build_properties('BWP_MINIFY', 'bwp-minify', $options, 'BetterWP Minify', dirname(dirname(__FILE__)) . '/bwp-minify.php', 'http://betterwp.net/wordpress-plugins/bwp-minify/', false);
  114.  
  115.         $this->add_option_key('BWP_MINIFY_OPTION_GENERAL', 'bwp_minify_general', __('Better WordPress Minify Settings', 'bwp-minify'));
  116.  
  117.         add_action('init', array($this, 'default_minurl'));
  118.         add_action('init', array($this, 'init'));
  119.     }
  120.  
  121.     function default_minurl()
  122.     {
  123.         $this->options_default['input_minurl'] = apply_filters('bwp_minify_min_dir', plugin_dir_url($this->plugin_file) . 'min/'); 
  124.     }
  125.  
  126.     function init_properties()
  127.     {
  128.         $this->parse_positions();
  129.         $this->get_base();
  130.         $this->ver = get_bloginfo('version');
  131.         $this->cache = (int) $this->options['input_maxage'] * (int) $this->options['select_time_type'];
  132.         $this->options['input_cache_dir'] = empty($this->options['input_cache_dir']) ? $this->get_cache_dir() : $this->options['input_cache_dir'];
  133.         $this->buster = $this->get_buster($this->options['select_buster_type']);
  134.     }
  135.  
  136.     private static function is_loadable()
  137.     {
  138.         // Ignore Geomashup for now
  139.         if (!empty($_GET['geo_mashup_content']) && 'render-map' == $_GET['geo_mashup_content'])
  140.             return false;
  141.         return true;
  142.     }
  143.  
  144.     function add_hooks()
  145.     {
  146.         // Certain plugins use a single file to show contents, which doesn't make use of wp_head and wp_footer action
  147.         if (!self::is_loadable())
  148.             return;
  149.  
  150.         // Allow other developers to use BWP Minify inside wp-admin, be very careful :-)
  151.         $allowed_in_admin = apply_filters('bwp_minify_allowed_in_admin', false);
  152.  
  153.         if ((!is_admin() || (is_admin() && $allowed_in_admin)))
  154.         {
  155.             // Minify scripts if needed
  156.             if ('yes' == $this->options['enable_min_js'])
  157.             {
  158.                 add_filter('print_scripts_array', array($this, 'minify_scripts'));
  159.                 // Hook to common head and footer actions
  160.                 add_action('wp_head', array($this, 'print_header_scripts'), 9);
  161.                 add_action('wp_head', array($this, 'print_dynamic_header_scripts'), 9);
  162.                 add_action('wp_footer', array($this, 'print_footer_scripts'), 100);
  163.                 add_action('wp_footer', array($this, 'print_dynamic_footer_scripts'), 100);
  164.                 add_action('login_head', array($this, 'print_header_scripts'));
  165.                 add_action('login_head', array($this, 'print_dynamic_header_scripts'));
  166.                 add_action('login_footer', array($this, 'print_footer_scripts'), 100);
  167.                 add_action('login_footer', array($this, 'print_dynamic_footer_scripts'), 100);
  168.                 add_action('admin_head', array($this, 'print_header_scripts'), 9);
  169.                 add_action('admin_head', array($this, 'print_dynamic_header_scripts'), 9);
  170.                 add_action('bwp_minify_before_header_scripts', array($this, 'print_header_scripts_l10n'));
  171.                 add_action('bwp_minify_before_footer_scripts', array($this, 'print_footer_scripts_l10n'), 100);
  172.             }
  173.            
  174.             // Minify styles if needed
  175.             if ('yes' == $this->options['enable_min_css'])
  176.             {
  177.                 add_filter('print_styles_array', array($this, 'minify_styles'));
  178.                 add_action('wp_head', array($this, 'print_styles'), 8);
  179.                 add_action('wp_head', array($this, 'print_media_styles'), 8);
  180.                 add_action('wp_head', array($this, 'print_dynamic_styles'), 8);
  181.                 add_action('wp_head', array($this, 'print_dynamic_media_styles'));
  182.                 // Add support for inline styles since WordPress 3.3
  183.                 if (version_compare(get_bloginfo('version'), '3.3', '>='))
  184.                     add_action('wp_head', array($this, 'print_inline_styles'), 8);
  185.                 add_action('login_head', array($this, 'print_styles'));
  186.                 add_action('login_head', array($this, 'print_media_styles'));
  187.                 add_action('login_head', array($this, 'print_dynamic_media_styles'));
  188.                 add_action('login_head', array($this, 'print_dynamic_styles'));
  189.                 add_action('admin_head', array($this, 'print_styles'), 8);
  190.                 add_action('admin_head', array($this, 'print_media_styles'), 8);
  191.                 add_action('admin_head', array($this, 'print_dynamic_media_styles'), 8);
  192.                 add_action('admin_head', array($this, 'print_dynamic_styles'), 8);
  193.             }
  194.         }
  195.  
  196.         if ('yes' == $this->options['enable_bloginfo'])
  197.         {
  198.             add_filter('stylesheet_uri', array($this, 'minify_item'));
  199.             add_filter('locale_stylesheet_uri', array($this, 'minify_item'));
  200.         }
  201.     }
  202.    
  203.     /**
  204.      * Build the Menus
  205.      */
  206.     function build_menus()
  207.     {
  208.         add_options_page(__('Better WordPress Minify', 'bwp-minify'), 'BWP Minify', BWP_MINIFY_CAPABILITY, BWP_MINIFY_OPTION_GENERAL, array($this, 'build_option_pages'));     
  209.     }
  210.    
  211.     /**
  212.      * Build the option pages
  213.      *
  214.      * Utilizes BWP Option Page Builder (@see BWP_OPTION_PAGE)
  215.      */
  216.     function build_option_pages()
  217.     {
  218.         if (!current_user_can(BWP_MINIFY_CAPABILITY))
  219.             wp_die(__('You do not have sufficient permissions to access this page.'));
  220.  
  221.         // Init the class
  222.         $page = $_GET['page'];     
  223.         $bwp_option_page = new BWP_OPTION_PAGE($page, $this->site_options);
  224.        
  225.         $options = array();
  226.        
  227. if (!empty($page))
  228. {  
  229.     if ($page == BWP_MINIFY_OPTION_GENERAL)
  230.     {
  231.         $form = array(
  232.             'items'         => array('heading', 'checkbox', 'checkbox', 'checkbox', 'checkbox', 'heading', 'input', 'input', 'input', 'select', 'heading', 'textarea', 'textarea', 'textarea', 'textarea', 'textarea'),
  233.             'item_labels'   => array
  234.             (
  235.                 __('General Options', 'bwp-minify'),
  236.                 __('Minify JS files automatically?', 'bwp-minify'),
  237.                 __('<strong>[BETA]</strong> Deferring js automatically?', 'bwp-minify'),
  238.                 __('Minify CSS files automatically?', 'bwp-minify'),
  239.                 __('Minify <code>bloginfo()</code> stylesheets?', 'bwp-minify'),
  240.                 __('Minifying Options', 'bwp-minify'),
  241.                 __('Minify URL (double-click to edit)', 'bwp-minify'),
  242.                 __('Cache directory (double-click to edit)', 'bwp-minify'),
  243.                 __('One minify string will contain', 'bwp-minify'),
  244.                 __('Append the minify string with', 'bwp-minify'),
  245.                 __('Minifying Scripts Options', 'bwp-minify'),
  246.                 __('Scripts to be minified in header', 'bwp-minify'),
  247.                 __('Scripts to be minified in footer', 'bwp-minify'),
  248.                 __('Scripts to be minified and then printed separately', 'bwp-minify'),
  249.                 __('Scripts to be ignored (not minified)', 'bwp-minify'),
  250.                 __('Scripts to not deferred (not asynchrone)', 'bwp-minify')
  251.             ),
  252.             'item_names'    => array('h1', 'cb1', 'cb4', 'cb3', 'cb2', 'h2', 'input_minurl', 'input_cache_dir', 'input_maxfiles', 'select_buster_type', 'h3', 'input_header', 'input_footer', 'input_direct', 'input_ignore', 'input_ignore_def'),
  253.             'heading'           => array(
  254.                 'h1'    => '',
  255.                 'h2'    => __('<em>Options that affect both your stylesheets and scripts.</em>', 'bwp-minify'),
  256.                 'h3'    => sprintf(__('<em>You can force the position of each script using those inputs below (e.g. you have a script registered in the header but you want to minify it in the footer instead). If you are still unsure, please read more <a href="%s#positioning-your-scripts">here</a>. Type in one script handle (<strong>NOT filename</strong>) per line.</em>.</br>Check the <a href="http://codex.wordpress.org/Function_Reference/wp_register_script#Handles_and_Their_Script_Paths_Registered_by_WordPress" target="_blank">default wordpress handle</a>', 'bwp-minify'), $this->plugin_url)
  257.             ),
  258.             'select' => array(
  259.                 'select_time_type' => array(
  260.                     __('second(s)', 'bwp-minify') => 1,
  261.                     __('minute(s)', 'bwp-minify') => 60,
  262.                     __('hour(s)', 'bwp-minify') => 3600,
  263.                     __('day(s)', 'bwp-minify') => 86400
  264.                 ),
  265.                 'select_buster_type' => array(
  266.                     __('Do not append anything', 'bwp-minify') => 'none',
  267.                     __('Cache folder&#8217;s last modified time', 'bwp-minify') => 'mtime',
  268.                     __('Your WordPress&#8217;s current version', 'bwp-minify') => 'wpver',
  269.                     __('Your theme&#8217;s current version', 'bwp-minify') => 'tver',
  270.                     __('A custom number', 'bwp-minify') => 'custom'
  271.                 )
  272.             ),
  273.             'checkbox'  => array(
  274.                 'cb1' => array(__('you can still use <code>bwp_minify()</code> helper function if you disable this.', 'bwp-minify') => 'enable_min_js'),
  275.                 'cb4' => array(__('Activate the deferred capabilities. <em>functionality inspired from the plugin : <a href="http://wordpress.org/plugins/wp-deferred-javascripts/" title="WP Deferred" target="_blank">WP Deferred</a></em>', 'bwp-minify') => 'enable_deferring'),
  276.                 'cb3' => array(__('you can still use <code>bwp_minify()</code> helper function if you disable this.', 'bwp-minify') => 'enable_min_css'),
  277.                 'cb2' => array(__('most themes (e.g. Twenty Ten) use <code>bloginfo()</code> to print the main stylesheet (i.e. <code>style.css</code>) and BWP Minify will not be able to add it to the main minify string. If you want to minify <code>style.css</code> with the rest of your css files, you must enqueue it.', 'bwp-minify') => 'enable_bloginfo')
  278.             ),
  279.             'input' => array(
  280.                 'input_minurl' => array('size' => 91, 'disabled' => ' readonly="readonly"', 'label' => sprintf(__('This should be set automatically. If you think the URL is too long, please read <a href="%s#customization">here</a> to know how to properly modify this.', 'bwp-minify'), $this->plugin_url)),
  281.                 'input_cache_dir' => array('size' => 91, 'disabled' => ' readonly="readonly"', 'label' => '<br />' . sprintf(__('<strong>Important</strong>: Changing cache directory is a two-step process, which is described in details <a href="%s#advanced_customization" target="_blank">here</a>. Please note that cache directory must be writable (i.e. CHMOD to 755 or 777).', 'bwp-minify'), $this->plugin_url)),
  282.                 'input_maxfiles' => array('size' => 3, 'label' => __('file(s) at most.', 'bwp-minify')),
  283.                 'input_maxage' => array('size' => 5, 'label' => __('&mdash;', 'bwp-minify')),
  284.                 'input_custom_buster' => array('pre' => __('<em>&rarr; /min/?f=file.js&amp;ver=</em> ', 'bwp-minify'), 'size' => 12, 'label' => '.', 'disabled' => ' disabled="disabled"')
  285.             ),
  286.             'textarea' => array
  287.             (
  288.                 'input_header' => array('cols' => 40, 'rows' => 3),
  289.                 'input_footer' => array('cols' => 40, 'rows' => 3),
  290.                 'input_direct' => array('cols' => 40, 'rows' => 3),
  291.                 'input_ignore' => array('cols' => 40, 'rows' => 3),
  292.                 'input_ignore_def' => array('cols' => 40, 'rows' => 3)
  293.             ),
  294.             'container' => array(
  295.                 'select_buster_type' => __('<em><strong>Note:</strong> When you append one of the things above you are basically telling browsers to clear their cached version of your CSS and JS files, which is very useful when you change source files. Use this feature wisely :).</em>', 'bwp-minify')
  296.             ),
  297.             'inline_fields' => array(
  298.                 'input_maxage' => array('select_time_type' => 'select'),
  299.                 'select_buster_type' => array('input_custom_buster' => 'input')
  300.             ),
  301.             'inline' => array(
  302.                 'input_cache_dir' => '<br /><br /><input type="submit" class="button-secondary action" name="flush_cache" value="' . __('Flush the cache', 'bwp-minify') . '" />'
  303.             )
  304.         );
  305.  
  306.         // Get the default options
  307.         $options = $bwp_option_page->get_options(array('input_minurl', 'input_cache_dir', 'input_maxfiles', 'input_header', 'input_footer', 'input_direct', 'input_ignore', 'input_ignore_def', 'input_custom_buster', 'select_buster_type', 'enable_min_js', 'enable_deferring', 'enable_min_css', 'enable_bloginfo'), $this->options);
  308.  
  309.         // Get option from the database
  310.         $options = $bwp_option_page->get_db_options($page, $options);
  311.  
  312.         $option_formats = array('input_maxfiles' => 'int', 'input_maxage' => 'int', 'select_time_type' => 'int');
  313.         $option_super_admin = $this->site_options;
  314.     }
  315. }
  316.  
  317.         // Flush the cache
  318.         if (isset($_POST['flush_cache']) && !$this->is_normal_admin())
  319.         {
  320.             check_admin_referer($page);
  321.             if ($deleted = self::flush_cache($options['input_cache_dir']))
  322.                 $this->add_notice('<strong>' . __('Notice', 'bwp-minify') . ':</strong> ' . sprintf(__("<strong>%d</strong> cached files have been deleted successfully!", 'bwp-minify'), $deleted));
  323.             else
  324.                 $this->add_notice('<strong>' . __('Notice', 'bwp-minify') . ':</strong> ' . __("Could not delete any cached files. Please manually check the cache directory.", 'bwp-minify'));
  325.         }
  326.  
  327.         // Get option from user input
  328.         if (isset($_POST['submit_' . $bwp_option_page->get_form_name()]) && isset($options) && is_array($options))
  329.         {
  330.             check_admin_referer($page);
  331.             foreach ($options as $key => &$option)
  332.             {
  333.                 // [WPMS Compatible]
  334.                 if ($this->is_normal_admin() && in_array($key, $option_super_admin))
  335.                 {
  336.                 }
  337.                 else
  338.                 {
  339.                     if (isset($_POST[$key]))
  340.                         $bwp_option_page->format_field($key, $option_formats);
  341.                     if (!isset($_POST[$key]) && !isset($form['input'][$key]['disabled']))
  342.                         $option = '';
  343.                     else if (isset($option_formats[$key]) && 0 == $_POST[$key] && 'int' == $option_formats[$key])
  344.                         $option = 0;
  345.                     else if (isset($option_formats[$key]) && empty($_POST[$key]) && 'int' == $option_formats[$key])
  346.                         $option = $this->options_default[$key];
  347.                     else if (!empty($_POST[$key]))
  348.                         $option = trim(stripslashes($_POST[$key]));
  349.                     else
  350.                         $option = '';
  351.                 }
  352.             }
  353.             update_option($page, $options);
  354.             // [WPMS Compatible]
  355.             if (!$this->is_normal_admin())
  356.                 update_site_option($page, $options);
  357.             // Update options successfully
  358.             $this->add_notice(__('All options have been saved.', 'bwp-minify'));
  359.         }
  360.  
  361.         // Guessing the cache directory
  362.         $options['input_cache_dir'] = (empty($options['input_cache_dir'])) ? $this->get_cache_dir($options['input_minurl']) : $options['input_cache_dir'];
  363.  
  364.         // [WPMS Compatible]
  365.         if ($this->is_normal_admin())
  366.             $bwp_option_page->kill_html_fields($form, array(5,6));
  367.         // Cache buster system
  368.         $this->options = array_merge($this->options, $options);
  369.         $options['input_custom_buster'] = $this->get_buster($options['select_buster_type']);
  370.         if ('custom' == $options['select_buster_type'])
  371.             unset($form['input']['input_custom_buster']['disabled']);
  372.  
  373.         if (!file_exists($options['input_cache_dir']) || !is_writable($options['input_cache_dir']))
  374.             $this->add_notice('<strong>' . __('Warning') . ':</strong> ' . __("Cache directory does not exist or is not writable. Please try CHMOD your cache directory to 755. If you still see this warning, CHMOD to 777.", 'bwp-minify'));
  375.  
  376. ?>
  377. <script type="text/javascript">
  378.         jQuery(document).ready(function(){
  379.             jQuery('.bwp-option-page :input[readonly]').dblclick(function(){
  380.                 jQuery(this).removeAttr('readonly');
  381.             });
  382.         })
  383.     </script>
  384. <?php
  385.         // Assign the form and option array    
  386.         $bwp_option_page->init($form, $options, $this->form_tabs);
  387.  
  388.         // Build the option page   
  389.         echo $bwp_option_page->generate_html_form();
  390.     }
  391.  
  392.     function get_cache_dir($minurl = '')
  393.     {
  394.         global $current_blog;
  395.  
  396.         if (empty($minurl))
  397.             $minurl = $this->options['input_minurl'];
  398.         $temp = @parse_url($minurl);
  399.         if (isset($temp['scheme']) && isset($temp['host']))
  400.         {
  401.             $port = (!empty($temp['port'])) ? ':' . $temp['port'] : '';
  402.             $site_url = $temp['scheme'] . '://' . $temp['host'] . $port;
  403.             $guess_cache = str_replace($site_url, '', $minurl);
  404.         }
  405.         else
  406.             $guess_cache = $minurl;
  407.         // @since 1.0.1
  408.         $multisite_path = (isset($current_blog->path) && '/' != $current_blog->path) ? $current_blog->path : '';
  409.         $guess_cache = str_replace($multisite_path, '', dirname($guess_cache));
  410.         $guess_cache = trailingslashit($_SERVER['DOCUMENT_ROOT']) . trim($guess_cache, '/') . '/cache/';
  411.         return apply_filters('bwp_minify_cache_dir', str_replace('\\', '/', $guess_cache));
  412.     }
  413.  
  414.     private static function flush_cache($cache_dir)
  415.     {
  416.         $cache_dir = trailingslashit($cache_dir);
  417.         $deleted = 0;
  418.         if (is_dir($cache_dir))
  419.         {
  420.             if ($dh = opendir($cache_dir))
  421.             {
  422.                 while (($file = readdir($dh)) !== false)
  423.                 {
  424.                     if (preg_match('/^minify_[a-z0-9_\-\.,]+(\.gz)?$/i', $file))
  425.                     {
  426.                         @unlink($cache_dir . $file);
  427.                         $deleted++;
  428.                     }
  429.                 }
  430.                 closedir($dh);
  431.             }
  432.         }
  433.         return $deleted;
  434.     }
  435.  
  436.     function parse_positions()
  437.     {
  438.         $positions = array(
  439.             'header'    => $this->options['input_header'],
  440.             'footer'    => $this->options['input_footer'],
  441.             'direct'    => $this->options['input_direct'],
  442.             'ignore'    => $this->options['input_ignore'],
  443.             'ignore_def'    => $this->options['input_ignore_def']
  444.         );
  445.  
  446.         foreach ($positions as &$position)
  447.         {
  448.             if (!empty($position))
  449.             {
  450.                 $position = explode("\n", $position);
  451.                 $position = array_map('trim', $position);
  452.             }
  453.         }              
  454.        
  455.         $this->print_positions = $positions;
  456.     }
  457.  
  458.     function get_base()
  459.     {
  460.         $temp = @parse_url(get_site_option('siteurl'));
  461.         $port = (!empty($temp['port'])) ? ':' . $temp['port'] : '';
  462.         $site_url = $temp['scheme'] . '://' . $temp['host'] . $port;
  463.         $raw_base = trim(str_replace($site_url, '', get_site_option('siteurl')), '/');
  464.         /* More filtering will ba added in future */
  465.         $this->base = $raw_base;
  466.     }
  467.  
  468.     function get_buster($type)
  469.     {
  470.         $buster = '';
  471.  
  472.         switch ($type)
  473.         {
  474.             case 'mtime':
  475.                 if (file_exists($this->options['input_cache_dir']))
  476.                     $buster = filemtime($this->options['input_cache_dir']);
  477.             break;
  478.  
  479.             case 'wpver':
  480.                 $buster = $this->ver;
  481.             break;
  482.  
  483.             case 'tver':
  484. if (function_exists('wp_get_theme')) {
  485.           $theme = wp_get_theme(STYLESHEETPATH);
  486.           if ($theme) {
  487.             $version = $theme->get('Version');
  488.             if (!empty($version))
  489.               $buster = $version;
  490.           }
  491.         } else {
  492.           $theme = get_theme_data(STYLESHEETPATH . '/style.css');
  493.           if (!empty($theme['Version']))
  494.             $buster = $theme['Version'];
  495.         }
  496.             break;
  497.  
  498.             case 'custom':
  499.                 $buster = $this->options['input_custom_buster'];
  500.             break;
  501.  
  502.             case 'none':
  503.             default:
  504.                 if (is_admin())
  505.                     $buster = __('empty', 'bwp-minify');
  506.             break;
  507.         }
  508.  
  509.         return apply_filters('bwp_minify_get_buster', $buster);
  510.     }
  511.    
  512.     function is_in($handle, $position = 'header')
  513.     {
  514.         if (!isset($this->print_positions[$position]) || !is_array($this->print_positions[$position]))
  515.             return false;
  516.         if (in_array($handle, $this->print_positions[$position]))
  517.             return true;
  518.     }
  519.  
  520.     function ignores_style($handle, &$temp, $deps)
  521.     {
  522.         if (!is_array($deps) || 0 == sizeof($deps) || 'wp' == $this->are_deps_added($handle, ''))
  523.         {
  524.             $temp[] = $handle;
  525.             $this->wp_styles_done[] = $handle;
  526.         }
  527.         else if (!in_array($handle, $this->dynamic_styles))
  528.             $this->dynamic_styles[] = $handle;
  529.     }
  530.  
  531.     private static function has_inline($handle)
  532.     {
  533.         global $wp_styles;
  534.         if (isset($wp_styles->registered[$handle]->extra['after']))
  535.             return true;
  536.         return false;
  537.     }
  538.  
  539.     /**
  540.      * Check if a sciprt has been localized using wp_localize_script()
  541.      *
  542.      * @see wp-includes/functions.wp-scripts.php
  543.      */
  544.     function is_l10n($handle)
  545.     {
  546.         global $wp_scripts;
  547.         // Since 3.3, 'l10n' has been changed into 'data'
  548.         if (isset($wp_scripts->registered[$handle]->extra['l10n']) || isset($wp_scripts->registered[$handle]->extra['data']))
  549.             return true;
  550.         return false;
  551.     }
  552.    
  553.     /**
  554.      * Check if media source is local
  555.      */
  556.     function is_local($src = '')
  557.     {
  558.         $url = @parse_url($src);
  559.         $blog_url = @parse_url(get_option('home'));
  560.         if (false === $url)
  561.             return false;
  562.         // If scheme is set
  563.         if (isset($url['scheme']))
  564.         {
  565.             if (false === strpos($url['host'], $blog_url['host']))
  566.                 return false;
  567.             return true;
  568.         }
  569.         else
  570.             // Probably a relative link
  571.             return true;
  572.     }
  573.  
  574.     /**
  575.      * Make sure the source is valid.
  576.      *
  577.      * @since 1.0.3
  578.      */
  579.     function is_source_static($src = '')
  580.     {
  581.         // Source that doesn't have .css or .js extesion is dynamic
  582.         if (!preg_match('#[^,]+\.(css|js)$#ui', $src))
  583.             return false;
  584.         // Source that contains ?, =, & is dynamic
  585.         if (strpos($src, '?') === false && strpos($src, '=') === false && strpos($src, '&') === false)
  586.             return true;
  587.         return false;
  588.     }
  589.  
  590.     /**
  591.      * Make sure the media file is in expected format before being added to our minify string
  592.      */
  593.     function process_media_source($src = '')
  594.     {
  595.         $src = trim($src);
  596.         // Absolute url
  597.         if (0 === strpos($src, 'http'))
  598.         {
  599.             // We will need to handle both http and https, even if site URL is still set to http
  600.             $tmp_src = str_replace(array(get_option('siteurl'), str_replace('http://', 'https://', get_option('siteurl'))), '', $src);
  601.             // If there was no difference between tmp_src and src, we need to loop through the base
  602.             if ($tmp_src == $src && !empty($this->base))
  603.             {
  604.                 $base_cpns = explode('/', preg_replace('/[\/]+/i', '/', $this->base));
  605.                 array_pop($base_cpns);
  606.                 foreach ($base_cpns as $key => $cpn)
  607.                 {
  608.                     $cpn_path = '/' . $cpn;
  609.                     for ($i = 0; $i < $key; $i++)
  610.                         $cpn_path = '/' . $base_cpns[$i] . $cpn_path;
  611.                     $cpn_url = 'http://' . $_SERVER['HTTP_HOST'] . $cpn_path;
  612.                     $src = str_replace(array($cpn_url, str_replace('http://', 'https://', $cpn_url)), '', $src);
  613.                 }
  614.  
  615.                 $src = $cpn_path . $src;
  616.             }
  617.             else
  618.                 $src = $this->base . $tmp_src;
  619.         }
  620.         // Relative absolute url from root
  621.         else if ('/' === substr($src, 0, 1))
  622.             // Add base for wp-includes and wp-admin directory
  623.             if (false !== strpos($src, 'wp-includes') || false !== strpos($src, 'wp-admin'))
  624.                 $src = $this->base . $src;
  625.         // @since 1.0.3
  626.         $src = str_replace('./', '/', $src);
  627.         $src = str_replace('\\', '/', $src);
  628.         $src = preg_replace('#[\/]{2,}#iu', '/', $src);
  629.         $src = ltrim($src, '/');
  630.         return esc_attr($src);
  631.     }
  632.  
  633.     /**
  634.      *  Have dependencies for the style / script been added / printed?
  635.      */
  636.     function are_deps_added($handle, $type = '', $media = NULL)
  637.     {
  638.         global $wp_styles, $wp_scripts;
  639.  
  640.         $type       = (!empty($type)) ? 'scripts' : '';
  641.         $wp_media   = ('scripts' == $type) ? $wp_scripts : $wp_styles;
  642.         $deps       = $wp_media->registered[$handle]->deps;
  643.         $return     = 'wp';
  644.  
  645.         foreach ($deps as $dep)
  646.         {
  647.             $dep_src = $wp_media->registered[$dep]->src;
  648.             $dep_src = ($this->is_local($dep_src)) ? $this->process_media_source($dep_src) : $dep_src;
  649.             $ext_dep = ($this->is_local($dep_src)) ? '' : $dep;
  650.             $is_added = $this->is_added($dep_src, $type, $ext_dep, $media, $dep);
  651.             if ('min' == $is_added)
  652.                 $return = 'min';
  653.             else if ('done' == $is_added)
  654.                 $return = 'wp';
  655.             if (!$is_added)
  656.                 return false;
  657.         }
  658.  
  659.         return $return;
  660.     }
  661.  
  662.     private function are_deps_added_to_media($handle)
  663.     {
  664.         global $wp_styles;
  665.         $deps = $wp_styles->registered[$handle]->deps;
  666.  
  667.         foreach ($deps as $dep)
  668.         {
  669.             if ((isset($this->media_styles['screen']) && array_key_exists($dep, $this->media_styles['screen'])) || (isset($this->media_styles['screen']) && array_key_exists($dep, $this->media_styles['print'])))
  670.                 return true;
  671.         }
  672.  
  673.         return false;
  674.     }
  675.  
  676.     function is_added($src, $type = 'scripts', $handle = '', $media = NULL, $dep_handle = '')
  677.     {
  678.         if (!isset($media))
  679.             $media = ('scripts' == $type) ? array_merge($this->header_scripts, $this->footer_scripts) : array_merge($this->styles, $this->media_styles);
  680.  
  681.         // Loop through media array to find the source
  682.         foreach ($media as $media_string)
  683.             if (in_array($src, $media_string))
  684.                 return 'min';
  685.  
  686.         // Loop throught done array to find the handle
  687.         $done_media = ('scripts' == $type) ? $this->wp_scripts_done : $this->wp_styles_done;
  688.         foreach ($done_media as $media_handle)
  689.             if ($dep_handle == $media_handle)
  690.                 return 'done';
  691.  
  692.         // Also check extra media if needed
  693.         if (!empty($handle))
  694.         {
  695.             $extra_media = ('scripts' == $type) ? array_merge($this->header_dynamic, $this->footer_dynamic) : array_merge($this->dynamic_styles);
  696.             if (in_array($handle, $extra_media))
  697.                 return 'wp';
  698.         }      
  699.         return false;
  700.     }
  701.  
  702.     function append_minify_string(&$media = array(), &$count = 0, $src = '', $done = false, $parent = false, $type = 'scripts')
  703.     {
  704.         $current_pointer = sizeof($media) - 1;
  705.         // Don't append if already added
  706.         if (!$this->is_added($src, $type))
  707.             $media[$current_pointer][] = $src;
  708.         if (false == $parent && $this->options['input_maxfiles'] <= $count || true == $done)
  709.         {
  710.             $current_pointer = sizeof($media) - 1;
  711.             $count = 0;
  712.             if (!$done)
  713.                 $media[] = array();
  714.         }
  715.     }
  716.  
  717.     function get_minify_src($string)
  718.     {
  719.         if (empty($string))
  720.             return '';
  721.  
  722.         $buster = (!empty($this->buster)) ? '&amp;ver=' . $this->buster : '';
  723.         $scheme_str = is_ssl() && !is_admin() ? 'https://' : 'http://';
  724.  
  725.         return apply_filters('bwp_get_minify_src', trailingslashit(str_replace(array('http://', 'https://'), $scheme_str, $this->options['input_minurl'])) . '?f=' . $string . $buster, $string, $buster, $this->options['input_minurl']);
  726.     }
  727.  
  728.     function get_minify_tag($string, $type, $media = '')
  729.     {
  730.         if (empty($string))
  731.             return '';
  732.  
  733.         switch ($type)
  734.         {
  735.             case 'script':
  736.                 $return  = "<script type='text/javascript' src='" . $this->get_minify_src($string) . "'></script>\n";
  737.             break;
  738.            
  739.             case 'style':
  740.             default:           
  741.                 $return = "<link rel='stylesheet' type='text/css' media='all' href='" . $this->get_minify_src($string) . "' />\n";
  742.             break;
  743.  
  744.             case 'media':
  745.                 $return = "<link rel='stylesheet' type='text/css' media='$media' href='" . $this->get_minify_src($string) . "' />\n";
  746.             break;
  747.         }
  748.  
  749.         return apply_filters('bwp_get_minify_tag', $return, $string, $type, $media);
  750.     }
  751.  
  752.     /**
  753.      * Get the correct href for rtl css.
  754.      *
  755.      * This is actually borrowed from wp-includes/class.wp-styles.php:70
  756.      */
  757.     function rtl_css_href($handle)
  758.     {
  759.         global $wp_styles;
  760.  
  761.         if (is_bool($wp_styles->registered[$handle]->extra['rtl']))
  762.         {
  763.             $suffix = isset($wp_styles->registered[$handle]->extra['suffix'] ) ? $wp_styles->registered[$handle]->extra['suffix'] : '';
  764.             $rtl_href = str_replace("{$suffix}.css", "-rtl{$suffix}.css", $wp_styles->registered[$handle]->src);
  765.         }
  766.         else
  767.             $rtl_href = $wp_styles->registered[$handle]->extra['rtl'];
  768.        
  769.         return $this->process_media_source($rtl_href);
  770.     }
  771.  
  772.     function minify_item($src)
  773.     {
  774.         return $this->get_minify_src($this->process_media_source($src));
  775.     }
  776.  
  777.     /**
  778.      * Loop through current todo array to build our minify string (min/?f=style1.css,style2.css)
  779.      *
  780.      * If the number of stylesheets reaches a limit (by default the limit is 10) this plugin will split the minify string
  781.      * into an appropriate number of <link> tags. This is only to beautify your minify strings (they might get too long).
  782.      * If you don't like such behaviour, change the limit to something higher, e.g. 50.
  783.      */
  784.     function minify_styles($todo)
  785.     {
  786.         global $wp_styles;
  787.  
  788.         $total = sizeof($todo);
  789.         $count = 0;
  790.         $queued = 0;
  791.         $temp = array();
  792.         $this->print_positions['style_ignore'] = apply_filters('bwp_minify_style_ignore', array());
  793.         $this->print_positions['style_direct'] = apply_filters('bwp_minify_style_direct', array('admin-bar'));
  794.         $this->print_positions['style_allowed'] = apply_filters('bwp_minify_allowed_styles', 'all');
  795.  
  796.         foreach ($todo as $key => $handle)
  797.         {
  798.             $count++;
  799.             // Take the src from registred stylesheets, do not proceed if the src is external
  800.             // Also do not proceed if we can not print any more (except for login page)
  801.             $the_style = $wp_styles->registered[$handle];
  802.             $src = $the_style->src;
  803.             $ignore = false;
  804.             // Check for is_bool @since 1.0.2
  805.             if ($this->is_in($handle, 'style_ignore') || is_bool($src))
  806.                 $ignore = true;
  807.             else if (!$this->is_source_static($src))
  808.                 $ignore = true;
  809.             // If style handle is not allowed
  810.             else if ('all' != $this->print_positions['style_allowed'] && !$this->is_in($handle, 'style_allowed'))
  811.                 $ignore = true;
  812.             else if ($this->is_in($handle, 'style_direct'))
  813.             {
  814.                 $src = $this->process_media_source($src);
  815.                 $the_style->src = $this->get_minify_src($src);
  816.                 $the_style->ver = NULL;
  817.                 $ignore = true;
  818.             }
  819.             else if ((has_action('login_head') || true == $this->printable) && !empty($src) && $this->is_local($src))
  820.             {
  821.                 $src = $this->process_media_source($src);
  822.                 // If styles have been printed, ignore this style
  823.                 if (did_action('bwp_minify_after_styles'))
  824.                 {
  825.                     if (!$this->is_added($src, 'styles', $handle))
  826.                         $temp[] = $handle;
  827.                     continue;
  828.                 }
  829.                 // If this style has a different media type rather than 'all', '',
  830.                 // we will have to append it to other strings
  831.                 $the_style->args = (isset($the_style->args)) ? trim($the_style->args) : '';
  832.                 if (!empty($the_style->args) && 'all' != $the_style->args)
  833.                 {
  834.                     $media = $the_style->args;
  835.                     // if this style has dependency, make sure it is added after all parent media styles
  836.                     $media_array = (isset($the_style->deps[0])) ? $this->dynamic_media_styles : $this->media_styles;
  837.                     if (!isset($media_array[$media]))
  838.                         $media_array[$media] = array();
  839.                     $media_array[$media][$handle] = $src;
  840.                     // pass the media styles back
  841.                     if (isset($the_style->deps[0]))
  842.                         $this->dynamic_media_styles = $media_array;
  843.                     else
  844.                         $this->media_styles = $media_array;
  845.                 }
  846.                 // If this style needs conditional statement (e.g. IE-specific stylesheets)
  847.                 // or is an alternate stylesheet (@see http://www.w3.org/TR/REC-html40/present/styles.html#h-14.3.1),
  848.                 // we will not enqueue it (but still minify it).
  849.                 else if ((isset($the_style->extra['conditional']) && $the_style->extra['conditional']) || (isset($the_style->extra['alt']) && $the_style->extra['alt']))
  850.                 {
  851.                     $the_style->src = $this->get_minify_src($src);
  852.                     $the_style->ver = NULL;
  853.                     $temp[] = $handle;
  854.                 }
  855.                 else
  856.                 {
  857.                     // If this 'all' style has dependencies that are put inside a media link tag, we need to
  858.                     // add this style to dynamic_media_styles and ignore it here
  859.                     if (isset($the_style->deps[0]) && $this->are_deps_added_to_media($handle))
  860.                     {
  861.                         if (!isset($this->dynamic_media_styles['all']))
  862.                             $this->dynamic_media_styles['all'] = array();
  863.                         $this->dynamic_media_styles['all'][] = $src;
  864.                         continue;
  865.                     }
  866.                     $queued++;
  867.                     $this->append_minify_string($this->styles, $queued, $src, $count == $total, true, 'styles');
  868.                     // If this style has support for rtl language and the locale is rtl,
  869.                     // we will have to append the rtl stylesheet also
  870.                     if ('rtl' === $wp_styles->text_direction && isset($the_style->extra['rtl']) && $the_style->extra['rtl'])
  871.                         $this->append_minify_string($this->styles, $queued, $this->rtl_css_href($handle), $count == $total, false, 'styles');
  872.                 }
  873.  
  874.                 // If this style has inline styles, add them too
  875.                 if (true == self::has_inline($handle))
  876.                     $this->inline_styles[] = $handle;
  877.  
  878.             }
  879.             else
  880.                 $this->ignores_style($handle, $temp, $the_style->deps);
  881.  
  882.             if (true == $ignore)
  883.                 $this->ignores_style($handle, $temp, $the_style->deps);
  884.         }
  885.  
  886.         //$this->printable = false;
  887.  
  888.         return $temp;
  889.     }
  890.  
  891.     /**
  892.      * Main function to print out our stylesheets
  893.      *
  894.      * Use actions provided to add other things before or after the output.
  895.      */
  896.     function print_styles()
  897.     {
  898.         do_action('bwp_minify_before_styles');
  899.  
  900.         // Print <link> tags
  901.         $styles = (array) $this->styles;
  902.         foreach ($styles as $style_array)
  903.         {
  904.             if (0 < sizeof($style_array))
  905.                 echo $this->get_minify_tag(implode(',', $style_array), 'style');
  906.         }
  907.  
  908.         do_action('bwp_minify_after_styles');
  909.     }
  910.    
  911.     function print_media_styles()
  912.     {
  913.         do_action('bwp_minify_before_media_styles');
  914.  
  915.         // Print <link> tags
  916.         $styles = (array) $this->media_styles;
  917.         foreach ($styles as $key => $style_array)
  918.         {
  919.             if (0 < sizeof($style_array))
  920.                 echo $this->get_minify_tag(implode(',', $style_array), 'media', $key);
  921.         }
  922.  
  923.         do_action('bwp_minify_after_media_styles');
  924.     }
  925.  
  926.     function print_dynamic_media_styles()
  927.     {
  928.         $styles = (array) $this->dynamic_media_styles;
  929.  
  930.         foreach ($styles as $key => $style_array)
  931.         {
  932.             if (0 < sizeof($style_array))
  933.                 echo $this->get_minify_tag(implode(',', $style_array), 'media', $key);
  934.         }
  935.     }
  936.  
  937.     function print_dynamic_styles()
  938.     {
  939.         global $wp_styles;
  940.  
  941.         foreach ($this->dynamic_styles as $handle)
  942.             $wp_styles->do_item($handle);
  943.     }
  944.  
  945.     public function print_inline_styles()
  946.     {
  947.         global $wp_styles;
  948.  
  949.         foreach ($this->inline_styles as $handle)
  950.             $wp_styles->print_inline_style($handle);
  951.     }
  952.  
  953.     function ignores_script($handle, &$temp, $deps, $type = 'header')
  954.     {
  955.         global $wp_scripts;
  956.  
  957.         $are_deps_added = $this->are_deps_added($handle, $type);
  958.         $are_deps_min = $this->are_deps_added($handle, $type, $this->header_scripts);
  959.         $wp = ('wp' == $are_deps_added || ('min' == $are_deps_min && 'footer' == $type && did_action('bwp_minify_printed_header_scripts'))) ? true : false;
  960.  
  961.         if (!is_array($deps) || 0 == sizeof($deps) || $wp)
  962.         {
  963.             if (!in_array($handle, $this->header_dynamic))
  964.                 $temp[] = $handle;
  965.  
  966.             if (!in_array($handle, $this->wp_scripts_done))
  967.                 $this->wp_scripts_done[] = $handle;
  968.         }
  969.         else if ('header' == $type)
  970.         {
  971.             if (!in_array($handle, $this->header_dynamic))
  972.                 $this->header_dynamic[] = $handle;
  973.         }
  974.         else if ('footer' == $type && 'min' != $are_deps_min)
  975.         {
  976.             if (!in_array($handle, $this->footer_dynamic))
  977.                 $this->footer_dynamic[] = $handle;
  978.         }
  979.     }
  980.  
  981.     /**
  982.      * Loop through current todo array to build our minify string (min/?f=script1.js,script2.js)
  983.      *
  984.      * If the number of scripts reaches a limit (by default the limit is 10) this plugin will split the minify string
  985.      * into an appropriate number of <script> tags. This is to beautify your minify strings (or else they might get too long).
  986.      * If you don't like such behaviour, change the limit to something higher, e.g. 50.
  987.      */
  988.     function minify_scripts($todo)
  989.     {
  990.         global $wp_scripts;
  991.         // Avoid conflict with WordPress 3.1
  992.         if (1 == sizeof($todo) && isset($todo[0]) && 'l10n' == $todo[0])
  993.             return array();
  994.  
  995.         // @since 1.0.5 - 1.0.6
  996.         $this->print_positions['header']  = apply_filters('bwp_minify_script_header', $this->print_positions['header']);
  997.         $this->print_positions['footer']  = apply_filters('bwp_minify_script_footer', $this->print_positions['footer']);
  998.         $this->print_positions['ignore']  = apply_filters('bwp_minify_script_ignore', $this->print_positions['ignore']);
  999.         $this->print_positions['direct']  = apply_filters('bwp_minify_script_direct', $this->print_positions['direct']);
  1000.         $this->print_positions['ignore_def']  = apply_filters('bwp_minify_script_ignore_def', $this->print_positions['ignore_def']);
  1001.         $this->print_positions['allowed'] = apply_filters('bwp_minify_allowed_scripts', 'all');
  1002.  
  1003.         $header_count = 0;
  1004.         $footer_count = 0;
  1005.         $count_f = 0; $count_h = 0;
  1006.  
  1007.         $total = sizeof($todo);
  1008.         $total_footer = 0;
  1009.         foreach ($wp_scripts->groups as $count)
  1010.             if (0 < $count)
  1011.                 $total_footer++;
  1012.         // Get the correct total
  1013.         foreach ($todo as $script_handle)
  1014.         {
  1015.             if ($this->is_in($script_handle, 'ignore') || $this->is_in($script_handle, 'ignore_def') || $this->is_in($script_handle, 'direct'))
  1016.             {
  1017.                 $total--;
  1018.                 if (isset($wp_scripts->groups[$script_handle]) && 1 == $wp_scripts->groups[$script_handle])
  1019.                     $total_footer--;
  1020.             }
  1021.         }
  1022.  
  1023.         $temp = array();
  1024.  
  1025.         foreach ($todo as $key => $script_handle)
  1026.         {
  1027.             $the_script = $wp_scripts->registered[$script_handle];
  1028.             // Take the src from registred scripts, do not proceed if the src is external
  1029.             $src = $the_script->src;
  1030.             $expected_in_footer = ((isset($wp_scripts->groups[$script_handle]) && 0 < $wp_scripts->groups[$script_handle]) || did_action('wp_footer')) ? true : false;
  1031.             $ignore_type = ($expected_in_footer) ? 'footer' : 'header';
  1032.             if (!empty($src) && $this->is_source_static($src) && $this->is_local($src))
  1033.             {
  1034.                 $src = $this->process_media_source($src);
  1035.                 // If this script is not allowed
  1036.                 if ('all' != $this->print_positions['allowed'] && !$this->is_in($script_handle, 'allowed'))
  1037.                     $this->ignores_script($script_handle, $temp, $the_script->deps, $ignore_type);
  1038.                 // If this script does not belong to 'direct' or 'ignore' list
  1039.                 else if (!$this->is_in($script_handle, 'ignore') && !$this->is_in($script_handle, 'ignore_def') && !$this->is_in($script_handle, 'direct'))
  1040.                 {
  1041.                     // If this script belongs to footer (logically or 'intentionally') and is not 'forced' to be in header
  1042.                     if (!$this->is_in($script_handle, 'header') && ($this->is_in($script_handle, 'footer') || $expected_in_footer))
  1043.                     {
  1044.                         // If footer scripts have already been printed, ignore this script
  1045.                         if (did_action('bwp_minify_printed_footer_scripts'))
  1046.                         {
  1047.                             if (!$this->is_added($src, 'scripts', $script_handle))
  1048.                                 $temp[] = $script_handle;
  1049.                             continue;
  1050.                         }
  1051.                         else if ($this->is_added($src, 'scripts', $script_handle))
  1052.                         // If footer scripts have not yet been printed, ignore this script if it was added before
  1053.                                 continue;
  1054.  
  1055.                         $count_f++; $footer_count++;
  1056.                         $this->append_minify_string($this->footer_scripts, $footer_count, $src, $count_f == $total_footer);
  1057.                         if (true == $this->is_l10n($script_handle))
  1058.                             $this->footer_l10n[] = $script_handle;
  1059.                     }
  1060.                     else if (true == $this->queueable)
  1061.                     {
  1062.                         // If header scripts have already been printed, ignore this script
  1063.                         if (did_action('bwp_minify_printed_header_scripts'))
  1064.                         {
  1065.                             if (!$this->is_added($src, 'scripts', $script_handle))
  1066.                                 $temp[] = $script_handle;
  1067.                             continue;
  1068.                         }
  1069.  
  1070.                         $count_h++; $header_count++;
  1071.                         $this->append_minify_string($this->header_scripts, $header_count, $src, $count_h == $total - $total_footer);
  1072.                         if (true == $this->is_l10n($script_handle))
  1073.                             $this->header_l10n[] = $script_handle;
  1074.                     }
  1075.                     else
  1076.                         $this->ignores_script($script_handle, $temp, $the_script->deps, $ignore_type);             
  1077.                 }
  1078.                 else
  1079.                 {
  1080.                     // If belongs to 'direct', minify it and let WordPress print it the normal way
  1081.                     if ($this->is_in($script_handle, 'direct') || $this->is_in($script_handle, 'ignore_def'))
  1082.                     {
  1083.                         $wp_scripts->registered[$script_handle]->src = $this->get_minify_src($src);
  1084.                         $wp_scripts->registered[$script_handle]->ver = NULL;
  1085.                     }
  1086.                    
  1087.                     $this->ignores_script($script_handle, $temp, $the_script->deps, $ignore_type);
  1088.                 }
  1089.             }
  1090.             else
  1091.                 $this->ignores_script($script_handle, $temp, $the_script->deps, $ignore_type);
  1092.         }
  1093.  
  1094.         //$this->queueable = false;
  1095.  
  1096.         return $temp;
  1097.     }
  1098.  
  1099.     /**
  1100.      * Main function to print out our scripts
  1101.      *
  1102.      * Use actions provided to add other things before or after the output.
  1103.      */
  1104.      
  1105.     function print_scripts($action = 'header')
  1106.     {
  1107.         do_action('bwp_minify_before_' . $action . '_scripts');
  1108.         // Print script tags
  1109.         $scripts = ('header' == $action) ? $this->header_scripts : $this->footer_scripts;
  1110.            
  1111.         foreach ($scripts as $script_array)
  1112.         {
  1113.             if (0 < sizeof($script_array))
  1114.                 // If setting Deferring actif
  1115.                 if ('yes' == $this->options['enable_deferring']){
  1116.                     // Deferring
  1117.                         $to_deferring = implode(',', $script_array);
  1118.                         $scheme_str = is_ssl() && !is_admin() ? 'https://' : 'http://';
  1119.                         $path_and_deferring = apply_filters('bwp_get_minify_src', trailingslashit(str_replace(array('http://', 'https://'), $scheme_str, $this->options['input_minurl'])) . '?f=' . $to_deferring, $this->options['input_minurl']);
  1120.                         $do_deferring .= '<script>' . "\r\n";                  
  1121.                         $do_deferring .= '$LAB'.'.script("'.$path_and_deferring.'").wait()';
  1122.                         $do_deferring .= ';' . "\r\n" . '</script>';
  1123.                     echo $do_deferring;
  1124.                     // End of deferring
  1125.                 }
  1126.                 else {
  1127.                  echo $this->get_minify_tag(implode(',', $script_array), 'script');
  1128.                 }
  1129.         do_action('bwp_minify_after_' . $action . '_scripts');
  1130.         }
  1131.     }
  1132.  
  1133.  
  1134.  
  1135.  
  1136.     function print_header_scripts()
  1137.         // If setting Deferring actif (verification if there is some script in header)
  1138.     {   if ('yes' == $this->options['enable_deferring']){
  1139.         $verif_header = ($this->header_scripts);
  1140.             foreach ($verif_header as $verif_header_string)
  1141.             // if there is some script in header > echo...
  1142.             if (!empty($verif_header_string)){
  1143.                 echo '<script src="http://cdnjs.cloudflare.com/ajax/libs/labjs/2.0.3/LAB.min.js"></script>' . "\r\n";}}
  1144.         $this->print_scripts();
  1145.         do_action('bwp_minify_printed_header_scripts');
  1146.     }
  1147.  
  1148.     function print_footer_scripts()
  1149.         // If setting Deferring actif (verification if there is some script in header)
  1150.     {   if ('yes' == $this->options['enable_deferring']){
  1151.             $verif_header = ($this->header_scripts);
  1152.                 foreach ($verif_header as $verif_header_string)
  1153.                 // if there is no script in header > echo...
  1154.                 if (empty($verif_header_string)){
  1155.                     echo '<script src="http://cdnjs.cloudflare.com/ajax/libs/labjs/2.0.3/LAB.min.js"></script>' . "\r\n";}}
  1156.         $this->print_scripts('footer');
  1157.         do_action('bwp_minify_printed_footer_scripts');
  1158.     }
  1159.  
  1160.     function print_dynamic_scripts($type = 'header')
  1161.         {
  1162.                 global $wp_scripts;
  1163.  
  1164.                 $scripts = ('header' == $type) ? $this->header_dynamic : $this->footer_dynamic;
  1165.  
  1166.                 foreach ($scripts as $handle) {
  1167.                         if ( $wp_scripts->registered[$handle]->src !== false ) {
  1168.                                 $wp_scripts->do_item($handle);
  1169.                         }
  1170.                 }
  1171.         }
  1172.  
  1173.     function print_dynamic_header_scripts()
  1174.     {
  1175.         $this->print_dynamic_scripts();
  1176.     }
  1177.  
  1178.     function print_dynamic_footer_scripts()
  1179.     {
  1180.         $this->print_dynamic_scripts('footer');
  1181.     }
  1182.  
  1183.     function print_scripts_l10n($scripts)
  1184.     {
  1185.         global $wp_scripts;
  1186.         foreach ($scripts as $handle)
  1187.             if (version_compare($this->ver, '3.3', '>=')){
  1188.                 $wp_scripts->print_extra_script($handle);}
  1189.             else
  1190.                 $wp_scripts->print_scripts_l10n($handle);
  1191.     }
  1192.  
  1193.     function print_header_scripts_l10n()
  1194.     {
  1195.         $this->print_scripts_l10n($this->header_l10n);
  1196.     }
  1197.  
  1198.     function print_footer_scripts_l10n()
  1199.     {
  1200.         $this->print_scripts_l10n($this->footer_l10n);
  1201.     }
  1202. }
  1203.  
  1204. ?>
Advertisement
Add Comment
Please, Sign In to add comment