Cyr to Lat reloaded – transliteration of links and file names - Version 1.0.3

Version Description

Download this release

Release Info

Developer webcraftic
Plugin Icon 128x128 Cyr to Lat reloaded – transliteration of links and file names
Version 1.0.3
Comparing to
See all releases

Version 1.0.3

Files changed (2) hide show
  1. cyr-and-lat.php +128 -0
  2. readme.txt +57 -0
cyr-and-lat.php ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: Cyr-And-Lat
4
+ Plugin URI:
5
+ Description: Converts Cyrillic characters in post and term slugs to Latin characters. Useful for creating human-readable URLs. Allows to use both of cyrillic and latin slugs.
6
+ Author:
7
+ Author URI:
8
+ Version: 1.0.3
9
+ */
10
+
11
+ function cal_sanitize_title($title,$savesymbols = false) {
12
+ global $wpdb;
13
+
14
+ $iso9_table = array(
15
+ 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Ѓ' => 'G`',
16
+ 'Ґ' => 'G`', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'YO', 'Є' => 'YE',
17
+ 'Ж' => 'ZH', 'З' => 'Z', 'Ѕ' => 'Z', 'И' => 'I', 'Й' => 'J',
18
+ 'Ј' => 'J', 'І' => 'I', 'Ї' => 'YI', 'К' => 'K', 'Ќ' => 'K`',
19
+ 'Л' => 'L', 'Љ' => 'L', 'М' => 'M', 'Н' => 'N', 'Њ' => 'N`',
20
+ 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T',
21
+ 'У' => 'U', 'Ў' => 'U`', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'TS',
22
+ 'Ч' => 'CH', 'Џ' => 'DH', 'Ш' => 'SH', 'Щ' => 'SHH', 'Ъ' => '``',
23
+ 'Ы' => 'Y`', 'Ь' => '`', 'Э' => 'E`', 'Ю' => 'YU', 'Я' => 'YA',
24
+ 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'ѓ' => 'g',
25
+ 'ґ' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'є' => 'ye',
26
+ 'ж' => 'zh', 'з' => 'z', 'ѕ' => 'z', 'и' => 'i', 'й' => 'j',
27
+ 'ј' => 'j', 'і' => 'i', 'ї' => 'yi', 'к' => 'k', 'ќ' => 'k`',
28
+ 'л' => 'l', 'љ' => 'l', 'м' => 'm', 'н' => 'n', 'њ' => 'n`',
29
+ 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
30
+ 'у' => 'u', 'ў' => 'u`', 'ф' => 'f', 'х' => 'h', 'ц' => 'ts',
31
+ 'ч' => 'ch', 'џ' => 'dh', 'ш' => 'sh', 'щ' => 'shh', 'ъ' => '``',
32
+ 'ы' => 'y`', 'ь' => '`', 'э' => 'e`', 'ю' => 'yu', 'я' => 'ya'
33
+ );
34
+ $geo2lat = array(
35
+ 'ა' => 'a', 'ბ' => 'b', 'გ' => 'g', 'დ' => 'd', 'ე' => 'e', 'ვ' => 'v',
36
+ 'ზ' => 'z', 'თ' => 'th', 'ი' => 'i', 'კ' => 'k', 'ლ' => 'l', 'მ' => 'm',
37
+ 'ნ' => 'n', 'ო' => 'o', 'პ' => 'p','ჟ' => 'zh','რ' => 'r','ს' => 's',
38
+ 'ტ' => 't','უ' => 'u','ფ' => 'ph','ქ' => 'q','ღ' => 'gh','ყ' => 'qh',
39
+ 'შ' => 'sh','ჩ' => 'ch','ც' => 'ts','ძ' => 'dz','წ' => 'ts','ჭ' => 'tch',
40
+ 'ხ' => 'kh','ჯ' => 'j','ჰ' => 'h'
41
+ );
42
+ $iso9_table = array_merge($iso9_table, $geo2lat);
43
+
44
+ $locale = get_locale();
45
+ switch ( $locale ) {
46
+ case 'bg_BG':
47
+ $iso9_table['Щ'] = 'SHT';
48
+ $iso9_table['щ'] = 'sht';
49
+ $iso9_table['Ъ'] = 'A`';
50
+ $iso9_table['ъ'] = 'a`';
51
+ break;
52
+ case 'uk':
53
+ $iso9_table['И'] = 'Y`';
54
+ $iso9_table['и'] = 'y`';
55
+ break;
56
+ }
57
+
58
+ $is_term = false;
59
+ $backtrace = debug_backtrace();
60
+ foreach ( $backtrace as $backtrace_entry ) {
61
+ if ( $backtrace_entry['function'] == 'wp_insert_term' ) {
62
+ $is_term = true;
63
+ break;
64
+ }
65
+ }
66
+
67
+ $term = $is_term ? $wpdb->get_var("SELECT slug FROM {$wpdb->terms} WHERE name = '$title'") : '';
68
+ if ( empty($term) ) {
69
+ $title = strtr($title, apply_filters('cal_table', $iso9_table));
70
+ if (function_exists('iconv')){
71
+ $title = iconv('UTF-8', 'UTF-8//TRANSLIT//IGNORE', $title);
72
+ }
73
+ if (!$savesymbols) {
74
+ $title = preg_replace("/[^A-Za-z0-9'_\-\.]/", '-', $title);
75
+ $title = preg_replace('/\-+/', '-', $title);
76
+ $title = preg_replace('/^-+/', '', $title);
77
+ $title = preg_replace('/-+$/', '', $title);
78
+ }
79
+ } else {
80
+ $title = $term;
81
+ }
82
+
83
+ return $title;
84
+ }
85
+ add_filter('sanitize_title', 'cal_sanitize_title', 9);
86
+ add_filter('sanitize_file_name', 'cal_sanitize_title');
87
+
88
+ function cal_convert_existing_slugs() {
89
+ global $wpdb;
90
+
91
+ $posts = $wpdb->get_results("SELECT ID, post_name FROM {$wpdb->posts} WHERE post_name REGEXP('[^A-Za-z0-9\-]+') AND post_status IN ('publish', 'future', 'private')");
92
+ foreach ( (array) $posts as $post ) {
93
+ $sanitized_name = cal_sanitize_title(urldecode($post->post_name));
94
+ if ( $post->post_name != $sanitized_name ) {
95
+ add_post_meta($post->ID, '_wp_old_slug', $post->post_name);
96
+ $wpdb->update($wpdb->posts, array( 'post_name' => $sanitized_name ), array( 'ID' => $post->ID ));
97
+ }
98
+ }
99
+
100
+ $terms = $wpdb->get_results("SELECT term_id, slug FROM {$wpdb->terms} WHERE slug REGEXP('[^A-Za-z0-9\-]+') ");
101
+ foreach ( (array) $terms as $term ) {
102
+ $sanitized_slug = cal_sanitize_title(urldecode($term->slug));
103
+ if ( $term->slug != $sanitized_slug ) {
104
+ $wpdb->update($wpdb->terms, array( 'slug' => $sanitized_slug ), array( 'term_id' => $term->term_id ));
105
+ }
106
+ }
107
+ }
108
+
109
+ function cal_schedule_conversion() {
110
+ add_action('shutdown', 'cal_convert_existing_slugs');
111
+ }
112
+ register_activation_hook(__FILE__, 'cal_schedule_conversion');
113
+
114
+ function cal_use_cyrillic_alias($request){
115
+ if (isset($request['name'])){
116
+ $request['name'] = cal_sanitize_title(urldecode($request['name']));
117
+ }
118
+ if (isset($request['category_name'])){
119
+ $request['category_name'] = cal_sanitize_title(urldecode($request['category_name']),true);
120
+ }
121
+ if (isset($request['tag'])){
122
+ $request['tag'] = cal_sanitize_title(urldecode($request['tag']));
123
+ }
124
+ return $request;
125
+ }
126
+ add_filter('request','cal_use_cyrillic_alias');
127
+
128
+ ?>
readme.txt ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Cyr-And-Lat reloaded ===
2
+ Contributors: webcraftic, preslilvs
3
+ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=VDX7JNTQPNPFW
4
+ Tags: cyrillic, latin, l10n, russian, rustolat, slugs, translations, transliteration, media, georgian, european, diacritics, muiltilanguage
5
+ Requires at least: 2.3
6
+ Tested up to: 4.9
7
+ Requires PHP: 5.2
8
+ Stable tag: trunk
9
+ License: GPLv2
10
+ License URI: https://www.gnu.org/licenses/gpl-2.0.html
11
+
12
+ Allows to use both "old" cyrillic and "new" latin slugs at the same time. Now it works for terms (tags and categories slugs) too.
13
+
14
+ == Description ==
15
+
16
+ Converts Cyrillic characters in post, page and term slugs to Latin characters. Useful for creating human-readable URLs. After the activation changes every slug to Latin characters, but old slugs will work too.
17
+
18
+ = Features =
19
+ * Automatically converts existing post, page and term slugs on activation.
20
+ * Saves access to posts by old cyrillic slugs (prevents broken links).
21
+ * Saves existing post and page permalinks integrity.
22
+ * Performs transliteration of attachment file names.
23
+ * Includes Russian, Belarusian, Ukrainian, Bulgarian and Macedonian characters
24
+ * Transliteration table can be customized without editing the plugin itself
25
+
26
+ Based on the Cyr-To-Lat plugin by Sergey Biryukov, which based on the original Rus-To-Lat plugin by Anton Skorobogatov.
27
+
28
+ == Installation ==
29
+
30
+ 1. Upload `cyrandlat` folder to the `/wp-content/plugins/` directory.
31
+ 2. Activate the plugin through the 'Plugins' menu in WordPress.
32
+ 3. Make sure your system has iconv set up right, or iconv is not installed at all. If you have any problems (trimmed slugs, strange characters, question marks) - please ask for support.
33
+
34
+ == Frequently Asked Questions ==
35
+
36
+ = How can I define my own substitutions? =
37
+
38
+ Add this code to your theme's `functions.php` file:
39
+ `
40
+ function my_cyr_and_lat_table($cal_table) {
41
+ $cal_table['Ъ'] = 'U';
42
+ $cal_table['ъ'] = 'u';
43
+ return $cal_table;
44
+ }
45
+ add_filter('cal_table', 'my_cyr_and_lat_table');
46
+ `
47
+
48
+ == Changelog ==
49
+
50
+ = 1.0.2 =
51
+ * Backward сompatibility with "old" russian slugs works in terms (tags and categories) too.
52
+
53
+ = 1.0.1 =
54
+ * Fixed minor bug
55
+
56
+ = 1.0 =
57
+ * Initial release