Skip to content

Commit

Permalink
change user to role
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanedemotte committed Oct 11, 2018
1 parent 05b3376 commit 737c761
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 20 deletions.
1 change: 1 addition & 0 deletions acf-origin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

Expand Down
127 changes: 127 additions & 0 deletions fields/origin-acf-field-role-selector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
class acf_field_role_selector extends acf_field {

function __construct() {
$this->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"' : '';
?>

<select name='<?php echo $field['name'] ?>[]' <?php echo $multiple ?>>
<?php
foreach( $roles as $role => $data ) :
if ( 'object' === $field['return_value'] ) {
$selected = ( !empty( $field['value'] ) && in_array( $role, wp_list_pluck( $field['value'], 'name' ) ) ) ? 'selected="selected"' : '';
} else {
$selected = ( !empty( $field['value'] ) && in_array( $role, $field['value'] ) ) ? 'selected="selected"' : '';
}
?>
<option <?php echo $selected ?> value='<?php echo $role ?>'><?php echo $data['name'] ?></option>
<?php endforeach; ?>
</select>
<?php
// checkbox and radio button fields
else :
echo '<ul class="acf-'.$field['field_type'].'-list '.$field['field_type'].' vertical">';
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"' : '';
}
?>
<li><label><input <?php echo $checked ?> type="<?php echo $field['field_type'] ?>" name="<?php echo $field['name'] ?>[]" value="<?php echo $role ?>"><?php echo $data['name'] ?></label></li>
<?php
endforeach;

echo '<input type="hidden" name="' . $field['name'] . '[]" value="" />';

echo '</ul>';
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();
18 changes: 9 additions & 9 deletions options/origin-acf-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,19 +18,19 @@
'class' => '',
'id' => '',
),
'role' => '',
'allow_null' => 0,
'multiple' => 1,
'return_value' => 'name',
'allowed_roles' => '',
'field_type' => 'multi_select',
),
array(
'key' => 'field_5a75b9d6337d3',
'label' => '[ACF-REST-API] Split language keys',
'name' => 'split_language_keys',
'type' => 'true_false',
'instructions' => 'Change la structure des datas recupérés par acf-rest-api.<br>
Nomer les champs dans ce style : fr.title, fr.meta.title, en.title, en.meta.title.<br>
[acf][fr.title] => [acf][fr][title]<br>
[acf][fr.meta.title] => [acf][fr][meta][title]',
Nomer les champs dans ce style : fr.title, fr.meta.title, en.title, en.meta.title.<br>
[acf][fr.title] => [acf][fr][title]<br>
[acf][fr.meta.title] => [acf][fr][meta][title]',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array(
Expand Down
15 changes: 4 additions & 11 deletions options/origin-acf-options.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,13 @@ function check_acf_rest_api($post_type) {
}

function check_clean_admin_ui() {
$users = get_field('clean_admin_ui', 'origin');
$roles = get_field('clean_admin_ui_role', 'origin');
$current_user = wp_get_current_user();
$role = $current_user->roles ? $current_user->roles[0] : false;

if(!$users)
return;

$clean = false;

foreach($users as $user):
if($user['ID'] == $current_user->ID)
$clean = true;
endforeach;
if(!$roles) return;

if($clean):
if(array_search($role, $roles) !== false):
add_action('admin_menu', [$this, 'clean_admin_ui'], 9999);
add_filter('acf/settings/show_admin', '__return_false');
endif;
Expand Down

0 comments on commit 737c761

Please sign in to comment.