Skip to content
Merged
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
4 changes: 4 additions & 0 deletions php/class-fieldmanager-autocomplete.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ public function presave_alter_values( $values, $current_values = array() ) {
if ( empty( $this->data_id ) ) {
return $values;
}

if ( ! empty( $this->datasource->only_save_to_taxonomy ) ) {
$this->skip_save = true;
}
return $this->datasource->presave_alter_values( $this, $values, $current_values );
}

Expand Down
12 changes: 12 additions & 0 deletions php/class-fieldmanager-group.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ public function presave( $values, $current_values = array() ) {
}

// Then, dispatch them for sanitization to the children.
$skip_save_all = true;
foreach ( $this->children as $k => $element ) {
$element->data_id = $this->data_id;
$element->data_type = $this->data_type;
Expand All @@ -323,6 +324,17 @@ public function presave( $values, $current_values = array() ) {
if ( is_array( $values[ $element->name ] ) && empty( $values[ $element->name ] ) ) unset( $values[ $element->name ] );
elseif ( empty( $values[ $element->name ] ) ) unset( $values[ $element->name ] );
}

if ( ! empty( $element->datasource->only_save_to_taxonomy ) ) {
unset( $values[ $element->name ] );
continue;
}

$skip_save_all = false;
}

if ( $skip_save_all ) {
$this->skip_save = true;
}

if ( is_callable( $this->group_is_empty ) ) {
Expand Down
3 changes: 3 additions & 0 deletions php/class-fieldmanager-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public function preload_alter_values( $values ) {
*/
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;
}
return $this->datasource->presave_alter_values( $this, $values, $current_values );
}
return $values;
Expand Down
16 changes: 8 additions & 8 deletions tests/php/test-fieldmanager-datasource-term.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function test_datasource_term_save_only_tax() {
$this->save_values( $terms, $this->post, $this->term->term_id );

$saved_value = get_post_meta( $this->post->ID, 'test_terms', true );
$this->assertSame( array(), $saved_value );
$this->assertSame( '', $saved_value );

$post_terms = wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'ids' ) );
$this->assertSame( array( $this->term->term_id ), $post_terms );
Expand Down Expand Up @@ -120,7 +120,7 @@ public function test_datasource_term_save_multi_only_tax() {
$this->save_values( $terms, $this->post, array( $this->term->term_id, $term->term_id ) );

$saved_value = get_post_meta( $this->post->ID, 'test_terms', true );
$this->assertSame( array(), $saved_value );
$this->assertSame( '', $saved_value );

$post_terms = wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'ids' ) );
$this->assertCount( 2, $post_terms );
Expand All @@ -147,7 +147,7 @@ public function test_datasource_term_save_multi_only_tax_inexact() {
$this->save_values( $terms, $this->post, array( $new_term ) );

$saved_value = get_post_meta( $this->post->ID, 'test_terms', true );
$this->assertSame( array(), $saved_value );
$this->assertSame( '', $saved_value );

$post_terms = wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'names' ) );
$this->assertCount( 1, $post_terms );
Expand All @@ -159,7 +159,7 @@ public function test_datasource_term_save_multi_only_tax_inexact() {
$this->save_values( $terms, $this->post, array( "={$numeric_term}" ) );

$saved_value = get_post_meta( $this->post->ID, 'test_terms', true );
$this->assertSame( array(), $saved_value );
$this->assertSame( '', $saved_value );

$post_terms = wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'names' ) );
$this->assertCount( 1, $post_terms );
Expand All @@ -170,7 +170,7 @@ public function test_datasource_term_save_multi_only_tax_inexact() {
$this->save_values( $terms, $this->post, array( "={$numeric_term}" ) );

$saved_value = get_post_meta( $this->post->ID, 'test_terms', true );
$this->assertSame( array(), $saved_value );
$this->assertSame( '', $saved_value );

$post_terms = wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'names' ) );
$this->assertCount( 1, $post_terms );
Expand Down Expand Up @@ -301,7 +301,7 @@ public function test_group_datasource_term_save_only_tax_with_unseriaized_data()
$base = new Fieldmanager_Group( $args );
$base->add_meta_box( 'test meta box', 'post' )->save_to_post_meta( $this->post->ID, $data );
$this->assertSame( $data['test_basic'], get_post_meta( $this->post->ID, 'base_group_test_basic', true ) );
$this->assertSame( array(), get_post_meta( $this->post->ID, 'base_group_test_datasource', true ) );
$this->assertSame( '', get_post_meta( $this->post->ID, 'base_group_test_datasource', true ) );
$this->assertSame(
array( $this->term->term_id ),
wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'ids' ) )
Expand All @@ -312,7 +312,7 @@ public function test_group_datasource_term_save_only_tax_with_unseriaized_data()
$base = new Fieldmanager_Group( array_merge( $args, array( 'add_to_prefix' => false ) ) );
$base->add_meta_box( 'test meta box', 'post' )->save_to_post_meta( $this->post->ID, $data );
$this->assertSame( $data['test_basic'], get_post_meta( $this->post->ID, 'test_basic', true ) );
$this->assertSame( array(), get_post_meta( $this->post->ID, 'test_datasource', true ) );
$this->assertSame( '', get_post_meta( $this->post->ID, 'test_datasource', true ) );
$this->assertSame(
array( $this->term->term_id ),
wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'ids' ) )
Expand Down Expand Up @@ -405,7 +405,7 @@ public function test_saving_taxonomies_to_users() {
) );
$field->add_user_form( 'test' )->save_to_user_meta( $user_id, array( 'test_terms' => $term['term_id'] ) );

$this->assertSame( array(), get_user_meta( $user_id, 'test_terms', true ) );
$this->assertSame( '', get_user_meta( $user_id, 'test_terms', true ) );
$this->assertSame( array( $term['term_id'] ), wp_get_object_terms( $user_id, 'user-tax', array( 'fields' => 'ids' ) ) );
}
}