Skip to content

Commit

Permalink
added after update hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Hube2 committed May 22, 2019
1 parent 2b4de85 commit e7ffb47
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
41 changes: 26 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
```
10 changes: 9 additions & 1 deletion acf-post2post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 === '') {
Expand All @@ -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

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
30 changes: 25 additions & 5 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down

0 comments on commit e7ffb47

Please sign in to comment.