-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathforce-update-translations.php
executable file
·163 lines (143 loc) · 4.36 KB
/
force-update-translations.php
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
<?php
/**
* Plugin Name: Force Update Translations
* Description: Download WordPress theme/plugin translations and apply them to your site manually even if their language pack haven't been released or reviewed on translate.wordpress.org
* Author: Mayo Moriyama & Contributors
* Author URI: https://github.com/mayukojpn/force-update-translations/graphs/contributors
* Version: 0.5
*/
class Force_Update_Translations {
public $admin_notices = array();
/**
* Constructor.
*/
public function __construct() {
include 'lib/glotpress/locales.php';
include 'inc/plugins.php';
include 'inc/themes.php';
}
/**
* Get translation files.
*
* @param array $projects Array of translation projects.
*
* @return void
*/
public function get_files( $projects ) {
foreach ( $projects as $key => $project ) {
foreach ( array( 'po', 'mo' ) as $format ) {
$file = $this->get_file( $project, get_user_locale(), $format );
if ( is_wp_error( $file ) ) {
$this->admin_notices[ $key ][] = array(
'status' => 'error',
'content' => $file->get_error_message(),
);
}
} // endforeach;
if ( empty( $this->admin_notices[ $key ] ) ) {
$this->admin_notices[ $key ][] = array(
'status' => 'success',
'content' => sprintf(
/* translators: %s: Translation file. */
__( 'Translation files have been downloaded: %s', 'force-update-translations' ),
'<b>' . esc_html( $project['sub_project']['name'] ) . '</b>'
),
);
}
}
// Show admin notices of the projects translation update.
self::admin_notices();
}
/**
* Get translation source file.
*
* @param array $project File project.
* @param string $locale File locale.
* @param string $format File format.
*
* @return null|WP_Error File path to get source..
*/
public function get_file( $project, $locale = '', $format = 'mo' ) {
if ( empty( $locale ) ) {
$locale = get_user_locale();
}
switch ( $project['type'] ) {
case 'plugin':
$target_path = 'plugins/' . $project['sub_project']['slug'];
$project_path = 'wp-' . $target_path . '/dev';
break;
case 'theme':
$target_path = 'themes/' . $project['sub_project']['slug'];
$project_path = 'wp-' . $target_path;
break;
}
$source = $this->get_source_path( $project_path, $locale, $format );
$target = sprintf(
'%s-%s.%s',
$target_path,
$locale,
$format
);
$response = wp_remote_get( $source );
if ( ! is_array( $response )
|| $response['headers']['content-type'] !== 'application/octet-stream' ) {
return new WP_Error(
'fdt-source-not-found',
sprintf(
/* translators: %s: Translation file. */
__( 'Cannot get source file: %s', 'force-update-translations' ),
'<b>' . esc_html( $source ) . '</b>'
)
);
} else {
$translation_path = WP_LANG_DIR . '/' . $target;
if ( ! file_exists( pathinfo( $translation_path, PATHINFO_DIRNAME ) ) ) {
mkdir( pathinfo( $translation_path, PATHINFO_DIRNAME ), 0777, true );
}
file_put_contents( $translation_path, $response['body'] ); // phpcs:ignore
return;
}
}
/**
* Generate a file path to get translation file.
*
* @param string $project File project.
* @param string $locale File locale.
* @param string $format File format.
* @return $path File path to get source.
*/
public function get_source_path( $project, $locale, $format = 'mo' ) {
$locale = GP_Locales::by_field( 'wp_locale', $locale );
// Defaults to 'slug/default' if is a Root Locale, 'slug/variant' if is variant.
$locale_slug = $locale->slug;
if ( ! isset( $locale->root_slug ) ) {
$locale_slug .= '/default';
}
$path = sprintf(
'https://translate.wordpress.org/projects/%1$s/%2$s/export-translations?filters[status]=current_or_waiting_or_fuzzy',
$project,
$locale_slug
);
$path = ( $format === 'po' ) ? $path : $path . '&format=' . $format;
$path = esc_url_raw( $path );
return $path;
}
/**
* Prints admin screen notices.
*/
public function admin_notices() {
if ( empty( $this->admin_notices ) ) {
return;
}
foreach ( $this->admin_notices as $project ) {
foreach ( $project as $notice ) {
?>
<div class="notice notice-<?php echo esc_attr( $notice['status'] ); ?> inline">
<p><?php echo wp_kses_post( $notice['content'] ); ?></p>
</div>
<?php
}
}
}
}
new Force_Update_Translations();