Skip to content

Commit 1a70841

Browse files
committed
Add optimization of Monster Insights.
1 parent 182d52d commit 1a70841

File tree

4 files changed

+89
-6
lines changed

4 files changed

+89
-6
lines changed

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,19 @@
3232
"dealerdirect/phpcodesniffer-composer-installer": true
3333
},
3434
"platform": {
35-
"php": "7.0"
35+
"php": "7.2"
3636
}
3737
},
3838
"require": {
3939
"ext-json": "*",
40-
"tedivm/jshrink": "^v1.6.8"
40+
"tedivm/jshrink": "^v1.7.0"
4141
},
4242
"require-dev": {
4343
"roave/security-advisories": "dev-latest",
44-
"squizlabs/php_codesniffer": "^3.7.2",
44+
"squizlabs/php_codesniffer": "^3.11.1",
4545
"phpcompatibility/php-compatibility": "^9.3.5",
46-
"phpcompatibility/phpcompatibility-wp": "^2.1.3",
47-
"wp-coding-standards/wpcs": "^3.0.0"
46+
"phpcompatibility/phpcompatibility-wp": "^2.1.5",
47+
"wp-coding-standards/wpcs": "^3.1.0"
4848
},
4949
"autoload": {
5050
"psr-4": {

src/php/DelayedScript.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public static function launch_stored_scripts(): void {
182182
* @noinspection JSUnusedLocalSymbols
183183
*/
184184
private static function get_js( array $args, bool $async = true ): string {
185+
unset( $args['async'] );
185186
$async_string = $async ? 'true' : 'false';
186187

187188
$js = <<<JS
@@ -196,7 +197,7 @@ private static function get_js( array $args, bool $async = true ): string {
196197
foreach ( $args as $key => $arg ) {
197198
if ( 'data' === $key ) {
198199
foreach ( $arg as $data_key => $data_arg ) {
199-
$js .= "\t\t\ts.dataset.$data_key = '$data_arg';\n";
200+
$js .= "\t\t\ts.setAttribute( 'data-' + '$data_key', '$data_arg' );\n";
200201
}
201202
continue;
202203
}

src/php/Main.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function init(): void {
107107
new FBShareLikeButton();
108108
new StatCounter();
109109
new Clutch();
110+
new MonsterInsights();
110111
}
111112

112113
/**

src/php/MonsterInsights.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* MonsterInsights class file.
4+
*
5+
* @package kagg_pagespeed_optimization
6+
*/
7+
8+
namespace KAGG\PageSpeed\Optimization;
9+
10+
/**
11+
* Class MonsterInsights.
12+
*/
13+
class MonsterInsights {
14+
15+
/**
16+
* Constructor.
17+
*/
18+
public function __construct() {
19+
add_action( 'plugins_loaded', [ $this, 'init_hooks' ] );
20+
}
21+
22+
/**
23+
* Init hooks.
24+
*/
25+
public function init_hooks(): void {
26+
if ( ! defined( 'MONSTERINSIGHTS_VERSION' ) ) {
27+
return;
28+
}
29+
30+
add_action( 'monsterinsights_tracking_before_gtag', [ $this, 'before_gtag' ] );
31+
add_action( 'monsterinsights_tracking_after_gtag', [ $this, 'after_gtag' ] );
32+
}
33+
34+
/**
35+
* Before gtag.
36+
*
37+
* @return void
38+
*/
39+
public function before_gtag(): void {
40+
ob_start();
41+
}
42+
43+
/**
44+
* After gtag.
45+
*
46+
* @return void
47+
*/
48+
public function after_gtag(): void {
49+
$g_tag = ob_get_clean();
50+
51+
if ( preg_match( '#<script.+googletagmanager.+?</script>#', $g_tag, $matches ) ) {
52+
$script_tag = $matches[0];
53+
54+
preg_match( '#<script (.+)></script>#', $script_tag, $matches );
55+
56+
$script_args = $matches[1] ?? '';
57+
58+
preg_match_all( '#([\w|-]+)=["\']([^"\']+)["\']|([\w|-]+)#', $script_args, $matches );
59+
60+
$keys = array_filter( $matches[1] ) + array_filter( $matches[3] );
61+
$values = $matches[2];
62+
$args = array_combine( $keys, $values );
63+
$data = [];
64+
65+
foreach ( $args as $key => $value ) {
66+
if ( 0 === strpos( $key, 'data-' ) ) {
67+
$data[ str_replace( 'data-', '', $key ) ] = $value;
68+
unset( $args[ $key ] );
69+
}
70+
}
71+
72+
$args['data'] = $data;
73+
74+
$g_tag = str_replace( $script_tag, '', $g_tag );
75+
76+
DelayedScript::launch( $args );
77+
}
78+
79+
DelayedScript::launch_html( $g_tag );
80+
}
81+
}

0 commit comments

Comments
 (0)