Skip to content

Commit

Permalink
Merge ab11c8f into e43c288
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Feb 21, 2021
2 parents e43c288 + ab11c8f commit 1ce66fd
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 37 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
@@ -1,10 +1,14 @@
## 4.6.2 (11.02.2021) =
## 4.6.3 (21.02.2021)
* Fix bug with attachment post type filtered by 'ctl_post_types'
* Fix bug with background conversion of product attribute terms

## 4.6.2 (11.02.2021)
* Fix bug with non-existing function PLL().

## 4.6.1 (10.02.2021) =
## 4.6.1 (10.02.2021)
* Fix bug with Polylang

## 4.6.0 (10.02.2021) =
## 4.6.0 (10.02.2021)
* Add compatibility with Polylang
* Add confirmation popup before mass conversion of slugs
* Improve selection of posts and tags for conversion to avoid selection of excessive items for mass conversion
Expand Down
13 changes: 8 additions & 5 deletions classes/class-converter.php
Expand Up @@ -187,13 +187,16 @@ protected function convert_existing_post_slugs( $args = [] ) {

$regexp = $wpdb->prepare( '%s', self::ALLOWED_CHARS_REGEX );

$post_sql =
$post_sql =
'post_status IN (' . $this->main->prepare_in( $parsed_args['post_status'] ) . ')' .
' AND post_type IN (' . $this->main->prepare_in( $parsed_args['post_type'] ) . ')';
$media_sql = "post_status = 'inherit' AND post_type = 'attachment'";
$all_posts_sql = '(' . $post_sql . ') OR (' . $media_sql . ')';

$sql = "SELECT ID, post_name, post_type FROM $wpdb->posts WHERE LOWER(post_name) NOT REGEXP($regexp) AND ($all_posts_sql)";
if ( isset( $parsed_args['post_type']['attachment'] ) ) {
$media_sql = "post_status = 'inherit' AND post_type = 'attachment'";
$post_sql = '(' . $post_sql . ') OR (' . $media_sql . ')';
}

$sql = "SELECT ID, post_name, post_type FROM $wpdb->posts WHERE LOWER(post_name) NOT REGEXP($regexp) AND ($post_sql)";

// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
// phpcs:ignore WordPress.DB.DirectDatabaseQuery
Expand Down Expand Up @@ -232,7 +235,7 @@ protected function convert_existing_term_slugs() {
$terms = $wpdb->get_results(
$wpdb->prepare(
"SELECT t.term_id, slug, tt.taxonomy, tt.term_taxonomy_id FROM $wpdb->terms t, $wpdb->term_taxonomy tt
WHERE LOWER(t.slug) NOT REGEXP(%s) AND tt.term_id = t.term_id",
WHERE LOWER(t.slug) NOT REGEXP(%s) AND tt.taxonomy NOT REGEXP ('^pa_.*$') AND tt.term_id = t.term_id",
self::ALLOWED_CHARS_REGEX
)
);
Expand Down
2 changes: 2 additions & 0 deletions classes/class-main.php
Expand Up @@ -196,6 +196,8 @@ protected function is_wc_attribute_taxonomy( $title ) {
return false;
}

$title = str_replace( 'pa_', '', $title );

$attribute_taxonomies = wc_get_attribute_taxonomies();

foreach ( $attribute_taxonomies as $attribute_taxonomy ) {
Expand Down
6 changes: 3 additions & 3 deletions cyr-to-lat.php
Expand Up @@ -9,8 +9,8 @@
* Author URI: https://profiles.wordpress.org/sergeybiryukov/
* Requires at least: 5.1
* Tested up to: 5.6
* Version: 4.6.2
* Stable tag: 4.6.2
* Version: 4.6.3
* Stable tag: 4.6.3
*
* Text Domain: cyr2lat
* Domain Path: /languages/
Expand All @@ -34,7 +34,7 @@
/**
* Plugin version.
*/
define( 'CYR_TO_LAT_VERSION', '4.6.2' );
define( 'CYR_TO_LAT_VERSION', '4.6.3' );

/**
* Path to the plugin dir.
Expand Down
85 changes: 82 additions & 3 deletions readme.txt
Expand Up @@ -3,7 +3,7 @@ Contributors: SergeyBiryukov, mihdan, karevn, webvitaly, kaggdesign
Tags: cyrillic, belorussian, ukrainian, bulgarian, macedonian, georgian, kazakh, latin, l10n, russian, cyr-to-lat, cyr2lat, rustolat, slugs, translations, transliteration
Requires at least: 5.1
Tested up to: 5.6
Stable tag: 4.6.2
Stable tag: 4.6.3
Requires PHP: 5.6.20

Converts Cyrillic characters in post, page and term slugs to Latin characters.
Expand Down Expand Up @@ -52,6 +52,7 @@ function my_ctl_table( $table ) {

return $table;
}

add_filter( 'ctl_table', 'my_ctl_table' );
`

Expand All @@ -75,12 +76,86 @@ function my_ctl_table( $table ) {

return $table;
}

add_filter( 'ctl_table', 'my_ctl_table' );
`

= How can I convert a large number of posts/terms using wp-cli? =
= How can I define own transliteration of titles? =

Add similar code to your theme's `functions.php` file:

`
/**
* Filter title before sanitizing.
*
* @param string|false $result Sanitized title.
* @param string $title Title.
*
* @return string|false
*/
function my_ctl_pre_sanitize_title( $result, $title ) {
if ( 'пиво' === $title ) {
return 'beer';
}

return $result;
}

add_filter( 'ctl_pre_sanitize_title', 10, 2 );
`

= How can I define own transliteration of filenames? =

Use the following command in console:
Add similar code to your theme's `functions.php` file:

`
/**
* Filter filename before sanitizing.
*
* @param string|false $result Sanitized filename.
* @param string $filename Title.
*
* @return string|false
*/
function my_ctl_pre_sanitize_filename( $result, $filename ) {
if ( 'пиво' === $filename ) {
return 'beer';
}

return $result;
}

add_filter( 'ctl_pre_sanitize_filename', 10, 2 );
`

= How can I limit post types for background conversion? =

Add similar code to your theme's `functions.php` file:

`
/**
* Filter post types allowed for background conversion.
*
* @param array $post_types Allowed post types.
*
* @return array
*/
function my_ctl_post_types( $post_types ) {
return [
'post' => 'post',
'page' => 'page',
'attachment' => 'attachment',
'product' => 'product',
'nav_menu_item' => 'nav_menu_item',
];
}

add_filter( 'ctl_post_types', 'my_ctl_post_types' );
`

= How can I convert many posts/terms using wp-cli? =

Use the following command in the console:

`
wp cyr2lat regenerate [--post_type=<post_type>] [--post_status=<post_status>]
Expand All @@ -99,6 +174,10 @@ Yes you can!

== Changelog ==

= 4.6.3 (21.02.2021) =
* Fix bug with attachment post type filtered by 'ctl_post_types'
* Fix bug with background conversion of product attribute terms

= 4.6.2 (11.02.2021) =
* Fix bug with non-existing function PLL().

Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/bootstrap.php
Expand Up @@ -37,7 +37,7 @@
/**
* Plugin version.
*/
define( 'CYR_TO_LAT_TEST_VERSION', '4.6.2' );
define( 'CYR_TO_LAT_TEST_VERSION', '4.6.3' );

/**
* Path to the plugin dir.
Expand Down
53 changes: 36 additions & 17 deletions tests/phpunit/tests/class-test-converter.php
Expand Up @@ -258,12 +258,13 @@ public function dp_test_process_handler() {
/**
* Test convert_existing_slugs()
*
* @param array $posts Posts to convert.
* @param array $terms Terms to convert.
* @param array $posts Posts to convert.
* @param array $terms Terms to convert.
* @param bool $include_attachment Include attachment as post type.
*
* @dataProvider dp_test_convert_existing_slugs
*/
public function test_convert_existing_slugs( $posts, $terms ) {
public function test_convert_existing_slugs( $posts, $terms, $include_attachment ) {
global $wpdb;

$main = Mockery::mock( Main::class );
Expand All @@ -278,12 +279,20 @@ public function test_convert_existing_slugs( $posts, $terms ) {
$admin_notices
);

$post_types = [
'post' => 'post',
'page' => 'page',
'attachment' => 'attachment',
];
$post_types_in = "'post', 'page', 'attachment'";
if ( $include_attachment ) {
$post_types = [
'post' => 'post',
'page' => 'page',
'attachment' => 'attachment',
];
$post_types_in = "'post', 'page', 'attachment'";
} else {
$post_types = [
'post' => 'post',
'page' => 'page',
];
$post_types_in = "'post', 'page'";
}

$post_statuses = [ 'publish', 'future', 'private' ];
$post_statuses_in = "'publish', 'future', 'private'";
Expand All @@ -308,14 +317,23 @@ public function test_convert_existing_slugs( $posts, $terms ) {
$wpdb->terms = 'wp_terms';
$wpdb->term_taxonomy = 'wp_term_taxonomy';

$regexp = $subject::ALLOWED_CHARS_REGEX;
$post_query =
"SELECT ID, post_name, post_type FROM $wpdb->posts " .
'WHERE LOWER(post_name) NOT REGEXP(%s) ' .
"AND ((post_status IN ($post_statuses_in) AND post_type IN ($post_types_in)) OR (post_status = 'inherit' AND post_type = 'attachment'))";
$regexp = $subject::ALLOWED_CHARS_REGEX;

if ( $include_attachment ) {
$post_query =
"SELECT ID, post_name, post_type FROM $wpdb->posts " .
'WHERE LOWER(post_name) NOT REGEXP(%s) ' .
"AND ((post_status IN ($post_statuses_in) AND post_type IN ($post_types_in)) OR (post_status = 'inherit' AND post_type = 'attachment'))";
} else {
$post_query =
"SELECT ID, post_name, post_type FROM $wpdb->posts " .
'WHERE LOWER(post_name) NOT REGEXP(%s) ' .
"AND (post_status IN ($post_statuses_in) AND post_type IN ($post_types_in))";
}

$term_query =
"SELECT t.term_id, slug, tt.taxonomy, tt.term_taxonomy_id FROM $wpdb->terms t, $wpdb->term_taxonomy tt
WHERE LOWER(t.slug) NOT REGEXP(%s) AND tt.term_id = t.term_id";
WHERE LOWER(t.slug) NOT REGEXP(%s) AND tt.taxonomy NOT REGEXP ('^pa_.*$') AND tt.term_id = t.term_id";

$post_query_prepared = str_replace( '%s', "'" . $subject::ALLOWED_CHARS_REGEX . "'", $post_query );
$term_query_prepared = str_replace( '%s', "'" . $subject::ALLOWED_CHARS_REGEX . "'", $term_query );
Expand Down Expand Up @@ -366,8 +384,9 @@ public function test_convert_existing_slugs( $posts, $terms ) {
*/
public function dp_test_convert_existing_slugs() {
return [
[ null, null ],
[ [ 'post1', 'post2' ], [ 'term1', 'term2' ] ],
[ null, null, true ],
[ [ 'post1', 'post2' ], [ 'term1', 'term2' ], true ],
[ [ 'post1', 'post2' ], [ 'term1', 'term2' ], false ],
];
}

Expand Down
11 changes: 6 additions & 5 deletions tests/phpunit/tests/class-test-main.php
Expand Up @@ -416,10 +416,11 @@ public function dp_test_sanitize_title_for_wc_attribute_taxonomy() {
];

return [
'no wc' => [ 'color', false, null, 1 ],
'no attr taxes' => [ 'color', true, [], 1 ],
'not in attr taxes' => [ 'color', true, $attribute_taxonomies, 1 ],
'in attr taxes' => [ 'цвет', true, $attribute_taxonomies, 0 ],
'no wc' => [ 'color', false, null, 1 ],
'no attr taxes' => [ 'color', true, [], 1 ],
'not in attr taxes' => [ 'color', true, $attribute_taxonomies, 1 ],
'in attr taxes' => [ 'цвет', true, $attribute_taxonomies, 0 ],
'in attr taxes with pa_' => [ 'pa_цвет', true, $attribute_taxonomies, 0 ],
];
}

Expand Down Expand Up @@ -567,7 +568,7 @@ function ( $arg ) {
}

/**
* Data provider for test_sanitize_title
* Data provider for test_sanitize_filename
*
* @return array
*/
Expand Down

0 comments on commit 1ce66fd

Please sign in to comment.