| 1 |
<?php |
| 2 |
|
| 3 |
use Kibo\PhastPlugins\PhastPress\Compat; |
| 4 |
|
| 5 |
function phastpress_deploy() { |
| 6 |
// We have to deploy on plugins_loaded action so we get wp_get_current_user() to be defined. |
| 7 |
if (is_admin()) { |
| 8 |
return; |
| 9 |
} |
| 10 |
|
| 11 |
// This is only defined in the main index.php. |
| 12 |
if (!defined('WP_USE_THEMES') || !WP_USE_THEMES) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
// Do not optimize when using editor plug-ins. |
| 17 |
if (// Elementor |
| 18 |
isset($_GET['elementor-preview']) |
| 19 |
// YellowPencil |
| 20 |
|| (defined('YP_VERSION') && isset($_GET['yellow_pencil_frame'])) |
| 21 |
// Thrive Architect |
| 22 |
|| (isset($_GET['tve']) && $_GET['tve'] === 'true') |
| 23 |
// Divi Visual Builder |
| 24 |
|| (isset($_GET['PageSpeed']) && $_GET['PageSpeed'] === 'off') |
| 25 |
// Child Theme Configurator |
| 26 |
|| (isset($_GET['ModPagespeed']) && $_GET['ModPagespeed'] === 'off') |
| 27 |
// Nimble Page Builder |
| 28 |
|| ( |
| 29 |
defined('NIMBLE_VERSION') && ( |
| 30 |
!empty($_GET['customize_changeset_uuid']) |
| 31 |
|| !empty($_POST['customize_changeset_uuid']) |
| 32 |
) |
| 33 |
) |
| 34 |
// Asset CleanUp: Page Speed Booster |
| 35 |
|| ( |
| 36 |
defined('WPACU_LOAD_ASSETS_REQ_KEY') |
| 37 |
&& !empty($_REQUEST[WPACU_LOAD_ASSETS_REQ_KEY]) |
| 38 |
) |
| 39 |
// WPBakery |
| 40 |
|| ( |
| 41 |
defined('WPB_VC_VERSION') |
| 42 |
&& isset($_GET['vc_editable']) |
| 43 |
&& $_GET['vc_editable'] === 'true' |
| 44 |
) |
| 45 |
// Oxygen |
| 46 |
|| ( |
| 47 |
defined('CT_VERSION') |
| 48 |
&& isset($_GET['ct_builder']) |
| 49 |
&& $_GET['ct_builder'] === 'true' |
| 50 |
) |
| 51 |
) { |
| 52 |
Compat\Log::add(null, 'Disabling PhastPress during visual editing'); |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
// Support phast_no_defer script attribute. |
| 57 |
add_filter('script_loader_tag', function ($tag, $handle) { |
| 58 |
if (!wp_scripts()->get_data($handle, 'phast_no_defer')) { |
| 59 |
return $tag; |
| 60 |
} |
| 61 |
|
| 62 |
return preg_replace('~<script\b~i', '$0 data-phast-no-defer', $tag); |
| 63 |
}, 10, 2); |
| 64 |
|
| 65 |
add_filter('wp_print_scripts', function () { |
| 66 |
foreach (wp_scripts()->registered as $handle => $item) { |
| 67 |
if (empty($item->extra['phast_no_defer']) |
| 68 |
|| empty($item->extra['data']) |
| 69 |
) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
$item->extra['data'] = "'phast-no-defer';\n" . $item->extra['data']; |
| 73 |
} |
| 74 |
}); |
| 75 |
|
| 76 |
foreach ([ |
| 77 |
Compat\MonsterInsights::class, |
| 78 |
Compat\Slimstat::class, |
| 79 |
Compat\GoogleSiteKit::class, |
| 80 |
Compat\GAGoogleAnalytics::class, |
| 81 |
Compat\AdThrive::class, |
| 82 |
Compat\TwentyTwentyOneDarkMode::class, |
| 83 |
Compat\BurstStatistics::class, |
| 84 |
] as $class) { |
| 85 |
(new $class())->setup(); |
| 86 |
} |
| 87 |
|
| 88 |
$sdk = phastpress_get_plugin_sdk(); |
| 89 |
$handler = $sdk->getPhastAPI()->deployOutputBufferForDocument(); |
| 90 |
|
| 91 |
// Allow disabling PhastPress with hook. |
| 92 |
if ($handler) { |
| 93 |
add_action('template_redirect', function () use ($handler) { |
| 94 |
if (apply_filters('phastpress_disable', false)) { |
| 95 |
$handler->cancel(); |
| 96 |
} |
| 97 |
}, 100); |
| 98 |
} |
| 99 |
|
| 100 |
$plugin_config = $sdk->getPluginConfiguration(); |
| 101 |
$display_footer = $plugin_config->shouldDisplayFooter(); |
| 102 |
if ($display_footer) { |
| 103 |
add_action('wp_head', 'phastpress_render_footer_css', 0, 2); |
| 104 |
add_action('wp_footer', 'phastpress_render_footer'); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
function phastpress_render_footer_css() { |
| 109 |
echo <<<STYLE |
| 110 |
<style> |
| 111 |
.phast-footer a:link, |
| 112 |
.phast-footer a:visited, |
| 113 |
.phast-footer a:hover { |
| 114 |
display: block; |
| 115 |
font-size: 12px; |
| 116 |
text-align: center; |
| 117 |
height: 20px; |
| 118 |
background: black; |
| 119 |
color: white; |
| 120 |
position: relative; |
| 121 |
top: 0; |
| 122 |
z-index: 1000; |
| 123 |
} |
| 124 |
</style> |
| 125 |
STYLE; |
| 126 |
} |
| 127 |
|
| 128 |
function phastpress_render_footer() { |
| 129 |
echo '<div class="phast-footer">' |
| 130 |
. '<a href="https://wordpress.org/plugins/phastpress/" target="_blank">' |
| 131 |
. __('Optimized by PhastPress', 'phastpress') . '</a></div>'; |
| 132 |
} |
| 133 |
|
| 134 |
function phastpress_script(string $script, array $attrs = []): string { |
| 135 |
if (!isset($attrs['nonce'])) { |
| 136 |
$nonce = apply_filters('phastpress_csp_nonce', null); |
| 137 |
if ($nonce !== null) { |
| 138 |
$attrs['nonce'] = (string) $nonce; |
| 139 |
} |
| 140 |
} |
| 141 |
return (string) wp_get_inline_script_tag($script, $attrs); |
| 142 |
} |
| 143 |
|