forked from mitcho/hookpress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
includes.php
361 lines (305 loc) · 12.5 KB
/
includes.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
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
<?php
require('services.php');
require('hooks.php');
// OPTIONS
function hookpress_get_fields( $type ) {
global $wpdb;
$map = array('POST' => array($wpdb->posts),
'PARENT_POST' => array($wpdb->posts),
'COMMENT' => array($wpdb->comments),
'CATEGORY' => array($wpdb->terms,$wpdb->term_taxonomy),
'ATTACHMENT' => array($wpdb->posts),
'LINK' => array($wpdb->links),
'USER' => array($wpdb->users),
'TAG_OBJ' => array($wpdb->terms,$wpdb->term_taxonomy),
'USER_OBJ' => array($wpdb->users),
'OLD_USER_OBJ' => array($wpdb->users));
$tables = $map[$type];
$fields = array();
foreach ( (array) $tables as $table) {
if (is_array($table))
$fields = array_merge($fields,$table);
else
$fields = array_merge($fields,$wpdb->get_col("show columns from $table"));
}
// if it's a POST, we have a URL for it as well.
if ($type == 'POST' || $type == 'PARENT_POST') {
$fields[] = 'post_url';
$fields[] = 'featured_image_src';
$fields[] = 'featured_image_width';
$fields[] = 'featured_image_height';
$fields[] = 'featured_image_sizes';
$meta_keys = $wpdb->get_col("select distinct(meta_key) from $wpdb->postmeta");
$fields = array_merge($fields, $meta_keys);
}
if ($type == 'PARENT_POST')
$fields = array_map(create_function('$x','return "parent_$x";'),$fields);
if ($type == 'OLD_USER_OBJ')
$fields = array_map(create_function('$x','return "old_$x";'),$fields);
$fields = apply_filters('hookpress_get_fields', $fields, $type);
return array_unique($fields);
}
function hookpress_print_edit_webhook( $id ){
?>
<?php
global $wpdb, $hookpress_actions, $hookpress_filters;
$webhooks = hookpress_get_hooks( );
$desc = $webhooks[$id];
if ($desc['type'] == 'action') {
$actions = apply_filters( 'hookpress_actions', $hookpress_actions );
$hooks = array_keys($actions);
}
if ($desc['type'] == 'filter')
$hooks = array_keys($hookpress_filters);
?>
<div id='hookpress-webhook' style='display:block;'>
<form id='editform'>
<input type="hidden" name="edit-hook-id" id="edit-hook-id" value="<?php echo $id ?>" />
<input type="hidden" name="enabled" id="enabled" value="<?php echo $desc['enabled']; ?>" />
<table>
<tr><td><label style='font-weight: bold' for='edithook'><?php _e("WordPress hook type",'hookpress');?>: </label></td>
<td><input type='radio' id='action' class='newtype' name='newtype' <?php checked('action',$desc['type']);?>> <?php _e("action","hookpress");?></input>
<input type='radio' id='filter' class='newtype' name='newtype' <?php checked('filter',$desc['type']);?>> <?php _e("filter","hookpress");?></input></td></tr>
<tr>
<td><label style='font-weight: bold' for='edithook' id='action_or_filter'>
<?php
if ($desc['type'] == 'action')
echo 'Action:';
if ($desc['type'] == 'filter')
echo 'Filter:';
?>
</label></td>
<td><select name='edithook' id='edithook'>
<?php
sort($hooks);
foreach ($hooks as $hook) {
$selected = ($hook == $desc['hook'])?'selected="true"':'';
$hook = esc_html( $hook );
echo "<option value='$hook' $selected>$hook</option>";
}
$nonce_submit = "<input type='hidden' id='submit-nonce' name='submit-nonce' value='" . wp_create_nonce( 'submit-webhook') . "' />";
?>
</select></td></tr>
<tr><td style='vertical-align: top'><label style='font-weight: bold' for='editfields'><?php _e("Fields",'hookpress');?>: </label>
<br/>
<small><?php _e("Ctrl-click on Windows or Command-click on Mac to select multiple. The <code>hook</code> field with the relevant hook name is always sent.");?></small>
<br/>
<span id='filtermessage'><small><?php _e('The first argument of a filter must always be sent and should be returned by the webhook, with modification.','hookpress');?></small></span>
</td>
<td>
<select style='vertical-align: top' name='editfields' id='editfields' multiple='multiple' size='8'>
<?php
global $wpdb, $hookpress_actions, $hookpress_filters;
if ($desc['type'] == 'action') {
$hooks = apply_filters( 'hookpress_actions', $hookpress_actions );
$args = $hooks[$desc['hook']];
}
if ($desc['type'] == 'filter')
$args = $hookpress_filters[$desc['hook']];
$fields = array();
foreach ($args as $arg) {
if (preg_match('/[A-Z]+/',$arg))
$fields = array_merge($fields,hookpress_get_fields($arg));
else
$fields[] = $arg;
}
if ($desc['type'] == 'filter') {
$first = array_shift($fields);
$first = esc_html( $first );
echo "<option value='$first' selected='selected' class='first'>$first</option>";
}
sort($fields);
foreach ($fields as $field) {
$selected = '';
foreach($desc['fields'] as $cmp){
if($cmp==$field){
$selected = 'selected="true"';
}
}
$field = esc_html( $field );
echo "<option value='$field' $selected>$field</option>";
}
$desc['url'] = esc_html( $desc['url'] );
?></select></td></tr>
<tr><td><label style='font-weight: bold' for='newurl'><?php _e("URL",'hookpress');?>: </label></td>
<td><input name='editurl' id='editurl' size='40' value="<?php echo $desc['url']; ?>"></input></td></tr>
</table>
<?php echo $nonce_submit; ?>
<center><span id='editindicator'></span><br/>
<input type='button' class='button' id='editsubmit' value='<?php _e('Save webhook','hookpress');?>'/>
<input type='button' class='button' id='editcancel' value='<?php _e('Cancel');?>'/></center>
</form>
</div>
<?php
}
function hookpress_print_webhook_row( $id ) {
# $webhooks = get_option('hookpress_webhooks');
$webhooks = hookpress_get_hooks( );
$desc = $webhooks[$id];
if( !empty( $desc ) ):
$is_active = $desc['enabled'];
$html_safe['id'] = esc_html( $id );
if ( $is_active ) :
$nonce_action = "<input type='hidden' id='action-nonce-{$html_safe['id']}' name='action-nonce-{$html_safe['id']}' value='" . wp_create_nonce( 'deactivate-webhook-' . $html_safe['id'] ) . "' />";
$action = '<a href="#" id="on'. $html_safe['id'] . '" title="' . __('Deactivate this webhook') . '" class="on">' . __('Deactivate') . '</a>';
else :
$nonce_action = "<input type='hidden' id='action-nonce-{$html_safe['id']}' name='action-nonce-{$html_safe['id']}' value='" . wp_create_nonce( 'activate-webhook-' . $html_safe['id'] ) . "' />";
# $action = '<a href="#'. $nonce_action . '" id="off'. $html_safe['id'] . '" title="' . __('Activate this webhook') . '" class="off">' . __('Activate') . '</a>';
$action = '<a href="#" id="off'. $html_safe['id'] . '" title="' . __('Activate this webhook') . '" class="off">' . __('Activate') . '</a>';
endif;
$nonce_delete = "<input type='hidden' id='delete-nonce-{$html_safe['id']}' name='delete-nonce-{$html_safe['id']}' value='" . wp_create_nonce( 'delete-webhook-' . $html_safe['id'] ) . "' />";
$delete = '<a href="#" id="delete'. $html_safe['id'] . '" title="' . __('Delete this webhook') . '" class="delete">' . __('Delete') . '</a>';
if( count( $desc['fields'] ) > 1 ) {
$desc['fields'] = array_map( 'esc_html', $desc['fields'] );
$fields = implode('</code>, <code>', $desc['fields'] );
} else
$fields = esc_html( $desc['fields'][0] );
$edit = '<a href="#TB_inline?inlineId=hookpress-webhook&height=330&width=500" id="edit'. $html_safe['id'] . '" title="' . __('Edit this webhook') . '" class="thickbox edit">' . __('Edit') . '</a>';
$activeornot = $desc['enabled'] ? 'active' : 'inactive';
$html_safe['hook'] = esc_html( $desc['hook'] );
$html_safe['url'] = esc_html( $desc['url'] );
echo "
<tr id='$id' class='$activeornot'>
<td class='webhook-title'><strong>{$html_safe['hook']}</strong>
<div class='row-actions'>$nonce_action $nonce_delete<span class='edit'>$edit | <span class='delete'>$delete | </span><span class='action'>$action</span></div></td>
<td class='desc'><p>{$html_safe['url']}</p></td>
<td class='desc'><code ".($desc['type'] == 'filter' ? " style='background-color:#ECEC9D' title='".__('The data in the highlighted field is expected to be returned from the webhook, with modification.','hookpress')."'":"").">$fields</code></td>
</tr>\n";
endif;
}
function hookpress_print_webhooks_table() {
global $page;
$webhooks = null;
# $webhooks = get_option('hookpress_webhooks');
$webhooks = hookpress_get_hooks( );
?>
<table class="widefat" cellspacing="0" id="webhooks">
<thead>
<tr>
<th scope="col" class="manage-column" style="width:15%"><?php _e("Hook","hookpress");?></th>
<th scope="col" class="manage-column" style="width:25%"><?php _e("URL","hookpress");?></th>
<th scope="col" class="manage-column"><?php _e("Fields","hookpress");?></th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="col" class="manage-column"><?php _e("Hook","hookpress");?></th>
<th scope="col" class="manage-column"><?php _e("URL","hookpress");?></th>
<th scope="col" class="manage-column"><?php _e("Fields","hookpress");?></th>
</tr>
</tfoot>
<tbody class="webhooks">
<?php
if ( !empty($webhooks) ) :
foreach ( (array)$webhooks as $id => $desc) :
if( !empty( $desc ) ):
hookpress_print_webhook_row( $id );
endif;
endforeach;
endif;
?>
</tbody>
</table>
<?php
}
// MAGIC
function hookpress_register_hooks() {
global $hookpress_callbacks, $hookpress_actions, $hookpress_filters;
$hookpress_callbacks = array();
$all_hooks = hookpress_get_hooks( );
if (!is_array( $all_hooks ) )
return;
foreach ( $all_hooks as $id => $desc) {
if (count($desc) && $desc['enabled']) {
$hookpress_callbacks[$id] = create_function('','
$args = func_get_args();
return hookpress_generic_action('.$id.',$args);
');
$arg_count = 0;
if (isset($desc['type']) && $desc['type'] == 'filter')
$arg_count = count($hookpress_filters[$desc['hook']]);
else {
$hooks = apply_filters( 'hookpress_actions', $hookpress_actions );
$arg_count = count($hooks[$desc['hook']]);
}
add_filter($desc['hook'], $hookpress_callbacks[$id], HOOKPRESS_PRIORITY, $arg_count);
}
}
}
function hookpress_generic_action($id,$args) {
global $hookpress_version, $wpdb, $hookpress_actions, $hookpress_filters, $wp_version;
$webhooks = hookpress_get_hooks( );
$desc = $webhooks[$id];
do_action( 'hookpress_hook_fired', $desc );
$obj = array();
// generate the expected argument names
if (isset($desc['type']) && $desc['type'] == 'filter')
$arg_names = $hookpress_filters[$desc['hook']];
else {
$hooks = apply_filters( 'hookpress_actions', $hookpress_actions );
$arg_names = $hooks[$desc['hook']];
}
foreach($args as $i => $arg) {
$newobj = apply_filters('hookpress_get_data', $obj, $arg_names[$i], $arg);
switch($arg_names[$i]) {
case 'POST':
case 'ATTACHMENT':
$newobj = get_post($arg,ARRAY_A);
if ($arg_names[$i] == 'POST') {
$newobj["post_url"] = get_permalink($newobj["ID"]);
$meta = get_post_meta($newobj["ID"], '', true);
$newobj = array_merge($meta, $newobj);
$f_im_data = get_the_post_thumbnail($newobj["ID"]);
$ex = [];
preg_match_all("/([a-z]+)=\"([a-z0-9A-Z_\-\.\/:]+)\"/", $f_im_data, $ex);
foreach ($ex[1] as $key => $value) {
if ( array_search("featured_image_{$value}",$desc['fields']) ) {
error_log(">>>> featured_image_{$value}");
$newobj["featured_image_{$value}"] = $ex[ 2 ][ $key ];
}
unset($key,$value);
}
error_log(print_r($desc['fields'],true));
error_log(print_r($ex[1],true));
unset($f_im_data,$ex);
}
if (wp_is_post_revision($arg)) {
$parent = get_post(wp_is_post_revision($arg));
foreach ($parent as $key => $val) {
$newobj["parent_$key"] = $val;
}
$newobj["parent_post_url"] = get_permalink($newobj["parent_ID"]);
}
break;
case 'COMMENT':
$arg = (int) $arg;
$newobj = (array) get_comment( $arg );
break;
case 'CATEGORY':
$newobj = $wpdb->get_row("select * from $wpdb->categories where cat_ID = $arg",ARRAY_A);
break;
case 'USER':
$newobj = $wpdb->get_row("select * from $wpdb->users where ID = $arg",ARRAY_A);
break;
case 'LINK':
$newobj = $wpdb->get_row("select * from $wpdb->links where link_id = $arg",ARRAY_A);
break;
case 'TAG_OBJ':
$newobj = (array) $arg;
break;
case 'USER_OBJ':
$newobj = (array) $arg;
case 'OLD_USER_OBJ':
$newobj = array_map(create_function('$x','return "old_$x";'), (array) $arg);
default:
$newobj[$arg_names[$i]] = $arg;
}
$obj = array_merge($obj,$newobj);
}
// take only the fields we care about
$obj_to_post = array_intersect_key($obj,array_flip($desc['fields']));
$obj_to_post['hook'] = $desc['hook'];
$user_agent = "HookPress/{$hookpress_version} (compatible; WordPress {$wp_version}; +http://mitcho.com/code/hookpress/)";
$request = apply_filters( 'hookpress_request', array('user-agent' => $user_agent, 'body' => $obj_to_post, 'referer' => get_bloginfo('url')) );
return wp_remote_post($desc['url'], $request);
}