1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
// Prevent loading this file directly
defined( 'ABSPATH' ) || exit;
// Make sure "select" field is loaded
require_once WCQD_METABOX_FIELDS_DIR . 'select-advanced.php';
if ( ! class_exists( 'WCQD_METABOX_User_Field' ) )
{
class WCQD_METABOX_User_Field extends WCQD_METABOX_Select_Advanced_Field
{
/**
* Get field HTML
*
* @param mixed $meta
* @param array $field
*
* @return string
*/
static function html( $meta, $field )
{
$field['options'] = self::get_options( $field );
switch ( $field['field_type'] )
{
case 'select':
return WCQD_METABOX_Select_Field::html( $meta, $field );
break;
case 'select_advanced':
default:
return WCQD_METABOX_Select_Advanced_Field::html( $meta, $field );
}
}
/**
* Normalize parameters for field
*
* @param array $field
*
* @return array
*/
static function normalize_field( $field )
{
$field = wp_parse_args( $field, array(
'field_type' => 'select_advanced',
'parent' => false,
'query_args' => array(),
) );
$field['std'] = empty( $field['std'] ) ? __( 'Select an user', 'meta-box' ) : $field['std'];
$field['query_args'] = wp_parse_args( $field['query_args'], array(
'orderby' => 'display_name',
'order' => 'asc',
'role' => '',
'fields' => 'all',
) );
switch ( $field['field_type'] )
{
case 'select':
return WCQD_METABOX_Select_Field::normalize_field( $field );
break;
case 'select_advanced':
default:
return WCQD_METABOX_Select_Advanced_Field::normalize_field( $field );
}
}
/**
* Get users
*
* @param array $field
*
* @return array
*/
static function get_options( $field )
{
$results = get_users( $field['query_args'] );
$options = array();
foreach ( $results as $result )
{
$options[$result->ID] = $result->display_name;
}
return $options;
}
/**
* Get option label to display in the frontend
*
* @param int $value Option value
* @param int $index Array index
* @param array $field Field parameter
*
* @return string
*/
static function get_option_label( &$value, $index, $field )
{
$user = get_userdata( $value );
$value = '<a href="' . get_author_posts_url( $value ) . '">' . $user->display_name . '</a>';
}
}
}