Skip to content

Commit

Permalink
add basic qe+be support for user field #110
Browse files Browse the repository at this point in the history
  • Loading branch information
mcguffin committed Aug 25, 2021
1 parent 9dad612 commit df91154
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 29 deletions.
3 changes: 2 additions & 1 deletion css/acf-qef-field-group.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion css/acf-quickedit.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion include/ACFQuickEdit/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function ajax_get_acf_post_meta( $params ) {

if ( $field_object = Fields\Field::getFieldObject( $field ) ) {
$value = $field_object->get_value( $object_id, false );

error_log(var_export($value,true));
if ( ! isset( $data[ $key ] ) ) {
// first iteration - always set value
$val = $field_object->get_value( $object_id, false );
Expand Down
21 changes: 12 additions & 9 deletions include/ACFQuickEdit/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public static function get_types() {
'taxonomy' => [ 'column' => true, 'quickedit' => true, 'bulkedit' => true ],
'user' => [
'column' => current_user_can('list_users'),
'quickedit' => false,
'bulkedit' => false
'quickedit' => current_user_can('list_users'),
'bulkedit' => current_user_can('list_users')
],

// jQuery
Expand Down Expand Up @@ -373,19 +373,22 @@ public function sanitize_value( $value, $context = 'db' ) {
* @param array $arr
*/
protected function sanitize_strings_array( $arr ) {
$arr = $arr;
array_walk( $arr, [ $this, '_sanitize_strings_array_cb' ] );
return $arr;

return array_combine(
array_map( [ $this, 'sanitize_string_or_leave_int' ], array_keys( $arr ) ),
array_map( 'sanitize_text_field', array_values( $arr ) )
);

}

/**
* array_walk callback
*/
private function _sanitize_strings_array_cb( &$value, &$key ) {
if ( ! is_int( $key ) ) {
$key = sanitize_text_field( $key );
private function sanitize_string_or_leave_int( $value ) {
if ( is_int( $value ) ) {
return $value;
}
$value = sanitize_text_field( $value );
return sanitize_text_field( $value );
}

}
28 changes: 26 additions & 2 deletions include/ACFQuickEdit/Fields/UserField.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
if ( ! defined( 'ABSPATH' ) )
die('Nope.');

class UserField extends Field {
class UserField extends SelectField {

/**
* @inheritdoc
Expand All @@ -27,11 +27,35 @@ public function render_column( $object_id ) {

}


/**
* @inheritdoc
*/
public function sanitize_value( $value, $context = 'db' ) {
public function render_input( $input_atts, $is_quickedit = true ) {
$get_users_args = [
'fields' => [ 'ID', 'user_nicename' ],
];

if ( ! empty( $this->acf_field['role'] ) ) {
$get_users_args['role__in'] = $this->acf_field['role'];
}
$users = get_users($get_users_args);
$this->acf_field['ui'] = true;
$this->acf_field['choices'] = array_combine(
array_map( function($user) { return $user->ID; }, $users ),
array_map( function($user) { return $user->user_nicename; }, $users ),
);
return parent::render_input( $input_atts, $is_quickedit );

}

/**
* @inheritdoc
*/
public function sanitize_value( $value, $context = 'db' ) {
if ( is_array( $value ) ) {
return array_map( 'intval', $value );
}
return intval( $value );

}
Expand Down
3 changes: 2 additions & 1 deletion js/acf-columns.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion js/acf-qef-field-group.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion js/acf-quickedit.js

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion src/js/lib/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import textarea from 'fields/textarea.js'
import time_picker from 'fields/time_picker.js'
import true_false from 'fields/true_false.js'

const user = Object.assign({},select,{type:'user'} );

const View = wp.media.View.extend({
events:{
'change [type="checkbox"][data-is-do-not-change="true"]' : 'dntChanged',
Expand Down Expand Up @@ -150,7 +152,7 @@ field.add_type( button_group );
field.add_type( select );

/**
* field type select
* field type post_object
*/
field.add_type( post_object );

Expand All @@ -159,6 +161,11 @@ field.add_type( post_object );
*/
field.add_type( taxonomy );

/**
* field type user
*/
field.add_type( user );

/**
* field type true_false
*/
Expand Down
2 changes: 1 addition & 1 deletion test/acf-json/group_acf_qef_basic.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"active": 0,
"description": "",
"modified": 1567242877
}
2 changes: 1 addition & 1 deletion test/acf-json/group_acf_qef_choice.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"active": 0,
"description": "",
"modified": 1566807827
}
Loading

0 comments on commit df91154

Please sign in to comment.