-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwidget-plugin.php
262 lines (209 loc) · 10.6 KB
/
widget-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
<?php
/*
Plugin Name: eBird widget Plugin
Plugin URI: http://www.sanisoft.com/ebird
Description: A simple plugin that adds a widget to show eBird data for a selected region
Version: 1.0
Author: Dr. Tarique Sani <[email protected]>
Author URI: http://www.sanisoft.com/blog/author/tariquesani/
License: GPL3
*/
require "includes/ebirdapi.php";
class ebird_widget_plugin extends WP_Widget {
// constructor
function ebird_widget_plugin() {
parent::WP_Widget(false, $name = __('eBird Widget', 'ebird_widget_plugin') );
}
// widget form creation
function form($instance) {
// Check values
if( $instance) {
$region_code = esc_attr($instance['region_code']);
$region_name = esc_attr($instance['region_name']);
$days_back = esc_textarea($instance['days_back']);
} else {
$region_code = 'IN';
$region_name = 'India';
$days_back = '1';
}
?>
<p>
<label for="<?php echo $this->get_field_id('region_code'); ?>"><?php _e('Region Code:', 'ebird_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('region_code'); ?>" name="<?php echo $this->get_field_name('region_code'); ?>" type="text" value="<?php echo $region_code; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('region_name'); ?>"><?php _e('Title:', 'ebird_widget_plugin'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('region_name'); ?>" name="<?php echo $this->get_field_name('region_name'); ?>" type="text" value="<?php echo $region_name; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('days_back'); ?>"><?php _e('For last how many days:', 'ebird_widget_plugin'); ?></label>
<input id="<?php echo $this->get_field_id('days_back'); ?>" name="<?php echo $this->get_field_name('days_back'); ?>" type="text" value="<?php echo $days_back; ?>" size="3"/>
</p>
<?php
}
// widget update
function update($new_instance, $old_instance) {
$instance = $old_instance;
// Fields
$instance['region_code'] = strip_tags($new_instance['region_code']);
$instance['region_name'] = strip_tags($new_instance['region_name']);
$instance['days_back'] = strip_tags($new_instance['days_back']);
delete_transient( 'ebird_data' );
return $instance;
}
// widget display
function widget($args, $instance) {
extract( $args );
// these are the widget options
$title = $instance['region_name'];
$title = apply_filters('widget_title', $title);
$checklists = array();
echo $before_widget;
// Display the widget
echo '<div class="widget-text ebird_widget_plugin_box">';
// Check if title is set
if ( $title ) {
echo $before_title . $title . $after_title;
}
$data = get_transient('ebird_data');
?>
<div id="tab-container" class="tab-container">
<ul class='etabs'>
<li class='tab active'><a href="#observations">Observations</a></li>
<li class='tab'><a href="#checklists">Checklist</a></li>
</ul>
<div id="observations">
<?php
if( $data ) {
$countObservations = count($data->recentObservations);
//print_r($data->recentObservations);
if (!$countObservations) {
delete_transient( 'ebird_data' );
}
echo '<p class="ebird_widget_p">'.sprintf(__("%d observations in last %d days!",'ebird_widget_plugin'), $countObservations, $instance['days_back']).'</p>';
if(count($data->fullObservations)){
echo '<div id="scrollbar1">
<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>
<div class="viewport">
<div class="overview">';
echo"<ul>";
$i =0;
foreach ($data->fullObservations as $observation) {
$observation = array_shift($observation);
@$checklists[$observation->subID]['count']++;
@$checklists[$observation->subID]['location'] = ucwords(strtolower($observation->locName));
@$checklists[$observation->subID]['observer'] = ucwords(strtolower($observation->firstName." ".$observation->lastName));
//print_r($observation);
echo '<li><a class="inline" href="#inline_content_'.$i.'" ><div class="bird-name">'. $observation->comName .'</div><div class="badge">'. @$observation->howMany.'</div></a></li>';
echo "<div style='display:none'>";
echo "<div id='inline_content_$i' style='padding:10px; background:#fff;'>";
echo "<h3> $observation->comName (<i>$observation->sciName</i>) - ".@$observation->howMany."</h3><br>";
echo "<div class='cboxLabel'>Observer:</div><div class='cboxValue'>".ucwords(strtolower($observation->firstName." ".$observation->lastName))."</div>";
echo "<div class='cboxLabel'>Location:</div><div class='cboxValue'>$observation->locName </div>";
echo "<div class='cboxLabel'>Date:</div><div class='cboxValue'>$observation->obsDt</div>";
echo "<div class='cboxLabel'>Status:</div><div class='cboxValue'>".($observation->obsValid? 'Approved':'Not Approved')."</div>";
echo "<br>";
echo "<a href=http://ebird.org/ebird/view/checklist?subID=$observation->subID target=_blank ><img src='" . plugins_url( 'images/checklist.png' , __FILE__ ) . "' title='See checklist' ></a>";
echo "<a href=http://maps.google.com?t=p&z=13&q=$observation->lat,$observation->lng&ll=$observation->lat,$observation->lng target=_blank ><img src='" . plugins_url( 'images/map-marker.png' , __FILE__ ) . "' title='See location map' ></a>";
echo "<a href='http://en.wikipedia.org/wiki/$observation->comName' target=_blank ><img src='" . plugins_url( 'images/wikipedia.png' , __FILE__ ) . "' title='See wikipedia page' ></a>";
echo "</div>";
echo "</div>";
$i++;
}
echo"</ul>";
echo '</div>
</div>
</div>';
//print_r($data);
?>
</div>
<div id="checklists">
<ul>
<?php
foreach ($checklists as $subID => $checklist) {
# code...
echo '<li class="chklst_li" ><a href=http://ebird.org/ebird/view/checklist?subID='.$subID.' target=_blank ><div class="chklst_count" >'.$checklist['count'].'</div><div class="chklst_location" >'.$checklist['location'].'</div><div class="chklst_observer" >'.$checklist['observer'].'</div></a></li>';
}
?>
</ul>
<pre>
<?php //print_r($checklists); ?>
</pre>
</div>
<!-- Be nice and do not remove the credits -->
<div class="sanisoft_credits"><a href="//ebird.org">eBird</a> widget created by <a href="//sanisoft.com">SANIsoft</a></div>
</div>
<?php
} else {
echo'<p class="ebird_widget_p">';
_e(' Details are being fetched from eBird site please check back later', 'ebird_widget_plugin');
echo '</p>';
}
} else {
_e('The data is being fetched from eBird site please check back later', 'ebird_widget_plugin');
}
echo '</div>';
echo $after_widget;
}
}
// register widget
add_action('widgets_init', create_function('', 'return register_widget("ebird_widget_plugin");'));
add_action('get_data', 'get_data');
$data = get_transient('ebird_data');
if(!$data && false === wp_get_schedule( 'get_data' )) {
wp_schedule_single_event(time(), 'get_data');
}
function get_data(){
$ebw = new ebird_widget_plugin;
$settings = $ebw->get_settings();
$settings = array_shift($settings);
$eb = new NagpurBirds\EbirdAPI();
$options['back'] = $settings['days_back'];
$data = new stdClass();
$data->recentObservations = $recentObservations = array();
$data->fullObservations = $fullObservations = array();
$recentObservations = json_decode($eb->recentObservationsInARegion($settings['region_code'], $options));
$data->recentObservations = $recentObservations;
if (!$eb->error) {
set_transient('ebird_data', $data, 12*60*60);
//return true;
}else{
echo $eb->errorMsg;
return false;
}
// rest of the code deals with getting checkLists - not executed for now
$options['detail'] = 'full';
foreach ($data->recentObservations as $observation) {
$fullObservations[] = json_decode(
$eb->recentObservationsOfASpeciesAtLocations(
$observation->locID,
$observation->sciName,
$options
)
);
}
$data->fullObservations = $fullObservations;
if (!$eb->error) {
set_transient('ebird_data', $data, 12*60*60);
return true;
}else{
echo $eb->errorMsg;
return false;
}
}
function ebird_scripts()
{
wp_register_script( 'tinyscrollbar', plugins_url( '/js/tinyscrollbar.min.js', __FILE__ ) );
wp_register_script( 'colorbox', plugins_url( '/js/jquery.colorbox-min.js', __FILE__ ),array( 'jquery' ));
wp_register_script( 'addcolorbox', plugins_url( '/js/addcolorbox.js', __FILE__ ),array( 'colorbox','easytabs','tinyscrollbar' ),1,true);
wp_register_script( 'easytabs', plugins_url( '/js/jquery.easytabs.min.js', __FILE__ ),array( 'jquery' ));
wp_register_style( 'tinyscrollbar', plugins_url( '/css/tinyscrollbar.css', __FILE__ ));
wp_register_style( 'colorbox', plugins_url( '/css/colorbox.css', __FILE__ ));
wp_enqueue_script( 'tinyscrollbar' );
wp_enqueue_script( 'colorbox' );
wp_enqueue_script( 'addcolorbox' );
wp_enqueue_script( 'easytabs' );
wp_enqueue_style( 'tinyscrollbar' );
wp_enqueue_style( 'colorbox' );
}
add_action( 'wp_enqueue_scripts', 'ebird_scripts' );