Skip to content

Commit

Permalink
Merge 98dd83b into 3aad025
Browse files Browse the repository at this point in the history
  • Loading branch information
kagg-design committed Dec 19, 2021
2 parents 3aad025 + 98dd83b commit e186525
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 21 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.2.4
* Version: 5.2.5
* Requires at least: 5.1
* Requires PHP: 5.6.20
* Author: Sergey Biryukov, Mikhail Kobzarev, Igor Gergel
Expand All @@ -36,7 +36,7 @@
/**
* Plugin version.
*/
define( 'CYR_TO_LAT_VERSION', '5.2.4' );
define( 'CYR_TO_LAT_VERSION', '5.2.5' );

/**
* Path to the plugin dir.
Expand Down
7 changes: 6 additions & 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: 5.8
Stable tag: 5.2.4
Stable tag: 5.2.5
Requires PHP: 5.6.20

Convert Non-Latin characters in post, page and term slugs to Latin characters.
Expand Down Expand Up @@ -188,6 +188,11 @@ Yes you can!

== Changelog ==

= 5.2.5 (19.12.2021) =
* Fix issue with Polylang - do not modify admin language when editing a post.
* Fix issue with JetPack - fatal error on synchronisation.
* Fix 404 on archives created with wpml before activation of cyr2lat.

= 5.2.4 (07.09.2021) =
* Fix issue with not showing WooCommerce variable product attributes.
* Fix issue with Elementor and WPML, endless loop.
Expand Down
4 changes: 4 additions & 0 deletions src/php/Settings/Abstracts/SettingsBase.php
Expand Up @@ -968,6 +968,10 @@ public function load_plugin_textdomain() {
* @return bool
*/
protected function is_options_screen() {
if ( ! function_exists( 'get_current_screen' ) ) {
return false;
}

$current_screen = get_current_screen();

$screen_id = $this->screen_id();
Expand Down
4 changes: 4 additions & 0 deletions src/php/Settings/Tables.php
Expand Up @@ -264,6 +264,10 @@ public function admin_enqueue_scripts() {
* Setup settings sections.
*/
public function setup_sections() {
if ( ! $this->is_options_screen() ) {
return;
}

foreach ( $this->form_fields as $form_field ) {
add_settings_section(
$form_field['section'],
Expand Down
26 changes: 20 additions & 6 deletions src/php/class-main.php
Expand Up @@ -115,6 +115,13 @@ class Main {
*/
protected $wpml_languages;

/**
* Current request is frontend.
*
* @var bool|null
*/
protected $is_frontend;

/**
* Main constructor.
*/
Expand Down Expand Up @@ -142,7 +149,8 @@ public function __construct() {
$this->cli = new WP_CLI( $this->converter );
}

$this->acf = new ACF( $this->settings );
$this->acf = new ACF( $this->settings );
$this->is_frontend = $this->request->is_frontend();
}

/**
Expand Down Expand Up @@ -176,7 +184,7 @@ public function init_hooks() {
add_filter( 'wp_insert_post_data', [ $this, 'sanitize_post_name' ], 10, 2 );
add_filter( 'pre_insert_term', [ $this, 'pre_insert_term_filter' ], PHP_INT_MAX, 2 );

if ( ! $this->request->is_frontend() ) {
if ( ! $this->is_frontend ) {
add_filter( 'get_terms_args', [ $this, 'get_terms_args_filter' ], PHP_INT_MAX, 2 );
}

Expand Down Expand Up @@ -247,12 +255,14 @@ public function sanitize_title( $title, $raw_title = '', $context = '' ) {
}

if ( ! empty( $term ) ) {
$title = $term;
} else {
$title = $this->is_wc_attribute_taxonomy( $title ) ? $title : $this->transliterate( $title );
return $term;
}

return $title;
if ( $this->is_frontend || $this->is_wc_attribute_taxonomy( $title ) ) {
return $title;
}

return $this->transliterate( $title );
}

/**
Expand Down Expand Up @@ -509,6 +519,10 @@ public function pll_locale_filter( $locale ) {
return $locale;
}

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

$pll_get_post_language = $this->pll_locale_filter_with_classic_editor();
if ( $pll_get_post_language ) {
$this->pll_locale = $pll_get_post_language;
Expand Down
14 changes: 14 additions & 0 deletions src/php/class-request.php
Expand Up @@ -88,4 +88,18 @@ protected function get_rest_route() {

return $is_rest ? substr( $current_path, strlen( $rest_path ) ) : '';
}

/**
* If current request is POST.
*
* @return bool
*/
public function is_post() {
$request_method = filter_var(
isset( $_SERVER['REQUEST_METHOD'] ) ? wp_unslash( $_SERVER['REQUEST_METHOD'] ) : '',
FILTER_SANITIZE_STRING
);

return 'POST' === $request_method;
}
}
16 changes: 16 additions & 0 deletions tests/phpunit/Settings/Abstracts/SettingsBaseTest.php
Expand Up @@ -1660,4 +1660,20 @@ public function dp_test_is_options_screen() {
'Plugin screen, main menu page' => [ (object) [ 'id' => 'toplevel_page_cyr-to-lat' ], true, true ],
];
}

/**
* Test is_options_screen() when get_current_screen() does not exist.
*/
public function test_is_options_screen_when_get_current_screen_does_not_exist() {
FunctionMocker::replace(
'function_exists',
static function ( $function ) {
return 'get_current_screen' !== $function;
}
);

$subject = Mockery::mock( SettingsBase::class )->makePartial()->shouldAllowMockingProtectedMethods();

self::assertFalse( $subject->is_options_screen() );
}
}
6 changes: 5 additions & 1 deletion tests/phpunit/Settings/SettingsTest.php
Expand Up @@ -89,7 +89,7 @@ public function test_get() {

$tables = Mockery::mock( Tables::class );
$tables->shouldReceive( 'get' )->andReturnUsing(
function( $key, $empty_value ) use ( $tables_key, $tables_value ) {
function( $key, $empty_value ) use ( $tables_key, &$tables_value ) {
if ( $key === $tables_key ) {
return $tables_value;
}
Expand Down Expand Up @@ -133,6 +133,10 @@ function( $key, $empty_value ) use ( $converter_key, $converter_value ) {

$empty_value = 'empty value';
self::assertSame( $empty_value, $subject->get( 'non-existent key', $empty_value ) );

$tables_value = '';
$empty_value = '';
self::assertSame( $empty_value, $subject->get( $tables_key, $empty_value ) );
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/phpunit/Settings/TablesTest.php
Expand Up @@ -345,6 +345,9 @@ public function test_admin_enqueue_scripts_not_on_own_screen() {
*/
public function test_setup_sections() {
$tab_option_page = 'cyr-to-lat';
$current_screen = (object) [ 'id' => 'settings_page_cyr-to-lat' ];

WP_Mock::userFunction( 'get_current_screen' )->with()->once()->andReturn( $current_screen );

$subject = Mockery::mock( Tables::class )->makePartial()->shouldAllowMockingProtectedMethods();
$subject->shouldReceive( 'option_page' )->andReturn( $tab_option_page );
Expand All @@ -366,4 +369,15 @@ public function test_setup_sections() {

$subject->setup_sections();
}

/**
* Test setup_sections() not on own screen.
*
* @throws ReflectionException ReflectionException.
*/
public function test_setup_sections_not_on_own_screen() {
$subject = Mockery::mock( Tables::class )->makePartial();

$subject->setup_sections();
}
}
2 changes: 1 addition & 1 deletion tests/phpunit/bootstrap.php
Expand Up @@ -37,7 +37,7 @@
/**
* Plugin version.
*/
const CYR_TO_LAT_TEST_VERSION = '5.2.4';
const CYR_TO_LAT_TEST_VERSION = '5.2.5';

/**
* Path to the plugin dir.
Expand Down
55 changes: 45 additions & 10 deletions tests/phpunit/tests/class-test-main.php
Expand Up @@ -59,12 +59,18 @@ public function tearDown(): void {
*/
public function test_constructor() {
$classname = Main::class;
$frontend = false;

// Test when requirements are met.
$requirements_met = true;

$request = Mockery::mock( 'overload:' . Request::class );
$request->shouldReceive( 'is_cli' )->with()->andReturn( true );
$request->shouldReceive( 'is_frontend' )->with()->andReturnUsing(
function () use ( &$frontend ) {
return $frontend;
}
);

Mockery::mock( 'overload:' . Settings::class );
Mockery::mock( 'overload:' . Admin_Notices::class );
Expand Down Expand Up @@ -98,6 +104,7 @@ function () use ( &$requirements_met ) {
self::assertInstanceOf( Converter::class, $this->get_protected_property( $mock, 'converter' ) );
self::assertInstanceOf( WP_CLI::class, $this->get_protected_property( $mock, 'cli' ) );
self::assertInstanceOf( ACF::class, $this->get_protected_property( $mock, 'acf' ) );
self::assertSame( $frontend, $this->get_protected_property( $mock, 'is_frontend' ) );

// Test when requirements are not met.
$requirements_met = false;
Expand All @@ -118,6 +125,7 @@ function () use ( &$requirements_met ) {
self::assertNull( $this->get_protected_property( $mock, 'converter' ) );
self::assertNull( $this->get_protected_property( $mock, 'cli' ) );
self::assertNull( $this->get_protected_property( $mock, 'acf' ) );
self::assertNull( $this->get_protected_property( $mock, 'is_frontend' ) );
}

/**
Expand Down Expand Up @@ -198,12 +206,9 @@ public function test_init_with_cli() {
public function test_init_hooks( $polylang, $sitepress, $frontend ) {
$wpml_locale = 'en_US';

$request = Mockery::mock( Request::class );

$subject = Mockery::mock( Main::class )->makePartial()->shouldAllowMockingProtectedMethods();
$request->shouldReceive( 'is_frontend' )->andReturn( $frontend );

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

WP_Mock::expectFilterAdded( 'sanitize_title', [ $subject, 'sanitize_title' ], 9, 3 );
WP_Mock::expectFilterAdded( 'sanitize_file_name', [ $subject, 'sanitize_filename' ], 10, 2 );
Expand Down Expand Up @@ -493,6 +498,20 @@ public function dp_test_sanitize_title_for_get_terms() {
];
}

/**
* Test sanitize_title() for frontend.
*
* @throws ReflectionException ReflectionException.
*/
public function test_sanitize_title_for_frontend() {
$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'is_frontend', true );

$title = 'some title';

self::assertSame( $title, $subject->sanitize_title( $title ) );
}

/**
* Test sanitize_title() for term WC attribute taxonomy
*
Expand Down Expand Up @@ -956,19 +975,35 @@ function () use ( &$data ) {
}

/**
* Test pll_locale_filter() on allowed request.
* Test pll_locale_filter() on frontend.
*
* @throws ReflectionException ReflectionException.
*/
public function test_pll_locale_filter_on_frontend() {
$locale = 'en_US';

$subject = Mockery::mock( Main::class )->makePartial();

WP_Mock::userFunction( 'is_admin' )->with()->andReturn( false );

self::assertSame( $locale, $subject->pll_locale_filter( $locale ) );
}

/**
* Test pll_locale_filter() on backend GET.
*
* @throws ReflectionException ReflectionException.
*/
public function test_pll_locale_filter_on_backend_get() {
$locale = 'en_US';

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

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

WP_Mock::userFunction( 'is_admin' )->with()->andReturn( false );
WP_Mock::userFunction( 'is_admin' )->with()->andReturn( true );

self::assertSame( $locale, $subject->pll_locale_filter( $locale ) );
}
Expand All @@ -984,7 +1019,7 @@ public function test_pll_locale_filter_with_classic_editor_and_post_id() {
$post_id = 23;

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

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );
Expand Down Expand Up @@ -1028,7 +1063,7 @@ public function test_pll_locale_filter_with_classic_editor_and_pll_post_id() {
$post_id = 23;

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

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );
Expand Down Expand Up @@ -1072,7 +1107,7 @@ public function test_pll_locale_filter_with_classic_editor_and_post() {
$post_id = 23;

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

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );
Expand Down Expand Up @@ -1117,7 +1152,7 @@ public function test_pll_locale_filter_with_term() {
$term_lang_choice = 92;

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

$subject = Mockery::mock( Main::class )->makePartial();
$this->set_protected_property( $subject, 'request', $request );
Expand Down
16 changes: 16 additions & 0 deletions tests/phpunit/tests/class-test-request.php
Expand Up @@ -239,4 +239,20 @@ public function dp_test_get_rest_route() {
'some request' => [ '/some-request', '' ],
];
}

/**
* Test is_post().
*/
public function test_is_post() {
WP_Mock::passthruFunction( 'wp_unslash' );

$subject = new Request();
self::assertFalse( $subject->is_post() );

$_SERVER['REQUEST_METHOD'] = 'some';
self::assertFalse( $subject->is_post() );

$_SERVER['REQUEST_METHOD'] = 'POST';
self::assertTrue( $subject->is_post() );
}
}

0 comments on commit e186525

Please sign in to comment.