This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wviewforwp.php
491 lines (371 loc) · 19.9 KB
/
wviewforwp.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/*
Plugin Name: WView for Wordpress
Plugin URI: http://www.alberniweather.ca/
Description: This plugin connects to files generated by the wview weather server software (or other software using supported format and naming conventions) and displays the weather data in wordpress using widgets.
Requirements for pre-1.0 release:
- Latest WView weather server software (http://www.wviewweather.com)
Note: JPGraph Library/Support removed due to License Incompatibility
INSTALL:
1: install the plugin in Wordpress
2: install the included phpparameter.htx (found in the wviewforwp plugin directory) in WView and set to generate and upload to your root web folder
2b: if you are not running wview you can still use this plugin by having your weather software create a delimited text file similar to phpparameter.htx. Name and value pairs can be omitted but names (before the semi-colon) MUST remain the same.
3: setup the WviewforWP plugin in the "Settings" area of Wordpress
4: add and setup a Widget
5: You're done!
Author: Chris Alemany
Version: 0.5
Author URI: http://www.alberniweather.ca/
*/
/* Copyright 2012 Chris Alemany (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**/
/** FIRST DEFINE A CONSTANT FOR PATHS AND SUCH **/
define( 'WVIEWFORWP_PATH', plugin_dir_path(__FILE__) );
/* Now bring in WeatherArray creation function */
require WVIEWFORWP_PATH . 'adminoptions.php';
require WVIEWFORWP_PATH . 'weatherarray.php';
function selectorFunction1 ($widgetArray,$weatherArray) {
// Output our first set of Weather Values
// First the Text if they'd like something describing the value
$selectorText1=$widgetArray[0];
$selectorValue1=$widgetArray[1];
$selectorUnit1=$widgetArray[2];
$selectorExtra1=$widgetArray[3];
$selectorText2=$widgetArray[4];
$selectorValue2=$widgetArray[5];
$selectorUnit2=$widgetArray[6];
$selectorExtra2=$widgetArray[7];
$weatherperiod=$widgetArray[8];
if ($selectorText1) {
$output = '<p><strong style="color:green">' . $selectorText1 . '</strong></p>';
}
if ($selectorText2) {
$output = '<p><strong style="color:green">' . $selectorText1 . '</strong></p>';
}
$output = $output . '<div id="wviewwidget1">
<table style="border:1px dotted black; background-color: rgba(20%,20%,20%,0.1);">
<tr>';
//Now the Value itself in the first table row
if ($selectorValue1 != 'none') {
$output = $output . '<td style="padding: 2px;">'.$weatherArray[$selectorValue1].'';
}
//Now the Unit if desired
if ($selectorUnit1 != 'none') {
$output = $output . $weatherArray[$selectorUnit1].'';
}
//Now the Extra if desired
if ($selectorExtra1 != 'none') {
$output = $output . $weatherArray[$selectorExtra1].'</td>';
}
//Now the Value itself in the first table row
if ($selectorValue2 != 'none') {
$output = $output . '<td style="padding: 2px;">'.$weatherArray[$selectorValue2].'';
}
//Now the Unit if desired
if ($selectorUnit2 != 'none') {
$output = $output . $weatherArray[$selectorUnit2].'';
}
//Now the Extra if desired
if ($selectorExtra2 != 'none') {
$output = $output . $weatherArray[$selectorExtra2].'</td>';
}
$output = $output . '<tr/></table></div>';
return $output;
}//END selectorFunction1
/* Now here is the main function that brings everything together from the Admin form and runs it all. */
function mainwpwvfunc ($widgetArray) {
$weatherArray = weathersetup($widgetArray[8]);
$output = selectorFunction1($widgetArray,$weatherArray);
return $output;
}
/** AJAX FUNCTION **/
function wviewforWP_main_values_widget_request_handler() {
// Check that all parameters have been passed
if ((isset($_GET['wviewforWP_main_values_widget_request']) && ($_GET['wviewforWP_main_values_widget_request'] == 'update_wview')) &&
isset($_GET['wviewforWP_main_values_widget_selectorText1']) &&
isset($_GET['wviewforWP_main_values_widget_selectorValue1']) &&
isset($_GET['wviewforWP_main_values_widget_selectorUnit1']) &&
isset($_GET['wviewforWP_main_values_widget_selectorExtra1']) &&
isset($_GET['wviewforWP_main_values_widget_selectorText2']) &&
isset($_GET['wviewforWP_main_values_widget_selectorValue2']) &&
isset($_GET['wviewforWP_main_values_widget_selectorUnit2']) &&
isset($_GET['wviewforWP_main_values_widget_selectorExtra2']) &&
isset($_GET['wviewforWP_main_values_widget_weatherperiod']))
{
$widgetArray = Array(strip_tags($_GET['wviewforWP_main_values_widget_selectorText1']),
strip_tags($_GET['wviewforWP_main_values_widget_selectorValue1']), strip_tags($_GET['wviewforWP_main_values_widget_selectorUnit1']),
strip_tags($_GET['wviewforWP_main_values_widget_selectorExtra1']),
strip_tags($_GET['wviewforWP_main_values_widget_selectorText2']),
strip_tags($_GET['wviewforWP_main_values_widget_selectorValue2']), strip_tags($_GET['wviewforWP_main_values_widget_selectorUnit2']),
strip_tags($_GET['wviewforWP_main_values_widget_selectorExtra2']),
strip_tags($_GET['wviewforWP_main_values_widget_weatherperiod']));
// Output the response from your call and exit
echo mainwpwvfunc($widgetArray);
exit();
} elseif (isset($_GET['wviewforWP_main_values_widget_request']) && ($_GET['wviewforWP_main_values_widget_request'] == 'some_action')) {
// Otherwise display an error and exit the call
echo "Error: Unable to display request.";
exit();
}
}
/**
* Widget Class
*/
/********************************************************
********************************************************/
class wviewforWP_main_values_widget extends WP_Widget {
/*constructor - This creates the widget. You can create as many of these as you want by copying the function and changing the name to suit the new widget you create. */
public function wviewforWP_main_values_widget() {
parent::WP_Widget(false, $name = 'Main WView Values');
// Load jQuery
wp_enqueue_script('jquery');
}
/* This controls what the widget actually does. Gets stuff for it and displays it on the website.
/** @see WP_Widget::widget */
public function widget($args, $instance) {
extract( $args ); //This gets arguments and grabs the options that are set in the admin panel for the widget in Wordpress
// Get the div id of the widget
$widgetid = $args['widget_id'];
// these are our widget options
$title = apply_filters('widget_title', $instance['title']);
$text = $instance['text'];
$checkbox = $instance['checkbox'];
//for Ajax
$useAjax = $instance['useAjax'];
$showUpdates = $instance['showUpdates'];
//The options for the first widget instance
$weatherperiod = $instance['weatherperiod'];
$selectorText1 = $instance['selectorText1'];
$selectorValue1 = $instance['selectorValue1'];
$selectorUnit1 = $instance['selectorUnit1'];
$selectorExtra1 = $instance['selectorExtra1'];
$selectorText2 = $instance['selectorText2'];
$selectorValue2 = $instance['selectorValue2'];
$selectorUnit2 = $instance['selectorUnit2'];
$selectorExtra2 = $instance['selectorExtra2'];
//This variable is used by AJAX to update only the live data and not other non-live elements in the widget.
$widgetIDUpdater = $widgetid . 'update';
echo $before_widget;
if ($title) {
echo $before_title . '<strong style="color:red">' . $title .'</strong>' . $after_title;
}
//Now we'll check if we want to display the Live Updates notification
if ($showUpdates == 1) {
echo '<strong>Live Updates';
//Then we'll change the display be it on or off
if ($useAjax == 1) {
echo ' On';
}
else{
echo ' Off';
}
echo '</strong> <br/>';
}//End ShowUpdates IF
//Now we create the container for the live stuff
$widgetArray = Array($selectorText1,$selectorValue1,$selectorUnit1,$selectorExtra1,$selectorText2,$selectorValue2,$selectorUnit2,$selectorExtra2,$weatherperiod);
echo '<span id="' . $widgetIDUpdater . '">';
if ($useAjax == 1) {
?>
<script type="text/javascript">
SANAjax = function() {
jQuery(document).ready(function($){
$.ajax({
type : "GET",
url : "index.php",
data : { wviewforWP_main_values_widget_request : "update_wview",
wviewforWP_main_values_widget_selectorText1 : "<?php echo $selectorText1; ?>",
wviewforWP_main_values_widget_selectorValue1 : "<?php echo $selectorValue1; ?>",
wviewforWP_main_values_widget_selectorUnit1 : "<?php echo $selectorUnit1; ?>",
wviewforWP_main_values_widget_selectorExtra1 : "<?php echo $selectorExtra1; ?>",
wviewforWP_main_values_widget_selectorText2 : "<?php echo $selectorText2; ?>",
wviewforWP_main_values_widget_selectorValue2 : "<?php echo $selectorValue2; ?>",
wviewforWP_main_values_widget_selectorUnit2 : "<?php echo $selectorUnit2; ?>",
wviewforWP_main_values_widget_selectorExtra2 : "<?php echo $selectorExtra2; ?>",
wviewforWP_main_values_widget_weatherperiod : "<?php echo $weatherperiod; ?>"},
success : function(response) {
// The server has finished executing PHP and has returned something,
// so display it!
$("#<?php echo $widgetIDUpdater; ?>").empty().append(response);
}
});
});
}
SANAjax();
setInterval( "SANAjax();", 10000 );
</script>
<?php
// Otherwise AJAX is not on and we output HTML method
} else {
echo mainwpwvfunc($widgetArray);
}
//Now we close the container for the live stuff
echo '</span>';
echo $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['text'] = strip_tags($new_instance['text']);
$instance['checkbox'] = strip_tags($new_instance['checkbox']);
$instance['textarea'] = strip_tags($new_instance['textarea']);
$instance['showUpdates'] = strip_tags($new_instance['showUpdates']);
$instance['useAjax'] = strip_tags($new_instance['useAjax']);
$instance['weatherperiod'] = strip_tags($new_instance['weatherperiod']);
$instance['selectorText1'] = strip_tags($new_instance['selectorText1']);
$instance['selectorValue1'] = strip_tags($new_instance['selectorValue1']);
$instance['selectorUnit1'] = strip_tags($new_instance['selectorUnit1']);
$instance['selectorExtra1'] = strip_tags($new_instance['selectorExtra1']);
$instance['selectorText2'] = strip_tags($new_instance['selectorText2']);
$instance['selectorValue2'] = strip_tags($new_instance['selectorValue2']);
$instance['selectorUnit2'] = strip_tags($new_instance['selectorUnit2']);
$instance['selectorExtra2'] = strip_tags($new_instance['selectorExtra2']);
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$text = isset( $instance['text'] ) ? esc_attr( $instance['text'] ) : '';
$checkbox = isset( $instance['checkbox'] ) ? esc_attr( $instance['checkbox'] ) : '';
$textarea = isset( $instance['textarea'] ) ? esc_attr( $instance['textarea'] ) : '';
$weatherperiod = isset( $instance['weatherperiod'] ) ? esc_attr( $instance['weatherperiod'] ) : '';
$showUpdates = isset( $instance['showUpdates'] ) ? esc_attr( $instance['showUpdates'] ) : '';
$useAjax = isset( $instance['useAjax'] ) ? esc_attr( $instance['useAjax'] ) : '';
$selectorText1 = isset( $instance['selectorText1'] ) ? esc_attr( $instance['selectorText1'] ) : '';
$selectorValue1 = isset( $instance['selectorValue1'] ) ? esc_attr( $instance['selectorValue1'] ) : '';
$selectorUnit1 = isset( $instance['selectorUnit1'] ) ? esc_attr( $instance['selectorUnit1'] ) : '';
$selectorExtra1 = isset( $instance['selectorExtra1'] ) ? esc_attr( $instance['selectorExtra1'] ) : '';
$selectorText2 = isset( $instance['selectorText2'] ) ? esc_attr( $instance['selectorText2'] ) : '';
$selectorValue2 = isset( $instance['selectorText2'] ) ? esc_attr( $instance['selectorValue2'] ) : '';
$selectorUnit2 = isset( $instance['selectorText2'] ) ? esc_attr( $instance['selectorUnit2'] ) : '';
$selectorExtra2 = isset( $instance['selectorExtra2'] ) ? esc_attr( $instance['selectorExtra2'] ) : '';
/** Call the Weather setup function to do the work for admin and sort it alphabetically **/
//$weatherperiod=0; // Getting only 24hr values;
$weatherArray = weathersetup($weatherperiod);
ksort($weatherArray); //Sort the Array Alphabetically
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Widget Title'); ?></label> - (Blank for None)
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>Enable Ajax Live Updates: <input id="<?php echo $this->get_field_id('useAjax'); ?>" name="<?php echo $this->get_field_name('useAjax'); ?>" type="checkbox" value="1" <?php checked( '1', $useAjax ); ?>></p>
<p>Show Live Update Status: <input id="<?php echo $this->get_field_id('showUpdates'); ?>" name="<?php echo $this->get_field_name('showUpdates'); ?>" type="checkbox" value="1" <?php checked( '1', $showUpdates ); ?></p>
<!-- <p>
<input id="<?php echo $this->get_field_id('showUpdates'); ?>" name="<?php echo $this->get_field_name('showUpdates'); ?>" type="checkbox" value="1" <?php checked( '1', $showUpdates ); ?>
<label for="<?php echo $this->get_field_id('showUpdates'); ?>"><?php _e('This is a checkbox'); ?></label>
</p>
<p>
<label for="<?php echo $this->get_field_id('textarea'); ?>"><?php _e('Enter custom value shortcodes here(future):'); ?></label>
<textarea class="widefat" id="<?php echo $this->get_field_id('textarea'); ?>" name="<?php echo $this->get_field_name('textarea'); ?>"><?php echo $textarea; ?></textarea>
</p>
-->
<p>
<label for="<?php echo $this->get_field_id('weatherperiod'); ?>"><?php _e('Weather Period'); ?></label>
<select name="<?php echo $this->get_field_name('weatherperiod'); ?>" id="<?php echo $this->get_field_id('weatherperiod'); ?>">
<?php
echo '<option value="none" id="none"', $weatherperiod == 'none' ? ' selected="selected"' : '', '>Choose Time Frame</option>';
$options = Array(0=>'24hr',1=>'Weekly',2=>'Monthly',3=>'Yearly');
foreach ($options as $value => $name) {
echo '<option value="' . $value . '" id="' . $value . '"', $weatherperiod == $value ? ' selected="selected"' : '', '>', $name, '</option>';
}
?>
</select>
</p>
<p><strong>First Set of Weather Data:</strong><br/>
<label for="<?php echo $this->get_field_id('selectorText1'); ?>"><?php _e('Description'); ?></label>
<input id="<?php echo $this->get_field_id('selectorText1'); ?>" name="<?php echo $this->get_field_name('selectorText1'); ?>" type="text" value="<?php echo $selectorText1; ?>" /><br/>
</p>
<p>
<label for="<?php echo $this->get_field_id('selectorValue1'); ?>"><?php _e(' Value to Display:'); ?></label>
<select name="<?php echo $this->get_field_name('selectorValue1'); ?>" id="<?php echo $this->get_field_id('selectorValue1'); ?>">
<?php
$options = $weatherArray;
//First the default option:
echo '<option value="none" id="none"', $selectorValue1 == 'none' ? ' selected="selected"' : '', '> none</option>';
foreach ($options as $value => $name) {
echo '<option value="' . $value . '" id="' . $value . '"', $selectorValue1 == $value ? ' selected="selected"' : '', '>', $value, '</option>';
}
?>
</select>
<label for="<?php echo $this->get_field_id('selectorUnit1'); ?>"><?php _e('Units'); ?></label>
<select name="<?php echo $this->get_field_name('selectorUnit1'); ?>" id="<?php echo $this->get_field_id('selectorUnit1'); ?>">
<?php
$options = $weatherArray;
//First the default option:
echo '<option value="none" id="none"', $selectorUnit1 == 'none' ? ' selected="selected"' : '', '> none</option>';
foreach ($options as $value => $name) {
echo '<option value="' . $value . '" id="' . $value . '"', $selectorUnit1 == $value ? ' selected="selected"' : '', '>', $value, '</option>';
}
?>
</select>
<label for="<?php echo $this->get_field_id('selectorExtra1'); ?>"><?php _e('Extra'); ?></label>
<select name="<?php echo $this->get_field_name('selectorExtra1'); ?>" id="<?php echo $this->get_field_id('selectorExtra1'); ?>">
<?php
$options = $weatherArray;
//First the default option:
echo '<option value="none" id="none"', $selectorExtra1 == 'none' ? ' selected="selected"' : '', '> none</option>';
foreach ($options as $value => $name) {
echo '<option value="' . $value . '" id="' . $value . '"', $selectorExtra1 == $value ? ' selected="selected"' : '', '>', $value, '</option>';
}
?>
</select>
</p>
<p><strong>Second Set of Weather Data:</strong><br/>
<label for="<?php echo $this->get_field_id('selectorText2'); ?>"><?php _e('Description'); ?></label>
<input id="<?php echo $this->get_field_id('selectorText2'); ?>" name="<?php echo $this->get_field_name('selectorText2'); ?>" type="text" value="<?php echo $selectorText1; ?>" /><br/>
</p>
<p>
<label for="<?php echo $this->get_field_id('selectorValue2'); ?>"><?php _e(' Value to Display:'); ?></label>
<select name="<?php echo $this->get_field_name('selectorValue2'); ?>" id="<?php echo $this->get_field_id('selectorValue2'); ?>">
<?php
$options = $weatherArray;
//First the default option:
echo '<option value="none" id="none"', $selectorValue1 == 'none' ? ' selected="selected"' : '', '> none</option>';
foreach ($options as $value => $name) {
echo '<option value="' . $value . '" id="' . $value . '"', $selectorValue2 == $value ? ' selected="selected"' : '', '>', $value, '</option>';
}
?>
</select>
<label for="<?php echo $this->get_field_id('selectorUnit2'); ?>"><?php _e('Units'); ?></label>
<select name="<?php echo $this->get_field_name('selectorUnit2'); ?>" id="<?php echo $this->get_field_id('selectorUnit2'); ?>">
<?php
$options = $weatherArray;
//First the default option:
echo '<option value="none" id="none"', $selectorUnit2 == 'none' ? ' selected="selected"' : '', '> none</option>';
foreach ($options as $value => $name) {
echo '<option value="' . $value . '" id="' . $value . '"', $selectorUnit2 == $value ? ' selected="selected"' : '', '>', $value, '</option>';
}
?>
</select>
<label for="<?php echo $this->get_field_id('selectorExtra2'); ?>"><?php _e('Extra'); ?></label>
<select name="<?php echo $this->get_field_name('selectorExtra2'); ?>" id="<?php echo $this->get_field_id('selectorExtra2'); ?>">
<?php
$options = $weatherArray;
//First the default option:
echo '<option value="none" id="none"', $selectorExtra2 == 'none' ? ' selected="selected"' : '', '> none</option>';
foreach ($options as $value => $name) {
echo '<option value="' . $value . '" id="' . $value . '"', $selectorExtra2 == $value ? ' selected="selected"' : '', '>', $value, '</option>';
}
?>
</select>
</p>
<?php
}
} //END OF THE MAIN WIDGET CLASS
/********************************************************
********************************************************/
// register example widget
add_action('widgets_init', create_function('', 'return register_widget("wviewforWP_main_values_widget");'));
add_action('init', 'wviewforWP_main_values_widget_request_handler');
?>