class-wpml-custom-field-setting.php
7.13 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
use WPML\FP\Logic;
abstract class WPML_Custom_Field_Setting extends WPML_TM_User {
/** @var string $index */
private $index;
/**
* WPML_Custom_Field_Setting constructor.
*
* @param TranslationManagement $tm_instance
* @param string $index
*/
public function __construct( &$tm_instance, $index ) {
parent::__construct( $tm_instance );
$this->index = $index;
}
/**
* @return string
*/
public function get_index() {
return $this->index;
}
/**
* @return bool true if the custom field setting is given by a setting in
* a wpml-config.xml
*/
public function is_read_only() {
return in_array(
$this->index,
$this->tm_instance->settings[ $this->get_array_setting_index( 'readonly_config' ) ],
true
);
}
/**
* @return bool
*/
public function is_unlocked() {
return isset( $this->tm_instance->settings[ $this->get_unlocked_setting_index() ][ $this->index ] ) &&
(bool) $this->tm_instance->settings[ $this->get_unlocked_setting_index() ][ $this->index ];
}
/**
* @return bool
*/
public function excluded() {
return in_array( $this->index, $this->get_excluded_keys() ) ||
( $this->is_read_only() &&
$this->status() === WPML_IGNORE_CUSTOM_FIELD &&
! $this->is_unlocked()
);
}
public function status() {
$state_index = $this->get_state_array_setting_index();
if ( ! isset( $this->tm_instance->settings[ $state_index ][ $this->index ] ) ) {
$this->tm_instance->settings[ $state_index ][ $this->index ] = WPML_IGNORE_CUSTOM_FIELD;
}
return (int) $this->tm_instance->settings[ $state_index ][ $this->index ];
}
public function make_read_only() {
$ro_index = $this->get_array_setting_index( 'readonly_config' );
$this->tm_instance->settings[ $ro_index ][] = $this->index;
$this->tm_instance->settings[ $ro_index ] = array_unique( $this->tm_instance->settings[ $ro_index ] );
}
public function set_to_copy() {
$this->set_state( WPML_COPY_CUSTOM_FIELD );
}
public function set_to_copy_once() {
$this->set_state( WPML_COPY_ONCE_CUSTOM_FIELD );
}
public function set_to_translatable() {
$this->set_state( WPML_TRANSLATE_CUSTOM_FIELD );
}
public function set_to_nothing() {
$this->set_state( WPML_IGNORE_CUSTOM_FIELD );
}
public function set_editor_style( $style ) {
$this->tm_instance->settings[ $this->get_array_setting_index( 'editor_style' ) ][ $this->index ] = $style;
}
public function get_editor_style() {
$setting = $this->get_array_setting_index( 'editor_style' );
return isset( $this->tm_instance->settings[ $setting ][ $this->index ] ) ? $this->tm_instance->settings[ $setting ][ $this->index ] : '';
}
public function set_editor_label( $label ) {
$this->tm_instance->settings[ $this->get_array_setting_index( 'editor_label' ) ][ $this->index ] = $label;
}
public function get_editor_label() {
$setting = $this->get_array_setting_index( 'editor_label' );
return isset( $this->tm_instance->settings[ $setting ][ $this->index ] ) ? $this->tm_instance->settings[ $setting ][ $this->index ] : '';
}
public function set_editor_group( $group ) {
$this->tm_instance->settings[ $this->get_array_setting_index( 'editor_group' ) ][ $this->index ] = $group;
}
public function get_editor_group() {
$setting = $this->get_array_setting_index( 'editor_group' );
return isset( $this->tm_instance->settings[ $setting ][ $this->index ] ) ? $this->tm_instance->settings[ $setting ][ $this->index ] : '';
}
public function set_translate_link_target( $state, $sub_fields ) {
if ( isset( $sub_fields['value'] ) ) {
// it's a single sub field
$sub_fields = array( $sub_fields );
}
$this->tm_instance->settings[ $this->get_array_setting_index( 'translate_link_target' ) ][ $this->index ] = array(
'state' => $state,
'sub_fields' => $sub_fields,
);
}
public function is_translate_link_target() {
$array_index = $this->get_array_setting_index( 'translate_link_target' );
return isset( $this->tm_instance->settings[ $array_index ][ $this->index ] ) ?
( $this->tm_instance->settings[ $array_index ][ $this->index ]['state'] ||
$this->get_translate_link_target_sub_fields() ) :
false;
}
public function get_translate_link_target_sub_fields() {
$array_index = $this->get_array_setting_index( 'translate_link_target' );
return isset( $this->tm_instance->settings[ $array_index ][ $this->index ]['sub_fields'] ) ?
$this->tm_instance->settings[ $array_index ][ $this->index ]['sub_fields'] :
array();
}
public function set_convert_to_sticky( $state ) {
$this->tm_instance->settings[ $this->get_array_setting_index( 'convert_to_sticky' ) ][ $this->index ] = $state;
}
public function is_convert_to_sticky() {
$array_index = $this->get_array_setting_index( 'convert_to_sticky' );
return isset( $this->tm_instance->settings[ $array_index ][ $this->index ] ) ?
$this->tm_instance->settings[ $array_index ][ $this->index ] :
false;
}
public function set_encoding( $encoding ) {
if ( Logic::isNotNull( $encoding ) ) {
$this->tm_instance->settings[ $this->get_array_setting_index( 'encoding' ) ][ $this->index ] = $encoding;
} else {
unset( $this->tm_instance->settings[ $this->get_array_setting_index( 'encoding' ) ][ $this->index ] );
}
}
public function get_encoding() {
$setting = $this->get_array_setting_index( 'encoding' );
return isset( $this->tm_instance->settings[ $setting ][ $this->index ] ) ? $this->tm_instance->settings[ $setting ][ $this->index ] : '';
}
/**
* @param array $whitelist
*/
public function set_attributes_whitelist( $whitelist ) {
if ( ! is_array( $whitelist ) ) {
throw new InvalidArgumentException( '$whitelist should be an array.' );
}
$this->tm_instance->settings[ $this->get_array_setting_index( 'attributes_whitelist' ) ][ $this->index ] = $whitelist;
}
public function get_attributes_whitelist() {
$setting = $this->get_array_setting_index( 'attributes_whitelist' );
return isset( $this->tm_instance->settings[ $setting ][ $this->index ] ) ? $this->tm_instance->settings[ $setting ][ $this->index ] : array();
}
private function set_state( $state ) {
$this->tm_instance->settings[ $this->get_state_array_setting_index() ][ $this->index ] = $state;
}
/**
* @return string
*/
private function get_array_setting_index( $index ) {
return $this->get_setting_prefix() . $index;
}
/**
* @return string
*/
abstract protected function get_state_array_setting_index();
abstract protected function get_unlocked_setting_index();
/**
* @return string[]
*/
abstract protected function get_excluded_keys();
/**
* @return string
*/
abstract protected function get_setting_prefix();
/**
* @return string
*/
public function get_html_disabled() {
$isDisabled = $this->is_read_only() && ! $this->is_unlocked();
/**
* This filter hook give the ability to disable the HTML radio buttons
* for the custom field preference.
*
* @since 4.6.0
*
* @param bool $isDisabled
* @param WPML_Custom_Field_Setting $instance
*/
return apply_filters( 'wpml_custom_field_setting_is_html_disabled', $isDisabled, $this )
? 'disabled="disabled"'
: '';
}
}