-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathebics-api.php
More file actions
344 lines (312 loc) · 12.3 KB
/
ebics-api.php
File metadata and controls
344 lines (312 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* Plugin Name: EBICS API
* Description: Provides a Wordpress-friendly interface to an EBICS API microservice for secure bank communication.
* Version: 1.0.0
* Author: Andrii Svirin
* License: GPL-2.0+
* Text Domain: ebics-api
*/
// Exit if accessed directly to prevent security leaks
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Check for Composer dependencies and handle missing ones gracefully.
*/
if ( ! file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
/**
* Display a warning notice if dependencies are missing.
*/
function ebics_api_missing_dependencies_notice() {
?>
<div class="notice notice-error">
<p><?php esc_html_e( 'EBICS API plugin requires Composer dependencies. Please run "composer install" in the plugin directory.', 'ebics-api' ); ?></p>
</div>
<?php
}
add_action( 'admin_notices', 'ebics_api_missing_dependencies_notice' );
/**
* Renders a placeholder settings page with a warning.
*/
function ebics_api_placeholder_settings_page() {
?>
<div class="wrap">
<h1><?php esc_html_e( 'EBICS API', 'ebics-api' ); ?></h1>
<div class="notice notice-error">
<p><?php esc_html_e( 'EBICS API plugin is not fully functional because required dependencies are missing. Please run "composer install" in the plugin directory.', 'ebics-api' ); ?></p>
</div>
</div>
<?php
}
/**
* Adds a submenu page that shows the placeholder content.
*/
function ebics_api_add_placeholder_admin_menu() {
add_submenu_page(
'options-general.php',
'EBICS API',
'EBICS API',
'manage_options',
'ebics-api',
'ebics_api_placeholder_settings_page'
);
}
add_action( 'admin_menu', 'ebics_api_add_placeholder_admin_menu' );
// Stop loading the rest of the plugin to prevent fatal errors.
return;
}
// If we get here, dependencies are loaded. Proceed with the full plugin.
require_once __DIR__ . '/vendor/autoload.php';
// Include the class files.
require_once __DIR__ . '/includes/class-ebics-api-client-service.php';
require_once __DIR__ . '/includes/class-ebics-api-transaction-page.php';
// Initialize the transaction page handler.
$ebics_api_transaction_page = new Ebics_API_Transaction_Page();
/**
* Adds a submenu page under the Settings menu.
*/
function ebics_api_add_admin_menu() {
add_submenu_page(
'options-general.php',
'EBICS API',
'EBICS API',
'manage_options',
'ebics-api',
'ebics_api_settings_page'
);
}
add_action( 'admin_menu', 'ebics_api_add_admin_menu' );
/**
* Register settings and fields.
*/
function ebics_api_settings_init() {
register_setting(
'ebics_api_settings_group',
'ebics_api_settings',
'ebics_api_settings_sanitize'
);
add_settings_section(
'ebics_api_general_section',
__( 'General Settings', 'ebics-api' ),
'ebics_api_general_section_callback',
'ebics-api'
);
add_settings_field(
'api_host',
__( 'API Host', 'ebics-api' ),
'ebics_api_api_host_callback',
'ebics-api',
'ebics_api_general_section'
);
add_settings_field(
'api_key',
__( 'API Key', 'ebics-api' ),
'ebics_api_api_key_callback',
'ebics-api',
'ebics_api_general_section'
);
}
add_action( 'admin_init', 'ebics_api_settings_init' );
/**
* Sanitize callback for EBICS API settings.
*/
function ebics_api_settings_sanitize( $input ) {
$new_input = [];
if ( isset( $input['api_host'] ) ) {
$new_input['api_host'] = esc_url_raw( rtrim( $input['api_host'], '/' ) );
}
if ( isset( $input['api_key'] ) ) {
$new_input['api_key'] = sanitize_text_field( $input['api_key'] );
}
return $new_input;
}
/**
* Section callback for general settings.
*/
function ebics_api_general_section_callback() {
echo '<p>' . esc_html__( 'Base URL of EBICS API service and authentication key.', 'ebics-api' ) . '</p>';
}
/**
* Render callback for API Host field.
*/
function ebics_api_api_host_callback() {
$options = get_option( 'ebics_api_settings' );
$api_host = $options['api_host'] ?? '';
echo '<input type="url" id="api_host" name="ebics_api_settings[api_host]" value="' . esc_attr( $api_host ) . '" class="regular-text" required />';
echo '<p class="description">' . esc_html__( 'Base URL of EBICS API service.', 'ebics-api' ) . '</p>';
}
/**
* Render callback for API Key field.
*/
function ebics_api_api_key_callback() {
$options = get_option( 'ebics_api_settings' );
$api_key = $options['api_key'] ?? '';
echo '<input type="text" id="api_key" name="ebics_api_settings[api_key]" value="' . esc_attr( $api_key ) . '" class="regular-text" required />';
echo '<p class="description">' . esc_html__( 'Authentication key for EBICS API.', 'ebics-api' ) . '</p>';
}
/**
* Renders the settings page with tabs.
*/
function ebics_api_settings_page() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$active_tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : 'settings';
?>
<div class="wrap">
<h1><?php esc_html_e( 'EBICS API', 'ebics-api' ); ?></h1>
<h2 class="nav-tab-wrapper">
<a href="?page=ebics-api&tab=settings" class="nav-tab <?php echo $active_tab === 'settings' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Settings', 'ebics-api' ); ?></a>
<a href="?page=ebics-api&tab=connections" class="nav-tab <?php echo $active_tab === 'connections' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Connections', 'ebics-api' ); ?></a>
<a href="?page=ebics-api&tab=transaction" class="nav-tab <?php echo $active_tab === 'transaction' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Transaction', 'ebics-api' ); ?></a>
<a href="?page=ebics-api&tab=logs" class="nav-tab <?php echo $active_tab === 'logs' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Logs', 'ebics-api' ); ?></a>
</h2>
<div class="tab-content">
<?php
switch ( $active_tab ) {
case 'connections':
ebics_api_render_connections_tab();
break;
case 'transaction':
ebics_api_render_transaction_tab();
break;
case 'logs':
ebics_api_render_logs_tab();
break;
case 'settings':
default:
ebics_api_render_settings_tab();
break;
}
?>
</div>
</div>
<?php
}
/**
* Renders the content for the Settings tab.
*/
function ebics_api_render_settings_tab() {
?>
<form method="post" action="options.php">
<?php
settings_fields( 'ebics_api_settings_group' );
do_settings_sections( 'ebics-api' );
submit_button();
?>
</form>
<?php
}
/**
* Renders the content for the Connections tab.
*/
function ebics_api_render_connections_tab() {
echo '<h3>' . esc_html__( 'Connections', 'ebics-api' ) . '</h3>';
try {
$client = Ebics_API_Client_Service::get_client();
$connections = $client->connectionList()->body;
if ( empty( $connections ) ) {
echo '<p>' . esc_html__( 'No connections found.', 'ebics-api' ) . '</p>';
return;
}
?>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th scope="col" class="manage-column"><?php esc_html_e( 'ID', 'ebics-api' ); ?></th>
<th scope="col" class="manage-column"><?php esc_html_e( 'Name', 'ebics-api' ); ?></th>
<th scope="col" class="manage-column"><?php esc_html_e( 'User ID', 'ebics-api' ); ?></th>
<th scope="col" class="manage-column"><?php esc_html_e( 'Status', 'ebics-api' ); ?></th>
<th scope="col" class="manage-column"><?php esc_html_e( 'Created At', 'ebics-api' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $connections as $connection ) : ?>
<tr>
<td><?php echo esc_html( $connection['id'] ?? '' ); ?></td>
<td><?php echo esc_html( $connection['name'] ?? '' ); ?></td>
<td><?php echo esc_html( $connection['user_id'] ?? '' ); ?></td>
<td><?php echo esc_html( $connection['keyring_status'] ?? '' ); ?></td>
<td><?php echo esc_html( $connection['created_at'] ?? '' ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
} catch ( \Exception $e ) {
/* translators: %s: Error message */
echo '<div class="notice notice-error"><p>' . sprintf( esc_html__( 'Unexpected error: %s', 'ebics-api' ), esc_html( $e->getMessage() ) ) . '</p></div>';
}
}
/**
* Renders the content for the Transaction tab.
*/
function ebics_api_render_transaction_tab() {
global $ebics_api_transaction_page;
$ebics_api_transaction_page->render();
}
/**
* Renders the content for the Logs tab.
*/
function ebics_api_render_logs_tab() {
echo '<h3>' . esc_html__( 'Logs', 'ebics-api' ) . '</h3>';
try {
$client = Ebics_API_Client_Service::get_client();
$limit = 10;
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
$current_page = isset( $_GET['paged'] ) ? absint( wp_unslash( $_GET['paged'] ) ) : 1;
$current_page = max( 1, intval( $current_page ) );
$logs_response = $client->accessLogList( [
'page' => $current_page,
'limit' => $limit,
] );
$logs = $logs_response->body;
$x_total = $logs_response->headers['x-total'] ?? 0;
$total_items = is_array( $x_total ) ? (int) ( $x_total[0] ?? 0 ) : (int) $x_total;
$total_pages = ceil( $total_items / $limit );
if ( empty( $logs ) ) {
echo '<p>' . esc_html__( 'No logs found.', 'ebics-api' ) . '</p>';
return;
}
?>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th scope="col" class="manage-column"><?php esc_html_e( 'ID', 'ebics-api' ); ?></th>
<th scope="col" class="manage-column"><?php esc_html_e( 'C#', 'ebics-api' ); ?></th>
<th scope="col" class="manage-column"><?php esc_html_e( 'Request', 'ebics-api' ); ?></th>
<th scope="col" class="manage-column"><?php esc_html_e( 'Response', 'ebics-api' ); ?></th>
<th scope="col" class="manage-column"><?php esc_html_e( 'Created At', 'ebics-api' ); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ( $logs as $log ) : ?>
<tr>
<td><?php echo esc_html( $log['id'] ?? '' ); ?></td>
<td><?php echo esc_html( $log['ebics_connection_id'] ?? '' ); ?></td>
<td><?php echo esc_html( $log['request_message'] ?? '' ); ?></td>
<td><?php echo esc_html( $log['response_message'] ?? '' ); ?></td>
<td><?php echo esc_html( $log['created_at'] ?? '' ); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<div class="tablenav bottom">
<div class="tablenav-pages">
<?php
echo wp_kses_post( paginate_links( [
'base' => add_query_arg( 'paged', '%#%' ),
'format' => '',
'prev_text' => __( '«', 'ebics-api' ),
'next_text' => __( '»', 'ebics-api' ),
'total' => $total_pages,
'current' => $current_page,
] ) );
?>
</div>
</div>
<?php
} catch ( \Exception $e ) {
/* translators: %s: Error message */
echo '<div class="notice notice-error"><p>' . sprintf( esc_html__( 'Unexpected error: %s', 'ebics-api' ), esc_html( $e->getMessage() ) ) . '</p></div>';
}
}