-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApbcPCORota.php
266 lines (218 loc) · 8.02 KB
/
ApbcPCORota.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
<?php
/*
Plugin Name: APBC PCO Rota Widget
Plugin URI: http://tetrahedra.co.uk/ApbcPCORota
Description: Plugin and widget to allow display of rota and service information from
Planning Center Online (http://www.planningcenteronline.com). The plugin uses the Planning Center API
and is based on the following tutorials and sample code:
- http://planningcenteronline.com/home/api
- https://github.com/ministrycentered/planningcenteronline-php-api-example
- http://mg.mkgarrison.com/2010/08/using-planningcenteronlinecoms-api-with.html
Version: 0.1 BETA
Author: John Adams
Author URI: http://www.tetrahedra.co.uk
License: GPL2
*/
/*
Copyright 2012 John Adams, Tetrahedra (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 as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
*/
//include other libraries
require_once "OAuth.php"; //oauth library
require_once "common.php"; //common functions and variables
require_once "plans.php"; //functions to manage PCO plans
require_once "pco_options.php"; //manages the plugin administration
$NOTOKENS = "You have no tokens, you will need to authorise your PCO account on the Settings page";
//create consumer tokens
function createTokens() {
global $test_consumer;
global $access_consumer;
$options = get_option('pco_plugin_options');
//if any of the keys are missing...
if (!$options['pco_key'] || !$options['pco_secret'] || !$options['pco_tokenkey'] || !$options['pco_tokensecret'])
return false;
$test_consumer = new OAuthConsumer($options['pco_key'], $options['pco_secret'], NULL);
$access_consumer = new OAuthConsumer($options['pco_tokenkey'], $options['pco_tokensecret'], NULL);
return true;
}
/**
* Add function to widgets_init that'll load our widget.
* @since 0.1
*/
add_action( 'widgets_init', 'apbc_pco_rota_load_widgets' );
/**
* Register our widget.
* 'Apbc_PCO_Rota_Widget' is the widget class used below.
*
* @since 0.1
*/
function apbc_pco_rota_load_widgets() {
register_widget( 'Apbc_PCO_Rota_Widget' );
}
/**
* Apbc_PCO_Rota_Widget class.
* This class handles everything that needs to be handled with the widget:
* the settings, form, display, and update. Nice!
*
* @since 0.1
*/
class Apbc_PCO_Rota_Widget extends WP_Widget {
/**
* Widget setup.
*/
function Apbc_PCO_Rota_Widget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'apbc-pco', 'description' => __('A widget to display rota information from Planning Center Online') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'apbc-pco-widget' );
/* Create the widget. */
$this->WP_Widget( 'apbc-pco-widget', __('APBC PCO Rota'), $widget_ops, $control_ops );
}
/**
* How to display the widget on the screen.
*/
function widget( $args, $instance ) {
extract( $args );
/* Variables from the widget settings. */
$title = apply_filters('widget_title', $instance['title'] );
$errorMsg = $instance['error'];
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo $before_title . $title . $after_title;
//set up access
global $test_consumer, $access_consumer;
if (createTokens()) {
//retrieve and display the data:
$feed = getNextServiceSummary($test_consumer,$access_consumer);
if ($feed) {
$this->showThisWeek($feed,$pageurl);
}
else {
echo $errorMsg;
}
}
else {
global $NOTOKENS;
echo $NOTOKENS;
}
/* After widget (defined by themes). */
echo $after_widget;
}
/**
* Update the widget settings.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['error'] = strip_tags( $new_instance['error'] );
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
* Make use of the get_field_id() and get_field_name() function
* when creating your form elements. This handles the confusing stuff.
*/
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array( 'title' => __('Recent Attachments'), 'num' => __(5), 'error' => __("No documents found."), 'parent' => null );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" />
</p>
<!-- Error message: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'error' ); ?>"><?php _e('Error message:'); ?></label>
<input id="<?php echo $this->get_field_id( 'error' ); ?>" name="<?php echo $this->get_field_name( 'error' ); ?>" value="<?php echo $instance['error']; ?>" />
</p>
<?php
}
/*
* Private widget functions
*
*/
function showThisWeek($feed,$pageurl) {
$sunday = date('l jS F Y', $this->ukStrToTime($feed[0]['date']));
$leader = $feed[0]["leader"];
$speaker = $feed[0]["speaker"];
$time = $feed[0]["time"];
echo("<div id='inline_apbcrota'>");
echo("<h2>$sunday</h2>");
echo("<ul class='thisWeek'>");
if($time != "")
echo ($time . "<br />");
else
echo("11am - 12.30pm <br />");
echo("Worship leader: <strong>$leader</strong><br />");
echo("Speaker: <strong>$speaker</strong><br />");
echo("Coffee and tea afterwards<br />");
//echo("<a href='$pageurl'>Full WT Rota</a>");
echo("</ul>");
echo("</div>");
}
function ukStrToTime($str) {
return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
}
}
/*
* General plugin functions
*/
// [apbcpcorota] shortcode
function apbcpcorota_func( $atts ) {
//set up access by creating tokens
global $test_consumer, $access_consumer;
if(createTokens()) {
//retrieve and display the data:
$feed = getNextServiceSummary($test_consumer,$access_consumer);
if ($feed) {
return pco_showThisWeek($feed,$pageurl);
}
else {
echo $errorMsg;
}
}
else {
global $NOTOKENS;
echo $NOTOKENS;
}
}
function pco_showThisWeek($feed,$pageurl) {
$sunday = date('l jS F Y', pco_ukStrToTime($feed[0]['date']));
$leader = $feed[0]["leader"];
$speaker = $feed[0]["speaker"];
$time = $feed[0]["time"];
$optionalText = $feed[0]["notes"];
$output = "<div id='inline_apbcrota'>";
$output .= "<h2>$sunday</h2>";
if ($time != "")
$output .= $time . "<br />";
else
$output .= "11am - 12.30pm <br />";
$output .= "Worship leader: <strong>$leader</strong><br />";
$output .= "Speaker: <strong>$speaker</strong><br />";
if($optionalText != "")
$output .= "<em>" . $optionalText . "</em><br />";
$output .= "Join us afterwards for tea and coffee";
if($pageurl)
$output .= "<br /><a href='$pageurl'>Full WT Rota</a>";
$output .= "</div>";
return $output;
}
function pco_ukStrToTime($str) {
return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
}
add_shortcode( 'apbcpcorota', 'apbcpcorota_func' );
?>