Skip to content

Commit 8c28773

Browse files
Merge pull request alleyinteractive#440 from alleyinteractive/user-taxonomies
Allow saving taxonomy data to users
2 parents 5c55d5e + a4df7f5 commit 8c28773

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

php/class-fieldmanager-autocomplete.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ public function form_element( $value = Null ) {
146146
* @param array $current_values existing post values
147147
*/
148148
public function presave_alter_values( $values, $current_values = array() ) {
149-
// return if there are no saved values, if this isn't a post, or if the reciprocal relationship isn't set.
150-
if ( empty( $this->data_id ) || $this->data_type !== 'post' ) return $values;
149+
// return if there is no data id
150+
if ( empty( $this->data_id ) ) {
151+
return $values;
152+
}
151153
return $this->datasource->presave_alter_values( $this, $values, $current_values );
152154
}
153155

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

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

385+
386+
/**
387+
* Test saving term relationships to users.
388+
*/
389+
public function test_saving_taxonomies_to_users() {
390+
// Create a new taxonomy and add a term to it
391+
register_taxonomy( 'user-tax', 'user' );
392+
$term = wp_insert_term( 'test-term', 'user-tax' );
393+
394+
// Create a user to which we'll save this data
395+
$user_id = wp_create_user( rand_str(), rand_str(), '[email protected]' );
396+
$user = get_user_by( 'id', $user_id );
397+
398+
// Create the field and save the data
399+
$field = new Fieldmanager_Autocomplete( array(
400+
'name' => 'test_terms',
401+
'datasource' => new Fieldmanager_Datasource_Term( array(
402+
'taxonomy' => 'user-tax',
403+
'only_save_to_taxonomy' => true,
404+
) ),
405+
) );
406+
$field->add_user_form( 'test' )->save_to_user_meta( $user_id, array( 'test_terms' => $term['term_id'] ) );
407+
408+
$this->assertSame( array(), get_user_meta( $user_id, 'test_terms', true ) );
409+
$this->assertSame( array( $term['term_id'] ), wp_get_object_terms( $user_id, 'user-tax', array( 'fields' => 'ids' ) ) );
410+
}
385411
}

0 commit comments

Comments
 (0)