: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* Modules TrafficCop Page Parser.
* @author Advanced Ads <info@wpadvancedads.com>
namespace AdvancedAds\Modules\OneClick;
use AdvancedAds\Framework\Interfaces\Integration_Interface;
defined( 'ABSPATH' ) || exit;
* Modules TrafficCop Page Parser.
* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
class Page_Parser implements Integration_Interface {
public static function get_instance() {
if ( null === $instance ) {
$instance = new Page_Parser();
public function hooks() {
add_action( 'template_redirect', [ $this, 'start_buffer' ], -9999 );
add_action( 'wp_footer', [ $this, 'flush_page' ], 9999 );
public function get_page() {
public function start_buffer() {
ob_start( [ $this, 'parse' ] );
* Parse page for script tag
* @param string $buffer Page buffer.
public function parse( $buffer ): string {
$this->loop_script_tags();
$this->page = apply_filters( 'pubguru_current_page', $this->page ); // phpcs:ignore
* Flush page after footer
public function flush_page() {
$buffer_status = ob_get_status();
! empty( $buffer_status ) &&
1 === $buffer_status['type'] &&
get_class( $this ) . '::parse' === $buffer_status['name']
* Loop through script tags.
public function loop_script_tags() {
if ( ! has_filter( 'pubguru_page_script_tag' ) ) {
$scripts = $this->get_script_tags();
foreach ( $scripts as $script ) {
$replace = apply_filters( 'pubguru_page_script_tag', $script );
if ( false !== $replace ) {
$this->page = str_replace( $find, $replace, $this->page );
private function get_script_tags() {
preg_match_all( '/<script[\s\S]*?>[\s\S]*?<\/script>/i', $this->page, $matches );
return isset( $matches[0] ) ? $matches[0] : [];