@@ 300-358 (lines=59) @@ | ||
297 | * |
|
298 | * @param array $group_rows Group rows |
|
299 | */ |
|
300 | public function process_loaded_values( $group_rows ) { |
|
301 | $input_groups = array(); |
|
302 | ||
303 | // Set default values |
|
304 | $field_names = array(); |
|
305 | foreach ( $this->groups as $group ) { |
|
306 | $group_fields = $group->get_fields(); |
|
307 | foreach ( $group_fields as $field ) { |
|
308 | $field_names[] = $field->get_name(); |
|
309 | $field->set_value( $field->get_default_value() ); |
|
310 | } |
|
311 | } |
|
312 | ||
313 | if ( empty( $group_rows ) ) { |
|
314 | return; |
|
315 | } |
|
316 | ||
317 | // load and parse values and group type |
|
318 | foreach ( $group_rows as $row ) { |
|
319 | if ( ! preg_match( Helper::get_complex_field_regex( $this->name, array_keys( $this->groups ), $field_names ), $row['field_key'], $field_name ) ) { |
|
320 | continue; |
|
321 | } |
|
322 | ||
323 | $row['field_value'] = maybe_unserialize( $row['field_value'] ); |
|
324 | $input_groups[ $field_name['index'] ]['type'] = $field_name['group']; |
|
325 | ||
326 | if ( ! empty( $field_name['trailing'] ) ) { |
|
327 | $input_groups[ $field_name['index'] ][ $field_name['key'] . '_' . $field_name['sub'] . '-' . $field_name['trailing'] ] = $row['field_value']; |
|
328 | } else if ( ! empty( $field_name['sub'] ) ) { |
|
329 | $input_groups[ $field_name['index'] ][ $field_name['key'] ][ $field_name['sub'] ] = $row['field_value']; |
|
330 | } else { |
|
331 | $input_groups[ $field_name['index'] ][ $field_name['key'] ] = $row['field_value']; |
|
332 | } |
|
333 | } |
|
334 | ||
335 | // create groups list with loaded fields |
|
336 | ksort( $input_groups ); |
|
337 | ||
338 | foreach ( $input_groups as $index => $values ) { |
|
339 | $value_group = array( 'type' => $values['type'] ); |
|
340 | $group_fields = $this->groups[ $values['type'] ]->get_fields(); |
|
341 | unset( $values['type'] ); |
|
342 | ||
343 | foreach ( $group_fields as $field ) { |
|
344 | // set value from the group |
|
345 | $tmp_field = clone $field; |
|
346 | ||
347 | if ( is_a( $field, __NAMESPACE__ . '\\Complex_Field' ) ) { |
|
348 | $tmp_field->load_values_from_array( $values ); |
|
349 | } else { |
|
350 | $tmp_field->set_value_from_input( $values ); |
|
351 | } |
|
352 | ||
353 | $value_group[] = $tmp_field; |
|
354 | } |
|
355 | ||
356 | $this->values[] = $value_group; |
|
357 | } |
|
358 | } |
|
359 | ||
360 | /** |
|
361 | * Retrieve the field values. |
@@ 309-367 (lines=59) @@ | ||
306 | * |
|
307 | * @param array $group_rows Group rows |
|
308 | */ |
|
309 | public function process_loaded_values( $group_rows ) { |
|
310 | $input_groups = array(); |
|
311 | ||
312 | // Set default values |
|
313 | $field_names = array(); |
|
314 | foreach ( $this->groups as $group ) { |
|
315 | $group_fields = $group->get_fields(); |
|
316 | foreach ( $group_fields as $field ) { |
|
317 | $field_names[] = $field->get_name(); |
|
318 | $field->set_value( $field->get_default_value() ); |
|
319 | } |
|
320 | } |
|
321 | ||
322 | if ( empty( $group_rows ) ) { |
|
323 | return; |
|
324 | } |
|
325 | ||
326 | // load and parse values and group type |
|
327 | foreach ( $group_rows as $row ) { |
|
328 | if ( ! preg_match( Helper::get_complex_field_regex( $this->name, array_keys( $this->groups ), $field_names ), $row['field_key'], $field_name ) ) { |
|
329 | continue; |
|
330 | } |
|
331 | ||
332 | $row['field_value'] = maybe_unserialize( $row['field_value'] ); |
|
333 | $input_groups[ $field_name['index'] ]['type'] = $field_name['group']; |
|
334 | ||
335 | if ( ! empty( $field_name['trailing'] ) ) { |
|
336 | $input_groups[ $field_name['index'] ][ $field_name['key'] . '_' . $field_name['sub'] . '-' . $field_name['trailing'] ] = $row['field_value']; |
|
337 | } else if ( ! empty( $field_name['sub'] ) ) { |
|
338 | $input_groups[ $field_name['index'] ][ $field_name['key'] ][ $field_name['sub'] ] = $row['field_value']; |
|
339 | } else { |
|
340 | $input_groups[ $field_name['index'] ][ $field_name['key'] ] = $row['field_value']; |
|
341 | } |
|
342 | } |
|
343 | ||
344 | // create groups list with loaded fields |
|
345 | ksort( $input_groups ); |
|
346 | ||
347 | foreach ( $input_groups as $index => $values ) { |
|
348 | $value_group = array( 'type' => $values['type'] ); |
|
349 | $group_fields = $this->groups[ $values['type'] ]->get_fields(); |
|
350 | unset( $values['type'] ); |
|
351 | ||
352 | foreach ( $group_fields as $field ) { |
|
353 | // set value from the group |
|
354 | $tmp_field = clone $field; |
|
355 | ||
356 | if ( is_a( $field, __NAMESPACE__ . '\\Complex_Field' ) ) { |
|
357 | $tmp_field->load_values_from_array( $values ); |
|
358 | } else { |
|
359 | $tmp_field->set_value_from_input( $values ); |
|
360 | } |
|
361 | ||
362 | $value_group[] = $tmp_field; |
|
363 | } |
|
364 | ||
365 | $this->values[] = $value_group; |
|
366 | } |
|
367 | } |
|
368 | ||
369 | /** |
|
370 | * Retrieve the field values. |