class-wpml-multilingual-options.php
5.28 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
<?php
/**
* Class WPML_Multilingual_Options
*/
class WPML_Multilingual_Options {
private $array_helper;
private $registered_options = array();
private $sitepress;
private $utils;
/**
* WPML_Multilingual_Options constructor.
*
* @param SitePress $sitepress
* @param WPML_Multilingual_Options_Array_Helper $array_helper
* @param WPML_Multilingual_Options_Utils $utils
*/
public function __construct( SitePress $sitepress, WPML_Multilingual_Options_Array_Helper $array_helper, WPML_Multilingual_Options_Utils $utils ) {
$this->sitepress = $sitepress;
$this->array_helper = $array_helper;
$this->utils = $utils;
}
/**
* @param string $new_code New WPML default language code
* @param string $previous_default Previous WPML default language code
*/
public function default_language_changed_action( $new_code, $previous_default ) {
if ( $new_code !== $previous_default ) {
foreach ( $this->registered_options as $option_name ) {
$default_options = $this->utils->get_option_without_filtering( $option_name, null );
$translated_option = get_option( "{$option_name}_{$new_code}", null );
if ( $translated_option ) {
$new_value = $this->merge( $default_options, $translated_option );
remove_filter( "pre_option_{$option_name}", array( $this, 'pre_option_filter' ), 10 );
remove_filter(
"pre_update_option_{$option_name}",
array(
$this,
'pre_update_option_filter',
),
10
);
update_option( $option_name, $new_value );
delete_option( "{$option_name}_{$new_code}" );
$new_translated_option = $default_options;
if ( is_array( $default_options ) && is_array( $new_value ) ) {
$new_translated_option = $this->array_helper->array_diff_recursive( $default_options, $new_value );
}
update_option( "{$option_name}_{$previous_default}", $new_translated_option, 'no' );
$this->multilingual_options_action( $option_name );
}
$this->invalidate_cache( $option_name, $previous_default );
$this->invalidate_cache( $option_name, $new_code );
}
}
}
/**
* @param string $option_name
*/
public function multilingual_options_action( $option_name ) {
if ( ! in_array( $option_name, $this->registered_options, true ) ) {
$this->registered_options[] = $option_name;
$current_language = $this->sitepress->get_current_language();
$default_language = $this->sitepress->get_default_language();
if ( $current_language !== $default_language ) {
add_filter( "pre_option_{$option_name}", array( $this, 'pre_option_filter' ), 10, 2 );
add_filter( "pre_update_option_{$option_name}", array( $this, 'pre_update_option_filter' ), 10, 3 );
}
}
}
public function init_hooks() {
add_action( 'wpml_multilingual_options', array( $this, 'multilingual_options_action' ) );
add_action( 'icl_after_set_default_language', array( $this, 'default_language_changed_action' ), 10, 2 );
}
/**
* @param mixed $value
* @param string $option_name
*
* @return mixed
*/
public function pre_option_filter( $value, $option_name ) {
$current_language = $this->sitepress->get_current_language();
$cache_found = null;
$options_filtered = wp_cache_get( "{$option_name}_{$current_language}_filtered", 'options', false, $cache_found );
if ( $cache_found ) {
return $options_filtered;
}
$default_options = $this->utils->get_option_without_filtering( $option_name, null );
$translated_value = get_option( "{$option_name}_{$current_language}", $value );
$value = $this->merge( $default_options, $translated_value );
$this->update_cache( $option_name, $current_language, $value );
return $value;
}
/**
* @param string $option_name
* @param string $language
* @param mixed $value
*
* @return bool
*/
private function update_cache( $option_name, $language, $value ) {
return wp_cache_set( "{$option_name}_{$language}_filtered", $value, 'options' );
}
/**
* @param array<mixed>|mixed $new_value
* @param array<mixed>|mixed $old_value
* @param string $option_name
*
* @return array
*/
public function pre_update_option_filter( $new_value, $old_value, $option_name ) {
$current_language = $this->sitepress->get_current_language();
$default_options = $this->utils->get_option_without_filtering( $option_name, null );
$translated_option = null;
if ( is_array( $new_value ) && is_array( $default_options ) ) {
$translated_option = $this->array_helper->array_diff_recursive( $new_value, $default_options );
} elseif ( $new_value !== $default_options ) {
$translated_option = $new_value;
}
update_option( "{$option_name}_{$current_language}", $translated_option, 'no' );
$this->invalidate_cache( $option_name, $current_language );
return $default_options;
}
/**
* @param string $option_name
* @param string $language
*
* @return bool
*/
private function invalidate_cache( $option_name, $language ) {
return wp_cache_delete( "{$option_name}_{$language}_filtered", 'options' );
}
/**
* @param array $target
* @param array $source
*
* @return array
*/
private function merge( $target, $source ) {
$value = $source;
if ( is_array( $source ) && is_array( $target ) ) {
$value = $this->array_helper->recursive_merge( $target, $source );
}
return $value;
}
}