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