class-wpml-mo-file-search.php
3.92 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
<?php
class WPML_MO_File_Search {
/**
* @var SitePress
*/
private $sitepress;
/**
* @var array
*/
private $settings;
/**
* @var WP_Filesystem_Direct
*/
private $filesystem;
/**
* @var array
*/
private $locales;
/**
* @param SitePress $sitepress
*/
public function __construct( SitePress $sitepress, WP_Filesystem_Direct $filesystem = null ) {
$this->sitepress = $sitepress;
if ( ! $filesystem ) {
$filesystem = $sitepress->get_wp_api()->get_wp_filesystem_direct();
}
$this->filesystem = $filesystem;
$this->settings = $this->sitepress->get_settings();
$this->locales = $this->sitepress->get_locale_file_names();
}
/**
* @param array $active_languages
*
* @return bool
*/
public function has_mo_file_for_any_language( $active_languages ) {
foreach ( $active_languages as $lang ) {
if ( $this->can_find_mo_file( $lang['code'] ) ) {
return true;
}
}
return false;
}
public function reload_theme_dirs() {
$dirs = $this->find_theme_mo_dirs();
$this->save_mo_dirs( $dirs );
$this->settings['theme_language_folders'] = $dirs;
}
/**
* @param string $lang_code
*
* @return bool
*/
public function can_find_mo_file( $lang_code ) {
if ( ! isset( $this->locales[ $lang_code ] ) ) {
return false;
}
$file_names = $this->locales[ $lang_code ];
if ( isset( $this->settings['theme_language_folders']['parent'] ) ) {
$files[] = $this->settings['theme_language_folders']['parent'] . '/' . $file_names . '.mo';
}
if ( isset( $this->settings['theme_language_folders']['child'] ) ) {
$files[] = $this->settings['theme_language_folders']['child'] . '/' . $file_names . '.mo';
}
$files[] = $this->get_template_path() . '/' . $file_names . '.mo';
foreach ( $files as $file ) {
if ( $this->filesystem->is_readable( $file ) ) {
return true;
}
}
return false;
}
/**
* @return string
*/
protected function get_template_path() {
return TEMPLATEPATH;
}
/**
* @return array
*/
public function find_theme_mo_dirs() {
$parent_theme = get_template_directory();
$child_theme = get_stylesheet_directory();
$languages_folders = null;
if ( $found_folder = $this->determine_mo_folder( $parent_theme ) ) {
$languages_folders['parent'] = $found_folder;
}
if ( $parent_theme != $child_theme && $found_folder = $this->determine_mo_folder( $child_theme ) ) {
$languages_folders['child'] = $found_folder;
}
return $languages_folders;
}
/**
* @param string $folder
* @param int $rec
*
* @return bool
*/
public function determine_mo_folder( $folder, $rec = 0 ) {
$lfn = $this->sitepress->get_locale_file_names();
$files = $this->filesystem->dirlist( $folder, false, false );
foreach ( $files as $file => $data ) {
if ( 0 === strpos( $file, '.' ) ) {
continue;
}
if ( $this->filesystem->is_file( $folder . '/' . $file ) && preg_match( '#\.mo$#i', $file )
&& in_array( preg_replace( '#\.mo$#i', '', $file ), $lfn )
) {
return $folder;
} elseif ( $this->filesystem->is_dir( $folder . '/' . $file ) && $rec < 5 ) {
if ( $f = $this->determine_mo_folder( $folder . '/' . $file, $rec + 1 ) ) {
return $f;
};
}
}
return false;
}
/**
* @return array
*/
public function get_dir_names() {
$dirs = array();
if ( isset( $this->settings['theme_language_folders']['parent'] ) ) {
$dirs[] = $this->settings['theme_language_folders']['parent'];
}
if ( isset( $this->settings['theme_language_folders']['child'] ) ) {
$dirs[] = $this->settings['theme_language_folders']['child'];
}
if ( empty( $dirs ) ) {
$template = get_option( 'template' );
$dirs[] = get_theme_root( $template ) . '/' . $template;
}
return $dirs;
}
/**
* @param array $dirs
*/
public function save_mo_dirs( $dirs ) {
$sitepress_settings = $this->sitepress->get_settings();
$sitepress_settings['theme_language_folders'] = $dirs;
$this->sitepress->save_settings( $sitepress_settings );
}
}