adblocker-image-data.js
1 year ago
admin-global.js
1 year ago
admin.js
2 days ago
advertisement.js
3 months ago
conditions.js
1 year ago
dialog-advads-modal.js
1 year ago
termination.js
1 year ago
ui.js
3 years ago
admin-global.js
446 lines
| 1 | /* eslint-disable no-unused-vars */ |
| 2 | /* eslint-disable camelcase */ |
| 3 | /* |
| 4 | * global js functions for Advanced Ads |
| 5 | */ |
| 6 | jQuery(document).ready(function () { |
| 7 | /** |
| 8 | * ADMIN NOTICES |
| 9 | */ |
| 10 | // close button |
| 11 | // .advads-notice-dismiss class can be used to add a custom close button (e.g., link) |
| 12 | jQuery(document).on( |
| 13 | 'click', |
| 14 | '.advads-admin-notice .notice-dismiss, .advads-notice-dismiss', |
| 15 | function (event) { |
| 16 | event.preventDefault(); |
| 17 | const messagebox = jQuery(this).parents('.advads-admin-notice'); |
| 18 | if (messagebox.attr('data-notice') === undefined) return; |
| 19 | |
| 20 | const query = { |
| 21 | action: 'advads-close-notice', |
| 22 | notice: messagebox.attr('data-notice'), |
| 23 | nonce: advadsglobal.ajax_nonce, |
| 24 | }; |
| 25 | // send query |
| 26 | jQuery.post(ajaxurl, query, function (r) { |
| 27 | messagebox.fadeOut(); |
| 28 | }); |
| 29 | } |
| 30 | ); |
| 31 | // hide notice for 7 days |
| 32 | jQuery(document).on( |
| 33 | 'click', |
| 34 | '.advads-admin-notice .advads-notice-hide', |
| 35 | function () { |
| 36 | const messagebox = jQuery(this).parents('.advads-admin-notice'); |
| 37 | if (messagebox.attr('data-notice') === undefined) return; |
| 38 | |
| 39 | const query = { |
| 40 | action: 'advads-hide-notice', |
| 41 | notice: messagebox.attr('data-notice'), |
| 42 | nonce: advadsglobal.ajax_nonce, |
| 43 | }; |
| 44 | // send query |
| 45 | jQuery.post(ajaxurl, query, function (r) { |
| 46 | messagebox.fadeOut(); |
| 47 | }); |
| 48 | } |
| 49 | ); |
| 50 | |
| 51 | // autoresponder button |
| 52 | jQuery('body').on('click', '.advads-notices-button-subscribe', function () { |
| 53 | if (this.dataset.notice === undefined) { |
| 54 | return; |
| 55 | } |
| 56 | const messageboxes = jQuery(this).parents('.advads-admin-notice'); |
| 57 | if (!messageboxes.length) { |
| 58 | return; |
| 59 | } |
| 60 | const $messagebox = jQuery(messageboxes[0]); |
| 61 | jQuery('<span class="spinner advads-spinner"></span>').insertAfter( |
| 62 | this |
| 63 | ); |
| 64 | |
| 65 | const query = { |
| 66 | action: 'advads-subscribe-notice', |
| 67 | notice: this.dataset.notice, |
| 68 | nonce: advadsglobal.ajax_nonce, |
| 69 | }; |
| 70 | // send and replace with server message |
| 71 | jQuery |
| 72 | .post(ajaxurl, query) |
| 73 | .success(function (response) { |
| 74 | $messagebox |
| 75 | .children('.advads-notice-box_wrapper') |
| 76 | .html('<p>' + response.data.message + '</p>'); |
| 77 | $messagebox.addClass('notice-success notice'); |
| 78 | }) |
| 79 | .fail(function (response) { |
| 80 | $messagebox |
| 81 | .children('.advads-notice-box_wrapper') |
| 82 | .html('<p>' + response.responseJSON.data.message + '</p>'); |
| 83 | $messagebox.addClass('notice-error notice'); |
| 84 | }) |
| 85 | .always(function () { |
| 86 | $messagebox.removeClass('notice-info'); |
| 87 | }); |
| 88 | }); |
| 89 | |
| 90 | /** |
| 91 | * Functions for Ad Health Notifications in the backend |
| 92 | */ |
| 93 | // hide button (adds item to "ignore" list) |
| 94 | jQuery(document).on('click', '.advads-ad-health-notice-hide', function () { |
| 95 | const notice = jQuery(this).parents('li'); |
| 96 | if (notice.attr('data-notice') === undefined) return; |
| 97 | // var list = notice.parent( 'ul' ); |
| 98 | const remove = jQuery(this).hasClass('remove'); |
| 99 | |
| 100 | // fix height to prevent the box from going smaller first, then show the "show" link and grow again |
| 101 | const notice_box = jQuery('#advads_overview_notices'); |
| 102 | notice_box.css('height', notice_box.height() + 'px'); |
| 103 | |
| 104 | const query = { |
| 105 | action: 'advads-ad-health-notice-hide', |
| 106 | notice: notice.attr('data-notice'), |
| 107 | nonce: advadsglobal.ajax_nonce, |
| 108 | }; |
| 109 | // fade out first or remove, so users can’t click twice |
| 110 | if (remove) { |
| 111 | notice.remove(); |
| 112 | } else { |
| 113 | notice.hide(); |
| 114 | } |
| 115 | // show loader |
| 116 | notice_box.find('.advads-loader').show(); |
| 117 | advads_ad_health_maybe_remove_list(); |
| 118 | // send query |
| 119 | jQuery.post(ajaxurl, query, function (r) { |
| 120 | // update number in menu |
| 121 | advads_ad_health_reload_number_in_menu(); |
| 122 | // update show button |
| 123 | advads_ad_health_reload_show_link(); |
| 124 | // remove the fixed height |
| 125 | jQuery('#advads_overview_notices').css('height', ''); |
| 126 | // remove loader |
| 127 | notice_box.find('.advads-loader').hide(); |
| 128 | }); |
| 129 | }); |
| 130 | // show all hidden notices |
| 131 | jQuery(document).on( |
| 132 | 'click', |
| 133 | '.advads-ad-health-notices-show-hidden', |
| 134 | function () { |
| 135 | advads_ad_health_show_hidden(); |
| 136 | } |
| 137 | ); |
| 138 | |
| 139 | /** |
| 140 | * DEACTIVATION FEEDBACK FORM |
| 141 | */ |
| 142 | // show overlay when clicked on "deactivate" |
| 143 | const advads_deactivate_link = jQuery( |
| 144 | '.wp-admin.plugins-php tr[data-slug="advanced-ads"] .row-actions .deactivate a' |
| 145 | ); |
| 146 | |
| 147 | const advads_deactivate_link_url = advads_deactivate_link.attr('href'); |
| 148 | advads_deactivate_link.on('click', function (e) { |
| 149 | e.preventDefault(); |
| 150 | // only show feedback form once per 30 days |
| 151 | const c_value = advads_admin_get_cookie( |
| 152 | 'advanced_ads_hide_deactivate_feedback' |
| 153 | ); |
| 154 | if (c_value === undefined) { |
| 155 | jQuery('#advanced-ads-feedback-overlay').show(); |
| 156 | } else { |
| 157 | // click on the link |
| 158 | window.location.href = advads_deactivate_link_url; |
| 159 | } |
| 160 | }); |
| 161 | // show text fields |
| 162 | jQuery('#advanced-ads-feedback-content input[type="radio"]').on( |
| 163 | 'click', |
| 164 | function () { |
| 165 | // show text field if there is one |
| 166 | jQuery(this) |
| 167 | .parents('li') |
| 168 | .next('li') |
| 169 | .children('input[type="text"], textarea') |
| 170 | .show(); |
| 171 | } |
| 172 | ); |
| 173 | // handle technical issue feedback in particular |
| 174 | jQuery('#advanced-ads-feedback-content .advanced_ads_disable_help_text').on( |
| 175 | 'focus', |
| 176 | function () { |
| 177 | // show text field if there is one |
| 178 | jQuery(this) |
| 179 | .parents('li') |
| 180 | .siblings('.advanced_ads_disable_reply') |
| 181 | .show(); |
| 182 | } |
| 183 | ); |
| 184 | // send form or close it |
| 185 | jQuery('#advanced-ads-feedback-content .button').on('click', function (e) { |
| 186 | e.preventDefault(); |
| 187 | const self = jQuery(this); |
| 188 | // set cookie for 30 days |
| 189 | advads_store_feedback_cookie(); |
| 190 | // save if plugin should be disabled |
| 191 | const disable_plugin = self.hasClass( |
| 192 | 'advanced-ads-feedback-not-deactivate' |
| 193 | ) |
| 194 | ? false |
| 195 | : true; |
| 196 | |
| 197 | // hide the content of the feedback form |
| 198 | jQuery('#advanced-ads-feedback-content form').hide(); |
| 199 | if (self.hasClass('advanced-ads-feedback-submit')) { |
| 200 | // show feedback message |
| 201 | jQuery('#advanced-ads-feedback-after-submit-waiting').show(); |
| 202 | if (disable_plugin) { |
| 203 | jQuery( |
| 204 | '#advanced-ads-feedback-after-submit-disabling-plugin' |
| 205 | ).show(); |
| 206 | } |
| 207 | jQuery.ajax({ |
| 208 | type: 'POST', |
| 209 | url: ajaxurl, |
| 210 | dataType: 'json', |
| 211 | data: { |
| 212 | action: 'advads_send_feedback', |
| 213 | feedback: self.hasClass( |
| 214 | 'advanced-ads-feedback-not-deactivate' |
| 215 | ) |
| 216 | ? true |
| 217 | : false, |
| 218 | formdata: jQuery( |
| 219 | '#advanced-ads-feedback-content form' |
| 220 | ).serialize(), |
| 221 | }, |
| 222 | complete(MLHttpRequest, textStatus, errorThrown) { |
| 223 | // deactivate the plugin and close the popup with a timeout |
| 224 | setTimeout(function () { |
| 225 | jQuery('#advanced-ads-feedback-overlay').remove(); |
| 226 | }, 2000); |
| 227 | if (disable_plugin) { |
| 228 | window.location.href = advads_deactivate_link_url; |
| 229 | } |
| 230 | }, |
| 231 | }); |
| 232 | } else { |
| 233 | // currently not reachable |
| 234 | jQuery('#advanced-ads-feedback-overlay').remove(); |
| 235 | window.location.href = advads_deactivate_link_url; |
| 236 | } |
| 237 | }); |
| 238 | // close form and disable the plugin without doing anything |
| 239 | jQuery('.advanced-ads-feedback-only-deactivate').on('click', function () { |
| 240 | // hide the content of the feedback form |
| 241 | jQuery('#advanced-ads-feedback-content form').hide(); |
| 242 | // show feedback message |
| 243 | jQuery('#advanced-ads-feedback-after-submit-goodbye').show(); |
| 244 | jQuery('#advanced-ads-feedback-after-submit-disabling-plugin').show(); |
| 245 | // set cookie for 30 days |
| 246 | advads_store_feedback_cookie(); |
| 247 | // wait one second |
| 248 | setTimeout(function () { |
| 249 | jQuery('#advanced-ads-feedback-overlay').hide(); |
| 250 | window.location.href = advads_deactivate_link_url; |
| 251 | }, 1000); |
| 252 | }); |
| 253 | // close button for feedback form |
| 254 | jQuery('#advanced-ads-feedback-overlay-close-button').on( |
| 255 | 'click', |
| 256 | function () { |
| 257 | jQuery('#advanced-ads-feedback-overlay').hide(); |
| 258 | } |
| 259 | ); |
| 260 | |
| 261 | jQuery('.advads-help').on('mouseenter', function (event) { |
| 262 | const tooltip = jQuery(event.target).children('.advads-tooltip')[0]; |
| 263 | if (typeof tooltip === 'undefined') { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | // reset inline styles before getting bounding client rect. |
| 268 | tooltip.style.position = ''; |
| 269 | tooltip.style.left = ''; |
| 270 | tooltip.style.top = ''; |
| 271 | |
| 272 | const topParentRect = document |
| 273 | .getElementById('wpbody') |
| 274 | .getBoundingClientRect(), |
| 275 | helpRect = event.target.getBoundingClientRect(), |
| 276 | offsets = { |
| 277 | left: Math.ceil(helpRect.left) + 13, |
| 278 | top: Math.ceil(helpRect.top) + 13, |
| 279 | }; |
| 280 | let tooltipRect = tooltip.getBoundingClientRect(); |
| 281 | |
| 282 | tooltip.style.position = 'fixed'; |
| 283 | tooltip.style.left = offsets.left + 'px'; |
| 284 | tooltip.style.top = offsets.top + 'px'; |
| 285 | |
| 286 | // check element is not overflowing to the right. |
| 287 | while (tooltipRect.right > topParentRect.right - 20) { |
| 288 | offsets.left -= 10; |
| 289 | tooltip.style.left = offsets.left + 'px'; |
| 290 | tooltipRect = tooltip.getBoundingClientRect(); |
| 291 | } |
| 292 | |
| 293 | // check element is not overflowing bottom of parent and is within viewport. |
| 294 | while (tooltipRect.bottom > Math.min(topParentRect.bottom, jQuery(window).height()) - 20) { |
| 295 | offsets.top -= 10; |
| 296 | tooltip.style.top = offsets.top + 'px'; |
| 297 | tooltipRect = tooltip.getBoundingClientRect(); |
| 298 | } |
| 299 | }); |
| 300 | }); |
| 301 | |
| 302 | // remove duplicate close buttons |
| 303 | jQuery(window).on('load', function () { |
| 304 | jQuery('a.notice-dismiss').next('button.notice-dismiss').remove(); |
| 305 | }); |
| 306 | |
| 307 | function advads_admin_get_cookie(name) { |
| 308 | let i, x, y; |
| 309 | const ADVcookies = document.cookie.split(';'); |
| 310 | for (i = 0; i < ADVcookies.length; i++) { |
| 311 | x = ADVcookies[i].substr(0, ADVcookies[i].indexOf('=')); |
| 312 | y = ADVcookies[i].substr(ADVcookies[i].indexOf('=') + 1); |
| 313 | x = x.replace(/^\s+|\s+$/g, ''); |
| 314 | if (x === name) { |
| 315 | return unescape(y); |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Store a cookie for 30 days |
| 322 | * The cookie prevents the feedback form from showing multiple times |
| 323 | */ |
| 324 | function advads_store_feedback_cookie() { |
| 325 | const exdate = new Date(); |
| 326 | exdate.setSeconds(exdate.getSeconds() + 2592000); |
| 327 | document.cookie = |
| 328 | 'advanced_ads_hide_deactivate_feedback=1; expires=' + |
| 329 | exdate.toUTCString() + |
| 330 | '; path=/'; |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Ad Health Notices in backend |
| 335 | */ |
| 336 | // display notices list (deprecated because we load it without AJAX now) |
| 337 | function advads_display_ad_health_notices() { |
| 338 | const query = { |
| 339 | action: 'advads-ad-health-notice-display', |
| 340 | nonce: advadsglobal.ajax_nonce, |
| 341 | }; |
| 342 | |
| 343 | const widget = jQuery('#advads_overview_notices .main'); |
| 344 | |
| 345 | // add loader icon to the widget |
| 346 | widget.html('<span class="advads-loader"></span>'); |
| 347 | // send query |
| 348 | jQuery.post(ajaxurl, query, function (r) { |
| 349 | widget.html(r); |
| 350 | |
| 351 | // update number in menu |
| 352 | advads_ad_health_reload_number_in_menu(); |
| 353 | // update list headlines |
| 354 | advads_ad_health_maybe_remove_list(); |
| 355 | |
| 356 | // remove widget, if return is empty |
| 357 | if (r === '') { |
| 358 | jQuery('#advads_overview_notices').remove(); |
| 359 | } |
| 360 | }); |
| 361 | } |
| 362 | // push a notice to the queue |
| 363 | function advads_push_notice(key, attr = '') { |
| 364 | const query = { |
| 365 | action: 'advads-ad-health-notice-push-adminui', |
| 366 | key, |
| 367 | attr, |
| 368 | nonce: advadsglobal.ajax_nonce, |
| 369 | }; |
| 370 | // send query |
| 371 | jQuery.post(ajaxurl, query, function (r) {}); |
| 372 | } |
| 373 | // show notices of a given type again |
| 374 | function advads_ad_health_show_hidden() { |
| 375 | const notice_box = jQuery('#advads__overview_notices'); |
| 376 | const query = { |
| 377 | action: 'advads-ad-health-notice-unignore', |
| 378 | nonce: advadsglobal.ajax_nonce, |
| 379 | }; |
| 380 | // show all hidden |
| 381 | jQuery(document) |
| 382 | .find('#advads_overview_notices .advads-ad-health-notices > li:hidden') |
| 383 | .show(); |
| 384 | // show loader |
| 385 | notice_box.find('.advads-loader').show(); |
| 386 | // update the button |
| 387 | advads_ad_health_reload_show_link(); |
| 388 | advads_ad_health_maybe_remove_list(); |
| 389 | // send query |
| 390 | jQuery.post(ajaxurl, query, function (r) { |
| 391 | // update issue count |
| 392 | advads_ad_health_reload_number_in_menu(); |
| 393 | // hide loader |
| 394 | notice_box.find('.advads-loader').hide(); |
| 395 | }); |
| 396 | } |
| 397 | // hide list fragments if last item was hidden/removed |
| 398 | function advads_ad_health_maybe_remove_list() { |
| 399 | // get all lists |
| 400 | const lists = jQuery(document).find( |
| 401 | '#advads_overview_notices .advads-ad-health-notices' |
| 402 | ); |
| 403 | |
| 404 | // check each list separately |
| 405 | lists.each(function (index) { |
| 406 | const list = jQuery(this); |
| 407 | // check if there are visible items in the list |
| 408 | if (list.find('li:visible').length) { |
| 409 | // show parent headline |
| 410 | list.prev('h3').show(); |
| 411 | } else { |
| 412 | // hide parent headline |
| 413 | list.prev('h3').hide(); |
| 414 | } |
| 415 | }); |
| 416 | } |
| 417 | // reload number of notices shown in the sidebar based on element in the problems list |
| 418 | function advads_ad_health_reload_number_in_menu() { |
| 419 | // get number of notices |
| 420 | const number = jQuery(document).find( |
| 421 | '#advads_overview_notices .advads-ad-health-notices > li:visible' |
| 422 | ).length; |
| 423 | jQuery('#toplevel_page_advanced-ads .update-count').html(number); |
| 424 | } |
| 425 | // update show X issues link – number and visibility |
| 426 | function advads_ad_health_reload_show_link() { |
| 427 | // get number of invisible elements |
| 428 | const number = jQuery(document).find( |
| 429 | '#advads_overview_notices .advads-ad-health-notices > li:hidden' |
| 430 | ).length; |
| 431 | const show_link = jQuery('.advads-ad-health-notices-show-hidden'); |
| 432 | // update number in the link |
| 433 | jQuery('.advads-ad-health-notices-show-hidden .count').html(number); |
| 434 | // hide of show, depending on number |
| 435 | if (0 === number) { |
| 436 | show_link.hide(); |
| 437 | } else { |
| 438 | show_link.show(); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | //Radio Toggle visibility |
| 443 | function toggle_visibility(currentElement, toggleElement) { |
| 444 | jQuery(toggleElement).toggle(jQuery(currentElement).val() === 'on'); |
| 445 | } |
| 446 |