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

Revision 5875, 5.2 KB (checked in by Poco, 4 years ago)

Updated the version number for the new tag.

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.2
8Author URI: http://notions.okuda.ca
9*/
10
11
12class ko_blogger_title_fix
13{
14        var $option_name = "ko_blogger_title_fix";
15
16        function get_default_options()
17        {
18                $options = array(
19                        "excerpt_length" => 48,
20                        "cut_word" => false
21                );
22               
23                return $options;
24        }
25       
26        function get_options()
27        {
28                // If the options exists in the database then retrieve them
29                if ( false !== get_option($this->option_name) )
30                {
31                        // Now we can get the options out of the database.
32                        $options = get_option($this->option_name);
33                }
34                else
35                {
36                        // Otherwise initialize them to the defaults.
37                        $options = $this->get_default_options();
38                }
39               
40                return $options;
41        }
42       
43        function save_options($options)
44        {
45                update_option($this->option_name, $options);
46        }
47
48        function fragment($str, $n, $delim='...', $word_cut=false)
49        {
50                $len = strlen($str);
51                if ($len > $n)
52                {
53                        if ($word_cut)
54                        {
55                                // This code would limit the output to about n characters and not cut any words.
56                                // This looks a bit better than the default, but this is not how Blogger reacts.
57                                preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
58                                return rtrim($matches[1]) . $delim;
59                        }
60                        else
61                        {
62                                // Just take the first n characters, like Blogger does.
63                                return substr(trim($str), 0, $n) . $delim;
64                        }
65                }
66                else
67                {
68                        return $str;
69                }
70        }
71
72        function is_bad_title($title)
73        {
74                // Look for a title that is only decimal digits (or empty)
75                return preg_match("/^\d*$/", $title);
76        }
77
78        function filter_title($content = '', $thispost = NULL)
79        {
80                global $post;
81                $options = $this->get_options();
82
83                // If we didn't get a post parameter then use the global one.
84                if (!isset($thispost))
85                {
86                        $thispost = post;
87                }
88                else
89                {
90                        // In generate $thispost will only contain the ID and Title and we need more
91                        // So get it now.
92                        $thispost = get_post($thispost->ID);
93                }
94
95                if ($this->is_bad_title($content))
96                {
97                        // Couldn't think of a better way to handle this.
98                        // Stole this code from get_the_excerpt except that it uses the available $thispost
99                        // instead of the global $post.
100                        $output = $thispost->post_excerpt;
101                        if ( !empty($thispost->post_password) ) { // if there's a password
102                                if ( $_COOKIE['wp-postpass_'.COOKIEHASH] != $thispost->post_password ) {  // and it doesn't match the cookie
103                                        $output = "";  // empty title
104                                }
105                        }
106
107                        // If there is no excerpt then we take the content.  Again, there was no good function to call
108                        // here since they all assume $post is global.
109                        if ( '' == $output ) {
110                                $output = $thispost->post_content;
111                                $output = apply_filters('the_content', $output);
112                                $output = str_replace(']]>', ']]&gt;', $output);
113                                $output = strip_tags($output);
114                        }
115
116
117                        $newtitle = $this->fragment($output, $options["excerpt_length"], '...', $options["cut_word"]);
118                        return $newtitle;
119                }
120                else
121                {
122                        return $content;
123                }
124        }
125
126        function title_fix_page()
127        {
128                // Get the current value of the options.
129                $options = $this->get_options();
130
131
132                if (isset($_POST['reset_default']))
133                {
134                        ?><div class="updated"><p><strong><?php 
135                                // Store the default options to the database
136                                $options = $this->get_default_options();
137                                $this->save_options($options);
138                                _e('Settings Reset', 'Localization name');
139                        ?></strong></p></div><?php
140                }
141                else if (isset($_POST['info_update']))
142                {
143                        ?><div class="updated"><p><strong><?php 
144
145                        if (isset($_POST['excerpt_length']))
146                        {
147                                $options["excerpt_length"] = $_POST['excerpt_length'];
148                        }
149       
150                        if (isset($_POST['cut_word']))
151                        {
152                                $options["cut_word"] = true;
153                        }
154
155                        // Store the new options to the database
156                        $this->save_options($options);
157                       
158                        _e('Settings Saved', 'Localization name');
159                       
160                        ?></strong></p></div><?php
161                }
162
163                ?><div class=wrap>
164                        <form method="post">
165                                <h2>Blogger Title Fix</h2>
166                                <fieldset class="options" name="set1">
167                                        <legend><?php _e('Excerpt Options', 'Localization name') ?></legend>
168                                        Number of characters from the post to use in the title: <input type="text" name="excerpt_length" value="<?php echo $options["excerpt_length"] ?>" ><br />
169                                        Cut title at word boundary: <input type="checkbox" name="cut_word" <?php if ($options["cut_word"]) { print ('checked="checked"'); } ?> >
170                                </fieldset>
171                                <div class="submit">
172                                        <input type="submit" name="info_update" value="<?php
173                                        _e('Update options', 'Localization name')
174                                        ?> �" />
175                                        <input type="submit" name="reset_default" value="<?php
176                                        _e('Reset to Defaults', 'Localization name')
177                                        ?> �" />
178                                </div>
179                        </form>
180                </div><?php
181        }
182       
183       
184        function add_page()
185        {
186                if (function_exists('add_options_page'))
187                {
188                        add_options_page('Blogger Title Fix', 'Blogger Title Fix', 10, basename(__FILE__), array(&$this, 'title_fix_page'));
189                }
190        }
191}
192
193
194$ko_blogger_title_fix_instance = new ko_blogger_title_fix();
195add_filter('the_title', array(&$ko_blogger_title_fix_instance, 'filter_title'),10,2);
196add_action('admin_menu', array(&$ko_blogger_title_fix_instance, 'add_page'));
197
198?>
Note: See TracBrowser for help on using the browser.