diff --git a/README.md b/README.md index b2e5eea..7794b58 100644 --- a/README.md +++ b/README.md @@ -63,26 +63,37 @@ You can disable automatic bidirectional relationships for specific field keys us add_filter('acf/post2post/update_relationships/key=field_XXXXXXXX', '__return_false'); ``` +## After update hooks +There are two actions that can be used after a post is updated and passes a single post ID. Please make sure you see the subtle difference in these two hooks. + +The first is run after each related post is updated +``` +add_action('acf/post2post/relationship_updated', 'my_post_updated_action'); +function my_post_updated_action($post_id) { + // $post_id == the post ID that was updated + // do something after the related post is updated +} +``` + +The second is run after all posts are updated and passes an array of post IDs. +``` +add_action('acf/post2post/relationships_updated', 'my_post_updated_action'); +function my_post_updated_action($posts) { + // $posts == and array of post IDs that were updated + // do something to all posts after update + foreach ($posts as $post_id) { + // do something to post + } +} +``` + ## So why do this? -I did not actually create this plugin to make it easier on the person that's managing a site, although that is -an added side benifit. The main reason for implementing some way to make relationship and post object fields -biderectional is that doing reverse relationship queries for ACF is a huge PITA. I completely understand why this is -not built into ACF. If it was built in then Elliot would need to deal with relationship fields in repeaters and -flex fields and nested repeaters. And then there is the problem of relationships with different post types and fields -with different names. It would be a deep dark rabbit hole from which I don't think he'd return. There are too many -parameters. On the other hand, filters and actions can be created with a finite number of options to do the work that's -needed. +I did not actually create this plugin to make it easier on the person that's managing a site, although that is an added side benifit. The main reason for implementing some way to make relationship and post object fields biderectional is that doing reverse relationship queries for ACF is a huge PITA. I completely understand why this is not built into ACF. If it was built in then Elliot would need to deal with relationship fields in repeaters and +flex fields and nested repeaters. And then there is the problem of relationships with different post types and fields with different names. It would be a deep dark rabbit hole from which I don't think he'd return. There are too many parameters and possibilities. On the other hand, filters and actions can be created with a finite number of options to do the work that's needed. #### Automatic Updates Github updater support has been removed. This plugin has been published to WordPress.Org here https://wordpress.org/plugins/post-2-post-for-acf/. If you are having problems updating please try installing from there. -#### Remove Nag -You may notice that I've started adding a little nag to my plugins. It's just a box on some pages that lists my -plugins that you're using with a request to consider making a donation for using them. If you want to disable them -add the following filter to your functions.php file. -``` -add_filter('remove_hube2_nag', '__return_true'); -``` diff --git a/acf-post2post.php b/acf-post2post.php index 8afb07b..6f6e629 100644 --- a/acf-post2post.php +++ b/acf-post2post.php @@ -4,7 +4,7 @@ Plugin Name: ACF Post-2-Post Plugin URI: https://github.com/Hube2/acf-post2post Description: Two way relationship fields - Version: 1.3.2 + Version: 1.4.0 Author: John A. Huebner II Author URI: https://github.com/Hube2 GitHub Plugin URI: https://github.com/Hube2/acf-post2post @@ -51,6 +51,7 @@ public function update_relationship_field($value, $post_id, $field) { if (!$update) { return $value; } + $updated_posts = array(); $field_name = $field['name']; $previous = maybe_unserialize(get_post_meta($post_id, $field_name, true)); if ($previous === '') { @@ -72,14 +73,19 @@ public function update_relationship_field($value, $post_id, $field) { foreach ($previous as $related_id) { if (!in_array($related_id, $new)) { $this->remove_relationship($related_id, $field_name, $post_id); + $updated_posts[] = $related_id; } } } if (count($new)) { foreach ($new as $related_id) { $this->add_relationship($related_id, $field_name, $post_id); + $updated_posts[] = $related_id; } } + if (count($updated_posts)) { + do_action('acf/post2post/relationships_updated', $updated_posts); + } return $value; } // end public function update_relationship_field @@ -123,6 +129,7 @@ private function remove_relationship($post_id, $field_name, $related_id) { } update_post_meta($post_id, $field_name, $new_values); update_post_meta($post_id, '_'.$field_name, $field['key']); + do_action('acf/post2post/relationship_updated', $post_id); } // end private function remove_relationship private function add_relationship($post_id, $field_name, $related_id) { @@ -186,6 +193,7 @@ private function add_relationship($post_id, $field_name, $related_id) { } update_post_meta($post_id, $field_name, $value); update_post_meta($post_id, '_'.$field_name, $field['key']); + do_action('acf/post2post/relationship_updated', $post_id); } // end private function add_relationship public function get_field($post_id, $field_name) { diff --git a/readme.txt b/readme.txt index cf7291d..ba27597 100644 --- a/readme.txt +++ b/readme.txt @@ -3,7 +3,7 @@ Contributors: Hube2 Tags: acf, advanced custom fields, add on, bidirectional, 2 way, two way, relationship Requires at least: 4.0 Tested up to: 5.1 -Stable tag: 1.3.2 +Stable tag: 1.4.0 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -89,17 +89,37 @@ You can disable automatic bidirectional relationships for specific field keys us // field_XXXXXXXX = the field key of the field // you want to disable bidirectional relationships for add_filter('acf/post2post/update_relationships/key=field_XXXXXXXX', '__return_false'); -` -== Remove Nag == -If you would like to remove my little nag that appears on some admin pages add the following to your functions.php file +== After update hooks == +There are two actions that can be used after a post is updated and passes a single post ID. Please make sure you see the subtle difference in these two hooks. + +The first is run after each related post is updated +` +add_action('acf/post2post/relationship_updated', 'my_post_updated_action'); +function my_post_updated_action($post_id) { + // $post_id == the post ID that was updated + // do something after the related post is updated +} +` + +The second is run after all posts are updated and passes an array of post IDs. ` -add_filter('remove_hube2_nag', '__return_true'); +add_action('acf/post2post/relationships_updated', 'my_post_updated_action'); +function my_post_updated_action($posts) { + // $posts == and array of post IDs that were updated + // do something to all posts after update + foreach ($posts as $post_id) { + // do something to post + } +} ` == Changelog == += 1.4.0 = +* added actions after updates to related posts to allow 3rd party integrations + = 1.3.2 = * removed donation nag