Skip to content

Commit cea0ffb

Browse files
Introduce saving taxonomy relationships as a concern of Storable_Context
It makes more sense to collect all of the terms needing a relationship to the context, and then save the relationships at once
1 parent 6b66290 commit cea0ffb

3 files changed

+32
-44
lines changed

php/context/class-fieldmanager-context-storable.php

+21-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
*/
1010
abstract class Fieldmanager_Context_Storable extends Fieldmanager_Context {
1111

12+
/**
13+
* @var array
14+
*/
15+
public $taxonomies_to_save = array();
16+
1217
/**
1318
* Render the field.
1419
*
@@ -39,6 +44,7 @@ protected function save( $data = null ) {
3944
// Reset the save keys in the event this context instance is saved twice
4045
$this->save_keys = array();
4146

47+
$this->fm->current_context = $this;
4248
if ( $this->fm->serialize_data ) {
4349
$this->save_field( $this->fm, $data, $this->fm->data_id );
4450
} else {
@@ -47,6 +53,12 @@ protected function save( $data = null ) {
4753
}
4854
$this->save_walk_children( $this->fm, $data, $this->fm->data_id );
4955
}
56+
if ( ! empty( $this->taxonomies_to_save ) ) {
57+
foreach( $this->taxonomies_to_save as $taxonomy => $term_ids ) {
58+
$this->update_term_relationships( $this->fm->data_id, $term_ids, $taxonomy );
59+
}
60+
$this->taxonomies_to_save = array();
61+
}
5062
}
5163

5264
/**
@@ -205,4 +217,12 @@ abstract protected function update_data( $data_id, $data_key, $data_value, $data
205217
* @see delete_post_meta().
206218
*/
207219
abstract protected function delete_data( $data_id, $data_key, $data_value = '' );
208-
}
220+
221+
/**
222+
* Method to update the term relations for the context
223+
*/
224+
protected function update_term_relationships( $data_id, $term_ids, $taxonomy ) {
225+
wp_set_object_terms( $data_id, $term_ids, $taxonomy );
226+
}
227+
228+
}

php/datasource/class-fieldmanager-datasource-term.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ public function presave_alter_values( Fieldmanager_Field $field, $values, $curre
192192
$tax_values = $value;
193193
}
194194
}
195-
$this->save_taxonomy( $tax_values, $field->data_id );
195+
$this->pre_save_taxonomy( $tax_values, $field );
196196
}
197197
if ( $this->only_save_to_taxonomy ) {
198198
if ( empty( $values ) && ! ( $this->append_taxonomy ) ) {
199-
$this->save_taxonomy( array(), $field->data_id );
199+
$this->pre_save_taxonomy( array(), $field );
200200
}
201201
return array();
202202
}
@@ -215,7 +215,7 @@ public function presave( Fieldmanager_Field $field, $value, $current_value ) {
215215
* @param mixed[] $tax_values
216216
* @return void
217217
*/
218-
public function save_taxonomy( $tax_values, $data_id ) {
218+
public function pre_save_taxonomy( $tax_values, $field ) {
219219

220220
$tax_values = array_map( 'intval', $tax_values );
221221
$tax_values = array_unique( $tax_values );
@@ -227,14 +227,16 @@ public function save_taxonomy( $tax_values, $data_id ) {
227227
$taxonomies_to_save = array();
228228
foreach ( $tax_values as $term_id ) {
229229
$term = $this->get_term( $term_id );
230-
if ( empty( $taxonomies_to_save[ $term->taxonomy ] ) ) $taxonomies_to_save[ $term->taxonomy ] = array();
231-
$taxonomies_to_save[ $term->taxonomy ][] = $term_id;
232-
}
233-
foreach ( $taxonomies_to_save as $taxonomy => $terms ) {
234-
wp_set_object_terms( $data_id, $terms, $taxonomy, $this->append_taxonomy );
230+
if ( empty( $field->current_context->taxonomies_to_save[ $term->taxonomy ] ) ) {
231+
$field->current_context->taxonomies_to_save[ $term->taxonomy ] = array();
232+
}
233+
$field->current_context->taxonomies_to_save[ $term->taxonomy ][] = $term_id;
235234
}
236235
} else {
237-
wp_set_object_terms( $data_id, $tax_values, $taxonomies[0], $this->append_taxonomy );
236+
if ( ! isset( $field->current_context->taxonomies_to_save[ $taxonomies[0] ] ) ) {
237+
$field->current_context->taxonomies_to_save[ $taxonomies[0] ] = array();
238+
}
239+
$field->current_context->taxonomies_to_save[ $taxonomies[0] ] = array_merge( $field->current_context->taxonomies_to_save[ $taxonomies[0] ], $tax_values );
238240
}
239241
}
240242

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

-34
Original file line numberDiff line numberDiff line change
@@ -382,38 +382,4 @@ public function test_append_true_after_first_save() {
382382
);
383383
}
384384

385-
public function test_disable_append_true_after_first_save() {
386-
$fm = new Fieldmanager_Group( array(
387-
'name' => 'author_data',
388-
'children' => array(
389-
'school' => new Fieldmanager_Group(array(
390-
'label' => 'School',
391-
'add_more_label' => 'Add new',
392-
'limit' => 0,
393-
'children' => array(
394-
'school_city' => new Fieldmanager_Autocomplete( array(
395-
'label' => 'School city',
396-
'datasource' => new Fieldmanager_Datasource_Term( array(
397-
'taxonomy' => 'post_tag',
398-
'taxonomy_save_to_terms' => true,
399-
'append_after_first_save' => false,
400-
))
401-
)),
402-
)
403-
))
404-
)
405-
) );
406-
$data = array(
407-
'school' => array(
408-
array( 'school_city' => $this->term->term_id ),
409-
array( 'school_city' => $this->term_2->term_id ),
410-
),
411-
);
412-
$fm->add_meta_box( 'test meta box', 'post' )->save_to_post_meta( $this->post->ID, $data );
413-
$this->assertEquals(
414-
array( $this->term_2->term_id ),
415-
wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'ids' ) )
416-
);
417-
}
418-
419385
}

0 commit comments

Comments
 (0)