: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* actually this method was moved inside the ad network class.
* however the responsive addon depends on it, so i made it global again.
* this makes it downward compatible (with an older version of responsive),
* but you should probably adjust the responsive plugin to make use of
* the static method (AdvancedAdsNetworkAdsense.gadsenseFormatAdContent)
* in case you come across a missing method originating from the deleted new-ad.js,
* please just make the methods static and create a wrapper function like the one below
window.gadsenseFormatAdContent = function () {
AdvancedAdsNetworkAdsense.gadsenseFormatAdContent();
class AdvancedAdsNetworkAdsense extends AdvancedAdsAdNetwork {
this.parseCodeBtnClicked = false;
this.preventCloseAdSelector = false;
// this.adUnitName = null;
// the legacy code of gadsense executes a script inside a php template and will may not have been executed
// at this stage. the AdvancedAdsAdNetwork class already knows the publisher id, so we will overwrite
// the field in gadsenseData to be up to date at all times.
// TODO: the use of gadsenseData.pubId could be removed from this class in favor of this.vars.pubId
gadsenseData.pubId = this.vars.pubId;
* Format the post content field
static gadsenseFormatAdContent() {
const slotId = jQuery( '#ad-parameters-box #unit-code' ).val();
const unitType = jQuery( '#ad-parameters-box #unit-type' ).val();
const publisherId = jQuery( '#advads-adsense-pub-id' ).val() ? jQuery( '#advads-adsense-pub-id' ).val() : gadsenseData.pubId;
if ( unitType === 'responsive' ) {
let resize = jQuery( '#ad-parameters-box #ad-resize-type' ).val();
adContent.resize = resize;
if ( unitType === 'in-feed' ) {
adContent.layout_key = jQuery( '#ad-parameters-box #ad-layout-key' ).val();
if ( typeof adContent.resize !== 'undefined' && adContent.resize !== 'auto' ) {
jQuery( document ).trigger( 'gadsenseFormatAdResponsive', [adContent] );
jQuery( document ).trigger( 'gadsenseFormatAdContent', [adContent] );
if ( typeof window.gadsenseAdContent !== 'undefined' ) {
adContent = window.gadsenseAdContent;
delete( window.gadsenseAdContent );
jQuery( '#advads-ad-content-adsense' ).val( JSON.stringify( adContent, false, 2 ) );
if ( this.preventCloseAdSelector ) {
AdvancedAdsAdmin.AdImporter.closeAdSelector();
const pubId = gadsenseData.pubId || false;
const slotId = jQuery( '#unit-code' ).val().trim();
return pubId && slotId ? 'ca-' + pubId + ':' + slotId : null;
selectAdFromList( slotId ) {
this.preventCloseAdSelector = true;
this.onSelectAd( slotId );
AdvancedAdsAdmin.AdImporter.openExternalAdsList();
const report = Advanced_Ads_Adsense_Report_Helper.getReportObject();
report.filter = slotId.split( ':' )[1];
updateAdFromList( slotId ) {
this.getRemoteCode( slotId );
getRefreshAdsParameters() {
nonce: AdsenseMAPI.nonce,
action: 'advanced_ads_get_ad_units_adsense',
account: gadsenseData.pubId,
inactive: ! this.hideIdle
jQuery( '.advads-adsense-code' ).css( 'display', 'none' );
jQuery( '#remote-ad-unsupported-ad-type' ).css( 'display', 'none' );
* Parse the code of an adsense ad and adjust the GUI
* call it, when an ad unit was selected.
* returns the parsed obj or false, when the ad code could not be parsed
const parsed = this.parseAdContentFailsafe( code );
this.setDetailsFromAdCode( parsed );
jQuery( '#remote-ad-code-error' ).hide();
jQuery( '#remote-ad-unsupported-ad-type' ).hide();
jQuery( '#remote-ad-code-error' ).show();
if ( typeof this.codes[slotID] !== 'undefined' ) {
this.getSavedDetails( slotID );
this.getRemoteCode( slotID );
getSavedDetails( slotID ) {
if ( typeof this.codes[slotID] !== 'undefined' ) {
const parsed = this.processAdCode( this.codes[slotID] );
if ( parsed !== false ) {
jQuery( '#remote-ad-unsupported-ad-type' ).css( 'display', 'none' );
this.preventCloseAdSelector = false;
getRemoteCode( slotID ) {
jQuery( '#mapi-loading-overlay' ).css( 'display', 'block' );
nonce: AdsenseMAPI.nonce,
action: 'advads_mapi_get_adCode',
success: function ( response ) {
jQuery( '#mapi-loading-overlay' ).css( 'display', 'none' );
if ( typeof response.code !== 'undefined' ) {
jQuery( '#remote-ad-code-msg' ).empty();
if ( self.processAdCode( response.code ) !== false ) {
self.codes[slotID] = response.code;
AdvancedAdsAdmin.AdImporter.unitIsSupported( slotID );
AdvancedAdsAdmin.AdImporter.highlightSelectedRowInExternalAdsList();
jQuery( '[data-slotid="' + slotID + '"]' ).children( '.unittype' ).text( response.type );
if ( typeof response.raw !== 'undefined' ) {
jQuery( '#remote-ad-code-msg' ).html( response.raw );
} else if ( typeof response.msg !== 'undefined' ) {
if ( typeof response.reload !== 'undefined' ) {
AdvancedAdsAdmin.AdImporter.emptyMapiSelector( response.msg );
if ( response.msg === 'doesNotSupportAdUnitType' ) {
AdvancedAdsAdmin.AdImporter.unitIsNotSupported( slotID );
jQuery( '#remote-ad-code-msg' ).html( response.msg );
if ( typeof response.raw !== 'undefined' ) {
console.log( response.raw );
jQuery( '#mapi-loading-overlay' ).css( 'display', 'none' );
parseAdContent( content ) {
const rawContent = typeof content !== 'undefined' ? content.trim() : '';
const theContent = jQuery( '<div />' ).html( rawContent );
const adByGoogle = theContent.find( 'ins' );
theAd.slotId = adByGoogle.attr( 'data-ad-slot' ) || '';
if ( typeof adByGoogle.attr( 'data-ad-client' ) !== 'undefined' ) {
theAd.pubId = adByGoogle.attr( 'data-ad-client' ).substr( 3 );
if ( theAd.slotId !== undefined && theAd.pubId !== '' ) {
theAd.display = adByGoogle.css( 'display' );
theAd.format = adByGoogle.attr( 'data-ad-format' );
theAd.layout = adByGoogle.attr( 'data-ad-layout' ); // for In-feed and In-article
theAd.layout_key = adByGoogle.attr( 'data-ad-layout-key' ); // for InFeed
theAd.style = adByGoogle.attr( 'style' ) || '';
if ( typeof theAd.format === 'undefined' && theAd.style.indexOf( 'width' ) !== - 1 ) {
theAd.width = adByGoogle.css( 'width' ).replace( 'px', '' );
theAd.height = adByGoogle.css( 'height' ).replace( 'px', '' );
} else if ( typeof theAd.format !== 'undefined' && theAd.format === 'auto' ) {
// Responsive ad, auto resize.
theAd.type = 'responsive';
} else if ( typeof theAd.format !== 'undefined' && theAd.format === 'link' ) {
// Older link unit format; for new ads the format type is no longer needed; link units are created through the AdSense panel
if ( theAd.style.indexOf( 'width' ) !== - 1 ) {
theAd.width = adByGoogle.css( 'width' ).replace( 'px', '' );
theAd.height = adByGoogle.css( 'height' ).replace( 'px', '' );
theAd.type = 'link-responsive';
} else if ( typeof theAd.format !== 'undefined' && theAd.format === 'autorelaxed' ) {
// Responsive Matched Content
theAd.type = 'matched-content';
} else if ( typeof theAd.format !== 'undefined' && theAd.format === 'fluid' ) {
// In-article & In-feed ads.
if ( typeof theAd.layout !== 'undefined' && theAd.layout === 'in-article' ) {
theAd.type = 'in-article';
if ( rawContent.indexOf( 'google_ad_slot' ) !== - 1 ) {
const adClient = rawContent.match( /google_ad_client ?= ?["']([^'"]+)/ );
const adSlot = rawContent.match( /google_ad_slot ?= ?["']([^'"]+)/ );
const adFormat = rawContent.match( /google_ad_format ?= ?["']([^'"]+)/ );
const adWidth = rawContent.match( /google_ad_width ?= ?([\d]+)/ );
const adHeight = rawContent.match( /google_ad_height ?= ?([\d]+)/ );
theAd.pubId = adClient[1].substr( 3 );
theAd.slotId = adSlot[1];
if ( adFormat !== null ) {
theAd.format = adFormat[1];
if ( adWidth !== null ) {
theAd.width = parseInt( adWidth[1] );
if ( adHeight !== null ) {
theAd.height = parseInt( adHeight[1] );
if ( typeof theAd.format === 'undefined' ) {
if ( theAd.slotId === '' && gadsenseData.pubId && gadsenseData.pubId !== '' ) {
theAd.type = jQuery( '#unit-type' ).val();
if ( rawContent.indexOf( 'enable_page_level_ads' ) !== - 1 || /script[^>]+data-ad-client=/.test( rawContent ) ) {
theAd = {'parse_message': 'pageLevelAd'};
} else if ( ! theAd.type ) {
theAd = {'parse_message': 'unknownAd'};
jQuery( document ).trigger( 'gadsenseParseAdContent', [theAd, adByGoogle] );
parseAdContentFailsafe( code ) {
if ( typeof code === 'string' ) {
code = JSON.parse( code );
return this.parseAdContent( code );
* Handle result of parsing content.
handleParseResult( parseResult ) {
jQuery( '#pastecode-msg' ).empty();
switch ( parseResult.parse_message ) {
advads_show_adsense_auto_ads_warning();
// Not recognized ad code.
if ( this.parseCodeBtnClicked && 'post-new.php' === gadsenseData.pagenow ) {
// do not show if just after switching to AdSense ad type on ad creation.
jQuery( '#pastecode-msg' ).append( jQuery( '<p />' ).css( 'color', 'red' ).html( gadsenseData.msg.unknownAd ) );
this.setDetailsFromAdCode( parseResult );
if ( typeof AdsenseMAPI !== 'undefined' && typeof AdsenseMAPI.hasToken !== 'undefined' && AdsenseMAPI.pubId === parseResult.pubId ) {
const content = jQuery( '#advanced-ads-ad-parameters input[name="advanced_ad[content]"]' ).val();
this.mapiSaveAdCode( content, parseResult.slotId );
jQuery( '.advads-adsense-code' ).hide();
jQuery( '.advads-adsense-show-code' ).show();
jQuery( '.mapi-insert-code' ).show();
const customInputs = this.getCustomInputs();
* Set ad parameters fields from the result of parsing ad code
setDetailsFromAdCode( theAd ) {
jQuery( '#unit-code' ).val( theAd.slotId );
jQuery( '#advads-adsense-pub-id' ).val( theAd.pubId );
if ( theAd.type === 'normal' ) {
jQuery( '#unit-type' ).val( 'normal' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( theAd.width );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( theAd.height );
} else if ( theAd.type === 'responsive' ) {
jQuery( '#unit-type' ).val( 'responsive' );
jQuery( '#ad-resize-type' ).val( 'auto' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' );
} else if ( theAd.type === 'link') {
jQuery( '#unit-type' ).val( 'link' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( theAd.width );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( theAd.height );
} else if ( theAd.type === 'link-responsive' ) {
jQuery( '#unit-type' ).val( 'link-responsive' );
jQuery( '#ad-resize-type' ).val( 'auto' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' );
} else if ( theAd.type === 'matched-content' ) {
jQuery( '#unit-type' ).val( 'matched-content' );
jQuery( '#ad-resize-type' ).val( 'auto' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' );
} else if ( theAd.type === 'in-article' ) {
jQuery( '#unit-type' ).val( 'in-article' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' );
} else if ( theAd.type === 'in-feed' ) {
jQuery( '#unit-type' ).val( 'in-feed' );
jQuery( '#ad-layout-key' ).val( theAd.layout_key );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' );
jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' );
const storedPubId = gadsenseData.pubId;
if ( storedPubId !== '' && theAd.pubId !== storedPubId && theAd.slotId !== '' ) {
jQuery( '#adsense-ad-param-error' ).text( gadsenseData.msg.pubIdMismatch );
jQuery( '#adsense-ad-param-error' ).empty();
jQuery( document ).trigger( 'this.setDetailsFromAdCode', [theAd] );
jQuery( '#unit-type' ).trigger( 'change' );
const type = $( '#unit-type' ).val();
const layout = $( '.advads-adsense-layout' );
const layoutKey = $( '.advads-adsense-layout-key' );
const size = $( '#advanced-ads-ad-parameters-size' );
const width = $( '[name="advanced_ad\[width\]"]' );
const height = $( '[name="advanced_ad\[height\]"]' );
layout.hide().next( 'div' ).hide();
layoutKey.hide().next( 'div' ).hide();
jQuery( '.advads-ad-notice-in-feed-add-on' ).hide();
if ( type === 'responsive' || type === 'link-responsive' || type === 'matched-content' ) {
size.hide().prev( '.label' ).hide();
size.next( '.hr' ).hide();
$( '.clearfix-before' ).show();
} else if ( type === 'in-feed' ) {
layout.hide().next( 'div' ).hide();
layoutKey.show().next( 'div' ).show();
size.hide().prev( '.label' ).hide();
size.next( '.hr' ).hide();
$( '.advads-ad-notice-in-feed-add-on' ).show();
$( '.clearfix-before' ).show();
} else if ( type === 'in-article' ) {
size.hide().prev( '.label' ).hide();
size.next( '.hr' ).hide();
$( '.clearfix-before' ).show();
} else if ( type === 'normal' || type === 'link' ) {
size.show().prev( '.label' ).show();
size.next( '.hr' ).show();
$( '.clearfix-before' ).hide();
$( document ).trigger( 'gadsenseUnitChanged' );
AdvancedAdsNetworkAdsense.gadsenseFormatAdContent();
this.show_float_warnings( type );
* Show / hide position warning.
show_float_warnings( unit_type ) {
const resize_type = jQuery( '#ad-resize-type' ).val();
const position = jQuery( 'input[name="advanced_ad[output][position]"]:checked' ).val();
( ['link-responsive', 'matched-content', 'in-article', 'in-feed'].indexOf( unit_type ) !== - 1
|| ( unit_type === 'responsive' && resize_type !== 'manual' )
&& ( position.startsWith( 'left' ) || position.startsWith( 'right' ) )
jQuery( '#ad-parameters-box-notices .advads-ad-notice-responsive-position' ).show();
jQuery( '#ad-parameters-box-notices .advads-ad-notice-responsive-position' ).hide();
* Legacy method - adds readonly to relevant inputs