Skip to content

Commit

Permalink
Merge pull request #127 from mihdan/V5.5.1
Browse files Browse the repository at this point in the history
V5.5.1
  • Loading branch information
kagg-design committed Mar 21, 2023
2 parents 94ffe3d + 5b8a5e2 commit f174a6f
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 32 deletions.
4 changes: 2 additions & 2 deletions cyr-to-lat.php
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: Cyr-To-Lat
* Plugin URI: https://wordpress.org/plugins/cyr2lat/
* Description: Convert Non-Latin characters in post and term slugs to Latin characters. Useful for creating human-readable URLs. Based on the original plugin by Anton Skorobogatov.
* Version: 5.5.0
* Version: 5.5.1
* Requires at least: 5.1
* Requires PHP: 5.6.20
* Author: Sergey Biryukov, Mikhail Kobzarev, Igor Gergel
Expand Down Expand Up @@ -43,7 +43,7 @@
/**
* Plugin version.
*/
define( 'CYR_TO_LAT_VERSION', '5.5.0' );
define( 'CYR_TO_LAT_VERSION', '5.5.1' );

/**
* Path to the plugin dir.
Expand Down
2 changes: 1 addition & 1 deletion 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: 6.2
Stable tag: 5.5.0
Stable tag: 5.5.1
Requires PHP: 5.6.20
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down
33 changes: 29 additions & 4 deletions src/php/class-main.php
Expand Up @@ -161,10 +161,6 @@ public function __construct() {
* @noinspection PhpUndefinedClassInspection
*/
public function init() {
if ( ! $this->request->is_allowed() ) {
return;
}

if ( $this->request->is_cli() ) {
try {
/**
Expand All @@ -185,6 +181,15 @@ public function init() {
* Init class hooks.
*/
public function init_hooks() {
if ( $this->is_frontend ) {
add_action( 'woocommerce_before_template_part', [ $this, 'woocommerce_before_template_part_filter' ] );
add_action( 'woocommerce_after_template_part', [ $this, 'woocommerce_after_template_part_filter' ] );
}

if ( ! $this->request->is_allowed() ) {
return;
}

add_filter( 'sanitize_title', [ $this, 'sanitize_title' ], 9, 3 );
add_filter( 'sanitize_file_name', [ $this, 'sanitize_filename' ], 10, 2 );
add_filter( 'wp_insert_post_data', [ $this, 'sanitize_post_name' ], 10, 2 );
Expand Down Expand Up @@ -273,6 +278,26 @@ public function sanitize_title( $title, $raw_title = '', $context = '' ) {
return $this->is_wc_attribute_taxonomy( $title ) ? $title : $this->transliterate( $title );
}

/**
* WC before template part filter.
* Add sanitize_title filter to support transliteration of WC attributes on frontend.
*
* @return void
*/
public function woocommerce_before_template_part_filter() {
add_filter( 'sanitize_title', [ $this, 'sanitize_title' ], 9, 3 );
}

/**
* WC after template part filter.
* Remove sanitize_title filter after supporting transliteration of WC attributes on frontend.
*
* @return void
*/
public function woocommerce_after_template_part_filter() {
remove_filter( 'sanitize_title', [ $this, 'sanitize_title' ], 9 );
}

/**
* Check if title is an attribute taxonomy.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/bootstrap.php
Expand Up @@ -39,7 +39,7 @@
/**
* Plugin version.
*/
const CYR_TO_LAT_TEST_VERSION = '5.5.0';
const CYR_TO_LAT_TEST_VERSION = '5.5.1';

/**
* Path to the plugin dir.
Expand Down
87 changes: 63 additions & 24 deletions tests/phpunit/tests/class-test-main.php
Expand Up @@ -132,40 +132,20 @@ function () use ( &$requirements_met ) {
/**
* Test init()
*
* @param bool $allowed Plugin is allowed to work.
*
* @dataProvider dp_test_init
* @throws ReflectionException ReflectionException.
*/
public function test_init( $allowed ) {
public function test_init() {
$request = Mockery::mock( Request::class );
$request->shouldReceive( 'is_cli' )->andReturn( false );
$request->shouldReceive( 'is_allowed' )->andReturn( $allowed );

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );

if ( $allowed ) {
$subject->shouldReceive( 'init_hooks' )->once();
} else {
$subject->shouldReceive( 'init_hooks' )->never();
}
$subject->shouldReceive( 'init_hooks' )->once();

$subject->init();
}

/**
* Data provider for test_init().
*
* @return array
*/
public function dp_test_init() {
return [
[ false ],
[ true ],
];
}

/**
* Test init() with CLI when CLI throws an Exception
*
Expand All @@ -175,7 +155,6 @@ public function dp_test_init() {
public function test_init_with_cli_error() {
$request = Mockery::mock( Request::class );
$request->shouldReceive( 'is_cli' )->andReturn( true );
$request->shouldReceive( 'is_allowed' )->andReturn( true );

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );
Expand All @@ -202,7 +181,6 @@ static function () {
public function test_init_with_cli() {
$request = Mockery::mock( Request::class );
$request->shouldReceive( 'is_cli' )->andReturn( true );
$request->shouldReceive( 'is_allowed' )->andReturn( true );

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );
Expand Down Expand Up @@ -231,7 +209,11 @@ public function test_init_with_cli() {
public function test_init_hooks( $polylang, $sitepress, $frontend ) {
$wpml_locale = 'en_US';

$request = Mockery::mock( Request::class );
$request->shouldReceive( 'is_allowed' )->andReturn( true );

$subject = Mockery::mock( Main::class )->makePartial()->shouldAllowMockingProtectedMethods();
$this->set_protected_property( $subject, 'request', $request );

$this->set_protected_property( $subject, 'is_frontend', $frontend );

Expand Down Expand Up @@ -279,6 +261,7 @@ static function ( $class ) use ( $polylang, $sitepress ) {
);
} else {
WP_Mock::expectFilterNotAdded( 'ctl_locale', [ $subject, 'wpml_locale_filter' ] );
WP_Mock::expectActionNotAdded( 'wpml_language_has_switched', [ $subject, 'wpml_language_has_switched' ] );
}

WP_Mock::expectActionAdded( 'before_woocommerce_init', [ $subject, 'declare_wc_compatibility' ] );
Expand Down Expand Up @@ -308,6 +291,35 @@ public function dp_test_init_hooks() {
];
}

/**
* Test init_hooks()
*
* @throws ReflectionException ReflectionException.
*/
public function test_init_hooks_when_not_allowed() {
$request = Mockery::mock( Request::class );
$request->shouldReceive( 'is_allowed' )->andReturn( false );

$subject = Mockery::mock( Main::class )->makePartial()->shouldAllowMockingProtectedMethods();
$this->set_protected_property( $subject, 'request', $request );

$this->set_protected_property( $subject, 'is_frontend', false );

WP_Mock::expectFilterNotAdded( 'woocommerce_before_template_part', [ $subject, 'woocommerce_before_template_part_filter' ] );
WP_Mock::expectFilterNotAdded( 'woocommerce_after_template_part', [ $subject, 'woocommerce_after_template_part_filter' ] );
WP_Mock::expectFilterNotAdded( 'sanitize_title', [ $subject, 'sanitize_title' ] );
WP_Mock::expectFilterNotAdded( 'sanitize_file_name', [ $subject, 'sanitize_filename' ] );
WP_Mock::expectFilterNotAdded( 'wp_insert_post_data', [ $subject, 'sanitize_post_name' ] );
WP_Mock::expectFilterNotAdded( 'pre_insert_term', [ $subject, 'pre_insert_term_filter' ] );
WP_Mock::expectFilterNotAdded( 'get_terms_args', [ $subject, 'get_terms_args_filter' ] );
WP_Mock::expectFilterNotAdded( 'locale', [ $subject, 'pll_locale_filter' ] );
WP_Mock::expectFilterNotAdded( 'ctl_locale', [ $subject, 'wpml_locale_filter' ] );
WP_Mock::expectActionNotAdded( 'wpml_language_has_switched', [ $subject, 'wpml_language_has_switched' ] );
WP_Mock::expectActionNotAdded( 'before_woocommerce_init', [ $subject, 'declare_wc_compatibility' ] );

$subject->init_hooks();
}

/**
* Test that sanitize_title() does nothing when context is 'query'
*/
Expand Down Expand Up @@ -620,6 +632,33 @@ public function dp_test_sanitize_title_for_wc_attribute_taxonomy() {
];
}

/**
* Test woocommerce_before_template_part_filter().
*
* @return void
*/
public function test_woocommerce_before_template_part_filter() {
$subject = Mockery::mock( Main::class )->makePartial();

WP_Mock::expectFilterAdded( 'sanitize_title', [ $subject, 'sanitize_title' ], 9, 3 );

$subject->woocommerce_before_template_part_filter();
}

/**
* Test woocommerce_after_template_part_filter().
*
* @return void
*/
public function test_woocommerce_after_template_part_filter() {
$subject = Mockery::mock( Main::class )->makePartial();

WP_Mock::userFunction( 'remove_filter' )
->with( 'sanitize_title', [ $subject, 'sanitize_title' ], 9 )->once();

$subject->woocommerce_after_template_part_filter();
}

/**
* Test transliterate()
*
Expand Down

0 comments on commit f174a6f

Please sign in to comment.