Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/js/admin-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@
var value = jQuery( '#woocommerce_postnl_environment_mode' ).val();
if ( 'production' === value ) {
jQuery('#woocommerce_postnl_api_keys').closest('tr').show();
jQuery('#woocommerce_postnl_api_keys_new').closest('tr').show();
jQuery('#woocommerce_postnl_api_keys_sandbox').closest('tr').hide();
} else {
jQuery('#woocommerce_postnl_api_keys').closest('tr').hide();
jQuery('#woocommerce_postnl_api_keys_new').closest('tr').hide();
jQuery('#woocommerce_postnl_api_keys_sandbox').closest('tr').show();
}
},
Expand Down
32 changes: 32 additions & 0 deletions assets/js/new-api-key-banner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
( function ( $ ) {
if ( typeof window.postnlNewApiKeyBanner === 'undefined' ) {
return;
}

var config = window.postnlNewApiKeyBanner;

function send( $banner, mode ) {
$.post(
config.ajaxUrl,
{
action: config.action,
nonce: $banner.data( 'nonce' ),
mode: mode
}
).always( function () {
$banner.fadeOut( 200, function () {
$( this ).remove();
} );
} );
}

$( document ).on( 'click', '.postnl-new-api-key-remind', function ( e ) {
e.preventDefault();
send( $( this ).closest( '.postnl-new-api-key-banner' ), 'remind' );
} );

$( document ).on( 'click', '.postnl-new-api-key-dismiss', function ( e ) {
e.preventDefault();
send( $( this ).closest( '.postnl-new-api-key-banner' ), 'dismiss' );
} );
} )( jQuery );
32 changes: 32 additions & 0 deletions languages/postnl-for-woocommerce-nl_NL.po
Original file line number Diff line number Diff line change
Expand Up @@ -1907,3 +1907,35 @@ msgstr ""
#: src/Shipping_Method/Settings.php:401
msgid "Pickup Points"
msgstr ""

#: src/Shipping_Method/Settings.php
msgid "New API Key"
msgstr "Nieuwe API Key"

#: src/Shipping_Method/Settings.php
msgid "Enter the new API key here, required to access the new APIs when these have been released in the plug-in."
msgstr "Vul hier je nieuwe API key in, die je toegang geeft tot de nieuwe APIs zodra deze zijn uitgerold in de plug-in."

#: src/Shipping_Method/PostNL.php
msgid "The newly entered API key is invalid. Please check the key and enter it again."
msgstr "Ingevulde nieuwe API key ongeldig. Controleer de key en vul deze opnieuw in."

#: src/Shipping_Method/PostNL.php
msgid "Please fill in Customer Code and Customer Number first to validate the new API key."
msgstr "Vul eerst de Klantcode en het Klantnummer in om de nieuwe API key te valideren."

#: src/Admin/Api_Key_Banner.php
msgid "PostNL: New API Key required"
msgstr "PostNL: nieuwe API key vereist"

#: src/Admin/Api_Key_Banner.php
msgid "Important: In the latest update of the plug-in, an additional API key field has been added to the account configuration of the PostNL plug-in. This field must be filled in with the new API key that can be obtained via the Self Service module on the PostNL Business Portal. This API key is required to gain access to the new APIs that will be rolled out in a future update of the plug-in. It is very important that this key is entered before the relevant update is performed; otherwise, no connection can be made to the new PostNL APIs, and it will not be possible to create labels or use checkout features such as delivery days and pickup points."
msgstr "Let op: er is in de laatste update van de plug-in een extra API key veld toegevoegd aan de accountconfiguratie van de PostNL plug-in. In dit veld moet de nieuwe API key ingevuld worden die je via de Self Service module op het Business Portal van PostNL kunt verkrijgen. Deze API key is nodig om toegang te krijgen tot de nieuwe APIs die zullen worden uitgerold in een toekomstige update van de plug-in. Het is van groot belang dat deze key ingevuld wordt voordat de betreffende update wordt uitgevoerd, omdat er anders geen verbinding gemaakt kan worden met de nieuwe PostNL APIs en er dus geen labels aangemaakt kunnen worden of gebruik gemaakt kan worden van de check-out functies zoals bezorgdagen en afhaalpunten."

#: src/Admin/Api_Key_Banner.php
msgid "Remind me later"
msgstr "Herinner me later"

#: src/Admin/Api_Key_Banner.php
msgid "Dismiss permanently"
msgstr "Definitief verbergen"
210 changes: 210 additions & 0 deletions src/Admin/Api_Key_Banner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
<?php
/**
* Class Admin\Api_Key_Banner file.
*
* Renders the admin banner that asks merchants to enter the new PostNL API
* key ahead of the upcoming API migration. The banner is shown on the PostNL
* settings screen and on the WooCommerce orders list. Two dismissal modes
* are supported: "remind me later" (cleared on the next login) and a
* permanent dismiss stored per user.
*
* @package PostNLWooCommerce\Admin
*/

namespace PostNLWooCommerce\Admin;

use PostNLWooCommerce\Shipping_Method\Settings;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

/**
* Class Api_Key_Banner
*/
class Api_Key_Banner {

const META_DISMISSED = 'postnl_new_api_key_banner_dismissed';
const META_REMIND_LATER = 'postnl_new_api_key_banner_remind_later';
const NONCE_ACTION = 'postnl_new_api_key_banner';
const AJAX_ACTION = 'postnl_dismiss_new_api_key_banner';

/**
* Register hooks.
*/
public function __construct() {
add_action( 'admin_notices', array( $this, 'maybe_render' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
add_action( 'wp_ajax_' . self::AJAX_ACTION, array( $this, 'handle_ajax_dismiss' ) );
add_action( 'wp_login', array( $this, 'clear_remind_later_on_login' ), 10, 2 );
}

/**
* Is the current screen one where the banner should appear?
*/
protected function is_target_screen() {
if ( ! function_exists( 'get_current_screen' ) ) {
return false;
}

$screen = get_current_screen();
if ( empty( $screen ) ) {
return false;
}

if ( 'woocommerce_page_wc-settings' === $screen->id
&& isset( $_GET['section'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
&& POSTNL_SETTINGS_ID === sanitize_text_field( wp_unslash( $_GET['section'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended
) {
return true;
}

if ( 'edit-shop_order' === $screen->id ) {
return true;
}

if ( 'woocommerce_page_wc-orders' === $screen->id ) {
return true;
}

return false;
}

/**
* Should the banner be visible right now for this user?
*/
protected function should_show() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
return false;
}

if ( ! $this->is_target_screen() ) {
return false;
}

$settings = Settings::get_instance();
$new_key = $settings->get_api_key_new();

// If a valid new key has already been entered we have what we need.
if ( '' !== $new_key && $settings->is_api_key_new_validated() ) {
return false;
}

$user_id = get_current_user_id();
if ( get_user_meta( $user_id, self::META_DISMISSED, true ) ) {
return false;
}

if ( get_user_meta( $user_id, self::META_REMIND_LATER, true ) ) {
return false;
}

return true;
}

/**
* Render the banner markup.
*/
public function maybe_render() {
if ( ! $this->should_show() ) {
return;
}

$nonce = wp_create_nonce( self::NONCE_ACTION );
?>
<div class="notice notice-warning postnl-new-api-key-banner" data-nonce="<?php echo esc_attr( $nonce ); ?>">
<p><strong><?php esc_html_e( 'PostNL: New API Key required', 'postnl-for-woocommerce' ); ?></strong></p>
<p><?php echo esc_html( $this->get_message() ); ?></p>
<p>
<button type="button" class="button button-secondary postnl-new-api-key-remind">
<?php esc_html_e( 'Remind me later', 'postnl-for-woocommerce' ); ?>
</button>
<button type="button" class="button-link postnl-new-api-key-dismiss">
<?php esc_html_e( 'Dismiss permanently', 'postnl-for-woocommerce' ); ?>
</button>
</p>
</div>
<?php
}

/**
* Banner body text.
*/
protected function get_message() {
return __(
'Important: In the latest update of the plug-in, an additional API key field has been added to the account configuration of the PostNL plug-in. This field must be filled in with the new API key that can be obtained via the Self Service module on the PostNL Business Portal. This API key is required to gain access to the new APIs that will be rolled out in a future update of the plug-in. It is very important that this key is entered before the relevant update is performed; otherwise, no connection can be made to the new PostNL APIs, and it will not be possible to create labels or use checkout features such as delivery days and pickup points.',
'postnl-for-woocommerce'
);
}

/**
* Enqueue the dismissal JS on screens where the banner can appear.
*
* @param string $hook_suffix Current admin page hook.
*/
public function enqueue_assets( $hook_suffix ) {
unset( $hook_suffix );

if ( ! $this->is_target_screen() ) {
return;
}

wp_enqueue_script(
'postnl-new-api-key-banner',
POSTNL_WC_PLUGIN_DIR_URL . '/assets/js/new-api-key-banner.js',
array( 'jquery' ),
POSTNL_WC_VERSION,
true
);

wp_localize_script(
'postnl-new-api-key-banner',
'postnlNewApiKeyBanner',
array(
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'action' => self::AJAX_ACTION,
)
);
}

/**
* AJAX handler for both dismiss modes.
*/
public function handle_ajax_dismiss() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_send_json_error( array( 'message' => 'forbidden' ), 403 );
}

check_ajax_referer( self::NONCE_ACTION, 'nonce' );

$mode = isset( $_POST['mode'] ) ? sanitize_key( wp_unslash( $_POST['mode'] ) ) : '';
$user_id = get_current_user_id();

if ( 'remind' === $mode ) {
update_user_meta( $user_id, self::META_REMIND_LATER, time() );
wp_send_json_success();
}

if ( 'dismiss' === $mode ) {
update_user_meta( $user_id, self::META_DISMISSED, 1 );
wp_send_json_success();
}

wp_send_json_error( array( 'message' => 'invalid mode' ), 400 );
}

/**
* Wipe the "remind me later" flag whenever the user logs in, so the
* banner comes back the next session.
*
* @param string $user_login Username.
* @param \WP_User $user Logged-in user.
*/
public function clear_remind_later_on_login( $user_login, $user ) {
unset( $user_login );

if ( $user instanceof \WP_User ) {
delete_user_meta( $user->ID, self::META_REMIND_LATER );
}
}
}
4 changes: 4 additions & 0 deletions src/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,10 @@ public function init() {
$this->load_fill_in_with_postnl_settings();
$this->get_frontend();
$this->get_product_editor();

if ( is_admin() ) {
new Admin\Api_Key_Banner();
}
}

/**
Expand Down
Loading