diff --git a/acf-origin.php b/acf-origin.php index 2fd8f64..7217e3f 100644 --- a/acf-origin.php +++ b/acf-origin.php @@ -46,6 +46,7 @@ function acf_include_field_types( $version = false ) { include_once('fields/origin-acf-field-unique.php'); include_once('fields/origin-acf-field-slug.php'); include_once('fields/origin-acf-field-tab-name.php'); + include_once('fields/origin-acf-field-role-selector.php'); } } diff --git a/fields/origin-acf-field-role-selector.php b/fields/origin-acf-field-role-selector.php new file mode 100644 index 0000000..237311f --- /dev/null +++ b/fields/origin-acf-field-role-selector.php @@ -0,0 +1,127 @@ +name = 'role_selector'; + $this->label = 'Origin/Role-selector'; + $this->category = 'basic'; + $this->defaults = array( + 'return_value' => 'name', + 'field_type' => 'checkbox', + 'allowed_roles' => '', + ); + + parent::__construct(); + } + + function render_field_settings( $field ) { + acf_render_field_setting( $field, array( + 'label' => __('Return Format','acf-role-selector-field'), + 'instructions' => __('Specify the returned value type','acf-role-selector-field'), + 'type' => 'radio', + 'name' => 'return_value', + 'layout' => 'horizontal', + 'choices' => array( + 'name' => __( 'Role Name', 'acf-role-selector-field' ), + 'object' => __( 'Role Object', 'acf-role-selector-field' ), + ) + )); + + global $wp_roles; + acf_render_field_setting( $field, array( + 'label' => __('Allowed Roles','acf-role-selector-field'), + 'type' => 'select', + 'name' => 'allowed_roles', + 'multiple' => true, + 'instructions' => __( 'To allow all roles, select none or all of the options to the right', 'acf-role-selector-field' ), + 'choices' => $wp_roles->role_names + )); + + acf_render_field_setting( $field, array( + 'label' => __('Field Type','acf-role-selector-field'), + 'type' => 'select', + 'name' => 'field_type', + 'choices' => array( + __( 'Multiple Values', 'acf-role-selector-field' ) => array( + 'checkbox' => __( 'Checkbox', 'acf-role-selector-field' ), + 'multi_select' => __( 'Multi Select', 'acf-role-selector-field' ) + ), + __( 'Single Value', 'acf-role-selector-field' ) => array( + 'radio' => __( 'Radio Buttons', 'acf-role-selector-field' ), + 'select' => __( 'Select', 'acf-role-selector-field' ) + ) + ) + )); + } + + function render_field( $field ) { + global $wp_roles; + $roles = $wp_roles->roles; + + foreach( $roles as $role => $data ) { + if( is_array( $field['allowed_roles'] ) && !in_array( $role, $field['allowed_roles'] ) ) { + unset( $roles[$role] ); + } + } + + $roles = apply_filters( 'acfrsf/allowed_roles', $roles, $field ); + + // Select and multiselect fields + if( $field['field_type'] == 'select' || $field['field_type'] == 'multi_select' ) : + $multiple = ( $field['field_type'] == 'multi_select' ) ? 'multiple="multiple"' : ''; + ?> + + + '; + foreach( $roles as $role => $data ) : + if ( 'object' === $field['return_value'] ) { + $checked = ( !empty( $field['value'] ) && in_array( $role, wp_list_pluck( $field['value'], 'name' ) ) ) ? 'checked="checked"' : ''; + } else { + $checked = ( !empty( $field['value'] ) && in_array( $role, $field['value'] ) ) ? 'checked="checked"' : ''; + } + ?> +
+ '; + + echo ''; + endif; + } + + function format_value($value, $post_id, $field) { + if( $field['return_value'] == 'object' && is_array( $value ) ) { + foreach( $value as $key => $role ) { + $value[$key] = get_role( $role->name ); + } + } + return $value; + } + + function load_value($value, $post_id, $field) { + if( $field['return_value'] == 'object' && is_array( $value ) ) { + foreach( $value as $key => $name ) { + $value[$key] = get_role( $name ); + } + } + return $value; + } + +} + +new acf_field_role_selector(); diff --git a/options/origin-acf-fields.php b/options/origin-acf-fields.php index 022cd93..778650d 100644 --- a/options/origin-acf-fields.php +++ b/options/origin-acf-fields.php @@ -6,10 +6,10 @@ 'title' => 'Options', 'fields' => array( array( - 'key' => 'field_5a734005957ef', + 'key' => 'field_5bbf92f771f4b', 'label' => 'Clean Admin UI', - 'name' => 'clean_admin_ui', - 'type' => 'user', + 'name' => 'clean_admin_ui_role', + 'type' => 'role_selector', 'instructions' => '', 'required' => 0, 'conditional_logic' => 0, @@ -18,9 +18,9 @@ 'class' => '', 'id' => '', ), - 'role' => '', - 'allow_null' => 0, - 'multiple' => 1, + 'return_value' => 'name', + 'allowed_roles' => '', + 'field_type' => 'multi_select', ), array( 'key' => 'field_5a75b9d6337d3', @@ -28,9 +28,9 @@ 'name' => 'split_language_keys', 'type' => 'true_false', 'instructions' => 'Change la structure des datas recupérés par acf-rest-api.