: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
use AdvancedAds\Assets_Registry;
use AdvancedAds\Entities;
use AdvancedAds\Utilities\WordPress;
defined( 'ABSPATH' ) || exit;
* Class Advanced_Ads_Admin_Meta_Boxes
class Advanced_Ads_Admin_Meta_Boxes {
* Instance of this class.
protected static $instance = null;
* @var array $meta_box_ids
protected $meta_box_ids = [];
* Advanced_Ads_Admin_Meta_Boxes constructor.
private function __construct() {
add_action( 'add_meta_boxes_' . Entities::POST_TYPE_AD, [ $this, 'add_meta_boxes' ] );
// add meta box for post types edit pages.
add_action( 'add_meta_boxes', [ $this, 'add_post_meta_box' ] );
add_action( 'save_post', [ $this, 'save_post_meta_box' ] );
// register dashboard widget.
add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widget' ] );
add_action( 'wp_dashboard_setup', [ $this, 'add_adsense_widget' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'adsense_widget_js' ] );
// fixes compatibility issue with WP QUADS PRO.
add_action( 'quads_meta_box_post_types', [ $this, 'fix_wpquadspro_issue' ], 11 );
public function add_adsense_widget() {
if ( Advanced_Ads_AdSense_Data::get_instance()->is_setup()
&& ! Advanced_Ads_AdSense_Data::get_instance()->is_hide_stats()
&& isset ( Advanced_Ads::get_instance()->get_adsense_options()['adsense-wp-widget'] ) ) {
'custom_dashboard_widget',
__( 'AdSense Earnings', 'advanced-ads' ),
[ $this, 'adsense_widget_content' ],
public function adsense_widget_content() {
Advanced_Ads_Overview_Widgets_Callbacks::add_meta_box(
'advads_overview_adsense_stats',
public function adsense_widget_js() {
if ( 'index.php' === $pagenow ) {
Assets_Registry::enqueue_script( 'wp-widget-adsense' );
* Return an instance of this class.
* @return object A single instance of this class.
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null === self::$instance ) {
self::$instance = new self();
public function add_meta_boxes() {
$post_type = Entities::POST_TYPE_AD;
__( 'Ad Type', 'advanced-ads' ),
[ $this, 'markup_meta_boxes' ],
&& Advanced_Ads_AdSense_Data::get_instance()->is_setup()
&& ! Advanced_Ads_AdSense_Data::get_instance()->is_hide_stats()
$ad_unit = Advanced_Ads_Network_Adsense::get_instance()->get_ad_unit( $post->ID );
/* translators: 1: Name of ad unit */
esc_html__( 'Earnings of %1$s', 'advanced-ads' ),
esc_html( $ad_unit->name )
[ $this, 'markup_meta_boxes' ],
// use dynamic filter from to add close class to ad type meta box after saved first time.
add_filter( 'postbox_classes_advanced_ads_ad-main-box', [ $this, 'close_ad_type_metabox' ] );
// show the Usage box for saved ads
if ( $post->filter === 'edit' ) {
__( 'Usage', 'advanced-ads' ),
[ $this, 'markup_meta_boxes' ],
__( 'Ad Parameters', 'advanced-ads' ),
[ $this, 'markup_meta_boxes' ],
__( 'Layout / Output', 'advanced-ads' ),
[ $this, 'markup_meta_boxes' ],
__( 'Targeting', 'advanced-ads' ),
[ $this, 'markup_meta_boxes' ],
if ( ! defined( 'AAP_VERSION' ) ) {
__( 'Increase your ad revenue', 'advanced-ads' ),
[ $this, 'markup_meta_boxes' ],
if ( ! defined( 'AAT_VERSION' ) ) {
__( 'Statistics', 'advanced-ads' ),
[ $this, 'markup_meta_boxes' ],
// register meta box ids.
'revisionsdiv', // revisions – only when activated.
'advanced_ads_groupsdiv', // automatically added by ad groups taxonomy.
// force AA meta boxes to never be completely hidden by screen options.
add_filter( 'hidden_meta_boxes', [ $this, 'unhide_meta_boxes' ], 10, 2 );
// hide the checkboxes for "unhideable" meta boxes within screen options via CSS.
add_action( 'admin_head', [ $this, 'unhide_meta_boxes_style' ] );
$whitelist = apply_filters(
'advanced-ads-ad-edit-allowed-metaboxes',
[ // meta boxes in this array can be hidden using Screen Option
'ad-layer-ads-box', // deprecated.
// remove non-white-listed meta boxes.
foreach ( [ 'normal', 'advanced', 'side' ] as $context ) {
if ( isset( $wp_meta_boxes[ $post_type ][ $context ] ) ) {
foreach ( [ 'high', 'sorted', 'core', 'default', 'low' ] as $priority ) {
if ( isset( $wp_meta_boxes[ $post_type ][ $context ][ $priority ] ) ) {
foreach ( (array) $wp_meta_boxes[ $post_type ][ $context ][ $priority ] as $id => $box ) {
if ( ! in_array( $id, $whitelist ) ) {
unset( $wp_meta_boxes[ $post_type ][ $context ][ $priority ][ $id ] );
* Load templates for all meta boxes
* @param WP_Post $post WP_Post object.
* @param array $box meta box information.
* @todo move ad initialization to main function and just global it
public function markup_meta_boxes( $post, $box ) {
$ad = \Advanced_Ads\Ad_Repository::get( $post->ID );
$view = 'ad-main-metabox.php';
$hndlelinks = '<a href="https://wpadvancedads.com/manual/ad-types?utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-ad-type" target="_blank" class="advads-manual-link">' . __( 'Manual', 'advanced-ads' ) . '</a>';
$view = 'ad-usage-metabox.php';
case 'ad-parameters-box':
$view = 'ad-parameters-metabox.php';
$positioning = ( new Advanced_Ads_Ad_Positioning( $ad ) )->return_admin_view();
$wrapper_id = $ad->options( 'output.wrapper-id', '' );
$wrapper_class = $ad->options( 'output.wrapper-class', '' );
$debug_mode_enabled = (bool) $ad->options( 'output.debugmode', false );
$view = 'ad-output-metabox.php';
$hndlelinks = '<a href="https://wpadvancedads.com/manual/optimizing-the-ad-layout/?utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-ad-layout" target="_blank" class="advads-manual-link">' . __( 'Manual', 'advanced-ads' ) . '</a>';
$view = 'conditions/ad-targeting-metabox.php';
$hndlelinks = '<a href="#" class="advads-video-link">' . __( 'Video', 'advanced-ads' ) . '</a>';
$hndlelinks .= '<a href="https://wpadvancedads.com/manual/display-conditions/?utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-display" target="_blank" class="advads-manual-link">' . __( 'Display Conditions', 'advanced-ads' ) . '</a>';
$hndlelinks .= '<a href="https://wpadvancedads.com/manual/visitor-conditions/?utm_source=advanced-ads&utm_medium=link&utm_campaign=edit-visitor" target="_blank" class="advads-manual-link">' . __( 'Visitor Conditions', 'advanced-ads' ) . '</a>';
$videomarkup = '<iframe width="420" height="315" src="https://www.youtube-nocookie.com/embed/VjfrRl5Qn4I?rel=0&showinfo=0" frameborder="0" allowfullscreen></iframe>';
$ad_options = $ad->options();
$display_conditions = is_array( $ad_options['conditions'] ) ? $ad_options['conditions'] : []; // default value for older version is `""` (empty string)
$visitor_conditions = is_array( $ad_options['visitors'] ) ? $ad_options['visitors'] : []; // default value for older version is `""` (empty string)
$display_conditions_available = ( empty( $display_conditions ) );
$visitor_conditions_available = ( empty( $visitor_conditions ) );
$view = 'upgrades/all-access.php';
case 'advads-tracking-pitch':
$view = 'upgrades/tracking.php';
case 'advads-gadsense-box':
if ( $ad && isset( $ad->type ) && 'adsense' === $ad->type ) {
if ( isset( $ad->content ) ) {
$json_content = json_decode( $ad->content );
if ( isset( $json_content->slotId ) ) {
$unit_code = $json_content->slotId;
$report_filter = $unit_code;
$advads_gadsense_options['hidden'] = ! $unit_code;
$view = 'gadsense-dashboard.php';
$pub_id = Advanced_Ads_AdSense_Data::get_instance()->get_adsense_id();
$hndlelinks = '<a href="' . esc_url( admin_url( 'admin.php?page=advanced-ads-settings#top#adsense' ) ) . '" target="_blank">' . __( 'Disable', 'advanced-ads' ) . '</a>';
if ( ! isset( $view ) ) {
// markup moved to handle headline of the metabox.
if ( isset( $hndlelinks ) ) {
?><span class="advads-hndlelinks hidden">
if ( isset( $videomarkup ) ) {
echo '<div class="advads-video-link-container" data-videolink=\'' . wp_kses(
* elements in $warnings contain [text] and [class] attributes.
// show warning if ad contains https in parameters box.
$https_message = Advanced_Ads_Ad_Debug::is_https_and_http( $ad );
if ( 'ad-parameters-box' === $box['id'] && $https_message ) {
'text' => $https_message,
'class' => 'advads-ad-notice-https-missing advads-notice-inline advads-error',
if ( 'ad-parameters-box' === $box['id'] ) {
'text' => Advanced_Ads_AdSense_Admin::get_auto_ads_messages()[ Advanced_Ads_AdSense_Data::get_instance()->is_page_level_enabled() ? 'enabled' : 'disabled' ],
'class' => 'advads-auto-ad-in-ad-content hidden advads-notice-inline advads-error',
// Let users know that they could use the Google AdSense ad type when they enter an AdSense code.
if ( 'ad-parameters-box' === $box['id'] && Advanced_Ads_Ad_Type_Adsense::content_is_adsense( $ad->content ) && in_array( $ad->type, [ 'plain', 'content' ], true ) ) {
false === strpos( $ad->content, 'enable_page_level_ads' )
&& ! preg_match( '/script[^>]+data-ad-client=/', $ad->content )
$adsense_auto_ads = Advanced_Ads_AdSense_Data::get_instance()->is_page_level_enabled();
'class' => 'advads-adsense-found-in-content advads-notice-inline advads-error',
// translators: %1$s opening button tag, %2$s closing button tag.
esc_html__( 'This looks like an AdSense ad. Switch the ad type to “AdSense ad” to make use of more features. %1$sSwitch to AdSense ad%2$s.', 'advanced' ),
'<button class="button-secondary" id="switch-to-adsense-type">',
$warnings = apply_filters( 'advanced-ads-ad-notices', $warnings, $box, $post );
echo '<ul id="' . esc_attr( $box['id'] ) . '-notices" class="advads-metabox-notices">';
foreach ( $warnings as $_warning ) {
if ( isset( $_warning['text'] ) ) :
$warning_class = isset( $_warning['class'] ) ? $_warning['class'] : '';
echo '<li class="' . esc_attr( $warning_class ) . '">';
// skip CodeSniffer because this could be complex HTML.
include ADVADS_ABSPATH . 'admin/views/' . $view;
* Force all AA related meta boxes to stay visible
* @param array $hidden An array of hidden meta boxes.
* @param WP_Screen $screen WP_Screen object of the current screen.
public function unhide_meta_boxes( $hidden, $screen ) {
// only check on Advanced Ads edit screen.
if ( ! isset( $screen->id ) || 'advanced_ads' !== $screen->id || ! is_array( $this->meta_box_ids ) ) {
// return only hidden elements which are not among the Advanced Ads meta box ids.
return array_diff( $hidden, (array) apply_filters( 'advanced-ads-unhide-meta-boxes', $this->meta_box_ids ) );
* Add dynamic CSS for un-hideable meta boxes.
public function unhide_meta_boxes_style() {
$screen = get_current_screen();
if ( empty( $screen ) || ! isset( $screen->id ) || 'advanced_ads' !== $screen->id ) {
$meta_boxes = (array) apply_filters( 'advanced-ads-unhide-meta-boxes', $this->meta_box_ids );
if ( empty( $meta_boxes ) ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- we don't need to escape the string we just concatenated.
printf( '<style>%s {display: none;}</style>', implode( ', ', array_reduce( $meta_boxes, function( $styles, $box_id ) {
$styles[] = sprintf( 'label[for="%s-hide"]', $box_id );
* Add a meta box to post type edit screens with ad settings
* @param string $post_type current post type.
public function add_post_meta_box( $post_type = '' ) {
// don’t display for non admins.
if ( ! WordPress::user_can( 'advanced_ads_edit_ads' ) ) {
// get public post types.
$public_post_types = get_post_types(
'publicly_queryable' => true,
// limit meta box to public post types.
if ( in_array( $post_type, $public_post_types ) ) {
$disabled_post_types = Advanced_Ads::get_instance()->options()['pro']['general']['disable-by-post-types'] ?? [];
__( 'Ad Settings', 'advanced-ads' ),
[ $this, in_array( $post_type, $disabled_post_types, true ) ? 'render_disable_post_type_notice' : 'render_post_meta_box' ],
* Render meta box for ad settings on a per post basis
* @param WP_Post $post The post object.
public function render_post_meta_box( $post ) {
// nonce field to check when we save the values.
wp_nonce_field( 'advads_post_meta_box', 'advads_post_meta_box_nonce' );
// retrieve an existing value from the database.
$values = get_post_meta( $post->ID, '_advads_ad_settings', true );
include ADVADS_ABSPATH . 'admin/views/post-ad-settings-metabox.php';
do_action( 'advanced_ads_render_post_meta_box', $post, $values );
* Save the ad meta when the post is saved.
* @param int $post_id The ID of the post being saved.
* @return mixed empty or post ID.