|
1 | 1 | ;(function ( $, window, document ) {
|
2 | 2 | 'use strict';
|
3 | 3 |
|
4 |
| - // Check whether PayPal Payments is installed. |
5 |
| - let is_paypal_payments_installed = false, |
6 |
| - is_paypal_payments_active = false; |
| 4 | + // Plugin rows. |
| 5 | + let $notice_row = $( 'tr#ppec-migrate-notice' ); |
| 6 | + let $ppec_row = $notice_row.prev(); |
| 7 | + let $ppcp_row = $( 'tr[data-slug="woocommerce-paypal-payments"]' ); |
7 | 8 |
|
8 |
| - const targetElement = $( 'tr[data-slug="woocommerce-paypal-payments"]' ); |
9 |
| - if ( targetElement.length ) { |
10 |
| - is_paypal_payments_installed = true; |
| 9 | + $ppec_row.toggleClass( 'hide-border', true ); |
11 | 10 |
|
12 |
| - if ( targetElement.hasClass( 'active' ) ) { |
13 |
| - is_paypal_payments_active = true; |
14 |
| - } |
| 11 | + // Check whether PayPal Payments is installed. |
| 12 | + let is_paypal_payments_installed = $ppcp_row.length > 0; |
| 13 | + let is_paypal_payments_active = is_paypal_payments_installed && $ppcp_row.hasClass( 'active' ); |
15 | 14 |
|
| 15 | + let updateUI = function() { |
16 | 16 | // Dynamically update plugin activation link to handle plugin folder renames.
|
17 |
| - let activation_url = $( targetElement ).find( 'span.activate a' ).attr( 'href' ); |
18 |
| - $( 'a#ppec-activate-paypal-payments' ).attr( 'href', activation_url ); |
19 |
| - } |
20 |
| - |
21 |
| - // Hide notice/buttons conditionally. |
22 |
| - if ( is_paypal_payments_installed && is_paypal_payments_active ) { |
23 |
| - $( 'tr#ppec-migrate-notice' ).hide(); |
24 |
| - } else if ( is_paypal_payments_installed ) { |
25 |
| - $( 'a#ppec-install-paypal-payments' ).hide(); |
26 |
| - } else { |
27 |
| - $( 'a#ppec-activate-paypal-payments' ).hide(); |
28 |
| - } |
29 |
| - |
30 |
| - // Display buttons area |
31 |
| - $( '#ppec-migrate-notice .ppec-notice-buttons' ).removeClass( 'hidden' ); |
| 17 | + if ( is_paypal_payments_installed > 0 ) { |
| 18 | + $notice_row.find( 'a#ppec-activate-paypal-payments' ).attr( 'href', $ppcp_row.find( 'span.activate a' ).attr( 'href' ) ); |
| 19 | + } |
| 20 | + |
| 21 | + // Hide notice/buttons conditionally. |
| 22 | + $notice_row.find( 'a#ppec-install-paypal-payments' ).toggle( ! is_paypal_payments_installed ); |
| 23 | + $notice_row.find( 'a#ppec-activate-paypal-payments' ).toggle( is_paypal_payments_installed && ! is_paypal_payments_active ); |
| 24 | + |
| 25 | + // Display buttons area. |
| 26 | + $notice_row.find( '.ppec-notice-buttons' ).removeClass( 'hidden' ); |
| 27 | + }; |
32 | 28 |
|
33 | 29 | // Handle delete event for PayPal Payments.
|
34 | 30 | $( document ).on( 'wp-plugin-delete-success', function( event, response ) {
|
35 |
| - if ( is_paypal_payments_installed && 'woocommerce-paypal-payments' === response.slug ) { |
36 |
| - $( 'a#ppec-activate-paypal-payments' ).hide(); |
37 |
| - $( 'a#ppec-install-paypal-payments' ).show(); |
| 31 | + if ( 'woocommerce-paypal-payments' === response.slug ) { |
| 32 | + is_paypal_payments_installed = false; |
| 33 | + is_paypal_payments_active = false; |
| 34 | + updateUI(); |
38 | 35 | }
|
39 | 36 | } );
|
40 | 37 |
|
41 | 38 | // Change button text when install link is clicked.
|
42 |
| - $( document ).on( 'click', '#ppec-install-paypal-payments', function( e ) { |
| 39 | + $notice_row.find( '#ppec-install-paypal-payments' ).click( function( e ) { |
43 | 40 | e.preventDefault();
|
44 | 41 | $( this ).addClass( 'updating-message' ).text( 'Installing...' );
|
45 | 42 | const install_link = $( this ).attr('href');
|
46 | 43 | setTimeout( function(){
|
47 | 44 | window.location = install_link;
|
48 |
| - }, 100 ); |
49 |
| - }); |
| 45 | + }, 50 ); |
| 46 | + } ); |
| 47 | + |
| 48 | + // Dismiss button. |
| 49 | + $( document).on( 'click', '#ppec-migrate-notice button.notice-dismiss', function( e ) { |
| 50 | + $.ajax( |
| 51 | + { |
| 52 | + url: ajaxurl, |
| 53 | + method: 'POST', |
| 54 | + data: { |
| 55 | + action: 'ppec_dismiss_ppec_upgrade_notice', |
| 56 | + _ajax_nonce: $notice_row.attr( 'data-dismiss-nonce' ) |
| 57 | + }, |
| 58 | + dataType: 'json', |
| 59 | + success: function( res ) { |
| 60 | + $ppec_row.removeClass( 'hide-border' ); |
| 61 | + } |
| 62 | + } |
| 63 | + ); |
| 64 | + } ); |
| 65 | + |
| 66 | + updateUI(); |
| 67 | + |
50 | 68 | })( jQuery, window, document );
|
0 commit comments