class-wpml-tm-job-layout.php
4.06 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
use \WPML\TM\Jobs\FieldId;
class WPML_TM_Job_Layout {
private $layout = array();
private $custom_fields = array();
private $grouped_custom_fields = array();
private $terms = array();
private $wp_api;
public $wpdb;
public function __construct( wpdb $wpdb, WPML_WP_API $wp_api ) {
$this->wpdb = $wpdb;
$this->wp_api = $wp_api;
}
public function get_wpdb() {
return $this->wpdb;
}
public function run( array $fields, $tm_instance = null ) {
foreach ( $fields as $field ) {
$this->layout[] = $field['field_type'];
}
$this->order_main_fields();
$this->extract_terms();
$this->extract_custom_fields( $tm_instance );
$this->append_terms();
$this->append_grouped_custom_fields();
$this->append_custom_fields();
return apply_filters( 'wpml_tm_job_layout', array_values( $this->layout ) );
}
private function order_main_fields() {
$ordered_elements = array();
foreach ( array( 'title', 'body', 'excerpt' ) as $type ) {
foreach ( $this->layout as $key => $element ) {
if ( $element === $type ) {
unset( $this->layout[ $key ] );
$ordered_elements[] = $type;
}
}
}
$this->layout = array_merge( $ordered_elements, $this->layout );
}
private function extract_custom_fields( $tm_instance ) {
foreach ( $this->layout as $key => $field ) {
if ( FieldId::is_a_custom_field( $field ) ) {
$group = $this->get_group_custom_field_belongs_to( $field, $tm_instance );
if ( $group ) {
if ( ! isset( $this->grouped_custom_fields[ $group ] ) ) {
$this->grouped_custom_fields[ $group ] = array();
}
$this->grouped_custom_fields[ $group ][] = $field;
} else {
$this->custom_fields[] = $field;
}
unset( $this->layout[ $key ] );
}
}
}
private function get_group_custom_field_belongs_to( $field, $tm_instance ) {
$group = '';
if ( $tm_instance ) {
$settings = new WPML_Custom_Field_Editor_Settings( new WPML_Custom_Field_Setting_Factory( $tm_instance ) );
$group = $settings->get_group( WPML_TM_Field_Type_Sanitizer::sanitize( $field ) );
}
return $group;
}
private function extract_terms() {
foreach ( $this->layout as $key => $field ) {
if ( FieldId::is_any_term_field( $field ) ) {
$this->terms[] = $field;
unset( $this->layout[ $key ] );
}
}
}
private function append_grouped_custom_fields() {
foreach ( $this->grouped_custom_fields as $group => $fields ) {
$data = array(
'field_type' => 'tm-section',
'title' => $group,
'fields' => $fields,
'empty' => false,
'empty_message' => '',
'sub_title' => '',
);
$this->layout[] = $data;
}
}
private function append_custom_fields() {
if ( count( $this->custom_fields ) ) {
$data = array(
'field_type' => 'tm-section',
'title' => __( 'Custom Fields', 'wpml-translation-management' ),
'fields' => $this->custom_fields,
'empty' => false,
'empty_message' => '',
'sub_title' => '',
);
$this->layout[] = $data;
}
}
private function append_terms() {
if ( count( $this->terms ) ) {
$taxonomy_fields = [];
foreach ( $this->terms as $term ) {
$term_id = FieldId::get_term_id( $term );
$query = $this->wpdb->prepare( "SELECT taxonomy FROM {$this->wpdb->term_taxonomy} WHERE term_taxonomy_id = %d", $term_id );
$taxonomy = $this->wpdb->get_var( $query );
if ( ! isset( $taxonomy_fields[ $taxonomy ] ) ) {
$taxonomy_fields[ $taxonomy ] = [];
}
$taxonomy_fields[ $taxonomy ][] = $term;
}
foreach ( $taxonomy_fields as $taxonomy => $fields ) {
$taxonomy = $this->wp_api->get_taxonomy( $taxonomy );
$data = array(
'field_type' => 'tm-section',
'title' => $taxonomy->labels->name,
'fields' => $fields,
'empty' => false,
'empty_message' => '',
'sub_title' => __( 'Changes in these translations will affect terms in general! (Not only for this post)', 'wpml-translation-management' ),
);
$this->layout[] = $data;
}
}
}
}