* @since 1.48.0 */ 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 { /** * Hold page. * * @var string */ private $page = ''; /** * Get Parser Instance * * @return Page_Parser */ public static function get_instance() { static $instance; if ( null === $instance ) { $instance = new Page_Parser(); $instance->hooks(); } return $instance; } /** * Hook into WordPress. * * @return void */ public function hooks(): void { add_filter( 'wp_template_enhancement_output_buffer', [ $this, 'parse' ] ); } /** * Parse page for script tag * * @param string $buffer Page buffer. * * @return string */ public function parse( $buffer ): string { $this->page = $buffer; $this->loop_script_tags(); $this->page = apply_filters( 'advanced-ads-pubguru-current-page', $this->page ); // phpcs:ignore return $this->page; } /** * Loop through script tags. * * @return void */ public function loop_script_tags() { // Early bail!! if ( ! has_filter( 'advanced-ads-pubguru-page-script-tag' ) ) { return; } $scripts = $this->get_script_tags(); foreach ( $scripts as $script ) { $find = $script; $replace = apply_filters( 'advanced-ads-pubguru-page-script-tag', $script ); if ( false !== $replace ) { $this->page = str_replace( $find, $replace, $this->page ); } } } /** * Get script tags only. * * @return list */ private function get_script_tags() { $matches = []; preg_match_all( '/[\s\S]*?<\/script>/i', $this->page, $matches ); return isset( $matches[0] ) ? $matches[0] : []; } }