PluginProbe ʕ •ᴥ•ʔ
JetFormBuilder — Dynamic Blocks Form Builder / 3.2.3
JetFormBuilder — Dynamic Blocks Form Builder v3.2.3
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 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.1.0 2.1.1 2.1.10 2.1.11 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 3.0.0 3.0.0.1 3.0.0.2 3.0.0.3 3.0.1 3.0.1.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.0.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.3.1 3.3.4 3.3.4.1 3.3.4.2 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.5.1 3.4.5.2 3.4.6 3.4.7 3.4.7.1 3.5.0 3.5.1 3.5.1.1 3.5.1.2 3.5.2 3.5.2.1 3.5.3 3.5.4 3.5.5 3.5.6 3.5.6.1 3.5.6.2 3.5.6.3 3.6.0
jetformbuilder / components / admin / page / traits / table-record-prepare-trait.php
jetformbuilder / components / admin / page / traits Last commit date
action-page-trait.php 2 years ago admin-dashboard-page-trait.php 2 years ago table-advanced-record-prepare-trait.php 2 years ago table-record-prepare-trait.php 2 years ago
table-record-prepare-trait.php
111 lines
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