Manager.php
3.11 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
<?php
namespace WPML\ST\TranslationFile;
use WP_Filesystem_Direct;
use WPML\Collect\Support\Collection;
use WPML\ST\MO\File\makeDir;
use WPML_Language_Records;
use WPML_ST_Translations_File_Dictionary;
use function wpml_collect;
use WPML_ST_Translations_File_Entry;
abstract class Manager {
use makeDir;
const SUB_DIRECTORY = 'wpml';
/** @var StringsRetrieve $strings */
protected $strings;
/** @var WPML_Language_Records $language_records */
protected $language_records;
/** @var Builder $builder */
protected $builder;
/** @var WPML_ST_Translations_File_Dictionary $file_dictionary */
protected $file_dictionary;
/** @var Domains $domains */
protected $domains;
public function __construct(
StringsRetrieve $strings,
Builder $builder,
WP_Filesystem_Direct $filesystem,
WPML_Language_Records $language_records,
Domains $domains
) {
$this->strings = $strings;
$this->builder = $builder;
$this->filesystem = $filesystem;
$this->language_records = $language_records;
$this->domains = $domains;
}
/**
* @param string $domain
* @param string $locale
*/
public function remove( $domain, $locale ) {
$this->filesystem->delete( $this->getFilepath( $domain, $locale ) );
}
/**
* @param string $domain
* @param string $locale
*
* @return bool
*/
public function add( $domain, $locale ) {
if ( ! $this->maybeCreateSubdir() ) {
return false;
}
$lang_code = $this->language_records->get_language_code( $locale );
$strings = $this->strings->get( $domain, $lang_code, $this->isPartialFile() );
if ( ! $strings && $this->isPartialFile() ) {
$this->remove( $domain, $locale );
return false;
}
$file_content = $this->builder
->set_language( $locale )
->get_content( $strings );
$filepath = $this->getFilepath( $domain, $locale );
$chmod = defined( 'FS_CHMOD_FILE' ) ? FS_CHMOD_FILE : 0644;
return $this->filesystem->put_contents( $filepath, $file_content, $chmod );
}
/**
* @param string $domain
* @param string $locale
*
* @return string|null
*/
public function get( $domain, $locale ) {
$filepath = $this->getFilepath( $domain, $locale );
if ( $this->filesystem->is_file( $filepath ) && $this->filesystem->is_readable( $filepath ) ) {
return $filepath;
}
return null;
}
/**
* @param string $domain
* @param string $locale
*
* @return string
*/
public function getFilepath( $domain, $locale ) {
return $this->getSubdir() . '/' . strtolower( $domain ) . '-' . $locale . '.' . $this->getFileExtension();
}
/**
* @param string $domain
*
* @return bool
*/
public function handles( $domain ) {
return $this->getDomains()->contains( $domain );
}
/** @return string */
public static function getSubdir() {
$subdir = WP_LANG_DIR . '/' . self::SUB_DIRECTORY;
$site_id = get_current_blog_id();
if ( $site_id > 1 ) {
$subdir .= '/' . $site_id;
}
return $subdir;
}
/**
* @return string
*/
abstract protected function getFileExtension();
/**
* @return bool
*/
abstract public function isPartialFile();
/**
* @return Collection
*/
abstract protected function getDomains();
}