|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Registers a User field for the forms. |
| 4 | + * |
| 5 | + * @package wp-user-manager |
| 6 | + * @copyright Copyright (c) 2021, WP User Manager |
| 7 | + * @license https://opensource.org/licenses/GPL-3.0 GNU Public License |
| 8 | + */ |
| 9 | + |
| 10 | +// Exit if accessed directly |
| 11 | +if ( ! defined( 'ABSPATH' ) ) exit; |
| 12 | + |
| 13 | +/** |
| 14 | + * Register a dropdown field type. |
| 15 | + */ |
| 16 | +class WPUM_Field_Userrole extends WPUM_Field_Type { |
| 17 | + |
| 18 | + public function __construct() { |
| 19 | + $this->name = esc_html__( 'User Role', 'wp-user-manager' ); |
| 20 | + $this->type = 'userrole'; |
| 21 | + $this->icon = 'dashicons-admin-generic'; |
| 22 | + $this->group = 'advanced'; |
| 23 | + $this->allow_default = true; |
| 24 | + $this->min_addon_version = '2.5'; |
| 25 | + } |
| 26 | + |
| 27 | + public function get_data_keys() { |
| 28 | + $keys = parent::get_data_keys(); |
| 29 | + |
| 30 | + return array_merge( $keys, array_keys( $this->get_editor_settings()['general'] ) ); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @return array |
| 35 | + */ |
| 36 | + public function get_editor_settings() { |
| 37 | + $settings = [ |
| 38 | + 'general' => [ |
| 39 | + 'options' => array( |
| 40 | + 'type' => 'multiselect', |
| 41 | + 'label' => __( 'User Roles', 'wp-user-manager' ), |
| 42 | + 'hint' => esc_html__( 'List of roles the users can select from', 'wp-user-manager' ), |
| 43 | + 'model' => 'options', |
| 44 | + 'labels' => [], |
| 45 | + 'required' => true, |
| 46 | + 'options' => wpum_get_roles(), |
| 47 | + 'multiple' => true, |
| 48 | + ), |
| 49 | + 'type_label' => array( |
| 50 | + 'type' => 'input', |
| 51 | + 'inputType' => 'text', |
| 52 | + 'label' => esc_html__( 'Type label', 'wp-user-manager' ), |
| 53 | + 'model' => 'type_label', |
| 54 | + 'default' => 'Role', |
| 55 | + ), |
| 56 | + ], |
| 57 | + ]; |
| 58 | + |
| 59 | + if ( wpum_get_option( 'allow_multiple_user_roles' ) ) { |
| 60 | + $settings['allow_multiple'] = array( |
| 61 | + 'type' => 'checkbox', |
| 62 | + 'label' => esc_html__( 'Allow multiple selection', 'wp-user-manager' ), |
| 63 | + 'hint' => esc_html__( 'Allow users to select multiple roles for themselves', 'wp-user-manager' ), |
| 64 | + 'model' => 'allow_multiple', |
| 65 | + 'default' => false, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + return $settings; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Format the output onto the profiles for the taxonomy field. |
| 74 | + * |
| 75 | + * @param object $field |
| 76 | + * @param mixed $value |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + function get_formatted_output( $field, $value ) { |
| 80 | + if ( ! is_array( $value ) ) { |
| 81 | + $value = array( $value ); |
| 82 | + } |
| 83 | + |
| 84 | + return implode( ', ', $value ); |
| 85 | + } |
| 86 | + |
| 87 | + public function template() { |
| 88 | + return $this->type; |
| 89 | + } |
| 90 | +} |
0 commit comments