| 1 |
<?php |
| 2 |
|
| 3 |
function phastpress_deploy() { |
| 4 |
// We have to deploy on plugins_loaded action so we get wp_get_current_user() to be defined. |
| 5 |
if (is_admin()) { |
| 6 |
return; |
| 7 |
} |
| 8 |
|
| 9 |
// This is only defined in the main index.php. |
| 10 |
if (!defined('WP_USE_THEMES') || !WP_USE_THEMES) { |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
// Do not optimize when using editor plug-ins. |
| 15 |
if (// Elementor |
| 16 |
isset($_GET['elementor-preview']) |
| 17 |
// YellowPencil |
| 18 |
|| (defined('YP_VERSION') && isset($_GET['yellow_pencil_frame'])) |
| 19 |
// Thrive Architect |
| 20 |
|| (isset($_GET['tve']) && $_GET['tve'] === 'true') |
| 21 |
// Divi Visual Builder |
| 22 |
|| (isset($_GET['PageSpeed']) && $_GET['PageSpeed'] === 'off') |
| 23 |
// Child Theme Configurator |
| 24 |
|| (isset($_GET['ModPagespeed']) && $_GET['ModPagespeed'] === 'off') |
| 25 |
// Nimble Page Builder |
| 26 |
|| ( |
| 27 |
defined('NIMBLE_VERSION') && ( |
| 28 |
!empty($_GET['customize_changeset_uuid']) |
| 29 |
|| !empty($_POST['customize_changeset_uuid']) |
| 30 |
) |
| 31 |
) |
| 32 |
// Asset CleanUp: Page Speed Booster |
| 33 |
|| ( |
| 34 |
defined('WPACU_LOAD_ASSETS_REQ_KEY') |
| 35 |
&& !empty($_REQUEST[WPACU_LOAD_ASSETS_REQ_KEY]) |
| 36 |
) |
| 37 |
) { |
| 38 |
return; |
| 39 |
} |
| 40 |
|
| 41 |
// Support phast_no_defer script attribute. |
| 42 |
add_filter('script_loader_tag', function ($tag, $handle) { |
| 43 |
if (!wp_scripts()->get_data($handle, 'phast_no_defer')) { |
| 44 |
return $tag; |
| 45 |
} |
| 46 |
|
| 47 |
return preg_replace('~<script\b~i', '$0 data-phast-no-defer', $tag); |
| 48 |
}, 10, 2); |
| 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. |
| 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 |
$sdk = phastpress_get_plugin_sdk(); |
| 84 |
$handler = $sdk->getPhastAPI()->deployOutputBufferForDocument(); |
| 85 |
|
| 86 |
// Allow disabling PhastPress with hook. |
| 87 |
add_filter('template_redirect', function () use ($handler) { |
| 88 |
if (apply_filters('phastpress_disable', false)) { |
| 89 |
$handler->cancel(); |
| 90 |
} |
| 91 |
}, 100); |
| 92 |
|
| 93 |
$plugin_config = $sdk->getPluginConfiguration(); |
| 94 |
$display_footer = $plugin_config->shouldDisplayFooter(); |
| 95 |
if ($display_footer) { |
| 96 |
add_action('wp_head', 'phastpress_render_footer_css', 0, 2); |
| 97 |
add_action('wp_footer', 'phastpress_render_footer'); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
function phastpress_render_footer_css() { |
| 102 |
echo <<<STYLE |
| 103 |
<style> |
| 104 |
.phast-footer a:link, |
| 105 |
.phast-footer a:visited, |
| 106 |
.phast-footer a:hover { |
| 107 |
display: block; |
| 108 |
font-size: 12px; |
| 109 |
text-align: center; |
| 110 |
height: 20px; |
| 111 |
background: black; |
| 112 |
color: white; |
| 113 |
position: relative; |
| 114 |
top: 0; |
| 115 |
z-index: 1000; |
| 116 |
} |
| 117 |
</style> |
| 118 |
STYLE; |
| 119 |
} |
| 120 |
|
| 121 |
function phastpress_render_footer() { |
| 122 |
echo '<div class="phast-footer">' |
| 123 |
. '<a href="https://wordpress.org/plugins/phastpress/" target="_blank">' |
| 124 |
. __('Optimized by PhastPress', 'phastpress') . '</a></div>'; |
| 125 |
} |
| 126 |
|