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' ) . '

'; } if ( Jsonpage_Client::has_key() && get_transient( 'jsonpage_key_rejected' ) ) { echo '

' . esc_html__( 'Jsonpage: the API refused your key. Check it in your Jsonpage dashboard. Orders are not blocked in the meantime.', 'jsonpage' ) . '

'; } // Result of the connection test, kept for one minute for the user who ran it. if ( $here ) { $key = self::test_result_key(); $result = get_transient( $key ); if ( false !== $result ) { delete_transient( $key ); if ( 'ok' === $result ) { echo '

' . esc_html__( 'Connection works: your key is accepted.', 'jsonpage' ) . '

'; } else { echo '

' . esc_html__( 'The test failed: the key is missing, wrong, or the service did not answer.', 'jsonpage' ) . '

'; } } } } /** * Name of the transient that holds the result of the connection test of the current user. * * @return string */ private static function test_result_key() { return 'jsonpage_test_' . get_current_user_id(); } /** * Tests the key with one autocomplete request. */ public static function test_connection() { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'You do not have permission to do this.', 'jsonpage' ), '', array( 'response' => 403 ) ); } check_admin_referer( 'jsonpage_test' ); delete_transient( 'jsonpage_key_rejected' ); delete_transient( 'jsonpage_down' ); $result = Jsonpage_Client::autocomplete( 'carrefour test ' . wp_rand( 100, 999 ), array( 'FR' ) ); set_transient( self::test_result_key(), null === $result ? 'failed' : 'ok', MINUTE_IN_SECONDS ); wp_safe_redirect( admin_url( 'options-general.php?page=' . self::PAGE ) ); exit; } /** * Names of the countries, in the language of the admin. * * @return array */ private static function country_names() { return array( 'FR' => __( 'France', 'jsonpage' ), 'BE' => __( 'Belgium', 'jsonpage' ), 'CH' => __( 'Switzerland', 'jsonpage' ), 'GB' => __( 'United Kingdom', 'jsonpage' ), 'NO' => __( 'Norway', 'jsonpage' ), 'FI' => __( 'Finland', 'jsonpage' ), 'LV' => __( 'Latvia', 'jsonpage' ), ); } /** * Page. */ public static function render() { if ( ! current_user_can( self::CAP ) ) { return; } $s = jsonpage_settings(); $name = self::OPTION; $names = self::country_names(); ?>

jsonpage.com/en/privacy', 'jsonpage.com/integrations/woocommerce' ), array( 'a' => array( 'href' => true, 'target' => true, 'rel' => true, ), ) ); ?>