This repository has been archived by the owner on Jan 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspaceapi.php
324 lines (280 loc) · 11.3 KB
/
spaceapi.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
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
<?php
/*
Plugin Name: Spaceapi
Plugin URI: http://fholzhauer.de
Description: Generate a space api file
Version: 0.1.2
Author: Florian Holzhauer
License: GPL
*/
/*
* This plugin is pretty much an ugly proof of concept.
*
* The basic idea is to add direct rendering and validation of the settings from the json api spec, which would be way nicer and somewhat useful
*
*/
if (!function_exists('add_action')) {
//Direct access, no WP call
header('HTTP/1.1 403 Forbidden');
die();
}
if (!class_exists('fh_wpplugin_spaceapi')) {
define('FH_WPPLUGIN_SPACEAPI_FORMPREFIX', 'fh_wpplugin_spaceapi');
class fh_wpplugin_spaceapi
{
var $base = false;
var $folder = false;
const VERSION = '0.1.2';
/**
* Identifier-Prefix for Forms and Settings
* @var string
*/
const PREFIX = FH_WPPLUGIN_SPACEAPI_FORMPREFIX;
private $textfields = array(
'name' => 'The name of your space <b>mandatory</b>',
'logo' => 'URL to your space logo <b>mandatory</b>',
'url' => 'URL to your space website *',
'location' => array(
'address' => 'The postal address of your space',
'lat' => 'Latitude (degree with decimal places, positive=N, negative=S) *',
'lon' => 'Longitude (degree with decimal places, positive=W, negative=E) *',
),
'contact' => array(
'phone' => 'Phone number, including country code with a leading plus sign.',
'sip' => 'URI for Voice-over-IP via SIP.',
'irc' => 'URL of the IRC channel, in the form irc://example.org/#channelname',
'twitter' => 'Twitter handle, with leading @',
'facebook' => 'Facebook',
'identica' => 'Identi.ca or StatusNet account, in the form [email protected]',
'foursquare' => 'Foursquare ID, in the form 4d8a9114d85f3704eab301dc.',
'email' => 'E-mail address for contacting your space. If this is a mailing list consider to use the contact/ml field.',
'ml' => 'The e-mail address of your mailing list.',
'jabber' => 'A public Jabber/XMPP multi-user chatroom in the form [email protected]',
'issue' => 'A seperate email address for issue reports (see the issue_report_channels field).',
)
);
public function __construct()
{
$this->base = plugin_basename(__FILE__);
$this->folder = dirname($this->base);
add_action('init', array(&$this, 'init'));
add_action('generate_rewrite_rules', array(&$this, 'generateRewriteRules'));
add_filter('query_vars', array(&$this, 'queryVars'));
add_action('parse_request', array(&$this, 'parseRequest'));
add_action('plugins_loaded', array(&$this, 'onStart'));
add_action('admin_menu', array(&$this, 'addAdminMenu'));
//WARNING: Prio has to be set, or it wont work - see http://stackoverflow.com/questions/1580378/plugin-action-links-not-working-in-wordpress-2-8
add_filter('plugin_action_links', array(&$this, 'addActionLinks'), 10, 2);
}
public function init()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
public function generateRewriteRules()
{
//http://stackoverflow.com/questions/13140182/wordpress-wp-rewrite-rules
global $wp_rewrite;
$new_rules = array(
'(spaceapi.json)?$' => 'index.php?spaceapi=show'
);
// Always add your rules to the top, to make sure your rules have priority
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
/*
* Renders Admin interface, stores settings
* @todo i18n
*/
public function adminInterface()
{
if (isset($_POST['submitted'])) {
foreach ($_POST as $key => $value) {
$l = strlen(self::PREFIX . '_s_');
if (substr($key, 0, $l) == self::PREFIX . '_s_') {
update_option($key, trim($value));
}
}
}
$storedValues = array();
foreach ($this->textfields as $key => $value) {
if (is_array($value)) {
foreach ($value as $innerKey => $innerValue) {
$id = $key . '_' . $innerKey;
$storedValues[$id] = get_option(self::PREFIX . '_s_' . $id);
}
} else {
$storedValues[$key] = get_option(self::PREFIX . '_s_' . $key);
}
}
$inner = $this->innerAdminInterfaceHtml($storedValues);
echo $this->adminInterfaceHtml($inner, 'Space Api Config');
}
/**
* Inits Admin-Hooks, adds Settings-Link in Backend
*/
public function addAdminMenu()
{
add_options_page(
'Space Api Options',
'Space Api',
'manage_options',
self::PREFIX,
array(&$this, 'adminInterface')
);
}
public function addActionLinks($action_links, $plugin_file)
{
$this_file = basename(__FILE__);
if (substr($plugin_file, -strlen($this_file)) == $this_file) {
$new_action_links = array(
"<a href='options-general.php?page=" . self::PREFIX . "'>Settings</a>"
);
return $new_action_links;
}
return $action_links;
}
public function onStart()
{
if (is_admin()) {
if (get_option(self::PREFIX . 'warnings')) {
add_action(
'admin_notices',
create_function(
'',
'echo \'<div id="message" class="error"><p>' . get_option(
self::PREFIX . 'warnings'
) . '</p></div>\';'
)
);
}
}
}
public function parseRequest($wp)
{
if (array_key_exists('spaceapi', $wp->query_vars)
&& $wp->query_vars['spaceapi'] == 'show'
) {
header('Content-Type: application/json');
$data = array();
foreach ($this->textfields as $key => $value) {
if (is_array($value)) {
$tmp = array();
foreach ($value as $innerKey => $innerValue) {
$id = $key . '_' . $innerKey;
$databaseValue = get_option(self::PREFIX . '_s_' . $id);
if (!empty($databaseValue)) {
$tmp[$innerKey] = $databaseValue;
}
}
$data[$key] = $tmp;
} else {
$databaseValue = get_option(self::PREFIX . '_s_' . $key);
if (!empty($databaseValue)) {
$data[$key] = $databaseValue;
}
}
}
$data['api'] = '0.12';
$data['open'] = null;
$data = apply_filters('spaceapi_data_result',$data);
echo json_encode($data);
die();
}
}
public function queryVars()
{
return array('spaceapi');
}
public function adminInterfaceHtml(
$inner = '',
$title = 'Admin Menu',
$submitbutton = 'Save »'
) {
$return = <<<EOF
<div class="wrap">
<h2>$title</h2>
<form id="settings" action="" method="post">
<p><b>Warning: No field validation!</b></p>
<p>This plugin is <b>deprecated</b>. Please consider disabling it, and to use the <a href="https://wordpress.org/plugins/hackerspace/">hackerspace plugin</a> instead.</p>
$inner
<p class="submit">
<input type="hidden" name="submitted" />
<input type="submit" name="Submit" class="button-primary" value="$submitbutton" />
</p>
</form>
</div>
EOF;
return $return;
}
public function innerAdminInterfaceHtml(
$data = array()
) {
/*
* This is a very stupid initial version to get the plugin started.
* The idea here is to remove all those hard coded fields and do a direct
* rendering of the schema.json files - which is way cooler.
*
* Since this is only a temporary solution, I did not put too much effort in it,
* hence the "array" fields are not supported.
*/
$str = '<table class="form-table"><tbody>';
//issue report channel
foreach ($this->textfields as $name => $descr) {
if (is_array($descr)) {
continue;
}
$str .= $this->trStringInput(
$descr,
$name,
array_key_exists($name, $data) ? $data[$name] : ''
);
}
$str .= "</tbody></table>\n<h3>Contact:</h3>\n<table class='form-table'><tbody>\n";
foreach ($this->textfields['contact'] as $name => $descr) {
$str .= $this->trStringInput(
$descr,
'contact_' . $name,
array_key_exists('contact_' . $name, $data) ? $data['contact_' . $name] : ''
);
}
$str .= "</tbody></table>\n<h3>Location:</h3>\n<table class='form-table'><tbody>\n";
foreach ($this->textfields['location'] as $name => $descr) {
$str .= $this->trStringInput(
$descr,
'location_' . $name,
array_key_exists('location_' . $name, $data) ? $data['location_' . $name] : ''
);
}
$str .= '</tbody></table>';
return $str;
}
public function trStringInput(
$description,
$name,
$value
) {
$str = '<tr><th scope="row" width="33%"><label for="' . FH_WPPLUGIN_SPACEAPI_FORMPREFIX . '_s_' . $name . '">';
$pseudotitle = explode('_', $name);
//oh boy, this is sooo ugly :)
foreach ($pseudotitle as $t) {
$str .= ucfirst($t) . ' ';
}
$str = substr($str, 0, -1);
$str .= ':';
$str .= '</label></th>';
$str .= '<td><input name="' . FH_WPPLUGIN_SPACEAPI_FORMPREFIX . '_s_' . $name . '" value="' . $value . '" />';
$str .= '<p class="need-' . $name . ' description">' . $description . '</p></td></tr>';
$str .= "\n";
return $str;
}
}
}
/**
* Global Init Funktion, called by wordpress on init.
* All other hooks and settings are set in the constructor of the object.
*/
function fh_wpplugin_spaceapi_init()
{
new fh_wpplugin_spaceapi();
}
add_action('plugins_loaded', 'fh_wpplugin_spaceapi_init');