: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
<?php // phpcs:ignoreFile
use AdvancedAds\Utilities\WordPress;
* AdSense Management API class.
class Advanced_Ads_AdSense_MAPI {
const OPTNAME = 'advanced-ads-adsense-mapi';
const ALERTS_URL = 'https://adsense.googleapis.com/v2/accounts/PUBID/alerts';
const CID = '400595147946-3ot506jh20qld7bqmg1l87ms4vn2uok5.apps.googleusercontent.com';
const CS = 'WKX8ghwUbxdrBcVmZ9WXOKph';
const REDIRECT_URI = 'https://c.wpadvancedads.com/oauth.php';
private static $instance = null;
private static $default_options = [];
private static $empty_account_data = [
private function __construct() {
add_action( 'admin_enqueue_scripts', [ $this, 'admin_scripts' ] );
add_action( 'wp_ajax_advads_gadsense_mapi_confirm_code', [ $this, 'ajax_confirm_code' ] );
//add_action( 'wp_ajax_advads_gadsense_mapi_get_adUnits', array( $this, 'ajax_get_adUnits' ) );
add_action( 'wp_ajax_advads_gadsense_mapi_get_details', [ $this, 'ajax_get_account_details' ] );
add_action( 'wp_ajax_advads_gadsense_mapi_select_account', [ $this, 'ajax_account_selected' ] );
add_action( 'wp_ajax_advads_mapi_get_adCode', [ $this, 'ajax_get_ad_code' ] );
add_action( 'wp_ajax_advads-mapi-reconstructed-code', [ $this, 'ajax_save_reconstructed_code' ] );
add_action( 'wp_ajax_advads-mapi-save-manual-code', [ $this, 'ajax_save_manual_code' ] );
add_action( 'wp_ajax_advads-mapi-revoke-token', [ $this, 'ajax_revoke_tokken' ] );
add_action( 'wp_ajax_advads-mapi-get-alerts', [ $this, 'ajax_get_account_alerts' ] );
add_action( 'wp_ajax_advads-mapi-dismiss-alert', [ $this, 'ajax_dismiss_alert' ] );
add_action( 'wp_ajax_advads_adsense_report_refresh', [ 'Advanced_Ads_Overview_Widgets_Callbacks', 'ajax_gadsense_dashboard' ] );
add_action( 'admin_footer', [ $this, 'admin_footer' ] );
self::$default_options = [
'unsupported_units' => [],
'count' => self::CALL_PER_24H,
add_filter( 'advanced-ads-support-messages', [ 'Advanced_Ads_AdSense_MAPI', 'adsense_warnings_check' ] );
add_action( 'wp_loaded', [ $this, 'update_ad_health_notices' ] );
* Update all MAPI related notices.
public function update_ad_health_notices() {
$mapi_options = self::get_option();
$connection_error_messages = self::get_connect_error_messages();
$health_class = Advanced_Ads_Ad_Health_Notices::get_instance();
// Last connection failed.
if ( isset ( $mapi_options['connect_error'] ) && ! empty( $mapi_options['connect_error'] ) ) {
if ( isset( $connection_error_messages[ $mapi_options['connect_error']['reason'] ] ) ) {
$health_class->add( 'adsense_connect_' . $mapi_options['connect_error']['reason'] );
$health_class->add( 'adsense_connect_' . $mapi_options['connect_error']['reason'], [
'text' => esc_html__( 'Last AdSense account connection attempt failed.', 'advanced-ads' ) . ' ' . $mapi_options['connect_error']['message'],
foreach ( $health_class->notices as $key => $value ) {
// There was already a connection error but the user tried again and obtained another error.
if ( false !== stripos( $key, 'adsense_connect_' ) && 'adsense_connect_' . $mapi_options['connect_error']['reason'] !== $key ) {
$health_class->remove( $key );
// Once a connection has been established (or a the warning has been discarded on the AA settings page), remove connection related notices.
foreach ( $health_class->notices as $key => $value ) {
if ( false !== stripos( $key, 'adsense_connect_' ) ) {
$health_class->remove( $key );
$gadsense_data = Advanced_Ads_AdSense_Data::get_instance();
$adsense_id = $gadsense_data->get_adsense_id();
$alerts = Advanced_Ads_AdSense_MAPI::get_stored_account_alerts( $adsense_id );
// AdSense account alerts (can not happens simultaneously with the connection error).
if ( is_array( $alerts ) && isset( $alerts['items'] ) && is_array( $alerts['items'] ) && $alerts['items'] ) {
$alerts_advads_messages = Advanced_Ads_Adsense_MAPI::get_adsense_alert_messages();
foreach ( $alerts['items'] as $internal_id => $item ) {
$item_id = isset( $item['id'] ) ? $item['id'] : str_replace( '-', '_', strtoupper( $item['type'] ) );
if ( isset( $alerts_advads_messages[ $item_id ] ) ) {
$health_class->add( 'adsense_alert_' . $item_id );
'adsense_alert_' . $item_id,
'text' => $item['message'] . ' ' . self::get_adsense_error_link( $item_id ),
// Remove notices that no more exist in the AdSense account (or have been dismissed).
foreach ( $health_class->notices as $key => $value ) {
if ( false !== stripos( $key, 'adsense_alert_' ) ) {
$alert_id = substr( $key, strlen( 'adsense_alert_' ) );
if ( ! in_array( $alert_id, $item_ids, true ) ) {
foreach ( $_remove_ids as $id ) {
$health_class->remove( $id );
foreach ( $health_class->notices as $key => $value ) {
if ( false !== stripos( $key, 'adsense_alert_' ) ) {
$health_class->remove( $key );
* Get available quota and eventual message about remaining call
public function get_quota() {
$options = $this->get_option();
if ( self::use_user_app() ) {
return [ 'count' => PHP_INT_MAX ];
if ( $now > $options['quota']['ts'] + ( 24 * 3600 ) ) {
'count' => self::CALL_PER_24H,
$msg = $this->get_quota_msg();
'count' => $options['quota']['count'],
public function get_quota_msg() {
$options = $this->get_option();
$secs = $options['quota']['ts'] + ( 24 * 3600 ) - $now;
$hours = floor( $secs / 3600 );
$mins = ceil( ( $secs - ( $hours * 3600 ) ) / 60 );
if ( 0 === (int) $options['quota']['count'] ) {
'No API call left before %1$s %2$s.',
sprintf( '%s hours', $hours ),
sprintf( '%s minutes', $mins )
$msg = 'No API call left before.';
$msg = 'No API call left.';
'%1$s for the next %2$s %3$s',
sprintf( '%s API calls remaining', $options['quota']['count'] ),
sprintf( '%s hours', $hours ),
sprintf( '%s minutes', $mins )
$msg = sprintf( '%s API calls remaining.', $options['quota']['count'] );
'%1$s for the next %2$s',
sprintf( '%s API calls remaining', $options['quota']['count'] ),
sprintf( '%s hours', $hours )
* Decrement quota by 1, and return message about remaining call
public function decrement_quota() {
$options = $this->get_option();
if ( 0 < $options['quota']['count'] ) {
$options['quota']['count']--;
if ( $now > $options['quota']['ts'] + ( 24 * 3600 ) ) {
$options['quota']['ts'] = $now;
update_option( self::OPTNAME, $options );
return $this->get_quota_msg();
* Get ad code from Google fpr a given ad unit
* @param string $ad_unit the ad unit to get the ad code for.
* @return array response to send back to the browser.
public function get_ad_code( $ad_unit ) {
$options = self::get_option();
$gadsense_data = Advanced_Ads_AdSense_Data::get_instance();
$adsense_id = $gadsense_data->get_adsense_id();
$unit_id = explode( ':', $ad_unit )[1];
$url = 'https://adsense.googleapis.com/v2/accounts/' . $adsense_id . '/adclients/ca-' . $adsense_id . '/adunits/' . $unit_id . '/adcode';
$access_token = self::get_access_token( $adsense_id );
foreach ( Advanced_Ads_Network_Adsense::get_instance()->get_external_ad_units() as $unit ) {
&& in_array( $unit->raw['contentAdsSettings']['type'], [ 'ARTICLE', 'FEED', 'MATCHED_CONTENT' ], true )
&& $ad_unit === $unit->id
&& ! array_key_exists( $ad_unit, $options['ad_codes'] )
$options['unsupported_units'][ $ad_unit ] = 1;
update_option( self::OPTNAME, $options );
'msg' => 'doesNotSupportAdUnitType',
if ( ! isset( $access_token['msg'] ) ) {
$headers = [ 'Authorization' => 'Bearer ' . $access_token ];
$response = wp_remote_get( $url, [ 'headers' => $headers ] );
self::log( 'Get ad code for ad Unit [' . $ad_unit . ']' );
if ( is_wp_error( $response ) ) {
// translators: %s: ad unit ID.
'msg' => sprintf( esc_html__( 'Error while retrieving ad code for "%s".', 'advanced-ads' ), $ad_unit ),
'raw' => $response->get_error_message(),
$ad_code = json_decode( $response['body'], true );
if ( null === $ad_code || ! isset( $ad_code['adCode'] ) ) {
if ( $ad_code['error'] &&
$ad_code['error']['errors'] &&
isset( $ad_code['error']['errors'][0]['reason'] ) &&
'doesNotSupportAdUnitType' === $ad_code['error']['errors'][0]['reason']
if ( array_key_exists( $ad_unit, $options['ad_codes'] ) && array_key_exists( $ad_unit, $options['unsupported_units'] ) ) {
unset( $options['unsupported_units'][ $ad_unit ] );
$options['unsupported_units'][ $ad_unit ] = 1;
update_option( self::OPTNAME, $options );
'msg' => 'doesNotSupportAdUnitType',
// translators: %s: ad unit ID.
'msg' => sprintf( esc_html__( 'Invalid response while retrieving ad code for "%s".', 'advanced-ads' ), $ad_unit ),
'raw' => $response['body'],
$options['ad_codes'][ $ad_unit ] = $ad_code['adCode'];
if ( isset( $options['unsupported_units'][ $ad_unit ] ) ) {
unset( $options['unsupported_units'][ $ad_unit ] );
update_option( self::OPTNAME, $options );
return $ad_code['adCode'];
// return the original error info.
* Convert ad unit data to v1.4 format to match the current UI and logics
* @param array $ad_unit ad unit in MAPI v2 format.
* @return array the ad unit in MAPI v1.4 format.
public static function convert_ad_unit_format( $ad_unit ) {
$chunks = explode( '/', $ad_unit['name'] );
'name' => $ad_unit['displayName'],
'nameV2' => $ad_unit['name'],
'id' => $ad_unit['reportingDimensionId'],
'code' => $chunks[ count( $chunks ) - 1 ],
'status' => $ad_unit['state'],
'contentAdsSettings' => $ad_unit['contentAdsSettings'],
'reportingDimensionId' => $ad_unit['reportingDimensionId'],
* Get/Update ad unit list for a given client
* @param string $account publisher ID.
public static function get_ad_units( $account ) {
$url = 'https://adsense.googleapis.com/v2/accounts/' . $account . '/adclients/ca-' . $account . '/adunits?pageSize=350';
$access_token = self::get_access_token( $account );
if ( isset( $access_token['msg'] ) ) {
// Return the token related message.
$response = wp_remote_get( $url, [ 'headers' => [ 'Authorization' => 'Bearer ' . $access_token ] ] );
self::log( 'Get ad units list for ca-' . $account );
if ( is_wp_error( $response ) ) {
// translators: %s is the publisher ID.
'msg' => sprintf( esc_html__( 'Error while retrieving ad unit list for "%s".', 'advanced-ads' ), $account ),
'raw' => $response->get_error_message(),
if ( trim( $response['body'] ) === '{}' ) {
// translators: %1$s is the AdSense publisher ID; %2$s a starting a tag to the AdSense ad unit list and %3$s the closing link.
esc_html__( 'The account "%1$s" does not seem to have any ad units. Please create some %2$shere%3$s.', 'advanced-ads' ),
'<a href="https://www.google.com/adsense/new/u/0/' . $account . '/myads/units" target="_blank">',
'raw' => $response['body'],
$response_body = json_decode( trim( $response['body'] ), true );
if ( null === $response_body || ! isset( $response_body['adUnits'] ) ) {
// translators: %s is the publisher ID.
$error_message = sprintf( esc_html__( 'Invalid response while retrieving ad unit list for "%s".', 'advanced-ads' ), $account );
// check the response for errors and display them for better problem-solving.
if ( ! empty( $response_body['error']['errors'] ) ) {
foreach ( $response_body['error']['errors'] as $err ) {
$hint = self::get_adsense_error_hint( $err['reason'] );
$error_message .= '<p class="description">' . $hint . '</p>';
$error_message .= sprintf(
'<p class="description">%1$s %2$s<br>%3$s %4$s</p>',
_x( 'Reason:', 'Reason of the API call error', 'advanced-ads' ),
_x( 'Message:', 'Error message from Google', 'advanced-ads' ),
'raw' => trim( $response['body'] ),
$options = self::get_option();
if ( ! isset( $response_body['nextPageToken'] ) ) {
// Results fit into a single page (of 350 items).
foreach ( $response_body['adUnits'] as $item ) {
$item = self::convert_ad_unit_format( $item );
$new_ad_units[ $item['id'] ] = $item;
$options['accounts'][ $account ]['ad_units'] = $new_ad_units;
update_option( self::OPTNAME, $options );
return [ 'done' => true ];
// There are more than 350 items in the account.
$page_token = $response_body['nextPageToken'];
foreach ( $response_body['adUnits'] as $item ) {
$item = self::convert_ad_unit_format( $item );
$new_ad_units[ $item['id'] ] = $item;
// While there is a next page of results do . . .
$access_token = self::get_access_token( $account );
if ( isset( $access_token['msg'] ) ) {
// return the original error info.
$next_url = $url . '&pageToken=' . urlencode( $page_token );
'Authorization' => 'Bearer ' . $access_token,
$response = wp_remote_get( $next_url, [ 'headers' => $headers ] );
self::log( 'Get ad unit list for ca-' . $account . ' page ' . $page );
if ( is_wp_error( $response ) ) {
// translators: %s is the publisher ID.
'msg' => sprintf( esc_html__( 'Error while retrieving ad unit list for "%s".', 'advanced-ads' ), $account ),
'raw' => $response->get_error_message(),
$response_body = json_decode( $response['body'], true );
// Update page token if there are ad units left.
$page_token = isset( $response_body['nextPageToken'] ) ? $response_body['nextPageToken'] : false;
// Add items from this page into the final result.
foreach ( $response_body['adUnits'] as $item ) {
$item = self::convert_ad_unit_format( $item );
$new_ad_units[ $item['id'] ] = $item;