Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added custom fields and hooks around actions code #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions hookpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Plugin Name: HookPress
Plugin URI: http://mitcho.com/code/hookpress/
Description: HookPress turns all of your WordPress-internal hooks into webhooks. Possible uses include generating push notifications or using non-PHP web technology to extend WordPress. Read more about webhooks at <a href='http://webhooks.org/'>the webhooks site</a>.
Version: 1.14
Version: 1.15
Author: mitcho (Michael Yoshitaka Erlewine)
Author URI: http://mitcho.com/
Donate link: http://tinyurl.com/donatetomitcho
*/

define('HOOKPRESS_PRIORITY',12838790321);
$hookpress_version = "1.14";
$hookpress_version = "1.15";
require('includes.php');

function hookpress_init() {
Expand All @@ -21,9 +21,11 @@ function hookpress_init() {
update_option('hookpress_version',$hookpress_version);

add_action('admin_menu', 'hookpress_config_page');
hookpress_register_hooks();

}

add_action('init', 'hookpress_init');
hookpress_register_hooks();

// register ajax service
add_action('wp_ajax_hookpress_get_fields', 'hookpress_ajax_get_fields');
Expand Down
91 changes: 56 additions & 35 deletions includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,38 @@ function hookpress_get_fields( $type ) {
}

// if it's a POST, we have a URL for it as well.
if ($type == 'POST' || $type == 'PARENT_POST')
if ($type == 'POST' || $type == 'PARENT_POST') {

$fields[] = 'post_url';

$meta_keys = $wpdb->get_col("select distinct(meta_key) from $wpdb->postmeta");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose here? Adding separate default columns might be something to put in a separate patch.


$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')
$hooks = array_keys($hookpress_actions);

if ($desc['type'] == 'action') {
$actions = apply_filters( 'hookpress_actions', $hookpress_actions );
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I would have thought to filter much earlier, like in init.

$hooks = array_keys($actions);
}
if ($desc['type'] == 'filter')
$hooks = array_keys($hookpress_filters);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe also add a filter to the set of filters, while we're at it?

?>
Expand All @@ -58,7 +68,7 @@ function hookpress_print_edit_webhook( $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>
<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'>
Expand All @@ -70,7 +80,7 @@ function hookpress_print_edit_webhook( $id ){
?>
</label></td>
<td><select name='edithook' id='edithook'>
<?php
<?php
sort($hooks);
foreach ($hooks as $hook) {
$selected = ($hook == $desc['hook'])?'selected="true"':'';
Expand All @@ -90,19 +100,21 @@ function hookpress_print_edit_webhook( $id ){
<select style='vertical-align: top' name='editfields' id='editfields' multiple='multiple' size='8'>
<?php
global $wpdb, $hookpress_actions, $hookpress_filters;
if ($desc['type'] == 'action')
$args = $hookpress_actions[$desc['hook']];
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 (ereg('[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 );
Expand Down Expand Up @@ -140,7 +152,7 @@ function hookpress_print_webhook_row( $id ) {
$desc = $webhooks[$id];

if( !empty( $desc ) ):

$is_active = $desc['enabled'];
$html_safe['id'] = esc_html( $id );

Expand All @@ -160,9 +172,9 @@ function hookpress_print_webhook_row( $id ) {
$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'] );
Expand Down Expand Up @@ -205,10 +217,10 @@ function hookpress_print_webhooks_table() {
<?php

if ( !empty($webhooks) ) :

foreach ( (array)$webhooks as $id => $desc) :
if( !empty( $desc ) ):

if( !empty( $desc ) ):
hookpress_print_webhook_row( $id );
endif;
endforeach;
Expand All @@ -224,9 +236,9 @@ function hookpress_print_webhooks_table() {
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;

Expand All @@ -240,8 +252,10 @@ function hookpress_register_hooks() {
$arg_count = 0;
if (isset($desc['type']) && $desc['type'] == 'filter')
$arg_count = count($hookpress_filters[$desc['hook']]);
else
$arg_count = count($hookpress_actions[$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);
}
Expand All @@ -250,38 +264,45 @@ function hookpress_register_hooks() {

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
$arg_names = $hookpress_actions[$desc['hook']];

$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 = array();
$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')
if ($arg_names[$i] == 'POST') {
$newobj["post_url"] = get_permalink($newobj["ID"]);


$meta = get_post_meta($newobj["ID"], '', true);
$newobj = array_merge($meta, $newobj);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This too goes with the above comment -- adding extra columns should be a separate patch.

}

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;
Expand All @@ -308,14 +329,14 @@ function hookpress_generic_action($id,$args) {
}
$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);
}
10 changes: 7 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Donate link: http://tinyurl.com/donatetomitcho
Tags: hook, filter, action, plugin, webhook, webhooks, notification, internal
Requires at least: 3.6
Tested up to: 4.1
Stable tag: 1.14
Stable tag: 1.15

HookPress turns your WordPress-internal hooks into webhooks. Possible uses include generating push notifications or extending WordPress with non-PHP.

Expand Down Expand Up @@ -40,6 +40,10 @@ HookPress currently makes requests synchronously so can measurably affect perfor

== Changelog ==

= 1.15 =
* Added custom fields to post related hooks.
* Added hooks around action handlers to allow addition of hooks from an external plugin.

= 1.14 =
* Additional security hardening for `test.php`, now no longer bundled as a `.php`

Expand All @@ -62,12 +66,12 @@ HookPress currently makes requests synchronously so can measurably affect perfor
* added webhook editing
* Various miscellaneous features: added nonces for security, modularized the code a bit, etc.
= 0.1.8 =
* Added more filters�now covers basic + comment filters
* Added more filters�now covers basic + comment filters
* Marking as compatible through 2.9.9, because it should be.
* Added version checking and beta offers to the options screen.
= 0.1.7 =
* Now supports basically all actions. (Still no actions with no arguments.)
* Added more filter options�now covers all basic database read filters.
* Added more filter options�now covers all basic database read filters.
= 0.1.6 =
* Added another batch of actions. (Still no actions with no arguments, though... something to consider.)
* Fixed hooks which referred to the users and links tables.
Expand Down
12 changes: 8 additions & 4 deletions services.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

function hookpress_ajax_get_fields() {
global $wpdb, $hookpress_actions, $hookpress_filters;
if ($_POST['type'] == 'action')
$args = $hookpress_actions[$_POST['hook']];
if ($_POST['type'] == 'action') {
$hooks = apply_filters( 'hookpress_actions', $hookpress_actions );
$args = $hooks[$_POST['hook']];
}
if ($_POST['type'] == 'filter')
$args = $hookpress_filters[$_POST['hook']];

Expand Down Expand Up @@ -119,8 +121,10 @@ function hookpress_ajax_edit_hook( $id ) {

function hookpress_ajax_get_hooks() {
global $wpdb, $hookpress_actions, $hookpress_filters;
if ($_POST['type'] == 'action')
$hooks = array_keys($hookpress_actions);
if ($_POST['type'] == 'action') {
$hooks = apply_filters( 'hookpress_actions', $hookpress_actions );
$hooks = array_keys($hooks);
}
if ($_POST['type'] == 'filter')
$hooks = array_keys($hookpress_filters);

Expand Down