Guest User

Untitled

a guest
Jan 12th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. <?php
  2. define('TD_SPEED_BOOSTER' , 'v4.2');
  3. class td_speed_booster {
  4. private $style_footer_queue = array(); // here we keep all the stylesheets IDs that we want to move to the footer
  5. private $is_ie = false; // if the browser is detected as IE, treat it differently
  6. // here we keep the theme information
  7. private $td_theme_name = '';
  8. private $td_theme_version = '';
  9. private $td_deploy_mode = '';
  10. private $allowed_plugins = array(
  11. 'bbpress/bbpress.php',
  12. 'contact-form-7/wp-contact-form-7.php',
  13. 'jetpack/jetpack.php',
  14. 'js_composer/js_composer.php',
  15. 'td-speed-booster/td-speed-booster.php',
  16. 'font-awesome-4-menus/n9m-font-awesome-4.php',
  17. 'wordpress-seo/wp-seo.php',
  18. 'wp-user-avatar/wp-user-avatar.php',
  19. 'td-social-counter/td-social-counter.php',
  20. 'wp-super-cache/wp-cache.php',
  21. 'woocommerce/woocommerce.php'
  22.  
  23. );
  24. function __construct() {
  25.  
  26. add_action('td_wp_booster_loaded', array($this, 'start_booster'));
  27. }
  28. function start_booster() {
  29. // read the theme version and name if defined
  30. if (defined('TD_THEME_VERSION') and defined('TD_THEME_NAME') and defined('TD_DEPLOY_MODE')) {
  31. $this->td_theme_version = TD_THEME_VERSION;
  32. $this->td_theme_name = TD_THEME_NAME;
  33. $this->td_deploy_mode = TD_DEPLOY_MODE;
  34. } else {
  35. return;
  36. }
  37. // detect IE 8 9 10 11
  38. if (
  39. !empty($_SERVER['HTTP_USER_AGENT'])
  40. and (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) or (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false))
  41. ) {
  42. $this->is_ie = true;
  43. }
  44. // add hooks
  45. add_action('wp_enqueue_scripts', array($this, 'css_mover'), 1002); // 1002 priority - because visual composer has 1000 and we use 1001 in the wp010 theme
  46. add_action('wp_enqueue_scripts', array($this, 'js_mover'), 1002); // 1002 priority - because visual composer has 1000 and we use 1001 in the wp010 theme
  47. add_action('wp_footer', array($this, 'render_footer_styles'), 15);
  48. // hide the body only on IE and Newspaper 6+
  49. if (
  50. $this->is_ie === false
  51. and $this->td_theme_name == 'Newspaper'
  52. ) {
  53. add_action('wp_head', array($this, 'hide_body_inline_css'), 15);
  54. }
  55. add_action('wp_print_styles', array($this, 'jetpack_mover'), 10);
  56. add_action('wp_print_footer_scripts', array($this, 'jetpack_mover'), 10);
  57. }
  58. function jetpack_mover() {
  59. $this->move_style_to_footer_queue('jetpack_css');
  60. }
  61. function css_mover() {
  62. if ($this->td_theme_name == 'Newspaper') {
  63. // wp 011 - Newspaper 6
  64.  
  65. //$this->move_style_to_footer_queue('bbp-default-bbpress'); //bpress old
  66. //$this->move_style_to_footer_queue('bbp-default'); //bpress
  67. $this->move_style_to_footer_queue('google_font_open_sans');
  68. $this->move_style_to_footer_queue('google_font_roboto');
  69. $this->move_style_to_footer_queue('contact-form-7');
  70.  
  71. if ($this->is_ie === false) {
  72. // move style.css theme style
  73. //$this->move_style_to_footer_queue('js_composer_front');
  74. $this->move_style_to_footer_queue('td-theme'); //this is the main style of the theme. It's only moved on Newspaper 6!
  75. $this->move_style_to_footer_queue('td-theme-woo'); //this is the main style of the theme. It's only moved on Newspaper 6!
  76. }
  77. }
  78. $this->move_style_to_footer_queue('woocommerce_frontend_styles'); //old woocommerce?
  79. $this->move_style_to_footer_queue('woocommerce-layout');
  80. $this->move_style_to_footer_queue('woocommerce-smallscreen');
  81. $this->move_style_to_footer_queue('woocommerce-general');
  82. $this->move_style_to_footer_queue('bp-legacy-css');
  83. $this->move_style_to_footer_queue('font-awesome-four');
  84. $this->move_style_to_footer_queue('wpdreams-asp-basic');
  85. $this->move_style_to_footer_queue('wpdreams-ajaxsearchpro-instances');
  86. $this->move_style_to_footer_queue('archives-cal-classiclight');
  87. $this->move_style_to_footer_queue('js_composer_front');
  88. $this->move_style_to_footer_queue('td-theme');
  89. $this->move_style_to_footer_queue('td-theme-child');
  90.  
  91. //move revolution slider css
  92. $this->move_style_to_footer_queue('rs-plugin-settings');
  93. $this->move_style_to_footer_queue('genericons'); //still rev slider
  94.  
  95.  
  96. //$this->move_style_to_footer_queue('js_composer_front');
  97.  
  98. }
  99. function js_mover() {
  100. global $wp_scripts;
  101. //detect revmin - revolution slider and do not move jquery, the plugin outputs raw js in the page!
  102. if( !is_admin() and !isset($wp_scripts->registered['revmin'])){
  103. if (is_ssl()) {
  104. $td_protocol = 'https';
  105. } else {
  106. $td_protocol = 'http';
  107. }
  108.  
  109. wp_deregister_script('jquery');
  110. wp_register_script('jquery', ($td_protocol . '://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'), true, '1.11.1', true);
  111. wp_enqueue_script('jquery');
  112. }
  113.  
  114. $this->move_js_to_footer('themepunchtools');
  115. $this->move_js_to_footer('revmin');
  116. $this->move_js_to_footer('wpdreams-photostack');
  117. $this->move_js_to_footer('jquery');
  118. $this->move_js_to_footer('comment-reply');
  119. $this->move_js_to_footer('td-site-min');
  120.  
  121. //print_r($wp_scripts);
  122. // replace comment-reply.min.js with inline version
  123. }
  124. function render_footer_styles() {
  125. //get the theme version for style
  126. $current_theme_version = $this->td_theme_version;
  127.  
  128. //on demo mode, auto generate version hourly + day - so we get a fresh css hourly
  129. if ($this->td_deploy_mode == 'demo') {
  130. $current_theme_version = date('jG');
  131. }
  132.  
  133. //print_r($this->style_footer_queue);
  134.  
  135. if (isset($this->style_footer_queue['td-theme'])) {
  136. // whatever the order, make damn sure that our td-theme style is LAST - we remove it from the queue and add it again at the end
  137. $td_theme_value = $this->style_footer_queue['td-theme'];
  138. unset($this->style_footer_queue['td-theme']);
  139. $this->style_footer_queue['td-theme'] = $td_theme_value;
  140. }
  141.  
  142.  
  143. if (isset($this->style_footer_queue['td-theme-woo'])) {
  144. // whatever the order, make damn sure that our td-theme style is LAST - we remove it from the queue and add it again at the end
  145. $td_theme_value = $this->style_footer_queue['td-theme-woo'];
  146. unset($this->style_footer_queue['td-theme-woo']);
  147. $this->style_footer_queue['td-theme-woo'] = $td_theme_value;
  148. }
  149.  
  150. // output the new styles
  151. foreach ($this->style_footer_queue as $style_id => $wp_dependency_object) {
  152. if (strrpos($wp_dependency_object->src, '?') === false) {
  153. echo "<link rel='stylesheet' id='" . $style_id . "-css' href='" . $wp_dependency_object->src . "?ver=" . $current_theme_version . "' type='text/css' media='" . $wp_dependency_object->args . "' />\n";
  154. } else {
  155. echo "<link rel='stylesheet' id='" . $style_id . "-css' href='" . $wp_dependency_object->src . "&ver=" . $current_theme_version . "' type='text/css' media='" . $wp_dependency_object->args . "' />\n";
  156. }
  157.  
  158.  
  159. }
  160. }
  161. function hide_body_inline_css() {
  162. echo '<style>body {visibility:hidden;}</style>';
  163. }
  164. function move_style_to_footer_queue($style_id) {
  165. global $wp_styles;
  166. if (!empty($wp_styles->registered[$style_id]) and !empty($wp_styles->registered[$style_id]->src)) {
  167. //print_r($wp_styles->registered[$style_id]);
  168. $this->style_footer_queue[$style_id] = $wp_styles->registered[$style_id];
  169. wp_deregister_style($style_id);
  170. }
  171. }
  172. function move_js_to_footer($js_id) {
  173. global $wp_scripts;
  174. if (isset($wp_scripts->registered[$js_id])) {
  175. wp_enqueue_script($js_id, ($wp_scripts->registered[$js_id]->src), '', $wp_scripts->registered[$js_id]->ver, true);
  176. }
  177. }
  178. }
  179. new td_speed_booster();
Advertisement
Add Comment
Please, Sign In to add comment