Skip to content

Commit

Permalink
Инициализация плагина на GitHub, привел код к wpcs 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
mihdan committed Dec 10, 2018
1 parent 906072c commit 998ccec
Show file tree
Hide file tree
Showing 2 changed files with 246 additions and 0 deletions.
174 changes: 174 additions & 0 deletions cyr-to-lat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php
/**
* Plugin Name: Cyr-To-Lat
* Plugin URI: http://wordpress.org/extend/plugins/cyr2lat/
* Description: Converts Cyrillic characters in post and term slugs to Latin characters. Useful for creating human-readable URLs. Based on the original plugin by Anton Skorobogatov.
* Author: Sol, Sergey Biryukov, Mikhail Kobzarev
* Author URI: http://ru.wordpress.org/
* Version: 3.3
*/

function ctl_sanitize_title( $title ) {
global $wpdb;

$iso9_table = array(
'А' => 'A',
'Б' => 'B',
'В' => 'V',
'Г' => 'G',
'Ѓ' => 'G`',
'Ґ' => 'G`',
'Д' => 'D',
'Е' => 'E',
'Ё' => 'YO',
'Є' => 'YE',
'Ж' => 'ZH',
'З' => 'Z',
'Ѕ' => 'Z',
'И' => 'I',
'Й' => 'J',
'Ј' => 'J',
'І' => 'I',
'Ї' => 'YI',
'К' => 'K',
'Ќ' => 'K`',
'Л' => 'L',
'Љ' => 'L',
'М' => 'M',
'Н' => 'N',
'Њ' => 'N`',
'О' => 'O',
'П' => 'P',
'Р' => 'R',
'С' => 'S',
'Т' => 'T',
'У' => 'U',
'Ў' => 'U`',
'Ф' => 'F',
'Х' => 'H',
'Ц' => 'TS',
'Ч' => 'CH',
'Џ' => 'DH',
'Ш' => 'SH',
'Щ' => 'SHH',
'Ъ' => '``',
'Ы' => 'Y`',
'Ь' => '`',
'Э' => 'E`',
'Ю' => 'YU',
'Я' => 'YA',
'а' => 'a',
'б' => 'b',
'в' => 'v',
'г' => 'g',
'ѓ' => 'g',
'ґ' => 'g',
'д' => 'd',
'е' => 'e',
'ё' => 'yo',
'є' => 'ye',
'ж' => 'zh',
'з' => 'z',
'ѕ' => 'z',
'и' => 'i',
'й' => 'j',
'ј' => 'j',
'і' => 'i',
'ї' => 'yi',
'к' => 'k',
'ќ' => 'k`',
'л' => 'l',
'љ' => 'l',
'м' => 'm',
'н' => 'n',
'њ' => 'n`',
'о' => 'o',
'п' => 'p',
'р' => 'r',
'с' => 's',
'т' => 't',
'у' => 'u',
'ў' => 'u`',
'ф' => 'f',
'х' => 'h',
'ц' => 'ts',
'ч' => 'ch',
'џ' => 'dh',
'ш' => 'sh',
'щ' => 'shh',
'ъ' => '``',
'ы' => 'y`',
'ь' => '`',
'э' => 'e`',
'ю' => 'yu',
'я' => 'ya',
);

$locale = get_locale();
switch ( $locale ) {
case 'bg_BG':
$iso9_table['Щ'] = 'SHT';
$iso9_table['щ'] = 'sht';
$iso9_table['Ъ'] = 'A`';
$iso9_table['ъ'] = 'a`';
break;
case 'uk':
$iso9_table['И'] = 'Y`';
$iso9_table['и'] = 'y`';
break;
}

$is_term = false;
$backtrace = debug_backtrace();
foreach ( $backtrace as $backtrace_entry ) {
if ( 'wp_insert_term' == $backtrace_entry['function'] ) {
$is_term = true;
break;
}
}

$term = $is_term ? $wpdb->get_var( "SELECT slug FROM {$wpdb->terms} WHERE name = '$title'" ) : '';

if ( empty( $term ) ) {
$title = strtr( $title, apply_filters( 'ctl_table', $iso9_table ) );
$title = preg_replace( "/[^A-Za-z0-9`'_\-\.]/", '-', $title );
} else {
$title = $term;
}

return $title;
}
add_filter( 'sanitize_title', 'ctl_sanitize_title', 9 );
add_filter( 'sanitize_file_name', 'ctl_sanitize_title' );

function ctl_convert_existing_slugs() {
global $wpdb;

$posts = $wpdb->get_results( "SELECT ID, post_name FROM {$wpdb->posts} WHERE post_name REGEXP('[^A-Za-z0-9\-]+') AND post_status = 'publish'" );

foreach ( (array) $posts as $post ) {
$sanitized_name = ctl_sanitize_title( urldecode( $post->post_name ) );

if ( $post->post_name != $sanitized_name ) {
add_post_meta( $post->ID, '_wp_old_slug', $post->post_name );
$wpdb->update( $wpdb->posts, array( 'post_name' => $sanitized_name ), array( 'ID' => $post->ID ) );
}
}

$terms = $wpdb->get_results( "SELECT term_id, slug FROM {$wpdb->terms} WHERE slug REGEXP('[^A-Za-z0-9\-]+') " );

foreach ( (array) $terms as $term ) {
$sanitized_slug = ctl_sanitize_title( urldecode( $term->slug ) );

if ( $term->slug != $sanitized_slug ) {
$wpdb->update( $wpdb->terms, array( 'slug' => $sanitized_slug ), array( 'term_id' => $term->term_id ) );
}
}
}

function ctl_schedule_conversion() {
add_action( 'shutdown', 'ctl_convert_existing_slugs' );
}
register_activation_hook( __FILE__, 'ctl_schedule_conversion' );

// eof;
72 changes: 72 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
=== Cyr-To-Lat ===
Contributors: Atrax, SergeyBiryukov, mihdan
Tags: cyrillic, latin, l10n, russian, rustolat, slugs, translations, transliteration
Requires at least: 2.3
Tested up to: 5.0
Stable tag: 3.3

Converts Cyrillic characters in post, page and term slugs to Latin characters.

== Description ==

Converts Cyrillic characters in post, page and term slugs to Latin characters. Useful for creating human-readable URLs.

= Features =
* Automatically converts existing post, page and term slugs on activation
* Saves existing post and page permalinks integrity
* Performs transliteration of attachment file names
* Includes Russian, Belarusian, Ukrainian, Bulgarian and Macedonian characters
* Transliteration table can be customized without editing the plugin itself

Based on the original Rus-To-Lat plugin by Anton Skorobogatov.

== Installation ==

1. Upload `cyr2lat` folder to the `/wp-content/plugins/` directory.
2. Activate the plugin through the 'Plugins' menu in WordPress.

== Frequently Asked Questions ==

= How can I define my own substitutions? =

Add this code to your theme's `functions.php` file:
`
function my_cyr_to_lat_table($ctl_table) {
$ctl_table['Ъ'] = 'U';
$ctl_table['ъ'] = 'u';
return $ctl_table;
}
add_filter('ctl_table', 'my_cyr_to_lat_table');
`

== Changelog ==

= 3.2.1 =
* mihdan, you are welcome :)

= 3.2 =
* Added transliteration when publishing via XML-RPC
* Fixed Invalid Taxonomy error when viewing the most used tags

= 3.1 =
* Fixed transliteration when saving a draft

= 3.0 =
* Added automatic conversion of existing post, page and term slugs
* Added saving of existing post and page permalinks integrity
* Added transliteration of attachment file names
* Adjusted transliteration table in accordance with ISO 9 standard
* Included Russian, Belarusian, Ukrainian, Bulgarian and Macedonian characters
* Added filter for the transliteration table

= 2.1 =
* Optimized filter call

= 2.0 =
* Added check for existing terms

= 1.0.1 =
* Updated description

= 1.0 =
* Initial release

0 comments on commit 998ccec

Please sign in to comment.