| 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 |
/** |
| 52 |
* @param array $args |
| 53 |
*/ |
| 54 |
public function prepare_field_html( $args ) { |
| 55 |
$args = $this->fill_display_field_values( $args ); |
| 56 |
$value = $this->get_field_value( $args ); |
| 57 |
|
| 58 |
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"; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* @since 4.03.06 |
| 63 |
* |
| 64 |
* @param array $args Field display arguments. |
| 65 |
* |
| 66 |
* @return int|string |
| 67 |
*/ |
| 68 |
protected function get_field_value( $args ) { |
| 69 |
$user_ID = get_current_user_id(); |
| 70 |
$user_ID = $user_ID ? $user_ID : ''; |
| 71 |
$posted_value = FrmAppHelper::is_admin() && $_POST && isset( $_POST['item_meta'][ $this->field['id'] ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 72 |
$action = $args['action'] ?? $args['form_action'] ?? ''; |
| 73 |
$updating = $action === 'update'; |
| 74 |
return is_numeric( $this->field['value'] ) || $posted_value || $updating ? $this->field['value'] : $user_ID; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* @param array $args |
| 79 |
* |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
public function validate( $args ) { |
| 83 |
// phpcs:ignore Universal.Operators.StrictComparisons |
| 84 |
if ( '' == $args['value'] ) { |
| 85 |
return array(); |
| 86 |
} |
| 87 |
|
| 88 |
// Make sure we have a user ID. |
| 89 |
if ( ! is_numeric( $args['value'] ) ) { |
| 90 |
$args['value'] = FrmAppHelper::get_user_id_param( $args['value'] ); |
| 91 |
FrmEntriesHelper::set_posted_value( $this->field, $args['value'], $args ); |
| 92 |
} |
| 93 |
|
| 94 |
// Add user id to post variables to be saved with entry. |
| 95 |
$_POST['frm_user_id'] = $args['value']; |
| 96 |
|
| 97 |
return array(); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* @param array|string $value |
| 102 |
* @param array $atts |
| 103 |
* |
| 104 |
* @return array|string A string is returned, but the return signature should match FrmFieldType. |
| 105 |
*/ |
| 106 |
protected function prepare_display_value( $value, $atts ) { |
| 107 |
$user_info = $this->prepare_user_info_attribute( $atts ); |
| 108 |
return FrmFieldsHelper::get_user_display_name( $value, $user_info, $atts ); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Generate the user info attribute for displaying |
| 113 |
* a value from the user ID |
| 114 |
* From the get_display_name() function |
| 115 |
* |
| 116 |
* @since 3.0 |
| 117 |
* |
| 118 |
* @param array $atts |
| 119 |
* |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
private function prepare_user_info_attribute( $atts ) { |
| 123 |
if ( isset( $atts['show'] ) ) { |
| 124 |
return $atts['show'] === 'id' ? 'ID' : $atts['show']; |
| 125 |
} |
| 126 |
|
| 127 |
return apply_filters( 'frm_user_id_display', 'display_name' ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @param string $value |
| 132 |
* @param array $atts |
| 133 |
* |
| 134 |
* @return int |
| 135 |
*/ |
| 136 |
protected function prepare_import_value( $value, $atts ) { |
| 137 |
return FrmAppHelper::get_user_id_param( trim( $value ) ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* @since 4.0.04 |
| 142 |
* |
| 143 |
* @param mixed $value Field value passed by reference. |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function sanitize_value( &$value ) { |
| 148 |
if ( '' === $value ) { |
| 149 |
// Allow for an empty User ID. Return early to prevent it from getting set to 0. |
| 150 |
return; |
| 151 |
} |
| 152 |
FrmAppHelper::sanitize_value( 'intval', $value ); |
| 153 |
} |
| 154 |
} |
| 155 |
|