| |
| <?php
|
| function convert_to_sentence_case($text) {
|
| $exceptions = array(
|
| 'API', 'URL', 'HTML', 'CSS', 'PHP', 'SQL', 'XML', 'JSON', 'HTTP', 'HTTPS',
|
| 'CEO', 'CTO', 'CFO', 'USA', 'UK', 'EU', 'AI', 'ML', 'IoT', 'GPS',
|
| 'FAQ', 'PDF', 'SEO', 'ROI', 'KPI', 'B2B', 'B2C', 'SaaS',
|
| 'WordPress', 'WooCommerce', 'jQuery', 'JavaScript', 'MySQL', 'AWS'
|
| );
|
| $text = trim($text);
|
| $protected_words = array();
|
| $placeholder_counter = 0;
|
| $text = preg_replace_callback('/\[kp\](.*?)\[\/kp\]/i', function($matches) use (&$protected_words, &$placeholder_counter) {
|
| $placeholder = '___PROTECTED_' . $placeholder_counter . '___';
|
| $protected_words[$placeholder] = $matches[1];
|
| $placeholder_counter++;
|
| return $placeholder;
|
| }, $text);
|
| $words = explode(' ', $text);
|
| $processed_words = array();
|
| foreach ($words as $index => $word) {
|
| if (strpos($word, '___PROTECTED_') !== false) {
|
| $processed_words[] = $word;
|
| } else {
|
| $clean_word = preg_replace('/[^\w]/', '', $word);
|
| $punctuation = preg_replace('/[\w]/', '', $word);
|
| if (in_array(strtoupper($clean_word), array_map('strtoupper', $exceptions))) {
|
| $found_exception = '';
|
| foreach ($exceptions as $exception) {
|
| if (strtoupper($clean_word) === strtoupper($exception)) {
|
| $found_exception = $exception;
|
| break;
|
| }
|
| }
|
| $processed_words[] = $found_exception . $punctuation;
|
| } else {
|
| $lower_word = strtolower($word);
|
| if ($index === 0) {
|
| $processed_words[] = ucfirst($lower_word);
|
| } else {
|
| $prev_word = isset($processed_words[$index - 1]) ? $processed_words[$index - 1] : '';
|
| if (preg_match('/[.!?:]$/', $prev_word)) {
|
| $processed_words[] = ucfirst($lower_word);
|
| } else {
|
| $processed_words[] = $lower_word;
|
| }
|
| }
|
| }
|
| }
|
| }
|
| $result = implode(' ', $processed_words);
|
| foreach ($protected_words as $placeholder => $original) {
|
| $result = str_replace($placeholder, $original, $result);
|
| }
|
| return $result;
|
| }
|
| function modify_heading_content($content) {
|
| $pattern = '/<(h[1-6])([^>]*)>(.*?)<\/\1>/i';
|
| $content = preg_replace_callback($pattern, function($matches) {
|
| $tag = $matches[1];
|
| $attributes = $matches[2];
|
| $heading_content = $matches[3];
|
| if (strpos($heading_content, '<a') !== false) {
|
| $heading_content = preg_replace_callback('/<a([^>]*)>(.*?)<\/a>/i', function($link_matches) {
|
| $link_attributes = $link_matches[1];
|
| $link_text = $link_matches[2];
|
| $converted_text = convert_to_sentence_case(strip_tags($link_text));
|
| return '<a' . $link_attributes . '>' . $converted_text . '</a>';
|
| }, $heading_content);
|
| } else {
|
| $clean_text = strip_tags($heading_content);
|
| $heading_content = convert_to_sentence_case($clean_text);
|
| }
|
| return '<' . $tag . $attributes . '>' . $heading_content . '</' . $tag . '>';
|
| }, $content);
|
| return $content;
|
| }
|
| function modify_text_before_colons($content) {
|
| $pattern = '/(?<![<\/])\b([A-Za-z][^:<>]*?)(\s*:)/';
|
| $content = preg_replace_callback($pattern, function($matches) {
|
| $text_before_colon = trim($matches[1]);
|
| $colon_part = $matches[2];
|
| $sentence_case_text = convert_to_sentence_case($text_before_colon);
|
| return $sentence_case_text . $colon_part;
|
| }, $content);
|
| return $content;
|
| }
|
| add_filter('the_content', 'modify_heading_content', 20);
|
| add_filter('the_content', 'modify_text_before_colons', 25);
|
| add_filter('widget_text', 'modify_heading_content', 20);
|
| add_filter('widget_text', 'modify_text_before_colons', 25);
|
| add_filter('the_excerpt', 'modify_heading_content', 20);
|
| add_filter('the_excerpt', 'modify_text_before_colons', 25);
|
| |
| |
Comments