From d54a3ea445704befba578d76c3632cc296d25eac Mon Sep 17 00:00:00 2001 From: Sibin Grasic Date: Wed, 11 Nov 2020 11:39:20 +0100 Subject: [PATCH 01/13] Further refactoring --- lib/Core/Bootstrap.php | 1 + lib/Frontend/Core.php | 84 ++++++++++++++---------------------------- lib/Frontend/Fixes.php | 3 +- 3 files changed, 31 insertions(+), 57 deletions(-) diff --git a/lib/Core/Bootstrap.php b/lib/Core/Bootstrap.php index b6071b8..58dffd4 100644 --- a/lib/Core/Bootstrap.php +++ b/lib/Core/Bootstrap.php @@ -91,6 +91,7 @@ public function load_frontend() { new Frontend\Core(); + new Frontend\Fixes(); new Shortcode\Info(); new Shortcode\Cyrilizer(); diff --git a/lib/Frontend/Core.php b/lib/Frontend/Core.php index 635ac41..a494133 100644 --- a/lib/Frontend/Core.php +++ b/lib/Frontend/Core.php @@ -27,39 +27,16 @@ class Core */ private $shortcodes; - /** - * @var array List of actions and hooks to apply the action on - * - * @since 2.4 - */ - private static $actions = [ - 'buffer_start' => ['wp_head', 'rss_head', 'atom_head', 'rdf_head', 'rss2_head'], - 'buffer_end' => ['wp_footer', 'rss_footer', 'atom_footer', 'rdf_footer', 'rss2_footer'] - ]; - - /** - * @var array List of filters and hooks to apply the filter on - * - * @since 2.4 - */ - private static $filters = [ - 'convert_script' => ['gettext', 'ngettext', 'gettext_with_context', 'ngettext_with_context'] - ]; - public function __construct() { if (is_admin()) return; - $this->lm = LM::get_instance(); - - $this->opts = getOptions(); - + $this->lm = LM::get_instance(); + $this->opts = getOptions(); $this->shortcodes = []; - $filter_priority = 9999; - /** * Filters the priorty for transliteration engine * @@ -67,13 +44,25 @@ public function __construct() * * @param filter_priority Integer defining transliterator priority */ - $filter_priority = apply_filters('sgi/stl/filter_priority', $filter_priority); + $filter_priority = apply_filters('sgi/stl/filter_priority', 9999); - - if ( $this->lm->get_script() != 'lat' && !$this->lm->in_serbian) : - return; + if ( $this->lm->get_script() == 'lat' && $this->lm->in_serbian ) : + $this->loadTransliterator($filter_priority); endif; + } + + /** + * Start the transliteration system. + * Function adds all the necessary filters and actions to predefined hooks + * + * @param int $filter_priority Priority on which to load actions and filters + * + * @since 2.4 + */ + public function loadTransliterator(int $filter_priority) + { + add_action('wp_head', [&$this, 'buffer_start'], $filter_priority); add_action('wp_footer', [&$this, 'buffer_end'], $filter_priority); @@ -99,16 +88,23 @@ public function __construct() add_filter('the_content', [&$this, 'change_image_urls'], $filter_priority, 1); endif; - } + /** + * Starts output buffering so we have one large string to transliterate + * + * @since 2.0 + */ public function buffer_start() { - ob_start(); - } + /** + * Ends output buffering and performs transliteration + * + * @since 2.0 + */ public function buffer_end() { @@ -128,33 +124,9 @@ public function buffer_end() } - public function convert_title($title,$sep = '',$location = '') - { - - return $this->convert_script($title); - - } - public function convert_script($content) { - return transliterate($content); - - } - - public function convert_title_parts($title) - { - - $newtitle = []; - - foreach ($title as $part => $value) : - - $newtitle[$part] = $this->convert_script($value); - - endforeach; - - return $newtitle; - } public function change_image_urls($output) diff --git a/lib/Frontend/Fixes.php b/lib/Frontend/Fixes.php index e2f22f9..62521ff 100644 --- a/lib/Frontend/Fixes.php +++ b/lib/Frontend/Fixes.php @@ -3,6 +3,7 @@ namespace SGI\STL\Frontend; use function SGI\STL\Utils\getOptions, + SGI\STL\Utils\get_script, SGI\STL\Utils\transliterate, SGI\STL\Utils\multiscript_sql_query; @@ -36,7 +37,7 @@ public function extend_wpml_ls($languages) return $languages; endif; - $active = $this->lm->get_script(); + $active = get_script(); $cir = $lat = $languages['sr']; From d9286a660bf14cf70bc5682a350407c9dc85bf93 Mon Sep 17 00:00:00 2001 From: Rados Perovic Date: Wed, 11 Nov 2020 14:34:58 +0100 Subject: [PATCH 02/13] modified settingspage --- lib/Admin/Settings/Files.php | 30 +++++++++++++-------------- lib/Admin/Settings/General.php | 24 ++++++++++----------- lib/Admin/SettingsPage.php | 38 +--------------------------------- srbtranslatin.php | 4 ++-- 4 files changed, 30 insertions(+), 66 deletions(-) diff --git a/lib/Admin/Settings/Files.php b/lib/Admin/Settings/Files.php index 66e6a56..1eaaf01 100644 --- a/lib/Admin/Settings/Files.php +++ b/lib/Admin/Settings/Files.php @@ -12,14 +12,14 @@ public function section_files() add_settings_section( 'stl_settings_file', - __('File and Media Settings', DOMAIN), + __('File and Media Settings', 'SrbTransLatin'), [&$this, 'callback_section_files'], 'stl_settings' ); add_settings_field( 'stl_file_names', - __('Fix Filenames', DOMAIN), + __('Fix Filenames', 'SrbTransLatin'), [&$this, 'callback_option_names'], 'stl_settings', 'stl_settings_file', @@ -28,7 +28,7 @@ public function section_files() add_settings_field( 'stl_file_translit', - __('Latin speficic filenames', DOMAIN), + __('Latin speficic filenames', 'SrbTransLatin'), [&$this, 'callback_option_translit'], 'stl_settings', 'stl_settings_file', @@ -40,7 +40,7 @@ public function section_files() add_settings_field( 'stl_file_delim', - __('Filename delimiter', DOMAIN), + __('Filename delimiter', 'SrbTransLatin'), [&$this, 'callback_option_delim'], 'stl_settings', 'stl_settings_file', @@ -49,7 +49,7 @@ public function section_files() // add_settings_field( // 'stl_core_content', - // __('Cookie', DOMAIN), + // __('Cookie', 'SrbTransLatin'), // [&$this, 'callback_option_content'], // 'stl_settings', // 'stl_settings_file', @@ -63,7 +63,7 @@ public function callback_section_files() printf( '

%s

', - __('File & Media settings control filename transliteration and media saving', DOMAIN) + __('File & Media settings control filename transliteration and media saving', 'SrbTransLatin') ); } @@ -74,9 +74,9 @@ public function callback_option_names($names) self::checkbox( $names, 'sgi/stl/opt[file][names]', - $this->expert_enable, - __('Transliterate filenames', DOMAIN), - __('Enable if you want to convert cyrillic filenames to latin', DOMAIN) + true, + __('Transliterate filenames', 'SrbTransLatin'), + __('Enable if you want to convert cyrillic filenames to latin', 'SrbTransLatin') ); } @@ -88,8 +88,8 @@ public function callback_option_translit($opts) $opts['translit'], 'sgi/stl/opt[file][translit]', true, - __('Script specific filenames', DOMAIN), - __('Enable if you want to have cyrillic and latin versions of an image ', DOMAIN) + __('Script specific filenames', 'SrbTransLatin'), + __('Enable if you want to have cyrillic and latin versions of an image ', 'SrbTransLatin') ); echo '
'; @@ -98,8 +98,8 @@ public function callback_option_translit($opts) $opts['names'], 'sgi/stl/opt[file][content]', true, - __('Script specific filenames in content only', DOMAIN), - __('Enable if you want to limit different filenames to post/page content only', DOMAIN) + __('Script specific filenames in content only', 'SrbTransLatin'), + __('Enable if you want to limit different filenames to post/page content only', 'SrbTransLatin') ); echo '
'; @@ -112,9 +112,9 @@ public function callback_option_delim($delim) self::input( $delim, 'sgi/stl/opt[file][delim]', - $this->expert_enable, + false, '', - __('Filename delimiter - i.e. - myfilename-cir.jpg', DOMAIN) + __('Filename delimiter - i.e. - myfilename-cir.jpg', 'SrbTransLatin') ); } diff --git a/lib/Admin/Settings/General.php b/lib/Admin/Settings/General.php index 97466e0..7c188d2 100644 --- a/lib/Admin/Settings/General.php +++ b/lib/Admin/Settings/General.php @@ -12,14 +12,14 @@ public function section_general() add_settings_section( 'stl_settings_core', - __('General Settings', DOMAIN), + __('General Settings', 'SrbTransLatin'), [&$this, 'callback_section_core'], 'stl_settings' ); add_settings_field( 'stl_core_script', - __('Default script', DOMAIN), + __('Default script', 'SrbTransLatin'), [&$this, 'callback_option_script'], 'stl_settings', 'stl_settings_core', @@ -28,7 +28,7 @@ public function section_general() add_settings_field( 'stl_core_param', - __('URL Parameter', DOMAIN), + __('URL Parameter', 'SrbTransLatin'), [&$this, 'callback_option_param'], 'stl_settings', 'stl_settings_core', @@ -37,7 +37,7 @@ public function section_general() add_settings_field( 'stl_core_cookie', - __('Cookie', DOMAIN), + __('Cookie', 'SrbTransLatin'), [&$this, 'callback_option_cookie'], 'stl_settings', 'stl_settings_core', @@ -51,7 +51,7 @@ public function callback_section_core() printf( '

%s

', - __('General settings control main functionality of the plugin', DOMAIN) + __('General settings control main functionality of the plugin', 'SrbTransLatin') ); } @@ -60,8 +60,8 @@ public function callback_option_script($script) { $options = array( - 'cir' => __('Cyrillic', DOMAIN), - 'lat' => __('Latin', DOMAIN) + 'cir' => __('Cyrillic', 'SrbTransLatin'), + 'lat' => __('Latin', 'SrbTransLatin') ); Generator::select( @@ -70,7 +70,7 @@ public function callback_option_script($script) 'sgi/stl/opt[core][script]', true, '', - __('Default script used for the website if user did not select a script', DOMAIN), + __('Default script used for the website if user did not select a script', 'SrbTransLatin'), '' ); @@ -82,9 +82,9 @@ public function callback_option_cookie($cookie) Generator::checkbox( $cookie, 'sgi/stl/opt[core][cookie]', - $this->expert_enable, - __('Use Cookie', DOMAIN), - __('Enable if you want to keep script preference setting in a user cookie', DOMAIN) + true, + __('Use Cookie', 'SrbTransLatin'), + __('Enable if you want to keep script preference setting in a user cookie', 'SrbTransLatin') ); } @@ -97,7 +97,7 @@ public function callback_option_param($param) 'sgi/stl/opt[core][param]', true, '', - __('URL parameter used for script selector', DOMAIN) + __('URL parameter used for script selector', 'SrbTransLatin') ); } diff --git a/lib/Admin/SettingsPage.php b/lib/Admin/SettingsPage.php index 78c8e9d..f09d142 100644 --- a/lib/Admin/SettingsPage.php +++ b/lib/Admin/SettingsPage.php @@ -25,23 +25,13 @@ class SettingsPage private $opts; - private $expert_enable; + public function __construct() { $this->opts = getOptions(); - /** - * Enables changing of expert settings - * - * @since 2.0.0 - * - * @param expert_enable - Flag which enables expert settings - */ - $expert_enable = apply_filters("sgi/stl/settings/expert", false); - - $this->expert_enable = $expert_enable; add_action('admin_init', [&$this, 'register_settings']); @@ -95,35 +85,13 @@ public function sanitize_opts($opts) ] ]; - $expert_settings = [ - 'cookie', - 'names', - 'delim', - 'permalinks' - ]; foreach ($checkboxes as $section => $array) : foreach ($array as $opt) : - if (in_array($opt, $expert_settings)) : - - if ($this->expert_enable) : - - $opts[$section][$opt] = (isset($opts[$section][$opt])) ? true : false; - - else : - - $opts[$section][$opt] = $prev_config[$section][$opt]; - - endif; - - else : - $opts[$section][$opt] = (isset($opts[$section][$opt])) ? true : false; - endif; - if ($opt == 'permalinks') : $wplang = get_locale(); @@ -145,12 +113,8 @@ public function sanitize_opts($opts) endforeach; - if (!$this->expert_enable) : - $opts['file']['delim'] = $prev_config['file']['delim']; - endif; - return $opts; } diff --git a/srbtranslatin.php b/srbtranslatin.php index 503a628..8933f01 100644 --- a/srbtranslatin.php +++ b/srbtranslatin.php @@ -33,10 +33,10 @@ function run_stl() global $wp_version; if (version_compare( PHP_VERSION, '7.0.0', '<' )) - throw new \Exception(__('STL - Serbian Latinisation plugin requires PHP 7.0 or greater.', SrbTransLatin)); + throw new \Exception(__('STL - Serbian Latinisation plugin requires PHP 7.0 or greater.', 'SrbTransLatin')); if (version_compare($wp_version, '4.8', '<')) - throw new \Exception(__('STL - Serbian Latinisation plugin requires WordPress 4.8.0.', SrbTransLatin)); + throw new \Exception(__('STL - Serbian Latinisation plugin requires WordPress 4.8.0.', 'SrbTransLatin')); STL(); From 28ac758784b9761c2c07dc8eb2cd40d78c4ccbcc Mon Sep 17 00:00:00 2001 From: Sibin Grasic Date: Thu, 12 Nov 2020 19:56:21 +0100 Subject: [PATCH 03/13] Fixed TinyMCE and settings --- lib/Admin/Settings/General.php | 9 ---- lib/Admin/Settings/Generator.php | 6 +-- lib/Admin/TinyMCE.php | 14 ++++++- lib/Core/Bootstrap.php | 15 ------- lib/Core/LanguageManager.php | 25 ++--------- lib/Shortcode/Translator.php | 2 - lib/Update/Handler.php | 2 - src/js/tinymce/stl_cyr.js | 71 +++++++++++++++++++++++++++----- 8 files changed, 80 insertions(+), 64 deletions(-) diff --git a/lib/Admin/Settings/General.php b/lib/Admin/Settings/General.php index 7c188d2..458ea58 100644 --- a/lib/Admin/Settings/General.php +++ b/lib/Admin/Settings/General.php @@ -35,15 +35,6 @@ public function section_general() $this->opts['core']['param'] ); - add_settings_field( - 'stl_core_cookie', - __('Cookie', 'SrbTransLatin'), - [&$this, 'callback_option_cookie'], - 'stl_settings', - 'stl_settings_core', - $this->opts['core']['cookie'] - ); - } public function callback_section_core() diff --git a/lib/Admin/Settings/Generator.php b/lib/Admin/Settings/Generator.php index 93c2c2e..8b7e4ab 100644 --- a/lib/Admin/Settings/Generator.php +++ b/lib/Admin/Settings/Generator.php @@ -12,7 +12,7 @@ public static function select(array $options, string $value, string $name, bool ' + + + + + + + \ No newline at end of file From d0349a4afeb0378ff40c658662a1729edfbd0b6e Mon Sep 17 00:00:00 2001 From: Rados Perovic Date: Fri, 13 Nov 2020 11:29:55 +0100 Subject: [PATCH 07/13] added fix ajax option in advanced settings --- lib/Admin/Settings/Fixes.php | 22 ++++++++++++++++++++++ lib/Admin/SettingsPage.php | 3 ++- lib/Utils/Global.php | 1 + 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/Admin/Settings/Fixes.php b/lib/Admin/Settings/Fixes.php index 677c915..db99d29 100644 --- a/lib/Admin/Settings/Fixes.php +++ b/lib/Admin/Settings/Fixes.php @@ -45,6 +45,15 @@ public function callback_section_fixes() $this->opts['fixes']['search'] ); + add_settings_field( + 'stl_fixes_ajax', + __('Fix Ajax', DOMAIN), + [&$this, 'callback_option_ajax'], + 'stl_settings', + 'stl_settings_fixes', + $this->opts['fixes']['ajax'] + ); + } public function callback_option_permalinks($permalinks) @@ -89,4 +98,17 @@ public function callback_option_search($search) } + public function callback_option_ajax($ajax) + { + + self::checkbox( + $ajax, + 'sgi/stl/opt[fixes][ajax]', + true, + __('Enable transliterate ajax calls', DOMAIN), + __('Transliterate ajax calls', DOMAIN) + ); + + } + } \ No newline at end of file diff --git a/lib/Admin/SettingsPage.php b/lib/Admin/SettingsPage.php index f09d142..e396637 100644 --- a/lib/Admin/SettingsPage.php +++ b/lib/Admin/SettingsPage.php @@ -78,7 +78,8 @@ public function sanitize_opts($opts) ], 'fixes' => [ 'permalinks', - 'search' + 'search', + 'ajax' ], 'ml' => [ 'wpml' diff --git a/lib/Utils/Global.php b/lib/Utils/Global.php index 4721653..803f81d 100644 --- a/lib/Utils/Global.php +++ b/lib/Utils/Global.php @@ -31,6 +31,7 @@ function getDefaultOptions() : array 'fixes' =>[ 'permalinks' => (get_option('WPLANG') == 'sr_RS') ? false : true, 'search' => true, + 'ajax' => false, ], 'menu' => [ 'extend' => true, From 620071ef959d4507708c9eb98698158eeaa49719 Mon Sep 17 00:00:00 2001 From: Sibin Grasic Date: Fri, 13 Nov 2020 11:43:23 +0100 Subject: [PATCH 08/13] Fixed default option handling and ajax translit --- lib/Admin/Settings/Fixes.php | 32 ++++++++++++++++++-------------- lib/Ajax/Engine.php | 2 +- lib/Utils/Global.php | 36 ++++++++++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/lib/Admin/Settings/Fixes.php b/lib/Admin/Settings/Fixes.php index db99d29..1675496 100644 --- a/lib/Admin/Settings/Fixes.php +++ b/lib/Admin/Settings/Fixes.php @@ -2,8 +2,6 @@ namespace SGI\STL\Admin\Settings; -use const SGI\STL\DOMAIN; - trait Fixes { @@ -12,7 +10,7 @@ public function section_fixes() add_settings_section( 'stl_settings_fixes', - __('Advanced Settings', DOMAIN), + __('Advanced Settings', 'SrbTransLatin'), [&$this, 'callback_section_fixes'], 'stl_settings' ); @@ -24,12 +22,12 @@ public function callback_section_fixes() printf( '

%s

', - __('Advanced settings control permalink and search settings', DOMAIN) + __('Advanced settings control permalink and search settings', 'SrbTransLatin') ); add_settings_field( 'stl_fixes_permalinks', - __('Fix Permalinks', DOMAIN), + __('Fix Permalinks', 'SrbTransLatin'), [&$this, 'callback_option_permalinks'], 'stl_settings', 'stl_settings_fixes', @@ -38,7 +36,7 @@ public function callback_section_fixes() add_settings_field( 'stl_fixes_search', - __('Fix Search', DOMAIN), + __('Fix Search', 'SrbTransLatin'), [&$this, 'callback_option_search'], 'stl_settings', 'stl_settings_fixes', @@ -47,7 +45,7 @@ public function callback_section_fixes() add_settings_field( 'stl_fixes_ajax', - __('Fix Ajax', DOMAIN), + __('Fix Ajax', 'SrbTransLatin'), [&$this, 'callback_option_ajax'], 'stl_settings', 'stl_settings_fixes', @@ -67,8 +65,8 @@ public function callback_option_permalinks($permalinks) $permalinks, 'sgi/stl/opt[fixes][permalinks]', !$already_cyrillic, - __('Transliterate permalinks to latin script', DOMAIN), - __('Enable if you want to change cyrillic permalinks to latin', DOMAIN) + __('Transliterate permalinks to latin script', 'SrbTransLatin'), + __('Enable if you want to change cyrillic permalinks to latin', 'SrbTransLatin') ); if ($already_cyrillic) : @@ -78,7 +76,7 @@ public function callback_option_permalinks($permalinks) __(sprintf( 'This option is currently disabled because your current locale is set to %s which will automatically change permalnks', $wplang - ), DOMAIN) + ), 'SrbTransLatin') ); endif; @@ -92,8 +90,8 @@ public function callback_option_search($search) $search, 'sgi/stl/opt[fixes][search]', true, - __('Enable search using latin script', DOMAIN), - __('Enables searching for posts using both cyrillic and latin script', DOMAIN) + __('Enable search using latin script', 'SrbTransLatin'), + __('Enables searching for posts using both cyrillic and latin script', 'SrbTransLatin') ); } @@ -105,8 +103,14 @@ public function callback_option_ajax($ajax) $ajax, 'sgi/stl/opt[fixes][ajax]', true, - __('Enable transliterate ajax calls', DOMAIN), - __('Transliterate ajax calls', DOMAIN) + __('Transliterate ajax calls', 'SrbTransLatin'), + __('Enable if you want to transliterate ajax calls on your website', 'SrbTransLatin') + ); + + printf( + '

%s%s

', + __('This functionality is in BETA. Test your website thoroughly when enabling.', 'SrbTransLatin'), + __('If you have problems, open a support ticket', 'SrbTransLatin') ); } diff --git a/lib/Ajax/Engine.php b/lib/Ajax/Engine.php index 725949d..4cedcff 100644 --- a/lib/Ajax/Engine.php +++ b/lib/Ajax/Engine.php @@ -50,7 +50,7 @@ public function __construct() $filter_priority = apply_filters('sgi/stl/filter_priority', 9999); - if ( $this->lm->get_script() == 'lat' && $this->lm->in_serbian ) : + if ( $this->lm->get_script() == 'lat' && $this->lm->in_serbian && $this->opts['fixes']['ajax']) : $this->loadTransliterator($filter_priority); endif; diff --git a/lib/Utils/Global.php b/lib/Utils/Global.php index 803f81d..e967ba3 100644 --- a/lib/Utils/Global.php +++ b/lib/Utils/Global.php @@ -5,6 +5,33 @@ use SGI\STL\Core\LanguageManager as LM, SGI\Transliterator as Transliterator; +/** + * Emulates wp_parse_args for multidimensional arrays + * + * @param array $a Options array + * @param array $b Default options array + * @return array Merged options array + * + * @since 2.4 + */ +function extendedParseArgs(array &$a, array $b) : array +{ + $result = $b; + + foreach ($a as $k => &$v) : + + if ( is_array($v) && isset($result[$k]) ) : + $result[$k] = extendedParseArgs($v, $result[$k]); + continue; + endif; + + $result[$k] = $v; + + endforeach; + + return $result; +} + /** * Returns default plugin options * @@ -58,10 +85,11 @@ function getDefaultOptions() : array function getOptions() : array { - return get_option( - 'sgi/stl/opt', - getDefaultOptions() - ); + $opts = get_option('sgi/stl/opt',[]); + $defaults = getDefaultOptions(); + + + return extendedParseArgs($opts, $defaults); } From 839be76d8d74e3461c075e8280a112ecb6e0cd1b Mon Sep 17 00:00:00 2001 From: Sibin Grasic Date: Fri, 13 Nov 2020 11:47:56 +0100 Subject: [PATCH 09/13] Fixed permalink transliteration --- lib/Admin/Core.php | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/lib/Admin/Core.php b/lib/Admin/Core.php index ed09a05..9dccf78 100644 --- a/lib/Admin/Core.php +++ b/lib/Admin/Core.php @@ -34,7 +34,8 @@ public function __construct() //Filename transliteration - //add_filter('sanitize_file_name', array(&$this, 'sanitize_file_name'),50,2); + add_filter('sanitize_file_name', array(&$this, 'sanitizeFilename'),50,2); + add_filter('sanitize_title', [&$this, 'sanitizeTitle'], 100, 2); } @@ -74,18 +75,6 @@ public function pluginMeta($plugin_meta, $plugin_file) } - public function sanitize_file_name($filename, $filename_raw) - { - - if (!$this->opts['file']['names']) - return $filename; - - $filename = Transliterator::cir_to_cut_lat($filename); - - return $filename; - - } - public function addMenuPages() { @@ -131,5 +120,27 @@ public function settings_page() echo ''; } + public function sanitizeFilename($filename, $filename_raw) + { + + if (!$this->opts['file']['names']) + return $filename; + + $filename = Transliterator::cir_to_cut_lat($filename); + + return $filename; + + } + + public function sanitizeTitle($title, $fallback_title) + { + + if (!$this->opts['fixes']['permalinks']) + return $title; + + return Transliterator::cir_to_cut_lat(($title)); + + } + } \ No newline at end of file From f2bb2efc7025bf0170c608d4f992a4764eb2b323 Mon Sep 17 00:00:00 2001 From: Sibin Grasic Date: Fri, 13 Nov 2020 12:51:14 +0100 Subject: [PATCH 10/13] Transliterate search query --- lib/Frontend/Fixes.php | 21 +++----------- lib/Utils/Global.php | 64 +++++++++++++----------------------------- readme.txt | 19 +++++++++---- 3 files changed, 37 insertions(+), 67 deletions(-) diff --git a/lib/Frontend/Fixes.php b/lib/Frontend/Fixes.php index 62521ff..c1d1902 100644 --- a/lib/Frontend/Fixes.php +++ b/lib/Frontend/Fixes.php @@ -5,7 +5,7 @@ use function SGI\STL\Utils\getOptions, SGI\STL\Utils\get_script, SGI\STL\Utils\transliterate, - SGI\STL\Utils\multiscript_sql_query; + SGI\STL\Utils\modifySearchQuery; class Fixes { @@ -75,24 +75,11 @@ public function extend_wpml_ls($languages) public function fix_search(string $search, \WP_Query $query) { - if (is_admin()) - return $search; - - if (!$this->opts['fixes']['search']) - return $search; - - if ( !$query->is_main_query()) - return $search; - - $g = $_GET['s'] ?? ''; - - if ($g == '') - return $search; - - if (!is_search()) + if ( !$this->opts['fixes']['search'] || empty($search) ) : return $search; + endif; - return multiscript_sql_query($search); + return modifySearchQuery($search); } diff --git a/lib/Utils/Global.php b/lib/Utils/Global.php index e967ba3..9e0340d 100644 --- a/lib/Utils/Global.php +++ b/lib/Utils/Global.php @@ -152,58 +152,34 @@ function reverse_transliterate($content) } -function multiscript_sql_query($search) +/** + * Enables searching for cyrilic post title/content using latin script + * + * @param string $search Search string to modify + * @return string SQL used in the WHERE clause of \WP_Query + * + * @since 2.0 + */ +function modifySearchQuery(string $search) : string { global $wpdb; $search_term = $_GET['s']; - if (preg_match('/\s/',$search_term)) : - - $search_term = rtrim($search_term); - $search_expl = explode(' ', $search_term); - - $search = 'AND ('; $first = true; - - foreach ($search_expl as $word ) : - - if (!$first) : - - $search .= ' AND '; - - endif; - $first = false; - - $lat_word = reverse_transliterate($word); - - $search .= sprintf( - "(%s.post_title LIKE '%%%s%%' OR %s.post_title LIKE '%%%s%%' OR %s.post_content LIKE '%%%s%%' OR %s.post_content LIKE '%%%s%%')", - $wpdb->posts, - $word, - $wpdb->posts, - $lat_word, - $wpdb->posts, - $word, - $wpdb->posts, - $lat_word - ); - - endforeach; - - $search .= ')'; - - else : - - $search_term = ' \'%'.$_GET['s'].'%\''; - $lat_search_term = reverse_transliterate($search_term); - - $search = " AND ( ({$wpdb->posts}.post_title LIKE ${lat_search_term} OR {$wpdb->posts}.post_title LIKE ${search_term} OR {$wpdb->posts}.post_content LIKE ${lat_search_term} OR {$wpdb->posts}.post_content LIKE ${search_term} ) ) "; + $search_term = rtrim(ltrim($search)); - endif; + $search_like_orig = '%' . $wpdb->esc_like($search_term) . '%'; + $search_like_tran = '%' . $wpdb->esc_like( transliterate($search_term) ) . '%'; - //var_dump($search); + $query = $wpdb->prepare( + " AND ({$wpdb->posts}.post_title LIKE %s OR {$wpdb->posts}.post_title LIKE %s OR {$wpdb->posts}.post_content LIKE %s OR {$wpdb->posts}.post_content LIKE %s)", + $search_like_orig, + $search_like_tran, + $search_like_orig, + $search_like_tran + ); - return $search; + return $query; } \ No newline at end of file diff --git a/readme.txt b/readme.txt index ebdeb31..3cee278 100644 --- a/readme.txt +++ b/readme.txt @@ -25,20 +25,23 @@ This plugin also fixes searching cyrillic posts using latin script. * Works everywhere - Plugin hooks into WordPress core transliterating your content inplace * **Auto-Fix permalinks** - Your cyrillic permalinks will be automatically saved as latin (optional) * **SEARCH FIX** - Search posts written in cyrillic script using both latin and cyrillic script -* Lightweight - Plugin does not use any external stylesheets, or js files. * **Partial transliteration** - You can selectively choose which parts of your website to keep in cyrilic -* WPML Compatible - Fully compatible with WPML. Transliteration only works on Serbian portion of your website -* Script / Language selector - Integrates into WPML language selectors in menu / widget * **Widget Ready** - Switch script via sidebar widget +* **Ajax Transliteration** - Transliterates dynamic parts of your website +* SEO Friendly - no double content or SERP penalties! +* Lightweight - Plugin does not use any external stylesheets, or js files. +* WPML Compatible - Fully compatible with WPML. Transliteration only works when Serbian language is active +* Polylang compatible - Fully compatible with Polylang. Transliteration only works when Serbian language is active +* Script / Language selector - Integrates into WPML language selectors in menu / widget == Documentation == -If you can't find your anwsers in the FAQ below, documentation can be found [here](https://rtfm.sgi.io/srbtranslatin) +If you can't find your anwsers in the FAQ below, documentation can be found [here](https://rtfm.oblak.studio/srbtranslatin) == Authorship == Original version of this plugin was developer by [Predrag Supurović](https://pedja.supurovic.net/). -Plugin development was handed over to [me](https://sgi.io) in march 2020. Since then, I am the sole author and maintainer of the plugin +Plugin development was handed over to [me](https://oblak.studio) in march 2020. Since then, I am the sole author and maintainer of the plugin == Installation == @@ -88,9 +91,13 @@ I'm working on a tool which will convert filenames and post / page content on yo Temporary fix: redownload all attachments with cyrilic filenames and reupload them. This plugin will do an on-the-fly conversion of filenames. += Will this plugin transliterate dynamic content (ajax) = + +Yes it will. Option was added in version 2.4 + = Some parts of my page aren't being transliterated = -Depending on the theme / plugins you're currently using, some content might be generated via javascript / ajax. This plugin works fully on the backend side (content is being transliterated via PHP), so it cannot influence JS generated content and content pulled via ajax calls. +Depending on the theme / plugins you're currently using, some content might be generated via REST API (wp-json). I'm working on adding the functionality to transliterate REST calls Feel free to contact me via e-mail, and I'll see if I can assist you for your specific case. From 5e19eabe2cf3ef15e3630c8199dced4531ca03f4 Mon Sep 17 00:00:00 2001 From: Sibin Grasic Date: Fri, 13 Nov 2020 12:58:30 +0100 Subject: [PATCH 11/13] Search fixes --- lib/Utils/Global.php | 8 ++++++-- readme.txt | 8 ++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/Utils/Global.php b/lib/Utils/Global.php index 9e0340d..9e400e9 100644 --- a/lib/Utils/Global.php +++ b/lib/Utils/Global.php @@ -167,10 +167,10 @@ function modifySearchQuery(string $search) : string $search_term = $_GET['s']; - $search_term = rtrim(ltrim($search)); + $search_term = rtrim(ltrim($search_term)); $search_like_orig = '%' . $wpdb->esc_like($search_term) . '%'; - $search_like_tran = '%' . $wpdb->esc_like( transliterate($search_term) ) . '%'; + $search_like_tran = '%' . $wpdb->esc_like( reverse_transliterate($search_term) ) . '%'; $query = $wpdb->prepare( " AND ({$wpdb->posts}.post_title LIKE %s OR {$wpdb->posts}.post_title LIKE %s OR {$wpdb->posts}.post_content LIKE %s OR {$wpdb->posts}.post_content LIKE %s)", @@ -180,6 +180,10 @@ function modifySearchQuery(string $search) : string $search_like_tran ); + echo '
';
+    var_dump($search_term, $query);
+    echo '
'; + return $query; } \ No newline at end of file diff --git a/readme.txt b/readme.txt index 3cee278..2dccb44 100644 --- a/readme.txt +++ b/readme.txt @@ -2,10 +2,10 @@ Contributors: seebeen Donate link: https://sgi.io/donate Tags: letter, serbian, cyrillic, latin, transliteration, latinisation, script, multilanguage, wpml-compatible -Requires at least: 4.0 -Tested up to: 5.3.2 -Requires PHP: 7.0 -Stable tag: 2.0.1 +Requires at least: 5.3 +Tested up to: 5.5 +Requires PHP: 7.2 +Stable tag: 2.4 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html From 95f0913d4eace80f1d7dce79799fef1c2f655607 Mon Sep 17 00:00:00 2001 From: Sibin Grasic Date: Fri, 13 Nov 2020 13:54:09 +0100 Subject: [PATCH 12/13] Added doc blocks --- lib/Ajax/Engine.php | 1 - lib/Utils/Admin.php | 11 +---------- lib/Utils/Frontend.php | 35 ++++++++++++++++++++++++++--------- lib/Utils/Global.php | 28 ++++++++++++++++------------ 4 files changed, 43 insertions(+), 32 deletions(-) diff --git a/lib/Ajax/Engine.php b/lib/Ajax/Engine.php index 4cedcff..a4c643c 100644 --- a/lib/Ajax/Engine.php +++ b/lib/Ajax/Engine.php @@ -49,7 +49,6 @@ public function __construct() */ $filter_priority = apply_filters('sgi/stl/filter_priority', 9999); - if ( $this->lm->get_script() == 'lat' && $this->lm->in_serbian && $this->opts['fixes']['ajax']) : $this->loadTransliterator($filter_priority); endif; diff --git a/lib/Utils/Admin.php b/lib/Utils/Admin.php index d327900..0962645 100644 --- a/lib/Utils/Admin.php +++ b/lib/Utils/Admin.php @@ -25,13 +25,4 @@ function getVersion() return $installed_ver; -} - -function get_settings_template(string $section) -{ - - $template_file = PATH."templates/settings/{$section}.php"; - - include $template_file; - -} +} \ No newline at end of file diff --git a/lib/Utils/Frontend.php b/lib/Utils/Frontend.php index cd8bbc2..c40db5a 100644 --- a/lib/Utils/Frontend.php +++ b/lib/Utils/Frontend.php @@ -26,7 +26,12 @@ function get_selector_template (string $template, array $args) } -function get_current_url() +/** + * Returns current url on the website + * + * @return string Current URL + */ +function get_current_url() : string { global $wp; @@ -40,13 +45,27 @@ function get_current_url() } +/** + * Function which displays the script selector. + * Used by the STL Widget, but can be used anywhere on the website + * + * @param array $args Options for the script selector + * @param mixed|null $base_url Deprecated - Base URL for the links + * @param mixed|null $depr_cyr_caption Deprecated - Text to use for cyrillic link + * @param mixed|null $depr_lat_caption Deprecated - Text to use for latin link + * @param mixed|null $depr_inactive Deprecated - Show inactive link? + * @param mixed|null $depr_wpml Deprecated - Unknown WPML flag + * + * @since 2.0 + * @todo Remove deprecated arguments in 2.6 + */ function script_selector( - $args = [], - $base_url = null, - $depr_cyr_caption = null, - $depr_lat_caption = null, - $depr_inactive = null, - $depr_wpml = null + array $args = [], + $base_url = null, + $depr_cyr_caption = null, + $depr_lat_caption = null, + $depr_inactive = null, + $depr_wpml = null ) { if (!is_array($args)) : @@ -106,8 +125,6 @@ function script_selector( $html .= get_selector_template('oneline', $args); break; - - endswitch; $html .= ''; diff --git a/lib/Utils/Global.php b/lib/Utils/Global.php index 9e400e9..45b076e 100644 --- a/lib/Utils/Global.php +++ b/lib/Utils/Global.php @@ -95,9 +95,7 @@ function getOptions() : array function get_script() { - return LM::get_instance()->get_script(); - } function get_script_param() @@ -112,9 +110,7 @@ function is_serbian() function is_wpml_active() { - return LM::is_wpml_active(); - } function is_cyrillic() @@ -124,9 +120,7 @@ function is_cyrillic() function is_latin() { - return (get_script() == 'lat') ? true : false; - } function get_query_param() @@ -134,7 +128,14 @@ function get_query_param() return getOptions()['core']['param']; } -function transliterate($content, $cut = false) +/** + * Main transliteration function which converts cyrillic script to latin + * + * @param null|string $content String to transliterate + * @param bool $cut Flag determining if transliteration is done to "cut" latin script + * @return null|string Transliterated script + */ +function transliterate(?string $content, bool $cut = false) { return (!$cut) ? @@ -145,7 +146,14 @@ function transliterate($content, $cut = false) } -function reverse_transliterate($content) +/** + * Reverse transliteration function - Transliterates content from latin to cyrillic + * + * @param mixed $content Content to perform reverse transliteration on + * + * @return string Reverse-transliterated content + */ +function reverse_transliterate($content) : string { return Transliterator::lat_to_cir($content); @@ -180,10 +188,6 @@ function modifySearchQuery(string $search) : string $search_like_tran ); - echo '
';
-    var_dump($search_term, $query);
-    echo '
'; - return $query; } \ No newline at end of file From f5f6b7028619013671e0e53cccda5f5841919963 Mon Sep 17 00:00:00 2001 From: Sibin Grasic Date: Fri, 13 Nov 2020 14:02:11 +0100 Subject: [PATCH 13/13] Fixed translations --- lib/Admin/Core.php | 16 +++++++------- lib/Admin/Settings/Menu.php | 28 ++++++++++++------------- lib/Admin/Settings/Multilanguage.php | 12 +++++------ lib/Widget/Selector.php | 31 ++++++++++++++-------------- 4 files changed, 40 insertions(+), 47 deletions(-) diff --git a/lib/Admin/Core.php b/lib/Admin/Core.php index 9dccf78..15e6a42 100644 --- a/lib/Admin/Core.php +++ b/lib/Admin/Core.php @@ -4,8 +4,6 @@ use const SGI\STL\{ BASENAME, - VERSION, - DOMAIN, PATH }; @@ -45,7 +43,7 @@ public function pluginLinks($links) $links[] = sprintf( '%s', admin_url('admin.php?page=stl_settings'), - __('Settings', DOMAIN) + __('Settings', 'SrbTransLatin') ); return $links; @@ -62,13 +60,13 @@ public function pluginMeta($plugin_meta, $plugin_file) $plugin_meta[] = sprintf( '%s', 'https://sgi.io/plugins/srbtranslatin', - __('Documentation', DOMAIN) + __('Documentation', 'SrbTransLatin') ); $plugin_meta[] = sprintf( '%s', 'https://paypal.me/seebeen', - __('Donate', DOMAIN) + __('Donate', 'SrbTransLatin') ); return $plugin_meta; @@ -81,8 +79,8 @@ public function addMenuPages() $image = file_get_contents(PATH . 'assets/img/stl-logo.svg'); add_menu_page( - __('Latinisation', DOMAIN), - __('Latinisation', DOMAIN), + __('Latinisation', 'SrbTransLatin'), + __('Latinisation', 'SrbTransLatin'), 'manage_options', 'stl', function(){}, @@ -92,8 +90,8 @@ function(){}, add_submenu_page( 'stl', - __('Settings - SrbTransLatin', DOMAIN), - __('Settings', DOMAIN), + __('Settings - SrbTransLatin', 'SrbTransLatin'), + __('Settings', 'SrbTransLatin'), 'manage_options', 'stl_settings', [&$this, 'settings_page'] diff --git a/lib/Admin/Settings/Menu.php b/lib/Admin/Settings/Menu.php index e8d618f..358742a 100644 --- a/lib/Admin/Settings/Menu.php +++ b/lib/Admin/Settings/Menu.php @@ -2,8 +2,6 @@ namespace SGI\STL\Admin\Settings; -use const SGI\STL\DOMAIN; - use function SGI\STL\Utils\is_wpml_active; trait Menu @@ -13,7 +11,7 @@ public function section_menu() { add_settings_section( 'stl_settings_menu', - __('Menu Settings', DOMAIN), + __('Menu Settings', 'SrbTransLatin'), [&$this, 'callback_section_menu'], 'stl_settings' ); @@ -24,12 +22,12 @@ public function callback_section_menu() printf( '

%s

', - __('Menu settings control extending and tweaking the script selector in theme menus', DOMAIN) + __('Menu settings control extending and tweaking the script selector in theme menus', 'SrbTransLatin') ); add_settings_field( 'stl_menu_enable', - __('Extend Nav Menu', DOMAIN), + __('Extend Nav Menu', 'SrbTransLatin'), [&$this, 'callback_option_extend'], 'stl_settings', 'stl_settings_menu', @@ -38,7 +36,7 @@ public function callback_section_menu() add_settings_field( 'stl_menu_selector', - __('Nav Menu to extend', DOMAIN), + __('Nav Menu to extend', 'SrbTransLatin'), [&$this, 'callback_option_selector'], 'stl_settings', 'stl_settings_menu', @@ -47,7 +45,7 @@ public function callback_section_menu() add_settings_field( 'stl_menu_type', - __('Selector type', DOMAIN), + __('Selector type', 'SrbTransLatin'), [&$this, 'callback_option_type'], 'stl_settings', 'stl_settings_menu', @@ -56,7 +54,7 @@ public function callback_section_menu() add_settings_field( 'stl_menu_label', - __('Menu Label', DOMAIN), + __('Menu Label', 'SrbTransLatin'), [&$this, 'callback_option_label'], 'stl_settings', 'stl_settings_menu', @@ -74,8 +72,8 @@ public function callback_option_extend($extend) $extend, 'sgi/stl/opt[menu][extend]', !$enable_extend, - __('Extend menu with script selector', DOMAIN), - __('Enables adding script selector to nav menus', DOMAIN) + __('Extend menu with script selector', 'SrbTransLatin'), + __('Enables adding script selector to nav menus', 'SrbTransLatin') ); } @@ -91,7 +89,7 @@ public function callback_option_selector($menu) 'sgi/stl/opt[menu][selector]', true, '', - __('Menu to extend with script selector', DOMAIN), + __('Menu to extend with script selector', 'SrbTransLatin'), '' ); @@ -101,8 +99,8 @@ public function callback_option_type($type) { $options = [ - 'dropdown' => __('Dropdown', DOMAIN), - 'inline' => __('Inline', DOMAIN) + 'dropdown' => __('Dropdown', 'SrbTransLatin'), + 'inline' => __('Inline', 'SrbTransLatin') ]; Generator::select( @@ -111,7 +109,7 @@ public function callback_option_type($type) 'sgi/stl/opt[menu][type]', true, '', - __('Menu to extend with script selector', DOMAIN), + __('Menu to extend with script selector', 'SrbTransLatin'), '' ); @@ -125,7 +123,7 @@ public function callback_option_label($label) 'sgi/stl/opt[menu][label]', true, '', - __('Label used for the menu dropdown', DOMAIN) + __('Label used for the menu dropdown', 'SrbTransLatin') ); } diff --git a/lib/Admin/Settings/Multilanguage.php b/lib/Admin/Settings/Multilanguage.php index 46b2d6c..db6fe6c 100644 --- a/lib/Admin/Settings/Multilanguage.php +++ b/lib/Admin/Settings/Multilanguage.php @@ -2,8 +2,6 @@ namespace SGI\STL\Admin\Settings; -use const SGI\STL\DOMAIN; - trait Multilanguage { @@ -12,7 +10,7 @@ public function section_ml() add_settings_section( 'stl_settings_ml', - __('Multilanguage Settings', DOMAIN), + __('Multilanguage Settings', 'SrbTransLatin'), [&$this, 'callback_section_ml'], 'stl_settings' ); @@ -24,12 +22,12 @@ public function callback_section_ml() printf( '

%s

', - __('Multilanguage settings control settings for language switchers', DOMAIN) + __('Multilanguage settings control settings for language switchers', 'SrbTransLatin') ); add_settings_field( 'stl_ml_wpml', - __('WPML Settings', DOMAIN), + __('WPML Settings', 'SrbTransLatin'), [&$this, 'callback_option_wpml'], 'stl_settings', 'stl_settings_ml', @@ -45,8 +43,8 @@ public function callback_option_wpml($wpml) $wpml, 'sgi/stl/opt[ml][wpml]', true, - __('Extend WPML Language Switcher', DOMAIN), - __('Enable if you want to enable script selector in WPML Language Switcher', DOMAIN) + __('Extend WPML Language Switcher', 'SrbTransLatin'), + __('Enable if you want to enable script selector in WPML Language Switcher', 'SrbTransLatin') ); } diff --git a/lib/Widget/Selector.php b/lib/Widget/Selector.php index 5120743..c938632 100644 --- a/lib/Widget/Selector.php +++ b/lib/Widget/Selector.php @@ -2,7 +2,6 @@ namespace SGI\STL\Widget; -use const SGI\STL\DOMAIN; use function SGI\STL\Utils\script_selector; @@ -15,21 +14,21 @@ public function __construct() $widget_id = 'sgi_stl_widget'; $widget_opts = [ 'id_base' => $widget_id, - 'description' => __('Serbian Script selection widget', DOMAIN) + 'description' => __('Serbian Script selection widget', 'SrbTransLatin') ]; $this->defaults = [ - 'title' => __('Script Selection', DOMAIN), + 'title' => __('Script Selection', 'SrbTransLatin'), 'selector_type' => 'oneline', 'separator' => ' | ', - 'cir_caption' => __('Ћирилица', DOMAIN), - 'lat_caption' => __('Latinica', DOMAIN), + 'cir_caption' => __('Ћирилица', 'SrbTransLatin'), + 'lat_caption' => __('Latinica', 'SrbTransLatin'), 'inactive_only' => false, ]; parent::__construct( $widget_id, - __('Serbian Script selector', DOMAIN), + __('Serbian Script selector', 'SrbTransLatin'), $widget_opts ); @@ -46,7 +45,7 @@ public function form($instance)

', $this->get_field_id( 'title' ), - __('Title', DOMAIN), + __('Title', 'SrbTransLatin'), $this->get_field_id( 'title' ), $this->get_field_name( 'title' ), esc_attr( $instance['title'] ) @@ -54,7 +53,7 @@ public function form($instance) printf( '

%s

', - __('Link Options', DOMAIN) + __('Link Options', 'SrbTransLatin') ); printf( @@ -63,7 +62,7 @@ public function form($instance)

', $this->get_field_id( 'cir_caption' ), - __('Link text - Cyrillic', DOMAIN), + __('Link text - Cyrillic', 'SrbTransLatin'), $this->get_field_id( 'cir_caption' ), $this->get_field_name( 'cir_caption' ), esc_attr( $instance['cir_caption'] ) @@ -75,7 +74,7 @@ public function form($instance)

', $this->get_field_id( 'lat_caption' ), - __('Link text - Latin', DOMAIN), + __('Link text - Latin', 'SrbTransLatin'), $this->get_field_id( 'lat_caption' ), $this->get_field_name( 'lat_caption' ), esc_attr( $instance['lat_caption'] ) @@ -83,7 +82,7 @@ public function form($instance) printf( '

%s

', - __('Display Options', DOMAIN) + __('Display Options', 'SrbTransLatin') ); printf( @@ -94,7 +93,7 @@ public function form($instance)

', $this->get_field_id( 'selector_type' ), - __('Selector style', DOMAIN), + __('Selector style', 'SrbTransLatin'), $this->get_field_id( 'selector_type' ), $this->get_field_name( 'selector_type' ), $this->selector_select($instance) @@ -106,7 +105,7 @@ public function form($instance)

', $this->get_field_id( 'separator' ), - __('Separator (oneline)', DOMAIN), + __('Separator (oneline)', 'SrbTransLatin'), $this->get_field_id( 'separator' ), $this->get_field_name( 'separator' ), esc_attr( $instance['separator'] ) @@ -118,9 +117,9 @@ private function selector_select($instance) { $selectors = [ - 'oneline' => __('One Line', DOMAIN), - 'list' => __('Dropdown', DOMAIN), - 'links' => __('List', DOMAIN) + 'oneline' => __('One Line', 'SrbTransLatin'), + 'list' => __('Dropdown', 'SrbTransLatin'), + 'links' => __('List', 'SrbTransLatin') ]; $html = '';