Completed
Push — master ( 1a7371...4a1bf4 )
by Stephanie
22s
created

FrmFieldUserID   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A include_form_builder_file() 0 3 1
B prepare_field_html() 0 11 8
A validate() 0 16 3
A prepare_display_value() 0 5 1
A prepare_user_info_attribute() 0 13 3
A prepare_import_value() 0 3 1
A sanitize_value() 0 3 1
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
42
		$user_ID      = get_current_user_id();
43
		$user_ID      = ( $user_ID ? $user_ID : '' );
44
		$posted_value = ( FrmAppHelper::is_admin() && $_POST && isset( $_POST['item_meta'][ $this->field['id'] ] ) ); // WPCS: CSRF ok.
45
		$updating     = ( isset( $args['action'] ) && $args['action'] == 'update' );
46
		$value        = ( is_numeric( $this->field['value'] ) || $posted_value || $updating ) ? $this->field['value'] : $user_ID;
47
48
		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";
49
	}
50
51
	public function validate( $args ) {
52
		if ( '' == $args['value'] ) {
53
			return array();
54
		}
55
56
		// make sure we have a user ID
57
		if ( ! is_numeric( $args['value'] ) ) {
58
			$args['value'] = FrmAppHelper::get_user_id_param( $args['value'] );
59
			FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args );
60
		}
61
62
		//add user id to post variables to be saved with entry
63
		$_POST['frm_user_id'] = $args['value'];
64
65
		return array();
66
	}
67
68
	/**
69
	 * @param $value
70
	 * @param $atts array
71
	 *
72
	 * @return false|mixed|string
73
	 */
74
	protected function prepare_display_value( $value, $atts ) {
75
		$user_info = $this->prepare_user_info_attribute( $atts );
76
77
		return FrmFieldsHelper::get_user_display_name( $value, $user_info, $atts );
78
	}
79
80
	/**
81
	 * Generate the user info attribute for displaying
82
	 * a value from the user ID
83
	 * From the get_display_name() function
84
	 *
85
	 * @since 3.0
86
	 *
87
	 * @param $atts
88
	 *
89
	 * @return string
90
	 */
91
	private function prepare_user_info_attribute( $atts ) {
92
		if ( isset( $atts['show'] ) ) {
93
			if ( $atts['show'] === 'id' ) {
94
				$user_info = 'ID';
95
			} else {
96
				$user_info = $atts['show'];
97
			}
98
		} else {
99
			$user_info = 'display_name';
100
		}
101
102
		return $user_info;
103
	}
104
105
	/**
106
	 * @param $value
107
	 * @param $atts
108
	 *
109
	 * @return int
110
	 */
111
	protected function prepare_import_value( $value, $atts ) {
112
		return FrmAppHelper::get_user_id_param( trim( $value ) );
113
	}
114
115
	/**
116
	 * @since 4.0
117
	 */
118
	public function sanitize_value( &$value ) {
119
		FrmAppHelper::sanitize_value( 'intval', $value );
120
	}
121
}
122