| @@ -2,117 +2,126 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Payplug\PayplugWoocommerce\Admin; |
| 4 | 4 | |
| 5 | 5 | // Exit if accessed directly |
| 6 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 7 | - exit; | |
| 6 | +if (!defined('ABSPATH')) { | |
| 7 | + exit; | |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | use Payplug\PayplugWoocommerce\PayplugWoocommerceHelper; |
| 11 | +use Payplug\PayplugWoocommerce\Traits\ServiceGetter; | |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * Handle admin notices. |
| 14 | - * | |
| 15 | - * @package Payplug\PayplugWoocommerce\Admin | |
| 16 | 15 | */ |
| 17 | -class Notices { | |
| 16 | +class Notices | |
| 17 | +{ | |
| 18 | + use ServiceGetter; | |
| 18 | 19 | |
| 19 | - public function __construct() { | |
| 20 | - add_action( 'admin_notices', [ $this, 'admin_notices' ] ); | |
| 21 | - add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); | |
| 22 | - } | |
| 20 | + private $options; | |
| 23 | 21 | |
| 24 | - /** | |
| 22 | + public function __construct() | |
| 23 | + { | |
| 24 | + $this->options = $this->get_service('configuration')->get_options(); | |
| 25 | + // $this->options = PayplugWoocommerceHelper::get_payplug_options(); | |
| 26 | + //add_action( 'admin_notices', [ $this, 'admin_notices' ] ); | |
| 27 | + //add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 25 | 31 | * Enqueue PayPlug notice style. |
| 26 | 32 | * |
| 27 | 33 | * @return void |
| 28 | - */ | |
| 29 | - public function admin_enqueue_scripts() { | |
| 30 | - $options = get_option( 'woocommerce_payplug_settings' ); | |
| 31 | - $payplug_test_key = ! empty( $options['payplug_test_key'] ) ? $options['payplug_test_key'] : ''; | |
| 32 | - $payplug_live_key = ! empty( $options['payplug_live_key'] ) ? $options['payplug_live_key'] : ''; | |
| 33 | - if ( empty( $payplug_test_key ) && empty( $payplug_live_key ) ) { | |
| 34 | - wp_enqueue_style( | |
| 35 | - 'payplug-notice', | |
| 36 | - PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/notice.css', | |
| 37 | - [], | |
| 38 | - PAYPLUG_GATEWAY_VERSION | |
| 39 | - ); | |
| 40 | - } | |
| 41 | - } | |
| 34 | + */ | |
| 35 | + public function admin_enqueue_scripts(): void | |
| 36 | + { | |
| 37 | + $api_key = json_decode($this->options, true); | |
| 38 | + $test_key = !empty($api_key['test']) ? $api_key['test'] : ''; | |
| 39 | + $live_key = !empty($api_key['live']) ? $api_key['live'] : ''; | |
| 40 | + if (empty($test_key) && empty($live_key)) { | |
| 41 | + wp_enqueue_style( | |
| 42 | + 'payplug-notice', | |
| 43 | + PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/css/notice.css', | |
| 44 | + [], | |
| 45 | + PAYPLUG_GATEWAY_VERSION | |
| 46 | + ); | |
| 47 | + } | |
| 48 | + } | |
| 42 | 49 | |
| 43 | - /** | |
| 44 | - * Display admin notices. | |
| 45 | - * | |
| 46 | - * @return void | |
| 47 | - */ | |
| 48 | - public function admin_notices() { | |
| 49 | - if ( ! current_user_can( 'manage_woocommerce' ) ) { | |
| 50 | - return; | |
| 51 | - } | |
| 50 | + /** | |
| 51 | + * Display admin notices. | |
| 52 | + * | |
| 53 | + * @return void | |
| 54 | + */ | |
| 55 | + public function admin_notices(): void | |
| 56 | + { | |
| 57 | + if (!current_user_can('manage_woocommerce')) { | |
| 58 | + return; | |
| 59 | + } | |
| 52 | 60 | |
| 53 | - /* | |
| 54 | - * Before Woocommerce 3.2.x, settings were saved just before displaying the page | |
| 55 | - * which cause the admin_notice to display old data. | |
| 56 | - * | |
| 57 | - * This condition check if we are on the PayPlug gateway settings page and if we | |
| 58 | - * have new settings to save. If true we hook the notice to a hook which will run after the new | |
| 59 | - * settings have been saved. | |
| 60 | - */ | |
| 61 | - $screen = get_current_screen(); | |
| 62 | - $wc = function_exists( 'WC' ) ? WC() : $GLOBALS['woocommerce']; | |
| 61 | + /* | |
| 62 | + * Before Woocommerce 3.2.x, settings were saved just before displaying the page | |
| 63 | + * which cause the admin_notice to display old data. | |
| 64 | + * | |
| 65 | + * This condition check if we are on the PayPlug gateway settings page and if we | |
| 66 | + * have new settings to save. If true we hook the notice to a hook which will run after the new | |
| 67 | + * settings have been saved. | |
| 68 | + */ | |
| 69 | + $screen = get_current_screen(); | |
| 70 | + $wc = function_exists('WC') ? WC() : $GLOBALS['woocommerce']; | |
| 63 | 71 | |
| 64 | - if ( | |
| 65 | - version_compare( $wc->version, '3.2.0', '<' ) | |
| 66 | - && ! empty( $_POST ) | |
| 67 | - && 'woocommerce_page_wc-settings' === $screen->id | |
| 68 | - && isset( $_GET['section'] ) | |
| 69 | - && 'payplug' === $_GET['section'] | |
| 70 | - ) { | |
| 71 | - add_action( 'woocommerce_settings_saved', [ $this, 'display_notice' ] ); | |
| 72 | + if ( | |
| 73 | + version_compare($wc->version, '3.2.0', '<') | |
| 74 | + && !empty($_POST) | |
| 75 | + && 'woocommerce_page_wc-settings' === $screen->id | |
| 76 | + && isset($_GET['section']) | |
| 77 | + && 'payplug' === $_GET['section'] | |
| 78 | + ) { | |
| 79 | + add_action('woocommerce_settings_saved', [$this, 'display_notice']); | |
| 72 | 80 | |
| 73 | - return; | |
| 74 | - } | |
| 81 | + return; | |
| 82 | + } | |
| 75 | 83 | |
| 76 | - $this->display_notice(); | |
| 77 | - } | |
| 84 | + $this->display_notice(); | |
| 85 | + } | |
| 78 | 86 | |
| 79 | - public function display_notice() { | |
| 80 | - $options = get_option( 'woocommerce_payplug_settings' ); | |
| 81 | - $testmode = ( isset( $options['mode'] ) && 'no' === $options['mode'] ) ? true : false; | |
| 82 | - $payplug_test_key = ! empty( $options['payplug_test_key'] ) ? $options['payplug_test_key'] : ''; | |
| 83 | - $payplug_live_key = ! empty( $options['payplug_live_key'] ) ? $options['payplug_live_key'] : ''; | |
| 87 | + public function display_notice(): void | |
| 88 | + { | |
| 89 | + $testmode = isset($this->options['mode']) && !(bool) $this->options['mode']; | |
| 90 | + $api_key = json_decode($this->options, true); | |
| 91 | + $test_key = !empty($api_key['test']) ? $api_key['test'] : ''; | |
| 92 | + $live_key = !empty($api_key['live']) ? $api_key['live'] : ''; | |
| 84 | 93 | |
| 85 | - if ( empty( $payplug_test_key ) && empty( $payplug_live_key ) ) { | |
| 86 | - ?> | |
| 87 | - <div class="notice notice--start"> | |
| 88 | - <div class="inside"> | |
| 89 | - <div class="main"> | |
| 90 | - <h2 class="notice__title"><?php _e( 'Thank you for installing PayPlug as your online payment solution.', 'payplug' ); ?> | |
| 91 | - <br> | |
| 92 | - <strong><?php _e( 'Only one step left to activate the plugin on your site !', 'payplug' ); ?></strong> | |
| 93 | - </h2> | |
| 94 | - <a href="<?php echo esc_url( PayplugWoocommerceHelper::get_setting_link() ); ?>" | |
| 95 | - class="button button-hero"><?php _e( 'Login', 'payplug' ); ?></a> | |
| 96 | - <img class="notice__img" | |
| 97 | - src="<?php echo esc_url( PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/images/PAYPLUG_LOGO_blanc.svg' ); ?>" | |
| 98 | - alt="PayPlug logo"> | |
| 99 | - </div> | |
| 100 | - </div> | |
| 101 | - </div> | |
| 94 | + if (empty($test_key) && empty($live_key)) { | |
| 95 | + ?> | |
| 96 | + <div class="notice notice--start"> | |
| 97 | + <div class="inside"> | |
| 98 | + <div class="main"> | |
| 99 | + <h2 class="notice__title"><?php _e('Thank you for installing PayPlug as your online payment solution.', 'payplug'); ?> | |
| 100 | + <br> | |
| 101 | + <strong><?php _e('Only one step left to activate the plugin on your site !', 'payplug'); ?></strong> | |
| 102 | + </h2> | |
| 103 | + <a href="<?php echo esc_url(PayplugWoocommerceHelper::get_setting_link()); ?>" | |
| 104 | + class="button button-hero"><?php _e('Login', 'payplug'); ?></a> | |
| 105 | + <img class="notice__img" | |
| 106 | + src="<?php echo esc_url(PAYPLUG_GATEWAY_PLUGIN_URL . 'assets/images/PAYPLUG_LOGO_blanc.svg'); ?>" | |
| 107 | + alt="PayPlug logo"> | |
| 108 | + </div> | |
| 109 | + </div> | |
| 110 | + </div> | |
| 102 | 111 | <?php |
| 103 | - } elseif ( ! empty( $payplug_test_key ) && empty( $payplug_live_key ) ) { | |
| 104 | - ?> | |
| 105 | - <div class="notice notice-warning"> | |
| 106 | - <p><strong><?php _e( 'PayPlug is in TEST mode', 'payplug' ); ?></strong></p> | |
| 107 | - <p><?php _e( 'Once your PayPlug account has been validated, please log out and log in again from the configuration page in order to activate LIVE mode.', 'payplug' ); ?></p> | |
| 108 | - </div> | |
| 112 | + } elseif (!empty($test_key) && empty($live_key)) { | |
| 113 | + ?> | |
| 114 | + <div class="notice notice-warning"> | |
| 115 | + <p><strong><?php _e('PayPlug is in TEST mode', 'payplug'); ?></strong></p> | |
| 116 | + <p><?php _e('Once your PayPlug account has been validated, please log out and log in again from the configuration page in order to activate LIVE mode.', 'payplug'); ?></p> | |
| 117 | + </div> | |
| 109 | 118 | <?php |
| 110 | - } elseif ( ! empty( $payplug_live_key ) && $testmode ) { | |
| 111 | - ?> | |
| 112 | - <div class="notice notice-info"> | |
| 113 | - <p><?php _e( 'Payments in TEST mode will be simulations and will not generate real transactions.', 'payplug' ); ?></p> | |
| 114 | - </div> | |
| 119 | + } elseif (!empty($live_key) && $testmode) { | |
| 120 | + ?> | |
| 121 | + <div class="notice notice-info"> | |
| 122 | + <p><?php _e('Payments in TEST mode will be simulations and will not generate real transactions.', 'payplug'); ?></p> | |
| 123 | + </div> | |
| 115 | 124 | <?php |
| 116 | - } | |
| 117 | - } | |
| 125 | + } | |
| 126 | + } | |
| 118 | 127 | } |