Skip to content

Commit a53c3ec

Browse files
committed
Skip save if only_save_to_post_parent
1 parent 915af49 commit a53c3ec

4 files changed

+16
-12
lines changed

php/class-fieldmanager-autocomplete.php

+3
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ public function presave_alter_values( $values, $current_values = array() ) {
153153

154154
if ( ! empty( $this->datasource->only_save_to_taxonomy ) ) {
155155
$this->skip_save = true;
156+
} elseif ( ! empty( $this->datasource->only_save_to_post_parent ) ) {
157+
$this->skip_save = true;
156158
}
159+
157160
return $this->datasource->presave_alter_values( $this, $values, $current_values );
158161
}
159162

php/class-fieldmanager-group.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public function presave( $values, $current_values = array() ) {
325325
elseif ( empty( $values[ $element->name ] ) ) unset( $values[ $element->name ] );
326326
}
327327

328-
if ( ! empty( $element->datasource->only_save_to_taxonomy ) ) {
328+
if ( ! empty( $element->datasource->only_save_to_taxonomy ) || ! empty( $element->datasource->only_save_to_post_parent ) ) {
329329
unset( $values[ $element->name ] );
330330
continue;
331331
}

php/class-fieldmanager-options.php

+2
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ public function presave_alter_values( $values, $current_values = array() ) {
224224
if ( !empty( $this->datasource ) ) {
225225
if ( ! empty( $this->datasource->only_save_to_taxonomy ) ) {
226226
$this->skip_save = true;
227+
} elseif ( ! empty( $this->datasource->only_save_to_post_parent ) ) {
228+
$this->skip_save = true;
227229
}
228230
return $this->datasource->presave_alter_values( $this, $values, $current_values );
229231
}

tests/php/test-fieldmanager-datasource-post.php

+10-11
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,7 @@ public function test_alter_child_invalid_publish() {
242242
* Test save_to_post_parent logic
243243
*/
244244
public function test_post_parent() {
245-
$test_data = $this->child_post_a->ID;
246-
$children = new Fieldmanager_Autocomplete( array(
245+
$fm = new Fieldmanager_Autocomplete( array(
247246
'name' => 'test_parent',
248247
'datasource' => new Fieldmanager_Datasource_Post( array(
249248
'query_args' => array(
@@ -252,18 +251,17 @@ public function test_post_parent() {
252251
'save_to_post_parent' => true,
253252
) ),
254253
) );
255-
$this->save_values( $children, $this->parent_post, $test_data );
256-
$parent = get_post( $this->parent_post->ID );
257-
$this->assertEquals( $parent->post_parent, $this->child_post_a->ID );
258-
$this->assertEquals( get_post_meta( $this->parent_post->ID, 'test_parent', true ), $this->child_post_a->ID );
254+
$this->save_values( $fm, $this->child_post_a, $this->parent_post->ID );
255+
$child = get_post( $this->child_post_a->ID );
256+
$this->assertEquals( $this->parent_post->ID, $child->post_parent );
257+
$this->assertEquals( $this->parent_post->ID, get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );
259258
}
260259

261260
/**
262261
* Test save_to_post_parent logic
263262
*/
264263
public function test_post_parent_nested() {
265-
$test_data = array( 'parent' => $this->child_post_a->ID );
266-
$children = new Fieldmanager_Group( array(
264+
$fm = new Fieldmanager_Group( array(
267265
'name' => 'test_parent',
268266
'children' => array(
269267
'parent' => new Fieldmanager_Autocomplete( 'Post Parent', array(
@@ -277,9 +275,10 @@ public function test_post_parent_nested() {
277275
) ),
278276
),
279277
) );
280-
$this->save_values( $children, $this->parent_post, $test_data );
281-
$parent = get_post( $this->parent_post->ID );
282-
$this->assertEquals( $parent->post_parent, $this->child_post_a->ID );
278+
$this->save_values( $fm, $this->child_post_a, array( 'parent' => $this->parent_post->ID ) );
279+
$child = get_post( $this->child_post_a->ID );
280+
$this->assertEquals( $this->parent_post->ID, $child->post_parent );
281+
$this->assertEquals( '', get_post_meta( $this->child_post_a->ID, 'test_parent', true ) );
283282
}
284283

285284
/**

0 commit comments

Comments
 (0)