-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpressable_api_connection.php
More file actions
173 lines (152 loc) · 6.43 KB
/
pressable_api_connection.php
File metadata and controls
173 lines (152 loc) · 6.43 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
<?php
/*
Plugin Name: Query Pressable API
Description: Demonstrating how to access the Pressable API using a simple WordPress plugin with settings page
Plugin URI: https://pressable.com/
Author: Pressable.com, tarhe
Version: 1.0
*/
// If this file is accessed directly, abort!
defined('ABSPATH') or die('Unauthorized Access');
/**
* Register settings for storing API credentials and request details.
*/
function pressable_api_register_settings() {
register_setting('pressable_api_options_group', 'pressable_api_client_id');
register_setting('pressable_api_options_group', 'pressable_api_client_secret');
register_setting('pressable_api_options_group', 'pressable_api_url');
register_setting('pressable_api_options_group', 'pressable_api_method');
}
add_action('admin_init', 'pressable_api_register_settings');
/**
* Add a settings page under "Settings".
*/
function pressable_api_settings_menu() {
add_options_page(
'Pressable API Settings',
'Pressable API',
'manage_options',
'pressable-api-settings',
'pressable_api_settings_page'
);
}
add_action('admin_menu', 'pressable_api_settings_menu');
/**
* Render the settings page.
*/
function pressable_api_settings_page() {
?>
<div class="wrap">
<h1>Pressable API Settings</h1>
<form method="post" action="options.php">
<?php settings_fields('pressable_api_options_group'); ?>
<?php do_settings_sections('pressable_api_options_group'); ?>
<table class="form-table">
<tr>
<th scope="row">Client ID</th>
<td><input type="text" name="pressable_api_client_id" value="<?php echo esc_attr(get_option('pressable_api_client_id')); ?>" class="regular-text" /></td>
</tr>
<tr>
<th scope="row">Client Secret</th>
<td><input type="password" name="pressable_api_client_secret" value="<?php echo esc_attr(get_option('pressable_api_client_secret')); ?>" class="regular-text" /></td>
</tr>
<tr>
<th scope="row">API Request URL</th>
<td><input type="url" name="pressable_api_url" value="<?php echo esc_attr(get_option('pressable_api_url')); ?>" class="regular-text" /></td>
</tr>
<tr>
<th scope="row">Request Method</th>
<td>
<select name="pressable_api_method">
<option value="GET" <?php selected(get_option('pressable_api_method'), 'GET'); ?>>GET</option>
<option value="POST" <?php selected(get_option('pressable_api_method'), 'POST'); ?>>POST</option>
<option value="PUT" <?php selected(get_option('pressable_api_method'), 'PUT'); ?>>PUT</option>
</select>
</td>
</tr>
</table>
<?php submit_button('Save Settings'); ?>
</form>
<?php if ( get_option('pressable_api_client_id') && get_option('pressable_api_client_secret') && get_option('pressable_api_url') ) : ?>
<hr/>
<form method="post">
<?php wp_nonce_field('pressable_api_test_nonce', 'pressable_api_test_nonce_field'); ?>
<input type="hidden" name="pressable_api_action" value="test_api_request" />
<?php submit_button('Test API Request', 'secondary'); ?>
</form>
<?php endif; ?>
</div>
<?php
}
/**
* Authenticate and request data from Pressable API.
*/
function pressable_api_get_send_data() {
$client_id = get_option('pressable_api_client_id');
$client_secret = get_option('pressable_api_client_secret');
$api_url = get_option('pressable_api_url');
$method = strtoupper(get_option('pressable_api_method', 'GET'));
if (!$client_id || !$client_secret || !$api_url) {
echo '<div class="notice notice-error"><p>Please configure your API settings first.</p></div>';
return;
}
// Check if token exists and not expired
$access_token = get_transient('pressable_access_token');
if (!$access_token) {
$response = wp_remote_post('https://my.pressable.com/auth/token', array(
'body' => array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'grant_type' => 'client_credentials',
),
));
if (is_wp_error($response)) {
echo '<div class="notice notice-error"><p>Token request failed: ' . esc_html($response->get_error_message()) . '</p></div>';
return;
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (empty($body['access_token'])) {
echo '<div class="notice notice-error"><p>Failed to retrieve access token. Check your credentials.</p></div>';
return;
}
$access_token = $body['access_token'];
set_transient('pressable_access_token', $access_token, $body['expires_in']);
}
// Build request args
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $access_token,
),
'method' => $method,
);
// Example payload for POST/PUT
if ($method === 'POST' || $method === 'PUT') {
$args['body'] = array(
'example_key' => 'example_value',
);
}
$request = wp_remote_request($api_url, $args);
if (is_wp_error($request)) {
echo '<div class="notice notice-error"><p>API request failed: ' . esc_html($request->get_error_message()) . '</p></div>';
return;
}
$results = json_decode(wp_remote_retrieve_body($request), true);
echo '<div class="notice notice-info" style="padding:15px; margin-top:20px;">';
echo '<h2>Pressable API Response</h2><pre>';
print_r($results);
echo '</pre></div>';
}
/**
* Handle "Test API Request" button action.
*/
add_action('admin_init', function() {
if ( isset($_POST['pressable_api_action']) && $_POST['pressable_api_action'] === 'test_api_request' ) {
if ( ! isset($_POST['pressable_api_test_nonce_field']) ||
! wp_verify_nonce($_POST['pressable_api_test_nonce_field'], 'pressable_api_test_nonce') ) {
wp_die('Security check failed');
}
add_action('admin_notices', function() {
pressable_api_get_send_data();
});
}
});