Jsonpage). * * @package Jsonpage */ defined( 'ABSPATH' ) || exit; class Jsonpage_Settings { const OPTION = 'jsonpage_settings'; const PAGE = 'jsonpage'; const GROUP = 'jsonpage_group'; const CAP = 'manage_options'; /** * Hooks. */ public static function init() { add_action( 'admin_menu', array( __CLASS__, 'menu' ) ); add_action( 'admin_init', array( __CLASS__, 'register' ) ); add_action( 'admin_post_jsonpage_test', array( __CLASS__, 'test_connection' ) ); add_action( 'admin_notices', array( __CLASS__, 'notices' ) ); // The settings form is saved by options.php, which checks the nonce of settings_fields() and this capability. add_filter( 'option_page_capability_' . self::GROUP, array( __CLASS__, 'capability' ) ); } /** * Capability needed to see and save the settings. * * @return string */ public static function capability() { return self::CAP; } /** * Menu entry. */ public static function menu() { add_options_page( __( 'Jsonpage', 'jsonpage' ), __( 'Jsonpage', 'jsonpage' ), self::CAP, self::PAGE, array( __CLASS__, 'render' ) ); } /** * Registers the option. */ public static function register() { register_setting( self::GROUP, self::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'default' => jsonpage_default_settings(), ) ); } /** * Cleans the values posted by the settings form. * * @param mixed $input Posted values. * @return array */ public static function sanitize( $input ) { $old = jsonpage_settings(); $input = is_array( $input ) ? $input : array(); $out = array(); $key = isset( $input['api_key'] ) ? trim( sanitize_text_field( wp_unslash( $input['api_key'] ) ) ) : ''; // An empty field keeps the saved key; the key is never printed back. $out['api_key'] = '' === $key ? $old['api_key'] : $key; if ( ! empty( $input['remove_key'] ) ) { $out['api_key'] = ''; } $countries = isset( $input['countries'] ) && is_array( $input['countries'] ) ? array_map( 'sanitize_text_field', wp_unslash( $input['countries'] ) ) : array(); $out['countries'] = array_values( array_intersect( jsonpage_supported_countries(), $countries ) ); if ( empty( $out['countries'] ) ) { $out['countries'] = jsonpage_supported_countries(); } $invalid = isset( $input['invalid_vat'] ) ? sanitize_key( wp_unslash( $input['invalid_vat'] ) ) : 'warn'; $out['invalid_vat'] = in_array( $invalid, array( 'block', 'warn', 'charge_vat' ), true ) ? $invalid : 'warn'; $closed = isset( $input['closed'] ) ? sanitize_key( wp_unslash( $input['closed'] ) ) : 'warn'; $out['closed'] = in_array( $closed, array( 'block', 'warn', 'allow' ), true ) ? $closed : 'warn'; foreach ( array( 'auto_exempt', 'require_vat', 'forms_enabled' ) as $flag ) { $out[ $flag ] = isset( $input[ $flag ] ) && 'yes' === sanitize_key( wp_unslash( $input[ $flag ] ) ) ? 'yes' : 'no'; } return $out; } /** * Admin notices: missing key, rejected key. */ public static function notices() { if ( ! current_user_can( self::CAP ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $here = $screen && 'settings_page_' . self::PAGE === $screen->id; if ( ! Jsonpage_Client::has_key() && $here ) { echo '
' . esc_html__( 'Jsonpage is not active yet: add your API key below. The free account is enough to start.', 'jsonpage' ) . '
' . esc_html__( 'Jsonpage: the API refused your key. Check it in your Jsonpage dashboard. Orders are not blocked in the meantime.', 'jsonpage' ) . '
' . esc_html__( 'Connection works: your key is accepted.', 'jsonpage' ) . '
' . esc_html__( 'The test failed: the key is missing, wrong, or the service did not answer.', 'jsonpage' ) . '
jsonpage.com/en/privacy', 'jsonpage.com/integrations/woocommerce' ), array( 'a' => array( 'href' => true, 'target' => true, 'rel' => true, ), ) ); ?>