This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclass-settings.php
194 lines (170 loc) · 6.32 KB
/
class-settings.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
186
187
188
189
190
191
192
193
194
<?php
namespace KymaProject\WordPressConnector;
class Settings
{
private $connector;
private $event_settings;
private $option_group;
private $page_name;
public function __construct($page_name, $option_group, Connector $connector, EventSettings $event_settings)
{
$this->connector = $connector;
$this->event_settings = $event_settings;
$this->option_group = $option_group;
$this->page_name = $page_name;
}
public function addSettingsPage()
{
add_options_page(
'Kyma Connector Settings',
'Kyma Connector',
'manage_options',
$this->page_name,
array($this, 'echoSettingsPage')
);
}
public function enqueueScripts($hook)
{
if ($hook !== 'settings_page_' . $this->page_name) {
return;
}
wp_enqueue_script('kyma-settings', Core::$scriptUrl . 'settings.js', array('jquery'));
wp_localize_script('kyma-settings', 'kyma_ajax_vars', array(
'connectnonce' => wp_create_nonce('kymaconnection'),
'disconnectnonce' => wp_create_nonce('kymadisconnection')
));
}
public function registerSettings()
{
add_settings_section(
'kymaconnector_settings',
'Setup',
null,
$this->page_name
);
add_settings_field(
'kymaconnector-setup',
'Kyma Connection',
array($this, 'echoFieldConnection'),
$this->page_name,
'kymaconnector_settings'
);
add_settings_field(
'kymaconnector-ssl',
'Verify SSL',
array($this, 'echoFieldSslVerify'),
$this->page_name,
'kymaconnector_settings'
);
add_settings_section(
'kymaconnector_api_settings',
'API Registration Settings',
function () {
echo '<p>API Registration details.</p>';
},
$this->page_name
);
add_settings_field(
'kymaconnector_user',
'Wordpress API User Name',
array($this, 'field_user_cb'),
$this->page_name,
'kymaconnector_api_settings'
);
add_settings_field(
'kymaconnector_password',
'Wordpress API User Password',
array($this, 'field_password_cb'),
$this->page_name,
'kymaconnector_api_settings'
);
add_settings_field(
'kymaconnector_name',
'Connector Name',
array($this, 'field_name_cb'),
$this->page_name,
'kymaconnector_api_settings'
);
add_settings_field(
'kymaconnector_description',
'Connector Description',
array($this, 'field_description_cb'),
$this->page_name,
'kymaconnector_api_settings'
);
register_setting($this->option_group, 'kymaconnector_sslverify');
register_setting($this->option_group, 'kymaconnector_user');
register_setting($this->option_group, 'kymaconnector_password');
register_setting($this->option_group, 'kymaconnector_name');
register_setting($this->option_group, 'kymaconnector_description');
add_option('kymaconnector_name', 'Wordpress');
add_option('kymaconnector_description', 'Wordpress API');
add_option('kymaconnector_sslverify', 1);
$this->event_settings->settings_page();
}
public function echoFieldConnection()
{
$connected = $this->connector->isConnected();
if ($connected === true) {
echo '<p class="notice notice-success">Connection to Kyma works</p>';
echo '<input type="button" id="kymadisconnectbtn" class="button" value="Disconnect">';
} elseif (is_wp_error($connected)) {
echo '<p class="notice notice-error">An error ocurred while checking the connection to Kyma: ' . esc_html($connected->get_error_message()) . '</p>';
} else {
echo '<input type="url" id="kyma-connect-url">';
echo '<input type="button" id="kymaconnectbtn" class="button" value="Connect">';
}
}
public function echoFieldSslVerify()
{
echo '<input name="kymaconnector_sslverify" id="kymaconnector_sslverify" type="checkbox" value="1" class="code" ' . checked( 1, get_option( 'kymaconnector_sslverify' ), false ) . ' />';
}
public function echoSettingsPage()
{
if (!current_user_can('manage_options')) {
wp_die(__('You do not have sufficient permissions to manage options for this site.'));
}
// In case settings were updated
if (get_option('kymaconnector_events_updated', '0') === '1') {
if (!empty(get_option('kymaconnector_metadata_url'))) {
Connector::register_application($this->event_settings->get_event_spec());
update_option('kymaconnector_events_updated', '0');
}
}
// show error/update messages
settings_errors( 'kymaconnector_messages' );
?>
<div class="wrap">
<h1>Kyma Connector Settings</h1>
<div id="kymanotices"></div>
<form method="post" action="options.php">
<?php
settings_fields($this->option_group);
do_settings_sections($this->page_name);
submit_button();
?>
</form>
</div>
<?php
}
public function field_password_cb()
{
$setting = get_option('kymaconnector_password', '');
printf('<input type="password" name="kymaconnector_password" value="%s">', esc_attr( $setting ));
}
public function field_user_cb()
{
$setting = get_option('kymaconnector_user', '');
printf('<input type="text" name="kymaconnector_user" value="%s">', esc_attr( $setting ));
}
public function field_name_cb()
{
$setting = get_option('kymaconnector_name', '');
printf('<input type="text" name="kymaconnector_name" value="%s">', esc_attr( $setting ));
}
public function field_description_cb()
{
$setting = get_option('kymaconnector_description', '');
printf('<textarea name="kymaconnector_description" rows="5" cols="50">%s</textarea>', esc_textarea( $setting ));
}
}