Completed
Push — master ( a1e96d...4ae10a )
by Stephanie
02:15
created

FrmFieldUserID::prepare_user_info_attribute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @since 3.0
5
 */
6
class FrmFieldUserID extends FrmFieldType {
7
8
	/**
9
	 * @var string
10
	 * @since 3.0
11
	 */
12
	protected $type = 'user_id';
13
14
	/**
15
	 * @var bool
16
	 * @since 3.0
17
	 */
18
	protected $has_input = false;
19
20
	/**
21
	 * @var bool
22
	 * @since 3.0
23
	 */
24
	protected $has_html = false;
25
26
	/**
27
	 * @var bool
28
	 * @since 3.0
29
	 */
30
	protected $holds_email_values = true;
31
32
	/**
33
	 * @return string
34
	 */
35
	protected function include_form_builder_file() {
36
		return FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/field-user-id.php';
37
	}
38
39
	public function prepare_field_html( $args ) {
40
		$args  = $this->fill_display_field_values( $args );
41
		$value = $this->get_field_value( $args );
42
43
		echo '<input type="hidden" name="' . esc_attr( $args['field_name'] ) . '" id="' . esc_attr( $args['html_id'] ) . '" value="' . esc_attr( $value ) . '" data-frmval="' . esc_attr( $value ) . '"/>' . "\n";
44
	}
45
46
	/**
47
	 * @since 4.03.06
48
	 */
49
	protected function get_field_value( $args ) {
50
		$user_ID      = get_current_user_id();
51
		$user_ID      = ( $user_ID ? $user_ID : '' );
52
		$posted_value = ( FrmAppHelper::is_admin() && $_POST && isset( $_POST['item_meta'][ $this->field['id'] ] ) ); // WPCS: CSRF ok.
53
		$action       = ( isset( $args['action'] ) ? $args['action'] : ( isset( $args['form_action'] ) ? $args['form_action'] : '' ) );
54
		$updating     = $action == 'update';
55
		return ( is_numeric( $this->field['value'] ) || $posted_value || $updating ) ? $this->field['value'] : $user_ID;
56
	}
57
58
	public function validate( $args ) {
59
		if ( '' == $args['value'] ) {
60
			return array();
61
		}
62
63
		// make sure we have a user ID
64
		if ( ! is_numeric( $args['value'] ) ) {
65
			$args['value'] = FrmAppHelper::get_user_id_param( $args['value'] );
66
			FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args );
67
		}
68
69
		//add user id to post variables to be saved with entry
70
		$_POST['frm_user_id'] = $args['value'];
71
72
		return array();
73
	}
74
75
	/**
76
	 * @param $value
77
	 * @param $atts array
78
	 *
79
	 * @return false|mixed|string
80
	 */
81
	protected function prepare_display_value( $value, $atts ) {
82
		$user_info = $this->prepare_user_info_attribute( $atts );
83
84
		return FrmFieldsHelper::get_user_display_name( $value, $user_info, $atts );
85
	}
86
87
	/**
88
	 * Generate the user info attribute for displaying
89
	 * a value from the user ID
90
	 * From the get_display_name() function
91
	 *
92
	 * @since 3.0
93
	 *
94
	 * @param $atts
95
	 *
96
	 * @return string
97
	 */
98
	private function prepare_user_info_attribute( $atts ) {
99
		if ( isset( $atts['show'] ) ) {
100
			if ( $atts['show'] === 'id' ) {
101
				$user_info = 'ID';
102
			} else {
103
				$user_info = $atts['show'];
104
			}
105
		} else {
106
			$user_info = 'display_name';
107
		}
108
109
		return $user_info;
110
	}
111
112
	/**
113
	 * @param $value
114
	 * @param $atts
115
	 *
116
	 * @return int
117
	 */
118
	protected function prepare_import_value( $value, $atts ) {
119
		return FrmAppHelper::get_user_id_param( trim( $value ) );
120
	}
121
122
	/**
123
	 * @since 4.0.04
124
	 */
125
	public function sanitize_value( &$value ) {
126
		FrmAppHelper::sanitize_value( 'intval', $value );
127
	}
128
}
129