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

PHP7 support - changed 'ereg'->'preg_match' & 'split'->'explode' #22

Open
wants to merge 3 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
2 changes: 1 addition & 1 deletion hookpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ 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
200 changes: 120 additions & 80 deletions includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,43 @@ 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';
$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')
$hooks = array_keys($hookpress_actions);

if ($desc['type'] == 'action') {
$actions = apply_filters( 'hookpress_actions', $hookpress_actions );
$hooks = array_keys($actions);
}
if ($desc['type'] == 'filter')
$hooks = array_keys($hookpress_filters);
?>
Expand All @@ -58,27 +73,27 @@ 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'>
<?php
if ($desc['type'] == 'action')
echo 'Action:';
if ($desc['type'] == 'filter')
echo 'Filter:';
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"':'';
<?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') . "' />";
?>
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/>
Expand All @@ -90,19 +105,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))
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 );
Expand All @@ -116,7 +133,7 @@ function hookpress_print_edit_webhook( $id ){
$selected = 'selected="true"';
}
}
$field = esc_html( $field );
$field = esc_html( $field );
echo "<option value='$field' $selected>$field</option>";
}
$desc['url'] = esc_html( $desc['url'] );
Expand All @@ -135,40 +152,40 @@ function hookpress_print_edit_webhook( $id ){
}

function hookpress_print_webhook_row( $id ) {
# $webhooks = get_option('hookpress_webhooks');
# $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 "
$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>
Expand All @@ -181,7 +198,7 @@ function hookpress_print_webhook_row( $id ) {
function hookpress_print_webhooks_table() {
global $page;
$webhooks = null;
# $webhooks = get_option('hookpress_webhooks');
# $webhooks = get_option('hookpress_webhooks');
$webhooks = hookpress_get_hooks( );
?>
<table class="widefat" cellspacing="0" id="webhooks">
Expand All @@ -205,13 +222,13 @@ function hookpress_print_webhooks_table() {
<?php

if ( !empty($webhooks) ) :
foreach ( (array)$webhooks as $id => $desc) :
if( !empty( $desc ) ):
hookpress_print_webhook_row( $id );
endif;
endforeach;

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

if( !empty( $desc ) ):
hookpress_print_webhook_row( $id );
endif;
endforeach;
endif;
?>
</tbody>
Expand All @@ -224,9 +241,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 +257,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 +269,59 @@ 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);

$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;
Expand All @@ -308,14 +348,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);
}
}
Loading