root/blogger-title-fix/tags/0.1/blogger-title-fix.php

Revision 5828, 2.4 KB (checked in by Poco, 4 years ago)

Added a file remotely

Line 
1<?php
2/*
3Plugin Name: Blogger Title Fix
4Plugin URI: http://notions.okuda.ca/wordpress-plugins/blogger-title-fix/
5Description: Replaces numerical titles (from Blogger import) with the first few words of the post.
6Author: Poco
7Version: 0.1
8Author URI: http://notions.okuda.ca
9*/
10
11
12class ko_blogger_title_fix
13{
14
15        function fragment($str, $n, $delim='...')
16        {
17                $len = strlen($str);
18                if ($len > $n)
19                {
20                        // This code would limit the output to about n characters and not cut any words.
21                        // This looks a bit better than the default, but this is not how Blogger reacts.
22                        //preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
23                        //return rtrim($matches[1]) . $delim;
24
25                        // Just take the first n characters, like Blogger does.
26                        return substr(trim($str), 0, $n) . $delim;
27                }
28                else
29                {
30                        return $str;
31                }
32        }
33
34        function is_bad_title($title)
35        {
36                // Look for a title that is only decimal digits (or empty)
37                return preg_match("/^\d*$/", $title);
38        }
39
40        function filter_title($content = '', $thispost = NULL)
41        {
42                global $post;
43
44                // If we didn't get a post parameter then use the global one.
45                if (!isset($thispost))
46                {
47                        $thispost = post;
48                }
49                else
50                {
51                        // In generate $thispost will only contain the ID and Title and we need more
52                        // So get it now.
53                        $thispost = get_post($thispost->ID);
54                }
55
56                if ($this->is_bad_title($content))
57                {
58                        // Couldn't think of a better way to handle this.
59                        // Stole this code from get_the_excerpt except that it uses the available $thispost
60                        // instead of the global $post.
61                        $output = $thispost->post_excerpt;
62                        if ( !empty($thispost->post_password) ) { // if there's a password
63                                if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $thispost->post_password ) {  // and it doesn't match the cookie
64                                        $output = "";  // empty title
65                                }
66                        }
67
68                        // If there is no excerpt then we take the content.  Again, there was no good function to call
69                        // here since they all assume $post is global.
70                        if ( '' == $output ) {
71                                $output = $thispost->post_content;
72                                $output = apply_filters('the_content', $output);
73                                $output = str_replace(']]>', ']]&gt;', $output);
74                                $output = strip_tags($output);
75                        }
76
77
78                        $newtitle = $this->fragment($output, 48, '...');
79                        return $newtitle;
80                }
81                else
82                {
83                        return $content;
84                }
85        }
86}
87
88$ko_blogger_title_fix_instance = new ko_blogger_title_fix();
89add_filter('the_title', array(&$ko_blogger_title_fix_instance, 'filter_title'),10,2);
90
91?>
Note: See TracBrowser for help on using the browser.