Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions php/datasource/class-fieldmanager-datasource-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,16 @@ public function presave_alter_values( Fieldmanager_Field $field, $values, $curre
* @return string
*/
public function presave( Fieldmanager_Field $field, $value, $current_value ) {
if ( empty( $value ) ) {
if ( $this->only_save_to_post_parent ) {
$current_value = $this->preload_alter_values( $field, $current_value );
}

if ( empty( $value ) && empty( $current_value ) ) {
return;
}
$value = intval( $value );

if ( ! empty( $this->publish_with_parent ) || ! empty( $this->reciprocal ) ) {
if ( ! empty( $value ) && ( ! empty( $this->publish_with_parent ) || ! empty( $this->reciprocal ) ) ) {
// There are no permissions in cron, but no changes are coming from a user either.
if ( ! defined( 'DOING_CRON' ) || ! DOING_CRON ) {
$post_type_obj = get_post_type_object( get_post_type( $value ) );
Expand Down
28 changes: 28 additions & 0 deletions tests/php/test-fieldmanager-datasource-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ public function test_post_parent_nested() {
$html
);
}

/**
* Test save_to_post_parent_only logic
*/
Expand Down Expand Up @@ -556,4 +557,31 @@ public function test_inherited_repeatable_post_parent_invalid() {
)
);
}

/**
* Test save_to_post_parent_only logic
*/
public function test_unsetting_post_parent_only() {
$fm = new Fieldmanager_Autocomplete(
array(
'name' => 'test_parent',
'datasource' => new Fieldmanager_Datasource_Post(
array(
'only_save_to_post_parent' => true,
'query_args' => array(
'post_type' => 'post',
),
)
),
)
);

$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->save_values( $fm, $this->child_post_a, '' );
$child = get_post( $this->child_post_a->ID );
$this->assertEquals( null, $child->post_parent );
}
}