Guest User

td-booster

a guest
Nov 18th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.16 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 7 and Newsmag 3 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.9.1
  8. Author URI: http://tagdiv.com
  9. */
  10.  
  11. /*
  12. 4.9 - tagdiv composer support
  13. - theme's bbpress style moved at the bottom
  14. 4.8 - child theme support fix
  15. 4.7 - added Newspaper image opacity issue fix
  16. 4.6 - changed the google fonts style
  17. - added revolution slider to allowed plugins list
  18. 4.5 - enqueue order changed to match the default plugin order and Newsmag 3 new grid
  19. 4.4 - woocommerce prettyPhoto css moved at the bottom
  20. 4.3 - enqueue order fix for bbpress and visual composer
  21. 4.2 - woocommerce support and style.css moved to bottom on Newspaper
  22. 4.1 - buddyPress fix
  23. 4.0 - the plugin was rewritten to better support Newspaper 6
  24. - if any unsupported plugin is detected, Speed Booster will disable itself
  25. - the js and css hooks are separated now, it's easier to modify them this way
  26. - all the code is cemented now
  27. 3.2 - better bbpress support on newsmag
  28. 3.1 - fixed issue with flashing white on load on newspaper theme
  29. - better compatibility with themes that do not have wp booster framework
  30. - Newsmag loads fonts in a bundle now
  31. 3.0 - better visual composer support
  32. 2.8 - Newsmag support added :)
  33. 2.7 - fixed ie 9 10 11 window resize bug
  34. 2.6 - fixed rendering bug on the loading of the page
  35. 2.5 - code improvements, newspaper 4 compatibility
  36. - makes most of the javascript files use defer parsing
  37. - better compatibility with revolution slider
  38. 2.4 - updated jquery version
  39. - support for https
  40. 2.3 - fixed warnings when trying to move javascript files that are not registered
  41. */
  42.  
  43.  
  44. define('TD_SPEED_BOOSTER' , 'v4.9.1');
  45.  
  46.  
  47. class td_speed_booster {
  48.  
  49. private $style_footer_queue = array(); // here we keep all the stylesheets IDs that we want to move to the footer
  50.  
  51. private $is_ie = false; // if the browser is detected as IE, treat it differently
  52.  
  53. // here we keep the theme information
  54. private $td_theme_name = '';
  55. private $td_theme_version = '';
  56. private $td_deploy_mode = '';
  57.  
  58.  
  59. private $allowed_plugins = array(
  60. 'bbpress/bbpress.php',
  61. 'contact-form-7/wp-contact-form-7.php',
  62. 'jetpack/jetpack.php',
  63. 'js_composer/js_composer.php',
  64. 'td-speed-booster/td-speed-booster.php',
  65. 'font-awesome-4-menus/n9m-font-awesome-4.php',
  66. 'wordpress-seo/wp-seo.php',
  67. 'wp-user-avatar/wp-user-avatar.php',
  68. 'td-social-counter/td-social-counter.php',
  69. 'wp-super-cache/wp-cache.php',
  70. 'woocommerce/woocommerce.php',
  71. 'td-mobile-plugin/td-mobile-plugin.php',
  72. 'revslider/revslider.php',
  73. 'td-composer/td-composer.php',
  74. 'td-multi-purpose/td-multi-purpose.php',
  75. 'td-newsletter/td-newsletter.php',
  76. 'td-amp/td-amp.php'
  77. );
  78.  
  79.  
  80.  
  81. // this class is instantiated at the bottom of this page
  82. function __construct() {
  83.  
  84. // disable the plugins if there are incompatible plugins
  85. $active_plugins = get_option('active_plugins');
  86. //print_r($active_plugins);
  87. //die;
  88. /* foreach ($active_plugins as $active_plugin) {
  89. if (!in_array($active_plugin, $this->allowed_plugins)) {
  90. define('TD_SPEED_BOOSTER_INCOMPATIBLE' , $active_plugin);
  91. return;
  92. }
  93. }*/
  94.  
  95. add_action('td_wp_booster_loaded', array($this, 'start_booster'));
  96. }
  97.  
  98.  
  99.  
  100. /**
  101. * everything starts from here
  102. */
  103. function start_booster() {
  104. // read the theme version and name if defined
  105. if (defined('TD_THEME_VERSION') and defined('TD_THEME_NAME') and defined('TD_DEPLOY_MODE')) {
  106. $this->td_theme_version = TD_THEME_VERSION;
  107. $this->td_theme_name = TD_THEME_NAME;
  108. $this->td_deploy_mode = TD_DEPLOY_MODE;
  109. } else {
  110. return;
  111. }
  112.  
  113.  
  114. // detect IE 8 9 10 11
  115. if (
  116. !empty($_SERVER['HTTP_USER_AGENT'])
  117. and (preg_match('~MSIE|Internet Explorer~i', $_SERVER['HTTP_USER_AGENT']) or (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident/7.0; rv:11.0') !== false))
  118. ) {
  119. $this->is_ie = true;
  120. }
  121.  
  122. // add hooks
  123. 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
  124. 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
  125. add_action('wp_footer', array($this, 'render_footer_styles'), 15);
  126.  
  127.  
  128. // hide the body only on IE and Newspaper 6+
  129. if (
  130. $this->is_ie === false
  131. ) {
  132. add_action('wp_head', array($this, 'hide_body_inline_css'), 15);
  133. }
  134.  
  135.  
  136. /**
  137. * jetpack is 'special' - it has to be moved like this. It has now only one CSS
  138. * the two hooks are needed!
  139. * @since 27.05.2015
  140. */
  141. add_action('wp_print_styles', array($this, 'jetpack_mover'), 10);
  142. add_action('wp_print_footer_scripts', array($this, 'jetpack_mover'), 10);
  143.  
  144. /**
  145. * fix opacity issue
  146. */
  147. add_action('wp_head', array($this, 'td_demo_flicker_fix'), 1);
  148. }
  149.  
  150.  
  151. /**
  152. * fix opacity issue
  153. * speedbooster moves css at the end of the page, this causes image opacity to switch from 1 to 0 and back to 1 when all images are loaded
  154. * @since 08.Mar.2015
  155. */
  156. function td_demo_flicker_fix() {
  157. if ($this->td_theme_name == 'Newspaper' && $this->td_deploy_mode == 'demo') {
  158. echo '<style type="text/css">
  159. body.td-animation-stack-type0 .td-animation-stack .entry-thumb,
  160. body.td-animation-stack-type0 .post img {
  161. opacity: 0;
  162. }
  163. </style>';
  164. }
  165. }
  166.  
  167.  
  168. /**
  169. * jetpack is 'special' - it has to be moved like this. It has now only one CSS
  170. * @since 27.05.2015
  171. */
  172. function jetpack_mover() {
  173. $this->move_style_to_footer_queue('jetpack_css');
  174. }
  175.  
  176.  
  177.  
  178. /**
  179. * here we move the css
  180. */
  181. function css_mover() {
  182. //if ($this->td_theme_name == 'Newspaper') { }
  183. // wp 011 - Newspaper 6
  184.  
  185. $this->move_style_to_footer_queue('bbp-default-bbpress'); //bbpress old
  186. $this->move_style_to_footer_queue('bbp-default'); // bbpress
  187. $this->move_style_to_footer_queue('contact-form-7');
  188.  
  189. if ($this->is_ie === false) {
  190. // move style.css theme style
  191. $this->move_style_to_footer_queue('js_composer_front');
  192. $this->move_style_to_footer_queue('td-theme');
  193. $this->move_style_to_footer_queue('td-theme-child');
  194. $this->move_style_to_footer_queue('td-theme-woo');
  195. $this->move_style_to_footer_queue('td-theme-demo-style');
  196. $this->move_style_to_footer_queue('td_live_css_frontend');
  197. $this->move_style_to_footer_queue('td-theme-bbpress');
  198. $this->move_style_to_footer_queue('toc-screen');
  199. // $this->move_style_to_footer_queue('td-plugin-multi-purpose');
  200. $this->move_style_to_footer_queue('td-plugin-newsletter');
  201. }
  202.  
  203. $this->move_style_to_footer_queue('font-awesome-four');
  204.  
  205. //@todo test on newsmag
  206. $this->move_style_to_footer_queue('woocommerce_prettyPhoto_css'); //on product page
  207. $this->move_style_to_footer_queue('woocommerce_frontend_styles'); //old woocommerce?
  208. $this->move_style_to_footer_queue('woocommerce-layout');
  209. $this->move_style_to_footer_queue('woocommerce-smallscreen');
  210. $this->move_style_to_footer_queue('woocommerce-general');
  211. //$this->move_style_to_footer_queue('bp-legacy-css');
  212.  
  213. //google fonts
  214. $this->move_style_to_footer_queue('google-fonts-style');
  215.  
  216. //move revolution slider css
  217. $this->move_style_to_footer_queue('rs-plugin-settings');
  218. $this->move_style_to_footer_queue('genericons'); //still rev slider
  219.  
  220.  
  221. //$this->move_style_to_footer_queue('js_composer_front');
  222.  
  223. }
  224.  
  225.  
  226.  
  227. /**
  228. * move the js
  229. */
  230. function js_mover() {
  231. global $wp_scripts;
  232.  
  233.  
  234. //detect revmin - revolution slider and do not move jquery, the plugin outputs raw js in the page!
  235. if( !is_admin() and !isset($wp_scripts->registered['revmin'])){
  236. if (is_ssl()) {
  237. $td_protocol = 'https';
  238. } else {
  239. $td_protocol = 'http';
  240. }
  241.  
  242. wp_deregister_script('jquery');
  243. wp_register_script('jquery', ($td_protocol . '://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js'), true, '1.11.1', true);
  244. wp_enqueue_script('jquery');
  245. }
  246.  
  247. /**
  248. * move javascript to footer
  249. */
  250. $this->move_js_to_footer('themepunchtools');
  251. $this->move_js_to_footer('revmin');
  252. $this->move_js_to_footer('vc_woocommerce-add-to-cart-js');
  253. //$this->move_js_to_footer('comment-reply');
  254.  
  255. //print_r($wp_scripts);
  256. // replace comment-reply.min.js with inline version
  257. }
  258.  
  259.  
  260.  
  261. /**
  262. * here we render the footer styles
  263. * the styles are already deregistered when you call @see move_style_to_footer_queue ($style_id)
  264. */
  265. function render_footer_styles() {
  266. //get the theme version for style
  267. $current_theme_version = $this->td_theme_version;
  268.  
  269. //on demo mode, auto generate version hourly + day - so we get a fresh css hourly
  270. if ($this->td_deploy_mode == 'demo') {
  271. $current_theme_version = date('jG');
  272. }
  273.  
  274. //print_r($this->style_footer_queue);
  275.  
  276. if (isset($this->style_footer_queue['js_composer_front'])) {
  277. $td_theme_value = $this->style_footer_queue['js_composer_front'];
  278. unset($this->style_footer_queue['js_composer_front']);
  279. $this->style_footer_queue['js_composer_front'] = $td_theme_value;
  280. }
  281.  
  282. if (isset($this->style_footer_queue['td-theme'])) {
  283. // 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
  284. $td_theme_value = $this->style_footer_queue['td-theme'];
  285. unset($this->style_footer_queue['td-theme']);
  286. $this->style_footer_queue['td-theme'] = $td_theme_value;
  287. }
  288.  
  289. if (isset($this->style_footer_queue['td-theme-child'])) {
  290. $td_theme_value = $this->style_footer_queue['td-theme-child'];
  291. unset($this->style_footer_queue['td-theme-child']);
  292. $this->style_footer_queue['td-theme-child'] = $td_theme_value;
  293. }
  294.  
  295. if (isset($this->style_footer_queue['td-theme-woo'])) {
  296. // 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
  297. $td_theme_value = $this->style_footer_queue['td-theme-woo'];
  298. unset($this->style_footer_queue['td-theme-woo']);
  299. $this->style_footer_queue['td-theme-woo'] = $td_theme_value;
  300. }
  301.  
  302. if (isset($this->style_footer_queue['td-theme-demo-style'])) {
  303. // 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
  304. $td_theme_value = $this->style_footer_queue['td-theme-demo-style'];
  305. unset($this->style_footer_queue['td-theme-demo-style']);
  306. $this->style_footer_queue['td-theme-demo-style'] = $td_theme_value;
  307. }
  308.  
  309. // output the new styles
  310. foreach ($this->style_footer_queue as $style_id => $wp_dependency_object) {
  311.  
  312. $script_version = ! empty($wp_dependency_object->ver) ? $wp_dependency_object->ver : $current_theme_version;
  313.  
  314. if (strrpos($wp_dependency_object->src, '?') === false) {
  315. echo "<link rel='stylesheet' id='" . $style_id . "-css' href='" . $wp_dependency_object->src . "?ver=" . $script_version . "' type='text/css' media='" . $wp_dependency_object->args . "' />\n";
  316. } else {
  317. echo "<link rel='stylesheet' id='" . $style_id . "-css' href='" . $wp_dependency_object->src . "&ver=" . $script_version . "' type='text/css' media='" . $wp_dependency_object->args . "' />\n";
  318. }
  319.  
  320.  
  321. }
  322. }
  323.  
  324.  
  325.  
  326. /**
  327. * prevent flickering of the image until the style is loaded
  328. */
  329. function hide_body_inline_css() {
  330. echo '<style>body {visibility:hidden;}</style>';
  331. }
  332.  
  333.  
  334.  
  335. /**
  336. * the id of the style can be found in the source of the page, for example:
  337. * <link rel='stylesheet' id='td-theme-css' => the id is 'td-theme' (note that the '-css' part is missing)
  338. * using this method you can move other styles if needed
  339. * Moves a style to the bottom of the page
  340. * @param $style_id string - the id of the style
  341. */
  342. function move_style_to_footer_queue($style_id) {
  343. global $wp_styles;
  344. if (!empty($wp_styles->registered[$style_id]) and !empty($wp_styles->registered[$style_id]->src)) {
  345. //print_r($wp_styles->registered[$style_id]);
  346. $this->style_footer_queue[$style_id] = $wp_styles->registered[$style_id];
  347. wp_deregister_style($style_id);
  348. }
  349. }
  350.  
  351.  
  352.  
  353. /**
  354. * @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
  355. */
  356. function move_js_to_footer($js_id) {
  357. global $wp_scripts;
  358. if (isset($wp_scripts->registered[$js_id])) {
  359. wp_enqueue_script($js_id, ($wp_scripts->registered[$js_id]->src), ($wp_scripts->registered[$js_id]->deps), $wp_scripts->registered[$js_id]->ver, true);
  360. }
  361.  
  362. }
  363.  
  364.  
  365.  
  366.  
  367. }
  368.  
  369. new td_speed_booster();
Advertisement
Add Comment
Please, Sign In to add comment