Cyr-To-Lat - Version 3.4

Version Description

(21.01.2019) = * Tested up to WP 5.1 * Code formatting to follow WPCS. * Strict comparisons. * Braces {} removed from MySQL statements to allow checking of table names in PhpStorm. * Updated .gitignore and README.md * Added new filter ctl_pre_sanitize_title

Download this release

Release Info

Developer mihdan
Plugin Icon 128x128 Cyr-To-Lat
Version 3.4
Comparing to
See all releases

Code changes from version 3.3 to 3.4

Files changed (2) hide show
  1. cyr-to-lat.php +26 -11
  2. readme.txt +12 -4
cyr-to-lat.php CHANGED
@@ -5,7 +5,9 @@
5
  * 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.
6
  * Author: Sergey Biryukov, Mikhail Kobzarev
7
  * Author URI: http://ru.wordpress.org/
8
- * Version: 3.3
 
 
9
  */
10
 
11
  if ( ! defined( 'ABSPATH' ) ) {
@@ -22,6 +24,12 @@ if ( ! defined( 'ABSPATH' ) ) {
22
  function ctl_sanitize_title( $title ) {
23
  global $wpdb;
24
 
 
 
 
 
 
 
25
  $iso9_table = array(
26
  'А' => 'A',
27
  'Б' => 'B',
@@ -165,16 +173,17 @@ function ctl_sanitize_title( $title ) {
165
  break;
166
  }
167
 
168
- $is_term = false;
 
169
  $backtrace = debug_backtrace();
170
  foreach ( $backtrace as $backtrace_entry ) {
171
- if ( 'wp_insert_term' == $backtrace_entry['function'] ) {
172
  $is_term = true;
173
  break;
174
  }
175
  }
176
 
177
- $term = $is_term ? $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM {$wpdb->terms} WHERE name = %s", $title ) ) : '';
178
 
179
  if ( ! empty( $term ) ) {
180
  $title = $term;
@@ -192,6 +201,7 @@ function ctl_sanitize_title( $title ) {
192
 
193
  return $title;
194
  }
 
195
  add_filter( 'sanitize_title', 'ctl_sanitize_title', 9 );
196
  add_filter( 'sanitize_file_name', 'ctl_sanitize_title' );
197
 
@@ -201,23 +211,23 @@ add_filter( 'sanitize_file_name', 'ctl_sanitize_title' );
201
  function ctl_convert_existing_slugs() {
202
  global $wpdb;
203
 
204
- $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')" );
205
 
206
  foreach ( (array) $posts as $post ) {
207
  $sanitized_name = ctl_sanitize_title( urldecode( $post->post_name ) );
208
 
209
- if ( $post->post_name != $sanitized_name ) {
210
  add_post_meta( $post->ID, '_wp_old_slug', $post->post_name );
211
  $wpdb->update( $wpdb->posts, array( 'post_name' => $sanitized_name ), array( 'ID' => $post->ID ) );
212
  }
213
  }
214
 
215
- $terms = $wpdb->get_results( "SELECT term_id, slug FROM {$wpdb->terms} WHERE slug REGEXP('[^A-Za-z0-9\-]+') " );
216
 
217
  foreach ( (array) $terms as $term ) {
218
  $sanitized_slug = ctl_sanitize_title( urldecode( $term->slug ) );
219
 
220
- if ( $term->slug != $sanitized_slug ) {
221
  $wpdb->update( $wpdb->terms, array( 'slug' => $sanitized_slug ), array( 'term_id' => $term->term_id ) );
222
  }
223
  }
@@ -226,11 +236,14 @@ function ctl_convert_existing_slugs() {
226
  function ctl_schedule_conversion() {
227
  add_action( 'shutdown', 'ctl_convert_existing_slugs' );
228
  }
 
229
  register_activation_hook( __FILE__, 'ctl_schedule_conversion' );
230
 
231
  /**
232
  * Check if Classic Editor plugin is active.
233
  *
 
 
234
  * @return bool
235
  */
236
  function ctl_is_classic_editor_plugin_active() {
@@ -281,8 +294,6 @@ function ctl_is_gutenberg_editor_active() {
281
  * @param array $data An array of slashed post data.
282
  * @param array $postarr An array of sanitized, but otherwise unmodified post data.
283
  *
284
- * @link https://kagg.eu/how-to-catch-gutenberg/
285
- *
286
  * @return mixed
287
  */
288
  function ctl_sanitize_post_name( $data, $postarr ) {
@@ -291,12 +302,16 @@ function ctl_sanitize_post_name( $data, $postarr ) {
291
  return $data;
292
  }
293
 
294
- if ( ! $data['post_name'] && $data['post_title'] && ! in_array( $data['post_status'], array( 'auto-draft', 'revision' ) ) ) {
 
 
 
295
  $data['post_name'] = sanitize_title( $data['post_title'] );
296
  }
297
 
298
  return $data;
299
  }
 
300
  add_filter( 'wp_insert_post_data', 'ctl_sanitize_post_name', 10, 2 );
301
 
302
  // eof;
5
  * 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.
6
  * Author: Sergey Biryukov, Mikhail Kobzarev
7
  * Author URI: http://ru.wordpress.org/
8
+ * Requires at least: 2.3
9
+ * Tested up to: 5.1
10
+ * Version: 3.4
11
  */
12
 
13
  if ( ! defined( 'ABSPATH' ) ) {
24
  function ctl_sanitize_title( $title ) {
25
  global $wpdb;
26
 
27
+ $pre = apply_filters( 'ctl_pre_sanitize_title', false, $title );
28
+
29
+ if ( false !== $pre ) {
30
+ return $pre;
31
+ }
32
+
33
  $iso9_table = array(
34
  'А' => 'A',
35
  'Б' => 'B',
173
  break;
174
  }
175
 
176
+ $is_term = false;
177
+ // @codingStandardsIgnoreLine
178
  $backtrace = debug_backtrace();
179
  foreach ( $backtrace as $backtrace_entry ) {
180
+ if ( 'wp_insert_term' === $backtrace_entry['function'] ) {
181
  $is_term = true;
182
  break;
183
  }
184
  }
185
 
186
+ $term = $is_term ? $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE name = %s", $title ) ) : '';
187
 
188
  if ( ! empty( $term ) ) {
189
  $title = $term;
201
 
202
  return $title;
203
  }
204
+
205
  add_filter( 'sanitize_title', 'ctl_sanitize_title', 9 );
206
  add_filter( 'sanitize_file_name', 'ctl_sanitize_title' );
207
 
211
  function ctl_convert_existing_slugs() {
212
  global $wpdb;
213
 
214
+ $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')" );
215
 
216
  foreach ( (array) $posts as $post ) {
217
  $sanitized_name = ctl_sanitize_title( urldecode( $post->post_name ) );
218
 
219
+ if ( $post->post_name !== $sanitized_name ) {
220
  add_post_meta( $post->ID, '_wp_old_slug', $post->post_name );
221
  $wpdb->update( $wpdb->posts, array( 'post_name' => $sanitized_name ), array( 'ID' => $post->ID ) );
222
  }
223
  }
224
 
225
+ $terms = $wpdb->get_results( "SELECT term_id, slug FROM $wpdb->terms WHERE slug REGEXP('[^A-Za-z0-9\-]+') " );
226
 
227
  foreach ( (array) $terms as $term ) {
228
  $sanitized_slug = ctl_sanitize_title( urldecode( $term->slug ) );
229
 
230
+ if ( $term->slug !== $sanitized_slug ) {
231
  $wpdb->update( $wpdb->terms, array( 'slug' => $sanitized_slug ), array( 'term_id' => $term->term_id ) );
232
  }
233
  }
236
  function ctl_schedule_conversion() {
237
  add_action( 'shutdown', 'ctl_convert_existing_slugs' );
238
  }
239
+
240
  register_activation_hook( __FILE__, 'ctl_schedule_conversion' );
241
 
242
  /**
243
  * Check if Classic Editor plugin is active.
244
  *
245
+ * @link https://kagg.eu/how-to-catch-gutenberg/
246
+ *
247
  * @return bool
248
  */
249
  function ctl_is_classic_editor_plugin_active() {
294
  * @param array $data An array of slashed post data.
295
  * @param array $postarr An array of sanitized, but otherwise unmodified post data.
296
  *
 
 
297
  * @return mixed
298
  */
299
  function ctl_sanitize_post_name( $data, $postarr ) {
302
  return $data;
303
  }
304
 
305
+ if (
306
+ ! $data['post_name'] && $data['post_title'] &&
307
+ ! in_array( $data['post_status'], array( 'auto-draft', 'revision' ), true )
308
+ ) {
309
  $data['post_name'] = sanitize_title( $data['post_title'] );
310
  }
311
 
312
  return $data;
313
  }
314
+
315
  add_filter( 'wp_insert_post_data', 'ctl_sanitize_post_name', 10, 2 );
316
 
317
  // eof;
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === Cyr-To-Lat ===
2
- Contributors: SergeyBiryukov, mihdan, karevn, webvitaly
3
  Tags: cyrillic, georgian, latin, l10n, russian, rustolat, slugs, translations, transliteration
4
  Requires at least: 2.3
5
- Tested up to: 5.0
6
- Stable tag: 3.3
7
 
8
  Converts Cyrillic characters in post, page and term slugs to Latin characters.
9
 
@@ -16,7 +16,7 @@ Converts Cyrillic characters in post, page and term slugs to Latin characters. U
16
  * Saves existing post and page permalinks integrity
17
  * Performs transliteration of attachment file names
18
  * Includes Russian, Belarusian, Ukrainian, Bulgarian and Macedonian characters
19
- * Transliteration table can be customized without editing the plugin itself
20
 
21
  Based on the original Rus-To-Lat plugin by Anton Skorobogatov.
22
 
@@ -41,6 +41,14 @@ add_filter('ctl_table', 'my_cyr_to_lat_table');
41
 
42
  == Changelog ==
43
 
 
 
 
 
 
 
 
 
44
  = 3.3 (18.01.2019) =
45
  * wpcs 1.0
46
  * Fixed many bugs
1
  === Cyr-To-Lat ===
2
+ Contributors: SergeyBiryukov, mihdan, karevn, webvitaly, kagg-design
3
  Tags: cyrillic, georgian, latin, l10n, russian, rustolat, slugs, translations, transliteration
4
  Requires at least: 2.3
5
+ Tested up to: 5.1
6
+ Stable tag: 3.4
7
 
8
  Converts Cyrillic characters in post, page and term slugs to Latin characters.
9
 
16
  * Saves existing post and page permalinks integrity
17
  * Performs transliteration of attachment file names
18
  * Includes Russian, Belarusian, Ukrainian, Bulgarian and Macedonian characters
19
+ * Transliteration table can be customized without editing the plugin by itself
20
 
21
  Based on the original Rus-To-Lat plugin by Anton Skorobogatov.
22
 
41
 
42
  == Changelog ==
43
 
44
+ = 3.4 (21.01.2019) =
45
+ * Tested up to WP 5.1
46
+ * Code formatting to follow WPCS.
47
+ * Strict comparisons.
48
+ * Braces {} removed from MySQL statements to allow checking of table names in PhpStorm.
49
+ * Updated .gitignore and README.md
50
+ * Added new filter `ctl_pre_sanitize_title`
51
+
52
  = 3.3 (18.01.2019) =
53
  * wpcs 1.0
54
  * Fixed many bugs