This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwptroopid_Plugin.php
379 lines (320 loc) · 11.4 KB
/
wptroopid_Plugin.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
include_once('wptroopid_LifeCycle.php');
class troopid_Plugin extends troopid_LifeCycle {
/**
* See: http://plugin.michael-simpson.com/?page_id=31
* @return array of option meta data.
*/
public function getOptionMetaData() {
// http://plugin.michael-simpson.com/?page_id=31
return array(
//'_version' => array('Installed Version'), // Leave this one commented-out. Uncomment to test upgrades.
'client_id' => array(__('id.me client_id', 'wordpress-troopid-plugin')),
'client_secret' => array(__('id.me client_secret', 'wordpress-troopid-plugin')),
'redirect_uri' => array(__('redirect uri (url) for id.me to target on success', 'wordpress-troopid-plugin')),
'sandbox' => array(__('run plugin in sandbox mode', 'wordpress-troopid-plugin'), 'true', 'false'),
'button-style-size' => array(__('Size of the button', 'wordpress-troopid-plugin'), 'small', 'medium', 'large'),
'button-style-color' => array(__('Color of the button', 'wordpress-troopid-plugin'), 'red', 'black', 'light gray', 'dark gray', 'white'),
'button-style-type' => array(__('Shape of the button', 'wordpress-troopid-plugin'), 'rounded', 'square')
);
}
// protected function getOptionValueI18nString($optionValue) {
// $i18nValue = parent::getOptionValueI18nString($optionValue);
// return $i18nValue;
// }
protected function initOptions() {
$options = $this->getOptionMetaData();
if (!empty($options)) {
foreach ($options as $key => $arr) {
if (is_array($arr) && count($arr > 1)) {
$this->addOption($key, $arr[1]);
}
}
}
}
public function getPluginDisplayName() {
return 'WordPress TroopID plugin';
}
protected function getMainPluginFileName() {
return 'wptroopid.php';
}
/**
* See: http://plugin.michael-simpson.com/?page_id=101
* Called by install() to create any database tables if needed.
* Best Practice:
* (1) Prefix all table names with $wpdb->prefix
* (2) make table names lower case only
* @return void
*/
protected function installDatabaseTables() {
// global $wpdb;
// $tableName = $this->prefixTableName('mytable');
// $wpdb->query("CREATE TABLE IF NOT EXISTS `$tableName` (
// `id` INTEGER NOT NULL");
}
/**
* See: http://plugin.michael-simpson.com/?page_id=101
* Drop plugin-created tables on uninstall.
* @return void
*/
protected function unInstallDatabaseTables() {
// global $wpdb;
// $tableName = $this->prefixTableName('mytable');
// $wpdb->query("DROP TABLE IF EXISTS `$tableName`");
}
/**
* Perform actions when upgrading from version X to version Y
* See: http://plugin.michael-simpson.com/?page_id=35
* @return void
*/
public function upgrade() {
}
public function addActionsAndFilters() {
// Add options administration page
// http://plugin.michael-simpson.com/?page_id=47
add_action('admin_menu', array(&$this, 'addSettingsSubMenuPage'));
// Example adding a script & style just for the options administration page
// http://plugin.michael-simpson.com/?page_id=47
// if (strpos($_SERVER['REQUEST_URI'], $this->getSettingsSlug()) !== false) {
// wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
// wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
// }
// Add Actions & Filters
// http://plugin.michael-simpson.com/?page_id=37
// Adding scripts & styles to all pages
// Examples:
// wp_enqueue_script('jquery');
// wp_enqueue_style('my-style', plugins_url('/css/my-style.css', __FILE__));
// wp_enqueue_script('my-script', plugins_url('/js/my-script.js', __FILE__));
//make sure jquery is loaded
wp_enqueue_script('jquery');
//load up bbq specifically for url deserialization in js
wp_enqueue_script('jquery-bbq', plugins_url('js/jquery-bbq-1.2.1/jquery.ba-bbq.min.js', __FILE__), 'jquery');
//load up jquery cookies, so we can see cookies in js
wp_enqueue_script('jquery-cookie', plugins_url('js/jquery.cookie-1.4.1.min.js', __FILE__), 'jquery');
//load up the redir js file for this plugin. This is used if we're not using the popup window.
wp_enqueue_script('troopid-redir', plugins_url('js/redir.js', __FILE__), 'jquery');
//load up jquery session storage, nicer than cookies...
wp_enqueue_script('jquery-storage', plugins_url('js/jQuery-Storage-API-1.7.2/jquery.storageapi.min.js', __FILE__), 'jquery');
//load up jquery post pessage, allows us to post variables on a simulated ajax call
wp_enqueue_script('jquery-postmessage', plugins_url('js/jquery.ba-postmessage.min.js', __FILE__), 'jquery');
//load up the receiver logic for this plugin
wp_register_script('troopid-receiver', plugins_url('js/receiver.js', __FILE__), 'jquery-postmessage');
//load up the pass through variables for the popup window.
wp_enqueue_script('troopid-popup', plugins_url('js/popup.js',__FILE__), 'jquery');
//set popup options from wp-options. Localized to js variable.
wp_localize_script('troopid-popup', 'idmepopupvars', array(
'sandbox' => __($this->getOption('sandbox'), 'wordpress-troopid-plugin'),
'client_id' => __($this->getOption('client_id'), 'wordpress-troopid-plugin'),
'scope' => __($this->getOption('scope'), 'wordpress-troopid-plugin'),
'redirect_uri' => __(esc_url($this->getOption('redirect_uri')), 'wordpress-troopid-plugin'),
'response_type' => __($this->getOption('response_type'), 'wordpress-troopid-plugin')
));
// Register short codes
// http://plugin.michael-simpson.com/?page_id=39
//Add a nice TroopID link if we don't want the button
add_shortcode('troopid_link', array($this, 'troopid_link'));
//Set up a button and allow for parameters for prepopulation of a form
add_shortcode('troopid_formload', array($this, 'troopid_formload'));
//Set up a button for a popup, no prefill options.
add_shortcode('troopid_popup', array($this, 'troopid_popup'));
//Set up a container to show verified in a nice display.
add_shortcode('troopid_verified_container', array($this, 'troopid_verified_container'));
// Register AJAX hooks
// http://plugin.michael-simpson.com/?page_id=41
//add other things
add_action('init', array($this, 'myStartSession'), 1);
add_action('wp_logout', array($this, 'myEndSession'));
add_action('wp_login', array($this, 'myEndSession'));
}
public function troopid_link() {
// get wp-options
$client_id = $this->getOption('client_id');
$redirect_uri = $this->getOption('redirect_uri');
$response_type = $this->getOption('response_type');
$sandbox = $this->getOption('sandbox');
$scope = $this->getOption('scope');
//test for sandbox so we know what api to call
if($sandbox=='true'){
$out = "<a href=\"https://api.sandbox.id.me/oauth/authorize?client_id=$client_id&redirect_uri=$redirect_uri&response_type=$response_type&scope=$scope\" class=\"idme_button\">ID.me</a>";
} else {
$out = "<a href=\"https://api.id.me/oauth/authorize?client_id=$client_id&redirect_uri=$redirect_uri&response_type=$response_type$scope=$scope\" class=\"idme_button\">ID.me</a>";
}
return $out;
}
public function troopid_verified_container(){
//drop the verified container in the page
$out = <<<EOT
<div id="troopid_verified" class="troopid-verified"></div>
EOT;
return $out;
}
public function myStartSession() {
if(session_status() == PHP_SESSION_NONE) {
session_start();
}
}
public function myEndSession() {
session_destroy ();
}
public function troopid_popup() {
//set up the button with options
switch($this->getOption('button-style-size')){
case 'small':
$size = '20';
break;
case 'medium':
$size = '32';
break;
case 'large':
$size = '40';
break;
default:
$size = '32';
}
switch($this->getOption('button-style-color')){
case 'red':
$color = 'red';
break;
case 'light gray':
$color = 'gray1';
break;
case 'dark gray':
$color = 'gray2';
break;
case 'black':
$color = 'black';
break;
case 'white':
$color = 'white';
break;
default:
$color = 'red';
}
switch($this->getOption('button-style-type')){
case 'rounded':
$type = 'rnd';
break;
case 'square':
$type = 'squ';
break;
default:
$type = 'rnd';
}
switch($this->getOption('scope')){
case 'military':
$kind = 'troop';
break;
case 'student':
$kind = 'Student';
break;
case 'responder':
$kind = 'Responder';
break;
default:
$kind = 'troop';
}
$scope = $this->getOption('scope');
$button = $kind.'_'.$color.'_'.$type.'_'.$size.'.png';
wp_enqueue_script('troopid-receiver');
//output the button html
$out = <<<EOT
<a class="troopid-login-trigger" href="#">
<img alt="Verify your $scope affiliation with TroopID" src="https://s3.amazonaws.com/idme/buttons/$button" />
</a>
EOT;
return $out;
}
public function troopid_formload($atts) {
//handle shortcode attributes
$a = shortcode_atts( array(
'first_name' => 'first_name',
'last_name' => 'last_name',
'email' => 'email',
'phone' => 'phone',
'zip' => 'zip',
'verified' => 'verified',
'affiliation' => 'affiliation',
'service_started' => 'service_started',
'service_ended' => 'service_ended'
), $atts );
//get wp-options for button display
switch($this->getOption('button-style-size')){
case 'small':
$size = '20';
break;
case 'medium':
$size = '32';
break;
case 'large':
$size = '40';
break;
default:
$size = '32';
}
switch($this->getOption('button-style-color')){
case 'red':
$color = 'red';
break;
case 'light gray':
$color = 'gray1';
break;
case 'dark gray':
$color = 'gray2';
break;
case 'black':
$color = 'black';
break;
case 'white':
$color = 'white';
break;
default:
$color = 'red';
}
switch($this->getOption('button-style-type')){
case 'rounded':
$type = 'rnd';
break;
case 'square':
$type = 'squ';
break;
default:
$type = 'rnd';
}
switch($this->getOption('scope')){
case 'military':
$kind = 'troop';
break;
case 'student':
$kind = 'Student';
break;
case 'responder':
$kind = 'Responder';
break;
default:
$kind = 'troop';
}
$scope = $this->getOption('scope');
$button = $kind.'_'.$color.'_'.$type.'_'.$size.'.png';
//drop the shortcode attribute values into the preload variable
wp_localize_script('troopid-receiver', 'troopidformload', array(
'first_name' => $a['first_name'],
'last_name' => $a['last_name'],
'email' => $a['email'],
'phone' => $a['phone'],
'zip' => $a['zip'],
'verified' => $a['verified'],
'affiliation' => $a['affiliation'],
'service_started' => $a['service_started'],
'service_ended' => $a['service_ended']
));
wp_enqueue_script('troopid-receiver');
//output the button html
$out = <<<EOT
<a class="troopid-login-trigger" href="#">
<img alt="Verify your $scope affiliation with TroopID" src="https://s3.amazonaws.com/idme/buttons/$button" />
</a>
EOT;
return $out;
}
}