This repository has been archived by the owner on May 13, 2018. It is now read-only.
/
bb-anonymous-posting.php
210 lines (182 loc) · 6.77 KB
/
bb-anonymous-posting.php
1
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/*
Plugin Name: BB Anonymous Posting
Plugin URI: http://www.adityanaik.com/projects/plugins/bb-anonymous-posting/
Description: Allows anonymous users to add posts and topics
Author: Aditya Naik
Version: 2.2
Author URI: http://www.adityanaik.com/
*/
// before a form is shown spoof the user
add_action('pre_post_form', 'bb_anon_spoof_user');
// after displaying the form unspoof the user so it
// does not affect any of the other functionality
add_action('post_post_form', 'bb_anon_unspoof_user');
// initialization
add_action('bb_init', 'bb_anon_init');
// add a location for the bb-post page
add_filter('bb_get_location', 'bb_anon_get_bb_post', 10, 2);
function bb_anon_get_bb_post($loc = '', $file = '') {
if (bb_find_filename('/' . $file) == 'bb-post.php')
return 'bb-post';
}
// initialization
// add anonymous role
// if the location is bb-post page spoof user
function bb_anon_init() {
global $bb_roles;
$perm_array = array (
'write_posts' => true,
'read' => true
);
if (bb_get_option('bb_anon_write_topics') == 'Y')
$perm_array['write_topics'] = true;
$bb_roles->add_role('anonymous', 'Anonymous', $perm_array);
if ('bb-post' == bb_get_location())
bb_anon_spoof_user();
}
// remove auth function for the bb-post page
// !function_exists is used only during plugin activation
if (!function_exists('bb_auth') && 'bb-post' == bb_get_location()) {
function bb_auth() {}
}
// meat of the plugin
function bb_anon_spoof_user() {
global $bb_current_user;
if (!$bb_current_user) {
$anon_id = bb_get_option('bb_anon_user_id');
if (!$anon_id) {
bb_anon_activate_plugin();
$anon_id = bb_get_option('bb_anon_user_id');
}
$bb_current_user = new BP_User($anon_id);
}
}
// remove meat of the plugin
function bb_anon_unspoof_user() {
global $bb_current_user, $bb_roles;
if ($bb_current_user->has_cap('anonymous')) {
$bb_current_user = false;
}
if (is_forum() && bb_get_option('bb_anon_write_topics') != "Y") {
echo '<p>';
printf(__('You must <a href="%s">log in</a> to post.'), attribute_escape( bb_get_option('uri') . 'bb-login.php' ));
echo '</p>';
}
}
// activate plugin
// create a anonymous user
//
// if the option for the anonmous user id is set, set the user id to that value
// this is to keep the user id consitent if you deactivate and activate it a couple of times
//
// add anonymous roles
add_action('bb_activate_plugin_' . bb_plugin_basename(__FILE__), 'bb_anon_activate_plugin');
function bb_anon_activate_plugin() {
global $bbdb;
$bbdb->query("INSERT INTO $bbdb->users (user_login,user_nicename, user_registered) VALUES ('anonymous','Anonymous', '" . bb_current_time('mysql') . "')");
if ($anon_id = bb_get_option('bb_anon_user_id')) {
$bbdb->query("UPDATE $bbdb->users SET ID = $anon_id where ID = " . $bbdb->insert_id);
} else {
$anon_id = $bbdb->insert_id;
bb_update_option('bb_anon_user_id', $anon_id);
}
$user = new BP_User($anon_id);
$user->add_role('anonymous');
$user->remove_role('member');
$user->add_cap('anonymous');
$user->remove_cap('member');
}
// remove user and meta data on deactivation
// do note remove the option in case you activate the plugin again later
add_action('bb_deactivate_plugin_' . bb_plugin_basename(__FILE__), 'bb_anon_deactivate_plugin');
function bb_anon_deactivate_plugin() {
global $bbdb;
$anon_id = bb_get_option('bb_anon_user_id');
$bbdb->query("DELETE FROM $bbdb->users WHERE ID = '$anon_id'");
$bbdb->query("DELETE FROM $bbdb->usermeta WHERE user_id = '$anon_id'");
}
// fixes the frontpage link to add new post
add_filter('new_topic_url', 'bb_anon_filter_new_topic_url');
function bb_anon_filter_new_topic_url($url) {
if (is_front() && bb_get_option('bb_anon_write_topics') == "Y")
$url = add_query_arg('new', '1', bb_get_option('uri'));
return $url;
}
// this fixes the display name and title for the anonymous user on the topic page
add_filter('get_post_author_id', 'bb_anon_filter_poster_id');
function bb_anon_filter_poster_id($poster_id) {
if ($poster_id == bb_get_option('bb_anon_user_id'))
return 0;
else
return $poster_id;
}
// this fixes the display names for topic last poster or topic author
add_filter('topic_last_poster','bb_anon_filter_poster',10,2);
add_filter('get_topic_author','bb_anon_filter_poster',10,2);
function bb_anon_filter_poster($last_poster, $poster_id) {
if ($poster_id == bb_get_option('bb_anon_user_id'))
return __('Anonymous');
else
return $last_poster;
}
// admin functionality
if (!BB_IS_ADMIN)
return;
add_action( 'bb_admin_menu_generator', 'bb_anon_add_admin_page' );
function bb_anon_add_admin_page() {
bb_admin_add_submenu(__('Anonymous Posting'), 'use_keys', 'bb_anon_settings_page', 'options-general.php');
}
function bb_anon_settings_page() {
$write_topics = bb_get_option('bb_anon_write_topics');
?>
<h2>Registration Settings</h2>
<form class="options" method="post">
<fieldset>
<label for="">
Let Anonymous Users create new Topics
</label>
<div>
Yes <input <?php if($write_topics == 'Y') echo ' checked="checked" ' ?> type="radio" name="bb_anon_write_topics" id="bb_anon_write_topics_y" value="Y"/>
No <input <?php if($write_topics != 'Y') echo ' checked="checked" ' ?> type="radio" name="bb_anon_write_topics" id="bb_anon_write_topics_n" value="N"/>
<p>By default if the plugin is enabled the users can create new posts. Selecting yes above will let users create new topics also.</p>
</div>
<p class="submit">
<input type="submit" name="bb_anon_submit_options" value="Update Options" />
</p>
</fieldset>
</form>
<?php
}
add_action('bb_anon_settings_page_pre_head','bb_anon_settings_page_process');
function bb_anon_settings_page_process() {
if(isset($_POST['bb_anon_submit_options'])) {
$anon_id = bb_get_option('bb_anon_user_id');
$user = new BP_User($anon_id);
if($_POST['bb_anon_write_topics'] == Y) {
bb_update_option('bb_anon_write_topics',"Y");
//$user->add_cap('write_topics');
} else {
bb_update_option('bb_anon_write_topics',"N");
//$user->remove_cap('write_topics');
}
$goback = add_query_arg('bb-anon-options-updated', 'true', wp_get_referer());
bb_safe_redirect($goback);
}
if (isset($_GET['bb-anon-options-updated'])) {
bb_admin_notice( __('Options Updated.') );
}
}
add_action('bb_admin-header.php','bb_anon_user_filter');
function bb_anon_user_filter() {
global $bb_user_search;
if($bb_user_search)
foreach ( $bb_user_search->results as $key => $user_id ) {
if ($user_id == bb_get_option('bb_anon_user_id')) {
unset($bb_user_search->results[$key]);
$bb_user_search->total_users_for_query = $bb_user_search->total_users_for_query -1;
break;
}
}
}
?>