wpml-settings-helper.class.php
10.1 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
<?php
class WPML_Settings_Helper {
const KEY_CPT_UNLOCK_OPTION = 'custom_posts_unlocked_option';
const KEY_TAXONOMY_UNLOCK_OPTION = 'taxonomies_unlocked_option';
/** @var SitePress */
protected $sitepress;
/** @var WPML_Post_Translation */
protected $post_translation;
/**
* @var WPML_Settings_Filters
*/
private $filters;
/**
* @param WPML_Post_Translation $post_translation
* @param SitePress $sitepress
*/
public function __construct( WPML_Post_Translation $post_translation, SitePress $sitepress ) {
$this->sitepress = $sitepress;
$this->post_translation = $post_translation;
}
/**
* @return WPML_Settings_Filters
*/
private function get_filters() {
if ( ! $this->filters ) {
$this->filters = new WPML_Settings_Filters();
}
return $this->filters;
}
function set_post_type_translatable( $post_type ) {
$this->set_post_type_translate_mode( $post_type, WPML_CONTENT_TYPE_TRANSLATE );
}
function set_post_type_display_as_translated( $post_type ) {
$this->set_post_type_translate_mode( $post_type, WPML_CONTENT_TYPE_DISPLAY_AS_IF_TRANSLATED );
}
function set_post_type_not_translatable( $post_type ) {
$sync_settings = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
if ( isset( $sync_settings[ $post_type ] ) ) {
unset( $sync_settings[ $post_type ] );
}
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'custom_posts_sync_option', $sync_settings, true );
}
private function set_post_type_translate_mode( $post_type, $mode ) {
$sync_settings = $this->sitepress->get_setting( 'custom_posts_sync_option', array() );
$sync_settings[ $post_type ] = $mode;
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'custom_posts_sync_option', $sync_settings, true );
$this->sitepress->verify_post_translations( $post_type );
$this->post_translation->reload();
}
function set_taxonomy_translatable( $taxonomy ) {
$this->set_taxonomy_translatable_mode( $taxonomy, WPML_CONTENT_TYPE_TRANSLATE );
}
function set_taxonomy_display_as_translated( $taxonomy ) {
$this->set_taxonomy_translatable_mode( $taxonomy, WPML_CONTENT_TYPE_DISPLAY_AS_IF_TRANSLATED );
}
function set_taxonomy_translatable_mode( $taxonomy, $mode ) {
$sync_settings = $this->sitepress->get_setting( 'taxonomies_sync_option', array() );
$sync_settings[ $taxonomy ] = $mode;
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'taxonomies_sync_option', $sync_settings, true );
$this->sitepress->verify_taxonomy_translations( $taxonomy );
}
function set_taxonomy_not_translatable( $taxonomy ) {
$sync_settings = $this->sitepress->get_setting( 'taxonomies_sync_option', array() );
if ( isset( $sync_settings[ $taxonomy ] ) ) {
unset( $sync_settings[ $taxonomy ] );
}
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'taxonomies_sync_option', $sync_settings, true );
}
function set_post_type_translation_unlocked_option( $post_type, $unlocked = true ) {
$unlocked_settings = $this->sitepress->get_setting( 'custom_posts_unlocked_option', array() );
$unlocked_settings[ $post_type ] = $unlocked ? 1 : 0;
$this->sitepress->set_setting( 'custom_posts_unlocked_option', $unlocked_settings, true );
}
function set_taxonomy_translation_unlocked_option( $taxonomy, $unlocked = true ) {
$unlocked_settings = $this->sitepress->get_setting( 'taxonomies_unlocked_option', array() );
$unlocked_settings[ $taxonomy ] = $unlocked ? 1 : 0;
$this->sitepress->set_setting( 'taxonomies_unlocked_option', $unlocked_settings, true );
}
/**
* @deprecated use the action `wpml_activate_slug_translation` instead
* or `WPML_ST_Post_Slug_Translation_Settings` instead (on ST side)
*
* @param string $post_type
*/
function activate_slug_translation( $post_type ) {
$slug_settings = $this->sitepress->get_setting( 'posts_slug_translation', array() );
$slug_settings['types'] = isset( $slug_settings['types'] )
? $slug_settings['types'] : array();
$slug_settings['types'][ $post_type ] = 1;
/** @deprected key `on`, use option `wpml_base_slug_translation` instead */
$slug_settings['on'] = 1;
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'posts_slug_translation', $slug_settings, true );
update_option( 'wpml_base_slug_translation', 1 );
}
/**
* @deprecated use `WPML_ST_Post_Slug_Translation_Settings` instead (on ST side)
*
* @param string $post_type
*/
function deactivate_slug_translation( $post_type ) {
$slug_settings = $this->sitepress->get_setting( 'posts_slug_translation', array() );
if ( isset( $slug_settings['types'][ $post_type ] ) ) {
unset( $slug_settings['types'][ $post_type ] );
}
$this->clear_ls_languages_cache();
$this->sitepress->set_setting( 'posts_slug_translation', $slug_settings, true );
}
/**
* @param array[] $taxs_obj_type
*
* @see \WPML_Config::maybe_add_filter
*
* @return array
*/
function _override_get_translatable_taxonomies( $taxs_obj_type ) {
global $wp_taxonomies;
$taxs = $taxs_obj_type['taxs'];
$object_type = $taxs_obj_type['object_type'];
foreach ( $taxs as $k => $tax ) {
if ( ! $this->sitepress->is_translated_taxonomy( $tax ) ) {
unset( $taxs[ $k ] );
}
}
$tm_settings = $this->sitepress->get_setting( 'translation-management', array() );
foreach ( $tm_settings['taxonomies_readonly_config'] as $tx => $translate ) {
if ( $translate
&& ! in_array( $tx, $taxs )
&& isset( $wp_taxonomies[ $tx ] )
&& in_array( $object_type, $wp_taxonomies[ $tx ]->object_type )
) {
$taxs[] = $tx;
}
}
$ret = array(
'taxs' => $taxs,
'object_type' => $taxs_obj_type['object_type'],
);
return $ret;
}
/**
* @param array[] $types
*
* @see \WPML_Config::maybe_add_filter
*
* @return array
*/
function _override_get_translatable_documents( $types ) {
$tm_settings = $this->sitepress->get_setting( 'translation-management', array() );
$cpt_unlocked_options = $this->sitepress->get_setting( 'custom_posts_unlocked_option', array() );
foreach ( $types as $k => $type ) {
if ( isset( $tm_settings['custom-types_readonly_config'][ $k ] )
&& ! $tm_settings['custom-types_readonly_config'][ $k ]
) {
unset( $types[ $k ] );
}
}
$types = $this->get_filters()->get_translatable_documents( $types, $tm_settings['custom-types_readonly_config'], $cpt_unlocked_options );
return $types;
}
/**
* Updates the custom post type translation settings with new settings.
*
* @param array $new_options
*
* @uses \SitePress::get_setting
* @uses \SitePress::save_settings
*
* @return array new custom post type settings after the update
*/
function update_cpt_sync_settings( array $new_options ) {
$cpt_sync_options = $this->sitepress->get_setting( WPML_Element_Sync_Settings_Factory::KEY_POST_SYNC_OPTION, array() );
$cpt_sync_options = array_merge( $cpt_sync_options, $new_options );
$new_options = array_filter( $new_options );
$this->clear_ls_languages_cache();
do_action( 'wpml_verify_post_translations', $new_options );
do_action( 'wpml_save_cpt_sync_settings' );
$this->sitepress->set_setting( WPML_Element_Sync_Settings_Factory::KEY_POST_SYNC_OPTION, $cpt_sync_options, true );
return $cpt_sync_options;
}
/**
* Updates the taxonomy type translation settings with new settings.
*
* @param array $new_options
*
* @uses \SitePress::get_setting
* @uses \SitePress::save_settings
*
* @return array new taxonomy type settings after the update
*/
function update_taxonomy_sync_settings( array $new_options ) {
$taxonomy_sync_options = $this->sitepress->get_setting( WPML_Element_Sync_Settings_Factory::KEY_TAX_SYNC_OPTION, [] );
$changed_sync_options = array_diff_assoc( $new_options, $taxonomy_sync_options );
$taxonomy_sync_options = array_merge( $taxonomy_sync_options, $new_options );
foreach ( $changed_sync_options as $taxonomy_name => $taxonomy_sync_option ) {
$this->sitepress->verify_taxonomy_translations( $taxonomy_name );
}
$this->clear_ls_languages_cache();
do_action( 'wpml_save_taxonomy_sync_settings' );
$this->sitepress->set_setting( WPML_Element_Sync_Settings_Factory::KEY_TAX_SYNC_OPTION, $taxonomy_sync_options, true );
return $taxonomy_sync_options;
}
/**
* Updates the custom post type unlocked settings with new settings.
*
* @param array $unlock_options
*
* @uses \SitePress::get_setting
* @uses \SitePress::save_settings
*
* @return array new custom post type unlocked settings after the update
*/
function update_cpt_unlocked_settings( array $unlock_options ) {
return $this->update_unlocked_settings( $unlock_options, self::KEY_CPT_UNLOCK_OPTION );
}
/**
* Updates the taxonomy type unlocked settings with new settings.
*
* @param array $unlock_options
*
* @uses \SitePress::get_setting
* @uses \SitePress::save_settings
*
* @return array new taxonomy type unlocked settings after the update
*/
function update_taxonomy_unlocked_settings( array $unlock_options ) {
return $this->update_unlocked_settings( $unlock_options, self::KEY_TAXONOMY_UNLOCK_OPTION );
}
/**
* @param array $unlock_options
* @param string $setting_key
*
* @return array
*/
private function update_unlocked_settings( array $unlock_options, $setting_key ) {
$cpt_unlock_options = $this->sitepress->get_setting( $setting_key, array() );
$cpt_unlock_options = array_merge( $cpt_unlock_options, $unlock_options );
$this->sitepress->set_setting( $setting_key, $cpt_unlock_options, true );
return $cpt_unlock_options;
}
/**
* @param string $config_type
*/
function maybe_add_filter( $config_type ) {
if ( $config_type === 'taxonomies' ) {
add_filter(
'get_translatable_taxonomies',
array( $this, '_override_get_translatable_taxonomies' )
);
} elseif ( $config_type === 'custom-types' ) {
add_filter(
'get_translatable_documents',
array( $this, '_override_get_translatable_documents' )
);
}
}
private function clear_ls_languages_cache() {
$cache = new WPML_WP_Cache( 'ls_languages' );
$cache->flush_group_cache();
}
}