Advertisement
Razorspined

Untitled

Apr 1st, 2023
801
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.20 KB | None | 0 0
  1. <?php
  2. /*
  3. * Plugin Name: Test Plugin
  4. *
  5. *
  6. */
  7.     // function abcd() {
  8.     //     if(is_single()) {
  9.     //         echo "<script>
  10.     //         alert('zdr kp');
  11.     //      </script>";
  12.     //     }
  13.     // }
  14.  
  15.     // function capitalize($content) {
  16.     //     $title = strtoupper($content);
  17.     //     return $title;
  18.     // }
  19.  
  20.     // add_action("the_post", "abcd");
  21.     // add_filter("the_title", "capitalize", 10, 1)
  22.  
  23.     function kebab_case($text) {
  24.         $kebab_text = strip_tags($text);
  25.         $kebab_text = _wp_to_kebab_case( $kebab_text );
  26.  
  27.         return $kebab_text;
  28.     }
  29.  
  30.     function filter_search_strings( $content ) {
  31.         preg_match_all('(\[[a-z]:.*\])', $content, $results);
  32.         foreach ($results[0] as $match) {
  33.             $search_string = str_replace(["[", "]"], "", $match);
  34.             $link_parts = explode(": ", $search_string);
  35.             $anchor_text = "";
  36.            
  37.             if($link_parts[0] == 'g') {
  38.                 $anchor_text =
  39.                 "<a target='_blank' href='http://www.google.com/search?q=" . $link_parts[1] . "'>".
  40.                 $link_parts[1] .
  41.                 "</a>";
  42.             }
  43.             elseif ($link_parts[0] == 'i') {
  44.                 $anchor_text =
  45.                 "<a target='_blank' href='https://imgur.com/search?q=" . $link_parts[1] . "'>".
  46.                 $link_parts[1] .
  47.                 "</a>";
  48.             }
  49.             elseif ($link_parts[0] == 'w') {
  50.                 $anchor_text =
  51.                 "<a target='_blank' href='https://en.wikipedia.org/wiki/" . $link_parts[1] . "'>".
  52.                 $link_parts[1] .
  53.                 "</a>";
  54.             }
  55.  
  56.             $content = str_replace($match, $anchor_text, $content);
  57.         }
  58.         return $content;
  59.     }
  60.  
  61.     function appendUserCommentCount($nick, $comment_id, $comment) {
  62.         global $wpdb;
  63.  
  64.         $comment_count = $wpdb->get_var("SELECT COUNT(*) FROM wp_comments WHERE user_id = {$comment->user_id}");
  65.  
  66.         $post_count = $wpdb->get_var("SELECT COUNT(*) FROM wp_posts WHERE post_author = '{$comment->user_id}' AND post_status='publish' AND post_type='post'");
  67.  
  68.  
  69.         return "{$nick} [{$post_count} posts, {$comment_count[0]} comments]";
  70.     }
  71.  
  72.     function appendUserPostCount($nick) {
  73.         global $wpdb;
  74.        
  75.         $user_id = $wpdb->get_col("SELECT ID FROM wp_users WHERE display_name='{$nick}'");
  76.         $comment_count = $wpdb->get_var("SELECT COUNT(*) FROM wp_comments WHERE user_id = {$user_id[0]}");
  77.  
  78.         $post_count = $wpdb->get_var("SELECT COUNT(*) FROM wp_posts WHERE post_author = '{$user_id[0]}' AND post_status='publish' AND post_type='post'");
  79.  
  80.         return "{$nick} [{$post_count} posts, {$comment_count[0]} comments]";
  81.     }
  82.    
  83.     add_filter("the_title", "kebab_case");
  84.     //add_filter("the_content", "kebab_case");
  85.     add_filter("the_author", "appendUserPostCount", 10, 1);
  86.     add_filter("get_comment_author", "appendUserCommentCount", 10, 3);
  87.     add_filter("the_content", "filter_search_strings");
  88.  
  89.  
  90.     /*
  91.     FILTER - да видим можем ли да подготвим нещо като парсър за специални ключови думи във
  92.     съдържанието на поста, за пример:
  93.     [g: Васил Левски]
  94.     [g: рецепта за боб] <а href="http://www.google.com/?q=рецепта за боб"</a>
  95.  
  96.     Regex патернът, който ще ни трябва, е (\[g:.*])
  97.     1. След като намерим match, replace-ваме '[' и ']' със ''
  98.     (махаме отварящи и затварящи квадратни скоби)
  99.     2. След това, оставащият текст разделяме на две части спрямо ':' символа.
  100.     3. Разделянето ни връща масив. Вторият елемент от масива е текстът, с който ще генерираме
  101.     HTML anchor href т.е. <а href="http://www.google.com/?q=рецепта за боб"</a>
  102.     4. След като сме си генерирали HTML линка, трябва текстът от стъпка 1 да бъде заменен със нашия
  103.     HTML
  104.     */
  105. ?>
  106.  
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement