forked from humanmade/Notify-Humans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify-humans.php
203 lines (163 loc) · 5.39 KB
/
notify-humans.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
<?php
/*
Plugin Name: Notify Humans
Version: 0.1-alpha
Description: If Then, Then That... for your applications.
Author: danielbachhuber, humanmade
Author URI: http://hmn.md/
Plugin URI: http://wordpress.org/extend/plugins/notify-humans/
Text Domain: notify-humans
Domain Path: /languages
*/
class Notify_Humans {
private $data;
private static $instance;
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new Notify_Humans;
self::$instance->setup_globals();
self::$instance->includes();
self::$instance->setup_actions();
}
return self::$instance;
}
private function __construct() {
/** Prevent the class from being loaded more than once **/
}
public function __isset( $key ) {
return isset( $this->data[$key] );
}
public function __get( $key ) {
return isset( $this->data[$key] ) ? $this->data[$key] : null;
}
public function __set( $key, $value ) {
$this->data[$key] = $value;
}
private function setup_globals() {
$this->file = __FILE__;
$this->basename = apply_filters( 'notify_humans_plugin_basenname', plugin_basename( $this->file ) );
$this->plugin_dir = apply_filters( 'notify_humans_plugin_dir_path', plugin_dir_path( $this->file ) );
$this->plugin_url = apply_filters( 'notify_humans_plugin_dir_url', plugin_dir_url ( $this->file ) );
}
private function includes() {
// Basic object classes
require_once( $this->plugin_dir . 'inc/class-notify-event.php' );
require_once( $this->plugin_dir . 'inc/class-notify-recipe.php' );
// Reusable actions
require_once( $this->plugin_dir . 'inc/class-notify-irc-action.php' );
require_once( $this->plugin_dir . 'inc/class-notify-hipchat-action.php' );
require_once( $this->plugin_dir . 'inc/class-notify-humans-of-events.php' );
}
private function setup_actions() {
if ( ! function_exists( 'hm_add_rewrite_rule' ) ) {
add_action( 'admin_notices', array( $this, 'admin_notices_missing_hm_rewrites' ) );
return;
}
add_action( 'init', array( $this, 'action_init_register_tables' ) );
do_action_ref_array( 'notify_humans_after_setup_actions', array( &$this ) );
}
/**
* A way of notifying ourselves of an event.
* Useful for when the action execution (e.g. IRC)
* may take longer than we want.
*/
public function of( $event, $payload = false ) {
$args = array(
'blocking' => false,
);
if ( $payload )
$args['body'] = $payload;
wp_remote_post( home_url( '/of/' . $event . '/' ), $args );
}
/**
* Start a new recipe
*
* @param string $event Event to start the recipe with
*/
public function if_event( $event ) {
$recipe = new Notify_Recipe;
$recipe->set_event( $event );
$this->data['current_recipe'] = $recipe;
return self::$instance;
}
/**
* Add an action to the current recipe, then save it
*
* @param $action Action to end the recipe with
*/
public function then_action( $action ) {
$recipe = $this->data['current_recipe'];
if ( empty( $recipe ) )
return false;
unset( $this->data['current_recipe'] );
$recipe->set_action( $action );
if ( ! isset( $this->data['recipes'] ) )
$this->data['recipes'] = array();
$this->data['recipes'][] = $recipe;
return true;
}
/**
* Do actions for an event.
*
* @todo return the WP_Errors we potentially collected
*
* @param obj $event The event object
* @return true|WP_Error $result True on success, WP_Error on a failure.
*/
public function do_event_actions( $event ) {
if ( ! is_a( $event, 'Notify_Event' ) )
return new WP_Error( 'invalid-event', __( 'Event is not valid.', 'notify-humans' ) );
$actions = wp_filter_object_list( $this->data['recipes'], array( 'event' => $event->get_slug() ) );
$ret = array();
foreach( $actions as $recipe ) {
$func_ret = call_user_func_array( $recipe->action, array( $event ) );
if ( is_wp_error( $func_ret ) )
$ret[] = $func_ret;
}
if ( empty( $ret ) )
return true;
}
/**
* To log our events and actions in the database, we must ensure the
* database exists
*/
public function action_init_register_tables() {
global $wpdb;
$table_name = $wpdb->prefix . 'notify_events';
if ( is_admin() ) {
// Maybe create the table(s)
if ( ! (bool)$wpdb->query( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ) ) {
$wpdb->query( "CREATE TABLE `{$table_name}` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`slug` varchar(255) NOT NULL DEFAULT '',
`payload` text,
`remote_host` varchar(255) DEFAULT NULL,
`remote_ip` int(10) unsigned DEFAULT NULL,
`timestamp` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;" );
}
}
$wpdb->tables[] = 'notify_events';
$wpdb->notify_events = $table_name;
}
/**
* Notify Humans is dependent on HM Rewrites
*/
public function admin_notices_missing_hm_rewrites() {
echo '<div class="error"><p>' . __( 'Please install HM Rewrites for Notify Humans to work properly', 'notify-humans' ) . '</p></div>';
}
/**
* Do a response to a request
*
* @param int $status HTTP status header
* @param string $message Message to include with response (optional)
*/
public static function do_response( $status, $message = '' ) {
HM_Rewrite::do_response( $status, $message );
}
}
function Notify_Humans() {
return Notify_Humans::get_instance();
}
add_action( 'plugins_loaded', 'Notify_Humans' );