diff --git a/README.md b/README.md index 8688926..7d6169c 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,8 @@ Também será necessário utilizar o plugin [WooCommerce Extra Checkout Fields f Pronto, sua loja já pode receber pagamentos pelo Pagar.me. +Mais informações sobre as configurações do plugin em: [https://docs.pagar.me/v2/docs/configurando-o-plugin-pagarme-woocommerce](https://docs.pagar.me/v2/docs/configurando-o-plugin-pagarme-woocommerce). + ## Frequently Asked Questions ## ### Qual é a licença do plugin? ### diff --git a/includes/admin/views/html-notice-missing-brazilian-market.php b/includes/admin/views/html-notice-missing-brazilian-market.php index 1d31253..2e9cb48 100644 --- a/includes/admin/views/html-notice-missing-brazilian-market.php +++ b/includes/admin/views/html-notice-missing-brazilian-market.php @@ -18,8 +18,8 @@ ?> -
+ diff --git a/includes/admin/views/html-notice-pagarme-documentation-link.php b/includes/admin/views/html-notice-pagarme-documentation-link.php new file mode 100644 index 0000000..042f445 --- /dev/null +++ b/includes/admin/views/html-notice-pagarme-documentation-link.php @@ -0,0 +1,16 @@ + + + diff --git a/tests/e2e/dismissable-notices.spec.js b/tests/e2e/dismissable-notices.spec.js new file mode 100644 index 0000000..ca689ba --- /dev/null +++ b/tests/e2e/dismissable-notices.spec.js @@ -0,0 +1,141 @@ +context('Wordpress admin notices', () => { + describe('when go to the wordpress plugins page', () => { + describe('and deactivate the woocommerce plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.deactivateWoocommercePlugin() + }) + + it('should contain error message', () => { + cy.get('.error a.button') + .contains('WooCommerce') + .should('have.attr', 'href') + .and('match', /action\=activate\&plugin\=woocommerce/) + }) + + describe('and when reactivate the woocommerce plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.activateWoocommercePlugin() + }) + + it('should not contain the message anymore', () => { + cy.get('.error a[href*="action=activate&plugin=woocommerce"]') + .should('not.exist') + }) + }) + }) + + describe('and deactivate the woocommerce-pagarme plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.deactivateWoocommercePagarmePlugin() + }) + + describe('and reactivate the woocommerce-pagarme plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.activateWoocommercePagarmePlugin() + }) + + it('should contain a message with the Pagar.me documentation link button', () => { + cy.get('.updated a.button-primary') + .should('have.attr', 'href') + .and('match', /docs.pagar.me/) + }) + }) + + describe('and when click to close the message', () => { + before(() => { + cy.goToPluginsPage() + cy.get('.is-dismissible a[href*="woocommerce-pagarme-hide-notice=documentation_link"]') + .click() + }) + + it('should not contain the message anymore', () => { + cy.get('.updated a[href*="docs.pagar.me"]') + .should('not.exist') + }) + }) + }) + + describe('and deactivate the brazilian market plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.deactivateBrazilianMarketPlugin() + }) + + describe('and deactivate the woocommerce-pagarme plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.deactivateWoocommercePagarmePlugin() + }) + + describe('and reactivate the woocommerce-pagarme plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.activateWoocommercePagarmePlugin() + }) + + it('should contain a message recommending the Brazilian Market plugin activation', () => { + cy.get('.updated a.button-primary') + .should('have.attr', 'href') + .and('match', /action=activate&plugin=woocommerce-extra-checkout-fields-for-brazil/) + }) + }) + + describe('and when click to close the message', () => { + before(() => { + cy.goToPluginsPage() + cy.get('.is-dismissible a[href*="woocommerce-pagarme-hide-notice=missing_brazilian_market"]') + .click() + }) + + it('should not contain the message anymore', () => { + cy.get('.updated a[href*="action=activate&plugin=woocommerce-extra-checkout-fields-for-brazil"]') + .should('not.exist') + }) + }) + }) + + describe('and deactivate the woocommerce-pagarme plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.deactivateWoocommercePagarmePlugin() + }) + + describe('and reactivate the woocommerce-pagarme plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.activateWoocommercePagarmePlugin() + }) + + it('should contain again a message recommending the Brazilian Market plugin activation', () => { + cy.get('.updated a.button-primary') + .should('have.attr', 'href') + .and('match', /action=activate&plugin=woocommerce-extra-checkout-fields-for-brazil/) + }) + }) + + describe('and when reactivate the brazilian market plugin', () => { + before(() => { + cy.goToPluginsPage() + cy.activateBrazilianMarketPlugin() + }) + + it('should not contain the message anymore', () => { + cy.get('.updated a[href*="action=activate&plugin=woocommerce-extra-checkout-fields-for-brazil"]') + .should('not.exist') + }) + }) + + after(() => { + cy.goToPluginsPage() + cy.get('.is-dismissible a[href*="woocommerce-pagarme-hide-notice=documentation_link"]') + .click() + }) + }) + }) + }) +}) + \ No newline at end of file diff --git a/tests/e2e/required-plugins.spec.js b/tests/e2e/required-plugins.spec.js deleted file mode 100644 index 4d9f0f7..0000000 --- a/tests/e2e/required-plugins.spec.js +++ /dev/null @@ -1,42 +0,0 @@ -context('WordPress Required Plugins', () => { - describe('when go to the wordpress plugins page', () => { - describe('and deactivate the brazilian market plugin', () => { - before(() => { - cy.goToPluginsPage() - cy.deactivateBrazilianMarketPlugin() - }) - - it('should contains error message', () => { - cy.get('.error a.button') - .contains('Brazilian Market') - .should('have.attr', 'href') - .and('match', /plugins.php\?action\=activate\&plugin\=woocommerce-extra-checkout-fields-for-brazil/) - }) - - after(() => { - cy.goToPluginsPage() - cy.activateBrazilianMarketPlugin() - }) - }) - - describe('and deactivate the woocommerce plugin', () => { - before(() => { - cy.goToPluginsPage() - cy.deactivateWoocommercePlugin() - }) - - it('should contains error message', () => { - cy.get('.error a.button') - .contains('WooCommerce') - .should('have.attr', 'href') - .and('match', /plugins.php\?action\=activate\&plugin\=woocommerce/) - }) - - after(() => { - cy.goToPluginsPage() - cy.activateWoocommercePlugin() - }) - }) - }) -}) - \ No newline at end of file diff --git a/tests/e2e/support/commands.js b/tests/e2e/support/commands.js index 42a7d55..80c0874 100644 --- a/tests/e2e/support/commands.js +++ b/tests/e2e/support/commands.js @@ -340,26 +340,40 @@ Cypress.Commands.add('deactivateBrazilianMarketPlugin', () => { cy.log('Deactivating the Brazilian Market plugin...') cy.get('#deactivate-woocommerce-extra-checkout-fields-for-brazil') - .click() + .click() }) Cypress.Commands.add('activateBrazilianMarketPlugin', () => { cy.log('Activating the Brazilian Market plugin...') cy.get('#activate-woocommerce-extra-checkout-fields-for-brazil') - .click() + .click() }) Cypress.Commands.add('deactivateWoocommercePlugin', () => { cy.log('Deactivating the WooCommerce plugin...') cy.get('#deactivate-woocommerce') - .click() + .click() }) Cypress.Commands.add('activateWoocommercePlugin', () => { cy.log('Activating the WooCommerce plugin...') cy.get('#activate-woocommerce') - .click() + .click() +}) + +Cypress.Commands.add('deactivateWoocommercePagarmePlugin', () => { + cy.log('Deactivating the WooCommerce Pagarme plugin...') + + cy.get('#deactivate-woocommerce-pagarme') + .click() +}) + +Cypress.Commands.add('activateWoocommercePagarmePlugin', () => { + cy.log('Activating the WooCommerce Pagarme plugin...') + + cy.get('#activate-woocommerce-pagarme') + .click() }) diff --git a/woocommerce-pagarme.php b/woocommerce-pagarme.php index fc2c036..4d8dd41 100644 --- a/woocommerce-pagarme.php +++ b/woocommerce-pagarme.php @@ -60,6 +60,14 @@ private function __construct() { if ( class_exists( 'Extra_Checkout_Fields_For_Brazil' ) ) { add_action( 'wcbcf_billing_fields', array( $this, 'wcbcf_billing_fields_custom_settings' ) ); } + + // Runs a specific method right after the plugin activation. + add_action( 'admin_init', array( $this, 'after_activation' ) ); + + // Dismissible notices. + add_action( 'wp_loaded', array( $this, 'hide_notices' ) ); + add_action( 'admin_notices', array( $this, 'brazilian_market_missing_notice' ) ); + add_action( 'admin_notices', array( $this, 'pagarme_documentation_link_notice' ) ); } /** @@ -143,6 +151,30 @@ public function woocommerce_missing_notice() { include dirname( __FILE__ ) . '/includes/admin/views/html-notice-missing-woocommerce.php'; } + /** + * Brazilian Market plugin missing notice. + */ + public function brazilian_market_missing_notice() { + if ( ( is_admin() && get_option( 'woocommerce_pagarme_admin_notice_missing_brazilian_market' ) === 'yes' ) ) { + // Do not show the notice if the Brazilian Market plugin is installed. + if ( class_exists( 'Extra_Checkout_Fields_For_Brazil' ) ) { + delete_option( 'woocommerce_pagarme_admin_notice_missing_brazilian_market' ); + return; + } + + include dirname( __FILE__ ) . '/includes/admin/views/html-notice-missing-brazilian-market.php'; + } + } + + /** + * Pagar.me documentation notice. + */ + public function pagarme_documentation_link_notice() { + if ( is_admin() && get_option( 'woocommerce_pagarme_admin_notice_documentation_link' ) === 'yes' ) { + include dirname( __FILE__ ) . '/includes/admin/views/html-notice-pagarme-documentation-link.php'; + } + } + /** * Upgrade. * @@ -196,8 +228,44 @@ public function wcbcf_billing_fields_custom_settings( $wcbcf_billing_fields ) { return $wcbcf_billing_fields; } + + /** + * Hide a notice if the GET variable is set. + */ + public static function hide_notices() { + if ( isset( $_GET['woocommerce-pagarme-hide-notice'] ) ) { + $notice_to_hide = sanitize_text_field( wp_unslash( $_GET['woocommerce-pagarme-hide-notice'] ) ); + delete_option( 'woocommerce_pagarme_admin_notice_' . $notice_to_hide ); + } + } + + /** + * Activate. + * + * Fired by `register_activation_hook` when the plugin is activated. + */ + public static function activation() { + if ( is_multisite() ) { + return; + } + + add_option( 'woocommerce_pagarme_activated', 'yes' ); + } + + /** + * After activation. + */ + public function after_activation() { + if ( is_admin() && get_option( 'woocommerce_pagarme_activated' ) === 'yes' ) { + delete_option( 'woocommerce_pagarme_activated' ); + + add_option( 'woocommerce_pagarme_admin_notice_documentation_link', 'yes' ); + add_option( 'woocommerce_pagarme_admin_notice_missing_brazilian_market', 'yes' ); + } + } } add_action( 'plugins_loaded', array( 'WC_Pagarme', 'get_instance' ) ); + register_activation_hook( plugin_basename( __FILE__ ), array( 'WC_Pagarme', 'activation' ) ); endif;