adsense.js
929 lines
| 1 | /** |
| 2 | * actually this method was moved inside the ad network class. |
| 3 | * however the responsive addon depends on it, so i made it global again. |
| 4 | * this makes it downward compatible (with an older version of responsive), |
| 5 | * but you should probably adjust the responsive plugin to make use of |
| 6 | * the static method (AdvancedAdsNetworkAdsense.gadsenseFormatAdContent) |
| 7 | * |
| 8 | * in case you come across a missing method originating from the deleted new-ad.js, |
| 9 | * please just make the methods static and create a wrapper function like the one below |
| 10 | */ |
| 11 | window.gadsenseFormatAdContent = function () { |
| 12 | AdvancedAdsNetworkAdsense.gadsenseFormatAdContent(); |
| 13 | }; |
| 14 | |
| 15 | class AdvancedAdsNetworkAdsense extends AdvancedAdsAdNetwork { |
| 16 | constructor( codes ) { |
| 17 | super( 'adsense' ); |
| 18 | this.name = 'adsense'; |
| 19 | this.codes = codes; |
| 20 | this.parseCodeBtnClicked = false; |
| 21 | this.preventCloseAdSelector = false; |
| 22 | // this.adUnitName = null; |
| 23 | // the legacy code of gadsense executes a script inside a php template and will may not have been executed |
| 24 | // at this stage. the AdvancedAdsAdNetwork class already knows the publisher id, so we will overwrite |
| 25 | // the field in gadsenseData to be up to date at all times. |
| 26 | // TODO: the use of gadsenseData.pubId could be removed from this class in favor of this.vars.pubId |
| 27 | gadsenseData.pubId = this.vars.pubId; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Legacy method |
| 32 | * |
| 33 | * Format the post content field |
| 34 | */ |
| 35 | static gadsenseFormatAdContent() { |
| 36 | const slotId = jQuery( '#ad-parameters-box #unit-code' ).val(); |
| 37 | const unitType = jQuery( '#ad-parameters-box #unit-type' ).val(); |
| 38 | const publisherId = jQuery( '#advads-adsense-pub-id' ).val() ? jQuery( '#advads-adsense-pub-id' ).val() : gadsenseData.pubId; |
| 39 | let adContent = { |
| 40 | slotId: slotId, |
| 41 | unitType: unitType, |
| 42 | pubId: publisherId |
| 43 | }; |
| 44 | if ( unitType === 'responsive' ) { |
| 45 | let resize = jQuery( '#ad-parameters-box #ad-resize-type' ).val(); |
| 46 | if ( resize === '0' ) { |
| 47 | resize = 'auto'; |
| 48 | } |
| 49 | adContent.resize = resize; |
| 50 | } |
| 51 | if ( unitType === 'in-feed' ) { |
| 52 | adContent.layout_key = jQuery( '#ad-parameters-box #ad-layout-key' ).val(); |
| 53 | } |
| 54 | if ( typeof adContent.resize !== 'undefined' && adContent.resize !== 'auto' ) { |
| 55 | jQuery( document ).trigger( 'gadsenseFormatAdResponsive', [adContent] ); |
| 56 | } |
| 57 | jQuery( document ).trigger( 'gadsenseFormatAdContent', [adContent] ); |
| 58 | |
| 59 | if ( typeof window.gadsenseAdContent !== 'undefined' ) { |
| 60 | adContent = window.gadsenseAdContent; |
| 61 | delete( window.gadsenseAdContent ); |
| 62 | } |
| 63 | jQuery( '#advads-ad-content-adsense' ).val( JSON.stringify( adContent, false, 2 ) ); |
| 64 | |
| 65 | } |
| 66 | |
| 67 | openSelector() { |
| 68 | |
| 69 | } |
| 70 | |
| 71 | closeAdSelector() { |
| 72 | if ( this.preventCloseAdSelector ) { |
| 73 | return; |
| 74 | } |
| 75 | AdvancedAdsAdmin.AdImporter.closeAdSelector(); |
| 76 | } |
| 77 | |
| 78 | getSelectedId() { |
| 79 | const pubId = gadsenseData.pubId || false; |
| 80 | const slotId = jQuery( '#unit-code' ).val().trim(); |
| 81 | return pubId && slotId ? 'ca-' + pubId + ':' + slotId : null; |
| 82 | } |
| 83 | |
| 84 | selectAdFromList( slotId ) { |
| 85 | this.preventCloseAdSelector = true; |
| 86 | this.onSelectAd( slotId ); |
| 87 | AdvancedAdsAdmin.AdImporter.openExternalAdsList(); |
| 88 | |
| 89 | const report = Advanced_Ads_Adsense_Report_Helper.getReportObject(); |
| 90 | if ( report ) { |
| 91 | report.filter = slotId.split( ':' )[1]; |
| 92 | report.updateHtmlAttr(); |
| 93 | report.refresh(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | updateAdFromList( slotId ) { |
| 98 | this.getRemoteCode( slotId ); |
| 99 | } |
| 100 | |
| 101 | getRefreshAdsParameters() { |
| 102 | return { |
| 103 | nonce: AdsenseMAPI.nonce, |
| 104 | action: 'advanced_ads_get_ad_units_adsense', |
| 105 | account: gadsenseData.pubId, |
| 106 | inactive: ! this.hideIdle |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | onManualSetup() { |
| 111 | jQuery( '.advads-adsense-code' ).css( 'display', 'none' ); |
| 112 | jQuery( '#remote-ad-unsupported-ad-type' ).css( 'display', 'none' ); |
| 113 | this.undoReadOnly(); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Parse the code of an adsense ad and adjust the GUI |
| 118 | * call it, when an ad unit was selected. |
| 119 | * returns the parsed obj or false, when the ad code could not be parsed |
| 120 | */ |
| 121 | processAdCode( code ) { |
| 122 | const parsed = this.parseAdContentFailsafe( code ); |
| 123 | if ( parsed ) { |
| 124 | this.undoReadOnly(); |
| 125 | this.setDetailsFromAdCode( parsed ); |
| 126 | this.makeReadOnly(); |
| 127 | jQuery( '#remote-ad-code-error' ).hide(); |
| 128 | jQuery( '#remote-ad-unsupported-ad-type' ).hide(); |
| 129 | this.closeAdSelector(); |
| 130 | } else { |
| 131 | jQuery( '#remote-ad-code-error' ).show(); |
| 132 | } |
| 133 | return parsed; |
| 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Clone of legacy method |
| 138 | * |
| 139 | * @param slotID |
| 140 | */ |
| 141 | onSelectAd( slotID ) { |
| 142 | if ( typeof this.codes[slotID] !== 'undefined' ) { |
| 143 | this.getSavedDetails( slotID ); |
| 144 | } else { |
| 145 | this.getRemoteCode( slotID ); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Legacy method |
| 151 | * |
| 152 | * @param slotID |
| 153 | */ |
| 154 | getSavedDetails( slotID ) { |
| 155 | if ( typeof this.codes[slotID] !== 'undefined' ) { |
| 156 | const parsed = this.processAdCode( this.codes[slotID] ); |
| 157 | if ( parsed !== false ) { |
| 158 | jQuery( '#remote-ad-unsupported-ad-type' ).css( 'display', 'none' ); |
| 159 | this.closeAdSelector(); |
| 160 | this.preventCloseAdSelector = false; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Legacy method |
| 167 | * |
| 168 | * @param slotID |
| 169 | */ |
| 170 | getRemoteCode( slotID ) { |
| 171 | if ( slotID === '' ) { |
| 172 | return; |
| 173 | } |
| 174 | jQuery( '#mapi-loading-overlay' ).css( 'display', 'block' ); |
| 175 | const self = this; |
| 176 | jQuery.ajax( { |
| 177 | type: 'post', |
| 178 | url: ajaxurl, |
| 179 | data: { |
| 180 | nonce: AdsenseMAPI.nonce, |
| 181 | action: 'advads_mapi_get_adCode', |
| 182 | unit: slotID |
| 183 | }, |
| 184 | success: function ( response ) { |
| 185 | jQuery( '#mapi-loading-overlay' ).css( 'display', 'none' ); |
| 186 | if ( typeof response.code !== 'undefined' ) { |
| 187 | jQuery( '#remote-ad-code-msg' ).empty(); |
| 188 | if ( self.processAdCode( response.code ) !== false ) { |
| 189 | self.codes[slotID] = response.code; |
| 190 | AdvancedAdsAdmin.AdImporter.unitIsSupported( slotID ); |
| 191 | } |
| 192 | AdvancedAdsAdmin.AdImporter.highlightSelectedRowInExternalAdsList(); |
| 193 | jQuery( '[data-slotid="' + slotID + '"]' ).children( '.unittype' ).text( response.type ); |
| 194 | self.closeAdSelector(); |
| 195 | |
| 196 | } else { |
| 197 | if ( typeof response.raw !== 'undefined' ) { |
| 198 | jQuery( '#remote-ad-code-msg' ).html( response.raw ); |
| 199 | } else if ( typeof response.msg !== 'undefined' ) { |
| 200 | if ( typeof response.reload !== 'undefined' ) { |
| 201 | AdvancedAdsAdmin.AdImporter.emptyMapiSelector( response.msg ); |
| 202 | } else { |
| 203 | if ( response.msg === 'doesNotSupportAdUnitType' ) { |
| 204 | AdvancedAdsAdmin.AdImporter.unitIsNotSupported( slotID ); |
| 205 | } else { |
| 206 | jQuery( '#remote-ad-code-msg' ).html( response.msg ); |
| 207 | } |
| 208 | } |
| 209 | if ( typeof response.raw !== 'undefined' ) { |
| 210 | console.log( response.raw ); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | }, |
| 215 | error: function () { |
| 216 | jQuery( '#mapi-loading-overlay' ).css( 'display', 'none' ); |
| 217 | |
| 218 | } |
| 219 | } ); |
| 220 | |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Legacy method |
| 225 | * |
| 226 | * Parse ad content. |
| 227 | * |
| 228 | * @return {!Object} |
| 229 | */ |
| 230 | parseAdContent( content ) { |
| 231 | const rawContent = typeof content !== 'undefined' ? content.trim() : ''; |
| 232 | const theContent = jQuery( '<div />' ).html( rawContent ); |
| 233 | const adByGoogle = theContent.find( 'ins' ); |
| 234 | let theAd = {}; |
| 235 | theAd.slotId = adByGoogle.attr( 'data-ad-slot' ) || ''; |
| 236 | if ( typeof adByGoogle.attr( 'data-ad-client' ) !== 'undefined' ) { |
| 237 | theAd.pubId = adByGoogle.attr( 'data-ad-client' ).substr( 3 ); |
| 238 | } |
| 239 | |
| 240 | if ( theAd.slotId !== undefined && theAd.pubId !== '' ) { |
| 241 | theAd.display = adByGoogle.css( 'display' ); |
| 242 | theAd.format = adByGoogle.attr( 'data-ad-format' ); |
| 243 | theAd.layout = adByGoogle.attr( 'data-ad-layout' ); // for In-feed and In-article |
| 244 | theAd.layout_key = adByGoogle.attr( 'data-ad-layout-key' ); // for InFeed |
| 245 | theAd.style = adByGoogle.attr( 'style' ) || ''; |
| 246 | |
| 247 | // Normal ad. |
| 248 | if ( typeof theAd.format === 'undefined' && theAd.style.indexOf( 'width' ) !== - 1 ) { |
| 249 | theAd.type = 'normal'; |
| 250 | theAd.width = adByGoogle.css( 'width' ).replace( 'px', '' ); |
| 251 | theAd.height = adByGoogle.css( 'height' ).replace( 'px', '' ); |
| 252 | } else if ( typeof theAd.format !== 'undefined' && theAd.format === 'auto' ) { |
| 253 | // Responsive ad, auto resize. |
| 254 | theAd.type = 'responsive'; |
| 255 | } else if ( typeof theAd.format !== 'undefined' && theAd.format === 'link' ) { |
| 256 | // Older link unit format; for new ads the format type is no longer needed; link units are created through the AdSense panel |
| 257 | if ( theAd.style.indexOf( 'width' ) !== - 1 ) { |
| 258 | // Is fixed size. |
| 259 | theAd.width = adByGoogle.css( 'width' ).replace( 'px', '' ); |
| 260 | theAd.height = adByGoogle.css( 'height' ).replace( 'px', '' ); |
| 261 | theAd.type = 'link'; |
| 262 | } else { |
| 263 | // Is responsive. |
| 264 | theAd.type = 'link-responsive'; |
| 265 | } |
| 266 | } else if ( typeof theAd.format !== 'undefined' && theAd.format === 'autorelaxed' ) { |
| 267 | // Responsive Matched Content |
| 268 | theAd.type = 'matched-content'; |
| 269 | } else if ( typeof theAd.format !== 'undefined' && theAd.format === 'fluid' ) { |
| 270 | // In-article & In-feed ads. |
| 271 | if ( typeof theAd.layout !== 'undefined' && theAd.layout === 'in-article' ) { |
| 272 | // In-article. |
| 273 | theAd.type = 'in-article'; |
| 274 | } else { |
| 275 | // In-feed. |
| 276 | theAd.type = 'in-feed'; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Synchronous code |
| 283 | */ |
| 284 | if ( rawContent.indexOf( 'google_ad_slot' ) !== - 1 ) { |
| 285 | const adClient = rawContent.match( /google_ad_client ?= ?["']([^'"]+)/ ); |
| 286 | const adSlot = rawContent.match( /google_ad_slot ?= ?["']([^'"]+)/ ); |
| 287 | const adFormat = rawContent.match( /google_ad_format ?= ?["']([^'"]+)/ ); |
| 288 | const adWidth = rawContent.match( /google_ad_width ?= ?([\d]+)/ ); |
| 289 | const adHeight = rawContent.match( /google_ad_height ?= ?([\d]+)/ ); |
| 290 | |
| 291 | theAd = {}; |
| 292 | theAd.pubId = adClient[1].substr( 3 ); |
| 293 | |
| 294 | if ( adSlot !== null ) { |
| 295 | theAd.slotId = adSlot[1]; |
| 296 | } |
| 297 | if ( adFormat !== null ) { |
| 298 | theAd.format = adFormat[1]; |
| 299 | } |
| 300 | if ( adWidth !== null ) { |
| 301 | theAd.width = parseInt( adWidth[1] ); |
| 302 | } |
| 303 | if ( adHeight !== null ) { |
| 304 | theAd.height = parseInt( adHeight[1] ); |
| 305 | } |
| 306 | if ( typeof theAd.format === 'undefined' ) { |
| 307 | theAd.type = 'normal'; |
| 308 | } |
| 309 | |
| 310 | } |
| 311 | |
| 312 | if ( theAd.slotId === '' && gadsenseData.pubId && gadsenseData.pubId !== '' ) { |
| 313 | theAd.type = jQuery( '#unit-type' ).val(); |
| 314 | } |
| 315 | |
| 316 | // Page-Level ad. |
| 317 | if ( rawContent.indexOf( 'enable_page_level_ads' ) !== - 1 || /script[^>]+data-ad-client=/.test( rawContent ) ) { |
| 318 | theAd = {'parse_message': 'pageLevelAd'}; |
| 319 | } else if ( ! theAd.type ) { |
| 320 | // Unknown ad. |
| 321 | theAd = {'parse_message': 'unknownAd'}; |
| 322 | } |
| 323 | |
| 324 | jQuery( document ).trigger( 'gadsenseParseAdContent', [theAd, adByGoogle] ); |
| 325 | return theAd; |
| 326 | } |
| 327 | |
| 328 | parseAdContentFailsafe( code ) { |
| 329 | if ( typeof code === 'string' ) { |
| 330 | try { |
| 331 | code = JSON.parse( code ); |
| 332 | } catch ( e ) { |
| 333 | return this.parseAdContent( code ); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | return code; |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Handle result of parsing content. |
| 342 | * |
| 343 | * Legacy method |
| 344 | */ |
| 345 | handleParseResult( parseResult ) { |
| 346 | jQuery( '#pastecode-msg' ).empty(); |
| 347 | switch ( parseResult.parse_message ) { |
| 348 | case 'pageLevelAd' : |
| 349 | advads_show_adsense_auto_ads_warning(); |
| 350 | break; |
| 351 | case 'unknownAd' : |
| 352 | // Not recognized ad code. |
| 353 | if ( this.parseCodeBtnClicked && 'post-new.php' === gadsenseData.pagenow ) { |
| 354 | // do not show if just after switching to AdSense ad type on ad creation. |
| 355 | jQuery( '#pastecode-msg' ).append( jQuery( '<p />' ).css( 'color', 'red' ).html( gadsenseData.msg.unknownAd ) ); |
| 356 | } |
| 357 | break; |
| 358 | default: |
| 359 | this.setDetailsFromAdCode( parseResult ); |
| 360 | if ( typeof AdsenseMAPI !== 'undefined' && typeof AdsenseMAPI.hasToken !== 'undefined' && AdsenseMAPI.pubId === parseResult.pubId ) { |
| 361 | const content = jQuery( '#advanced-ads-ad-parameters input[name="advanced_ad[content]"]' ).val(); |
| 362 | this.mapiSaveAdCode( content, parseResult.slotId ); |
| 363 | this.makeReadOnly(); |
| 364 | } |
| 365 | jQuery( '.advads-adsense-code' ).hide(); |
| 366 | jQuery( '.advads-adsense-show-code' ).show(); |
| 367 | jQuery( '.mapi-insert-code' ).show(); |
| 368 | const customInputs = this.getCustomInputs(); |
| 369 | customInputs.show(); |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Legacy method |
| 375 | * |
| 376 | * Set ad parameters fields from the result of parsing ad code |
| 377 | */ |
| 378 | setDetailsFromAdCode( theAd ) { |
| 379 | this.undoReadOnly(); |
| 380 | jQuery( '#unit-code' ).val( theAd.slotId ); |
| 381 | jQuery( '#advads-adsense-pub-id' ).val( theAd.pubId ); |
| 382 | if ( theAd.type === 'normal' ) { |
| 383 | jQuery( '#unit-type' ).val( 'normal' ); |
| 384 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( theAd.width ); |
| 385 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( theAd.height ); |
| 386 | } else if ( theAd.type === 'responsive' ) { |
| 387 | jQuery( '#unit-type' ).val( 'responsive' ); |
| 388 | jQuery( '#ad-resize-type' ).val( 'auto' ); |
| 389 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' ); |
| 390 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' ); |
| 391 | } else if ( theAd.type === 'link') { |
| 392 | jQuery( '#unit-type' ).val( 'link' ); |
| 393 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( theAd.width ); |
| 394 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( theAd.height ); |
| 395 | } else if ( theAd.type === 'link-responsive' ) { |
| 396 | jQuery( '#unit-type' ).val( 'link-responsive' ); |
| 397 | jQuery( '#ad-resize-type' ).val( 'auto' ); |
| 398 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' ); |
| 399 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' ); |
| 400 | } else if ( theAd.type === 'matched-content' ) { |
| 401 | jQuery( '#unit-type' ).val( 'matched-content' ); |
| 402 | jQuery( '#ad-resize-type' ).val( 'auto' ); |
| 403 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' ); |
| 404 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' ); |
| 405 | } else if ( theAd.type === 'in-article' ) { |
| 406 | jQuery( '#unit-type' ).val( 'in-article' ); |
| 407 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' ); |
| 408 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' ); |
| 409 | } else if ( theAd.type === 'in-feed' ) { |
| 410 | jQuery( '#unit-type' ).val( 'in-feed' ); |
| 411 | jQuery( '#ad-layout-key' ).val( theAd.layout_key ); |
| 412 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val( '' ); |
| 413 | jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val( '' ); |
| 414 | } |
| 415 | const storedPubId = gadsenseData.pubId; |
| 416 | |
| 417 | if ( storedPubId !== '' && theAd.pubId !== storedPubId && theAd.slotId !== '' ) { |
| 418 | jQuery( '#adsense-ad-param-error' ).text( gadsenseData.msg.pubIdMismatch ); |
| 419 | } else { |
| 420 | jQuery( '#adsense-ad-param-error' ).empty(); |
| 421 | } |
| 422 | jQuery( document ).trigger( 'this.setDetailsFromAdCode', [theAd] ); |
| 423 | jQuery( '#unit-type' ).trigger( 'change' ); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Legacy method |
| 428 | */ |
| 429 | updateAdsenseType() { |
| 430 | const $ = jQuery; |
| 431 | const type = $( '#unit-type' ).val(); |
| 432 | const layout = $( '.advads-adsense-layout' ); |
| 433 | const layoutKey = $( '.advads-adsense-layout-key' ); |
| 434 | const size = $( '#advanced-ads-ad-parameters-size' ); |
| 435 | const width = $( '[name="advanced_ad\[width\]"]' ); |
| 436 | const height = $( '[name="advanced_ad\[height\]"]' ); |
| 437 | |
| 438 | layout.hide().next( 'div' ).hide(); |
| 439 | layoutKey.hide().next( 'div' ).hide(); |
| 440 | |
| 441 | jQuery( '.advads-ad-notice-in-feed-add-on' ).hide(); |
| 442 | if ( type === 'responsive' || type === 'link-responsive' || type === 'matched-content' ) { |
| 443 | size.hide().prev( '.label' ).hide(); |
| 444 | size.next( '.hr' ).hide(); |
| 445 | $( '.clearfix-before' ).show(); |
| 446 | } else if ( type === 'in-feed' ) { |
| 447 | layout.hide().next( 'div' ).hide(); |
| 448 | layoutKey.show().next( 'div' ).show(); |
| 449 | size.hide().prev( '.label' ).hide(); |
| 450 | size.next( '.hr' ).hide(); |
| 451 | |
| 452 | // show add-on notice |
| 453 | $( '.advads-ad-notice-in-feed-add-on' ).show(); |
| 454 | $( '.clearfix-before' ).show(); |
| 455 | } else if ( type === 'in-article' ) { |
| 456 | size.hide().prev( '.label' ).hide(); |
| 457 | size.next( '.hr' ).hide(); |
| 458 | $( '.clearfix-before' ).show(); |
| 459 | } else if ( type === 'normal' || type === 'link' ) { |
| 460 | size.show().prev( '.label' ).show(); |
| 461 | size.next( '.hr' ).show(); |
| 462 | $( '.clearfix-before' ).hide(); |
| 463 | |
| 464 | if ( ! width.val() ) { |
| 465 | width.val( '300' ); |
| 466 | } |
| 467 | if ( ! height.val() ) { |
| 468 | height.val( '250' ); |
| 469 | } |
| 470 | } |
| 471 | $( document ).trigger( 'gadsenseUnitChanged' ); |
| 472 | AdvancedAdsNetworkAdsense.gadsenseFormatAdContent(); |
| 473 | |
| 474 | this.show_float_warnings( type ); |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Legacy method |
| 479 | * |
| 480 | * Show / hide position warning. |
| 481 | */ |
| 482 | show_float_warnings( unit_type ) { |
| 483 | const resize_type = jQuery( '#ad-resize-type' ).val(); |
| 484 | const position = jQuery( 'input[name="advanced_ad[output][position]"]:checked' ).val(); |
| 485 | |
| 486 | if ( |
| 487 | ( ['link-responsive', 'matched-content', 'in-article', 'in-feed'].indexOf( unit_type ) !== - 1 |
| 488 | || ( unit_type === 'responsive' && resize_type !== 'manual' ) |
| 489 | ) |
| 490 | && ( position.startsWith( 'left' ) || position.startsWith( 'right' ) ) |
| 491 | ) { |
| 492 | jQuery( '#ad-parameters-box-notices .advads-ad-notice-responsive-position' ).show(); |
| 493 | } else { |
| 494 | jQuery( '#ad-parameters-box-notices .advads-ad-notice-responsive-position' ).hide(); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | /** |
| 499 | * Legacy method - adds readonly to relevant inputs |
| 500 | */ |
| 501 | makeReadOnly() { |
| 502 | jQuery( '#unit-type option:not(:selected)' ).prop( 'disabled', true ); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Legacy method - removes readonly from relevant inputs (original name getSlotAndType_jq) |
| 507 | */ |
| 508 | undoReadOnly() { |
| 509 | jQuery( '#unit-code,#ad-layout,#ad-layout-key,[name="advanced_ad[width]"],[name="advanced_ad[height]"]' ).prop( 'readonly', false ); |
| 510 | jQuery( '#unit-type option:not(:selected)' ).prop( 'disabled', false ); |
| 511 | } |
| 512 | |
| 513 | getCustomInputs() { |
| 514 | const $div1 = jQuery( '#unit-code' ).closest( 'div' ); |
| 515 | const $label1 = $div1.prev(); |
| 516 | const $hr1 = $div1.next(); |
| 517 | const $label2 = $hr1.next(); |
| 518 | const $div2 = $label2.next(); |
| 519 | const $layoutKey = jQuery( '#ad-layout-key' ).closest( 'div' ); |
| 520 | const $layoutKeyLabel = $layoutKey.prev( '#advads-adsense-layout-key' ); |
| 521 | |
| 522 | return $div1.add( $label1 ).add( $hr1 ).add( $label2 ).add( $div2 ).add( $layoutKey ).add( $layoutKeyLabel ); |
| 523 | } |
| 524 | |
| 525 | onBlur() { |
| 526 | |
| 527 | } |
| 528 | |
| 529 | onSelected() { |
| 530 | // Handle a click from the "Switch to AdSense ad" button. |
| 531 | if ( AdvancedAdsAdmin.AdImporter.adsenseCode ) { |
| 532 | this.parseCodeBtnClicked = true; |
| 533 | const parseResult = this.parseAdContent( AdvancedAdsAdmin.AdImporter.adsenseCode ); |
| 534 | AdvancedAdsAdmin.AdImporter.adsenseCode = null; |
| 535 | this.handleParseResult( parseResult ); |
| 536 | } else { |
| 537 | // When you are not connected to adsense, or if the ad was edited manually open the manual setup view. |
| 538 | let switchToManualSetup = ! this.vars.connected; |
| 539 | if ( ! switchToManualSetup ) { |
| 540 | const parsedAd = this.parseAdContentFailsafe( this.codes[this.getSelectedId()] ); |
| 541 | if ( parsedAd ) { |
| 542 | // We need to check if the type of the ad is different from the default. this marks a manually setup ad. |
| 543 | if ( parsedAd.type !== jQuery( '#unit-type' ).val() ) { |
| 544 | // This ad was manually setup. don't open the selector, but switch to manual select. |
| 545 | switchToManualSetup = true; |
| 546 | |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | if ( switchToManualSetup ) { |
| 551 | AdvancedAdsAdmin.AdImporter.manualSetup(); |
| 552 | } else if ( AdvancedAdsAdmin.AdImporter.highlightSelectedRowInExternalAdsList() || ! this.getSelectedId() ) { |
| 553 | AdvancedAdsAdmin.AdImporter.openExternalAdsList(); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | } |
| 558 | |
| 559 | onDomReady() { |
| 560 | const self = this; |
| 561 | jQuery( document ).on( 'click', '.advads-adsense-close-code', function ( ev ) { |
| 562 | ev.preventDefault(); |
| 563 | self.onSelected(); |
| 564 | } ); |
| 565 | |
| 566 | jQuery( document ).on( 'click', '.advads-adsense-submit-code', function ( ev ) { |
| 567 | ev.preventDefault(); |
| 568 | self.parseCodeBtnClicked = true; |
| 569 | const rawContent = jQuery( '.advads-adsense-content' ).val(); |
| 570 | const parseResult = self.parseAdContent( rawContent ); |
| 571 | self.handleParseResult( parseResult ); |
| 572 | if ( AdvancedAdsAdmin.AdImporter.highlightSelectedRowInExternalAdsList() ) { |
| 573 | AdvancedAdsAdmin.AdImporter.openExternalAdsList(); |
| 574 | self.preventCloseAdSelector = true; |
| 575 | |
| 576 | // save the manually added ad code to the AdSense settings |
| 577 | wp.ajax.post( 'advads-mapi-save-manual-code', { |
| 578 | raw_code: encodeURIComponent( rawContent ), |
| 579 | parsed_code: parseResult, |
| 580 | nonce: AdsenseMAPI.nonce |
| 581 | } ) |
| 582 | .fail( function ( r ) { |
| 583 | const $notice = jQuery( '<div>' ).addClass( 'notice notice-error' ).html( jQuery( '<p>' ).text( r.responseJSON.data.message ) ); |
| 584 | jQuery( '#post' ).before( $notice ); |
| 585 | jQuery( 'body html' ).animate( |
| 586 | { |
| 587 | scrollTop: $notice.offset().top |
| 588 | }, |
| 589 | 200 |
| 590 | ); |
| 591 | } ); |
| 592 | } else { |
| 593 | // No adsense ad with this slot id was found. |
| 594 | // Switches to manual ad setup view. |
| 595 | self.preventCloseAdSelector = false; |
| 596 | AdvancedAdsAdmin.AdImporter.manualSetup(); |
| 597 | } |
| 598 | } ); |
| 599 | |
| 600 | jQuery( document ).on( 'gadsenseUnitChanged', function () { |
| 601 | const $row = jQuery( 'tr[data-slotid$="' + jQuery( '#unit-code' ).val() + '"]' ); |
| 602 | let type = window.adsenseAdvancedAdsJS.ad_types.display; |
| 603 | |
| 604 | switch ( jQuery( '#unit-type' ).val() ) { |
| 605 | case 'matched-content': |
| 606 | type = window.adsenseAdvancedAdsJS.ad_types.matched_content; |
| 607 | break; |
| 608 | case 'link': |
| 609 | case 'link-responsive': |
| 610 | type = window.adsenseAdvancedAdsJS.ad_types.link; |
| 611 | break; |
| 612 | case 'in-article': |
| 613 | type = window.adsenseAdvancedAdsJS.ad_types.in_article; |
| 614 | break; |
| 615 | case 'in-feed': |
| 616 | type = window.adsenseAdvancedAdsJS.ad_types.in_feed; |
| 617 | break; |
| 618 | } |
| 619 | |
| 620 | $row.children( '.unittype' ).text( type ); |
| 621 | } ); |
| 622 | |
| 623 | jQuery( document ).on( 'change', '#unit-type, #unit-code, #ad-layout-key', function () { |
| 624 | self.checkAdSlotId( this ); |
| 625 | } ); |
| 626 | |
| 627 | const inputCode = jQuery( '#unit-code' ); |
| 628 | if ( inputCode ) { |
| 629 | this.checkAdSlotId( inputCode[0] ); |
| 630 | } |
| 631 | |
| 632 | jQuery( document ).on( 'change', '#ad-resize-type', function () { |
| 633 | self.show_float_warnings( 'responsive' ); |
| 634 | } ); |
| 635 | this.updateAdsenseType(); |
| 636 | |
| 637 | if ( typeof AdsenseMAPI.hasToken !== 'undefined' ) { |
| 638 | this.mapiMayBeSaveAdCode(); |
| 639 | } |
| 640 | |
| 641 | jQuery( document ).on( 'click', '#mapi-archived-ads', function () { |
| 642 | self.showArchivedAds( jQuery( this ).hasClass( 'dashicons-visibility' ) ); |
| 643 | } ); |
| 644 | |
| 645 | jQuery( '#wpwrap' ).on( |
| 646 | 'advads-mapi-adlist-opened', |
| 647 | function () { |
| 648 | // Update ad unit list to v2 data the first time the ad list is opened. |
| 649 | if ( jQuery( '#mapi-force-v2-list-update' ).length ) { |
| 650 | jQuery( '#mapi-wrap i[data-mapiaction="updateList"]' ).trigger( 'click' ); |
| 651 | return; |
| 652 | } |
| 653 | self.showArchivedAds(); |
| 654 | } |
| 655 | ); |
| 656 | |
| 657 | this.onSelected(); |
| 658 | } |
| 659 | |
| 660 | showArchivedAds( show ) { |
| 661 | if ( typeof show === 'undefined' ) { |
| 662 | show = false; |
| 663 | } |
| 664 | const icon = jQuery( '#mapi-archived-ads' ); |
| 665 | const title = icon.attr( 'title' ); |
| 666 | const altTitle = icon.attr( 'data-alt-title' ); |
| 667 | if ( show ) { |
| 668 | jQuery( '#mapi-table-wrap tbody tr[data-archived="1"]' ).show(); |
| 669 | icon.removeClass( 'dashicons-visibility' ).addClass( 'dashicons-hidden' ).attr( 'title', altTitle ).attr( 'data-alt-title', title ); |
| 670 | } else { |
| 671 | jQuery( '#mapi-table-wrap tbody tr[data-archived="1"]' ).not( '.selected' ).hide(); |
| 672 | icon.removeClass( 'dashicons-hidden' ).addClass( 'dashicons-visibility' ).attr( 'title', altTitle ).attr( 'data-alt-title', title ); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | checkAdSlotId( elm ) { |
| 677 | if ( jQuery( elm ).attr( 'id' ) === 'unit-code' ) { |
| 678 | let val = jQuery( elm ).val(); |
| 679 | if ( val ) { |
| 680 | val = val.trim(); |
| 681 | } |
| 682 | if ( val.length > 0 && gadsenseData.pubId && val.indexOf( gadsenseData.pubId.substr( 4 ) ) !== -1 ) { |
| 683 | jQuery( '#advads-pubid-in-slot' ).css( 'display', 'block' ); |
| 684 | jQuery( elm ).css( 'background-color', 'rgba(255, 235, 59, 0.33)' ); |
| 685 | this.updateAdsenseType(); |
| 686 | return; |
| 687 | } |
| 688 | } |
| 689 | jQuery( '#unit-code' ).css( 'background-color', '#fff' ); |
| 690 | jQuery( '#advads-pubid-in-slot' ).css( 'display', 'none' ); |
| 691 | this.updateAdsenseType(); |
| 692 | } |
| 693 | |
| 694 | mapiSaveAdCode( code, slot ) { |
| 695 | if ( typeof AdsenseMAPI.hasToken !== 'undefined' && typeof this.codes['ca-' + AdsenseMAPI.pubId + ':' + slot] === 'undefined' ) { |
| 696 | this.codes['ca-' + AdsenseMAPI.pubId + ':' + slot] = code; |
| 697 | jQuery( '#mapi-loading-overlay' ).css( 'display', 'block' ); |
| 698 | jQuery.ajax( { |
| 699 | type: 'post', |
| 700 | url: ajaxurl, |
| 701 | data: { |
| 702 | nonce: AdsenseMAPI.nonce, |
| 703 | slot: slot, |
| 704 | code: code, |
| 705 | action: 'advads-mapi-reconstructed-code' |
| 706 | }, |
| 707 | success: function ( resp, status, XHR ) { |
| 708 | jQuery( '#mapi-loading-overlay' ).css( 'display', 'none' ); |
| 709 | }, |
| 710 | error: function ( req, status, err ) { |
| 711 | jQuery( '#mapi-loading-overlay' ).css( 'display', 'none' ); |
| 712 | } |
| 713 | } ); |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | mapiMayBeSaveAdCode() { |
| 718 | // MAPI not set up |
| 719 | if ( typeof AdsenseMAPI.hasToken === 'undefined' ) { |
| 720 | return; |
| 721 | } |
| 722 | const slotId = jQuery( '#unit-code' ).val(); |
| 723 | if ( ! slotId ) { |
| 724 | return; |
| 725 | } |
| 726 | |
| 727 | const type = jQuery( '#unit-type' ).val(); |
| 728 | const width = jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[width]"]' ).val().trim(); |
| 729 | const height = jQuery( '#advanced-ads-ad-parameters-size input[name="advanced_ad[height]"]' ).val().trim(); |
| 730 | const layout = jQuery( '#ad-layout' ).val(); |
| 731 | const layoutKey = jQuery( '#ad-layout-key' ).val(); |
| 732 | |
| 733 | let code = false; |
| 734 | |
| 735 | switch ( type ) { |
| 736 | case 'in-feed': |
| 737 | code = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>' + |
| 738 | '<ins class="adsbygoogle" ' + |
| 739 | 'style="display:block;" ' + |
| 740 | 'data-ad-client="ca-' + AdsenseMAPI.pubId + '" ' + |
| 741 | 'data-ad-slot="' + slotId + '" ' + |
| 742 | 'data-ad-layout-key="' + layoutKey + '" '; |
| 743 | code += 'data-ad-format="fluid"></ins>' + |
| 744 | '<script>' + |
| 745 | '(adsbygoogle = window.adsbygoogle || []).push({});' + |
| 746 | '</script>'; |
| 747 | break; |
| 748 | case 'in-article': |
| 749 | code = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>' + |
| 750 | '<ins class="adsbygoogle" ' + |
| 751 | 'style="display:block;text-align:center;" ' + |
| 752 | 'data-ad-client="ca-' + AdsenseMAPI.pubId + '" ' + |
| 753 | 'data-ad-slot="' + slotId + '" ' + |
| 754 | 'data-ad-layout="in-article" ' + |
| 755 | 'data-ad-format="fluid"></ins>' + |
| 756 | '<script>' + |
| 757 | '(adsbygoogle = window.adsbygoogle || []).push({});' + |
| 758 | '</script>'; |
| 759 | break; |
| 760 | case 'matched-content': |
| 761 | code = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>' + |
| 762 | '<ins class="adsbygoogle" ' + |
| 763 | 'style="display:block;" ' + |
| 764 | 'data-ad-client="ca-' + AdsenseMAPI.pubId + '" ' + |
| 765 | 'data-ad-slot="' + slotId + '" ' + |
| 766 | 'data-ad-format="autorelaxed"></ins>' + |
| 767 | '<script>' + |
| 768 | '(adsbygoogle = window.adsbygoogle || []).push({});' + |
| 769 | '</script>'; |
| 770 | break; |
| 771 | case 'link-responsive': |
| 772 | code = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>' + |
| 773 | '<ins class="adsbygoogle" ' + |
| 774 | 'style="display:block;" ' + |
| 775 | 'data-ad-client="ca-' + AdsenseMAPI.pubId + '" ' + |
| 776 | 'data-ad-slot="' + slotId + '" ' + |
| 777 | 'data-ad-format="link"></ins>' + |
| 778 | '<script>' + |
| 779 | '(adsbygoogle = window.adsbygoogle || []).push({});' + |
| 780 | '</script>'; |
| 781 | break; |
| 782 | case 'link': |
| 783 | code = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>' + |
| 784 | '<ins class="adsbygoogle" ' + |
| 785 | 'style="display:block;width:' + width + 'px;height:' + height + 'px" ' + |
| 786 | 'data-ad-client="ca-' + AdsenseMAPI.pubId + '" ' + |
| 787 | 'data-ad-slot="' + slotId + '" ' + |
| 788 | 'data-ad-format="link"></ins>' + |
| 789 | '<script>' + |
| 790 | '(adsbygoogle = window.adsbygoogle || []).push({});' + |
| 791 | '</script>'; |
| 792 | break; |
| 793 | case 'responsive': |
| 794 | code = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>' + |
| 795 | '<ins class="adsbygoogle" ' + |
| 796 | 'style="display:block;" ' + |
| 797 | 'data-ad-client="ca-' + AdsenseMAPI.pubId + '" ' + |
| 798 | 'data-ad-slot="' + slotId + '" ' + |
| 799 | 'data-ad-format="auto"></ins>' + |
| 800 | '<script>' + |
| 801 | '(adsbygoogle = window.adsbygoogle || []).push({});' + |
| 802 | '</script>'; |
| 803 | break; |
| 804 | case 'normal': |
| 805 | code = '<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>' + |
| 806 | '<ins class="adsbygoogle" ' + |
| 807 | 'style="display:inline-block;width:' + width + 'px;height:' + height + 'px" ' + |
| 808 | 'data-ad-client="ca-' + AdsenseMAPI.pubId + '" ' + |
| 809 | 'data-ad-slot="' + slotId + '"></ins>' + |
| 810 | '<script>' + |
| 811 | '(adsbygoogle = window.adsbygoogle || []).push({});' + |
| 812 | '</script>'; |
| 813 | break; |
| 814 | default: |
| 815 | } |
| 816 | |
| 817 | if ( code ) { |
| 818 | this.mapiSaveAdCode( code, slotId ); |
| 819 | } |
| 820 | |
| 821 | } |
| 822 | |
| 823 | getMapiAction( action ) { |
| 824 | const self = this; |
| 825 | if ( action === 'toggleidle' ) { |
| 826 | return function ( ev, el ) { |
| 827 | self.hideIdle = ! self.hideIdle; |
| 828 | AdvancedAdsAdmin.AdImporter.refreshAds(); |
| 829 | }; |
| 830 | } |
| 831 | return null; |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | // Creates a Advanced_Ads_Adsense_Report_UI instance on the fly |
| 836 | window.Advanced_Ads_Adsense_Report_Helper = window.Advanced_Ads_Adsense_Report_Helper || {}; |
| 837 | window.Advanced_Ads_Adsense_Report_Helper.init = function ( element ) { |
| 838 | if ( jQuery( element ).attr( 'data-arguments' ) ) { |
| 839 | try { |
| 840 | const reportArgs = JSON.parse( jQuery( element ).attr( 'data-arguments' ) ); |
| 841 | jQuery( element ).data( 'advadsAdsenseReport', new Advanced_Ads_Adsense_Report_UI( element, reportArgs ) ); |
| 842 | } catch ( ex ) { |
| 843 | console.error( 'Cannot parse report arguments' ); |
| 844 | } |
| 845 | } |
| 846 | }; |
| 847 | |
| 848 | window.Advanced_Ads_Adsense_Report_Helper.getReportObject = function () { |
| 849 | const reportElem = jQuery( '.advanced-ads-adsense-dashboard' ); |
| 850 | if ( reportElem.length ) { |
| 851 | let report = reportElem.data( 'advadsAdsenseReport' ); |
| 852 | if ( typeof report.refresh === 'function' ) { |
| 853 | return report; |
| 854 | } |
| 855 | } |
| 856 | return false; |
| 857 | }; |
| 858 | |
| 859 | class Advanced_Ads_Adsense_Report_UI { |
| 860 | constructor( el, args ) { |
| 861 | this.$el = jQuery( el ); |
| 862 | this.type = args.type; |
| 863 | this.filter = args.filter; |
| 864 | this.init(); |
| 865 | this.refreshing = false; |
| 866 | } |
| 867 | |
| 868 | // Update arguments attributes before refreshing. |
| 869 | updateHtmlAttr() { |
| 870 | this.$el.attr( 'data-arguments', JSON.stringify( {type: 'domain', filter: self.filter} ) ); |
| 871 | } |
| 872 | |
| 873 | // Get markup for the current arguments. |
| 874 | refresh() { |
| 875 | const self = this; |
| 876 | this.$el.html( '<p style="text-align:center;"><span class="report-need-refresh spinner advads-ad-parameters-spinner advads-spinner"></span></p>' ); |
| 877 | jQuery.ajax( { |
| 878 | type: 'POST', |
| 879 | url: ajaxurl, |
| 880 | data: { |
| 881 | nonce: window.Advanced_Ads_Adsense_Report_Helper.nonce, |
| 882 | type: this.type, |
| 883 | filter: this.filter, |
| 884 | action: 'advads_adsense_report_refresh' |
| 885 | }, |
| 886 | success: function ( response ) { |
| 887 | if ( response.success && response.data && response.data.html ) { |
| 888 | self.$el.html( response.data.html ); |
| 889 | } |
| 890 | }, error: function ( request, status, error ) { |
| 891 | console.log( 'Refreshing rerpot error: ' + error ); |
| 892 | } |
| 893 | } ); |
| 894 | } |
| 895 | |
| 896 | // Initialization - events binding. |
| 897 | init() { |
| 898 | if ( this.$el.find( '.report-need-refresh' ).length ) { |
| 899 | this.refresh(); |
| 900 | } |
| 901 | const self = this; |
| 902 | |
| 903 | // Hide dropdown on click on everything but the dropdown and its children. |
| 904 | jQuery( document ).on( 'click', '#wpwrap', function () { |
| 905 | const dd = jQuery( '#advads_overview_adsense_stats .advads-stats-dd-button .advads-stats-dd-items' ); |
| 906 | if ( dd.is( ':visible' ) ) { |
| 907 | dd.hide(); |
| 908 | } |
| 909 | } ); |
| 910 | |
| 911 | // Show the dropdown. |
| 912 | jQuery( document ).on( 'click', '#advads_overview_adsense_stats .advads-stats-dd-button', function ( ev ) { |
| 913 | // Stop bubbling. Prevents hiding the dropdown. |
| 914 | ev.stopPropagation(); |
| 915 | const dd = jQuery( this ).find( '.advads-stats-dd-items' ); |
| 916 | if ( ! dd.is( ':visible' ) ) { |
| 917 | dd.show(); |
| 918 | } |
| 919 | } ); |
| 920 | |
| 921 | // Dropdown item clicked. |
| 922 | jQuery( document ).on( 'click', '.advads-stats-dd-button .advads-stats-dd-item', function () { |
| 923 | self.filter = jQuery( this ).attr( 'data-domain' ); |
| 924 | self.updateHtmlAttr(); |
| 925 | self.refresh(); |
| 926 | } ); |
| 927 | } |
| 928 | } |
| 929 |