| @@ -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; |
| @@ -35,14 +33,8 @@ | ||
| 35 | 33 | || ( |
| 36 | 34 | defined('WPACU_LOAD_ASSETS_REQ_KEY') |
| 37 | 35 | && !empty($_REQUEST[WPACU_LOAD_ASSETS_REQ_KEY]) |
| 38 | 36 | ) |
| 39 | - // WPBakery | |
| 40 | - || ( | |
| 41 | - defined('WPB_VC_VERSION') | |
| 42 | - && isset($_GET['vc_editable']) | |
| 43 | - && $_GET['vc_editable'] === 'true' | |
| 44 | - ) | |
| 45 | 37 | ) { |
| 46 | 38 | return; |
| 47 | 39 | } |
| 48 | 40 | |
| @@ -54,34 +46,55 @@ | ||
| 54 | 46 | |
| 55 | 47 | return preg_replace('~<script\b~i', '$0 data-phast-no-defer', $tag); |
| 56 | 48 | }, 10, 2); |
| 57 | 49 | |
| 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. | |
| 58 | 59 | add_filter('wp_print_scripts', function () { |
| 59 | - foreach (wp_scripts()->registered as $handle => $item) { | |
| 60 | - if (empty($item->extra['phast_no_defer']) | |
| 61 | - || empty($item->extra['data']) | |
| 62 | - ) { | |
| 63 | - continue; | |
| 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; | |
| 64 | 68 | } |
| 65 | - $item->extra['data'] = "'phast-no-defer';\n" . $item->extra['data']; | |
| 66 | - } | |
| 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); | |
| 67 | 76 | }); |
| 68 | 77 | |
| 69 | - foreach ([ | |
| 70 | - Compat\MonsterInsights::class, | |
| 71 | - Compat\Slimstat::class, | |
| 72 | - Compat\GoogleSiteKit::class, | |
| 73 | - Compat\GAGoogleAnalytics::class, | |
| 74 | - Compat\AdThrive::class, | |
| 75 | - ] as $class) { | |
| 76 | - (new $class())->setup(); | |
| 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 | + }); | |
| 78 | 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 | + }); | |
| 91 | + | |
| 79 | 92 | $sdk = phastpress_get_plugin_sdk(); |
| 80 | 93 | $handler = $sdk->getPhastAPI()->deployOutputBufferForDocument(); |
| 81 | 94 | |
| 82 | 95 | // Allow disabling PhastPress with hook. |
| 83 | - add_action('template_redirect', function () use ($handler) { | |
| 96 | + add_filter('template_redirect', function () use ($handler) { | |
| 84 | 97 | if (apply_filters('phastpress_disable', false)) { |
| 85 | 98 | $handler->cancel(); |
| 86 | 99 | } |
| 87 | 100 | }, 100); |