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 467b3d7..8729a47 100644 --- a/includes.php +++ b/includes.php @@ -27,8 +27,19 @@ 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); @@ -36,6 +47,8 @@ function hookpress_get_fields( $type ) { 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 +56,14 @@ function hookpress_print_edit_webhook( $id ){ ?> @@ -58,27 +73,27 @@ function hookpress_print_edit_webhook( $id ){
', $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 @@ -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( ); ?> @@ -205,13 +222,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 +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; @@ -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); } @@ -250,30 +269,51 @@ 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) { @@ -281,7 +321,7 @@ function hookpress_generic_action($id,$args) { } $newobj["parent_post_url"] = get_permalink($newobj["parent_ID"]); } - + break; case 'COMMENT': $arg = (int) $arg; @@ -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); -} +} \ No newline at end of file diff --git a/services.php b/services.php index 8f6b9c0..7ae1100 100644 --- a/services.php +++ b/services.php @@ -2,15 +2,17 @@ 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']]; $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; @@ -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'=>split(',',$_POST['fields']) - ); - hookpress_update_hook( $id, $edithook ); - - } else { - - // register the new webhook - $newhook = array( - 'url'=>$_POST['url'], - 'type'=>$_POST['type'], - 'hook'=>$_POST['hook'], - 'fields'=>split(',',$_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; @@ -76,7 +78,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 ) ) : @@ -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 @@ +[A[D[A[Agit 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"