Make WordPress Core


Ignore:
Timestamp:
02/11/2008 05:45:18 PM (18 years ago)
Author:
ryan
Message:

New gzip compressor for TinyMCE from azaozz. fixes #5807

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/js/tinymce/tiny_mce_gzip.php

    r6765 r6789  
    1 <?php
    2 /** Based on:
    3  * $Id: tiny_mce_gzip.php 315 2007-10-25 14:03:43Z spocke $
    4  *
    5  * @author Moxiecode
    6  * @copyright Copyright © 2005-2006, Moxiecode Systems AB, All rights reserved.
    7  *
    8  * This file compresses the TinyMCE JavaScript using GZip and
    9  * enables the browser to do two requests instead of one for each .js file.
    10  * Notice: This script defaults the button_tile_map option to true for extra performance.
    11  */
    12 
    13 @require_once('../../../wp-config.php');  // For get_bloginfo().
    14 cache_javascript_headers();
    15 
    16 if ( isset($_GET['load']) ) {
    17 
    18     function getParam( $name, $def = false ) {
    19         if ( ! isset($_GET[$name]) )
    20             return $def;
    21 
    22         return preg_replace( "/[^0-9a-z\-_,]+/i", "", $_GET[$name] ); // Remove anything but 0-9,a-z,-_
    23     }
    24 
    25     function getFileContents($path) {
    26         $path = realpath($path);
    27 
    28         if ( ! $path || !@is_file($path) )
    29             return '';
    30 
    31         if ( function_exists('file_get_contents') )
    32             return @file_get_contents($path);
    33 
    34         $content = '';
    35         $fp = @fopen( $path, 'r' );
    36         if (!$fp)
    37             return '';
    38 
    39         while ( ! feof($fp) )
    40             $content .= fgets($fp);
    41 
    42         fclose($fp);
    43 
    44         return $content;
    45     }
    46 
    47     function putFileContents( $path, $content ) {
    48         if ( function_exists('file_put_contents') )
    49             return @file_put_contents( $path, $content );
    50 
    51         $fp = @fopen($path, 'wb');
    52         if ($fp) {
    53             fwrite($fp, $content);
    54             fclose($fp);
    55         }
    56     }
    57 
    58     // WP defaults
    59     $themes = explode( ',', getParam('themes', 'advanced') );
    60 
    61     $language = getParam( 'languages', 'en' );
    62     $language = strtolower( substr($language, 0, 2) ); // only ISO 639-1
    63 
    64     $plugins = explode( ',', getParam('plugins', '') );
    65     $cachePath = realpath('.'); // Cache path, this is where the .gz files will be stored
    66 
    67     $encodings = array();
    68     $supportsGzip = $diskCache = false;
    69     $compress = $core = true;
    70     $suffix = $content = $enc = $cacheKey = '';
    71 
    72     // Custom extra javascripts to pack
    73     // WP - add a hook for external plugins to be compressed too?
    74     $custom = array(/*
    75         'some custom .js file',
    76         'some custom .js file'
    77     */);
    78 
    79     // Setup cache info
    80     if ( $diskCache ) {
    81         if ( ! $cachePath )
    82             die('Real path failed.');
    83 
    84         $cacheKey = getParam( 'plugins', '' ) . getParam( 'languages', '' ) . getParam( 'themes', '' ) . $suffix;
    85 
    86         foreach ( $custom as $file )
    87             $cacheKey .= $file;
    88 
    89         $cacheKey = md5($cacheKey);
    90 
    91         if ( $compress )
    92             $cacheFile = $cachePath . '/tiny_mce_' . $cacheKey . '.gz';
    93         else
    94             $cacheFile = $cachePath . '/tiny_mce_' . $cacheKey . '.js';
    95     }
    96 
    97     // Check if it supports gzip
    98     if ( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) )
    99         $encodings = explode(',', strtolower(preg_replace( '/\s+/', '', $_SERVER['HTTP_ACCEPT_ENCODING']) ) );
    100 
    101     if ( ( in_array('gzip', $encodings ) || in_array( 'x-gzip', $encodings ) || isset($_SERVER['---------------']) ) && function_exists( 'ob_gzhandler' ) && ! ini_get('zlib.output_compression') ) {
    102         $enc = in_array( 'x-gzip', $encodings ) ? 'x-gzip' : 'gzip';
    103         $supportsGzip = true;
    104     }
    105 
    106     // Use cached file disk cache
    107     if ( $diskCache && $supportsGzip && file_exists($cacheFile) ) {
    108         if ( $compress )
    109             header('Content-Encoding: ' . $enc);
    110 
    111         echo getFileContents( $cacheFile );
    112         die();
    113     }
    114 
    115     // Add core
    116     if ( $core == 'true' ) {
    117         $content .= getFileContents( 'tiny_mce' . $suffix . '.js' );
    118 
    119         // Patch loading functions
    120         $content .= 'tinyMCE_GZ.start();';
    121     }
    122 
    123     // Add all languages (WP)
    124     include_once( dirname(__file__).'/langs/wp-langs.php' );
    125     $content .= $strings;
    126 
    127     // Add themes
    128     foreach ( $themes as $theme )
    129         $content .= getFileContents( 'themes/' . $theme . '/editor_template' . $suffix . '.js' );
    130    
    131     // Add plugins
    132     foreach ( $plugins as $plugin )
    133         $content .= getFileContents( 'plugins/' . $plugin . '/editor_plugin' . $suffix . '.js' );
    134 
    135     // Add custom files
    136     foreach ( $custom as $file )
    137         $content .= getFileContents($file);
    138 
    139     // Restore loading functions
    140     if ( $core == 'true' )
    141         $content .= 'tinyMCE_GZ.end();';
    142 
    143     // Generate GZIP'd content
    144     if ( $supportsGzip ) {
    145         if ( $compress ) {
    146             header('Content-Encoding: ' . $enc);
    147             $cacheData = gzencode( $content, 9, FORCE_GZIP );
    148         } else
    149             $cacheData = $content;
    150 
    151         // Write gz file
    152         if ( $diskCache && '' != $cacheKey )
    153             putFileContents( $cacheFile, $cacheData );
    154 
    155         // Stream to client
    156         echo $cacheData;
    157     } else {
    158         // Stream uncompressed content
    159         echo $content;
    160     }
    161 
    162     exit;
    163 }
    164 ?>
    165 
    166 var tinyMCEPreInit = {suffix : ''};
    167 
    168 var tinyMCE_GZ = {
    169     settings : {
    170         themes : '',
    171         plugins : '',
    172         languages : '',
    173         disk_cache : false,
    174         page_name : 'tiny_mce_gzip.php',
    175         debug : false,
    176         suffix : ''
    177     },
    178 
    179     opt : {},
    180 
    181     init : function(o, cb) {
    182         var t = this, n, s = t.settings, nl = document.getElementsByTagName('script');
    183 
    184         t.opt = o;
    185 
    186         s.themes = o.theme;
    187         s.plugins = o.plugins;
    188         s.languages = o.language;
    189         t.settings = s;
    190 
    191         t.cb = cb || '';
    192 
    193         for (i=0; i<nl.length; i++) {
    194             n = nl[i];
    195 
    196             if (n.src && n.src.indexOf('tiny_mce') != -1)
    197                 t.baseURL = n.src.substring(0, n.src.lastIndexOf('/'));
    198         }
    199         tinyMCEPreInit.base = t.baseURL;
    200        
    201         if (!t.coreLoaded)
    202             t.loadScripts(1, s.themes, s.plugins, s.languages);
    203     },
    204 
    205     loadScripts : function(co, th, pl, la, cb, sc) {
    206         var t = this, x, w = window, q, c = 0, ti, s = t.settings;
    207 
    208         function get(s) {
    209             x = 0;
    210 
    211             try {
    212                 x = new ActiveXObject(s);
    213             } catch (s) {
    214             }
    215 
    216             return x;
    217         };
    218 
    219         // Build query string
    220         q = 'load=true&js=true&diskcache=' + (s.disk_cache ? 'true' : 'false') + '&core=' + (co ? 'true' : 'false') + '&suffix=' + escape(s.suffix) + '&themes=' + escape(th) + '&plugins=' + escape(pl) + '&languages=' + escape(la);
    221 
    222         if (co)
    223             t.coreLoaded = 1;
    224 
    225     //  document.write('<sc'+'ript language="javascript" type="text/javascript" src="' + t.baseURL + '/' + s.page_name + '?' + q + '"></script>');
    226 
    227         // Send request
    228         x = w.XMLHttpRequest ? new XMLHttpRequest() : get('Msxml2.XMLHTTP') || get('Microsoft.XMLHTTP');
    229         x.overrideMimeType && x.overrideMimeType('text/javascript');
    230         x.open('GET', t.baseURL + '/' + s.page_name + '?' + q, !!cb);
    231 //      x.setRequestHeader('Content-Type', 'text/javascript');
    232         x.send('');
    233 
    234         // Handle asyncronous loading
    235         if (cb) {
    236             // Wait for response
    237             ti = w.setInterval(function() {
    238                 if (x.readyState == 4 || c++ > 10000) {
    239                     w.clearInterval(ti);
    240 
    241                     if (c < 10000 && x.status == 200) {
    242                         t.loaded = 1;
    243                         t.eval(x.responseText);
    244                         tinymce.dom.Event.domLoaded = true;
    245                     //  cb.call(sc || t, x);
    246                     }
    247 
    248                     ti = x = null;
    249                 }
    250             }, 10);
    251         } else
    252             t.eval(x.responseText);
    253     },
    254 
    255     start : function() {
    256         var t = this, each = tinymce.each, s = t.settings, sl, ln = s.languages.split(',');
    257 
    258         tinymce.suffix = s.suffix;
    259 
    260         // Extend script loader
    261         tinymce.create('tinymce.compressor.ScriptLoader:tinymce.dom.ScriptLoader', {
    262             loadScripts : function(sc, cb, s) {
    263                 var ti = this, th = [], pl = [], la = [];
    264 
    265                 each(sc, function(o) {
    266                     var u = o.url;
    267 
    268                     if ((!ti.lookup[u] || ti.lookup[u].state != 2) && u.indexOf(t.baseURL) === 0) {
    269                         // Collect theme
    270                         if (u.indexOf('editor_template') != -1) {
    271                             th.push(/\/themes\/([^\/]+)/.exec(u)[1]);
    272                             load(u, 1);
    273                         }
    274 
    275                         // Collect plugin
    276                         if (u.indexOf('editor_plugin') != -1) {
    277                             pl.push(/\/plugins\/([^\/]+)/.exec(u)[1]);
    278                             load(u, 1);
    279                         }
    280 
    281                         // Collect language
    282                         if (u.indexOf('/langs/') != -1) {
    283                             la.push(/\/langs\/([^.]+)/.exec(u)[1]);
    284                             load(u, 1);
    285                         }
    286                     }
    287                 });
    288 
    289                 if (th.length + pl.length + la.length > 0) {
    290                     if (sl.settings.strict_mode) {
    291                         // Async
    292                         t.loadScripts(0, th.join(','), pl.join(','), la.join(','), cb, s);
    293                         return;
    294                     } else
    295                         t.loadScripts(0, th.join(','), pl.join(','), la.join(','), cb, s);
    296                 }
    297 
    298                 return ti.parent(sc, cb, s);
    299             }
    300         });
    301 
    302         sl = tinymce.ScriptLoader = new tinymce.compressor.ScriptLoader();
    303 
    304         function load(u, sp) {
    305             var o;
    306 
    307             if (!sp)
    308                 u = t.baseURL + u;
    309 
    310             o = {url : u, state : 2};
    311             sl.queue.push(o);
    312             sl.lookup[o.url] = o;
    313         };
    314 
    315         // Add core languages
    316         each (ln, function(c) {
    317             if (c)
    318                 load('/langs/' + c + '.js');
    319         });
    320 
    321         // Add themes with languages
    322         each(s.themes.split(','), function(n) {
    323             if (n) {
    324                 load('/themes/' + n + '/editor_template' + s.suffix + '.js');
    325 
    326                 each (ln, function(c) {
    327                     if (c)
    328                         load('/themes/' + n + '/langs/' + c + '.js');
    329                 });
    330             }
    331         });
    332 
    333         // Add plugins with languages
    334         each(s.plugins.split(','), function(n) {
    335             if (n) {
    336                 load('/plugins/' + n + '/editor_plugin' + s.suffix + '.js');
    337 
    338                 each (ln, function(c) {
    339                     if (c)
    340                         load('/plugins/' + n + '/langs/' + c + '.js');
    341                 });
    342             }
    343         });
    344     },
    345 
    346     end : function() {
    347        tinyMCE.init(this.opt);
    348     },
    349 
    350     eval : function(co) {
    351         var w = window;
    352 
    353         // Evaluate script
    354         if (!w.execScript) {
    355             try {
    356                 eval.call(w, co);
    357             } catch (ex) {
    358                 eval(co, w); // Firefox 3.0a8
    359             }
    360         } else
    361             w.execScript(co); // IE
    362     }
    363 };
Note: See TracChangeset for help on using the changeset viewer.