ExtraFieldDataInEditor.php
5.42 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
<?php
namespace WPML\TM\Jobs;
use WPML\FP\Obj;
use WPML\FP\Str;
use WPML\FP\Relation;
use WPML\FP\Fns;
use function WPML\FP\pipe;
class ExtraFieldDataInEditor implements \IWPML_Backend_Action {
const MAX_ALLOWED_SINGLE_LINE_LENGTH = 50;
/** @var \WPML_Custom_Field_Editor_Settings */
private $customFieldEditorSettings;
public function __construct( \WPML_Custom_Field_Editor_Settings $customFieldEditorSettings ) {
$this->customFieldEditorSettings = $customFieldEditorSettings;
}
public function add_hooks() {
add_filter( 'wpml_tm_adjust_translation_fields', [ $this, 'appendTitleAndStyle' ], 1, 3 );
}
public function appendTitleAndStyle( array $fields, $job, $originalPost ) {
$appendTitleAndStyleStrategy = $this->isExternalElement( $job ) ?
$this->appendToExternalField( $originalPost ) :
$this->addTitleAndAdjustStyle( $job, $originalPost );
return Fns::map( pipe( $appendTitleAndStyleStrategy, $this->adjustFieldStyleForUnsafeContent() ), $fields );
}
private function addTitleAndAdjustStyle( $job, $originalPost ) {
return function ( $field ) use ( $job, $originalPost ) {
if ( FieldId::is_a_custom_field( $field['field_type'] ) ) {
return $this->appendToCustomField( $field, $job, $originalPost );
} elseif ( FieldId::is_a_term( $field['field_type'] ) ) {
return $this->appendToTerm( $field );
}
return $this->appendToRegularField( $field );
};
}
private function isExternalElement( $job ) {
return isset( $job->element_type_prefix ) && wpml_load_core_tm()->is_external_type( $job->element_type_prefix );
}
private function appendToExternalField( $originalPost ) {
return function ( $field ) use ( $originalPost ) {
$field['title'] = apply_filters( 'wpml_tm_editor_string_name', $field['field_type'], $originalPost );
$field['field_style'] = $this->applyStyleFilter(
Obj::propOr( '', 'field_style', $field ),
$field['field_type'],
$originalPost
);
return $field;
};
}
private function appendToCustomField( $field, $job, $originalPost ) {
$title = $this->getCustomFieldTitle( $field );
$style = $this->getCustomFieldStyle( $field );
$field = (array) apply_filters( 'wpml_editor_cf_to_display', (object) $field, $job );
$field['title'] = $title;
$field['field_style'] = $this->getAdjustedFieldStyle( $field, $style );
$field['field_style'] = $this->applyStyleFilter( $field['field_style'], $field['field_type'], $originalPost );
return $field;
}
private function appendToTerm( $field ) {
$field['title'] = '';
return $field;
}
private function applyStyleFilter( $style, $type, $originalPost ) {
return (string) apply_filters( 'wpml_tm_editor_string_style', $style, $type, $originalPost );
}
private function appendToRegularField( $field ) {
$field['title'] = \wpml_collect(
[
'title' => __( 'Title', 'wpml-translation-management' ),
'body' => __( 'Body', 'wpml-translation-management' ),
'excerpt' => __( 'Excerpt', 'wpml-translation-management' ),
]
)->get( $field['field_type'], $field['field_type'] );
if ( $field['field_type'] === 'excerpt' ) {
$field['field_style'] = '1';
}
return $field;
}
private function getCustomFieldTitle( $field ) {
$unfiltered_type = \WPML_TM_Field_Type_Sanitizer::sanitize( $field['field_type'] );
$element_field_type = $unfiltered_type;
/**
* @deprecated Use `wpml_editor_custom_field_name` filter instead
* @since 3.2
*/
$element_field_type = apply_filters( 'icl_editor_cf_name', $element_field_type );
$element_field_type = apply_filters( 'wpml_editor_custom_field_name', $element_field_type );
return $this->customFieldEditorSettings->filter_name( $unfiltered_type, $element_field_type );
}
private function getCustomFieldStyle( $field ) {
$type = \WPML_TM_Field_Type_Sanitizer::sanitize( $field['field_type'] );
$style = Str::includes( "\n", $field['field_data'] ) ? 1 : 0;
/**
* @deprecated Use `wpml_editor_custom_field_style` filter instead
* @since 3.2
*/
$style = apply_filters( 'icl_editor_cf_style', $style, $type );
$style = apply_filters( 'wpml_editor_custom_field_style', $style, $type );
return $this->customFieldEditorSettings->filter_style( $type, $style );
}
private function getAdjustedFieldStyle( array $field, $style ) {
/**
* wpml_tm_editor_max_allowed_single_line_length filter
*
* Filters the value of `\WPML_Translation_Editor_UI::MAX_ALLOWED_SINGLE_LINE_LENGTH`
*
* @param int $max_allowed_single_line_length MAX_ALLOWED_SINGLE_LINE_LENGTH The length of the string, after which it must use a multiline input
* @param array $field The generic field data
* @param array $custom_field_data The custom field specific data
*
* @since 2.3.1
*/
$maxAllowedLength = (int) apply_filters(
'wpml_tm_editor_max_allowed_single_line_length',
self::MAX_ALLOWED_SINGLE_LINE_LENGTH,
$field,
[ $field['title'], $style, $field ]
);
return 0 === (int) $style && strlen( $field['field_data'] ) > $maxAllowedLength ? '1' : $style;
}
private function adjustFieldStyleForUnsafeContent() {
return function ( array $field ) {
if ( Relation::propEq( 'field_style', '2', $field ) ) {
$black_list = [ 'script', 'style', 'iframe' ];
$black_list_pattern = '#</?(' . implode( '|', $black_list ) . ')[^>]*>#i';
if ( preg_replace( $black_list_pattern, '', $field['field_data'] ) !== $field['field_data'] ) {
$field['field_style'] = '1';
}
}
return $field;
};
}
}