Skip to content

Commit f5c813b

Browse files
committed
Класс PageSpeed_Resources_To_Footer::
1 parent 056d0f2 commit f5c813b

File tree

3 files changed

+190
-2
lines changed

3 files changed

+190
-2
lines changed

includes/class-pagespeed-optimization.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ public function print_scripts_action() {
737737

738738
// Google Analytics script.
739739
if ( $ga_id ) {
740-
echo "<script>
740+
echo "<script type='text/javascript' async>
741741
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
742742
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
743743
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
@@ -763,7 +763,7 @@ public function print_scripts_action() {
763763
// Yandex Metrika script.
764764
?>
765765
<!-- Yandex.Metrika counter -->
766-
<script type="text/javascript">
766+
<script type="text/javascript" async>
767767
(function (d, w, c) {
768768
(w[c] = w[c] || []).push(function () {
769769
try {
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
3+
/**
4+
* Class PageSpeed_Resources_To_Footer
5+
*
6+
* Move scripts and styles from header to footer.
7+
*/
8+
class PageSpeed_Resources_To_Footer {
9+
/** @var array Scripts to move from header to footer. */
10+
private $scripts = array(
11+
'admin-bar',
12+
'comment-reply',
13+
'layerslider',
14+
'layerslider-transitions',
15+
'contact-form-7',
16+
'jquery-ui-ru',
17+
'jquery-ui-timepicker-ru',
18+
'jquery-ui-datepicker',
19+
'jquery-ui-timepicker',
20+
'jquery-ui-slider',
21+
'jquery-ui-slider-access',
22+
'sb_instagram_scripts',
23+
'jquery',
24+
'jquery-ui-core',
25+
'jquery-ui-widget',
26+
'jcrop',
27+
'jquery.iframe-transport',
28+
'jquery.fileupload',
29+
'jquery.fileupload-process',
30+
'jquery.fileupload-validate',
31+
'uni-avatar-modal',
32+
'wpml-legacy-dropdown-0',
33+
'jquery-masonry',
34+
'jquery-bxslider-min',
35+
'jquery-blackandwhite',
36+
'jquery-fancybox',
37+
'isotope-min',
38+
'jquery-mousewheel',
39+
'jquery-jscrollpane-min',
40+
'jquery-dotdotdot-min',
41+
'jquery-blockui',
42+
'uni-bauhaus-theme-script',
43+
'unitheme-script',
44+
'share-script',
45+
'masked_input',
46+
'fortezza_script',
47+
'social-likes',
48+
'load-more',
49+
'contact-form',
50+
'jquery.cookie',
51+
'fca_pc_client_js',
52+
'fca_pc_tooltipster_js',
53+
'fca_pc_deactivation_js',
54+
'cmb2-scripts',
55+
'jquery-parsley',
56+
'parsley-localization',
57+
);
58+
59+
/** @var array Scripts to block. */
60+
private $block_scripts = array(
61+
'jquery-blackandwhite',
62+
'social-likes',
63+
'jquery-parsley',
64+
'parsley-localization',
65+
'comment-reply',
66+
'jquery-masonry',
67+
'jcrop',
68+
'jquery-ui-slider',
69+
'jquery-ui-slider-access',
70+
'jquery-ui-widget',
71+
'jquery-ui-core',
72+
);
73+
74+
/** @var array Styles to move from header to footer. */
75+
private $styles = array(
76+
'layerslider',
77+
'contact-form-7',
78+
'sb_instagram_styles',
79+
'sb-font-awesome',
80+
'bodhi-svgs-attachment',
81+
'admin-css',
82+
'wpml-legacy-dropdown-0',
83+
'ball-clip-rotate-style',
84+
'bxslider-style',
85+
'fancybox-style',
86+
'jscrollpane-style',
87+
'unitheme-styles',
88+
'unitheme-adaptive',
89+
'unichild-styles',
90+
'font-awesome',
91+
'fancybox',
92+
'js_composer_custom_css',
93+
'jquery-ui-timepicker',
94+
'dashicons',
95+
'cmb2-styles',
96+
);
97+
98+
/** @var array Styles to block. */
99+
private $block_styles = array(
100+
'sb-font-awesome',
101+
'font-awesome',
102+
'dashicons',
103+
'jscrollpane-style',
104+
'bodhi-svgs-attachment',
105+
'wpml-legacy-dropdown-0',
106+
'ball-clip-rotate-style',
107+
);
108+
109+
/**
110+
* PageSpeed_Resources_To_Footer constructor.
111+
*/
112+
public function __construct() {
113+
$this->init();
114+
}
115+
116+
/**
117+
* Init class hooks.
118+
*/
119+
public function init() {
120+
if ( ! is_admin() ) {
121+
add_action( 'wp_enqueue_scripts', array( $this, 'remove_scripts_from_header' ), PHP_INT_MAX );
122+
add_action( 'get_footer', array( $this, 'add_scripts_to_footer' ) );
123+
124+
add_action( 'wp_enqueue_scripts', array( $this, 'remove_styles_from_header' ), PHP_INT_MAX );
125+
add_action( 'get_footer', array( $this, 'add_styles_to_footer' ) );
126+
}
127+
}
128+
129+
/**
130+
* Remove scripts from header.
131+
*/
132+
public function remove_scripts_from_header() {
133+
$scripts = array_unique( array_merge( $this->scripts, $this->block_scripts ) );
134+
135+
foreach ( $scripts as $script ) {
136+
if ( wp_script_is( $script, 'enqueued' ) ) {
137+
wp_dequeue_script( $script );
138+
}
139+
}
140+
}
141+
142+
/**
143+
* Add scripts to footer.
144+
*/
145+
public function add_scripts_to_footer() {
146+
$scripts = array_diff( $this->scripts, $this->block_scripts );
147+
148+
foreach ( $scripts as $script ) {
149+
if ( wp_script_is( $script, 'registered' ) ) {
150+
wp_enqueue_script( $script );
151+
}
152+
}
153+
}
154+
155+
/**
156+
* Remove styles from header.
157+
*/
158+
public function remove_styles_from_header() {
159+
/** @var $wp_styles WP_Styles */
160+
global $wp_styles;
161+
162+
$styles = array_unique( array_merge( $this->styles, $this->block_styles ) );
163+
164+
foreach ( $styles as $style ) {
165+
if ( in_array( $style, $wp_styles->queue, true ) ) {
166+
wp_dequeue_style( $style );
167+
}
168+
}
169+
}
170+
171+
/**
172+
* Add styles to footer.
173+
*/
174+
public function add_styles_to_footer() {
175+
/** @var $wp_styles WP_Styles */
176+
global $wp_styles;
177+
178+
$styles = array_diff( $this->styles, $this->block_styles );
179+
180+
foreach ( $styles as $style ) {
181+
if ( isset( $wp_styles->registered[ $style ] ) ) {
182+
wp_enqueue_style( $style );
183+
}
184+
}
185+
}
186+
}

pagespeed-optimization.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ function init_pagespeed_optimization_class() {
3636
// Require plugin classes.
3737
require_once dirname( __FILE__ ) . '/includes/class-pagespeed-optimization.php';
3838
require_once dirname( __FILE__ ) . '/includes/class-pagespeed-filesystem.php';
39+
require_once dirname( __FILE__ ) . '/includes/class-pagespeed-resources-to-footer.php';
3940

4041
$plugin = new PageSpeed_Optimization();
42+
new PageSpeed_Resources_To_Footer();
4143
}
4244
}
4345

0 commit comments

Comments
 (0)