Ignore:
Timestamp:
01/07/2010 02:05:49 AM (2 years ago)
Author:
ofer
Message:

Async loading of transposh.js script, fix doc error, load scripts only as_needed with the help of lazyloader plugin. should improve page load speeds for translated pages quite nicely. work still needed on some edge cases + code linting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/transposh.php

    r348 r349  
    7878    /** @var string Saved search variables*/ 
    7979    private $search_s; 
    80     /** @var boolean If transposh.js is on the page*/ 
    81     public $js_included = false; 
    8280 
    8381    /** 
     
    118116        add_action('wp_print_styles', array(&$this,'add_transposh_css')); 
    119117        add_action('wp_print_scripts', array(&$this,'add_transposh_js')); 
     118        add_action('wp_head', array(&$this,'add_transposh_async')); 
    120119        add_action("sm_addurl",array(&$this,'add_sm_transposh_urls')); 
    121120        register_activation_hook(__FILE__, array(&$this,'plugin_activate')); 
     
    468467        wp_enqueue_style("transposh","{$this->transposh_plugin_url}/css/transposh.css",array(),TRANSPOSH_PLUGIN_VER); 
    469468        // we have to load the jquery-ui css just in some cases 
    470         if ($this->edit_mode || $this->options->get_widget_progressbar()) 
    471             wp_enqueue_style("jquery","http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css",array(),'1.0'); 
     469//        if ($this->edit_mode || $this->options->get_widget_progressbar()) 
     470//            wp_enqueue_style("jquery","http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/ui-lightness/jquery-ui.css",array(),'1.0'); 
    472471        logger("Added transposh_css",4); 
    473472    } 
     
    480479        if (!$this->edit_mode && !$this->is_auto_translate_permitted()) { 
    481480            return; 
    482         } 
    483  
    484         $edit_param = ""; 
    485         if($this->edit_mode) { 
    486             $edit_param = "&".EDIT_PARAM."=y"; 
    487         } 
    488  
    489         if($this->edit_mode || $this->options->get_widget_progressbar()) { 
    490             wp_enqueue_script("jqueryui","http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js",array("jquery"),'1.7.2',$this->options->get_enable_footer_scripts()); 
    491481        } 
    492482 
     
    497487            // toying around - for later... 
    498488            //wp_enqueue_script("jquery","http://code.jquery.com/jquery-1.4a2.min.js",array(),'1.4a2'); 
    499             // jQuery pushing below might cause issues 
    500             //wp_enqueue_script("jquery","http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js",array(),'1.3.2', $this->options->get_enable_footer_scripts()); 
    501             wp_enqueue_script("google","http://www.google.com/jsapi",array(),'1',$this->options->get_enable_footer_scripts()); 
    502             // Make sure msn translate is not needlessly loaded when we are only auto translating 
    503             if ($this->options->get_enable_msn_translate() && $this->edit_mode) { 
    504                 wp_enqueue_script("mstranslate","http://api.microsofttranslator.com/V1/Ajax.svc/Embed?appId=".$this->options->get_msn_key(),array(),'1',$this->options->get_enable_footer_scripts()); 
    505             } 
    506             wp_enqueue_script("transposh","{$this->transposh_plugin_url}/js/transposh.js?post_url={$this->post_url}{$edit_param}&lang={$this->target_language}&prefix=".SPAN_PREFIX,array("jquery"),TRANSPOSH_PLUGIN_VER,$this->options->get_enable_footer_scripts()); 
    507             $this->js_included = true; 
    508         } 
    509     } 
    510  
     489        } 
     490    } 
     491 
     492 
     493    function add_transposh_async() { 
     494        if (!$this->edit_mode && !$this->is_auto_translate_permitted()) { 
     495            return; 
     496        } 
     497 
     498        echo "<script type=\"text/javascript\"> 
     499 
     500        var _tr_p=_tr_p || []; 
     501        _tr_p.post_url='{$this->post_url}'; 
     502        _tr_p.plugin_url='{$this->transposh_plugin_url}'; 
     503        _tr_p.edit=".($this->edit_mode? 'true' : 'false')."; 
     504        _tr_p.lang='{$this->target_language}'; 
     505        _tr_p.prefix='".SPAN_PREFIX."'; 
     506        _tr_p.msnkey='{$this->options->get_msn_key()}'; 
     507        _tr_p.progress=".($this->edit_mode || $this->options->get_widget_progressbar() ? 'true' : 'false')."; 
     508 
     509  (function() { 
     510    var tp = document.createElement('script'); tp.type = 'text/javascript'; tp.async = true; 
     511    tp.src = '{$this->transposh_plugin_url}/js/transposh.js?ver=".TRANSPOSH_PLUGIN_VER."'; 
     512    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(tp); 
     513  })(); 
     514 
     515</script>"; 
     516    } 
    511517 
    512518    /** 
     
    651657     * Also - priority is reduced by 0.2 
    652658     * And this requires the following line at the sitemap-core.php, add-url function (line 1509 at version 3.2.2) 
    653      * do_action('sm_addurl',$loc, &$page); 
     659     * do_action('sm_addurl', &$page); 
    654660     * @param GoogleSitemapGeneratorPage $sm_page Object containing the page information 
    655661     */ 
Note: See TracChangeset for help on using the changeset viewer.