Update to latest tinyMCE compressor. Simplify MCE language loading. see #3882
git-svn-id: https://develop.svn.wordpress.org/trunk@5108 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
068b0f0957
commit
a2470ad625
@ -1,10 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* $RCSfile: tiny_mce_gzip.php,v $
|
* $Id: tiny_mce_gzip.php 158 2006-12-21 14:32:19Z spocke $
|
||||||
* $Revision: $
|
|
||||||
* $Date: $
|
|
||||||
*
|
*
|
||||||
* @version 1.08
|
|
||||||
* @author Moxiecode
|
* @author Moxiecode
|
||||||
* @copyright Copyright 2005-2006, Moxiecode Systems AB, All rights reserved.
|
* @copyright Copyright 2005-2006, Moxiecode Systems AB, All rights reserved.
|
||||||
*
|
*
|
||||||
@ -13,201 +10,223 @@
|
|||||||
* Notice: This script defaults the button_tile_map option to true for extra performance.
|
* Notice: This script defaults the button_tile_map option to true for extra performance.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@require_once('../../../wp-config.php');
|
@require_once('../../../wp-config.php'); // For get_bloginfo().
|
||||||
|
|
||||||
// gzip_compression();
|
// Get input
|
||||||
|
$plugins = explode(',', getParam("plugins", ""));
|
||||||
|
$languages = explode(',', getParam("languages", ""));
|
||||||
|
$themes = explode(',', getParam("themes", ""));
|
||||||
|
$diskCache = getParam("diskcache", "") == "true";
|
||||||
|
$isJS = getParam("js", "") == "true";
|
||||||
|
$compress = getParam("compress", "true") == "true";
|
||||||
|
$suffix = getParam("suffix", "_src") == "_src" ? "_src" : "";
|
||||||
|
$cachePath = realpath("."); // Cache path, this is where the .gz files will be stored
|
||||||
|
$expiresOffset = 3600 * 24 * 10; // Cache for 10 days in browser cache
|
||||||
|
$content = "";
|
||||||
|
$encodings = array();
|
||||||
|
$supportsGzip = false;
|
||||||
|
$enc = "";
|
||||||
|
$cacheKey = "";
|
||||||
|
|
||||||
function wp_tinymce_lang($path) {
|
// Custom extra javascripts to pack
|
||||||
global $language;
|
$custom = array(/*
|
||||||
|
"some custom .js file",
|
||||||
|
"some custom .js file"
|
||||||
|
*/);
|
||||||
|
|
||||||
$text = '';
|
// WP
|
||||||
|
$index = getParam("index", -1);
|
||||||
// Look for xx_YY.js, xx_yy.js, xx.js
|
$theme = getParam("theme", "");
|
||||||
$file = realpath(sprintf($path, $language));
|
$themes = array($theme);
|
||||||
if ( file_exists($file) )
|
$language = getParam("language", "en");
|
||||||
$text = file_get_contents($file);
|
if ( empty($language) )
|
||||||
$file = realpath(sprintf($path, strtolower($language)));
|
$language = 'en';
|
||||||
if ( file_exists($file) )
|
$languages = array($language);
|
||||||
$text = file_get_contents($file);
|
if ( $language != strtolower($language) )
|
||||||
$file = realpath(sprintf($path, substr($language, 0, 2)));
|
$languages[] = strtolower($language);
|
||||||
if ( file_exists($file) )
|
if ( $language != substr($language, 0, 2) )
|
||||||
$text = file_get_contents($file);
|
$languages[] = substr($language, 0, 2);
|
||||||
|
|
||||||
|
|
||||||
// Fall back on en.js
|
|
||||||
$file = realpath(sprintf($path, 'en'));
|
|
||||||
if ( empty($text) && file_exists($file) )
|
|
||||||
$text = file_get_contents($file);
|
|
||||||
|
|
||||||
// Send lang file through gettext
|
|
||||||
if ( function_exists('__') && strtolower(substr($language, 0, 2)) != 'en' ) {
|
|
||||||
$search1 = "/^tinyMCELang\\[(['\"])(.*)\\1\]( ?= ?)(['\"])(.*)\\4/Uem";
|
|
||||||
$replace1 = "'tinyMCELang[\\1\\2\\1]\\3'.stripslashes('\\4').__('\\5').stripslashes('\\4')";
|
|
||||||
|
|
||||||
$search2 = "/\\s:\\s(['\"])(.*)\\1(,|\\s*})/Uem";
|
|
||||||
$replace2 = "' : '.stripslashes('\\1').__('\\2').stripslashes('\\1').'\\3'";
|
|
||||||
|
|
||||||
$search = array($search1, $search2);
|
|
||||||
$replace = array($replace1, $replace2);
|
|
||||||
|
|
||||||
$text = preg_replace($search, $replace, $text);
|
|
||||||
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $text;
|
|
||||||
}
|
|
||||||
|
|
||||||
function wp_compact_tinymce_js($text) {
|
|
||||||
// This function was custom-made for TinyMCE 2.0, not expected to work with any other JS.
|
|
||||||
|
|
||||||
// Strip comments
|
|
||||||
$text = preg_replace("!(^|\s+)//.*$!m", '', $text);
|
|
||||||
$text = preg_replace("!/\*.*?\*/!s", '', $text);
|
|
||||||
|
|
||||||
// Strip leading tabs, carriage returns and unnecessary line breaks.
|
|
||||||
$text = preg_replace("!^\t+!m", '', $text);
|
|
||||||
$text = str_replace("\r", '', $text);
|
|
||||||
$text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text);
|
|
||||||
|
|
||||||
return "$text\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// General options
|
|
||||||
$suffix = ""; // Set to "_src" to use source version
|
|
||||||
$expiresOffset = 3600 * 24 * 10; // 10 days util client cache expires
|
|
||||||
$diskCache = false; // If you enable this option gzip files will be cached on disk.
|
|
||||||
$cacheDir = realpath("."); // Absolute directory path to where cached gz files will be stored
|
|
||||||
$debug = false; // Enable this option if you need debuging info
|
|
||||||
|
|
||||||
// Headers
|
|
||||||
header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
|
|
||||||
// header("Cache-Control: must-revalidate");
|
|
||||||
header("Vary: Accept-Encoding"); // Handle proxies
|
|
||||||
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
|
|
||||||
|
|
||||||
// Get data to load
|
|
||||||
$theme = isset($_GET['theme']) ? TinyMCE_cleanInput($_GET['theme']) : "";
|
|
||||||
$language = isset($_GET['language']) ? TinyMCE_cleanInput($_GET['language']) : "";
|
|
||||||
$plugins = isset($_GET['plugins']) ? TinyMCE_cleanInput($_GET['plugins']) : "";
|
|
||||||
$lang = isset($_GET['lang']) ? TinyMCE_cleanInput($_GET['lang']) : "en";
|
|
||||||
$index = isset($_GET['index']) ? TinyMCE_cleanInput($_GET['index']) : -1;
|
|
||||||
$cacheKey = md5($theme . $language . $plugins . $lang . $index . $debug);
|
|
||||||
$cacheFile = $cacheDir == "" ? "" : $cacheDir . "/" . "tinymce_" . $cacheKey . ".gz";
|
|
||||||
$cacheData = "";
|
|
||||||
|
|
||||||
// Patch older versions of PHP < 4.3.0
|
|
||||||
if (!function_exists('file_get_contents')) {
|
|
||||||
function file_get_contents($filename) {
|
|
||||||
$fd = fopen($filename, 'rb');
|
|
||||||
$content = fread($fd, filesize($filename));
|
|
||||||
fclose($fd);
|
|
||||||
return $content;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Security check function, can only contain a-z 0-9 , _ - and whitespace.
|
|
||||||
function TinyMCE_cleanInput($str) {
|
|
||||||
return preg_replace("/[^0-9a-z\-_,]+/i", "", $str); // Remove anything but 0-9,a-z,-_
|
|
||||||
}
|
|
||||||
|
|
||||||
function TinyMCE_echo($str) {
|
|
||||||
global $cacheData, $diskCache;
|
|
||||||
|
|
||||||
if ($diskCache)
|
|
||||||
$cacheData .= $str;
|
|
||||||
else
|
|
||||||
echo $str;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only gzip the contents if clients and server support it
|
|
||||||
$encodings = array();
|
|
||||||
|
|
||||||
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
|
|
||||||
$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
|
|
||||||
|
|
||||||
// Check for gzip header or northon internet securities
|
|
||||||
if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
|
|
||||||
$enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
|
|
||||||
|
|
||||||
// Use cached file if it exists but not in debug mode
|
|
||||||
if (file_exists($cacheFile) && !$debug) {
|
|
||||||
header("Content-Encoding: " . $enc);
|
|
||||||
echo file_get_contents($cacheFile);
|
|
||||||
die;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$diskCache)
|
|
||||||
ob_start("ob_gzhandler");
|
|
||||||
} else
|
|
||||||
$diskCache = false;
|
$diskCache = false;
|
||||||
|
$isJS = true;
|
||||||
|
$suffix = '';
|
||||||
|
|
||||||
|
// Headers
|
||||||
|
header("Content-Type: text/javascript; charset=" . get_bloginfo('charset'));
|
||||||
|
header("Vary: Accept-Encoding"); // Handle proxies
|
||||||
|
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expiresOffset) . " GMT");
|
||||||
|
|
||||||
|
// Is called directly then auto init with default settings
|
||||||
|
if (!$isJS) {
|
||||||
|
echo getFileContents("tiny_mce_gzip.js");
|
||||||
|
echo "tinyMCE_GZ.init({});";
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Setup cache info
|
||||||
|
if ($diskCache) {
|
||||||
|
if (!$cachePath)
|
||||||
|
die("alert('Real path failed.');");
|
||||||
|
|
||||||
|
$cacheKey = getParam("plugins", "") . getParam("languages", "") . getParam("themes", "");
|
||||||
|
|
||||||
|
foreach ($custom as $file)
|
||||||
|
$cacheKey .= $file;
|
||||||
|
|
||||||
|
$cacheKey = md5($cacheKey);
|
||||||
|
|
||||||
|
if ($compress)
|
||||||
|
$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".gz";
|
||||||
|
else
|
||||||
|
$cacheFile = $cachePath . "/tiny_mce_" . $cacheKey . ".js";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if it supports gzip
|
||||||
|
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']))
|
||||||
|
$encodings = explode(',', strtolower(preg_replace("/\s+/", "", $_SERVER['HTTP_ACCEPT_ENCODING'])));
|
||||||
|
|
||||||
|
if ((in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------'])) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression')) {
|
||||||
|
$enc = in_array('x-gzip', $encodings) ? "x-gzip" : "gzip";
|
||||||
|
$supportsGzip = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use cached file disk cache
|
||||||
|
if ($diskCache && $supportsGzip && file_exists($cacheFile)) {
|
||||||
|
if ($compress)
|
||||||
|
header("Content-Encoding: " . $enc);
|
||||||
|
|
||||||
|
echo getFileContents($cacheFile);
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
|
||||||
if ($index > -1) {
|
if ($index > -1) {
|
||||||
// Write main script and patch some things
|
// Write main script and patch some things
|
||||||
if ($index == 0) {
|
if ( $index == 0 ) {
|
||||||
TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("tiny_mce" . $suffix . ".js")))); // WP
|
// Add core
|
||||||
TinyMCE_echo('TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;');
|
$content .= wp_compact_tinymce_js(getFileContents("tiny_mce" . $suffix . ".js"));
|
||||||
TinyMCE_echo('TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;');
|
$content .= 'TinyMCE.prototype.orgLoadScript = TinyMCE.prototype.loadScript;';
|
||||||
|
$content .= 'TinyMCE.prototype.loadScript = function() {};var realTinyMCE = tinyMCE;';
|
||||||
} else
|
} else
|
||||||
TinyMCE_echo('tinyMCE = realTinyMCE;');
|
$content .= 'tinyMCE = realTinyMCE;';
|
||||||
|
|
||||||
|
// Patch loading functions
|
||||||
|
//$content .= "tinyMCE_GZ.start();";
|
||||||
|
|
||||||
// Do init based on index
|
// Do init based on index
|
||||||
TinyMCE_echo("tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);");
|
$content .= "tinyMCE.init(tinyMCECompressed.configs[" . $index . "]);";
|
||||||
|
|
||||||
// Load external plugins
|
// Load external plugins
|
||||||
if ($index == 0)
|
if ( $index == 0 )
|
||||||
TinyMCE_echo("tinyMCECompressed.loadPlugins();");
|
$content .= "tinyMCECompressed.loadPlugins();";
|
||||||
|
|
||||||
// Load theme, language pack and theme language packs
|
// Add core languages
|
||||||
if ($theme) {
|
foreach ($languages as $lang)
|
||||||
TinyMCE_echo(wp_compact_tinymce_js(file_get_contents(realpath("themes/" . $theme . "/editor_template" . $suffix . ".js")))); // WP
|
$content .= getFileContents("langs/" . $lang . ".js");
|
||||||
TinyMCE_echo(wp_tinymce_lang("themes/" . $theme . "/langs/%s.js")); // WP
|
|
||||||
|
// Add themes
|
||||||
|
foreach ($themes as $theme) {
|
||||||
|
$content .= wp_compact_tinymce_js(getFileContents( "themes/" . $theme . "/editor_template" . $suffix . ".js"));
|
||||||
|
|
||||||
|
foreach ($languages as $lang)
|
||||||
|
$content .= getFileContents("themes/" . $theme . "/langs/" . $lang . ".js");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* WP if ($language) WP */
|
// Add plugins
|
||||||
TinyMCE_echo(wp_tinymce_lang("langs/%s.js")); // WP
|
|
||||||
|
|
||||||
// Load all plugins and their language packs
|
|
||||||
$plugins = explode(",", $plugins);
|
|
||||||
foreach ($plugins as $plugin) {
|
foreach ($plugins as $plugin) {
|
||||||
$pluginFile = realpath("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
|
$content .= getFileContents("plugins/" . $plugin . "/editor_plugin" . $suffix . ".js");
|
||||||
/* WP $languageFile = realpath("plugins/" . $plugin . "/langs/" . $lang . ".js"); WP */
|
|
||||||
|
|
||||||
if ($pluginFile)
|
foreach ($languages as $lang)
|
||||||
TinyMCE_echo(file_get_contents($pluginFile));
|
$content .= getFileContents("plugins/" . $plugin . "/langs/" . $lang . ".js");
|
||||||
|
|
||||||
/* WP if ($languageFile) WP */
|
|
||||||
TinyMCE_echo(wp_tinymce_lang("plugins/" . $plugin . "/langs/%s.js")); // WP
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add custom files
|
||||||
|
foreach ($custom as $file)
|
||||||
|
$content .= getFileContents($file);
|
||||||
|
|
||||||
// Reset tinyMCE compressor engine
|
// Reset tinyMCE compressor engine
|
||||||
TinyMCE_echo("tinyMCE = tinyMCECompressed;");
|
$content .= "tinyMCE = tinyMCECompressed;";
|
||||||
|
|
||||||
// Write to cache
|
// Restore loading functions
|
||||||
if ($diskCache) {
|
//$content .= "tinyMCE_GZ.end();";
|
||||||
// Calculate compression ratio and debug target output path
|
|
||||||
if ($debug) {
|
|
||||||
$ratio = round(100 - strlen(gzencode($cacheData, 9, FORCE_GZIP)) / strlen($cacheData) * 100.0);
|
|
||||||
TinyMCE_echo("alert('TinyMCE was compressed by " . $ratio . "%.\\nOutput cache file: " . $cacheFile . "');");
|
|
||||||
}
|
|
||||||
|
|
||||||
$cacheData = gzencode($cacheData, 9, FORCE_GZIP);
|
// Generate GZIP'd content
|
||||||
|
if ($supportsGzip) {
|
||||||
|
if ($compress) {
|
||||||
|
header("Content-Encoding: " . $enc);
|
||||||
|
$cacheData = gzencode($content, 9, FORCE_GZIP);
|
||||||
|
} else
|
||||||
|
$cacheData = $content;
|
||||||
|
|
||||||
// Write to file if possible
|
// Write gz file
|
||||||
$fp = @fopen($cacheFile, "wb");
|
if ($diskCache && $cacheKey != "")
|
||||||
if ($fp) {
|
putFileContents($cacheFile, $cacheData);
|
||||||
fwrite($fp, $cacheData);
|
|
||||||
fclose($fp);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Output
|
// Stream to client
|
||||||
header("Content-Encoding: " . $enc);
|
|
||||||
echo $cacheData;
|
echo $cacheData;
|
||||||
|
} else {
|
||||||
|
// Stream uncompressed content
|
||||||
|
echo $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
|
||||||
|
function getParam($name, $def = false) {
|
||||||
|
if (!isset($_GET[$name]))
|
||||||
|
return $def;
|
||||||
|
|
||||||
|
return preg_replace("/[^0-9a-z\-_,]+/i", "", $_GET[$name]); // Remove anything but 0-9,a-z,-_
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFileContents($path) {
|
||||||
|
$path = realpath($path);
|
||||||
|
|
||||||
|
if (!$path || !@is_file($path))
|
||||||
|
return "";
|
||||||
|
|
||||||
|
if (function_exists("file_get_contents"))
|
||||||
|
return @file_get_contents($path);
|
||||||
|
|
||||||
|
$content = "";
|
||||||
|
$fp = @fopen($path, "r");
|
||||||
|
if (!$fp)
|
||||||
|
return "";
|
||||||
|
|
||||||
|
while (!feof($fp))
|
||||||
|
$content .= fgets($fp);
|
||||||
|
|
||||||
|
fclose($fp);
|
||||||
|
|
||||||
|
return $content;
|
||||||
|
}
|
||||||
|
|
||||||
|
function putFileContents($path, $content) {
|
||||||
|
if (function_exists("file_put_contents"))
|
||||||
|
return @file_put_contents($path, $content);
|
||||||
|
|
||||||
|
$fp = @fopen($path, "wb");
|
||||||
|
if ($fp) {
|
||||||
|
fwrite($fp, $content);
|
||||||
|
fclose($fp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// WP specific
|
||||||
|
function wp_compact_tinymce_js($text) {
|
||||||
|
// This function was custom-made for TinyMCE 2.0, not expected to work with any other JS.
|
||||||
|
|
||||||
|
// Strip comments
|
||||||
|
$text = preg_replace("!(^|\s+)//.*$!m", '', $text);
|
||||||
|
$text = preg_replace("!/\*.*?\*/!s", '', $text);
|
||||||
|
|
||||||
|
// Strip leading tabs, carriage returns and unnecessary line breaks.
|
||||||
|
$text = preg_replace("!^\t+!m", '', $text);
|
||||||
|
$text = str_replace("\r", '', $text);
|
||||||
|
$text = preg_replace("!(^|{|}|;|:|\))\n!m", '\\1', $text);
|
||||||
|
|
||||||
|
return "$text\n";
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
function TinyMCECompressed() {
|
function TinyMCECompressed() {
|
||||||
|
@ -15,7 +15,7 @@ class WP_Scripts {
|
|||||||
$this->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' );
|
$this->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' );
|
||||||
$this->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '3517' );
|
$this->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '3517' );
|
||||||
$this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', false, '3517' );
|
$this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', false, '3517' );
|
||||||
$this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php', false, '20070124' );
|
$this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php', false, '20070325' );
|
||||||
$mce_config = apply_filters('tiny_mce_config_url', '/wp-includes/js/tinymce/tiny_mce_config.php');
|
$mce_config = apply_filters('tiny_mce_config_url', '/wp-includes/js/tinymce/tiny_mce_config.php');
|
||||||
$this->add( 'wp_tiny_mce', $mce_config, array('tiny_mce'), '20070225' );
|
$this->add( 'wp_tiny_mce', $mce_config, array('tiny_mce'), '20070225' );
|
||||||
$this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0-0');
|
$this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.0-0');
|
||||||
|
Loading…
Reference in New Issue
Block a user