'error', 'message' => __( 'You are not allowed to change these settings.', 'sprintplan-pipedrive-integration' ), ); } $nonce = isset( $_POST['_wpspi_settings_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpspi_settings_nonce'] ) ) : ''; if ( ! wp_verify_nonce( $nonce, 'wpspi_save_settings' ) ) { return array( 'type' => 'error', 'message' => __( 'Settings were not saved because the security check failed. Refresh the page and try again.', 'sprintplan-pipedrive-integration' ), ); } $submitted = isset( $_POST['wpspi_smart_pipedrive_settings'] ) && is_array( $_POST['wpspi_smart_pipedrive_settings'] ) ? map_deep( wp_unslash( $_POST['wpspi_smart_pipedrive_settings'] ), 'sanitize_text_field' ) : array(); $settings = get_option( 'wpspi_smart_pipedrive_settings', array() ); if ( ! is_array( $settings ) ) { $settings = array(); } $tab = isset( $_POST['tab'] ) ? sanitize_key( wp_unslash( $_POST['tab'] ) ) : 'general'; if ( 'general' === $tab ) { $token = isset( $submitted['psn-token'] ) ? sanitize_text_field( $submitted['psn-token'] ) : ''; if ( '' === $token ) { return array( 'type' => 'error', 'message' => __( 'Enter a Pipedrive API token.', 'sprintplan-pipedrive-integration' ), ); } $settings['psn-token'] = $token; update_option( 'wpspi_smart_pipedrive_settings', $settings ); $api = new WPSPI_Smart_Pipedrive_API(); $validation = $api->validateApiToken(); if ( is_wp_error( $validation ) ) { WPSPI_Smart_Pipedrive_API::log_error( 'API token validation failed: ' . $validation->get_error_message() ); return array( 'type' => 'error', 'message' => sprintf( /* translators: %s: API token validation error */ __( 'API token saved, but authorization failed: %s', 'sprintplan-pipedrive-integration' ), $validation->get_error_message() ), ); } return array( 'type' => 'success', 'message' => __( 'API token saved and Pipedrive authorization successful.', 'sprintplan-pipedrive-integration' ), ); } if ( 'synch_settings' === $tab ) { $synch = isset( $submitted['synch'] ) && is_array( $submitted['synch'] ) ? $submitted['synch'] : array(); $settings['synch'] = array(); foreach ( $synch as $key => $enabled ) { if ( '1' === (string) $enabled ) { $settings['synch'][ sanitize_key( $key ) ] = 1; } } update_option( 'wpspi_smart_pipedrive_settings', $settings ); return array( 'type' => 'success', 'message' => __( 'Synchronization settings saved.', 'sprintplan-pipedrive-integration' ), ); } return array( 'type' => 'error', 'message' => __( 'Unknown settings section. Refresh the page and try again.', 'sprintplan-pipedrive-integration' ), ); } public function displaySettingsForm( $notice = null ) { require_once WPSPI_PLUGIN_PATH . 'admin/partials/settings.php'; } }