EntryConfigurator.php
1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace ACA\GravityForms\Column;
use AC;
use ACA\GravityForms\Column;
use ACA\GravityForms\FieldFactory;
use ACA\GravityForms\ListScreen;
final class EntryConfigurator implements AC\Registerable {
private $form_id;
private $column_factory;
private $field_factory;
public function __construct( $form_id, EntryFactory $column_factory, FieldFactory $field_factory ) {
$this->form_id = (int) $form_id;
$this->column_factory = $column_factory;
$this->field_factory = $field_factory;
}
public function register() {
add_action( 'ac/list_screen/column_created', [ $this, 'configure_column' ] );
}
public function configure_column( AC\Column $column ): void {
if ( ! $column instanceof Column\Entry ) {
return;
}
$list_screen = $column->get_list_screen();
if ( ! $list_screen instanceof ListScreen\Entry ) {
return;
}
if ( $list_screen->get_form_id() !== $this->form_id ) {
return;
}
$column->set_field( $this->field_factory->create( $this->get_field_id_by_type( $column->get_type() ), $this->form_id ) );
}
private function get_field_id_by_type( $type ) {
return str_replace( 'field_id-', '', $type );
}
public function register_entry_columns( ListScreen\Entry $list_screen ): void {
foreach ( ( new AC\DefaultColumnsRepository() )->get( $list_screen->get_key() ) as $type => $label ) {
$field_id = $this->get_field_id_by_type( $type );
$form_id = $list_screen->get_form_id();
if ( ! $this->column_factory->has_field( $field_id, $form_id ) ) {
continue;
}
$column = $this->column_factory->create( $field_id, $form_id );
$column->set_type( $type )
->set_label( $label )
->set_list_screen( $list_screen );
$this->configure_column( $column );
$list_screen->register_column_type( $column );
}
}
}