forked from adam-cowley/neo4j-wordpress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Admin.php
188 lines (157 loc) · 4.74 KB
/
Admin.php
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
<?php
namespace Neopress;
use Exception;
use GraphAware\Neo4j\Client\Client;
class Admin {
/**
* Register Admin Menus and Hooks
*
* @return void
*/
public static function init() {
static::registerSettings();
}
/**
* Register Connection Settings
*
* @return void
*/
private static function registerSettings() {
register_setting('neopress_connection', 'neopress_username');
register_setting('neopress_connection', 'neopress_password');
register_setting('neopress_connection', 'neopress_host');
register_setting('neopress_connection', 'neopress_port');
register_setting('neopress_connection', 'neopress_bolt_port');
add_settings_section(
'neopress_connection',
__('Connection Settings', 'neopress'),
static::class .'::checkConnectionStatus',
'neopress'
);
}
/**
* Check Connection and display statistics
*
* @return void
*/
public static function checkConnectionStatus() {
$class = 'error';
$message = '<p><strong>Could not connect to Neo4j. Please check your connection settings.</strong></p>';
try {
$result = Neopress::client()->run('MATCH (n) RETURN count(n) as count');
$class = 'updated';
$message = sprintf('<p><strong>Connection Successful.</strong></p><p>There are <strong>%d</strong> nodes in your database', $result->getRecord()->get('count'));
}
catch (Exception $e) {
$message .= '<p>'. $e->getMessage() .'</p>';
}
printf('<div id="neopress-response" class="%s">%s</strong></div>', $class, $message);
}
/**
* Register Configuration Menu
*
* @return void
*/
public static function menu() {
add_options_page(
__("Neo4j Connection Settings", 'neopress'),
__("Neopress", 'neopress'),
'manage_options',
'neopress',
static::class .'::menuConnection'
);
$options = [
'neopress_username' => 'Username',
'neopress_password' => 'Password',
'neopress_host' => 'Host',
'neopress_port' => 'HTTP Port',
'neopress_bolt_port' => 'Bolt Port',
];
foreach ($options as $key => $label) {
add_settings_field(
$key,
__($label, 'neopress'),
static::class .'::option_'. $key,
'neopress',
'neopress_connection'
);
}
}
/**
* Display HTML for Username input
*
* @return void
*/
public static function option_neopress_username() {
printf(
'<input type="text" id="neopress_username" name="neopress_username" value="%s" />',
get_option('neopress_username')
);
}
/**
* Display HTML for Password input
*
* TODO: Encrypt authentication details in database
*
* @return void
*/
public static function option_neopress_password() {
printf(
'<input type="password" id="neopress_password" name="neopress_password" value="%s" />',
get_option('neopress_password')
);
}
/**
* Display HTML for Host input
*
* @return void
*/
public static function option_neopress_host() {
printf(
'<input type="text" id="neopress_host" name="neopress_host" value="%s" />',
get_option('neopress_host')
);
}
/**
* Display HTML for Port Input
*
* @return void
*/
public static function option_neopress_port() {
printf(
'<input type="number" id="neopress_port" name="neopress_port" value="%s" />',
get_option('neopress_port', 7474)
);
}
/**
* Display HTML for Bolt Port Input
*
* @return void
*/
public static function option_neopress_bolt_port() {
printf(
'<input type="number" id="neopress_bolt_port" name="neopress_bolt_port" value="%s" />',
get_option('neopress_bolt_port', 7876)
);
}
/**
* Display HTML for Connection Options Page
*
* @return void
*/
public static function menuConnection() {
?>
<div class="wrap">
<h1><?php echo __("Neo4j Connection Settings", 'neopress'); ?></h1>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'neopress_connection' );
do_settings_sections( 'neopress' );
submit_button();
?>
</form>
</div>
<?php
}
}