| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 174 |
$user = bb_get_user(get_post_author_id()); |
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 |
if (isset($this->cache[$user->ID])) { |
|---|
| 178 |
return $this->cache[$user->ID]; |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 |
$currentPosts = $bbdb->get_var("SELECT COUNT(post_id) FROM `" . $bbdb->posts . "` WHERE `post_status` = '0' AND `poster_id` = '" . $user->ID . "';"); |
|---|
| 183 |
|
|---|
| 184 |
|
|---|
| 185 |
foreach ($this->titlesReverse as $posts => $title) { |
|---|
| 186 |
if ($currentPosts > $posts) { |
|---|
| 187 |
$currentTitle = $title; |
|---|
| 188 |
break; |
|---|
| 189 |
} |
|---|
| 190 |
} |
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 |
if (preg_match('@^(<a[^>]+>)([^<]+)@', $input, $matches)) { |
|---|
| 194 |
$profileLink = $matches[1]; |
|---|
| 195 |
$role = $matches[2]; |
|---|
| 196 |
} else { |
|---|
| 197 |
$role = $input; |
|---|
| 198 |
} |
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 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 |
|
|---|
| 220 |
$this->cache[$user->ID] = $output; |
|---|
| 221 |
|
|---|
| 222 |
return $output; |
|---|
| 223 |
} |
|---|
| 224 |
} |
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
// Initialise the class |
|---|
| 228 |
$post_count_titles = new Post_Count_Titles(); |
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
if ($post_count_titles->isActive()) { |
|---|
| 233 |
add_filter('post_author_title', array($post_count_titles, 'addPostTitleToUserTitle')); |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 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 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
function post_count_titles_admin_page_add() { |
|---|
| 255 |
if (function_exists('bb_admin_add_submenu')) { |
|---|
| 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'])) { |
|---|
| 261 |
$bb_submenu['plugins.php'][] = $submenu; |
|---|
| 262 |
} else { |
|---|
| 263 |
$bb_submenu['site.php'][] = $submenu; |
|---|
| 264 |
} |
|---|
| 265 |
} |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
|
|---|
| 362 |
function post_count_titles_admin_page_process() { |
|---|
| 363 |
if (isset($_POST['submit'])) { |
|---|
| 364 |
if ('post_count_titles_update' == $_POST['action']) { |
|---|
| 365 |
|
|---|
| 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 |
|
|---|
| 373 |
$titles = array(); |
|---|
| 374 |
|
|---|
| 375 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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 |
?> |
|---|