Skip to content

Commit

Permalink
Skip save if only_save_to_post_parent
Browse files Browse the repository at this point in the history
  • Loading branch information
mboynes committed Feb 20, 2016
1 parent 915af49 commit a53c3ec
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
3 changes: 3 additions & 0 deletions php/class-fieldmanager-autocomplete.php
Expand Up @@ -153,7 +153,10 @@ public function presave_alter_values( $values, $current_values = array() ) {

if ( ! empty( $this->datasource->only_save_to_taxonomy ) ) {
$this->skip_save = true;
} elseif ( ! empty( $this->datasource->only_save_to_post_parent ) ) {
$this->skip_save = true;
}

return $this->datasource->presave_alter_values( $this, $values, $current_values );
}

Expand Down
2 changes: 1 addition & 1 deletion php/class-fieldmanager-group.php
Expand Up @@ -325,7 +325,7 @@ public function presave( $values, $current_values = array() ) {
elseif ( empty( $values[ $element->name ] ) ) unset( $values[ $element->name ] );
}

if ( ! empty( $element->datasource->only_save_to_taxonomy ) ) {
if ( ! empty( $element->datasource->only_save_to_taxonomy ) || ! empty( $element->datasource->only_save_to_post_parent ) ) {
unset( $values[ $element->name ] );
continue;
}
Expand Down
2 changes: 2 additions & 0 deletions php/class-fieldmanager-options.php
Expand Up @@ -224,6 +224,8 @@ public function presave_alter_values( $values, $current_values = array() ) {
if ( !empty( $this->datasource ) ) {
if ( ! empty( $this->datasource->only_save_to_taxonomy ) ) {
$this->skip_save = true;
} elseif ( ! empty( $this->datasource->only_save_to_post_parent ) ) {
$this->skip_save = true;
}
return $this->datasource->presave_alter_values( $this, $values, $current_values );
}
Expand Down
21 changes: 10 additions & 11 deletions tests/php/test-fieldmanager-datasource-post.php
Expand Up @@ -242,8 +242,7 @@ public function test_alter_child_invalid_publish() {
* Test save_to_post_parent logic
*/
public function test_post_parent() {
$test_data = $this->child_post_a->ID;
$children = new Fieldmanager_Autocomplete( array(
$fm = new Fieldmanager_Autocomplete( array(
'name' => 'test_parent',
'datasource' => new Fieldmanager_Datasource_Post( array(
'query_args' => array(
Expand All @@ -252,18 +251,17 @@ public function test_post_parent() {
'save_to_post_parent' => true,
) ),
) );
$this->save_values( $children, $this->parent_post, $test_data );
$parent = get_post( $this->parent_post->ID );
$this->assertEquals( $parent->post_parent, $this->child_post_a->ID );
$this->assertEquals( get_post_meta( $this->parent_post->ID, 'test_parent', true ), $this->child_post_a->ID );
$this->save_values( $fm, $this->child_post_a, $this->parent_post->ID );
$child = get_post( $this->child_post_a->ID );
$this->assertEquals( $this->parent_post->ID, $child->post_parent );
$this->assertEquals( $this->parent_post->ID, get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );
}

/**
* Test save_to_post_parent logic
*/
public function test_post_parent_nested() {
$test_data = array( 'parent' => $this->child_post_a->ID );
$children = new Fieldmanager_Group( array(
$fm = new Fieldmanager_Group( array(
'name' => 'test_parent',
'children' => array(
'parent' => new Fieldmanager_Autocomplete( 'Post Parent', array(
Expand All @@ -277,9 +275,10 @@ public function test_post_parent_nested() {
) ),
),
) );
$this->save_values( $children, $this->parent_post, $test_data );
$parent = get_post( $this->parent_post->ID );
$this->assertEquals( $parent->post_parent, $this->child_post_a->ID );
$this->save_values( $fm, $this->child_post_a, array( 'parent' => $this->parent_post->ID ) );
$child = get_post( $this->child_post_a->ID );
$this->assertEquals( $this->parent_post->ID, $child->post_parent );
$this->assertEquals( '', get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );
}

/**
Expand Down

0 comments on commit a53c3ec

Please sign in to comment.