PluginProbe
JetFormBuilder — Dynamic Blocks Form Builder / 3.6.6
JetFormBuilder — Dynamic Blocks Form Builder v3.6.6
3.6.6.1 3.6.6 3.6.5.4 3.6.5.3 3.6.5.2 3.6.5.1 3.6.5 3.6.4.2 3.6.4.1 3.6.4 3.6.3.1 3.6.3 3.6.2.2 3.6.2.1 3.6.2 3.6.1.1 3.6.1 3.6.0.1 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 All 134 releases
jetformbuilder / components / admin / page / traits / table-record-prepare-trait.php

table-record-prepare-trait.php in JetFormBuilder — Dynamic Blocks Form Builder 3.6.6, at components/admin/page/traits/table-record-prepare-trait.php

111 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3
4 namespace JFB_Components\Admin\Page\Traits;
5
6 // If this file is called directly, abort.
7 if ( ! defined( 'WPINC' ) ) {
8 die;
9 }
10
11 trait Table_Record_Prepare_Trait {
12
13 abstract public function get_columns_handlers(): array;
14
15 abstract public function get_columns_headings(): array;
16
17 abstract public function get_raw_list( array $args ): array;
18
19 abstract public function get_list(): array;
20
21 public function prepare_list( array $custom_list = array() ): array {
22 $list = empty( $custom_list ) ? $this->get_list() : $custom_list;
23
24 if ( ! $list || ! is_array( $list ) ) {
25 return array();
26 }
27 $prepared = array();
28
29 foreach ( $list as $item_id => $record ) {
30 $prepared[ $item_id ] = $this->prepare_record( $record );
31 }
32
33 return $prepared;
34 }
35
36 public function before_prepare_record( array $record, $column_attrs, $column_name ) {
37 }
38
39 public function after_prepare_record( $prepared, array $record, $column_name ) {
40 }
41
42 public function prepare_record( $record ): array {
43 $prepared = array();
44
45 foreach ( $this->get_columns_handlers() as $column_name => $column_attrs ) {
46 $this->before_prepare_record( $record, $column_attrs, $column_name );
47
48 $prepared[ $column_name ] = $this->prepare_column_attrs( $record, $column_attrs, $column_name );
49
50 $this->after_prepare_record( $prepared[ $column_name ], $record, $column_name );
51 }
52
53 return $prepared;
54 }
55
56 public function prepare_column_attrs( array $record, $column_attrs, $column_name ): array {
57 $value = $column_attrs['value'] ?? $record[ $column_name ] ?? false;
58 $column_attrs['type'] = $column_attrs['type'] ?? 'string';
59 $column_attrs['default'] = $column_attrs['default'] ?? false;
60
61 $default = $this->prepare_record_value_type( $column_attrs['default'], $column_attrs );
62
63 if ( ! is_callable( $value ) ) {
64 $column_attrs['value'] = $value ?: $default;
65 } else {
66 $column_attrs['value'] = $this->prepare_record_value_type(
67 call_user_func( $value, $record, $default ),
68 $column_attrs
69 );
70 }
71
72 return $column_attrs;
73 }
74
75 public function transform_to_columns_values( array $columns ): array {
76 $transformed = array();
77
78 foreach ( $this->get_columns_handlers() as $column_name => $column_attrs ) {
79 if ( ! isset( $columns[ $column_name ] ) ) {
80 continue;
81 }
82 $column_attrs['type'] = $column_attrs['type'] ?? 'string';
83 $column_attrs['default'] = $column_attrs['default'] ?? false;
84 $column_attrs['value'] = $columns[ $column_name ];
85
86 $transformed[ $column_name ] = $column_attrs;
87 }
88
89 return $transformed;
90 }
91
92 protected function prepare_record_value_type( $value, $column ) {
93 $type = $column['type'] ?? 'string';
94
95 switch ( $type ) {
96 case 'integer':
97 return (int) $value;
98 case 'float':
99 return (float) $value;
100 case 'array':
101 return (array) $value;
102 case 'string':
103 return (string) $value;
104 case 'rawArray':
105 default:
106 return $value;
107 }
108 }
109
110 }
111