' . esc_html__( 'Settings', 'verified-net' ) . '' ); return $links; } /** * Register the option and its sanitizer. */ public static function register() { register_setting( 'verified_net', 'verified_net_options', array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize' ), 'default' => verified_net_defaults(), ) ); } /** * Sanitize submitted settings. An empty API key field keeps the saved key. * * @param mixed $input Raw input. * @return array */ public static function sanitize( $input ) { $old = verified_net_options(); $input = is_array( $input ) ? $input : array(); $out = verified_net_defaults(); $key = isset( $input['api_key'] ) ? preg_replace( '/[^A-Za-z0-9_\-]/', '', (string) $input['api_key'] ) : ''; if ( ! empty( $input['clear_key'] ) ) { $out['api_key'] = ''; } elseif ( '' !== $key ) { $out['api_key'] = $key; delete_option( 'verified_net_problem' ); delete_transient( 'verified_net_paused' ); } else { $out['api_key'] = $old['api_key']; } foreach ( array( 'check_email', 'check_phone', 'forms_cf7', 'forms_wpforms', 'forms_gravity', 'forms_register' ) as $flag ) { $out[ $flag ] = empty( $input[ $flag ] ) ? 0 : 1; } $out['on_reject'] = ( isset( $input['on_reject'] ) && 'allow' === $input['on_reject'] ) ? 'allow' : 'block'; $out['on_review'] = ( isset( $input['on_review'] ) && 'block' === $input['on_review'] ) ? 'block' : 'allow'; $country = isset( $input['default_country'] ) ? strtoupper( sanitize_text_field( (string) $input['default_country'] ) ) : ''; $out['default_country'] = preg_match( '/^[A-Z]{2}$/', $country ) ? $country : ''; $out['email_message'] = isset( $input['email_message'] ) ? sanitize_text_field( (string) $input['email_message'] ) : ''; $out['phone_message'] = isset( $input['phone_message'] ) ? sanitize_text_field( (string) $input['phone_message'] ) : ''; return $out; } /** * "Test connection": fetches the account (free) and shows the balance. */ public static function test_connection() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'verified-net' ) ); } check_admin_referer( 'verified_net_test' ); $options = verified_net_options(); if ( '' === $options['api_key'] ) { $notice = array( 'type' => 'error', 'text' => __( 'Add your API key first.', 'verified-net' ), ); } else { $account = Verified_Net_Client::account( $options['api_key'] ); if ( is_wp_error( $account ) ) { $notice = array( 'type' => 'error', /* translators: %s: error message from the API or the network */ 'text' => sprintf( __( 'Connection failed: %s', 'verified-net' ), $account->get_error_message() ), ); } else { $balance = isset( $account['credits']['balance'] ) ? (int) $account['credits']['balance'] : 0; $notice = array( 'type' => 'success', /* translators: %s: number of credits */ 'text' => sprintf( __( 'Connected. Your balance is %s credits.', 'verified-net' ), number_format_i18n( $balance ) ), ); } } set_transient( 'verified_net_test_' . get_current_user_id(), $notice, MINUTE_IN_SECONDS ); wp_safe_redirect( admin_url( 'options-general.php?page=' . self::PAGE ) ); exit; } /** * Notices: a missing key on the Plugins screen, API problems on the dashboard and settings page. */ public static function notices() { if ( ! current_user_can( 'manage_options' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $id = $screen ? $screen->id : ''; $options = verified_net_options(); $link = admin_url( 'options-general.php?page=' . self::PAGE ); if ( 'plugins' === $id && '' === $options['api_key'] ) { printf( '

%s %s

', esc_html__( 'Verified.net needs an API key before it can check form submissions.', 'verified-net' ), esc_url( $link ), esc_html__( 'Add your key', 'verified-net' ) ); } $problem = get_option( 'verified_net_problem' ); if ( ! in_array( $id, array( 'dashboard', 'settings_page_' . self::PAGE ), true ) || ! is_array( $problem ) || '' === $options['api_key'] ) { return; } $texts = array( 'invalid_key' => __( 'Verified.net rejected the API key. Form submissions are not being checked.', 'verified-net' ), 'no_credits' => __( 'Your Verified.net account is out of credits. Form submissions are not being checked until you add credits.', 'verified-net' ), 'unreachable' => __( 'Verified.net could not be reached recently. Submissions were let through unchecked meanwhile.', 'verified-net' ), 'rate_limited' => __( 'Verified.net rate limit reached recently. Some submissions were let through unchecked.', 'verified-net' ), ); $code = isset( $problem['code'] ) ? (string) $problem['code'] : ''; if ( ! isset( $texts[ $code ] ) ) { return; } $when = isset( $problem['time'] ) ? human_time_diff( (int) $problem['time'] ) : ''; printf( '

%s %s %s

', esc_html( $texts[ $code ] ), /* translators: %s: time span, e.g. "5 mins" */ esc_html( '' !== $when ? sprintf( __( '(%s ago)', 'verified-net' ), $when ) : '' ), esc_url( 'no_credits' === $code ? 'https://app.verified.net/billing' : $link ), esc_html( 'no_credits' === $code ? __( 'Add credits', 'verified-net' ) : __( 'Settings', 'verified-net' ) ) ); } /** * Render the settings page. */ public static function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } $o = verified_net_options(); $name = 'verified_net_options'; $notice = get_transient( 'verified_net_test_' . get_current_user_id() ); if ( is_array( $notice ) ) { delete_transient( 'verified_net_test_' . get_current_user_id() ); } $forms = array( 'forms_cf7' => array( __( 'Contact Form 7', 'verified-net' ), defined( 'WPCF7_VERSION' ) ), 'forms_wpforms' => array( __( 'WPForms', 'verified-net' ), defined( 'WPFORMS_VERSION' ) ), 'forms_gravity' => array( __( 'Gravity Forms', 'verified-net' ), class_exists( 'GFForms' ) ), 'forms_register' => array( __( 'WordPress registration form', 'verified-net' ), true ), ); ?>