| @@ -1,8 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use Kibo\PhastPlugins\PhastPress\Compat; | |
| 4 | - | |
| 5 | 3 | function phastpress_deploy() { |
| 6 | 4 | // We have to deploy on plugins_loaded action so we get wp_get_current_user() to be defined. |
| 7 | 5 | if (is_admin()) { |
| 8 | 6 | return; |
| @@ -48,17 +46,49 @@ | ||
| 48 | 46 | |
| 49 | 47 | return preg_replace('~<script\b~i', '$0 data-phast-no-defer', $tag); |
| 50 | 48 | }, 10, 2); |
| 51 | 49 | |
| 52 | - foreach ([ | |
| 53 | - Compat\MonsterInsights::class, | |
| 54 | - Compat\Slimstat::class, | |
| 55 | - Compat\GoogleSiteKit::class, | |
| 56 | - Compat\GAGoogleAnalytics::class, | |
| 57 | - Compat\AdThrive::class, | |
| 58 | - ] as $class) { | |
| 59 | - (new $class())->setup(); | |
| 60 | - } | |
| 50 | + // Don't delay Monsterinsights analytics script. | |
| 51 | + add_filter('monsterinsights_tracking_analytics_script_attributes', function ($attrs) { | |
| 52 | + if (is_array($attrs)) { | |
| 53 | + $attrs['data-phast-no-defer'] = ''; | |
| 54 | + } | |
| 55 | + return $attrs; | |
| 56 | + }); | |
| 57 | + | |
| 58 | + // Don't delay Slimstat Analytics. | |
| 59 | + add_filter('wp_print_scripts', function () { | |
| 60 | + if (!wp_script_is('wp_slimstat')) { | |
| 61 | + return; | |
| 62 | + } | |
| 63 | + | |
| 64 | + // Don't defer the tracker script. | |
| 65 | + add_filter('script_loader_tag', function ($tag, $handle, $src) { | |
| 66 | + if ($handle !== 'wp_slimstat') { | |
| 67 | + return $tag; | |
| 68 | + } | |
| 69 | + return preg_replace('~<script\b~', '$0 data-phast-no-defer async', $tag); | |
| 70 | + }, 10, 3); | |
| 71 | + | |
| 72 | + // Don't defer the inline parameters script. | |
| 73 | + ob_start(function ($chunk) { | |
| 74 | + return preg_replace('~(<script\b)([^>]*>\s*(/\*.*?\*/)?\s*var\s+SlimStatParams\s*=)~', '$1 data-phast-no-defer$2', $chunk); | |
| 75 | + }, 8192); | |
| 76 | + }); | |
| 77 | + | |
| 78 | + // Don't delay Google Site Kit Analytics. | |
| 79 | + add_filter('wp_print_scripts', function () { | |
| 80 | + wp_scripts()->add_data('google_gtagjs', 'phast_no_defer', true); | |
| 81 | + }); | |
| 82 | + | |
| 83 | + // Don't delay GA Google Analytics script. | |
| 84 | + add_filter('ga_google_analytics_script_atts_ext', function ($atts) { | |
| 85 | + return $atts . ' data-phast-no-defer'; | |
| 86 | + }); | |
| 87 | + | |
| 88 | + add_filter('ga_google_analytics_script_atts', function ($atts) { | |
| 89 | + return $atts . ' data-phast-no-defer'; | |
| 90 | + }); | |
| 61 | 91 | |
| 62 | 92 | $sdk = phastpress_get_plugin_sdk(); |
| 63 | 93 | $handler = $sdk->getPhastAPI()->deployOutputBufferForDocument(); |
| 64 | 94 | |