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

Feature/json #8

Open
wants to merge 5 commits 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
4 changes: 2 additions & 2 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 Down
31 changes: 29 additions & 2 deletions includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
require('services.php');
require('hooks.php');

function split_header($carry, $item) {
list($key, $value) = explode(":", $item);
$carry[trim($key)] = trim($value);
return $carry;
}

// OPTIONS

function hookpress_get_fields( $type ) {
Expand Down Expand Up @@ -123,6 +129,24 @@ function hookpress_print_edit_webhook( $id ){
?></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>
<tr><td><label style='font-weight: bold' for='newurlcontenttype'><?php _e("Content-Type",'hookpress');?>: </label></td>
<td>
<select name='editcontentype' id='editcontenttype'>
<?php
$default_content_type = 'application/x-www-form-urlencoded; charset=UTF-8';
$content_types = array($default_content_type, 'application/json');
foreach($content_types as $content_type) {
if ((empty($desc['contenttype']) && $content_type == $default_content_type) || $content_type == $desc['contenttype']) {
$selected = 'selected="true"';
} else {
$selected = '';
}
echo "<option value='$content_type' $selected>$content_type</option>";
}
?>
</select></td></tr>
<tr><td><label style='font-weight: bold' for='newheaders'><?php _e("Headers", 'hookpress');?>: </label></td>
<td><input name='editheaders' id='editheaders' size='40' value="<?php echo empty($desc['headers']) ? "" : implode(",", $desc['headers']); ?>"></input></td></tr>
</table>
<?php echo $nonce_submit; ?>
<center><span id='editindicator'></span><br/>
Expand Down Expand Up @@ -314,8 +338,11 @@ function hookpress_generic_action($id,$args) {
$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')) );
$headers = array_reduce($desc['headers'], 'split_header', array());
$content_type = empty($desc['contenttype']) ? 'application/x-www-form-urlencoded; charset=UTF-8' : $desc['contenttype'];
$headers = array_merge($headers, array('Content-Type' => $content_type));

$request = apply_filters( 'hookpress_request', array('headers' => $headers, 'user-agent' => $user_agent, 'body' => $content_type == 'application/json' ? json_encode($obj_to_post) : $obj_to_post, 'referer' => get_bloginfo('url')) );

return wp_remote_post($desc['url'], $request);
}
4 changes: 3 additions & 1 deletion options.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,14 @@
tb.find('#editindicator').html('<div class="webhooks-spinner">&nbsp;</div>');

id = tb.find('#edit-hook-id').val();

$.ajax({type: 'POST',
url:'admin-ajax.php',
data:'action=hookpress_add_fields'
+'&fields='+tb.find('#editfields').val().join()
+'&url='+tb.find('#editurl').val()
+'&contenttype='+tb.find('#editcontenttype').val()
+'&headers='+tb.find('#editheaders').val()
+'&type='+tb.find('.newtype:checked').attr('id')
+'&hook='+tb.find('#edithook').val()
+'&enabled='+tb.find('#enabled').val()
Expand Down
6 changes: 4 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ If you have a feature request or question, please use the [HookPress support for
HookPress currently makes requests synchronously so can measurably affect performance. I highly recommend using a caching plugin such as [WP-SuperCache](http://ocaoimh.ie/wp-super-cache/) to stem the performance hit. If your filters' results are time-sensitive or dependent on external data sources as well, make sure to set an appropriate cache expiration time.

== Changelog ==
= 1.15 =
* Added basic support for JSON webhooks and header based authentication.

= 1.14 =
* Additional security hardening for `test.php`, now no longer bundled as a `.php`
Expand All @@ -62,12 +64,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 filtersnow 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 optionsnow 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
4 changes: 3 additions & 1 deletion services.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ function hookpress_ajax_add_fields() {
'type'=>$_POST['type'],
'hook'=>$_POST['hook'],
'enabled'=>$_POST['enabled'],
'fields'=>split(',',$_POST['fields'])
'contenttype'=>$_POST['contenttype'],
'fields'=>split(',',$_POST['fields']),
'headers'=>split(',',$_POST['headers'])
);
hookpress_update_hook( $id, $edithook );

Expand Down