Guest User

Untitled

a guest
Sep 24th, 2015
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.51 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: tagDiv Speed Booster
  4. Plugin URI: http://tagdiv.com
  5. Description: Speed booster for Newspaper 6 and Newsmag 1.x themes - It moves the styles and all the scripts of the theme to the bottom when needed. It activates internal theme optimizations for better speed and it adds async js .
  6. Author: tagDiv
  7. Version: 4.1
  8. Author URI: http://tagdiv.com
  9. */
  10.  
  11. /*
  12.  
  13. 4.1 - buddyPress fix
  14. 4.0 - the plugin was rewritten to better support Newspaper 6
  15. - if any unsupported plugin is detected, Speed Booster will disable itself
  16. - the js and css hooks are separated now, it's easier to modify them this way
  17. - all the code is cemented now
  18. 3.2 - better bbpress support on newsmag
  19. 3.1 - fixed issue with flashing white on load on newspaper theme
  20. - better compatibility with themes that do not have wp booster framework
  21. - Newsmag loads fonts in a bundle now
  22. 3.0 - better visual composer support
  23. 2.8 - Newsmag support added :)
  24. 2.7 - fixed ie 9 10 11 window resize bug
  25. 2.6 - fixed rendering bug on the loading of the page
  26. 2.5 - code improvements, newspaper 4 compatibility
  27. - makes most of the javascript files use defer parsing
  28. - better compatibility with revolution slider
  29. 2.4 - updated jquery version
  30. - support for https
  31. 2.3 - fixed warnings when trying to move javascript files that are not registered
  32. */
  33.  
  34.  
  35. define('TD_SPEED_BOOSTER' , 'v4.1');
  36.  
  37.  
  38. class td_speed_booster {
  39.  
  40. private $style_footer_queue = array(); // here we keep all the stylesheets IDs that we want to move to the footer
  41.  
  42. private $is_ie = false; // if the browser is detected as IE, treat it differently
  43.  
  44. // here we keep the theme information
  45. private $td_theme_name = '';
  46. private $td_theme_version = '';
  47. private $td_deploy_mode = '';
  48.  
  49.  
  50. private $allowed_plugins = array(
  51. 'bbpress/bbpress.php',
  52. 'contact-form-7/wp-contact-form-7.php',
  53. 'jetpack/jetpack.php',
  54. 'js_composer/js_composer.php',
  55. 'td-speed-booster/td-speed-booster.php',
  56. 'font-awesome-4-menus/n9m-font-awesome-4.php',
  57. 'wordpress-seo/wp-seo.php',
  58. 'wp-user-avatar/wp-user-avatar.php',
  59. 'td-social-counter/td-social-counter.php',
  60. 'wp-super-cache/wp-cache.php'
  61. );
  62.  
  63.  
  64.  
  65. // this class is instantiated at the bottom of this page
  66. function __construct() {
  67.  
  68. // disable the plugins if there are incompatible plugins
  69. $active_plugins = get_option('active_plugins');
  70.  
  71. // foreach ($active_plugins as $active_plugin) {
  72. // if (!in_array($active_plugin, $this->allowed_plugins)) {
  73. // define('TD_SPEED_BOOSTER_INCOMPATIBLE' , $active_plugin);
  74. // return;
  75. // }
  76. // }
  77.  
  78. add_action('td_wp_booster_loaded', array($this, 'start_booster'));
  79. }
  80.  
  81.  
  82.  
  83. /**
  84. * everything starts from here
  85. */
  86. function start_booster() {
  87. // read the theme version and name if defined
  88. if (defined('TD_THEME_VERSION') and defined('TD_THEME_NAME') and defined('TD_DEPLOY_MODE')) {
  89. $this->td_theme_version = TD_THEME_VERSION;
  90. $this->td_theme_name = TD_THEME_NAME;
  91. $this->td_deploy_mode = TD_DEPLOY_MODE;
  92. } else {
  93. return;
  94. }
  95.  
  96.  
  97. // detect IE 8 9 10 11
  98. if (
  99. !empty($_SERVER['HTTP_USER_AGENT'])
  100. and (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) or (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false))
  101. ) {
  102. $this->is_ie = true;
  103. }
  104.  
  105. // add hooks
  106. 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
  107. 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
  108. add_action('wp_footer', array($this, 'render_footer_styles'), 15);
  109.  
  110.  
  111. // hide the body only on IE and Newspaper 6+
  112. if (
  113. $this->is_ie === false
  114. and $this->td_theme_name == 'Newspaper'
  115. ) {
  116. //add_action('wp_head', array($this, 'hide_body_inline_css'), 15);
  117. }
  118.  
  119.  
  120. /**
  121. * jetpack is 'special' - it has to be moved like this. It has now only one CSS
  122. * the two hooks are needed!
  123. * @since 27.05.2015
  124. */
  125. add_action('wp_print_styles', array($this, 'jetpack_mover'), 10);
  126. add_action('wp_print_footer_scripts', array($this, 'jetpack_mover'), 10);
  127. }
  128.  
  129.  
  130.  
  131. /**
  132. * jetpack is 'special' - it has to be moved like this. It has now only one CSS
  133. * @since 27.05.2015
  134. */
  135. function jetpack_mover() {
  136. $this->move_style_to_footer_queue('jetpack_css');
  137. }
  138.  
  139.  
  140.  
  141. /**
  142. * here we move the css
  143. */
  144. function css_mover() {
  145. if ($this->td_theme_name == 'Newspaper') {
  146. // wp 011 - Newspaper 6
  147.  
  148.  
  149. $this->move_style_to_footer_queue('google_font_open_sans');
  150. $this->move_style_to_footer_queue('google_font_roboto');
  151. $this->move_style_to_footer_queue('contact-form-7');
  152.  
  153. if ($this->is_ie === false) {
  154. // move style.css theme style
  155. }
  156. }
  157.  
  158. //@todo test on newsmag
  159. $this->move_style_to_footer_queue('woocommerce_frontend_styles'); //old woocommerce?
  160. $this->move_style_to_footer_queue('woocommerce-layout');
  161. $this->move_style_to_footer_queue('woocommerce-smallscreen');
  162. $this->move_style_to_footer_queue('woocommerce-general');
  163. $this->move_style_to_footer_queue('bp-legacy-css');
  164.  
  165.  
  166. $this->move_style_to_footer_queue('font-awesome-four');
  167.  
  168.  
  169. //move revolution slider css
  170. $this->move_style_to_footer_queue('rs-plugin-settings');
  171. $this->move_style_to_footer_queue('genericons'); //still rev slider
  172. }
  173.  
  174.  
  175.  
  176. /**
  177. * move the js
  178. */
  179. function js_mover() {
  180. global $wp_scripts;
  181.  
  182.  
  183. //detect revmin - revolution slider and do not move jquery, the plugin outputs raw js in the page!
  184. if( !is_admin() and !isset($wp_scripts->registered['revmin'])){
  185. if (is_ssl()) {
  186. $td_protocol = 'https';
  187. } else {
  188. $td_protocol = 'http';
  189. }
  190.  
  191. wp_deregister_script('jquery');
  192. wp_register_script('jquery', ($td_protocol . '://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'), true, '1.11.1', true);
  193. wp_enqueue_script('jquery');
  194. }
  195.  
  196. /**
  197. * move javascript to footer
  198. */
  199. $this->move_js_to_footer('themepunchtools');
  200. $this->move_js_to_footer('revmin');
  201.  
  202. //print_r($wp_scripts);
  203. // replace comment-reply.min.js with inline version
  204. }
  205.  
  206.  
  207.  
  208. /**
  209. * here we render the footer styles
  210. * the styles are already deregistered when you call @see move_style_to_footer_queue ($style_id)
  211. */
  212. function render_footer_styles() {
  213. //get the theme version for style
  214. $current_theme_version = $this->td_theme_version;
  215.  
  216. //on demo mode, auto generate version hourly + day - so we get a fresh css hourly
  217. if ($this->td_deploy_mode == 'demo') {
  218. $current_theme_version = date('jG');
  219. }
  220.  
  221.  
  222. if (isset($this->style_footer_queue['td-theme'])) {
  223. // 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
  224. $td_theme_value = $this->style_footer_queue['td-theme'];
  225. unset($this->style_footer_queue['td-theme']);
  226. $this->style_footer_queue['td-theme'] = $td_theme_value;
  227. }
  228.  
  229. // output the new styles
  230. foreach ($this->style_footer_queue as $style_id => $style_src) {
  231. echo "<link rel='stylesheet' id='" . $style_id . "-css' href='" . $style_src . "?ver=" . $current_theme_version . "' type='text/css' media='all' />\n";
  232. }
  233. }
  234.  
  235.  
  236.  
  237. /**
  238. * prevent flickering of the image until the style is loaded
  239. */
  240. function hide_body_inline_css() {
  241. echo '<style>body {visibility:hidden;}</style>';
  242. }
  243.  
  244.  
  245.  
  246. /**
  247. * the id of the style can be found in the source of the page, for example:
  248. * <link rel='stylesheet' id='td-theme-css' => the id is 'td-theme' (note that the '-css' part is missing)
  249. * using this method you can move other styles if needed
  250. * Moves a style to the bottom of the page
  251. * @param $style_id string - the id of the style
  252. */
  253. function move_style_to_footer_queue($style_id) {
  254. global $wp_styles;
  255. if (!empty($wp_styles->registered[$style_id]) and !empty($wp_styles->registered[$style_id]->src)) {
  256. $this->style_footer_queue[$style_id] = $wp_styles->registered[$style_id]->src;
  257. wp_deregister_style($style_id);
  258. }
  259. }
  260.  
  261.  
  262.  
  263. /**
  264. * @param $js_id string - the js file id (this is a bit more complex to get) @todo -> add a tutorial about how to get the theme js
  265. */
  266. function move_js_to_footer($js_id) {
  267. global $wp_scripts;
  268. if (isset($wp_scripts->registered[$js_id])) {
  269. wp_enqueue_script($js_id, ($wp_scripts->registered[$js_id]->src), '', $wp_scripts->registered[$js_id]->ver, true);
  270. }
  271.  
  272. }
  273.  
  274.  
  275.  
  276.  
  277. }
  278.  
  279.  
  280.  
  281.  
  282.  
  283. new td_speed_booster();
Advertisement
Add Comment
Please, Sign In to add comment