-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeeminder-ping.php
150 lines (112 loc) · 4.62 KB
/
beeminder-ping.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
<?php
/**
* Plugin Name: Beeminder Ping
* Plugin URI: http://www.philnewton.net/tools/beeminder-ping/
* Description: Simple plugin to ping a Beeminder goal whenever you post, either a simple counter or a wordcount.
* Version: 1.0
* Author: Phil Newton
*/
class BeeminderPingPlugin
{
/**
* Create a new plugin instance. Sets up hooks and runs activation functions
* plugin is being activated for the first time.
*/
public function __construct()
{
// Beeminder API autoloads
add_action('init', array(&$this, 'Handle_onInit_loadBeeminderApi'));
// Install / Uninstall hooks
register_activation_hook(__FILE__, array(&$this, 'Handle_onActivate'));
register_deactivation_hook(__FILE__, array(&$this, 'Handle_onDeActivate'));
// Options Page
add_action('admin_menu', array(&$this, 'Handle_createAdminMenu'));
// Post publish hook
add_action('publish_post', array(&$this, 'Handle_onPublishPost'), 10, 2);
}
// ------------------------------------------------------------
// -- Beeminder API autoloads
// ------------------------------------------------------------
/**
* Creates a Beeminder API autoloader.
*/
public function Handle_onInit_loadBeeminderApi()
{
require_once dirname(__FILE__) . '/vendor/beeminder-api/lib/Beeminder/Autoloader.php';
Beeminder_Autoloader::register();
}
// ------------------------------------------------------------
// -- Admin Area
// ------------------------------------------------------------
/**
* Creates the admin sidebar menu. Adds a page to the "settings" menu,
*/
function Handle_createAdminMenu()
{
add_options_page('Beeminder Ping', 'Beeminder Ping', 'manage_options', 'beeminder-ping', array(&$this, 'Handle_showAdminPage'));
}
function Handle_showAdminPage()
{
require_once dirname(__FILE__) . '/page-options.php';
}
// ------------------------------------------------------------
// -- Activation / Deactivation
// ------------------------------------------------------------
/**
* Called when the plugin is first activated.
*/
function Handle_onActivate()
{
}
/**
* Called when the plugin is deactivated.
*/
function Handle_onDeActivate()
{
}
// ------------------------------------------------------------
// -- Post Hooks
// ------------------------------------------------------------
/**
* Handles the "publish_post" hook. Checks for Beeminder options, checks the
* post is new and sends data to the appropriate goals.
*/
public function Handle_onPublishPost($postId, $post)
{
// Exit if plugin not setup or has pings disabled
if (!get_option('beeminder_ping_key') && !get_option('beeminder_ping_post_enabled') && !get_option('beeminder_ping_wordcount_enabled')) {
return;
}
// Exit if post is already published (i.e. this was an edit)
if (get_post_meta($postId, '_beeminder_ping_sent', true)) {
return;
}
// Create an API interface
$client = new Beeminder_Client();
$client->login(get_option('beeminder_ping_username'), get_option('beeminder_ping_key'));
// Send a single ping
if (get_option('beeminder_ping_post_enabled')) {
$data = $client->getDatapointApi()->createDatapoint(
get_option('beeminder_ping_post_goal'),
1,
"Post published: {$post->post_title}"
);
}
if (get_option('beeminder_ping_wordcount_enabled')) {
// Count words
$words = array();
preg_match_all("/\w+/", $post->post_content, $words);
$wordCount = count($words[0]);
// Send data
$client->getDatapointApi()->createDatapoint(
get_option('beeminder_ping_wordcount_goal'),
$wordCount,
"Post published: {$post->post_title}"
);
}
// Mark ping as sent
update_post_meta($postId, '_beeminder_ping_sent', true);
}
}
// Create plugin
$beeminderPingPluginInstance = new BeeminderPingPlugin();