-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtranslate-helper.php
More file actions
156 lines (133 loc) · 5.38 KB
/
translate-helper.php
File metadata and controls
156 lines (133 loc) · 5.38 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
<?php
// Register display names of all users with posting capabilities for translation
function register_users_display_names_for_translation() {
// Get plugin settings
$options = get_option('sls_settings');
// Check if username translation is enabled
if (empty($options['translate_usernames'])) {
return;
}
// Get all users with necessary fields
$users = get_users([
'fields' => ['display_name', 'ID', 'user_login'],
'capability__in' => ['edit_posts', 'publish_posts']
]);
// Register each user's display name for translation
foreach ($users as $user) {
pll_register_string(
'User Display Name - ' . $user->user_login,
$user->display_name,
'simple-language-switcher'
);
}
}
add_action('admin_init', 'register_users_display_names_for_translation');
// Filter to replace author's name with translated display name
function replace_author_with_display_name($display_name) {
// Get plugin settings
$options = get_option('sls_settings');
// Check if username translation is enabled
if (empty($options['translate_usernames'])) {
return $display_name;
}
global $post;
// First check if we have a valid post and post_author
if (!$post || !$post->post_author) {
return $display_name;
}
// Get the post author's data
$author = get_userdata($post->post_author);
// Get the translated display name if available, otherwise fallback to original display name
$translated_name = pll__($display_name, 'simple-language-switcher');
return !empty($translated_name) ? $translated_name : $author->display_name;
}
add_filter('the_author', 'replace_author_with_display_name');
add_filter('get_the_author_display_name', 'replace_author_with_display_name');
// Function to register the translatable strings (this should always run)
function register_translatable_strings() {
$strings = get_option('sls_translatable_strings', []);
foreach ($strings as $string) {
if (!empty($string['identifier']) && !empty($string['value'])) {
pll_register_string($string['identifier'], $string['value'], 'simple-language-switcher');
}
}
}
add_action('admin_init', 'register_translatable_strings');
// Shortcodes function (this should only run if shortcodes are enabled)
function register_translatable_string_shortcodes() {
$options = get_option('sls_settings');
if (empty($options['enable_shortcodes'])) {
return;
}
$strings = get_option('sls_translatable_strings', []);
foreach ($strings as $string) {
if (!empty($string['identifier'])) {
add_shortcode('SLS-' . $string['identifier'], 'handle_translatable_string_shortcode');
}
}
}
add_action('init', 'register_translatable_string_shortcodes');
// Function to handle the translatable string shortcode
function handle_translatable_string_shortcode($atts, $content, $tag) {
$identifier = substr($tag, 4);
$strings = get_option('sls_translatable_strings', []);
foreach ($strings as $string) {
if ($string['identifier'] === $identifier) {
return pll__($string['value'], 'simple-language-switcher');
}
}
return '';
}
// Function to register the translatable string block
function register_translatable_string_block() {
register_block_type(__DIR__ . '/blocks/translatable-string', [
'render_callback' => 'render_translatable_string_block',
'editor_script' => 'sls-translatable-string-editor',
'editor_style' => 'sls-translatable-string-editor'
]);
wp_register_script(
'sls-translatable-string-editor',
plugins_url('blocks/translatable-string/build/index.js', __FILE__),
['wp-blocks', 'wp-i18n', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-api-fetch'],
filemtime(plugin_dir_path(__FILE__) . 'blocks/translatable-string/build/index.js')
);
wp_register_style(
'sls-translatable-string-editor',
plugins_url('blocks/translatable-string/editor.css', __FILE__),
[],
filemtime(plugin_dir_path(__FILE__) . 'blocks/translatable-string/editor.css')
);
}
add_action('init', 'register_translatable_string_block');
// Function to render the translatable string block
function render_translatable_string_block($attributes) {
if (empty($attributes['identifier'])) {
return '';
}
$wrapper_attributes = get_block_wrapper_attributes([
'class' => 'wp-block-simple-language-switcher-translatable-string',
'lang' => substr(get_locale(), 0, 2)
]);
$strings = get_option('sls_translatable_strings', []);
foreach ($strings as $string) {
if ($string['identifier'] === $attributes['identifier']) {
return sprintf(
'<div %1$s>%2$s</div>',
$wrapper_attributes,
pll__($string['value'], 'simple-language-switcher')
);
}
}
return '';
}
// Register REST route for fetching translatable strings
function register_translatable_strings_rest_route() {
register_rest_route('simple-language-switcher/v1', '/strings', [
'methods' => 'GET',
'callback' => function() {
return get_option('sls_translatable_strings', []);
},
'permission_callback' => '__return_true'
]);
}
add_action('rest_api_init', 'register_translatable_strings_rest_route');