Esempio n. 1
0
 /**
  * wp_print_styles()
  *
  * @return void
  **/
 static function wp_print_styles()
 {
     static $done = false;
     if ($done) {
         return;
     }
     $done = true;
     global $wp_styles;
     if (!$wp_styles instanceof WP_Styles) {
         $wp_styles = new WP_Styles();
     }
     $queue = $wp_styles->queue;
     $wp_styles->all_deps($queue);
     if (!$wp_styles->to_do) {
         return;
     }
     $todo = array();
     $css = array();
     $dirs = array(content_url(), plugins_url(), includes_url());
     foreach ($wp_styles->to_do as $key => $handle) {
         $cssPath = $wp_styles->registered[$handle]->src;
         if (!asset_cache::startsWith($cssPath, site_url())) {
             $cssPath = site_url() . $cssPath;
         }
         $inDir = false;
         foreach ($dirs as $dir) {
             if (asset_cache::startsWith($cssPath, $dir)) {
                 $inDir = true;
                 break;
             }
         }
         $suffixMatch = asset_cache::endsWith($cssPath, ".css");
         if ($inDir && $suffixMatch) {
             $css[$handle] = $wp_styles->registered[$handle]->ver;
             $todo[] = $handle;
             unset($wp_styles->to_do[$key]);
             $wp_styles->done[] = $handle;
         }
     }
     if ($todo) {
         $file = '/assets/' . md5(serialize($css)) . '.css';
         if (!cache_fs::exists($file)) {
             asset_cache::concat_styles($file, $todo);
         }
         $wp_styles->default_version = null;
         wp_enqueue_style('styles_concat', content_url() . '/cache' . $file);
     }
     //		$wp_styles->do_concat = true;
     //		$wp_styles->do_items();
 }
Esempio n. 2
0
function sp_combine_plugin_css_files()
{
    global $sp_plugin_styles, $spDevice;
    if (!is_a($sp_plugin_styles, 'WP_Styles')) {
        $sp_plugin_styles = new WP_Styles();
    }
    # save copy of styles in case of failure writing
    $saved_styles = clone $sp_plugin_styles;
    # check for standard theme or mobile
    if ($spDevice == 'mobile') {
        $option = 'sp_css_concat_mobile';
    } else {
        if ($spDevice == 'tablet') {
            $option = 'sp_css_concat_tablet';
        } else {
            $option = 'sp_css_concat';
        }
    }
    $css_concat = sp_get_option($option);
    if (!is_array($css_concat)) {
        $css_concat = array();
    }
    $css_files_modify = array();
    $css_files = array();
    if (is_array($sp_plugin_styles->queue)) {
        # is there anything in the queue?
        $sp_plugin_styles->all_deps($sp_plugin_styles->queue);
        # preparing the queue taking dependencies into account
        foreach ($css_concat as $css => $value) {
            # going through all the already found css files, checking that they are still required
            if (!in_array(substr($css, 4), $sp_plugin_styles->to_do) && substr($css, 0, 4) == 'css-') {
                # if the css is not queued, rewrite the file
                $css_media = $value['type'];
                $css_files_modify[$css_media] = true;
                unset($css_concat[$css]);
            }
        }
        foreach ($sp_plugin_styles->to_do as $css) {
            $css_src = $sp_plugin_styles->registered[$css]->src;
            $css_media = $sp_plugin_styles->registered[$css]->args;
            # is the css is hosted localy AND is a css file?
            if ((!(strpos($css_src, get_bloginfo('url')) === false) || substr($css_src, 0, 1) === '/' || substr($css_src, 0, 1) === '.') && (substr($css_src, strrpos($css_src, '.'), 4) == '.css' || substr($css_src, strrpos($css_src, '.'), 4) == '.php')) {
                if (!is_array($css_files) || !array_key_exists($css_media, $css_files)) {
                    $css_files[$css_media] = array();
                }
                if (strpos($css_src, get_bloginfo('url')) === false) {
                    $css_relative_url = substr($css_src, 1);
                } else {
                    $css_relative_url = substr($css_src, strlen(get_bloginfo('url')) + 1);
                }
                if (strpos($css_relative_url, '?')) {
                    $css_relative_url = substr($css_relative_url, 0, strpos($css_relative_url, '?'));
                }
                # removing parameters
                $css_m_time = null;
                @($css_m_time = filemtime($css_relative_url));
                # getting the mofified time of the css file. extracting the file's dir
                if ($css_m_time) {
                    # only add the file if it's accessible
                    # check for php theme file indicating main theme file and save whole url vs just relative
                    if (substr($css_src, strrpos($css_src, '.'), 4) == '.php') {
                        array_push($css_files[$css_media], $css_src);
                    } else {
                        array_push($css_files[$css_media], $css_relative_url);
                    }
                    if (!file_exists(SP_COMBINED_CACHE_DIR . SP_COMBINED_CSS_BASE_NAME . $css_media . '.css') || isset($css_concat['css-' . $css]) && ($css_m_time != $css_concat['css-' . $css]['modified'] || $css_concat['css-' . $css]['type'] != $css_media) || !isset($css_concat['css-' . $css])) {
                        # css file is first identified
                        $css_files_modify[$css_media] = true;
                        # the combined file containing this media type css should be changed
                        if (isset($css_concat['css-' . $css]) && $css_concat['css-' . $css]['type'] != $css_media) {
                            # if the media type changed - rewrite both css files
                            $tmp = $css_concat['css-' . $css]['type'];
                            $css_files_modify[$tmp] = true;
                        }
                        if (!is_array($css_concat['css-' . $css])) {
                            $css_concat['css-' . $css] = array();
                        }
                        $css_concat['css-' . $css]['modified'] = $css_m_time;
                        # write the new modified date
                        $css_concat['css-' . $css]['type'] = $css_media;
                    }
                    $sp_plugin_styles->remove($css);
                    # removes the css file from the queue
                }
            }
        }
    }
    foreach ($css_files_modify as $key => $value) {
        $combined_file = fopen(SP_COMBINED_CACHE_DIR . SP_COMBINED_CSS_BASE_NAME . $key . '.css', 'w');
        if ($combined_file) {
            $css_content = '';
            if (is_array($css_files[$key])) {
                foreach ($css_files[$key] as $css_src) {
                    $css_content .= "\n" . sp_get_css_content($css_src) . "\n";
                }
            }
            if (!isset($css_concat['ver'][$key])) {
                $css_concat['ver'][$key] = 0;
            }
            $css_concat['ver'][$key]++;
            # compress the css before writing it out
            require 'sp-api-class-css-compressor.php';
            $css_content = Minify_CSS_Compressor::process($css_content);
            fwrite($combined_file, $css_content);
            fclose($combined_file);
        } else {
            # couldnt open file for writing so revert back to enqueueing all the styles
            if (!empty($saved_styles)) {
                foreach ($saved_styles->queue as $handle) {
                    wp_enqueue_style($handle, $saved_styles->registered[$handle]->src);
                }
            }
            return;
            # enqueued through wp now so bail
        }
    }
    foreach ($css_files as $key => $value) {
        # enqueue the combined css files
        wp_enqueue_style(SP_COMBINED_CSS_BASE_NAME . $key, SP_COMBINED_CACHE_URL . SP_COMBINED_CSS_BASE_NAME . $key . '.css', array(), $css_concat['ver'][$key]);
    }
    sp_update_option($option, $css_concat);
}