| 1 |
<?php |
| 2 |
namespace Kibo\PhastPlugins\PhastPress\Compat; |
| 3 |
|
| 4 |
class AdThrive { |
| 5 |
public function setup() { |
| 6 |
add_action('template_redirect', function () { |
| 7 |
$this->template_redirect(); |
| 8 |
}); |
| 9 |
} |
| 10 |
|
| 11 |
private function template_redirect() { |
| 12 |
if (!class_exists(\AdThrive_Ads\Components\Ads\Main::class)) { |
| 13 |
return; |
| 14 |
} |
| 15 |
|
| 16 |
if (!isset($GLOBALS['wp_filter']['wp_head'])) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
$filter = $GLOBALS['wp_filter']['wp_head']; |
| 21 |
|
| 22 |
array_walk($filter->callbacks, function (&$callbacks) { |
| 23 |
array_walk($callbacks, function (&$callback) { |
| 24 |
$function = $callback['function']; |
| 25 |
if (is_array($function) |
| 26 |
&& isset($function[0]) |
| 27 |
&& $function[0] instanceof \AdThrive_Ads\Components\Ads\Main |
| 28 |
&& isset($function[1]) |
| 29 |
&& $function[1] === 'ad_head' |
| 30 |
) { |
| 31 |
$callback['function'] = function (...$args) use ($function) { |
| 32 |
ob_start(function ($buffer) { |
| 33 |
return $this->processOutput($buffer); |
| 34 |
}); |
| 35 |
try { |
| 36 |
return call_user_func_array($function, $args); |
| 37 |
} finally { |
| 38 |
ob_end_flush(); |
| 39 |
} |
| 40 |
}; |
| 41 |
} |
| 42 |
}); |
| 43 |
}); |
| 44 |
} |
| 45 |
|
| 46 |
private function processOutput($output) { |
| 47 |
return preg_replace('~<script\b~i', '$0 data-phast-no-defer', $output); |
| 48 |
} |
| 49 |
} |
| 50 |
|