root/mini-posts/trunk/mini-posts.php

Revision 3749, 17.2 kB (checked in by morganiq, 3 years ago)

Bug fixes: label id, pagination on Options page, filename issues, WHERE and JOIN collision fixes.

Line 
1<?php
2/*
3Plugin Name: MiniPosts
4Plugin URI: http://doocy.net/mini-posts/
5Description: An approach to "asides", or small posts. Allows you to mark entries as "mini" posts and handle them differently than normal posts. <strong>Requires WordPress 1.5.</strong>
6Version: 0.5.2
7Author: Morgan Doocy
8Author URI: http://doocy.net/
9*/
10
11/*
12MiniPosts - Small posts, or "asides," plugin for WordPress (http://wordpress.org).
13Copyright (C) 2005  Morgan Doocy
14
15This program is free software; you can redistribute it and/or
16modify it under the terms of the GNU General Public License
17as published by the Free Software Foundation; either version 2
18of the License, or (at your option) any later version.
19
20This program is distributed in the hope that it will be useful,
21but WITHOUT ANY WARRANTY; without even the implied warranty of
22MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23GNU General Public License for more details.
24
25You should have received a copy of the GNU General Public License
26along with this program; if not, write to the Free Software
27Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
28*/
29
30/*
31NOTE: This release is ALPHA-level software. It is known to be feature-incomplete and/or partially functional.
32
33Changelog
34=========
350.1a   - Initial release.
360.2    - Corrected load_plugin_textdomain() domain.
370.3    - Updated to accommodate WP pages.
380.4    - Updated to accommodate feeds; added option to filter mini posts from feeds.
390.5    - Added support for 0-, 1-, and n-comment formatting to get_mini_posts().
40        (WARNING: The argument sequence of get_mini_posts() has changed to accommodate this.)
410.5.1  - Added 'save_post' hook
420.5.2  - Fixed label id for "This is a mini post" checkbox
43       - Added pagination on Options page
44       - Changed all references to the plugin's filename to basename(__FILE__), to avoid file naming issues
45       - Added aliases to JOIN and WHERE clauses to avoid collisions with other plugins
46        (thanks, Jerome and Mark!)
47
48*/
49
50if (is_plugin_page()) {
51        include(ABSPATH.'wp-blog-header.php');
52
53        $paged_val = get_query_var('paged');
54        $paged_val = $paged_val ? "&paged=$paged_val" : '';
55       
56        if (isset($_POST["update_options"])) {
57                update_option('filter_mini_posts_from_loop', $_POST['filter_mini_posts_from_loop'] == 1 ? 1 : 0);
58                update_option('suppress_autop_on_mini_posts', $_POST['suppress_autop_on_mini_posts'] == 1 ? 1 : 0);
59                update_option('filter_mini_posts_from_feeds', $_POST['filter_mini_posts_from_feeds'] == 1 ? 1 : 0);
60                echo '<div class="updated"><p><strong>' . __('Options saved.', 'MiniPosts') . '</strong></p></div>';
61        }
62        elseif (isset($_POST["update_miniposts"])) {
63                $is_mini_post = $_POST["is_mini_post"];
64                foreach ($posts as $post) {
65                        delete_post_meta($post->ID, '_mini_post');
66                        $setting = in_array($post->ID, $is_mini_post) ? 1 : 0;
67                        add_post_meta($post->ID, '_mini_post', $setting);
68                }
69                header("Location: options-general.php?page=" . basename(__FILE__) . "&miniposts_updated=true$paged_val");
70                exit();
71        }
72       
73        $ck_filter_mini_posts_from_loop = get_settings('filter_mini_posts_from_loop') == 1 ? 'checked="checked" ' : '';
74        $ck_suppress_autop_on_mini_posts = get_settings('suppress_autop_on_mini_posts') == 1 ? 'checked="checked" ' : '';
75        $ck_filter_mini_posts_from_feeds = get_settings('filter_mini_posts_from_feeds') == 1 ? 'checked="checked" ' : '';
76       
77        ?>
78        <div class="wrap">
79        <h2><?php _e('Mini Post Options', 'MiniPosts') ?></h2>
80        <form method="post" action="./options-general.php?page=<?php echo basename(__FILE__) . $paged_val; ?>">
81                <table width="100%" cellspacing="2" cellpadding="5" class="editform">
82                        <tr valign="top">
83                                <th width="33%" scope="row"><?php _e('Show/hide:', 'MiniPost') ?></th>
84                                <td>
85                                        <label for="filter_mini_posts_from_loop"><input type="checkbox" name="filter_mini_posts_from_loop" id="filter_mini_posts_from_loop" <?php echo $ck_filter_mini_posts_from_loop ?> value="1" /> <?php _e('Filter mini posts from the Loop', 'MiniPosts') ?></label><br />
86                                        <label for="filter_mini_posts_from_feeds"><input type="checkbox" name="filter_mini_posts_from_feeds" id="filter_mini_posts_from_feeds" <?php echo $ck_filter_mini_posts_from_feeds ?> value="1" /> <?php _e('Filter mini posts from subscription feeds', 'MiniPosts') ?></label><br />
87                                </td>
88                        </tr>
89                        <tr valign="top">
90                                <th width="33%" scope="row"><?php _e('Auto-paragraphing:', 'MiniPosts') ?></th>
91                                <td><label for="suppress_autop_on_mini_posts"><input type="checkbox" name="suppress_autop_on_mini_posts" id="suppress_autop_on_mini_posts" <?php echo $ck_suppress_autop_on_mini_posts ?> value="1" /> <?php _e('Suppress auto-paragraphing on mini posts in the Loop', 'MiniPosts') ?></label></td>
92                        </tr>
93                </table>
94                <div class="submit"><input type="submit" name="update_options" value="<?php _e('Update Options', 'MiniPosts') ?> &raquo;" /></div>
95        </form>
96        </div>
97        <?php
98       
99        if ($_GET["miniposts_updated"] == "true")        {
100               
101        ?>
102        <div class="updated"><p><strong><?php _e('Mini posts updated.', 'MiniPosts') ?></strong></p></div>
103        <?php
104       
105        }
106       
107        ?>
108        <div class="wrap">
109        <h2><?php _e('Mini Post Manager', 'MiniPosts') ?></h2>
110        <form method="post" action="./options-general.php?page=<?php echo basename(__FILE__); ?>&noheader=true<?php echo $paged_val; ?>">
111        <table width="100%" cellpadding="3" cellspacing="3">
112                <thead>
113                        <tr>
114                                <th scope="col"><?php _e('ID', 'MiniPosts') ?></th>
115                                <th scope="col"><?php _e('When', 'MiniPosts') ?></th>
116                                <th scope="col"><?php _e('Title', 'MiniPosts') ?></th>
117                                <th scope="col"><?php _e('MiniPost', 'MiniPosts') ?></th>
118                                <th scope="col"><?php _e('Categories', 'MiniPosts') ?></th>
119                                <th scope="col"><?php _e('Comments', 'MiniPosts') ?></th>
120                                <th scope="col"><?php _e('Author', 'MiniPosts') ?></th>
121                                <td colspan="3"></td>
122                        </tr>
123                </thead>
124                <tbody>
125        <?php
126       
127        foreach($posts as $post)
128        {
129                start_wp();
130                $checked = is_mini_post() ? 'checked="checked" ' : '';
131                $class = ('alternate' == $class) ? '' : 'alternate'; ?>
132                        <tr class="<?php echo $class ?>">
133                                <th scope="row"><?php echo $id ?></th>
134                                <td><?php the_time('Y-m-d \<\b\r \/\> g:i:s a'); ?></td>
135                                <td><?php the_title() ?><?php if ('private' == $post->post_status) _e(' - <strong>Private</strong>'); ?></td>
136                                <td><input type="checkbox" name="is_mini_post[]" id="is_mini_post[]" value="<?php echo $id ?>" <?php echo $checked ?>/></td>
137                                <td><?php the_category(','); ?></td>
138                                <td><a href="edit.php?p=<?php echo $id ?>&amp;c=1"><?php comments_number(__('0'), __('1'), __('%')) ?></a></td>
139                                <td><?php the_author() ?></td>
140                                <td><a href="<?php the_permalink(); ?>" rel="permalink" class="edit"><?php _e('View'); ?></a></td>
141                                <td><?php if (($user_level > $authordata->user_level) or ($user_login == $authordata->user_login)) { echo "<a href='post.php?action=edit&amp;post=$id' class='edit'>" . __('Edit') . "</a>"; } ?></td>
142                                <td><?php if (($user_level > $authordata->user_level) or ($user_login == $authordata->user_login)) { echo "<a href='post.php?action=delete&amp;post=$id' class='delete' onclick=\"return confirm('" . sprintf(__("You are about to delete this post \'%s\'\\n  \'OK\' to delete, \'Cancel\' to stop."), the_title('','',0)) . "')\">" . __('Delete') . "</a>"; } ?></td>
143                        </tr>
144                <?php
145        } ?>
146                </tbody>
147        </table>
148        <div class="navigation">
149                <?php
150                        // Remove 'miniposts_updated' value from query string, if present, so that
151                        // get_pagenum_link() doesn't perpetuate it.
152                        $_SERVER['REQUEST_URI'] = preg_replace('/&miniposts_updated=true/', '', $_SERVER['REQUEST_URI']);
153                        $_SERVER['REQUEST_URI'] = preg_replace('/\?miniposts_updated=true&/', '?', $_SERVER['REQUEST_URI']);
154                        $_SERVER['REQUEST_URI'] = preg_replace('/\?miniposts_updated=true/', '', $_SERVER['REQUEST_URI']);
155                ?>
156                <div class="alignleft"><?php next_posts_link(__('&laquo; Previous Entries')) ?></div>
157                <div class="alignright"><?php previous_posts_link(__('Next Entries &raquo;')) ?></div>
158        </div>
159        <div class="submit"><input type="submit" name="update_miniposts" value="<?php _e('Update Mini Posts', 'MiniPosts') ?> &raquo;" /></div>
160        </form>
161        </div>
162        <?php
163
164} else {
165       
166        load_plugin_textdomain('MiniPosts');
167       
168        add_option('filter_mini_posts_from_loop', 1);
169        add_option('suppress_autop_on_mini_posts', 1);
170        add_option('filter_mini_posts_from_feeds', 0);
171       
172        // Auto-install: If there are no previous meta values for '_mini_post', automatically add one for each post
173        if (0 == $wpdb->get_var("SELECT count(meta_value) FROM $wpdb->postmeta WHERE meta_key = '_mini_post'")) {
174                if ($posts = $wpdb->get_results("SELECT * FROM $wpdb->posts")) {
175                        foreach ($posts as $post) {
176                                add_post_meta($post->ID, '_mini_post', '0');
177                        }
178                }
179        }
180       
181        function is_mini_post() {
182                global $post;
183                return (bool) get_post_meta($post->ID, '_mini_post', true);
184        }
185       
186        function get_mini_posts($format = '%post% %commentcount% %permalink%', $permalink_text = '', $zero_comments = '', $one_comment = '', $more_comments = '', $limit = '') {
187                global $wpdb;
188               
189                if ('' != $limit) {
190                        $limit = (int) $limit;
191                        $limit = ' LIMIT '.$limit;
192                }
193               
194                // Determine whether a comment format was specified
195                if ('' == $zero_comments && '' == $one_comment && '' == $more_comments)
196                        $comment_count_format = false;
197                else
198                        $comment_count_format = true;
199               
200                // If only the 'zero' format is specified, use the same for all three
201                if ('' != $zero_comments && '' == $one_comment && '' == $more_comments)
202                        $one_comment = $more_comments = $zero_comments;
203               
204                // Make $permalink_text and comment count formats coincide with $format,
205                // resolving any conflicts.
206                if ('' != $format) {
207                        if (strstr($format, '%commentcount%') && !$comment_count_format) {
208                                $zero_comments = $one_comment = $more_comments = '(%s)';
209                                $comment_count_format = true;
210                        }
211                        elseif (!strstr($format, '%commentcount%') && $comment_count_format)
212                                $format = "$format %commentcount%";
213                        if (strstr($format, '%permalink%') && '' == $permalink_text)
214                                $permalink_text = '#';
215                        elseif (!strstr($format, '%permalink%') && '' != $permalink_text)
216                                $format = "$format %permalink%";
217                        if (!strstr($format, '%post%'))
218                                $format = "%post% $format";
219                } else {
220                        if ('' != $permalink_text && $comment_count_format)
221                                $format = '%post% %commentcount% %permalink%';
222                        elseif ('' == $permalink_text && $comment_count_format)
223                                $format = '%post% %commentcount%';
224                        elseif ('' != $permalink_text && !$comment_count_format)
225                                $format = '%post% %permalink%';
226                        elseif ('' == $permalink_text && !$comment_count_format)
227                                $format = '%post%';
228                }
229               
230                if ($comment_count_format && $commentcounts = $wpdb->get_results("SELECT ID, COUNT($wpdb->comments.comment_post_ID) AS comment_count FROM $wpdb->comments INNER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->postmeta.meta_key = '_mini_post' AND $wpdb->postmeta.meta_value = '1' $exclusions GROUP BY $wpdb->posts.ID")) {
231                        foreach ($commentcounts as $commentcount) {
232                                if ($commentcount > 0) {
233                                        $minipost_commentcounts["$commentcount->ID"] = $commentcount->comment_count;
234                                }
235                        }
236                }
237               
238                $now = current_time('mysql');
239               
240                if ($miniposts = $wpdb->get_results("SELECT ID, post_date, post_content, post_title FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE post_date < '$now' AND post_status = 'publish' AND $wpdb->postmeta.meta_key = '_mini_post' AND $wpdb->postmeta.meta_value = '1' ORDER BY post_date DESC" . $limit)) {
241                        foreach ($miniposts as $minipost) {
242                                if ($minipost->post_date != '0000-00-00 00:00:00') {
243                                        $url  = get_permalink($minipost->ID);
244                                        $title = $minipost->post_title;
245                                        if ($title) {
246                                                $title = strip_tags($title);
247                                        } else {
248                                                $title = $minipost->ID;
249                                        }
250                                        $text = $minipost->post_content;
251                                        $text = wptexturize($text);
252                                        $title_text = wp_specialchars($title, 1);
253                                       
254                                        if ('' != $permalink_text) {
255                                                $permalink = "<a class=\"minipost_permalink\" href=\"$url\" title=\"$title_text\">$permalink_text</a>";
256                                        }
257                                       
258                                        if ($comment_count_format) {
259                                                $count = isset($minipost_commentcounts["$minipost->ID"]) ? $minipost_commentcounts["$minipost->ID"] : 0;
260                                                if ($count == 0)
261                                                        $commentcounttext = $zero_comments;
262                                                elseif ($count == 1)
263                                                        $commentcounttext = $one_comment;
264                                                else
265                                                        $commentcounttext = $more_comments;
266                                                $commenturl = "$url#comments";
267                                                $commentcount = sprintf("<a class=\"minipost_commentlink\" href=\"$commenturl\" title=\"Comments for '$title_text'\">$commentcounttext</a>", $count);
268                                        }
269                                       
270                                        $meta = str_replace('%permalink%', $permalink, $format);
271                                        $meta = str_replace('%commentcount%', $commentcount, $meta);
272                                       
273                                        $text = str_replace('%post%', $text, $meta);
274                                       
275                                        echo "\t<li>$text</li>\n";
276                                }
277                        }
278                }
279        }
280       
281        function mini_posts_join($text) {
282                global $wpdb, $pagenow;
283               
284                if (!is_plugin_page() && $pagenow != 'post.php' && $pagenow != 'edit.php' && get_settings('filter_mini_posts_from_loop') && !is_single() && !is_archive() && !is_page() && (is_feed() && get_settings('filter_mini_posts_from_feeds') || !is_feed()) && !stristr($text, "wp_postmeta")) {
285                        $text .= " LEFT JOIN $wpdb->postmeta AS miniposts_meta ON ($wpdb->posts.ID = miniposts_meta.post_id)";
286                }
287               
288                return $text;
289        }
290       
291        function mini_posts_where($text) {
292                global $user_ID, $user_level, $wpdb, $pagenow;
293               
294                if (!is_plugin_page() && $pagenow != 'post.php' && $pagenow != 'edit.php' && get_settings('filter_mini_posts_from_loop') && !is_single() && !is_archive() && !is_page() && (is_feed() && get_settings('filter_mini_posts_from_feeds') || !is_feed())) {
295                        $text .= " AND (miniposts_meta.meta_key = '_mini_post' AND miniposts_meta.meta_value = 0)";
296                }
297               
298                return $text;
299        }
300       
301        if (get_settings('suppress_autop_on_mini_posts')) {
302                function mini_post_autop($pee, $br = 1) {
303                        if (!is_mini_post() || is_single()) {
304                                $pee = $pee . "\n"; // just to make things a little easier, pad the end
305                                $pee = preg_replace('|<br />\s*<br />|', "\n\n", $pee);
306                                // Space things out a little
307                                $pee = preg_replace('!(<(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|math|p|h[1-6])[^>]*>)!', "\n$1", $pee); 
308                                $pee = preg_replace('!(</(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|math|p|h[1-6])>)!', "$1\n", $pee);
309                                $pee = str_replace(array("\r\n", "\r"), "\n", $pee); // cross-platform newlines
310                                $pee = preg_replace("/\n\n+/", "\n\n", $pee); // take care of duplicates
311                                $pee = preg_replace('/\n?(.+?)(?:\n\s*\n|\z)/s', "\t<p>$1</p>\n", $pee); // make paragraphs, including one at the end
312                                $pee = preg_replace('|<p>\s*?</p>|', '', $pee); // under certain strange conditions it could create a P of entirely whitespace
313                                $pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); // don't pee all over a tag
314                                $pee = preg_replace("|<p>(<li.+?)</p>|", "$1", $pee); // problem with nested lists
315                                $pee = preg_replace('|<p><blockquote([^>]*)>|i', "<blockquote$1><p>", $pee);
316                                $pee = str_replace('</blockquote></p>', '</p></blockquote>', $pee);
317                                $pee = preg_replace('!<p>\s*(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|hr|pre|select|form|blockquote|math|p|h[1-6])[^>]*>)!', "$1", $pee);
318                                $pee = preg_replace('!(</?(?:table|thead|tfoot|caption|colgroup|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|math|p|h[1-6])[^>]*>)\s*</p>!', "$1", $pee); 
319                                if ($br) $pee = preg_replace('|(?<!<br />)\s*\n|', "<br />\n", $pee); // optionally make line breaks
320                                $pee = preg_replace('!(</?(?:table|thead|tfoot|caption|tbody|tr|td|th|div|dl|dd|dt|ul|ol|li|pre|select|form|blockquote|math|p|h[1-6])[^>]*>)\s*<br />!', "$1", $pee);
321                                $pee = preg_replace('!<br />(\s*</?(?:p|li|div|dl|dd|dt|th|pre|td|ul|ol)>)!', '$1', $pee);
322                                $pee = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') .  clean_pre('$2')  . '</pre>' ", $pee);
323                        }
324                        return $pee; 
325                }
326        }
327       
328        add_filter('posts_where', 'mini_posts_where');
329        add_filter('posts_join', 'mini_posts_join');
330        if (get_settings('suppress_autop_on_mini_posts')) {
331                remove_filter('the_content', 'wpautop');
332                add_filter('the_content', 'mini_post_autop');
333        }
334       
335        function mini_posts_checkbox() {
336                global $postdata;
337               
338                $is_mini = get_post_meta($postdata->ID, '_mini_post', true);
339                $check = $is_mini ? 'checked="checked" ' : '';
340               
341                echo '<fieldset><legend><a href="http://doocy.net/mini-posts/help/" title="Help with MiniPosts">' . __('MiniPosts', 'MiniPosts') . '</a></legend>';
342                echo '<label for="is_mini_post"><input type="checkbox" name="is_mini_post" id="is_mini_post" value="1" '. $check . '/> '. __('This is a mini post', 'MiniPosts') . '</label></fieldset>';
343        }
344       
345        function mini_update_post($id) {
346                delete_post_meta($id, '_mini_post');
347                $setting = (isset($_POST["is_mini_post"]) && $_POST["is_mini_post"] == "1") ? 1 : 0;
348                add_post_meta($id, '_mini_post', $setting);
349        }
350       
351        function mini_admin_menu() {
352                add_options_page(__('Mini Post Manager', 'MiniPosts'), __('MiniPosts', 'MiniPosts'), 5, basename(__FILE__));
353        }
354       
355        add_action('edit_form_advanced', 'mini_posts_checkbox');
356        add_action('simple_edit_form', 'mini_posts_checkbox');
357        add_action('save_post', 'mini_update_post');
358        add_action('edit_post', 'mini_update_post');
359        add_action('publish_post', 'mini_update_post');
360        add_action('admin_menu', 'mini_admin_menu');
361}
362?>
Note: See TracBrowser for help on using the browser.