: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* The class is responsible to inject header bidding tags.
* @author Advanced Ads <info@wpadvancedads.com>
namespace AdvancedAds\Modules\OneClick;
use AdvancedAds\Utilities\Str;
use AdvancedAds\Framework\Interfaces\Integration_Interface;
defined( 'ABSPATH' ) || exit;
class Header_Bidding implements Integration_Interface {
private $has_gpt = false;
* Check if has pubguru tag
private $has_pubguru = false;
* Check if has traffic cop atq
private $has_traffic_cop = false;
public function hooks(): void {
add_filter( 'pubguru_page_script_tag', [ $this, 'remove_tags' ] );
add_filter( 'pubguru_current_page', [ $this, 'add_tags' ] );
* @param string $page Page html.
public function add_tags( $page ): string {
$page = $this->add_script_tag( $page );
* @param string $script Scrip tag.
public function remove_tags( $script ): string {
if ( Str::str_contains( '/gpt.js', $script ) ) {
if ( Str::str_contains( '//m2d.m2.ai/', $script ) || Str::str_contains( '//c.pubguru.net/', $script ) ) {
$this->has_pubguru = true;
if ( Str::str_contains( 'window.pg.atq = window.pg.atq || [];', $script ) ) {
$this->has_traffic_cop = true;
* Guard the page from getting JS errors
* @param string $page Page html.
private function add_script_tag( $page ): string {
$name = Helpers::get_config_file();
$script[] = '<script src="https://securepubads.g.doubleclick.net/tag/js/gpt.js" async></script>'; // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
$script[] = '<script type="text/javascript">window.googletag=window.googletag||{};window.googletag.cmd=window.googletag.cmd||[];';
$script[] = 'window.googletag.cmd.push(function(){window.__onpageGptEmbed=(new Date()).getTime()})</script>';
if ( $this->has_pubguru ) {
$script[] = sprintf( '<script src="//c.pubguru.net/%s" async> </script>', $name ); // phpcs:ignore
if ( Helpers::is_module_enabled( 'traffic_cop' ) && Helpers::has_traffic_cop() && $this->has_traffic_cop ) {
$script[] = '<script>window.pg = window.pg || {};window.pg.atq = window.pg.atq || [];</script>';
$page = str_replace( '<head>', join( "\n", $script ), $page );