class-wpml-flags.php
4.96 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
<?php
use WPML\FP\Obj;
use WPML\TM\Settings\Flags\Options;
/**
* Class WPML_Flags
*
* @package wpml-core
*/
class WPML_Flags {
/** @var icl_cache */
private $cache;
/** @var wpdb $wpdb */
private $wpdb;
/** @var WP_Filesystem_Direct */
private $filesystem;
/**
* @param wpdb $wpdb
* @param icl_cache $cache
* @param WP_Filesystem_Direct $filesystem
*/
public function __construct( $wpdb, icl_cache $cache, WP_Filesystem_Direct $filesystem ) {
$this->wpdb = $wpdb;
$this->cache = $cache;
$this->filesystem = $filesystem;
}
/**
* @param string $lang_code
*
* @return \stdClass|null
*/
public function get_flag( $lang_code ) {
$flag = $this->cache->get( $lang_code );
if ( ! $flag ) {
$flag = $this->wpdb->get_row(
$this->wpdb->prepare(
"SELECT flag, from_template
FROM {$this->wpdb->prefix}icl_flags
WHERE lang_code=%s",
$lang_code
)
);
$this->cache->set( $lang_code, $flag );
}
return $flag;
}
/**
* @param string $lang_code
*
* @return string
*/
public function get_flag_url( $lang_code ) {
$flag = $this->get_flag( $lang_code );
if ( ! $flag ) {
return '';
}
$path = '';
if ( $flag->from_template ) {
$wp_upload_dir = wp_upload_dir();
$base_path = $wp_upload_dir['basedir'] . '/';
$base_url = $wp_upload_dir['baseurl'];
$path = 'flags/';
} else {
$base_path = self::get_wpml_flags_directory();
$base_url = self::get_wpml_flags_url();
}
$path .= $flag->flag;
if ( $this->flag_file_exists( $base_path . $path ) ) {
return $this->append_path_to_url( $base_url, $path );
}
return '';
}
/**
* @param string $lang_code
* @param int[] $size An array describing [ $width, $height ]. It defaults to [18, 12].
* @param string $fallback_text
* @param string[] $css_classes Array of CSS class strings.
*
* @return string
*/
public function get_flag_image( $lang_code, $size = [], $fallback_text = '', $css_classes = [] ) {
$url = $this->get_flag_url( $lang_code );
if ( ! $url ) {
return $fallback_text;
}
$class_attribute = is_array( $css_classes ) && ! empty( $css_classes )
? ' class="' . implode( ' ', $css_classes ) . '"'
: '';
return '<img' . $class_attribute . '
width="' . Obj::propOr( 18, 0, $size ) . '"
height="' . Obj::propOr( 12, 1, $size ) . '"
src="' . esc_url( $url ) . '"
alt="' . esc_attr( sprintf( __( 'Flag for %s', 'sitepress' ), $lang_code ) ) . '"
/>';
}
public function clear() {
$this->cache->clear();
}
/**
* @param array $allowed_file_types
*
* @return string[]
*/
public function get_wpml_flags( $allowed_file_types = null ) {
if ( null === $allowed_file_types ) {
$allowed_file_types = array( 'gif', 'jpeg', 'png', 'svg' );
}
$files = $this->filesystem->dirlist( $this->get_wpml_flags_directory(), false );
if ( ! $files ) {
return [];
}
$files = array_keys( $files );
$result = $this->filter_flag_files( $allowed_file_types, $files );
sort( $result );
return $result;
}
/**
* @return string
*/
final public function get_wpml_flags_directory() {
return WPML_PLUGIN_PATH . '/res/flags/';
}
/**
* @return string
*/
final public static function get_wpml_flags_url() {
return ICL_PLUGIN_URL . '/res/flags/';
}
/**
* @return string
*/
final public static function get_wpml_flags_by_locales_url() {
return ICL_PLUGIN_URL . '/res/flags_by_locales.json';
}
/**
* @return string
*/
final public static function get_wpml_flag_image_ext() {
return Options::getFormat();
}
/**
* @param string $path
*
* @return bool
*/
private function flag_file_exists( $path ) {
return $this->filesystem->exists( $path );
}
/**
* @param array $allowed_file_types
* @param array $files
*
* @return array
*/
private function filter_flag_files( $allowed_file_types, $files ) {
$result = array();
foreach ( $files as $file ) {
$path = $this->get_wpml_flags_directory() . $file;
if ( $this->flag_file_exists( $path ) ) {
$ext = pathinfo( $path, PATHINFO_EXTENSION );
if ( in_array( $ext, $allowed_file_types, true ) ) {
$result[] = $file;
}
}
}
return $result;
}
/**
* @param string $base_url
* @param string $path
*
* @return string
*/
private function append_path_to_url( $base_url, $path ) {
$base_url_parts = wp_parse_url( $base_url );
$base_url_path_components = array();
if ( $base_url_parts && array_key_exists( 'path', $base_url_parts ) ) {
$base_url_path_components = explode( '/', untrailingslashit( $base_url_parts['path'] ) );
}
$sub_dir_path_components = explode( '/', trim( $path, '/' ) );
foreach ( $sub_dir_path_components as $sub_dir_path_part ) {
$base_url_path_components[] = $sub_dir_path_part;
}
$base_url_parts['path'] = implode( '/', $base_url_path_components );
return http_build_url( $base_url_parts );
}
}