root/post-count-titles-for-bbpress/trunk/post-count-titles.php

Revision 865, 9.5 kB (checked in by sambauers, 10 months ago)

post-count-titles-for-bbpress: Check that the titles are actually there before transforming them. Tag version 1.0.2

Line 
1 <?php
2 /*
3 Plugin Name: Post count titles
4 Plugin URI: http://bbpress.org/plugins/topic/50
5 Description: Adds customisable titles for users based on their post count
6 Author: Sam Bauers
7 Version: 1.0.2
8 Author URI:
9
10 Version History:
11 1.0     : Initial Release
12 1.0.1    : Made PHP4 compatible
13 1.0.2    : Check that the titles are actually there before transforming them
14 */
15
16
17 /**
18  * Post count titles for bbPress version 1.0.2
19  *
20  * ----------------------------------------------------------------------------------
21  *
22  * Copyright (C) 2007 Sam Bauers (sam@viveka.net.au)
23  *
24  * ----------------------------------------------------------------------------------
25  *
26  * LICENSE:
27  *
28  * This program is free software; you can redistribute it and/or modify
29  * it under the terms of the GNU General Public License as published by
30  * the Free Software Foundation; either version 2 of the License, or
31  * (at your option) any later version.
32  *
33  * This program is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36  * GNU General Public License for more details.
37  *
38  * You should have received a copy of the GNU General Public License
39  * along with this program; if not, write to the Free Software
40  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
41  *
42  * ----------------------------------------------------------------------------------
43  *
44  * PHP version 4 and 5
45  *
46  * ----------------------------------------------------------------------------------
47  *
48  * @author    Sam Bauers <sam@viveka.net.au>
49  * @copyright 2007 Sam Bauers
50  * @license   http://www.gnu.org/licenses/gpl.txt GNU General Public License v2
51  * @version   1.0.2
52  **/
53
54
55 /**
56  * Container class for Post count titles
57  *
58  * @author  Sam Bauers
59  * @version 1.0.2
60  **/
61 class Post_Count_Titles
62 {
63     /**
64      * The current version of the plugin
65      *
66      * @var string
67      **/
68     var $version = '1.0.2';
69     
70     
71     /**
72      * Whether the plugin is enabled
73      *
74      * @var boolean
75      **/
76     var $enabled;
77     
78     
79     /**
80      * Whether the plugin has enough settings to work
81      *
82      * @var boolean
83      **/
84     var $active = false;
85     
86     
87     /**
88      * The post count titles in an array
89      *
90      * @var array
91      **/
92     var $titles;
93     
94     
95     /**
96      * The post count titles in an array, but reversed
97      *
98      * @var array
99      **/
100     var $titlesReverse;
101     
102     
103     /**
104      * The format of the post count string
105      *
106      * @var string
107      **/
108     var $format;
109     
110     
111     /**
112      * A cache of users titles
113      *
114      * @var array
115      **/
116     var $cache = array();
117     
118     
119     /**
120      * Pulls out database settings
121      *
122      * @return void
123      * @author Sam Bauers
124      **/
125     function Post_Count_Titles()
126     {
127         // An integer set to 1 for enabled or 0 for disabled
128         $this->enabled = bb_get_option('post_count_titles_enabled');
129         
130         $this->titles = bb_get_option('post_count_titles_titles');
131         
132         if (is_array($this->titles)) {
133             $this->titlesReverse = array_reverse($this->titles, true);
134             $this->active = true;
135         }
136         
137         $this->format = bb_get_option('post_count_titles_format');
138         
139         if (!$this->format) {
140             $this->format = '%ROLE-LINK%<br />%TITLE%<br />%POSTS% posts';
141         }
142     }
143     
144     
145     /**
146      * Returns whether the plugin is active or not
147      *
148      * @return boolean
149      * @author Sam Bauers
150      **/
151     function isActive()
152     {
153         if ($this->enabled && $this->active) {
154             return true;
155         } else {
156             return false;
157         }
158     }
159     
160     
161     /**
162      * Adds a string representing the users post count to the given string
163      *
164      * @return string
165      * @author Sam Bauers
166      **/
167     function addPostTitleToUserTitle($input)
168     {
169         global $bb_post;
170         global $bbdb;
171         global $bb_table_prefix;
172         
173         // Get the user object
174         $user = bb_get_user(get_post_author_id());
175         
176         // Return the cached version to speed things up a bit on repeats
177         if (isset($this->cache[$user->ID])) {
178             return $this->cache[$user->ID];
179         }
180         
181         // Get the live current posts
182         $currentPosts = $bbdb->get_var("SELECT COUNT(post_id) FROM `" . $bbdb->posts . "` WHERE `post_status` = '0' AND `poster_id` = '" . $user->ID . "';");
183         
184         // Work out the title for this user
185         foreach ($this->titlesReverse as $posts => $title) {
186             if ($currentPosts > $posts) {
187                 $currentTitle = $title;
188                 break;
189             }
190         }
191         
192         // Get the profile link from the input
193         if (preg_match('@^(<a[^>]+>)([^<]+)@', $input, $matches)) {
194             $profileLink = $matches[1];
195             $role = $matches[2];
196         } else {
197             $role = $input;
198         }
199         
200         // Transform the output based on the format
201         $output = str_replace(
202             array(
203                 '%ROLE%',
204                 '%ROLE-LINK%',
205                 '%TITLE%',
206                 '%TITLE-LINK%',
207                 '%POSTS%'
208             ),
209             array(
210                 $role,
211                 $input,
212                 $currentTitle,
213                 $profileLink . $currentTitle . '</a>',
214                 $currentPosts
215             ),
216             $this->format
217         );
218         
219         // Cache the result
220         $this->cache[$user->ID] = $output;
221         
222         return $output;
223     }
224 } // END class Post_Count_Titles
225
226
227 // Initialise the class
228 $post_count_titles = new Post_Count_Titles();
229
230
231 // If active, then add filters via API
232 if ($post_count_titles->isActive()) {
233     add_filter('post_author_title', array($post_count_titles, 'addPostTitleToUserTitle'));
234 }
235
236
237 /**
238  * The admin pages below are handled outside of the class due to constraints
239  * in the architecture of the admin menu generation routine in bbPress
240  */
241
242
243 // Add filters for the admin area
244 add_action('bb_admin_menu_generator', 'post_count_titles_admin_page_add');
245 add_action('bb_admin-header.php','post_count_titles_admin_page_process');
246
247
248 /**
249  * Adds in an item to the $bb_admin_submenu array
250  *
251  * @return void
252  * @author Sam Bauers
253  **/
254 function post_count_titles_admin_page_add() {
255     if (function_exists('bb_admin_add_submenu')) { // Build 794+
256         bb_admin_add_submenu(__('Post count titles'), 'use_keys', 'post_count_titles_admin_page');
257     } else {
258         global $bb_submenu;
259         $submenu = array(__('Post count titles'), 'use_keys', 'post_count_titles_admin_page');
260         if (isset($bb_submenu['plugins.php'])) { // Build 740-793
261             $bb_submenu['plugins.php'][] = $submenu;
262         } else { // Build 277-739
263             $bb_submenu['site.php'][] = $submenu;
264         }
265     }
266 }
267
268
269 /**
270  * Writes an admin page for the plugin
271  *
272  * @return string
273  * @author Sam Bauers
274  **/
275 function post_count_titles_admin_page() {
276     $enabled = bb_get_option('post_count_titles_enabled');
277     $titles = bb_get_option('post_count_titles_titles');
278     $format = bb_get_option('post_count_titles_format');
279     
280     if ($enabled) {
281         $enabled_checked = ' checked="checked"';
282     }
283     
284     if (!$titles) {
285         $titles = array(
286             '0' => 'French Fry',
287             '10' => 'Junior Burger',
288             '100' => 'Quarter Pounder',
289             '1000' => 'Big Mac'
290         );
291     }
292     
293     $title_form = array();
294     
295     foreach ($titles as $posts => $title) {
296         $title_form[] = array(
297             'posts' => $posts,
298             'title' => $title
299         );
300     }
301     
302     if (!$format) {
303         $format = '%ROLE-LINK%<br />%TITLE%<br />%POSTS% posts';
304     }
305 ?>
306     <h2>Post count titles</h2>
307     <h3>Enable</h3>
308     <form method="post">
309     <p>
310         <input type="checkbox" name="post_count_titles_enabled" value="1" tabindex="1"<?php echo $enabled_checked; ?> /> Enable post count titles<br />
311         &nbsp;
312     </p>
313     <h3>Titles</h3>
314     <p>
315         "Posts" refers to the number of posts the user must have made to achieve the given "Title"
316     </p>
317     <table>
318         <tr>
319             <th>Posts</th>
320             <th>Title</th>
321         </tr>
322 <?php
323     for ($i = 0; $i < 10; $i++) {
324 ?>
325         <tr>
326             <td><input type="text" name="post_count_titles_posts[<?php echo $i; ?>]" tabindex="<?php echo $i+1; ?>0" value="<?php echo $title_form[$i]['posts']; ?>" /></td>
327             <td><input type="text" name="post_count_titles_title[<?php echo $i; ?>]" tabindex="<?php echo $i+1; ?>1" value="<?php echo $title_form[$i]['title']; ?>" /></td>
328         </tr>
329 <?php
330     }
331 ?>
332     </table>
333     <h3>Format</h3>
334     <p>
335         The format of the string used in place of the normal users role on posts.
336     </p>
337     <ul>
338         <li>%ROLE% - the normal users role</li>
339         <li>%ROLE-LINK% - the normal users role with a link to their profile</li>
340         <li>%TITLE% - the users post count title</li>
341         <li>%TITLE-LINK% - the users post count title with a link to their profile</li>
342         <li>%POSTS% - the users post count</li>
343     </ul>
344     <p>
345         <input type="text" name="post_count_titles_format" value="<?php echo $format; ?>" tabindex="1000" size="50" />
346     </p>
347     <p class="submit alignleft">
348         <input name="submit" type="submit" value="<?php _e('Update'); ?>" tabindex="1001" />
349         <input type="hidden" name="action" value="post_count_titles_update" />
350     </p>
351     </form>
352 <?php
353 }
354
355
356 /**
357  * Processes the admin page form
358  *
359  * @return void
360  * @author Sam Bauers
361  **/
362 function post_count_titles_admin_page_process() {
363     if (isset($_POST['submit'])) {
364         if ('post_count_titles_update' == $_POST['action']) {
365             // Enable post count titles
366             if ($_POST['post_count_titles_enabled']) {
367                 bb_update_option('post_count_titles_enabled', $_POST['post_count_titles_enabled']);
368             } else {
369                 bb_delete_option('post_count_titles_enabled');
370             }
371             
372             // Set an empty titles array
373             $titles = array();
374             
375             // Enable email retrieval from LDAP
376             if ($_POST['post_count_titles_posts'] && $_POST['post_count_titles_title']) {
377                 $posts = $_POST['post_count_titles_posts'];
378                 $title = $_POST['post_count_titles_title'];
379                 
380                 for ($i = 0; $i < 10; $i++) {
381                     if (is_numeric($posts[$i]) && !empty($title[$i])) {
382                         $titles[$posts[$i]] = $title[$i];
383                     }
384                 }
385             }
386             
387             // Save or delete the titles
388             if (count($titles)) {
389                 ksort($titles, SORT_NUMERIC);
390                 bb_update_option('post_count_titles_titles', $titles);
391             } else {
392                 bb_delete_option('post_count_titles_titles');
393             }
394             
395             // Post count titles format
396             if ($_POST['post_count_titles_format']) {
397                 bb_update_option('post_count_titles_format', $_POST['post_count_titles_format']);
398             } else {
399                 bb_delete_option('post_count_titles_format');
400             }
401         }
402     }
403 }
404 ?>
Note: See TracBrowser for help on using the browser.