forked from francescolaffi/BP-Moderation
-
Notifications
You must be signed in to change notification settings - Fork 4
/
bpModLoader.php
255 lines (210 loc) · 6.3 KB
/
bpModLoader.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
/*
Plugin Name: BuddyPress Moderation
Plugin URI: http://buddypress.org/community/groups/bp-moderation/
Description: Plugin for moderation of buddypress communities, it adds a 'report this' link to every content so members can help admin finding inappropriate content.
Version: 0.1.8-dev
Author: Francesco Laffi
Author URI: http://flweb.it
License: GPL2
Text Domain: bp-moderation
Domain Path: /lang/
*/
/* Copyright 2011 Francesco Laffi (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
//load the plugin
new bpModLoader();
/**
* Loader for bp-moderation
*/
class bpModLoader
{
/**
* hook the plugin in buddypress and in activation/deactivation
*/
function __construct()
{
$this->basename = plugin_basename(dirname(__FILE__));
add_action('bp_setup_globals', array( $this, 'setup_globals' ) );
add_action('bp_init', array( $this, 'init' ) );
register_activation_hook(__FILE__, array($this, 'call_activate'));
register_deactivation_hook(__FILE__, array($this, 'call_deactivate'));
}
/**
* Setup globals.
*
* @since 0.2.0
*/
public function setup_globals() {
// register ourselves into BP's active components
$GLOBALS['bp']->moderation = new stdClass;
$GLOBALS['bp']->active_components['moderation'] = 1;
// notifications module
if ( bp_is_active( 'notifications' ) ) {
bpModLoader::load_class( 'bpModNotification' );
bpModNotification::init();
}
}
/**
* Init.
*/
public function init() {
if (is_admin() && !(defined('DOING_AJAX') && DOING_AJAX)) {
if (!empty($_REQUEST['bpmod-action'])) {
$mainclass = 'bpModBackendActions';
} else {
$mainclass = 'bpModBackend';
}
} else {
if (!empty($_REQUEST['bpmod-action']) || (!empty($_REQUEST['action']) && 'bpmodfrontend' === $_REQUEST['action'])) {
$mainclass = 'bpModActions';
} else {
$mainclass = 'bpModFrontend';
}
}
$this->loadL10n();
//create an istance of the main class for this situation
bpModLoader::load_class('bpModeration');
$bpMod =& bpModeration::get_istance($mainclass);
//load the default content types
bpModLoader::load_class('bpModDefaultContentTypes');
bpModDefaultContentTypes::init($bpMod);
do_action('bp_moderation_loaded', array(&$this));
do_action('bp_moderation_init', array(&$bpMod));
}
/**
* Load string translations
*
* will load the first translation found from:
* - wp-content/plugins/bp-moderation-xx_XX.mo
* - wp-content/plugins/bp-moderation/lang/bp-moderation-xx_XX.mo
*
* @return bool if a translation was found and loaded
*/
private function loadL10n(){
return load_plugin_textdomain('bp-moderation')
|| load_plugin_textdomain('bp-moderation', false, $this->basename.'/lang');
}
private function call_installer($method, $args = array())
{
$this->loadL10n();
bpModLoader::load_class('bpModInstaller');
$installer = new bpModInstaller();
call_user_func_array(array($installer,$method), $args);
}
/**
* activation callback
*/
function call_activate()
{
$this->call_installer('activate', func_get_args());
}
/**
* deactivation callback
*/
function call_deactivate()
{
$this->call_installer('deactivate', func_get_args());
}
/**
* load a class
*
* @param string $cname classname
*/
public static function load_class($cname)
{
if (class_exists($cname)) {
return;
}
require_once dirname(__FILE__) . '/classes/' . $cname . '.php';
}
/**
* bpModLoader file path
*
* @return string this file path
*/
static function file()
{
return __FILE__;
}
/**
* generated random data
*/
function test_data()
{
set_time_limit(0);
global $wpdb;
$users = $wpdb->get_col("SELECT ID FROM $wpdb->users WHERE ID != 1");
if (is_multisite()) {
$wpdb->query("DELETE FROM $wpdb->signups");
}
foreach ($users as $id)
{
bp_core_delete_account($id);
}
$ngu = 2; #how much only good users
$ngbu = 2; #how much not only good or only bad users
$nbu = 2; #how much only bad users
$content_types = array('A', 'B', 'C', 'D');
$bpmod =& bpModeration::get_istance();
$statuses = array_keys($bpmod->content_stati);
$n_contents = 20;
$flags_per_cont = 20; # +/- 30%
$goodusers = array();
$badusers = array();
for ($i = 1; $i <= $ngu + $ngbu + $nbu; $i++) {
$uid = bp_core_signup_user('user' . $i, 'pass', $i . '@foo.bar', array());
if (is_multisite()) {
global $wpdb;
$key_sql = "SELECT activation_key FROM $wpdb->signups WHERE user_email = '" . $i . "@foo.bar'";
$key = $wpdb->get_var($key_sql);
} else {
$key = get_user_meta($uid, 'activation_key');
}
$uid = bp_core_activate_signup($key);
is_multisite() and
wp_set_password('pass', $uid);
if ($i <= $ngu + $ngbu) {
$goodusers[] = $uid;
}
if ($i > $ngu) {
$badusers[] = $uid;
}
}
bpModLoader::load_class('bpModObjContent');
bpModLoader::load_class('bpModObjFlag');
for ($i = 1; $i <= $n_contents; $i++) {
$badu = $badusers[mt_rand(0, count($badusers) - 1)];
$cont = new bpModObjContent();
$cont->item_type = $content_types[mt_rand(0, count($content_types) - 1)];
$cont->item_id = mt_rand(1, 1000000);
$cont->item_author = $badu;
$cont->item_date = gmdate("Y-m-d H:i:s", time() - mt_rand(1000000, 2000000));
$cont->status = $statuses[mt_rand(0, count($statuses) - 1)];
$cont->save();
$flags = mt_rand($flags_per_cont * 0.7, $flags_per_cont * 1.3);
for ($j = 1; $j <= $flags; $j++) {
while ($badu == ($goodu = $goodusers[mt_rand(0, count($goodusers) - 1)])) {
;
}
$f = new bpModObjFlag();
$f->content_id = $cont->content_id;
$f->reporter_id = $goodu;
$f->date = gmdate("Y-m-d H:i:s", time() - mt_rand(0, 1000000));
$f->save();
}
}
update_site_option('bp_moderation_test_data_check', 'success');
}
}
?>