Skip to content

Commit

Permalink
Introduce saving taxonomy relationships as a concern of Storable_Context
Browse files Browse the repository at this point in the history
It makes more sense to collect all of the terms needing a relationship
to the context, and then save the relationships at once
  • Loading branch information
danielbachhuber committed Feb 19, 2016
1 parent 6b66290 commit cea0ffb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
22 changes: 21 additions & 1 deletion php/context/class-fieldmanager-context-storable.php
Expand Up @@ -9,6 +9,11 @@
*/
abstract class Fieldmanager_Context_Storable extends Fieldmanager_Context {

/**
* @var array
*/
public $taxonomies_to_save = array();

/**
* Render the field.
*
Expand Down Expand Up @@ -39,6 +44,7 @@ protected function save( $data = null ) {
// Reset the save keys in the event this context instance is saved twice
$this->save_keys = array();

$this->fm->current_context = $this;
if ( $this->fm->serialize_data ) {
$this->save_field( $this->fm, $data, $this->fm->data_id );
} else {
Expand All @@ -47,6 +53,12 @@ protected function save( $data = null ) {
}
$this->save_walk_children( $this->fm, $data, $this->fm->data_id );
}
if ( ! empty( $this->taxonomies_to_save ) ) {
foreach( $this->taxonomies_to_save as $taxonomy => $term_ids ) {
$this->update_term_relationships( $this->fm->data_id, $term_ids, $taxonomy );
}
$this->taxonomies_to_save = array();
}
}

/**
Expand Down Expand Up @@ -205,4 +217,12 @@ abstract protected function update_data( $data_id, $data_key, $data_value, $data
* @see delete_post_meta().
*/
abstract protected function delete_data( $data_id, $data_key, $data_value = '' );
}

/**
* Method to update the term relations for the context
*/
protected function update_term_relationships( $data_id, $term_ids, $taxonomy ) {
wp_set_object_terms( $data_id, $term_ids, $taxonomy );
}

}
20 changes: 11 additions & 9 deletions php/datasource/class-fieldmanager-datasource-term.php
Expand Up @@ -192,11 +192,11 @@ public function presave_alter_values( Fieldmanager_Field $field, $values, $curre
$tax_values = $value;
}
}
$this->save_taxonomy( $tax_values, $field->data_id );
$this->pre_save_taxonomy( $tax_values, $field );
}
if ( $this->only_save_to_taxonomy ) {
if ( empty( $values ) && ! ( $this->append_taxonomy ) ) {
$this->save_taxonomy( array(), $field->data_id );
$this->pre_save_taxonomy( array(), $field );
}
return array();
}
Expand All @@ -215,7 +215,7 @@ public function presave( Fieldmanager_Field $field, $value, $current_value ) {
* @param mixed[] $tax_values
* @return void
*/
public function save_taxonomy( $tax_values, $data_id ) {
public function pre_save_taxonomy( $tax_values, $field ) {

$tax_values = array_map( 'intval', $tax_values );
$tax_values = array_unique( $tax_values );
Expand All @@ -227,14 +227,16 @@ public function save_taxonomy( $tax_values, $data_id ) {
$taxonomies_to_save = array();
foreach ( $tax_values as $term_id ) {
$term = $this->get_term( $term_id );
if ( empty( $taxonomies_to_save[ $term->taxonomy ] ) ) $taxonomies_to_save[ $term->taxonomy ] = array();
$taxonomies_to_save[ $term->taxonomy ][] = $term_id;
}
foreach ( $taxonomies_to_save as $taxonomy => $terms ) {
wp_set_object_terms( $data_id, $terms, $taxonomy, $this->append_taxonomy );
if ( empty( $field->current_context->taxonomies_to_save[ $term->taxonomy ] ) ) {
$field->current_context->taxonomies_to_save[ $term->taxonomy ] = array();
}
$field->current_context->taxonomies_to_save[ $term->taxonomy ][] = $term_id;
}
} else {
wp_set_object_terms( $data_id, $tax_values, $taxonomies[0], $this->append_taxonomy );
if ( ! isset( $field->current_context->taxonomies_to_save[ $taxonomies[0] ] ) ) {
$field->current_context->taxonomies_to_save[ $taxonomies[0] ] = array();
}
$field->current_context->taxonomies_to_save[ $taxonomies[0] ] = array_merge( $field->current_context->taxonomies_to_save[ $taxonomies[0] ], $tax_values );
}
}

Expand Down
34 changes: 0 additions & 34 deletions tests/php/test-fieldmanager-datasource-term.php
Expand Up @@ -382,38 +382,4 @@ public function test_append_true_after_first_save() {
);
}

public function test_disable_append_true_after_first_save() {
$fm = new Fieldmanager_Group( array(
'name' => 'author_data',
'children' => array(
'school' => new Fieldmanager_Group(array(
'label' => 'School',
'add_more_label' => 'Add new',
'limit' => 0,
'children' => array(
'school_city' => new Fieldmanager_Autocomplete( array(
'label' => 'School city',
'datasource' => new Fieldmanager_Datasource_Term( array(
'taxonomy' => 'post_tag',
'taxonomy_save_to_terms' => true,
'append_after_first_save' => false,
))
)),
)
))
)
) );
$data = array(
'school' => array(
array( 'school_city' => $this->term->term_id ),
array( 'school_city' => $this->term_2->term_id ),
),
);
$fm->add_meta_box( 'test meta box', 'post' )->save_to_post_meta( $this->post->ID, $data );
$this->assertEquals(
array( $this->term_2->term_id ),
wp_get_post_terms( $this->post->ID, $this->term->taxonomy, array( 'fields' => 'ids' ) )
);
}

}

0 comments on commit cea0ffb

Please sign in to comment.