class-wpml-tm-mcs-custom-field-settings-menu.php
9.46 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
use WPML\TM\Menu\McSetup\CfMetaBoxOption;
abstract class WPML_TM_MCS_Custom_Field_Settings_Menu {
/** @var WPML_Custom_Field_Setting_Factory $settings_factory */
protected $settings_factory;
/** @var WPML_UI_Unlock_Button $unlock_button_ui */
private $unlock_button_ui;
/** @var WPML_Custom_Field_Setting_Query_Factory $query_factory */
private $query_factory;
/** @var WPML_Custom_Field_Setting_Query $query */
private $query;
/** @var string[] Custom field keys */
private $custom_fields_keys;
/** @var int $total_keys */
private $total_keys;
/** @var array Custom field options */
private $custom_field_options;
/** @var int Initial setting of items per page */
const ITEMS_PER_PAGE = 20;
public function __construct(
WPML_Custom_Field_Setting_Factory $settings_factory,
WPML_UI_Unlock_Button $unlock_button_ui,
WPML_Custom_Field_Setting_Query_Factory $query_factory
) {
$this->settings_factory = $settings_factory;
$this->unlock_button_ui = $unlock_button_ui;
$this->query_factory = $query_factory;
$this->custom_field_options = array(
WPML_IGNORE_CUSTOM_FIELD => __( "Don't translate", 'wpml-translation-management' ),
WPML_COPY_CUSTOM_FIELD => __( 'Copy from original to translation', 'wpml-translation-management' ),
WPML_COPY_ONCE_CUSTOM_FIELD => __( 'Copy once', 'wpml-translation-management' ),
WPML_TRANSLATE_CUSTOM_FIELD => __( 'Translate', 'wpml-translation-management' ),
);
}
/**
* This will fetch the data from DB
* depending on the user inputs (pagination/search)
*
* @param array $args
*/
public function init_data( array $args = array() ) {
if ( null === $this->custom_fields_keys ) {
$args = array_merge(
array(
'hide_system_fields' => ! $this->settings_factory->show_system_fields,
'items_per_page' => self::ITEMS_PER_PAGE,
'page' => 1,
),
$args
);
$this->custom_fields_keys = $this->get_query()->get( $args );
$this->total_keys = $this->get_query()->get_total_rows();
if ( $this->custom_fields_keys ) {
natcasesort( $this->custom_fields_keys );
}
}
}
/**
* @return string
*/
public function render() {
ob_start();
?>
<div class="wpml-section wpml-section-<?php echo esc_attr( $this->kind_shorthand() ); ?>-translation"
id="ml-content-setup-sec-<?php echo esc_attr( $this->kind_shorthand() ); ?>">
<div class="wpml-section-header">
<h3><?php echo esc_html( $this->get_title() ); ?></h3>
<p>
<?php
// We need htmlspecialchars() here only for testing, as DOMDocument::loadHTML() cannot parse url with '&'.
$toggle_system_fields = array(
'url' => htmlspecialchars(
add_query_arg(
array(
'show_system_fields' => ! $this->settings_factory->show_system_fields,
),
admin_url( 'admin.php?page=' . WPML_TM_FOLDER . WPML_Translation_Management::PAGE_SLUG_SETTINGS ) . '#ml-content-setup-sec-' . $this->kind_shorthand()
)
),
'text' => $this->settings_factory->show_system_fields ?
__( 'Hide system fields', 'wpml-translation-management' ) :
__( 'Show system fields', 'wpml-translation-management' ),
);
?>
<a href="<?php echo esc_url( $toggle_system_fields['url'] ); ?>"><?php echo esc_html( $toggle_system_fields['text'] ); ?></a>
</p>
</div>
<div class="wpml-section-content wpml-section-content-wide">
<form
id="icl_<?php echo esc_attr( $this->kind_shorthand() ); ?>_translation"
data-type="<?php echo esc_attr( $this->kind_shorthand() ); ?>"
name="icl_<?php echo esc_attr( $this->kind_shorthand() ); ?>_translation"
class="wpml-custom-fields-settings" action="">
<?php wp_nonce_field( 'icl_' . $this->kind_shorthand() . '_translation_nonce', '_icl_nonce' ); ?>
<?php
if ( empty( $this->custom_fields_keys ) ) {
?>
<p class="no-data-found">
<?php echo esc_html( $this->get_no_data_message() ); ?>
</p>
<?php
} else {
if ( esc_attr( $this->kind_shorthand() ) === 'cf' ) {
?>
<label><input type="checkbox" <?php if ( CfMetaBoxOption::get() ) : ?> checked="checked"
<?php endif; ?>id="show_cf_meta_box" name="translate_media"
value="1"/> <?php esc_html_e( 'Show "Multilingual Content Setup" meta box on post edit screen.', 'sitepress' ); ?>
</label>
<?php
}
?>
<div class="wpml-flex-table wpml-translation-setup-table wpml-margin-top-sm">
<?php echo $this->render_heading(); ?>
<div class="wpml-flex-table-body">
<?php
$this->render_body();
?>
</div>
</div>
<?php
$this->render_pagination( self::ITEMS_PER_PAGE, 1 );
?>
<p class="buttons-wrap">
<span class="icl_ajx_response"
id="icl_ajx_response_<?php echo esc_attr( $this->kind_shorthand() ); ?>"></span>
<input type="submit" class="button-primary"
value="<?php echo esc_attr__( 'Save', 'wpml-translation-management' ); ?>"/>
</p>
<?php
}
?>
</form>
</div>
<!-- .wpml-section-content -->
</div> <!-- .wpml-section -->
<?php
return ob_get_clean();
}
/**
* @return string
*/
abstract protected function kind_shorthand();
/**
* @return string
*/
abstract protected function get_title();
abstract protected function get_meta_type();
/**
* @param string $key
*
* @return WPML_Custom_Field_Setting
*/
abstract protected function get_setting( $key );
private function render_radio( $cf_key, $html_disabled, $status, $ref_status ) {
ob_start();
?>
<input type="radio" name="<?php echo $this->get_radio_name( $cf_key ); ?>"
value="<?php echo esc_attr( $ref_status ); ?>"
title="<?php echo esc_attr( $ref_status ); ?>" <?php echo $html_disabled; ?>
<?php
if ( $status == $ref_status ) :
?>
checked<?php endif; ?> />
<?php
return ob_get_clean();
}
private function get_radio_name( $cf_key ) {
return 'cf[' . esc_attr( base64_encode( $cf_key ) ) . ']';
}
private function get_unlock_name( $cf_key ) {
return 'cf_unlocked[' . esc_attr( base64_encode( $cf_key ) ) . ']';
}
/**
* @return string header and footer of the setting table
*/
private function render_heading() {
ob_start();
?>
<div class="wpml-flex-table-header wpml-flex-table-sticky">
<?php $this->render_search(); ?>
<div class="wpml-flex-table-row">
<div class="wpml-flex-table-cell name">
<?php echo esc_html( $this->get_column_header( 'name' ) ); ?>
</div>
<div class="wpml-flex-table-cell text-center">
<?php echo esc_html__( "Don't translate", 'wpml-translation-management' ); ?>
</div>
<div class="wpml-flex-table-cell text-center">
<?php echo esc_html_x( 'Copy', 'Verb', 'wpml-translation-management' ); ?>
</div>
<div class="wpml-flex-table-cell text-center">
<?php echo esc_html__( 'Copy once', 'wpml-translation-management' ); ?>
</div>
<div class="wpml-flex-table-cell text-center">
<?php echo esc_html__( 'Translate', 'wpml-translation-management' ); ?>
</div>
</div>
</div>
<?php
return ob_get_clean();
}
/**
* Render search box for Custom Field Settings.
*
* @param string $search_string Search String.
*/
public function render_search( $search_string = '' ) {
$search = new WPML_TM_MCS_Search_Factory();
echo $search->create( $search_string )->render();
}
/**
* Render body of Custom Field Settings.
*/
public function render_body() {
foreach ( $this->custom_fields_keys as $cf_key ) {
$setting = $this->get_setting( $cf_key );
$status = $setting->status();
$html_disabled = $setting->get_html_disabled();
?>
<div class="wpml-flex-table-row">
<div class="wpml-flex-table-cell name">
<?php
$override = false;
/**
* This filter hook give the ability to override the
* default custom field lock rendering.
*
* @since 4.6.0
*
* @param bool $override
* @param WPML_Custom_Field_Setting $setting
*/
if ( ! apply_filters( 'wpml_custom_field_settings_override_lock_render', $override, $setting ) ) {
$this->unlock_button_ui->render( $setting->is_read_only(), $setting->is_unlocked(), $this->get_radio_name( $cf_key ), $this->get_unlock_name( $cf_key ) );
}
echo esc_html( $cf_key );
?>
</div>
<?php
foreach ( $this->custom_field_options as $ref_status => $title ) {
?>
<div class="wpml-flex-table-cell text-center">
<?php
echo $this->render_radio( $cf_key, $html_disabled, $status, $ref_status );
?>
</div>
<?php
}
?>
</div>
<?php
}
}
/**
* Render pagination for Custom Field Settings.
*
* @param int $items_per_page Items per page to display.
* @param int $current_page Which page to display.
*/
public function render_pagination( $items_per_page, $current_page ) {
$pagination = new WPML_TM_MCS_Pagination_Render_Factory( $items_per_page );
echo $pagination->create( $this->total_keys, $current_page )->render();
}
abstract public function get_no_data_message();
abstract public function get_column_header( $id );
/**
* @return WPML_Custom_Field_Setting_Query
*/
private function get_query() {
if ( null === $this->query ) {
$this->query = $this->query_factory->create( $this->get_meta_type() );
}
return $this->query;
}
}