/* eslint-disable */ jQuery(document).ready(function ($) { $(document).on('click', '#switch-to-adsense-type', function (ev) { ev.preventDefault(); AdvancedAdsAdmin.AdImporter.adsenseCode = Advanced_Ads_Admin.get_ad_source_editor_text(); $('#advanced-ad-type-adsense').trigger('click'); $(this).closest('li').addClass('hidden'); }); // activate general buttons $('.advads-buttonset').advads_buttonset(); // activate accordions if ($.fn.accordion) { $('.advads-accordion').accordion({ active: false, collapsible: true, }); } // AD OVERVIEW PAGE $('.advads-ad-list-tooltip').advads_tooltip({ content() { return jQuery(this).find('.advads-ad-list-tooltip-content').html(); }, }); // show edit icon in the last head column $('.post-type-advanced_ads .wp-list-table thead th:last-of-type') .append('') .on('click', function () { $('#show-settings-link').trigger('click'); }); /** * Logic for placement list */ (() => { let selectedValue = '0'; let searchTerm = ''; const placementRows = jQuery('.advads-placements-table tbody tr'); const showHidePlacementRow = (callback) => { placementRows.each((index, element) => { const $row = jQuery(element); const rowData = $row.data('order'); if ( typeof rowData === 'undefined' || typeof rowData.type === 'undefined' || typeof rowData.name === 'undefined' ) { $row.show(); return; } $row.toggle( (selectedValue === '0' || rowData.type === selectedValue) && (searchTerm === '' || rowData.name .toLowerCase() .indexOf(searchTerm.toLowerCase()) !== -1) ); }); }; // filter placement by type jQuery('.advads_filter_placement_type').on('change', function () { selectedValue = jQuery(this).val(); showHidePlacementRow(); }); // search placement by name jQuery('.advads_search_placement_name').on('keyup', function () { searchTerm = this.value; showHidePlacementRow(); }); })(); /** * Filter ad/ad group selection in new placement form. */ (() => { const placementTypeRadios = document.querySelectorAll( '[name="advads[placement][type]"]' ); placementTypeRadios.forEach((radio) => { radio.addEventListener('input', (event) => { jQuery('[name="advads[placement][item]"]').attr( 'disabled', true ); wp.ajax .post(window.advadstxt.placements_allowed_ads.action, { _ajax_nonce: window.advadstxt.placements_allowed_ads.nonce, placement_type: event.target.value, }) .done((response) => { jQuery('[name="advads[placement][item]"]').replaceWith( wp.template('advads-placement-ad-select')({ items: Object.values(response.items), }) ); }); }); }); })(); jQuery('.advads-delete-tag').each(function () { jQuery(this).on('click', function () { const r = confirm(window.advadstxt.delete_placement_confirmation); if (r === true) { const row = jQuery(this).parents('.advanced-ads-placement-row'); row.find('.advads-placements-item-delete').prop( 'checked', true ); row.data('touched', true); jQuery('#advanced-ads-placements-form').submit(); } }); }); // sort placement by type order or name jQuery('.advads-sort').on('click', function (e) { const sort = jQuery(this); const orderBy = sort.data('orderby'); const table = jQuery('.advads-placements-table'); const rows = jQuery('> tbody > tr', table); const links = jQuery('> thead th > a', table); links.each(function () { jQuery(this).removeClass('advads-placement-sorted'); }); sort.addClass('advads-placement-sorted'); rows.sort(function (a, b) { const orderA = jQuery(a).data('order'); const orderB = jQuery(b).data('order'); if (orderBy === 'type') { if ( orderA['words-between-repeats'] !== orderB['words-between-repeats'] ) { return orderA['words-between-repeats'] ? 1 : -1; } if (orderA.order === orderB.order) { // Sort by index. if ( orderA['post-content-index'] && orderB['post-content-index'] && orderA['post-content-index'] !== orderB['post-content-index'] ) { return orderA['post-content-index'] < orderB['post-content-index'] ? -1 : 1; } // Sort by name. return orderA.name.localeCompare(orderB.name, undefined, { numeric: true, }); } return orderA.order - orderB.order; } return orderA.name.localeCompare(orderB.name, undefined, { numeric: true, }); }); jQuery.each(rows, function (index, row) { table.append(row); }); let url = window.location.pathname + window.location.search; if (url.indexOf('orderby=') !== -1) { url = url.replace( /\borderby=[0-9a-zA-Z_@.#+-]{1,50}\b/, 'orderby=' + orderBy ); } else { url += '&orderby=' + orderBy; } window.history.replaceState({ orderby: orderBy }, document.title, url); e.preventDefault(); }); // show warning if Container ID option contains invalid characters $('#advads-output-wrapper-id').on('keyup', function () { const id_value = $(this).val(); if (/^[a-z-0-9]*$/.test(id_value)) { $('.advads-output-wrapper-id-error').addClass('hidden'); } else { $('.advads-output-wrapper-id-error').removeClass('hidden'); } }); // show more than 3 ads when clicked on a link $('.advads-group-ads-list-show-more').on('click', function () { $(this) .hide() .parent() .siblings('.advads-ad-group-list-ads') .children('div') .show(); }); /** * SETTINGS PAGE */ // automatically copy the first entered license key into all other empty fields $('.advads-settings-tab-main-form .advads-license-key').on( 'blur', function () { // get number of license fields const license_key = $(this).val(); if ('' === license_key) { return; } const license_fields = $( '.advads-settings-tab-main-form .advads-license-key' ); const license_fields_without_value = []; // count license fields without value license_fields.each(function (i, el) { if ('' === $(el).val()) { license_fields_without_value.push(el); } }); // if there is only one field filled then take its content (probably a license key) and add it into the other fields if ( license_fields.length === license_fields_without_value.length + 1 ) { $.each(license_fields_without_value, function (i, el) { $(el).val(license_key); }); } } ); // activate licenses $('.advads-license-activate').on('click', function () { const button = $(this); if (!this.dataset.addon) { return; } advads_disable_license_buttons(true); const query = { action: 'advads-activate-license', addon: this.dataset.addon, pluginname: this.dataset.pluginname, optionslug: this.dataset.optionslug, license: $(this).parents('td').find('.advads-license-key').val(), security: $('#advads-licenses-ajax-referrer').val(), }; // show loader $('').insertAfter(button); // send and close message $.post(ajaxurl, query, function (r) { // remove spinner $('span.spinner').remove(); const parent = button.parents('td'); if (r === '1') { const key = 'advanced-ads-licenses[' + query.addon + ']'; advadsTermination.setInitialValue( key, document.querySelector('[name="' + key + '"]') ); parent.find('.advads-license-activate-error').remove(); parent.find('.advads-license-deactivate').show(); button.fadeOut(); parent.find('.advads-license-activate-active').fadeIn(); parent.find('input').prop('readonly', 'readonly'); advads_disable_license_buttons(false); } else if (r === 'ex') { const input = parent.find('input.advads-license-key'); const link = parent.find('a.advads-renewal-link'); if (input && link) { const license_key = input.val(); const href = link.prop('href'); link.prop( 'href', href.replace('%LICENSE_KEY%', license_key) ); } parent.find('.advads-license-activate-error').remove(); parent.find('.advads-license-expired-error').show(); advads_disable_license_buttons(false); } else { parent.find('.advads-license-activate-error').show().html(r); advads_disable_license_buttons(false); } }); }); // deactivate licenses $('.advads-license-deactivate').on('click', function () { const button = $(this); if (!this.dataset.addon) { return; } advads_disable_license_buttons(true); const query = { action: 'advads-deactivate-license', addon: this.dataset.addon, pluginname: this.dataset.pluginname, optionslug: this.dataset.optionslug, security: $('#advads-licenses-ajax-referrer').val(), }; // show loader $('').insertAfter(button); // send and close message $.post(ajaxurl, query, function (r) { // remove spinner $('span.spinner').remove(); if (r === '1') { button.siblings('.advads-license-activate-error').hide(); button.siblings('.advads-license-activate-active').hide(); button.siblings('.advads-license-activate').show(); button.siblings('input').prop('readonly', false); button.fadeOut(); advads_disable_license_buttons(false); } else if (r === 'ex') { button.siblings('.advads-license-activate-error').hide(); button.siblings('.advads-license-activate-active').hide(); button.siblings('.advads-license-expired-error').show(); button.siblings('input').prop('readonly', false); button.fadeOut(); advads_disable_license_buttons(false); } else { console.log(r); button .siblings('.advads-license-activate-error') .show() .html(r); button.siblings('.advads-license-activate-active').hide(); advads_disable_license_buttons(false); } }); }); // toggle license buttons – disable or not function advads_disable_license_buttons(disable = true) { const buttons = $( 'button.advads-license-activate, button.advads-license-deactivate' ); // all activation buttons // disable all buttons to prevent issues when users try to enable multiple licenses at the same time if (disable) { buttons.attr('disabled', 'disabled'); } else { buttons.removeAttr('disabled'); } } /** * There are two formats of URL supported: * admin.php?page=advanced-ads-settings#top#tab_id go to the `tab_id` * admin.php?page=advanced-ads-settings#tab_id__anchor go to the `tab_id`, scroll to the `anchor` */ /** * Extract the active tab and anchor from the URL hash. * * @param hash * @member {string} hash The URL hash. * * @return {{tab: string, anchor: string}} */ function advads_extract_tab(hash) { const hash_parts = hash .replace(/^#top(#|%23)/, '') .replace(/(#|%23)/, '') .split('__'); return { tab: hash_parts[0] || jQuery('.advads-tab').attr('id'), anchor: hash_parts[1], }; } /** * Set the active tab and optionally scroll to the anchor. * @param tab */ function advads_set_tab(tab) { jQuery('#advads-tabs').find('a').removeClass('nav-tab-active'); jQuery('.advads-tab').removeClass('active'); jQuery('#' + tab.tab).addClass('active'); jQuery('#' + tab.tab + '-tab').addClass('nav-tab-active'); if (tab.anchor) { const anchor_offset = document .getElementById(tab.anchor) .getBoundingClientRect().top; const admin_bar = 48; window.scrollTo(0, anchor_offset + window.scrollY - admin_bar); } } // While user is already on the Settings page, find links (in admin menu, // in the Checks at the top, in the notices at the top) to particular setting tabs and open them on click. jQuery(document).on( 'click', 'a[href*="page=advanced-ads-settings"]:not(.nav-tab)', function () { // Already on the Settings page, so set the new tab. // Extract the tab id from the url. const url = jQuery(this) .attr('href') .split('advanced-ads-settings')[1]; const tab = advads_extract_tab(url); advads_set_tab(tab); } ); /** * Handle the hashchange event, this enables back/forward navigation in the settings page. */ window.addEventListener('hashchange', (event) => { const hash = advads_extract_tab(new URL(event.newURL).hash); try { document .getElementById(hash.tab + '-tab') .dispatchEvent(new Event('click')); } catch (e) { // fail silently if element does not exist. } }); // activate specific or first tab const active_tab = advads_extract_tab(window.location.hash); advads_set_tab(active_tab); // set all tab urls advads_set_tab_hashes(); // dynamically generate the sub-menu jQuery('.advads-tab-sub-menu').each(function (key, e) { // abort if scrollIntoView is not supported; we can’t use anchors because they are used for tabs already if (typeof e.scrollIntoView !== 'function') { return; } // get all h2 headlines advads_settings_parent_tab = jQuery(e).parent('.advads-tab'); const headlines = advads_settings_parent_tab.find('h2'); // create list if (headlines.length > 1) { advads_submenu_list = jQuery('