-
Notifications
You must be signed in to change notification settings - Fork 0
/
autosub.php
executable file
·192 lines (170 loc) · 6.15 KB
/
autosub.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
<?php
/**
* Plugin Name: Autosubscribe
* Plugin URI: https://indylug.org
* Description: Automatically subscribe users to forum topics
* Version: 0.01
* Author: Josh Williams
* Author https://indylug.org
* License: GPL2
*/
# TODO: Hook into bbp_remove_topic_from_all_subscriptions
defined ('ABSPATH') or die('');
# This function is called when the plugin is installed.
function autosub_activate() {
$all_users = get_users('orderby=ID');
# Save a backup of everyone's subscription preferences so we can restore it if
# we decide not to use the plugin.
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
global $wpdb;
$table_name = $wpdb -> prefix . "autosub";
# Create table for backups if it doesn't already exist
$charset_collate = $wpdb -> get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
user_id mediumint(9) NOT NULL,
type varchar(20) NOT NULL,
subscriptions longtext,
UNIQUE KEY id (id)
) $charset_collate;";
dbDelta($sql);
# Delete old backups if they exist
$wpdb -> delete($table_name, array('type' => 'topic'));
$wpdb -> delete($table_name, array('type' => 'forum'));
# Store backups of subscriptions
foreach ($all_users as $user) {
$user_id = $user -> ID;
# Backup topic subscriptions
$topic_subscriptions = get_user_option('_bbp_subscriptions', $user_id);
if (strlen($topic_subscriptions) <= 0) continue;
$sql = "INSERT INTO $table_name
(user_id, type, subscriptions)
VALUES (
$user_id,
'topic',
'$topic_subscriptions'
);";
dbDelta($sql);
# Backup forum subscriptions
$forum_subscriptions = get_user_option('_bbp_forum_subscriptions', $user_id);
if (strlen($forum_subscriptions) <= 0) continue;
$sql = "INSERT INTO $table_name
(user_id, type, subscriptions)
VALUES (
$user_id,
'forum',
'$forum_subscriptions'
);";
dbDelta($sql);
}
# Delete subscriptions because they would otherwise turn into unsubscriptions.
foreach ($all_users as $user) {
if (!autosub_is_user_an_exception($user -> ID)) {
delete_user_option($user -> ID, '_bbp_subscriptions');
delete_user_option($user -> ID, '_bbp_forum_subscriptions');
}
}
} register_activation_hook(__FILE__, 'autosub_activate');
# This function is called when the plugin is disabled.
function autosub_deactivate() {
$all_users = get_users('orderby=ID');
global $wpdb;
$table_name = $wpdb -> prefix . "autosub";
# Restore topic subscriptions from backups
foreach ($all_users as $user) {
$user_id = $user -> ID;
if (autosub_is_user_an_exception($user_id)) continue;
$SQL = "SELECT subscriptions FROM $table_name
WHERE user_id = $user_id AND type = 'topic'";
$result = $wpdb -> get_results($SQL);
foreach ($result as $row) {
$subscriptions = $row -> subscriptions;
if (strlen($subscriptions) > 0) {
update_user_option($user_id, '_bbp_subscriptions', $subscriptions);
}
}
}
# Restore forum subscriptions from backups
foreach ($all_users as $user) {
$user_id = $user -> ID;
if (autosub_is_user_an_exception($user_id)) continue;
$SQL = "SELECT subscriptions FROM $table_name
WHERE user_id = $user_id AND type = 'forum'";
$result = $wpdb -> get_results($SQL);
foreach ($result as $row) {
$subscriptions = $row -> subscriptions;
if (strlen($subscriptions) > 0) {
update_user_option($user_id, '_bbp_forum_subscriptions', $subscriptions);
}
}
}
} register_deactivation_hook(__FILE__, 'autosub_deactivate');
# Return whether user is exempt from the inversion rule.
function autosub_is_user_an_exception($user_id) {
if (in_array($user_id, autosub_get_exceptions())) {
return true;
} else {
return false;
}
}
# Return array of all members exempt from inversion rule.
function autosub_get_exceptions() {
# Add user IDs here for every exception to the autosubscribe.
return array(-1);
}
function invert_is_user_subscribed_to_topic($is_subscribed, $user_id) {
if (autosub_is_user_an_exception($user_id)) {
return $is_subscribed;
} else {
return !$is_subscribed;
}
} add_filter('bbp_is_user_subscribed_to_topic', 'invert_is_user_subscribed_to_topic', 10, 2);
function invert_is_user_subscribed_to_forum($is_subscribed, $user_id) {
if (autosub_is_user_an_exception($user_id)) {
return $is_subscribed;
} else {
return !$is_subscribed;
}
} add_filter('bbp_is_user_subscribed_to_forum', 'invert_is_user_subscribed_to_forum', 10, 2);
function invert_is_user_subscribed($is_subscribed, $user_id, $object_id) {
# Use this if statement when applying user options later; ie, allow user to
# decide whether to autosubscribe to forums only, topics only, both, or none.
#if (get_post_type($object_id) == bbp_get_topic_post_type()) {
if (autosub_is_user_an_exception($user_id)) {
return $is_subscribed;
} else {
return !$is_subscribed;
}
}
add_filter('bbp_is_user_subscribed', 'invert_is_user_subscribed', 10, 3);
function invert_get_user_subscribe_link($html, $user_id) {
if (autosub_is_user_an_exception($user_id)) return $html;
if (strpos($html, "bbp_unsubscribe") != false) {
$html = str_replace("bbp_unsubscribe", "bbp_subscribe", $html);
} else {
$html = str_replace("bbp_subscribe", "bbp_unsubscribe", $html);
}
return $html;
} add_filter('bbp_get_user_subscribe_link', 'invert_get_user_subscribe_link', 10, 2);
function invert_get_topic_subscribers($users) {
$args = array('fields' => 'id');
$all_users = get_users($args);
$all_users = array_diff($all_users, autosub_get_exceptions());
$send_to_users = array_diff($all_users, $users);
return $send_to_users;
} add_filter('bbp_get_topic_subscribers', 'invert_get_topic_subscribers', 10, 1);
function invert_get_forum_subscribers($users) {
$args = array('fields' => 'id');
$all_users = get_users($args);
$all_users = array_diff($all_users, autosub_get_exceptions());
$send_to_users = array_diff($all_users, $users);
for ($i = 0; $i < count($users); $i++) {
if (autosub_is_user_an_exception($users[$i])) {
unset($users[$i]);
}
}
return $send_to_users;
} add_filter('bbp_get_forum_subscribers', 'invert_get_forum_subscribers', 10, 1);
function invert_form_topic_subscribed($checked, $user_id) {
return "unchecked";
} add_filter('bbp_get_form_topic_subscribed', 'invert_form_topic_subscribed', 10, 2);