-
Notifications
You must be signed in to change notification settings - Fork 2
/
page-options.php
223 lines (162 loc) · 7.65 KB
/
page-options.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
<?php
/**
* page-options.php
*
* Displays options for the beeminder ping tool. Used to set a username/token,
* as well as choose which post-publish hooks to use.
*
*/
$errors = null;
$updateMessage = '';
// Update options
if (isset($_POST['submit'])) {
// Fetch goals from Beeminder.com
$api = new Beeminder_Client();
$api->login($_POST['beeminder_username'], $_POST['beeminder_key']);
// Try fetching goals
try {
$goals = $api->getGoalApi()->getGoals();
// If goals valid, save them (and auth info)
update_option('beeminder_ping_goals', $goals);
update_option('beeminder_ping_username', $_POST['beeminder_username']);
update_option('beeminder_ping_key', $_POST['beeminder_key']);
$updateMessage = 'Settings saved';
}
catch (Exception $e) {
$errors = array('Invalid key or username');
}
}
if (isset($_POST['refresh'])) {
// Fetch goals from Beeminder.com
$api = new Beeminder_Client();
$api->login(get_option('beeminder_ping_username'), get_option('beeminder_ping_key'));
// Update option
update_option('beeminder_ping_goals', $api->getGoalApi()->getGoals());
$updateMessage = 'Goals Updated';
}
// Clear settings
if (isset($_POST['clear'])) {
delete_option('beeminder_ping_username');
delete_option('beeminder_ping_key');
delete_option('beeminder_ping_goals');
delete_option('beeminder_ping_post_enabled');
delete_option('beeminder_ping_post_goal');
delete_option('beeminder_ping_wordcount_enabled');
delete_option('beeminder_ping_wordcount_goal');
// Clear post values
unset($_POST);
$updateMessage = 'All settings cleared';
}
if (isset($_POST['update-hooks'])) {
// TODO: Check goals are valid
// Save options
update_option('beeminder_ping_post_enabled', $_POST['beeminder_single_enabled']);
update_option('beeminder_ping_wordcount_enabled', $_POST['beeminder_wordcount_enabled']);
update_option('beeminder_ping_post_goal', $_POST['beeminder_single_post']);
update_option('beeminder_ping_wordcount_goal', $_POST['beeminder_wordcount_post']);
$updateMessage = 'Hooks Updated';
}
$beeminder_username = get_option('beeminder_ping_username', $_POST['beeminder_username']);
$beeminder_key = get_option('beeminder_ping_key', $_POST['beeminder_key']);
$beeminder_goals = get_option('beeminder_ping_goals', null);
// Options
$beeminder_post_enabled = get_option('beeminder_ping_post_enabled', null);
$beeminder_wordcount_enabled = get_option('beeminder_ping_wordcount_enabled', null);
$beeminder_post_goal = get_option('beeminder_ping_post_goal', null);
$beeminder_wordcount_goal = get_option('beeminder_ping_wordcount_goal', null);
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br /></div>
<h2>Beeminder Ping Settings</h2>
<?php if ( isset($errors) && count($errors) > 0) { ?>
<div id="message" class="error fade"><p><strong>Error during update operation:</strong></p><ol><?php foreach($errors as $error) { echo "<li>$error</li>"; } ?></ol></div>
<?php } ?>
<?php if ($updateMessage) { ?>
<div id="message" class="updated fade"><p><strong><?php _e($updateMessage); ?></strong></p></div>
<?php } ?>
<div id="beeminder-ping-settings">
<h3>API Settings</h3>
<p>
Once you have saved your details you will be able to setup post
pings. You can get your API token by logging in to Beeminder and
visiting the following url:<br /> <code><a
href="https://www.beeminder.com/api/v1/auth_token.json">https://www.beeminder.com/api/v1/auth_token.json</a></code>
</p>
<form method="POST" action="">
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row"><label for="beeminder_username">Beeminder Username</label></th>
<td>
<input name="beeminder_username" type="text" id="beeminder_username" value="<?php echo $beeminder_username; ?>" class="regular-text" />
</td>
</tr>
<tr valign="top">
<th scope="row"><label for="beeminder_key">Beeminder API Key</label></th>
<td>
<input name="beeminder_key" type="text" id="beeminder_key" value="<?php echo $beeminder_key; ?>" class="regular-text" />
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button-primary" value="Save Changes" />
<input type="submit" name="refresh" id="refresh-goals" class="button" value="Refresh Goals" />
<input type="submit" name="clear" id="clear-settings" class="button" value="Clear Settings" />
</p>
</form>
</div>
<?php if ($beeminder_key) { ?>
<div id="beeminder-ping-actions">
<h3>Actions</h3>
<p>
Choose the actions that will be executed when a post is published. Either:
</p>
<ul style="margin-left: 48px; list-style: disc;">
<li>Send a single value (1) to a goal -- Use this if you have a goal such as "post 5 times a week"</li>
<li>Send the wordcount to a goal -- Use this for goals like "write 1,000 words a week.</li>
</ul>
<form method="POST" action="">
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row"><strong>Post Count Goal</strong></th>
<td>
<label for="beeminder_single_enabled">
<input name="beeminder_single_enabled" type="checkbox" id="beeminder_single_enabled" value="1" <?php if ($beeminder_post_enabled) { echo 'checked="checked"'; } ?> />
Send a ping when a post is published to the following goal:
</label>
<br />
<select name="beeminder_single_post">
<?php foreach ($beeminder_goals as $goal) { ?>
<option value="<?php echo $goal->slug; ?>" <?php if ($goal->slug == $beeminder_post_goal) { echo 'selected="true"'; } ?>><?php echo $goal->title; ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr valign="top">
<th scope="row"><strong>Word Count Goal</strong></th>
<td>
<label for="beeminder_wordcount_enabled">
<input name="beeminder_wordcount_enabled" type="checkbox" id="beeminder_wordcount_enabled" value="1" <?php if ($beeminder_wordcount_enabled) { echo 'checked="checked"'; } ?> />
Send the wordcount of a post when published to the following goal:
</label>
<br />
<select name="beeminder_wordcount_post">
<?php foreach ($beeminder_goals as $goal) { ?>
<option value="<?php echo $goal->slug; ?>" <?php if ($goal->slug == $beeminder_wordcount_goal) { echo 'selected="true"'; } ?>><?php echo $goal->title; ?></option>
<?php } ?>
</select>
</td>
</tr>
</tbody>
</table>
<p class="submit">
<input type="submit" name="update-hooks" id="update-hooks-submit" class="button-primary" value="Update Hooks" />
</p>
</form>
</div>
<?php } ?>
</div><!-- /div#wrap -->