This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmqtranslate_hooks.php
258 lines (226 loc) · 11.7 KB
/
mqtranslate_hooks.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php // encoding: utf-8
/*Copyright 2008Qian Qin(email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA02110-1301USA
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/* mqTranslate Hooks */
function qtrans_localeForCurrentLanguage($locale) {
if (!defined('QTRANS_INIT'))
return $locale;
global $q_config;
// try to figure out the correct locale
$lang = $q_config['language'];
$locale_lang = $q_config['locale'][$lang];
$locale = array();
$locale[] = $locale_lang.".utf8";
$locale[] = $locale_lang."@euro";
$locale[] = $locale_lang;
$locale[] = $q_config['windows_locale'][$lang];
$locale[] = $lang;
// return the correct locale and most importantly set it (wordpress doesn't, which is bad)
// only set LC_TIME as everyhing else doesn't seem to work with windows
setlocale(LC_TIME, $locale);
return $$locale_lang;
}
function qtrans_useCurrentLanguageIfNotFoundShowAvailable($content) {
if (!defined('QTRANS_INIT'))
return $content;
global $q_config;
return qtrans_use($q_config['language'], $content, true);
}
function qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($content) {
if (!defined('QTRANS_INIT'))
return $content;
global $q_config;
return qtrans_use($q_config['language'], $content, false);
}
function qtrans_useDefaultLanguage($content) {
global $q_config;
return qtrans_use($q_config['default_language'], $content, false);
}
function qtrans_excludePages($pages) {
global $wpdb, $q_config;
static $exclude = 0;
if(!$q_config['hide_untranslated']) return $pages;
if(is_array($exclude)) return array_merge($exclude, $pages);
$query = "SELECT id FROM $wpdb->posts WHERE post_type = 'page' AND post_status = 'publish' AND NOT ($wpdb->posts.post_title LIKE '%<!--:".qtrans_getLanguage()."-->%')" ;
$hide_pages = $wpdb->get_results($query);
$exclude = array();
foreach($hide_pages as $page) {
$exclude[] = $page->id;
}
return array_merge($exclude, $pages);
}
function qtrans_languageColumnHeader($columns){
$new_columns = array();
if(isset($columns['cb'])) $new_columns['cb'] = '';
if(isset($columns['title'])) $new_columns['title'] = '';
if(isset($columns['author'])) $new_columns['author'] = '';
if(isset($columns['categories'])) $new_columns['categories'] = '';
if(isset($columns['tags'])) $new_columns['tags'] = '';
$new_columns['language'] = __('Languages', 'mqtranslate');
return array_merge($new_columns, $columns);;
}
function qtrans_languageColumn($column) {
global $q_config, $post;
if ($column == 'language') {
$available_languages = qtrans_getAvailableLanguages($post->post_content);
$missing_languages = array_diff($q_config['enabled_languages'], $available_languages);
$available_languages_name = array();
$missing_languages_name = array();
foreach($available_languages as $language) {
$available_languages_name[] = $q_config['language_name'][$language];
}
$available_languages_names = join(", ", $available_languages_name);
echo apply_filters('qtranslate_available_languages_names',$available_languages_names);
do_action('qtranslate_languageColumn', $available_languages, $missing_languages);
}
return $column;
}
function qtrans_versionLocale() {
return 'en_US';
}
function qtrans_useRawTitle($title, $raw_title = '', $context = 'save') {
if($raw_title=='') $raw_title = $title;
if('save'==$context) {
$raw_title = qtrans_useDefaultLanguage($raw_title);
$title = remove_accents($raw_title);
}
return $title;
}
function qtrans_checkCanonical($redirect_url, $requested_url) {
// fix canonical conflicts with language urls
if(qtrans_convertURL($redirect_url)==qtrans_convertURL($requested_url))
return false;
return $redirect_url;
}
function qtrans_fixAdminBar($wp_admin_bar) {
global $wp_admin_bar;
$nodes = $wp_admin_bar->get_nodes();
if (is_array($nodes)) {
foreach($wp_admin_bar->get_nodes() as $node)
$wp_admin_bar->add_node(qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($node));
}
}
// Hooks for Plugin compatibility
function qtrans_supercache_dir($uri) {
global $q_config;
if(isset($q_config['url_info']['original_url'])) {
$uri = $q_config['url_info']['original_url'];
} else {
$uri = $_SERVER['REQUEST_URI'];
}
$uri = preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', str_replace( '/index.php', '/', str_replace( '..', '', preg_replace("/(\?.*)?$/", '', $uri ) ) ) );
$uri = str_replace( '\\', '', $uri );
$uri = strtolower(preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"])) . $uri; // To avoid XSS attacs
return $uri;
}
add_filter('supercache_dir', 'qtrans_supercache_dir',0);
function qtrans_gettext($translated_text) {
global $q_config;
if (!isset($q_config['language']))
return $translated_text;
return qtrans_use($q_config['language'], $translated_text, false);
}
function qtrans_gettext_with_context($translated_text) {
global $q_config;
if(!isset($q_config['language']))
return $translated_text;
return qtrans_use($q_config['language'], $translated_text, false);
}
// Hooks (Actions)
add_action('plugins_loaded', 'qtrans_init_language', 2);//user is not authenticated yet
add_action('init', 'qtrans_init');//user is authenticated
add_action('widgets_init', 'qtrans_widget_init');
// Hooks (execution time critical filters)
add_filter('gettext', 'qtrans_gettext',0);
add_filter('gettext_with_context', 'qtrans_gettext_with_context',0);
add_filter('the_content', 'qtrans_useCurrentLanguageIfNotFoundShowAvailable', 0);
add_filter('the_excerpt', 'qtrans_useCurrentLanguageIfNotFoundShowAvailable', 0);
add_filter('the_excerpt_rss', 'qtrans_useCurrentLanguageIfNotFoundShowAvailable', 0);
add_filter('sanitize_title', 'qtrans_useRawTitle',0, 3);
add_filter('comment_moderation_subject', 'qtrans_useDefaultLanguage',0);
add_filter('comment_moderation_text', 'qtrans_useDefaultLanguage',0);
add_filter('get_comment_date', 'qtrans_dateFromCommentForCurrentLanguage',0,2);
add_filter('get_comment_time', 'qtrans_timeFromCommentForCurrentLanguage',0,4);
add_filter('get_post_modified_time', 'qtrans_timeModifiedFromPostForCurrentLanguage',0,3);
add_filter('get_the_time', 'qtrans_timeFromPostForCurrentLanguage',0,3);
add_filter('get_the_date', 'qtrans_dateFromPostForCurrentLanguage',0,2);
add_filter('get_the_modified_date', 'qtrans_dateModifiedFromPostForCurrentLanguage',0,2);
add_filter('locale', 'qtrans_localeForCurrentLanguage',99);
add_filter('the_title', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage', 0);
add_filter('post_title', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage', 0);
add_filter('term_name', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('tag_rows', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('list_cats', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('wp_list_categories', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('wp_dropdown_cats', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('wp_title', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('single_post_title', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('bloginfo', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('get_others_drafts', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('get_bloginfo_rss', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('get_wp_title_rss', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('wp_title_rss', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('the_title_rss', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('the_content_rss', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('gettext', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('get_pages', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('category_description', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('bloginfo_rss', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('the_category_rss', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('wp_generate_tag_cloud', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('term_links-post_tag', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('link_name', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('link_description', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter('pre_option_rss_language', 'qtrans_getLanguage',0);
add_filter('the_author', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage',0);
add_filter( "_wp_post_revision_field_post_title", 'qtrans_showAllSeperated', 0);
add_filter( "_wp_post_revision_field_post_content", 'qtrans_showAllSeperated', 0);
add_filter( "_wp_post_revision_field_post_excerpt", 'qtrans_showAllSeperated', 0);
add_filter('get_the_author_description', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage');
add_filter('_wp_post_revision_field_post_title', 'qtrans_showAllSeperated', 0);
add_filter('_wp_post_revision_field_post_content', 'qtrans_showAllSeperated', 0);
add_filter('_wp_post_revision_field_post_excerpt', 'qtrans_showAllSeperated', 0);
// Hooks (execution time non-critical filters)
add_filter('author_feed_link', 'qtrans_convertURL');
add_filter('author_link', 'qtrans_convertURL');
add_filter('author_feed_link', 'qtrans_convertURL');
add_filter('day_link', 'qtrans_convertURL');
add_filter('get_comment_author_url_link', 'qtrans_convertURL');
add_filter('month_link', 'qtrans_convertURL');
add_filter('page_link', 'qtrans_convertURL');
add_filter('post_link', 'qtrans_convertURL');
add_filter('year_link', 'qtrans_convertURL');
add_filter('category_feed_link', 'qtrans_convertURL');
add_filter('category_link', 'qtrans_convertURL');
add_filter('tag_link', 'qtrans_convertURL');
add_filter('term_link', 'qtrans_convertURL');
add_filter('the_permalink', 'qtrans_convertURL');
add_filter('feed_link', 'qtrans_convertURL');
add_filter('post_comments_feed_link', 'qtrans_convertURL');
add_filter('tag_feed_link', 'qtrans_convertURL');
add_filter('get_pagenum_link', 'qtrans_convertURL');
add_filter('manage_posts_columns', 'qtrans_languageColumnHeader');
add_filter('manage_posts_custom_column', 'qtrans_languageColumn');
add_filter('manage_pages_columns', 'qtrans_languageColumnHeader');
add_filter('manage_pages_custom_column', 'qtrans_languageColumn');
add_filter('wp_list_pages_excludes', 'qtrans_excludePages');
add_filter('comment_notification_text', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage');
add_filter('comment_notification_headers', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage');
add_filter('comment_notification_subject', 'qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage');
add_filter('bloginfo_url', 'qtrans_convertBlogInfoURL',10,2);
add_filter('manage_language_columns', 'qtrans_language_columns');
add_filter('core_version_check_locale', 'qtrans_versionLocale');
add_filter('redirect_canonical', 'qtrans_checkCanonical', 10, 2);