From 2bad0492176ae2033f63cd06086115564370e257 Mon Sep 17 00:00:00 2001 From: Sohail Haider Date: Sat, 7 Oct 2017 12:55:34 +0500 Subject: [PATCH 1/3] PHP7 support - changed 'ereg'->'preg_match' & 'split'->'explode' If you have php7 installed it won't let you add webhook unless you make these changes. Changes are pretty forward 'ereg' & 'split' were introduced in php5 but now they are deprecated --- includes.php | 2 +- services.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/includes.php b/includes.php index 467b3d7..3f8f010 100644 --- a/includes.php +++ b/includes.php @@ -97,7 +97,7 @@ function hookpress_print_edit_webhook( $id ){ $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; diff --git a/services.php b/services.php index 8f6b9c0..c698909 100644 --- a/services.php +++ b/services.php @@ -10,7 +10,7 @@ function hookpress_ajax_get_fields() { $fields = array(); if (is_array($args)) { 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; @@ -46,7 +46,7 @@ function hookpress_ajax_add_fields() { 'type'=>$_POST['type'], 'hook'=>$_POST['hook'], 'enabled'=>$_POST['enabled'], - 'fields'=>split(',',$_POST['fields']) + 'fields'=>explode(',',$_POST['fields']) ); hookpress_update_hook( $id, $edithook ); @@ -57,7 +57,7 @@ function hookpress_ajax_add_fields() { 'url'=>$_POST['url'], 'type'=>$_POST['type'], 'hook'=>$_POST['hook'], - 'fields'=>split(',',$_POST['fields']), + 'fields'=>explode(',',$_POST['fields']), 'enabled'=>true ); $id = hookpress_add_hook($newhook); @@ -76,7 +76,7 @@ function hookpress_ajax_set_enabled() { $id = $_POST['id']; $enabled = $_POST['enabled']; - $nonce_compare = ($enabled == 'true'?'activate-webhook-' . $id:'deactivate-webhook-' . $id ); + $nonce_compare = ($enabled == 'true'?'activate-webhook-' . $id:'deactivate-webhook-' . $id ); if ( wp_verify_nonce( $nonce, $nonce_compare ) ) : From c12e2ebe47e281ecea6b96c2996340fdc84907dd Mon Sep 17 00:00:00 2001 From: Sohail Haider Date: Sat, 7 Oct 2017 13:13:11 +0500 Subject: [PATCH 2/3] Custom fields included as well, thansk to @davidyaha --- hookpress.php | 2 +- includes.php | 179 ++++++++++++++++++++++++++++---------------------- services.php | 64 +++++++++--------- xaa | 1 + 4 files changed, 136 insertions(+), 110 deletions(-) create mode 100644 xaa diff --git a/hookpress.php b/hookpress.php index 9cab3ee..c2df166 100644 --- a/hookpress.php +++ b/hookpress.php @@ -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'); diff --git a/includes.php b/includes.php index 3f8f010..a7325ef 100644 --- a/includes.php +++ b/includes.php @@ -27,15 +27,23 @@ 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"); + + $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); } @@ -43,12 +51,14 @@ function hookpress_print_edit_webhook( $id ){ ?> @@ -58,27 +68,27 @@ function hookpress_print_edit_webhook( $id ){ - @@ -181,7 +193,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( ); ?>
> +> >
"; - ?> + echo ""; + } + $nonce_submit = ""; + ?>

@@ -90,11 +100,13 @@ function hookpress_print_edit_webhook( $id ){ "; - $action = '' . __('Deactivate') . ''; - else : - $nonce_action = ""; -# $action = '' . __('Activate') . ''; - $action = '' . __('Activate') . ''; - endif; - $nonce_delete = ""; - $delete = '' . __('Delete') . ''; - if( count( $desc['fields'] ) > 1 ) { - $desc['fields'] = array_map( 'esc_html', $desc['fields'] ); - $fields = implode(', ', $desc['fields'] ); - } else - $fields = esc_html( $desc['fields'][0] ); - - $edit = '' . __('Edit') . ''; - - $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 = ""; + $action = '' . __('Deactivate') . ''; + else : + $nonce_action = ""; + # $action = '' . __('Activate') . ''; + $action = '' . __('Activate') . ''; + endif; + + $nonce_delete = ""; + $delete = '' . __('Delete') . ''; + if( count( $desc['fields'] ) > 1 ) { + $desc['fields'] = array_map( 'esc_html', $desc['fields'] ); + $fields = implode(', ', $desc['fields'] ); + } else + $fields = esc_html( $desc['fields'][0] ); + + $edit = '' . __('Edit') . ''; + + $activeornot = $desc['enabled'] ? 'active' : 'inactive'; + + $html_safe['hook'] = esc_html( $desc['hook'] ); + $html_safe['url'] = esc_html( $desc['url'] ); + + echo "
{$html_safe['hook']}
$nonce_action $nonce_delete$edit | $delete | $action
@@ -205,13 +217,13 @@ function hookpress_print_webhooks_table() { $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; ?> @@ -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; @@ -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); } @@ -250,30 +264,37 @@ 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); + } + if (wp_is_post_revision($arg)) { $parent = get_post(wp_is_post_revision($arg)); foreach ($parent as $key => $val) { @@ -281,7 +302,7 @@ function hookpress_generic_action($id,$args) { } $newobj["parent_post_url"] = get_permalink($newobj["parent_ID"]); } - + break; case 'COMMENT': $arg = (int) $arg; @@ -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); -} +} \ No newline at end of file diff --git a/services.php b/services.php index c698909..7ae1100 100644 --- a/services.php +++ b/services.php @@ -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']]; @@ -38,30 +40,30 @@ function hookpress_ajax_add_fields() { if ( wp_verify_nonce( $nonce, $nonce_compare ) ) : - if( isset($_POST['id']) ){ - - $id = (int) $_POST['id']; - $edithook = array( - 'url'=>$_POST['url'], - 'type'=>$_POST['type'], - 'hook'=>$_POST['hook'], - 'enabled'=>$_POST['enabled'], - 'fields'=>explode(',',$_POST['fields']) - ); - hookpress_update_hook( $id, $edithook ); - - } else { - - // register the new webhook - $newhook = array( - 'url'=>$_POST['url'], - 'type'=>$_POST['type'], - 'hook'=>$_POST['hook'], - 'fields'=>explode(',',$_POST['fields']), - 'enabled'=>true - ); - $id = hookpress_add_hook($newhook); - } + if( isset($_POST['id']) ){ + + $id = (int) $_POST['id']; + $edithook = array( + 'url'=>$_POST['url'], + 'type'=>$_POST['type'], + 'hook'=>$_POST['hook'], + 'enabled'=>$_POST['enabled'], + 'fields'=>explode(',',$_POST['fields']) + ); + hookpress_update_hook( $id, $edithook ); + + } else { + + // register the new webhook + $newhook = array( + 'url'=>$_POST['url'], + 'type'=>$_POST['type'], + 'hook'=>$_POST['hook'], + 'fields'=>explode(',',$_POST['fields']), + 'enabled'=>true + ); + $id = hookpress_add_hook($newhook); + } endif; @@ -104,7 +106,7 @@ function hookpress_ajax_delete_hook() { if ( !wp_verify_nonce( $nonce, $nonce_compare ) ) die("ERROR: invalid nonce"); - if (!$webhooks[$id]) + if (!$webhooks[$id]) die("ERROR: no webhook found for that id"); hookpress_delete_hook( $id ); echo "ok"; @@ -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); @@ -134,4 +138,4 @@ function hookpress_ajax_get_hooks() { } } exit; -} +} \ No newline at end of file diff --git a/xaa b/xaa new file mode 100644 index 0000000..9807090 --- /dev/null +++ b/xaa @@ -0,0 +1 @@ +git commit -m "PHP7 support - changed `ereg`->`preg_match` & `split`->`explode`" -m "If you have php7 installed it won't let you add webhook unless you make these changes. Changes are pretty forward `ereg` & `split` were introduced in php5 but now they are deprecated" From 6b16096579d7f6c6e02074c21dadec311a36f4e2 Mon Sep 17 00:00:00 2001 From: Sohail Haider Date: Sat, 7 Oct 2017 13:55:37 +0500 Subject: [PATCH 3/3] Sending featured images as well Thanks to @evaldobarbosa --- includes.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/includes.php b/includes.php index a7325ef..8729a47 100644 --- a/includes.php +++ b/includes.php @@ -30,10 +30,15 @@ function hookpress_get_fields( $type ) { 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') @@ -293,6 +298,20 @@ function hookpress_generic_action($id,$args) { $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)) {