From be828210a3699d05599435ecb039442f21a94c0f Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Fri, 19 Feb 2016 22:20:21 -0500 Subject: [PATCH 1/2] prevent saving to postmeta when only_save_to_taxonomy is true --- php/class-fieldmanager-group.php | 12 ++++++++++++ php/class-fieldmanager-options.php | 3 +++ 2 files changed, 15 insertions(+) diff --git a/php/class-fieldmanager-group.php b/php/class-fieldmanager-group.php index 7316c8297b..90425b6e21 100644 --- a/php/class-fieldmanager-group.php +++ b/php/class-fieldmanager-group.php @@ -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; @@ -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 ) ) { diff --git a/php/class-fieldmanager-options.php b/php/class-fieldmanager-options.php index 3637ceaff8..0a065d5ba9 100644 --- a/php/class-fieldmanager-options.php +++ b/php/class-fieldmanager-options.php @@ -224,6 +224,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; From 207ac57e2e56956f60f297409c3ee067e307d1ca Mon Sep 17 00:00:00 2001 From: xmattus Date: Sat, 20 Feb 2016 03:47:49 +0000 Subject: [PATCH 2/2] fix autocomplete fields and fix a bunch of tests to expect blank strings instead of empty arrays --- php/class-fieldmanager-autocomplete.php | 4 ++++ tests/php/test-fieldmanager-datasource-term.php | 16 ++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/php/class-fieldmanager-autocomplete.php b/php/class-fieldmanager-autocomplete.php index 7705a643be..b705391820 100644 --- a/php/class-fieldmanager-autocomplete.php +++ b/php/class-fieldmanager-autocomplete.php @@ -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 ); } diff --git a/tests/php/test-fieldmanager-datasource-term.php b/tests/php/test-fieldmanager-datasource-term.php index a9229548b3..118a2bf44a 100644 --- a/tests/php/test-fieldmanager-datasource-term.php +++ b/tests/php/test-fieldmanager-datasource-term.php @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); @@ -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 ); @@ -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' ) ) @@ -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' ) ) @@ -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' ) ) ); } }