From b8830f1a117a76491484cecfac55fc8e4361d5bf Mon Sep 17 00:00:00 2001 From: Romulo De Lazzari Date: Wed, 13 Jun 2018 16:38:40 +0100 Subject: [PATCH] delete connected poll when user remove poll using the checkbox option --- app/CustomFields.php | 13 +++++++++++++ tests/TestCustomFields.php | 1 + 2 files changed, 14 insertions(+) diff --git a/app/CustomFields.php b/app/CustomFields.php index 6592d49..2d58a79 100644 --- a/app/CustomFields.php +++ b/app/CustomFields.php @@ -104,6 +104,7 @@ public function register_article_fields( array $post_types ) { public function load_filters() { add_filter( 'acf/fields/post_object/query/name=article_poll', [ $this, 'query_polls' ] ); + add_filter( 'acf/update_value/name=article_has_poll', [ $this, 'delete_selected_poll' ], 10, 2 ); add_filter( 'acf/update_value/name=poll_date_limit', [ $this, 'add_fake_date' ] ); add_filter( 'acf/load_value/name=poll_date_limit', [ $this, 'empty_if_fake_date' ] ); } @@ -122,6 +123,18 @@ public function query_polls( $args ) { return $args; } + /** + * If the editor uncheck the `article has poll` option + * then also delete the poll connected with this article + */ + public function delete_selected_poll( $value, $post_id ) { + if ( ! (bool) $value ) { + delete_field( 'article_poll', $post_id ); + } + + return $value; + } + public function add_fake_date( $value ) { if ( empty( $value ) ) { $value = '2080-12-31 23:59:59'; diff --git a/tests/TestCustomFields.php b/tests/TestCustomFields.php index 39cd2b6..4ae1eeb 100644 --- a/tests/TestCustomFields.php +++ b/tests/TestCustomFields.php @@ -39,6 +39,7 @@ public function test_load_filters() { $obj->load_filters(); $this->assertTrue(has_filter('acf/fields/post_object/query/name=article_poll', [$obj, 'query_polls'])); + $this->assertTrue(has_filter('acf/update_value/name=article_has_poll', [$obj, 'delete_selected_poll'])); $this->assertTrue(has_filter('acf/update_value/name=poll_date_limit', [$obj, 'add_fake_date'])); $this->assertTrue(has_filter('acf/load_value/name=poll_date_limit', [$obj, 'empty_if_fake_date'])); }