Make WordPress Core


Ignore:
Timestamp:
02/11/2008 05:45:18 PM (19 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_config.php

    r6765 r6789  
    1 <?php
    2         @ require('../../../wp-config.php');
    3         cache_javascript_headers();
    4 
    5         // deprecated
    6     function wp_translate_tinymce_lang($text) {
    7                 if ( ! function_exists('__') ) {
    8                         return $text;
    9                 } else {
    10                         $search1 = "/^tinyMCELang\\[(['\"])(.*)\\1\]( ?= ?)(['\"])(.*)\\4/Uem";
    11                         $replace1 = "'tinyMCELang[\\1\\2\\1]\\3'.stripslashes('\\4').__('\\5').stripslashes('\\4')";
    12 
    13                         $search2 = "/ : (['\"])(.*)\\1/Uem";
    14                         $replace2 = "' : '.stripslashes('\\1').__('\\2').stripslashes('\\1')";
    15 
    16                         $search = array($search1, $search2);
    17                         $replace = array($replace1, $replace2);
    18 
    19                         $text = preg_replace($search, $replace, $text);
    20 
    21                         return $text;
     1<?php
     2// some code below is from:
     3/**
     4 * $Id: tiny_mce_gzip.php 315 2007-10-25 14:03:43Z spocke $
     5 *
     6 * @author Moxiecode
     7 * @copyright Copyright © 2005-2006, Moxiecode Systems AB, All rights reserved.
     8 *
     9 * This file compresses the TinyMCE JavaScript using GZip.
     10 **/
     11 
     12@ require('../../../wp-config.php');
     13
     14function getFileContents($path) {
     15        $path = realpath($path);
     16
     17        if ( ! $path || ! @is_file($path) )
     18                return '';
     19
     20        if ( function_exists('file_get_contents') )
     21                return @file_get_contents($path);
     22
     23        $content = '';
     24        $fp = @fopen($path, 'r');
     25        if ( ! $fp )
     26                return '';
     27
     28        while ( ! feof($fp) )
     29                $content .= fgets($fp);
     30
     31        fclose($fp);
     32        return $content;
     33}
     34
     35function putFileContents( $path, $content ) {
     36        if ( function_exists('file_put_contents') )
     37                return @file_put_contents( $path, $content );
     38
     39        $fp = @fopen( $path, 'wb' );
     40        if ($fp) {
     41                fwrite( $fp, $content );
     42                fclose($fp);
     43        }
     44}
     45
     46// Set up init variables
     47$https = ( isset($_SERVER['HTTPS']) && 'on' == $_SERVER['HTTPS'] ) ? true : false;
     48       
     49$baseurl = get_option('siteurl') . '/wp-includes/js/tinymce';
     50$baseurl = $https ? str_replace('http://', 'https://', $baseurl) : $baseurl;
     51
     52$mce_css = $baseurl . '/wordpress.css';
     53$mce_css = apply_filters('mce_css', $mce_css);
     54$mce_css = $https ? str_replace('http://', 'https://', $mce_css) : $mce_css;
     55
     56$valid_elements = '*[*]';
     57$valid_elements = apply_filters('mce_valid_elements', $valid_elements);
     58       
     59$invalid_elements = apply_filters('mce_invalid_elements', '');
     60
     61$plugins = array( 'safari', 'inlinepopups', 'autosave', 'spellchecker', 'paste', 'wordpress', 'media', 'fullscreen' );
     62
     63/*
     64The following filter takes an associative array of external plugins for TinyMCE in the form "name" => "url".
     65It adds the plugin's name (including the required dash) to TinyMCE's plugins init and the call to PluginManager to load the plugin.
     66The url should be absolute and should include the js file name to be loaded.
     67Example: array( 'myplugin' => 'http://my-site.com/wp-content/plugins/myfolder/mce_plugin.js' ).
     68If the plugin uses a button, it should be added with one of the "$mce_buttons" filters.
     69*/
     70$mce_external_plugins = apply_filters('mce_external_plugins', array());
     71
     72$ext_plugins = "\n";
     73foreach ( $mce_external_plugins as $name => $url ) {
     74        $plugins[] = '-' . $name;
     75        $url = $https ? str_replace('http://', 'https://', $url) : $url;
     76       
     77        $ext_plugins .= 'tinymce.PluginManager.load("' . $name . '", "' . $url . '");' . "\n";
     78}
     79
     80$plugins = implode($plugins, ',');
     81
     82$mce_buttons = apply_filters('mce_buttons', array('bold', 'italic', 'strikethrough', '|', 'bullist', 'numlist', 'outdent', 'indent', '|', 'justifyleft', 'justifycenter', 'justifyright', '|', 'link', 'unlink', 'image', 'wp_more', '|', 'spellchecker', '|', 'wp_help', 'wp_adv' ));
     83$mce_buttons = implode($mce_buttons, ',');
     84
     85$mce_buttons_2 = apply_filters('mce_buttons_2', array('formatselect', 'underline', 'justifyfull', 'forecolor', '|', 'pastetext', 'pasteword', '|', 'removeformat', 'cleanup', '|', 'media', 'charmap', 'blockquote', '|', 'undo', 'redo', 'fullscreen' ));
     86$mce_buttons_2 = implode($mce_buttons_2, ',');
     87
     88$mce_buttons_3 = apply_filters('mce_buttons_3', array());
     89$mce_buttons_3 = implode($mce_buttons_3, ',');
     90       
     91$mce_buttons_4 = apply_filters('mce_buttons_4', array());
     92$mce_buttons_4 = implode($mce_buttons_4, ',');
     93
     94// all these browsers are now 100% supported, no need for this
     95//$mce_browsers = apply_filters('mce_browsers', array('msie', 'gecko', 'opera', 'safari'));
     96//$mce_browsers = implode($mce_browsers, ',');
     97
     98$mce_locale = ( '' == get_locale() ) ? 'en' : strtolower( substr(get_locale(), 0, 2) ); // only ISO 639-1
     99
     100// TinyMCE init settings
     101$initArray = array (
     102        'mode' => 'none',
     103        'onpageload' => 'wpEditorInit',
     104    'width' => '100%',
     105        'theme' => 'advanced',
     106        'skin' => 'wp_theme',
     107        'theme_advanced_buttons1' => "$mce_buttons",
     108        'theme_advanced_buttons2' => "$mce_buttons_2",
     109        'theme_advanced_buttons3' => "$mce_buttons_3",
     110        'theme_advanced_buttons4' => "$mce_buttons_4",
     111        'language' => "$mce_locale",
     112        'theme_advanced_toolbar_location' => 'top',
     113        'theme_advanced_toolbar_align' => 'left',
     114        'theme_advanced_statusbar_location' => 'bottom',
     115        'theme_advanced_resizing' => true,
     116        'theme_advanced_resize_horizontal' => false,
     117//      'browsers' => "$mce_browsers",
     118        'dialog_type' => 'modal',
     119        'convert_urls' => false,
     120        'relative_urls' => false,
     121        'remove_script_host' => false,
     122        'fix_list_elements' => true,
     123        'fix_table_elements' => true,
     124        'gecko_spellcheck' => true,
     125        'entities' => '38,amp,60,lt,62,gt',
     126        'accessibility_focus' => false,
     127        'tab_focus' => ':next',
     128        'content_css' => "$mce_css",
     129        'save_callback' => 'switchEditors.saveCallback',
     130        'plugins' => "$plugins",
     131        // pass-through the settings for compression and caching, so they can be changed with "tiny_mce_before_init"
     132        'disk_cache' => true,
     133        'compress' => true,
     134        'del_old_cache' => true
     135);
     136
     137if ( $valid_elements ) $initArray['valid_elements'] = $valid_elements;
     138if ( $invalid_elements ) $initArray['invalid_elements'] = $invalid_elements;
     139
     140// For people who really REALLY know what they're doing with TinyMCE
     141// You can modify initArray to add, remove, change elements of the config before tinyMCE.init
     142$initArray = apply_filters('tiny_mce_before_init', $initArray); // changed from action to filter
     143
     144// support for deprecated actions
     145ob_start();
     146do_action('mce_options');
     147$mce_deprecated1 = ob_get_contents() || '';
     148ob_end_clean();
     149
     150/*
     151// Do we need to support this? Most likely will breal TinyMCE 3...
     152ob_start();
     153do_action('tinymce_before_init');
     154$mce_deprecated2 = ob_get_contents() || '';
     155ob_end_clean();
     156*/
     157
     158// Settings for the gzip compression and cache
     159$cache_path = dirname(__FILE__); // Cache path, this is where the .gz files will be stored
     160$cache_ext = '.js';
     161
     162$disk_cache = ( ! isset($initArray['disk_cache']) || false == $initArray['disk_cache'] ) ? false : true;
     163$compress = ( ! isset($initArray['compress']) || false == $initArray['compress'] ) ? false : true;
     164$del_old_cache = ( ! isset($initArray['del_old_cache']) || false == $initArray['del_old_cache'] ) ? false : true;
     165
     166$initArray['disk_cache'] = $initArray['compress'] = $initArray['del_old_cache'] = null;
     167unset( $initArray['disk_cache'], $initArray['compress'], $initArray['del_old_cache'] );
     168
     169$plugins = explode( ',', $initArray['plugins'] );
     170$theme = ( 'simple' == $initArray['theme'] ) ? 'simple' : 'advanced';
     171$language = isset($initArray['language']) ? substr( $initArray['language'], 0, 2 ) : 'en';
     172$enc = $cacheKey = $suffix = $mce_options = '';
     173
     174// Custom extra javascripts to pack
     175$custom_js = array(); //$custom_js = apply_filters('tinymce_custom_js', array());
     176
     177// Check if supports gzip
     178if ( $compress && isset($_SERVER['HTTP_ACCEPT_ENCODING']) ) {
     179        $encodings = explode( ',', strtolower( preg_replace('/\s+/', '', $_SERVER['HTTP_ACCEPT_ENCODING']) ) );
     180
     181        if ( (in_array('gzip', $encodings) || in_array('x-gzip', $encodings) || isset($_SERVER['---------------']) ) && function_exists('ob_gzhandler') && !ini_get('zlib.output_compression') ) {
     182                $enc = in_array('x-gzip', $encodings) ? 'x-gzip' : 'gzip';
     183                $cache_ext = '.gz';
     184        }
     185}
     186
     187// Setup cache info
     188if ( $disk_cache && $cache_path ) {
     189
     190        $ver = isset($_GET['ver']) ? (int) $_GET['ver'] : '';
     191        $cacheKey = $initArray['plugins'] . $language . $theme . $suffix . $ver;
     192
     193        foreach ( $custom_js as $file )
     194                $cacheKey .= $file;
     195
     196        $cacheKey = md5( $cacheKey );
     197        $cache_file = $cache_path . '/tiny_mce_' . $cacheKey . $cache_ext;
     198}
     199
     200cache_javascript_headers();
     201
     202// Use cached file if exists
     203if ( $disk_cache && file_exists($cache_file) ) {
     204        if ( '.gz' == $cache_ext )
     205                header( 'Content-Encoding: ' . $enc );
     206
     207        echo getFileContents( $cache_file );
     208        exit;
     209}
     210
     211foreach ( $initArray as $k => $v )
     212    $mce_options .= $k . ':"' . $v . '", ';
     213
     214$mce_options .= $mce_deprecated1;
     215$mce_options = rtrim( trim($mce_options), '\n\r,' );
     216
     217$content = 'var tinyMCEPreInit = { suffix : "' . $suffix . '", base : "' . $baseurl . '" };';
     218$content .= 'var tinyMCE_GZ = { settings : { themes : "' . $theme . '", plugins : "' . $initArray['plugins'] . '", languages : "' . $language . '", debug : false, suffix : "' . $suffix . '" }, baseURL : "' . $baseurl . '" };';
     219
     220// Load patch
     221$content .= getFileContents( 'tiny_mce_ext.js' );
     222
     223// Add core
     224$content .= getFileContents( 'tiny_mce' . $suffix . '.js' );
     225
     226// Patch loading functions
     227$content .= 'tinyMCE_GZ.start();';
     228
     229// Add all languages (WP)
     230include_once( dirname(__FILE__).'/langs/wp-langs.php' );
     231$content .= $strings;
     232
     233// Add themes
     234$content .= getFileContents( 'themes/' . $theme . '/editor_template' . $suffix . '.js' );
     235
     236// Add plugins
     237foreach ( $plugins as $plugin )
     238        $content .= getFileContents( 'plugins/' . $plugin . '/editor_plugin' . $suffix . '.js' );
     239
     240// Add custom files
     241foreach ( $custom_js as $file )
     242        $content .= getFileContents($file);
     243
     244// Add external plugins and init
     245$content .= $ext_plugins . 'tinyMCE.init({' . $mce_options . '});'; // $mce_deprecated2 .
     246
     247// Generate GZIP'd content
     248if ( '.gz' == $cache_ext ) {
     249        header('Content-Encoding: ' . $enc);
     250        $cache_data = gzencode( $content, 9, FORCE_GZIP );
     251} else
     252        $cache_data = $content;
     253
     254// Stream to client
     255echo $cache_data;
     256
     257// Write file
     258if ( '' != $cacheKey ) {
     259        if ( $del_old_cache ) {
     260                $old_key = getFileContents('tiny_mce_compressed_key');
     261                       
     262                if ( '' != $old_key ) { //  && $old_key != $cacheKey
     263                        $old_cache = $cache_path . '/tiny_mce_' . $old_key . $cache_ext;
     264                        @unlink($old_cache);
    22265                }
     266                       
     267                putFileContents( 'tiny_mce_compressed_key', $cacheKey );
    23268        }
    24 
    25         // Set up init variables
    26         $valid_elements = '*[*]';
    27         $valid_elements = apply_filters('mce_valid_elements', $valid_elements);
    28 
    29     $invalid_elements = apply_filters('mce_invalid_elements', '');
    30 
    31         $plugins = array( 'safari', 'inlinepopups', 'autosave', 'spellchecker', 'paste', 'wordpress', 'media', 'fullscreen' );
    32         $plugins = apply_filters('mce_plugins', $plugins);
    33         $plugins = implode($plugins, ',');
    34 
    35         $mce_buttons = apply_filters('mce_buttons', array('bold', 'italic', 'strikethrough', '|', 'bullist', 'numlist', 'outdent', 'indent', '|', 'justifyleft', 'justifycenter', 'justifyright', '|', 'link', 'unlink', 'image', 'wp_more', '|', 'spellchecker', '|', 'wp_help', 'wp_adv' ));
    36         $mce_buttons = implode($mce_buttons, ',');
    37 
    38 
    39         $mce_buttons_2 = apply_filters('mce_buttons_2', array('formatselect', 'underline', 'justifyfull', 'forecolor', '|', 'pastetext', 'pasteword', '|', 'removeformat', 'cleanup', '|', 'media', 'charmap', 'blockquote', '|', 'undo', 'redo', 'fullscreen' ));
    40         $mce_buttons_2 = implode($mce_buttons_2, ',');
    41 
    42         $mce_buttons_3 = apply_filters('mce_buttons_3', array());
    43         $mce_buttons_3 = implode($mce_buttons_3, ',');
    44 
    45         $mce_buttons_4 = apply_filters('mce_buttons_4', array());
    46         $mce_buttons_4 = implode($mce_buttons_4, ',');
    47 
    48         $mce_browsers = apply_filters('mce_browsers', array('msie', 'gecko', 'opera', 'safari'));
    49         $mce_browsers = implode($mce_browsers, ',');
    50 
    51         $mce_css = get_option('siteurl') . '/wp-includes/js/tinymce/wordpress.css';
    52         $mce_css = apply_filters('mce_css', $mce_css);
    53         if ( $_SERVER['HTTPS'] == 'on' )
    54                 $mce_css = str_replace('http://', 'https://', $mce_css);
    55 
    56         $mce_locale = ( '' == get_locale() ) ? 'en' : strtolower( substr(get_locale(), 0, 2) ); // only ISO 639-1
     269               
     270        putFileContents( $cache_file, $cache_data );
     271}
    57272?>
    58 
    59 initArray = {
    60         mode : "none",
    61         onpageload : "wpEditorInit",
    62     width : "100%",
    63         theme : "advanced",
    64         skin : "wp_theme",
    65         theme_advanced_buttons1 : "<?php echo $mce_buttons; ?>",
    66         theme_advanced_buttons2 : "<?php echo $mce_buttons_2; ?>",
    67         theme_advanced_buttons3 : "<?php echo $mce_buttons_3; ?>",
    68         theme_advanced_buttons4 : "<?php echo $mce_buttons_4; ?>",
    69         language : "<?php echo $mce_locale; ?>",
    70         theme_advanced_toolbar_location : "top",
    71         theme_advanced_toolbar_align : "left",
    72         theme_advanced_statusbar_location : "bottom",
    73         theme_advanced_resizing : true,
    74         browsers : "<?php echo $mce_browsers; ?>",
    75         dialog_type : "modal",
    76         theme_advanced_resize_horizontal : false,
    77         convert_urls : false,
    78         relative_urls : false,
    79         remove_script_host : false,
    80         fix_list_elements : true,
    81         fix_table_elements : true,
    82         gecko_spellcheck : true,
    83         entities : "38,amp,60,lt,62,gt",
    84         accessibility_focus : false,
    85         tab_focus : ":next",
    86         content_css : "<?php echo $mce_css; ?>",
    87         <?php if ( $valid_elements ) echo 'valid_elements : "' . $valid_elements . '",' . "\n"; ?>
    88         <?php if ( $invalid_elements ) echo 'invalid_elements : "' . $invalid_elements . '",' . "\n"; ?>
    89         save_callback : "switchEditors.saveCallback",
    90 <?php do_action('mce_options'); ?>
    91         plugins : "<?php echo $plugins; ?>"
    92 };
    93 
    94 <?php
    95         // For people who really REALLY know what they're doing with TinyMCE
    96         // You can modify initArray to add, remove, change elements of the config before tinyMCE.init
    97         do_action('tinymce_before_init');
    98 ?>
    99 
    100 tinyMCE_GZ.init(initArray);
Note: See TracChangeset for help on using the changeset viewer.