| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 3.0 |
| 8 |
*/ |
| 9 |
class FrmFieldUserID extends FrmFieldType { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var string |
| 13 |
* |
| 14 |
* @since 3.0 |
| 15 |
*/ |
| 16 |
protected $type = 'user_id'; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var bool |
| 20 |
* |
| 21 |
* @since 3.0 |
| 22 |
*/ |
| 23 |
protected $has_input = false; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var bool |
| 27 |
* |
| 28 |
* @since 3.0 |
| 29 |
*/ |
| 30 |
protected $has_html = false; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var bool |
| 34 |
* |
| 35 |
* @since 3.0 |
| 36 |
*/ |
| 37 |
protected $holds_email_values = true; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var bool |
| 41 |
*/ |
| 42 |
protected $array_allowed = false; |
| 43 |
|
| 44 |
/** |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
protected function include_form_builder_file() { |
| 48 |
return FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/field-user-id.php'; |
| 49 |
} |
| 50 |
|
| 51 |
public function prepare_field_html( $args ) { |
| 52 |
$args = $this->fill_display_field_values( $args ); |
| 53 |
$value = $this->get_field_value( $args ); |
| 54 |
|
| 55 |
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"; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @since 4.03.06 |
| 60 |
* |
| 61 |
* @param array $args Field display arguments. |
| 62 |
* |
| 63 |
* @return int|string |
| 64 |
*/ |
| 65 |
protected function get_field_value( $args ) { |
| 66 |
$user_ID = get_current_user_id(); |
| 67 |
$user_ID = ( $user_ID ? $user_ID : '' ); |
| 68 |
$posted_value = ( FrmAppHelper::is_admin() && $_POST && isset( $_POST['item_meta'][ $this->field['id'] ] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 69 |
$action = ( $args['action'] ?? $args['form_action'] ?? '' ); |
| 70 |
$updating = $action === 'update'; |
| 71 |
return is_numeric( $this->field['value'] ) || $posted_value || $updating ? $this->field['value'] : $user_ID; |
| 72 |
} |
| 73 |
|
| 74 |
public function validate( $args ) { |
| 75 |
if ( '' == $args['value'] ) { |
| 76 |
return array(); |
| 77 |
} |
| 78 |
|
| 79 |
// Make sure we have a user ID. |
| 80 |
if ( ! is_numeric( $args['value'] ) ) { |
| 81 |
$args['value'] = FrmAppHelper::get_user_id_param( $args['value'] ); |
| 82 |
FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args ); |
| 83 |
} |
| 84 |
|
| 85 |
// Add user id to post variables to be saved with entry. |
| 86 |
$_POST['frm_user_id'] = $args['value']; |
| 87 |
|
| 88 |
return array(); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @param array|string $value |
| 93 |
* @param array $atts |
| 94 |
* |
| 95 |
* @return array|string A string is returned, but the return signature should match FrmFieldType. |
| 96 |
*/ |
| 97 |
protected function prepare_display_value( $value, $atts ) { |
| 98 |
$user_info = $this->prepare_user_info_attribute( $atts ); |
| 99 |
|
| 100 |
return FrmFieldsHelper::get_user_display_name( $value, $user_info, $atts ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Generate the user info attribute for displaying |
| 105 |
* a value from the user ID |
| 106 |
* From the get_display_name() function |
| 107 |
* |
| 108 |
* @since 3.0 |
| 109 |
* |
| 110 |
* @param array $atts |
| 111 |
* |
| 112 |
* @return string |
| 113 |
*/ |
| 114 |
private function prepare_user_info_attribute( $atts ) { |
| 115 |
if ( isset( $atts['show'] ) ) { |
| 116 |
if ( $atts['show'] === 'id' ) { |
| 117 |
$user_info = 'ID'; |
| 118 |
} else { |
| 119 |
$user_info = $atts['show']; |
| 120 |
} |
| 121 |
} else { |
| 122 |
$user_info = apply_filters( 'frm_user_id_display', 'display_name' ); |
| 123 |
} |
| 124 |
|
| 125 |
return $user_info; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* @param string $value |
| 130 |
* @param array $atts |
| 131 |
* |
| 132 |
* @return int |
| 133 |
*/ |
| 134 |
protected function prepare_import_value( $value, $atts ) { |
| 135 |
return FrmAppHelper::get_user_id_param( trim( $value ) ); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* @since 4.0.04 |
| 140 |
* |
| 141 |
* @param mixed $value Field value passed by reference. |
| 142 |
* |
| 143 |
* @return void |
| 144 |
*/ |
| 145 |
public function sanitize_value( &$value ) { |
| 146 |
if ( '' === $value ) { |
| 147 |
// Allow for an empty User ID. Return early to prevent it from getting set to 0. |
| 148 |
return; |
| 149 |
} |
| 150 |
FrmAppHelper::sanitize_value( 'intval', $value ); |
| 151 |
} |
| 152 |
} |
| 153 |
|