* An implementation for ads served through Equativ Smart Ad Server.
* @package automattic/jetpack
use Automattic\Jetpack\Assets;
if ( ! defined( 'ABSPATH' ) ) {
// phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript
require_once WORDADS_ROOT . '/php/class-wordads-array-utils.php';
* Contains all the implementation details for Smart ads
* The single instance of the class.
protected static $instance = null;
* The parameters for WordAds.
* Has Smart asset been enqueued?
* @var bool True if Smart asset has been enqueued.
private $is_asset_enqueued = false;
* sidebar_widget formats represents the legacy Jetpack sidebar widget.
private $formats = array(
'bottom_sticky' => array(
'sidebar_sticky_right' => array(
'gutenberg_rectangle' => array(
'gutenberg_leaderboard' => array(
'gutenberg_mobile_leaderboard' => array(
'gutenberg_skyscraper' => array(
'sidebar_widget_mediumrectangle' => array(
'sidebar_widget_leaderboard' => array(
'sidebar_widget_wideskyscraper' => array(
private function __construct() {
* Ensures only one instance of WordAds_Smart is loaded or can be loaded.
public static function instance(): self {
if ( null === self::$instance ) {
self::$instance = new self();
* @param WordAds_Params $params Object containing WordAds settings.
public function init( WordAds_Params $params ) {
$this->override_formats_from_query_string();
if ( $this->has_any_format_enabled() ) {
* Enqueue any front-end CSS and JS.
public function enqueue_assets() {
if ( $this->is_asset_enqueued ) {
add_action( 'wp_head', array( $this, 'insert_config' ) );
'_inc/build/wordads/js/adflow-loader.min.js',
'nonmin_path' => 'modules/wordads/js/adflow-loader.js',
'dependencies' => array(),
'version' => JETPACK__VERSION,
esc_url( $this->get_config_url() ),
array( 'adflow_script_loader' ),
$this->is_asset_enqueued = true;
* Inserts ad tags on the page.
private function insert_ads() {
if ( $this->params->is_amp ) {
// Don't run on not found pages.
// Add the resource hints.
add_filter( 'wp_resource_hints', array( $this, 'resource_hints' ), 10, 2 );
$is_static_front_page = is_front_page() && 'page' === get_option( 'show_on_front' );
if ( ! ( $is_static_front_page || is_home() ) ) {
if ( $this->formats['inline']['enabled'] ) {
array( $this, 'insert_inline_marker' ),
if ( $this->formats['bottom_sticky']['enabled'] ) {
add_filter( 'wordads_iponweb_bottom_sticky_ad_disable', '__return_true', 10 );
if ( $this->formats['sidebar_sticky_right']['enabled'] ) {
add_filter( 'wordads_iponweb_sidebar_sticky_right_ad_disable', '__return_true', 10 );
* Inserts JS configuration used by watl.js.
public function insert_config() {
'post_id' => ( $post instanceof WP_Post ) && is_singular( 'post' ) ? $post->ID : null,
'theme' => get_stylesheet(),
'target' => $this->target_keywords(),
$js_config = WordAds_Array_Utils::array_to_js_object( $config );
wp_print_inline_script_tag( "var wa_smart = $js_config; wa_smart.cmd = [];" );
* Add the Smart resource hints.
* @param array $hints Domains for hinting.
* @param string $relation_type Resource type.
* @return array Domains for hinting.
public function resource_hints( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
$hints[] = '//af.pubmine.com';
* Gets the URL to a JSONP endpoint with configuration data.
* @return string The URL.
private function get_config_url(): string {
'https://public-api.wordpress.com/wpcom/v2/sites/%1$d/adflow/conf/?_jsonp=a8c_adflow_callback',
* Places marker at the end of the content so inline can identify the post content container.
* @param string|null $content The post content.
* @return string|null The post content with the marker appended.
public function insert_inline_marker( ?string $content ): ?string {
if ( null === $content ) {
$inline_ad_marker = '<span id="wordads-inline-marker" style="display: none;"></span>';
// Append the ad to the post content.
return $content . $inline_ad_marker;
* Gets a formatted list of target keywords.
* @return string Formatted list of target keywords.
private function target_keywords(): string {
$target_keywords = array_merge(
$this->get_blog_keywords(),
$this->get_language_keywords()
return implode( ';', $target_keywords );
* Gets a formatted list of blog keywords.
* @return array The list of blog keywords.
private function get_blog_keywords(): array {
return array( 'wp_blog_id=' . $this->params->blog_id );
* Gets the site language formatted as a keyword.
* @return array The language as a keyword.
private function get_language_keywords(): array {
return array( 'language=' . explode( '-', get_locale() )[0] );
* Enable formats by post types and the display options.
private function enable_formats(): void {
$this->formats['top']['enabled'] = $this->params->options['enable_header_ad'];
$this->formats['inline']['enabled'] = is_singular( 'post' ) && $this->params->options['wordads_inline_enabled'];
$this->formats['belowpost']['enabled'] = $this->params->should_show();
$this->formats['bottom_sticky']['enabled'] = $this->params->options['wordads_bottom_sticky_enabled'];
$this->formats['sidebar_sticky_right']['enabled'] = $this->params->options['wordads_sidebar_sticky_right_enabled'];
* Allow format enabled override from query string, eg. ?inline=true.
private function override_formats_from_query_string(): void {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_GET['wordads-logging'] ) ) {
foreach ( $this->formats as $format_type => $_ ) {
// phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET[ $format_type ] ) && 'true' === $_GET[ $format_type ] ) {
$this->formats[ $format_type ]['enabled'] = true;
* Check if has any format enabled.
* @return bool True if enabled, false otherwise.
private function has_any_format_enabled(): bool {
return in_array( true, array_column( $this->formats, 'enabled' ), true );