| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | +use Kibo\PhastPlugins\PhastPress\Compat; | |
| 4 | + | |
| 3 | 5 | function phastpress_deploy() { |
| 4 | 6 | // We have to deploy on plugins_loaded action so we get wp_get_current_user() to be defined. |
| 5 | 7 | if (is_admin()) { |
| 6 | 8 | return; |
| @@ -33,8 +35,14 @@ | ||
| 33 | 35 | || ( |
| 34 | 36 | defined('WPACU_LOAD_ASSETS_REQ_KEY') |
| 35 | 37 | && !empty($_REQUEST[WPACU_LOAD_ASSETS_REQ_KEY]) |
| 36 | 38 | ) |
| 39 | + // WPBakery | |
| 40 | + || ( | |
| 41 | + defined('WPB_VC_VERSION') | |
| 42 | + && isset($_GET['vc_editable']) | |
| 43 | + && $_GET['vc_editable'] === 'true' | |
| 44 | + ) | |
| 37 | 45 | ) { |
| 38 | 46 | return; |
| 39 | 47 | } |
| 40 | 48 | |
| @@ -46,55 +54,34 @@ | ||
| 46 | 54 | |
| 47 | 55 | return preg_replace('~<script\b~i', '$0 data-phast-no-defer', $tag); |
| 48 | 56 | }, 10, 2); |
| 49 | 57 | |
| 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 | 58 | add_filter('wp_print_scripts', function () { |
| 60 | - if (!wp_script_is('wp_slimstat')) { | |
| 61 | - return; | |
| 59 | + foreach (wp_scripts()->registered as $handle => $item) { | |
| 60 | + if (empty($item->extra['phast_no_defer']) | |
| 61 | + || empty($item->extra['data']) | |
| 62 | + ) { | |
| 63 | + continue; | |
| 64 | + } | |
| 65 | + $item->extra['data'] = "'phast-no-defer';\n" . $item->extra['data']; | |
| 62 | 66 | } |
| 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 | 67 | }); |
| 77 | 68 | |
| 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 | - }); | |
| 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 | + } | |
| 82 | 78 | |
| 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 | - | |
| 92 | 79 | $sdk = phastpress_get_plugin_sdk(); |
| 93 | 80 | $handler = $sdk->getPhastAPI()->deployOutputBufferForDocument(); |
| 94 | 81 | |
| 95 | 82 | // Allow disabling PhastPress with hook. |
| 96 | - add_filter('template_redirect', function () use ($handler) { | |
| 83 | + add_action('template_redirect', function () use ($handler) { | |
| 97 | 84 | if (apply_filters('phastpress_disable', false)) { |
| 98 | 85 | $handler->cancel(); |
| 99 | 86 | } |
| 100 | 87 | }, 100); |