Skip to content

Commit

Permalink
delete connected poll when user remove poll using the checkbox option
Browse files Browse the repository at this point in the history
  • Loading branch information
romulodl committed Jun 13, 2018
1 parent 7c6a41e commit b8830f1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/CustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] );
}
Expand All @@ -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';
Expand Down
1 change: 1 addition & 0 deletions tests/TestCustomFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
}
Expand Down

0 comments on commit b8830f1

Please sign in to comment.