PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.2
JetFormBuilder — Dynamic Blocks Form Builder v3.6.2
3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / includes / generators / get-from-users.php
jetformbuilder / includes / generators Last commit date
base-v2.php 2 months ago base.php 2 years ago get-from-db.php 2 months ago get-from-rest-api.php 2 months ago get-from-users.php 2 months ago get-related-posts.php 2 months ago legacy-parser.php 2 months ago num-range-manual.php 2 months ago num-range.php 2 months ago registry.php 2 months ago
get-from-users.php
270 lines
1 <?php
2 /**
3 * WordPress Users Generator.
4 *
5 * Generates a list of WordPress users as options for Select/Checkbox/Radio fields.
6 * Supports filtering by role and configurable value/label fields.
7 *
8 * @package Jet_Form_Builder\Generators
9 */
10
11 namespace Jet_Form_Builder\Generators;
12
13 // If this file is called directly, abort.
14 if ( ! defined( 'WPINC' ) ) {
15 die;
16 }
17
18 /**
19 * Get_From_Users class.
20 *
21 * Generates options from WordPress users list.
22 */
23 class Get_From_Users extends Base_V2 {
24
25 /**
26 * Returns generator ID.
27 *
28 * @return string
29 */
30 public function get_id() {
31 return 'get_from_users';
32 }
33
34 /**
35 * Returns generator name.
36 *
37 * @return string
38 */
39 public function get_name() {
40 return __( 'Get values list from WordPress Users', 'jet-form-builder' );
41 }
42
43 /**
44 * Returns structured settings schema.
45 *
46 * @return array
47 */
48 public function get_settings_schema(): array {
49 return array(
50 'roles' => array(
51 'type' => 'array',
52 'default' => array(),
53 'label' => __( 'Filter by Roles (optional)', 'jet-form-builder' ),
54 'control' => 'select',
55 'multiple' => true,
56 'options' => $this->get_roles_options(),
57 'help' => __( 'Select roles to include. Leave empty to include all roles.', 'jet-form-builder' ),
58 ),
59 'value_field' => array(
60 'type' => 'string',
61 'default' => 'ID',
62 'label' => __( 'Option Value', 'jet-form-builder' ),
63 'control' => 'select',
64 'options' => array(
65 array( 'value' => 'ID', 'label' => __( 'User ID', 'jet-form-builder' ) ),
66 array( 'value' => 'user_login', 'label' => __( 'Username (login)', 'jet-form-builder' ) ),
67 array( 'value' => 'user_email', 'label' => __( 'Email', 'jet-form-builder' ) ),
68 array( 'value' => 'user_nicename', 'label' => __( 'Nicename (slug)', 'jet-form-builder' ) ),
69 ),
70 ),
71 'label_field' => array(
72 'type' => 'string',
73 'default' => 'display_name',
74 'label' => __( 'Option Label', 'jet-form-builder' ),
75 'control' => 'select',
76 'options' => array(
77 array( 'value' => 'display_name', 'label' => __( 'Display Name', 'jet-form-builder' ) ),
78 array( 'value' => 'user_login', 'label' => __( 'Username (login)', 'jet-form-builder' ) ),
79 array( 'value' => 'user_email', 'label' => __( 'Email', 'jet-form-builder' ) ),
80 ),
81 ),
82 'include_current' => array(
83 'type' => 'boolean',
84 'default' => true,
85 'label' => __( 'Include Current User', 'jet-form-builder' ),
86 'control' => 'toggle',
87 ),
88 'orderby' => array(
89 'type' => 'string',
90 'default' => 'display_name',
91 'label' => __( 'Order By', 'jet-form-builder' ),
92 'control' => 'select',
93 'options' => array(
94 array( 'value' => 'display_name', 'label' => __( 'Display Name', 'jet-form-builder' ) ),
95 array( 'value' => 'user_login', 'label' => __( 'Username', 'jet-form-builder' ) ),
96 array( 'value' => 'registered', 'label' => __( 'Registration Date', 'jet-form-builder' ) ),
97 array( 'value' => 'ID', 'label' => __( 'User ID', 'jet-form-builder' ) ),
98 ),
99 ),
100 'number' => array(
101 'type' => 'number',
102 'default' => -1,
103 'label' => __( 'Max Users', 'jet-form-builder' ),
104 'control' => 'number',
105 'min' => -1,
106 'help' => __( 'Set to -1 to include all users.', 'jet-form-builder' ),
107 ),
108 );
109 }
110
111 /**
112 * Whether this generator supports auto-update/cascading feature.
113 *
114 * @return bool
115 */
116 public function supports_auto_update(): bool {
117 return true;
118 }
119
120 /**
121 * Returns context field hints for the editor.
122 * Signals that a single listened field provides the role to filter by.
123 *
124 * @return array
125 */
126 public function get_auto_update_context_fields(): array {
127 return array(
128 array(
129 'single' => true,
130 'description' => __( 'The Trigger Field value overrides the static "Filter by Roles" setting above. If the Trigger Field is empty, the static roles are used.', 'jet-form-builder' ),
131 'example' => __( 'Select the field that returns one or more user role slugs, for example: administrator, editor, subscriber.', 'jet-form-builder' ),
132 ),
133 );
134 }
135
136 /**
137 * Returns the auto-update value type supported by this generator.
138 *
139 * @return string
140 */
141 public function get_auto_update_value_type(): string {
142 return 'scalar_or_array';
143 }
144
145 /**
146 * Empty trigger should fall back to the static roles configuration.
147 *
148 * @return string
149 */
150 public function get_auto_update_empty_context_policy(): string {
151 return 'fallback_to_static';
152 }
153
154 /**
155 * Generates options with context from a dependent field.
156 * Overrides the roles setting with the value from the listened field.
157 *
158 * @param array $settings Parsed settings from block attributes.
159 * @param array $context Associative array ['field_name' => 'value'] from dependent fields.
160 *
161 * @return array Generated options
162 */
163 public function generate_with_context( array $settings, array $context = array() ): array {
164 if ( ! empty( $context ) ) {
165 $context_value = reset( $context );
166
167 // Support both single role (scalar) and multi-role (checkbox/multi-select array).
168 if ( is_array( $context_value ) ) {
169 $roles = array_filter(
170 array_map(
171 static function ( $role ) {
172 return sanitize_key( (string) $role );
173 },
174 $context_value
175 )
176 );
177
178 if ( ! empty( $roles ) ) {
179 $settings['roles'] = array_values( $roles );
180 }
181 } else {
182 $role = sanitize_key( (string) $context_value );
183 if ( $role ) {
184 $settings['roles'] = array( $role );
185 }
186 }
187 }
188
189 return $this->generate( $settings );
190 }
191
192 /**
193 * Returns WP roles as options array for multi-select.
194 *
195 * @return array
196 */
197 private function get_roles_options(): array {
198 $options = array();
199
200 foreach ( wp_roles()->roles as $slug => $role ) {
201 $options[] = array(
202 'value' => $slug,
203 'label' => translate_user_role( $role['name'] ),
204 );
205 }
206
207 return $options;
208 }
209
210 /**
211 * Generates options from WordPress users.
212 *
213 * @param array $args Settings from schema.
214 *
215 * @return array
216 */
217 public function generate( $args ) {
218 $query_args = array(
219 'orderby' => $args['orderby'] ?? 'display_name',
220 'order' => 'ASC',
221 'fields' => 'all',
222 );
223
224 $number = intval( $args['number'] ?? -1 );
225 if ( $number > 0 ) {
226 $query_args['number'] = $number;
227 }
228
229 // Filter by roles (array from multi-select)
230 $roles = $args['roles'] ?? array();
231 if ( is_array( $roles ) ) {
232 $roles = array_filter( $roles );
233 } else {
234 // Backward compat: handle legacy comma-separated string
235 $roles = array_filter( array_map( 'trim', explode( ',', (string) $roles ) ) );
236 }
237 if ( ! empty( $roles ) ) {
238 $query_args['role__in'] = array_values( $roles );
239 }
240
241 // Exclude current user if needed
242 $include_current = isset( $args['include_current'] ) ? (bool) $args['include_current'] : true;
243 if ( ! $include_current ) {
244 $current_user_id = get_current_user_id();
245 if ( $current_user_id ) {
246 $query_args['exclude'] = array( $current_user_id );
247 }
248 }
249
250 $users = get_users( $query_args );
251
252 $value_field = $args['value_field'] ?? 'ID';
253 $label_field = $args['label_field'] ?? 'display_name';
254
255 $result = array();
256
257 foreach ( $users as $user ) {
258 $value = $user->$value_field ?? $user->ID;
259 $label = $user->$label_field ?? $user->display_name;
260
261 $result[] = array(
262 'value' => strval( $value ),
263 'label' => strval( $label ),
264 );
265 }
266
267 return $result;
268 }
269 }
270