-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathknvb-api-plugin.php
More file actions
189 lines (157 loc) · 5.69 KB
/
knvb-api-plugin.php
File metadata and controls
189 lines (157 loc) · 5.69 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
<?php
/**
* Plugin Name: KNVB Api
* Plugin URI: http://www.hoest.nl
* Description: A plugin to use the KNVB Data API
* Version: 1.13
* Author: Jelle de Jong
* Author URI: http://www.hoest.nl
* */
// include the KnvbClient class
include 'knvb-client.php';
/***********************************************************************
Registreer [knvb ...]
*/
function knvb_shortcode($atts) {
$client = new KnvbClient(get_option('knvb_api_key'),
get_option('knvb_api_pathname'),
get_option('knvb_api_clubname'));
extract(shortcode_atts(array(
'uri' => 'uri',
'extra' => 'extra',
'template' => 'template',
), $atts));
return '<div class="knvb">'.$client->getData($uri, $extra, $template).'</div>';
}
add_shortcode("knvb", "knvb_shortcode");
/***********************************************************************
Registreer [knvbteam ...]
*/
function knvbteam_shortcode($atts) {
$client = new KnvbClient(get_option('knvb_api_key'),
get_option('knvb_api_pathname'),
get_option('knvb_api_clubname'));
extract(shortcode_atts(array(
'list' => 'list',
), $atts));
$output = '';
if(isset($list) && count(explode(';', $list)) > 0) {
foreach(explode(';', $list) as $teamId) {
$output = $output.'<div class="team">';
$output = $output.'<div class="team-results">'.$client->getData('/teams/'.$teamId.'/results', 'weeknummer=A').'</div>';
$output = $output.'<div class="team-ranking">'.$client->getData('/teams/'.$teamId.'/ranking').'</div>';
$output = $output.'<div class="team-schedule">'.$client->getData('/teams/'.$teamId.'/schedule', 'weeknummer=A').'</div>';
$output = $output.'</div>';
}
}
return '<div class="knvbteam">'.$output.'</div>';
}
add_shortcode("knvbteam", "knvbteam_shortcode");
/***********************************************************************
Registreer [knvbteam-slider ...]
*/
function knvbteam_slider_shortcode($atts) {
$client = new KnvbClient(get_option('knvb_api_key'),
get_option('knvb_api_pathname'),
get_option('knvb_api_clubname'));
extract(shortcode_atts(array(
'id' => 'id',
'extra' => 'extra',
), $atts));
$output = '';
if(isset($id)) {
$output = $output.$client->getData('/teams/'.$id.'/schedule', 'weeknummer=C&slider=1&'.$extra);
$output = $output.$client->getData('/teams/'.$id.'/results', 'weeknummer=A&slider=1&'.$extra);
}
return '<div class="knvbteam-slider">'.$output.'</div>';
}
add_shortcode("knvbteam-slider", "knvbteam_slider_shortcode");
/***********************************************************************
Voeg een optie-scherm toe
*/
function knvb_api_menu() {
add_options_page('KNVB API Opties',
'KNVB API',
'manage_options',
'knvb-api-menu',
'knvb_api_options');
}
/***********************************************************************
De inhoud van het optie-scherm
*/
function knvb_api_options() {
?>
<div class="wrap">
<h2>KNVB API Opties</h2>
<form method="post" action="options.php">
<?php settings_fields('knvb-api-settings-group'); ?>
<?php do_settings_sections('knvb-api-settings-group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">API sleutel</th>
<td>
<input type="text" name="knvb_api_key" value="<?php echo esc_attr(get_option('knvb_api_key')); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">Pathname</th>
<td>
<input type="text" name="knvb_api_pathname" value="<?php echo esc_attr(get_option('knvb_api_pathname')); ?>" />
</td>
</tr>
<tr valign="top">
<th scope="row">Clubnaam (volgens KNVB-site)</th>
<td>
<input type="text" name="knvb_api_clubname" value="<?php echo esc_attr(get_option('knvb_api_clubname')); ?>" />
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<h2>Alle teams</h2>
<?php
$cache_folder = plugin_dir_path(__FILE__).'cache/';
// create cache folder
if (!file_exists($cache_folder)) {
mkdir($cache_folder, 0777);
}
// emtpy cache folder
$files = glob($cache_folder.'*.rtpl.php'); // get all file names
foreach($files as $file) { // iterate files
if(is_file($file)) {
unlink($file); // delete file
echo '<p>Cache removed: <code>'.$file.'</code></p>';
}
}
// create a client and receive data
$client = new KnvbClient(get_option('knvb_api_key'), get_option('knvb_api_pathname'), get_option('knvb_api_clubname'));
$knvb_data = trim($client->getData('/teams', NULL, NULL, false));
if(!empty($knvb_data)) {
$dt = new DateTime('now');
$dt->setTimezone(new DateTimeZone('Europe/Amsterdam'));
echo '<p><em>Cache geleegd en data vernieuwd op '.$dt->format('d-m-Y \o\m H:i:s').'</em></p>';
echo $knvb_data;
}
else {
echo '<p>Zodra de bovenstaande settings correct zijn ingevoerd, verschijnt hier een overzicht van alle teams.</p>';
}
?>
</div>
<?php
}
/***********************************************************************
Registreerd de API-settings
*/
function knvb_api_regiter_settings() { // whitelist options
register_setting('knvb-api-settings-group', 'knvb_api_key');
register_setting('knvb-api-settings-group', 'knvb_api_pathname');
register_setting('knvb-api-settings-group', 'knvb_api_clubname');
}
/***********************************************************************
Admin init functie
*/
if(is_admin()) {
add_action('admin_menu', 'knvb_api_menu');
add_action('admin_init', 'knvb_api_regiter_settings');
}
?>