$errors ), 60 ); $args = array( 'tab' => 'security' ); if ( ! $errors ) { $args['updated'] = 1; } if ( '' !== $success ) { set_transient( 'bapi_forms_security_ok_' . get_current_user_id(), $success, 60 ); } wp_safe_redirect( SettingsPage::url( $args ) ); exit; } /** Verify a new key with Business API, swap it in and forget the old one. */ public function rotate(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'businessapi-forms' ), 403 ); } check_admin_referer( self::ROTATE_ACTION ); $options = $this->security->options(); $env = isset( $_POST['environment'] ) && Options::ENV_LIVE === $_POST['environment'] ? Options::ENV_LIVE : Options::ENV_TEST; $new = isset( $_POST['new_key'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['new_key'] ) ) ) : ''; if ( $options->key_is_constant( $env ) ) { self::done( array( __( 'This key is set in wp-config.php. Change it there.', 'businessapi-forms' ) ) ); } if ( ! Options::key_matches_environment( $new, $env ) ) { /* translators: %s: expected key prefix. */ self::done( array( sprintf( __( 'The new key was not saved. It must be a secret key starting with %s.', 'businessapi-forms' ), 'bapi_sk_' . $env . '_' ) ) ); } if ( $new === $options->key( $env ) ) { self::done( array( __( 'That is the key already in use.', 'businessapi-forms' ) ) ); } $check = $this->account_with( $new, $env ); if ( null === $check['account'] ) { /* translators: %s: error. */ self::done( array( sprintf( __( 'The new key was not saved: Business API did not accept it (%s).', 'businessapi-forms' ), $check['error'] ) ) ); } // Must be the same account as the current key, when that one still works. $old = '' !== $options->key( $env ) ? $this->account_with( $options->key( $env ), $env )['account'] : null; if ( null !== $old && $old !== $check['account'] ) { self::done( array( __( 'The new key belongs to a different Business API account than the current one. It was not saved. To switch accounts, remove the key on the Connection tab first.', 'businessapi-forms' ) ) ); } $plugin = Plugin::instance(); $this->security->rotating = true; $options->update( array( $env . '_key' => $new ) ); $this->security->rotating = false; $plugin->capabilities->flush(); $plugin->prices->flush(); $plugin->sites->register_if_missing( $env ); $this->security->audit->log( 'key_rotated', array( 'environment' => $env ) ); self::done( array(), __( 'Key rotated. The old key is no longer stored here: revoke it in your Business API dashboard (API keys) now.', 'businessapi-forms' ) ); } /** * Account name for a key that is not stored yet. * * @return array{account: ?string, error: string} account is null when refused. */ private function account_with( string $key, string $env ): array { $response = $this->security->client()->request( 'GET', 'account-status', array(), null, $env, array(), 0, $key ); if ( ! $response->ok() ) { return array( 'account' => null, /* translators: %d: HTTP status. */ 'error' => 0 === $response->status ? (string) $response->error : sprintf( __( 'HTTP %d', 'businessapi-forms' ), $response->status ), ); } $data = $response->data; return array( 'account' => strtolower( trim( (string) ( $data['accountName'] ?? '' ) ) ), 'error' => '', ); } public function check_now(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'businessapi-forms' ), 403 ); } check_admin_referer( self::CHECK_ACTION ); $this->security->checklist->debug_log_public( true ); ( new \BapiForms\Admin\Compatibility( \BapiForms\Plugin::instance()->options ) )->rest_check( true ); $result = $this->security->integrity->run(); $errors = array(); if ( Integrity::UNCHECKED === $result['status'] ) { /* translators: %s: error. */ $errors[] = sprintf( __( 'The integrity check could not run: %s', 'businessapi-forms' ), (string) ( $result['error'] ?? '' ) ); } self::done( $errors, Integrity::FAILED === $result['status'] ? '' : __( 'Checks ran again.', 'businessapi-forms' ) ); } public function render(): void { $ok = get_transient( 'bapi_forms_security_ok_' . get_current_user_id() ); delete_transient( 'bapi_forms_security_ok_' . get_current_user_id() ); if ( is_string( $ok ) && '' !== $ok ) { printf( '
%s
| user_login ); ?> |
Site Health. BusinessAPI Forms is not a security plugin: use a dedicated one for firewall and malware scanning.', 'businessapi-forms' ); ?>
| () |
|