-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminController.php
More file actions
206 lines (177 loc) · 5.08 KB
/
AdminController.php
File metadata and controls
206 lines (177 loc) · 5.08 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
<?php
namespace SyncEngine\WordPress\Controller;
use SyncEngine\WordPress\Api\Client;
use SyncEngine\WordPress\Service\Singleton;
class AdminController extends Singleton
{
const CAPABILITY = 'syncengine';
protected $option_name = 'syncengine';
public function register() {
add_action( 'admin_menu', array( $this, 'action_admin_menu' ) );
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
}
public function action_admin_menu() {
$cap = self::CAPABILITY;
if ( ! is_multisite() && ! current_user_can( $cap ) ) {
$cap = 'manage_options';
}
add_submenu_page(
'tools.php',
__( 'SyncEngine', 'syncengine' ),
__( 'SyncEngine', 'syncengine' ),
$cap,
'syncengine',
array( $this, 'page' ),
);
}
public function action_admin_init() {
register_setting( 'syncengine', 'syncengine' );
$this->register_section_api();
//$this->register_section_hooks();
}
public function register_section_api() {
add_settings_section(
'api',
__( 'Connect to API' ),
array( $this, 'settings_api_section' ),
'syncengine'
);
add_settings_field(
'host',
__( 'Domain/Host' ),
array( $this, 'settings_api_field_input' ),
'syncengine',
'api',
[
'name' => 'host',
'placeholder' => __( 'https://' ),
'section' => 'api',
'setting' => $this->option_name,
]
);
add_settings_field(
'token',
__( 'Token' ),
array( $this, 'settings_api_field_input' ),
'syncengine',
'api',
[
'type' => is_super_admin() ? 'text' : 'password',
'name' => 'token',
'placeholder' => __( '#' ),
'section' => 'api',
'setting' => $this->option_name,
]
);
add_settings_field(
'auth_header',
__( 'Auth Header' ),
array( $this, 'settings_api_field_input' ),
'syncengine',
'api',
[
'name' => 'auth_header',
'placeholder' => __( 'Bearer token (default)' ),
'section' => 'api',
'setting' => $this->option_name,
]
);
/*
add_settings_field(
'version',
__( 'Version' ),
array( $this, 'settings_api_field_input' ),
'syncengine',
'api',
[
'type' => 'number',
'name' => 'version',
'placeholder' => __( '1' ),
'section' => 'api',
'setting' => $this->option_name,
]
);
*/
}
public function register_section_hooks() {
add_settings_section(
'hooks',
__( 'SyncEngine Hooks' ),
array( $this, 'settings_api_section' ),
'syncengine'
);
add_settings_field(
'hooks',
__( 'Hooks' ),
array( $this, 'settings_api_field_hooks' ),
'syncengine',
'hooks',
);
}
public function page() {
$settings = get_option( $this->option_name );
$url = remove_query_arg( 'settings-updated' );
$api_settings = $settings['api'] ?? [];
$api = new Client(
$api_settings['host'] ?? '',
$api_settings['token'] ?? '',
$api_settings,
);
if ( ! empty( $_GET['refresh'] ) || ! empty( $_GET['settings-updated'] ) ) {
$api->clearCache();
$url = remove_query_arg( 'refresh', $url );
}
if ( ! empty( $_GET['execute_endpoint'] ) ) {
$result = $api->executeEndpoint( $_GET['execute_endpoint'] );
$url = remove_query_arg( 'execute_endpoint', $url );
}
$status = $api->status();
$endpoints = $api->listEndpoints();
?>
<div class="wrap">
<form action='options.php' method='post'>
<h1>
<img src="<?= \SyncEngine::get_url() . 'assets/img/icon.svg' ?>" alt="SyncEngine" style="width: 1.2em;height: 1.2em;display: inline-block;vertical-align: bottom;margin-right: .2em;"/>
<?= __( 'SyncEngine', 'syncengine' ) ?>
</h1>
<?php
settings_fields( 'syncengine' );
do_settings_sections( 'syncengine' );
submit_button();
?>
</form>
<p>Status: <?= $status ?></p>
<a class="button" href="<?= add_query_arg( 'refresh', true, $url ) ?>">Refresh</a>
<?php if ( $api->isOnline() && $endpoints ): ?>
<div>
<h2><?= __( 'Run automations manually', 'syncengine' ) ?></h2>
<?php foreach ( $endpoints as $endpoint ): ?>
<a class="button" href="<?= add_query_arg( 'execute_endpoint', $endpoint['endpoint'], $url ) ?>"><?= $endpoint['name'] ?></a>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ( ! empty( $result ) ): ?>
<div>
<h2><?= __( 'Execute results', 'syncengine' ) ?></h2>
<div class="code" style="background: #fff; padding: 1em;">
<pre style="margin: 0"><?= json_encode( $result, JSON_PRETTY_PRINT ) ?></pre>
</div>
</div>
<?php endif; ?>
</div>
<?php
}
public function settings_api_section( $args ) {
return $args['title'] ?? '';
}
public function settings_api_field_input( $args ) {
$type = $args['type'] ?? 'text';
$options = get_option( $args['setting'] );
$id = $args['setting'] . '_' . $args['section'] . '_' . $args['name'];
$name = $args['setting'] . '[' . $args['section'] . ']' . '[' . $args['name'] . ']';
$value = $options[ $args['section'] ][ $args['name'] ] ?? '';
?>
<input id="<?= $id ?>" type="<?= $type ?>" name="<?= $name; ?>" value="<?= $value; ?>" placeholder="<?= $args['placeholder'] ?? $args['label'] ?? $args['title'] ?>" />
<?php
}
}