Skip to content

Commit 70b16e6

Browse files
author
Noah Schoenholtz
committed
fixed user by id lookup
1 parent e20e72a commit 70b16e6

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

php/datasource/class-fieldmanager-datasource-user.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ class Fieldmanager_Datasource_User extends Fieldmanager_Datasource {
3434
* or 'user_nicename'
3535
*/
3636
public $display_property = 'display_name';
37-
37+
3838
/**
3939
* @var array
4040
* Allowed display properties for validation.
4141
*/
4242
protected $allowed_display_properties = array( 'display_name', 'user_login', 'user_email', 'user_nicename' );
43-
43+
4444
/**
4545
* @var string
4646
* Store property. Defaults to ID, but can also be 'user_login', 'user_email',
4747
* or 'user_nicename'.
4848
*/
4949
public $store_property = 'ID';
50-
50+
5151
/**
5252
* @var array
5353
* Allowed store properties for validation.
@@ -73,23 +73,23 @@ class Fieldmanager_Datasource_User extends Fieldmanager_Datasource {
7373
*/
7474
public function __construct( $options = array() ) {
7575
parent::__construct( $options );
76-
76+
7777
// Validate improper usage of store property
7878
if ( ! in_array( $this->store_property, $this->allowed_store_properties ) ) {
79-
throw new FM_Developer_Exception( sprintf(
79+
throw new FM_Developer_Exception( sprintf(
8080
__( 'Store property %s is invalid. Must be one of %s.', 'fieldmanager' ),
8181
$this->store_property,
8282
implode( ', ', $this->allowed_store_properties )
8383
) );
8484
}
85-
85+
8686
if ( ! empty( $this->reciprocal ) && 'ID' != $this->store_property ) {
8787
throw new FM_Developer_Exception( __( 'You cannot use reciprocal relationships with FM_Datasource_User if store_property is not set to ID.', 'fieldmanager' ) );
8888
}
89-
89+
9090
// Validate improper usage of display property
9191
if ( ! in_array( $this->display_property, $this->allowed_display_properties ) ) {
92-
throw new FM_Developer_Exception( sprintf(
92+
throw new FM_Developer_Exception( sprintf(
9393
__( 'Display property %s is invalid. Must be one of %s.', 'fieldmanager' ),
9494
$this->display_property,
9595
implode( ', ', $this->allowed_display_properties )
@@ -105,7 +105,7 @@ public function __construct( $options = array() ) {
105105
public function get_value( $value ) {
106106
switch ( $this->store_property ) {
107107
case 'ID':
108-
$field = 'ID';
108+
$field = 'id';
109109
break;
110110
case 'user_nicename':
111111
$field = 'slug';
@@ -117,7 +117,7 @@ public function get_value( $value ) {
117117
$field = 'login';
118118
break;
119119
}
120-
120+
121121
// Sanitize the value
122122
$value = $this->sanitize_value( $value );
123123

@@ -135,20 +135,20 @@ public function get_items( $fragment = Null ) {
135135
if ( is_callable( $this->query_callback ) ) {
136136
return call_user_func( $this->query_callback, $fragment );
137137
}
138-
138+
139139
$default_args = array();
140140
$user_args = array_merge( $default_args, $this->query_args );
141141
$ret = array();
142-
142+
143143
if ( $fragment ) {
144144
$user_args['search'] = $fragment;
145145
}
146-
146+
147147
$users = get_users( $user_args );
148148
foreach ( $users as $u ) {
149149
$ret[ $u->{$this->store_property} ] = $u->{$this->display_property};
150150
}
151-
151+
152152
return $ret;
153153
}
154154

@@ -181,7 +181,7 @@ public function presave_alter_values( Fieldmanager_Field $field, $values, $curre
181181
delete_user_meta( $user_id, $this->reciprocal, $field->data_id );
182182
}
183183
}
184-
184+
185185
return $values;
186186
}
187187

@@ -195,13 +195,13 @@ public function presave( Fieldmanager_Field $field, $value, $current_value ) {
195195
if ( empty( $value ) ) {
196196
return;
197197
}
198-
198+
199199
$return_single = False;
200200
if ( !is_array( $value ) ) {
201201
$return_single = True;
202202
$value = array( $value );
203203
}
204-
204+
205205
foreach ( $value as $i => $v ) {
206206
$value[$i] = $this->sanitize_value( $v );
207207
if( ! current_user_can( $this->capability, $v ) ) {
@@ -211,7 +211,7 @@ public function presave( Fieldmanager_Field $field, $value, $current_value ) {
211211
add_user_meta( $v, $this->reciprocal, $field->data_id );
212212
}
213213
}
214-
214+
215215
return $return_single ? $value[0] : $value;
216216
}
217217

@@ -237,7 +237,7 @@ public function get_edit_link( $value ) {
237237
esc_html__( 'Edit', 'fieldmanager' )
238238
);
239239
}
240-
240+
241241
/**
242242
* Sanitize the value based on store_property.
243243
* @param int|string $value
@@ -255,7 +255,7 @@ protected function sanitize_value( $value ) {
255255
$value = sanitize_text_field( $value );
256256
break;
257257
}
258-
258+
259259
return $value;
260260
}
261-
}
261+
}

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

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ public function test_reciprocal_meta() {
6363
$this->assertEquals( $this->author, get_post_meta( $this->post->ID, 'test_reciprocal', true ) );
6464
$this->assertEquals( $this->post->ID, get_user_meta( $this->author, 'reciprocal_post', true ) );
6565
}
66-
66+
6767
/**
6868
* Test that various store properties work
6969
*/
7070
public function test_store_properties() {
7171
$user = get_userdata( $this->author );
72-
72+
7373
$store_id = new Fieldmanager_Autocomplete( array(
7474
'name' => 'test_store_id',
7575
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -79,7 +79,7 @@ public function test_store_properties() {
7979

8080
$this->save_values( $store_id, $this->post, $user->ID );
8181
$this->assertEquals( $user->ID, get_post_meta( $this->post->ID, 'test_store_id', true ) );
82-
82+
8383
$store_user_login = new Fieldmanager_Autocomplete( array(
8484
'name' => 'test_store_user_login',
8585
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -89,7 +89,7 @@ public function test_store_properties() {
8989

9090
$this->save_values( $store_user_login, $this->post, $user->user_login );
9191
$this->assertEquals( $user->user_login, get_post_meta( $this->post->ID, 'test_store_user_login', true ) );
92-
92+
9393
$store_user_email = new Fieldmanager_Autocomplete( array(
9494
'name' => 'test_store_user_email',
9595
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -99,7 +99,7 @@ public function test_store_properties() {
9999

100100
$this->save_values( $store_user_email, $this->post, $user->user_email );
101101
$this->assertEquals( $user->user_email, get_post_meta( $this->post->ID, 'test_store_user_email', true ) );
102-
102+
103103
$store_user_nicename = new Fieldmanager_Autocomplete( array(
104104
'name' => 'test_store_user_nicename',
105105
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -110,7 +110,7 @@ public function test_store_properties() {
110110
$this->save_values( $store_user_nicename, $this->post, $user->user_nicename );
111111
$this->assertEquals( $user->user_nicename, get_post_meta( $this->post->ID, 'test_store_user_nicename', true ) );
112112
}
113-
113+
114114
/**
115115
* Test creating a field with an invalid store property.
116116
* @expectedException FM_Developer_Exception
@@ -125,7 +125,7 @@ public function test_save_invalid_store_property() {
125125

126126
$this->save_values( $test_invalid, $this->post, $this->author );
127127
}
128-
128+
129129
/**
130130
* Test creating a field with an invalid display property.
131131
* @expectedException FM_Developer_Exception
@@ -140,7 +140,7 @@ public function test_save_invalid_display_property() {
140140

141141
$this->save_values( $test_invalid, $this->post, $this->author );
142142
}
143-
143+
144144
/**
145145
* Test that we fail when trying to use reciprocals with something other than ID as a store property.
146146
* @expectedException FM_Developer_Exception
@@ -156,32 +156,32 @@ public function test_save_invalid_reciprocal() {
156156

157157
$this->save_values( $test_invalid, $this->post, $this->author );
158158
}
159-
159+
160160
/**
161161
* Test that this fails when a user doesn't have permission to list users.
162162
* @expectedException WPDieException
163163
*/
164164
public function test_save_permissions() {
165165
wp_set_current_user( $this->author );
166-
166+
167167
$test_invalid = new Fieldmanager_Autocomplete( array(
168168
'name' => 'test_invalid',
169169
'datasource' => new Fieldmanager_Datasource_User(),
170170
) );
171-
171+
172172
$this->save_values( $test_invalid, $this->post, $this->editor );
173-
173+
174174
wp_set_current_user( $this->author );
175175

176176
$this->save_values( $test_invalid, $this->post, $this->editor );
177177
}
178-
178+
179179
/**
180180
* Test that display property returns the correct value in all reasonable cases.
181181
*/
182182
public function test_display_properties() {
183183
$user = get_userdata( $this->author );
184-
184+
185185
$test_display_name = new Fieldmanager_Autocomplete( array(
186186
'name' => 'test_display_name',
187187
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -191,7 +191,7 @@ public function test_display_properties() {
191191

192192
$test_users_display_name = $test_display_name->datasource->get_items( $user->user_login );
193193
$this->assertEquals( $user->display_name, $test_users_display_name[ $user->ID ] );
194-
194+
195195
$test_user_login = new Fieldmanager_Autocomplete( array(
196196
'name' => 'test_user_login',
197197
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -201,7 +201,7 @@ public function test_display_properties() {
201201

202202
$test_users_user_login = $test_user_login->datasource->get_items( $user->user_login );
203203
$this->assertEquals( $user->user_login, $test_users_user_login[ $user->ID ] );
204-
204+
205205
$test_user_email = new Fieldmanager_Autocomplete( array(
206206
'name' => 'test_user_email',
207207
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -211,7 +211,7 @@ public function test_display_properties() {
211211

212212
$test_users_user_email = $test_user_email->datasource->get_items( $user->user_login );
213213
$this->assertEquals( $user->user_email, $test_users_user_email[ $user->ID ] );
214-
214+
215215
$test_user_nicename = new Fieldmanager_Autocomplete( array(
216216
'name' => 'test_user_nicename',
217217
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -222,13 +222,13 @@ public function test_display_properties() {
222222
$test_users_user_nicename = $test_user_nicename->datasource->get_items( $user->user_login );
223223
$this->assertEquals( $user->user_nicename, $test_users_user_nicename[ $user->ID ] );
224224
}
225-
225+
226226
/**
227227
* Test that store property returns the correct display value in all reasonable cases.
228228
*/
229229
public function test_store_property_display() {
230230
$user = get_userdata( $this->author );
231-
231+
232232
$test_id = new Fieldmanager_Autocomplete( array(
233233
'name' => 'test_display_name',
234234
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -239,7 +239,7 @@ public function test_store_property_display() {
239239
$test_users_id = $test_id->datasource->get_items( $user->user_login );
240240
$this->assertEquals( $user->display_name, $test_users_id[ $user->ID ] );
241241
$this->assertEquals( $user->display_name, $test_id->datasource->get_value( $user->ID ) );
242-
242+
243243
$test_user_login = new Fieldmanager_Autocomplete( array(
244244
'name' => 'test_user_login',
245245
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -250,7 +250,7 @@ public function test_store_property_display() {
250250
$test_users_user_login = $test_user_login->datasource->get_items( $user->user_login );
251251
$this->assertEquals( $user->display_name, $test_users_user_login[ $user->user_login ] );
252252
$this->assertEquals( $user->display_name, $test_user_login->datasource->get_value( $user->user_login ) );
253-
253+
254254
$test_user_email = new Fieldmanager_Autocomplete( array(
255255
'name' => 'test_user_email',
256256
'datasource' => new Fieldmanager_Datasource_User( array(
@@ -261,7 +261,7 @@ public function test_store_property_display() {
261261
$test_users_user_email = $test_user_email->datasource->get_items( $user->user_login );
262262
$this->assertEquals( $user->display_name, $test_users_user_email[ $user->user_email ] );
263263
$this->assertEquals( $user->display_name, $test_user_email->datasource->get_value( $user->user_email ) );
264-
264+
265265
$test_user_nicename = new Fieldmanager_Autocomplete( array(
266266
'name' => 'test_user_nicename',
267267
'datasource' => new Fieldmanager_Datasource_User( array(

0 commit comments

Comments
 (0)