Changeset 3213384
- Timestamp:
- 12/26/2024 03:02:12 PM (15 months ago)
- Location:
- pevne-mezery
- Files:
-
- 4 edited
- 1 copied
-
tags/1.3 (copied) (copied from pevne-mezery/trunk)
-
tags/1.3/includes/class-cache-handler.php (modified) (2 diffs)
-
tags/1.3/includes/class-content-handler.php (modified) (5 diffs)
-
trunk/includes/class-cache-handler.php (modified) (2 diffs)
-
trunk/includes/class-content-handler.php (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pevne-mezery/tags/1.3/includes/class-cache-handler.php
r3162288 r3213384 13 13 * @return string The unique cache key. 14 14 */ 15 private static function generate_cache_key(string $content ): string15 private static function generate_cache_key(string $content, string $context = 'default'): string 16 16 { 17 return 'fs_fixed_spaces_' . md5($content);17 return 'fs_fixed_spaces_' . $context . '_' . md5($content); 18 18 } 19 19 … … 70 70 { 71 71 $content = get_post_field('post_content', $post_id); 72 72 73 if ($content) { 73 74 self::delete_cached_content($content); 74 75 } 75 76 } 77 78 /** 79 * Deletes all cache entries for a specific context. 80 * 81 * @param string $context The context to delete cache for. 82 * @return void 83 */ 84 public static function delete_cache_by_context(string $context): void 85 { 86 global $wpdb; 87 $prefix = 'fs_fixed_spaces_' . $context; 88 $wpdb->query( 89 $wpdb->prepare( 90 "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", 91 '_transient_' . $prefix . '%' 92 ) 93 ); 94 } 76 95 } -
pevne-mezery/tags/1.3/includes/class-content-handler.php
r3164416 r3213384 5 5 class ContentHandler 6 6 { 7 /** 8 * Debug mode flag. If true, replaces non-breaking spaces with a visible symbol. 9 * 10 * @var bool 11 */ 12 private static $debug_mode = false; 13 14 /** 15 * Toggle debug mode. 16 * 17 * @param bool $enabled Enable or disable debug mode. 18 * @return void 19 */ 20 public static function set_debug_mode(bool $enabled): void 21 { 22 self::$debug_mode = $enabled; 23 } 24 7 25 /** 8 26 * Main function to process the content and apply typographical rules. … … 13 31 public static function process_content(string $content): string 14 32 { 33 // Check if content is already cached 34 $cached_content = CacheHandler::get_cached_content($content); 35 36 if ($cached_content !== null) { 37 return $cached_content; 38 } 39 15 40 // Split content into HTML and text parts 16 41 $text_parts = preg_split('/(<[^>]+>)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); … … 20 45 if (!preg_match('/^<.*>$/', $part)) { 21 46 foreach (self::get_regex_rules() as $pattern => $replacement) { 47 $replacement = self::$debug_mode 48 ? str_replace(' ', '⭕️', $replacement) 49 : $replacement; 22 50 $part = preg_replace($pattern, $replacement, $part); 23 51 } … … 25 53 } 26 54 27 return implode('', $text_parts); 55 $processed_content = implode('', $text_parts); 56 57 // Save processed content to cache 58 CacheHandler::save_cached_content($content, $processed_content); 59 60 return $processed_content; 28 61 } 29 62 … … 36 69 private static function get_regex_rules(): array 37 70 { 38 // Using combined regex for similar rules and more efficient replacements39 71 return [ 40 // Mat h expressions with non-breakable spaces72 // Matematické výrazy s pevnými mezerami 41 73 '/(\d)\s+([+\-*\/=])\s+(\d)/u' => '$1 $2 $3', 42 74 43 // Single-character prepositions and conjunctions75 // Jednopísmenné předložky a spojky 44 76 '/\b(k|s|v|z|o|u|a|i)\s+/iu' => '$1 ', 45 77 46 '/\s*–\s*/u' => ' – ', // Specific rule for en dash 78 // Pomlčka s pevnými mezerami 79 '/\s*–\s*/u' => ' – ', 80 '/(\d+)\s*–\s*(\d+)/u' => '$1–$2', 47 81 48 // Units of measurement49 '/(\d+)\s+( h|min|s|ms|m|m²|km|cm|mm|ha|km²|MB|GB|m\/s|km\/h|°|°C|°F|Kč|€|\$|%|dní|lidí)/u' => '$1 $2',82 // Jednotky a složené výrazy 83 '/(\d+)\s+(l|h|min|s|ms|m|m²|km|cm|mm|ha|km²|MB|GB|kW|W|m\/s|km\/h|l\/\d+|°|°C|°F|Kč|€|\$|%|dní|lidí|kg)/u' => '$1 $2', 50 84 51 // Czech abbreviations52 '/ \b(Bc|Mgr|Ing|Ph\.D|LL\.B|MUDr|JUDr|prof|voj|čet|rtm|por|kpt|plk|gen|Dr|doc|cca|č|čís|čj|čp|fa|fě|fy|kupř|mj|např|p|pí|popř|př|přib|přibl|sl|str|sv|tj|tzn|tzv|zvl)\.\s+/u' => '$1. ',85 // Pořadová čísla a zkratky 86 '/(\d+)\s*([%|kg])/u' => '$1 $2', 53 87 54 // Space after ordered number 55 '/(\d+\.)\s+([0-9a-záčďéěíňóřšťúýž])/u' => '$1 $2', // After ordinal numbers 88 // Zkratky (např., apod., atd.) 89 '/\b(např|atd|apod|tj|tzn|tzv|mj|cca|vs|resp|ap|fa|č|čj|čp|čís|kupř|mj|tj|tj.|tzn|tzv)\.\s+/u' => '$1. ', 90 91 // Tituly (např. JUDr., Ing., Mgr.) 92 '/\b(JUDr|Ph\.D|LL\.B|MUDr|Mgr|Bc|Ing|CSc|Th\.D|MBA|DiS|prof|doc|RNDr|PhDr|PaedDr|ThLic|Dr|BcA|MgA|PharmDr|MVDr|JUDr|ThDr|Ph\.Mr|prof|etc)\.\s+/u' => '$1. ', 93 '/(\d+\.)\s+(\S)/u' => '$1 $2', 94 95 // Úhlové stupně, minuty, vteřiny 96 '/(\d+)(\s*)(°|\'|\")/u' => '$1 $3', 97 98 // Lomítka 99 '/\s+\/\s+/u' => ' / ', 100 '/([a-zA-Z])\/([a-zA-Z])/u' => '$1/$2', 101 102 // Exponenty a indexy 103 '/(\d+)([²³])/u' => '$1$2', 104 105 // Tři tečky 106 '/(\S)\s*\.{3}/u' => '$1…', 107 '/\.{3}\s+/u' => '… ', 108 '/\s+\.{3}\s+/u' => ' … ', 109 '/(\S)…(\s| )/u' => '$1… ', 110 '/\s+…\s+/u' => ' … ', 111 '/…(\s| )/u' => '… ', 112 113 // § 114 '/\s+§\s+/u' => ' § ', 56 115 ]; 57 116 } -
pevne-mezery/trunk/includes/class-cache-handler.php
r3162288 r3213384 13 13 * @return string The unique cache key. 14 14 */ 15 private static function generate_cache_key(string $content ): string15 private static function generate_cache_key(string $content, string $context = 'default'): string 16 16 { 17 return 'fs_fixed_spaces_' . md5($content);17 return 'fs_fixed_spaces_' . $context . '_' . md5($content); 18 18 } 19 19 … … 70 70 { 71 71 $content = get_post_field('post_content', $post_id); 72 72 73 if ($content) { 73 74 self::delete_cached_content($content); 74 75 } 75 76 } 77 78 /** 79 * Deletes all cache entries for a specific context. 80 * 81 * @param string $context The context to delete cache for. 82 * @return void 83 */ 84 public static function delete_cache_by_context(string $context): void 85 { 86 global $wpdb; 87 $prefix = 'fs_fixed_spaces_' . $context; 88 $wpdb->query( 89 $wpdb->prepare( 90 "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", 91 '_transient_' . $prefix . '%' 92 ) 93 ); 94 } 76 95 } -
pevne-mezery/trunk/includes/class-content-handler.php
r3164416 r3213384 5 5 class ContentHandler 6 6 { 7 /** 8 * Debug mode flag. If true, replaces non-breaking spaces with a visible symbol. 9 * 10 * @var bool 11 */ 12 private static $debug_mode = false; 13 14 /** 15 * Toggle debug mode. 16 * 17 * @param bool $enabled Enable or disable debug mode. 18 * @return void 19 */ 20 public static function set_debug_mode(bool $enabled): void 21 { 22 self::$debug_mode = $enabled; 23 } 24 7 25 /** 8 26 * Main function to process the content and apply typographical rules. … … 13 31 public static function process_content(string $content): string 14 32 { 33 // Check if content is already cached 34 $cached_content = CacheHandler::get_cached_content($content); 35 36 if ($cached_content !== null) { 37 return $cached_content; 38 } 39 15 40 // Split content into HTML and text parts 16 41 $text_parts = preg_split('/(<[^>]+>)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); … … 20 45 if (!preg_match('/^<.*>$/', $part)) { 21 46 foreach (self::get_regex_rules() as $pattern => $replacement) { 47 $replacement = self::$debug_mode 48 ? str_replace(' ', '⭕️', $replacement) 49 : $replacement; 22 50 $part = preg_replace($pattern, $replacement, $part); 23 51 } … … 25 53 } 26 54 27 return implode('', $text_parts); 55 $processed_content = implode('', $text_parts); 56 57 // Save processed content to cache 58 CacheHandler::save_cached_content($content, $processed_content); 59 60 return $processed_content; 28 61 } 29 62 … … 36 69 private static function get_regex_rules(): array 37 70 { 38 // Using combined regex for similar rules and more efficient replacements39 71 return [ 40 // Mat h expressions with non-breakable spaces72 // Matematické výrazy s pevnými mezerami 41 73 '/(\d)\s+([+\-*\/=])\s+(\d)/u' => '$1 $2 $3', 42 74 43 // Single-character prepositions and conjunctions75 // Jednopísmenné předložky a spojky 44 76 '/\b(k|s|v|z|o|u|a|i)\s+/iu' => '$1 ', 45 77 46 '/\s*–\s*/u' => ' – ', // Specific rule for en dash 78 // Pomlčka s pevnými mezerami 79 '/\s*–\s*/u' => ' – ', 80 '/(\d+)\s*–\s*(\d+)/u' => '$1–$2', 47 81 48 // Units of measurement49 '/(\d+)\s+( h|min|s|ms|m|m²|km|cm|mm|ha|km²|MB|GB|m\/s|km\/h|°|°C|°F|Kč|€|\$|%|dní|lidí)/u' => '$1 $2',82 // Jednotky a složené výrazy 83 '/(\d+)\s+(l|h|min|s|ms|m|m²|km|cm|mm|ha|km²|MB|GB|kW|W|m\/s|km\/h|l\/\d+|°|°C|°F|Kč|€|\$|%|dní|lidí|kg)/u' => '$1 $2', 50 84 51 // Czech abbreviations52 '/ \b(Bc|Mgr|Ing|Ph\.D|LL\.B|MUDr|JUDr|prof|voj|čet|rtm|por|kpt|plk|gen|Dr|doc|cca|č|čís|čj|čp|fa|fě|fy|kupř|mj|např|p|pí|popř|př|přib|přibl|sl|str|sv|tj|tzn|tzv|zvl)\.\s+/u' => '$1. ',85 // Pořadová čísla a zkratky 86 '/(\d+)\s*([%|kg])/u' => '$1 $2', 53 87 54 // Space after ordered number 55 '/(\d+\.)\s+([0-9a-záčďéěíňóřšťúýž])/u' => '$1 $2', // After ordinal numbers 88 // Zkratky (např., apod., atd.) 89 '/\b(např|atd|apod|tj|tzn|tzv|mj|cca|vs|resp|ap|fa|č|čj|čp|čís|kupř|mj|tj|tj.|tzn|tzv)\.\s+/u' => '$1. ', 90 91 // Tituly (např. JUDr., Ing., Mgr.) 92 '/\b(JUDr|Ph\.D|LL\.B|MUDr|Mgr|Bc|Ing|CSc|Th\.D|MBA|DiS|prof|doc|RNDr|PhDr|PaedDr|ThLic|Dr|BcA|MgA|PharmDr|MVDr|JUDr|ThDr|Ph\.Mr|prof|etc)\.\s+/u' => '$1. ', 93 '/(\d+\.)\s+(\S)/u' => '$1 $2', 94 95 // Úhlové stupně, minuty, vteřiny 96 '/(\d+)(\s*)(°|\'|\")/u' => '$1 $3', 97 98 // Lomítka 99 '/\s+\/\s+/u' => ' / ', 100 '/([a-zA-Z])\/([a-zA-Z])/u' => '$1/$2', 101 102 // Exponenty a indexy 103 '/(\d+)([²³])/u' => '$1$2', 104 105 // Tři tečky 106 '/(\S)\s*\.{3}/u' => '$1…', 107 '/\.{3}\s+/u' => '… ', 108 '/\s+\.{3}\s+/u' => ' … ', 109 '/(\S)…(\s| )/u' => '$1… ', 110 '/\s+…\s+/u' => ' … ', 111 '/…(\s| )/u' => '… ', 112 113 // § 114 '/\s+§\s+/u' => ' § ', 56 115 ]; 57 116 }
Note: See TracChangeset
for help on using the changeset viewer.