| @@ -1,12 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 3 | - die( 'You are not allowed to call this page directly.' ); | |
| 4 | -} | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * A class for the field value selector |
| 8 | - * Used in field conditional logic, action conditional logic, Mailchimp action, etc. | |
| 5 | + * Used in field conditional logic, action conditional logic, MailChimp action, etc. | |
| 9 | 6 | * |
| 10 | 7 | * @since 2.03.05 |
| 11 | 8 | */ |
| 12 | 9 | class FrmFieldValueSelector { |
| @@ -25,13 +22,13 @@ | ||
| 25 | 22 | */ |
| 26 | 23 | protected $field_key = ''; |
| 27 | 24 | |
| 28 | 25 | /** |
| 29 | - * @var FrmProFieldSettings|null | |
| 26 | + * @var null | |
| 30 | 27 | * |
| 31 | 28 | * @since 2.03.05 |
| 32 | 29 | */ |
| 33 | - protected $field_settings; | |
| 30 | + protected $field_settings = null; | |
| 34 | 31 | |
| 35 | 32 | /** |
| 36 | 33 | * @var array |
| 37 | 34 | * |
| @@ -38,8 +35,9 @@ | ||
| 38 | 35 | * @since 2.03.05 |
| 39 | 36 | */ |
| 40 | 37 | protected $options = array(); |
| 41 | 38 | |
| 39 | + | |
| 42 | 40 | /** |
| 43 | 41 | * @var string |
| 44 | 42 | * |
| 45 | 43 | * @since 2.03.05 |
| @@ -67,37 +65,25 @@ | ||
| 67 | 65 | */ |
| 68 | 66 | protected $blank_option_label = ''; |
| 69 | 67 | |
| 70 | 68 | /** |
| 71 | - * @var object|null | |
| 69 | + * @var object | |
| 72 | 70 | * |
| 73 | 71 | * @since 2.03.05 |
| 74 | 72 | */ |
| 75 | - protected $db_row; | |
| 73 | + protected $db_row = null; | |
| 76 | 74 | |
| 77 | 75 | /** |
| 78 | - * Field dropdowns will truncate at 25 characters by default. | |
| 79 | - * | |
| 80 | - * @var int|null | |
| 81 | - * | |
| 82 | - * @since 6.16.2 | |
| 83 | - */ | |
| 84 | - protected $truncate; | |
| 85 | - | |
| 86 | - /** | |
| 87 | 76 | * FrmFieldValueSelector constructor |
| 88 | 77 | * |
| 89 | - * @param int|string $field_id Field ID. | |
| 90 | - * @param array $args Arguments for configuring the value selector. | |
| 78 | + * @param int|string $field_id | |
| 91 | 79 | */ |
| 92 | 80 | public function __construct( $field_id, $args ) { |
| 93 | 81 | $this->set_html_name( $args ); |
| 94 | 82 | $this->set_value( $args ); |
| 95 | 83 | $this->set_source( $args ); |
| 96 | - $this->set_truncate( $args ); | |
| 97 | 84 | |
| 98 | 85 | $this->field_id = (int) $field_id; |
| 99 | - | |
| 100 | 86 | if ( $this->field_id === 0 ) { |
| 101 | 87 | return; |
| 102 | 88 | } |
| 103 | 89 | |
| @@ -102,15 +88,13 @@ | ||
| 102 | 88 | } |
| 103 | 89 | |
| 104 | 90 | $this->set_db_row(); |
| 105 | 91 | |
| 106 | - if ( ! $this->has_db_row() ) { | |
| 107 | - return; | |
| 92 | + if ( $this->has_db_row() ) { | |
| 93 | + $this->set_field_key(); | |
| 94 | + $this->set_field_settings(); | |
| 95 | + $this->set_options(); | |
| 108 | 96 | } |
| 109 | - | |
| 110 | - $this->set_field_key(); | |
| 111 | - $this->set_field_settings(); | |
| 112 | - $this->set_options(); | |
| 113 | 97 | } |
| 114 | 98 | |
| 115 | 99 | /** |
| 116 | 100 | * Set the db_row property |
| @@ -115,10 +99,8 @@ | ||
| 115 | 99 | /** |
| 116 | 100 | * Set the db_row property |
| 117 | 101 | * |
| 118 | 102 | * @since 2.03.05 |
| 119 | - * | |
| 120 | - * @return void | |
| 121 | 103 | */ |
| 122 | 104 | private function set_db_row() { |
| 123 | 105 | $where = array( |
| 124 | 106 | 'id' => $this->field_id, |
| @@ -134,10 +116,8 @@ | ||
| 134 | 116 | /** |
| 135 | 117 | * Set the field_key property |
| 136 | 118 | * |
| 137 | 119 | * @since 2.03.05 |
| 138 | - * | |
| 139 | - * @return void | |
| 140 | 120 | */ |
| 141 | 121 | private function set_field_key() { |
| 142 | 122 | $this->field_key = $this->db_row->field_key; |
| 143 | 123 | } |
| @@ -145,10 +125,8 @@ | ||
| 145 | 125 | /** |
| 146 | 126 | * Set the field_settings property |
| 147 | 127 | * |
| 148 | 128 | * @since 2.03.05 |
| 149 | - * | |
| 150 | - * @return void | |
| 151 | 129 | */ |
| 152 | 130 | protected function set_field_settings() { |
| 153 | 131 | // Leave as null for free version |
| 154 | 132 | } |
| @@ -156,13 +134,11 @@ | ||
| 156 | 134 | /** |
| 157 | 135 | * Set the options property |
| 158 | 136 | * |
| 159 | 137 | * @since 2.03.05 |
| 160 | - * | |
| 161 | - * @return void | |
| 162 | 138 | */ |
| 163 | 139 | protected function set_options() { |
| 164 | - $field_obj = FrmFieldFactory::get_field_object( $this->db_row ); | |
| 140 | + $field_obj = FrmFieldFactory::get_field_object( $this->db_row ); | |
| 165 | 141 | $this->options = $field_obj->get_options( array() ); |
| 166 | 142 | } |
| 167 | 143 | |
| 168 | 144 | /** |
| @@ -170,10 +146,8 @@ | ||
| 170 | 146 | * |
| 171 | 147 | * @since 2.03.05 |
| 172 | 148 | * |
| 173 | 149 | * @param array $args |
| 174 | - * | |
| 175 | - * @return void | |
| 176 | 150 | */ |
| 177 | 151 | protected function set_html_name( $args ) { |
| 178 | 152 | if ( isset( $args['html_name'] ) ) { |
| 179 | 153 | $this->html_name = (string) $args['html_name']; |
| @@ -185,10 +159,8 @@ | ||
| 185 | 159 | * |
| 186 | 160 | * @since 2.03.05 |
| 187 | 161 | * |
| 188 | 162 | * @param array $args |
| 189 | - * | |
| 190 | - * @return void | |
| 191 | 163 | */ |
| 192 | 164 | protected function set_value( $args ) { |
| 193 | 165 | if ( isset( $args['value'] ) ) { |
| 194 | 166 | $this->value = (string) $args['value']; |
| @@ -200,10 +172,8 @@ | ||
| 200 | 172 | * |
| 201 | 173 | * @since 2.03.05 |
| 202 | 174 | * |
| 203 | 175 | * @param array $args |
| 204 | - * | |
| 205 | - * @return void | |
| 206 | 176 | */ |
| 207 | 177 | protected function set_source( $args ) { |
| 208 | 178 | if ( isset( $args['source'] ) ) { |
| 209 | 179 | $this->source = (string) $args['source']; |
| @@ -210,21 +180,8 @@ | ||
| 210 | 180 | } |
| 211 | 181 | } |
| 212 | 182 | |
| 213 | 183 | /** |
| 214 | - * @since 6.16.2 | |
| 215 | - * | |
| 216 | - * @param array $args | |
| 217 | - * | |
| 218 | - * @return void | |
| 219 | - */ | |
| 220 | - protected function set_truncate( $args ) { | |
| 221 | - if ( isset( $args['truncate'] ) && is_numeric( $args['truncate'] ) ) { | |
| 222 | - $this->truncate = (int) $args['truncate']; | |
| 223 | - } | |
| 224 | - } | |
| 225 | - | |
| 226 | - /** | |
| 227 | 184 | * Check if object has any options |
| 228 | 185 | * |
| 229 | 186 | * @since 2.03.05 |
| 230 | 187 | * |
| @@ -248,10 +205,8 @@ | ||
| 248 | 205 | /** |
| 249 | 206 | * Display the field value selector (dropdown or text field) |
| 250 | 207 | * |
| 251 | 208 | * @since 2.03.05 |
| 252 | - * | |
| 253 | - * @return void | |
| 254 | 209 | */ |
| 255 | 210 | public function display() { |
| 256 | 211 | if ( $this->has_options() ) { |
| 257 | 212 | $this->display_dropdown(); |
| @@ -263,10 +218,8 @@ | ||
| 263 | 218 | /** |
| 264 | 219 | * Print the field value text box |
| 265 | 220 | * |
| 266 | 221 | * @since 2.03.05 |
| 267 | - * | |
| 268 | - * @return void | |
| 269 | 222 | */ |
| 270 | 223 | public function display_text_box() { |
| 271 | 224 | echo '<input type="text" name="' . esc_attr( $this->html_name ) . '" value="' . esc_attr( trim( $this->value ) ) . '" />'; |
| 272 | 225 | } |
| @@ -274,41 +227,25 @@ | ||
| 274 | 227 | /** |
| 275 | 228 | * Display the field value selector |
| 276 | 229 | * |
| 277 | 230 | * @since 2.03.05 |
| 278 | - * | |
| 279 | - * @return void | |
| 280 | 231 | */ |
| 281 | 232 | protected function display_dropdown() { |
| 282 | 233 | echo '<select name="' . esc_attr( $this->html_name ) . '">'; |
| 283 | - echo '<option value="">' . esc_html( $this->blank_option_label ) . '</option>'; | |
| 234 | + echo '<option value="">' . esc_attr( $this->blank_option_label ) . '</option>'; | |
| 284 | 235 | |
| 285 | - if ( $this->options ) { | |
| 286 | - $truncate = $this->truncate ?? 25; | |
| 287 | - | |
| 236 | + if ( ! empty( $this->options ) ) { | |
| 288 | 237 | foreach ( $this->options as $key => $value ) { |
| 289 | - // phpcs:ignore Universal.Operators.StrictComparisons | |
| 290 | 238 | if ( $value == '' ) { |
| 291 | 239 | continue; |
| 292 | 240 | } |
| 293 | 241 | |
| 294 | 242 | $option = $this->get_single_field_option( $key, $value ); |
| 295 | - $option->print_single_option( $this->value, $truncate, $this->use_value_as_label_in_dropdown() ); | |
| 243 | + $option->print_single_option( $this->value, 25 ); | |
| 296 | 244 | } |
| 297 | 245 | } |
| 298 | 246 | |
| 299 | 247 | echo '</select>'; |
| 300 | - } | |
| 301 | - | |
| 302 | - /** | |
| 303 | - * Whether to use the field value as the option label in the dropdown when the label is empty. | |
| 304 | - * | |
| 305 | - * @since 6.25.1 | |
| 306 | - * | |
| 307 | - * @return bool | |
| 308 | - */ | |
| 309 | - protected function use_value_as_label_in_dropdown() { | |
| 310 | - return false; | |
| 311 | 248 | } |
| 312 | 249 | |
| 313 | 250 | /** |
| 314 | 251 | * Get an instance of FrmFieldOption |