-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththingdom.php
More file actions
371 lines (293 loc) · 8.75 KB
/
Copy paththingdom.php
File metadata and controls
371 lines (293 loc) · 8.75 KB
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
<?php
/*
Plugin Name: Thingdom
Plugin URI: https://www.github.com/thingdomio/thingdom-wordpress/
Description: Integrates your WordPress back-end with the Thingdom API.
Version: 1.0
Author: Nicholas Kreidberg
Author URI: http://niczak.com
*/
defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );
require_once('thingdomAPI.php');
if( !class_exists( 'ThingdomWP' )) {
class ThingdomWP {
//
// Instance variables
//
/**
* Display (user friendly) name to identify plugin.
* @var string
*/
protected $name = 'Thingdom WordPress';
/**
* List of options displayed on the settings page.
* @var array
*/
protected $options = array(
'comments' => array(
'label' => 'New comment alerts',
'type' => 'checkbox',
'default' => 1
),
'pages' => array(
'label' => 'New page alerts',
'type' => 'checkbox',
'default' => 1
),
'posts' => array(
'label' => 'New post alerts',
'type' => 'checkbox',
'default' => 1
),
'pages_update' => array(
'label' => 'Updated page alerts',
'type' => 'checkbox',
'default' => 1
),
'posts_update' => array(
'label' => 'Updated post alerts',
'type' => 'checkbox',
'default' => 1
)
);
/**
* Secret used to identify this WordPress instance.
* @var string
*/
protected $secret;
/**
* List of settings currently stored by the plugin.
* @var array
*/
protected $settings = array(
'comments' => '',
'pages' => '',
'pages_update' => '',
'posts' => '',
'posts_update' => '',
'secret' => ''
);
/**
* Slug used for admin menus.
* @var string
*/
protected $slug = 'thingdom-settings';
/**
* Tag identifier used by file includes and selector attributes.
* @var string
*/
protected $tag = 'thingdom-wp_';
/**
* Thingdom "Thing Name" used for all calls to Thingdom.
* @var string
*/
protected $thingName;
/**
* Thingdom "Product Type" used for all calls to Thingdom.
* @var string
*/
protected $thingType = 'wordpress';
/**
* Display (user friendly) name to identify plugin menu title.
* @var string
*/
protected $title = 'Thingdom WordPress Settings';
/**
* Current plugin version.
* @var string
*/
protected $version = '1.0';
//
// Public methods
//
/**
* Initiate plugin by getting options and setting instance variables.
*
* @access public
*/
public function __construct()
{
// get 'blogname' field from database, if empty default to 'My Blog'
$this->thingName = !empty(get_option('blogname')) ? get_option('blogname') : 'My Blog';
// look up settings and populate instance array
$this->getSettings();
// this will be used for Thingdom calls and will always contain a static value unless this is the first invocation of the plugin
$this->secret = $this->settings['secret'];
// fire off method to update Thingdom status variables
$this->updateStatus();
// get other plugin options and build array
if ( is_admin() ) {
add_action('admin_menu', array($this, 'registerMenu'));
if(!empty($this->secret)) {
// register all Thingdom-specific admin actions based on plugin configuration
if($this->settings['posts'] == 1 || $this->settings['pages'] == 1) {
add_action('transition_comment_status', array($this, 'updateComment'), 10, 3);
add_action('transition_post_status', array($this, 'postHandler'), 10, 3);
}
}
}
if(!empty($this->secret)) {
// register all Thingdom-specific non-admin actions based on plugin configuration
if($this->settings['comments'] == 1) {
add_action('comment_post', array($this, 'newComment'), 10, 1);
}
}
}
/**
* Method triggered whenever a page/post status changes.
*
* @access public
* @param string $new_status
* @param string $old_status
* @param object $post
*/
public function postHandler($new_status, $old_status, $post)
{
$thingdom = $this->getThingdom();
if(!$thingdom) {
return false;
}
$post_title = $post->post_title;
$thing = $thingdom->getThing($this->thingName, $this->thingType);
if($post->post_type == 'post' && $this->settings['posts'] == 1 && ($new_status == 'publish' && $old_status != 'publish')) {
$thing->feed('new_post', "New post published: {$post_title}");
} else if($post->post_type == 'page' && $this->settings['pages'] == 1 && ($new_status == 'publish' && $old_status != 'publish')) {
$thing->feed('new_page', "New page published: {$post_title}");
} else if($post->post_type == 'post' && $this->settings['posts_update'] == 1 && ($new_status == 'publish' && $old_status == 'publish')) {
$thing->feed('update_post', "Post Updated: {$post_title}");
} else if($post->post_type == 'page' && $this->settings['pages_update'] == 1 && ($new_status == 'publish' && $old_status == 'publish')) {
$thing->feed('update_page', "Page Updated: {$post_title}");
}
}
/**
* Method triggered whenever an auto-approved comment is posted to the site.
* This typically only happens (depending on configuration) when a logged in user
* or previously approved user leaves a comment.
*
* @access public
* @param int $comment_id
*/
public function newComment($comment_id)
{
$comment = get_comments(array('ID' => $comment_id));
$comment = $comment[0];
if($comment->comment_approved != 1) {
return false;
}
$thingdom = $this->getThingdom();
if(!$thingdom) {
return false;
}
$post_title = get_the_title($comment->comment_post_ID);
$thing = $thingdom->getThing($this->thingName, $this->thingType);
$thing->feed('new_comment', "New comment on: {$post_title}\n<br>From: {$comment->comment_author}<br>\nComment: {$comment->comment_content}");
}
/**
* Method triggered whenever a comment status changes from the admin panel.
*
* @access public
* @param string $new_status
* @param string $old_status
* @param object $comment
*/
public function updateComment($new_status, $old_status, $comment)
{
if($new_status == $old_status) {
return false;
}
$thingdom = $this->getThingdom();
if(!$thingdom) {
return false;
}
$thing = $thingdom->getThing($this->thingName, $this->thingType);
$post_title = get_the_title($comment->comment_post_ID);
if($old_status != 'approved' && $new_status == 'approved') {
$thing->feed('new_comment', "New comment on: {$post_title}\n<br>From: {$comment->comment_author}<br>\nComment: {$comment->comment_content}");
}
}
/**
* Method to handle updating status variables when plugin is invoked.
*
* @access public
*/
public function updateStatus()
{
$thingdom = $this->getThingdom();
if(!$thingdom) {
return false;
}
$page_count = wp_count_posts('page')->publish;
$post_count = wp_count_posts()->publish;
$comment_count = wp_count_comments()->approved;
$thing = $thingdom->getThing($this->thingName, $this->thingType);
$thing->status('page_count', $page_count);
$thing->status('post_count', $post_count);
$thing->status('comment_count', $comment_count);
}
/**
* Load settings view.
*
* @access public
*/
public function loadSettings()
{
include('settings.php');
}
/**
* Admin menu action method to add options page and call initialize settings.
*
* @access public
*/
public function registerMenu()
{
add_options_page( 'Thingdom Settings', 'Thingdom Settings', 'manage_options', $this->slug, array($this, 'loadSettings'));
add_action('admin_init', array($this, 'registerSettings'));
}
/**
* Register all setting options using instance array to initialize.
*
* @access public
*/
public function registerSettings()
{
foreach($this->options as $id => $options) {
register_setting('thingdom-options', $this->tag.$id);
}
}
//
// Private methods
//
/**
* Retrieve plugin settings from database and return array to constructor.
* @access private
* @return array
*/
private function getSettings()
{
foreach($this->settings as $key => $val) {
$this->settings[$key] = !empty(get_option($this->tag.$key)) ? get_option($this->tag.$key) : '';
}
}
/**
* Initialize and return an authenticated Thingdom object.
* @access private
* @return (object) Thingdom
*/
private function getThingdom()
{
try {
$thingdom = new thingdom();
$secret = $thingdom->authenticate($this->secret);
} catch (Exception $ex) {
error_log( json_encode($ex->getMessage()) );
return false;
}
if(empty($this->secret)) {
// first time plugin has run on this instance, write secret to wp_options table
update_option($this->tag.'secret', $secret);
}
return $thingdom;
}
}
new ThingdomWP();
}