-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbbpress-private-topics.php
278 lines (190 loc) · 6.34 KB
/
bbpress-private-topics.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<?php
/*
Plugin Name: bbPress Private Topics
plugin URI:
Description: WPsite bbPress private topics allows only admin and the author of that topic to view a topic. This makes it so that any logged in user cannot see other user's topics.
version: 1.0.0
Author: WPSITE.NET
Author URI: http://www.wpsite.net
License: GPL2
*/
/**
* Global Definitions
*/
// Single Task Groups ID
if (!defined('SINGLE_TASK_GROUP_ID'))
define('SINGLE_TASK_GROUP_ID', 4);
// Single Task Forum ID
if (!defined('SINGLE_TASK_FORUM_ID'))
define('SINGLE_TASK_FORUM_ID', 19);
// Gold Membership Product ID
if (!defined('GOLD_MEMBERSHIP_PRODUCT_ID'))
define('GOLD_MEMBERSHIP_PRODUCT_ID', 8);
// Silver Membership Product ID
if (!defined('SILVER_MEMBERSHIP_PRODUCT_ID'))
define('SILVER_MEMBERSHIP_PRODUCT_ID', 9);
add_action('pre_get_posts', 'wps_private_topics');
/**
* Only display topics to admins and the author of that topic
*
* @access public
* @param mixed $query
* @return void
*/
function wps_private_topics( $query ) {
// If user is admin then just return null because they can see all topics and fourms
if (current_user_can('manage_options')) {
return;
}
// Check if user has a single task membership
global $wpdb;
$groups = $wpdb->get_results(
$wpdb->prepare('SELECT * FROM `' . $wpdb->prefix . 'groups_user_group` WHERE `user_id` = %d',
get_current_user_id()
)
);
$single_task = false;
foreach ($groups as $group) {
if ($group->group_id == SINGLE_TASK_GROUP_ID) {
$single_task = true;
}
}
// Check if displaying Single Task form
if ($single_task) {
// Check if we are trying to retireve topics and that user is not an admin
if (isset($query->query['post_type']) && $query->query['post_type'] == 'topic') {
// Add parameter for dislaying posts only created by the current user
$query->set( 'author__in', array(get_current_user_id()));
return;
}
}
else {
// Check if we are trying to retireve topics and that user is not an admin
if (isset($query->query['post_type']) && ($query->query['post_type'] == 'topic' || $query->query['post_type'] == 'forum')) {
// Add parameter for dislaying posts only created by the current user
$query->set( 'author__in', array(get_current_user_id()));
return;
}
}
}
add_action( 'bbp_new_topic_pre_extras', 'wps_check_topics_in_progress' );
/**
* Return an Error message if user currently has one topic in progress and
* attempts to create another.
*
* @access public
* @param mixed $post_id
* @return void
*/
function wps_check_topics_in_progress( $forum_id ) {
// Get current user ID
$user_id = get_current_user_id();
if ( !current_user_can('manage_options') && $user_id && $forum_id != SINGLE_TASK_FORUM_ID) {
// Get all topics that are in progress
$topics_in_progress = get_posts( array(
'post_type' => 'topic',
'post_parent' => $forum_id,
'author' => $user_id,
'post_status' => array( 'publish', 'pending', 'open' ),
'posts_per_page' => -1
) );
// If there is a topic in progress then thrown an error
if (count($topics_in_progress) > 0) {
bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: You can only have one topic in progress at a time. Please wait until your current topic has been completed before submitting another one.', 'bbpress' ) );
return;
}
}
}
add_action( 'bbp_new_topic_pre_extras', 'wps_check_single_topic_count' );
/**
* Return an Error message if user tries to create more topics then
* the number of single topics they have purchased
*
* @access public
* @param mixed $post_id
* @return void
*/
function wps_check_single_topic_count( $forum_id ) {
// Get current user ID
$user_id = get_current_user_id();
if ( $user_id && $forum_id == SINGLE_TASK_FORUM_ID ) {
// Get all topics that are in progress
$topics = get_posts( array(
'post_type' => 'topic',
'post_parent' => $forum_id,
'author' => $user_id,
'posts_per_page' => -1
) );
$orders = get_posts( array(
'post_type' => 'shop_order',
'post_status' => 'wc-completed',
'author' => $user_id,
'posts_per_page' => -1
) );
global $wpdb;
$order_quantity = 0;
foreach ($orders as $order) {
$item = $wpdb->get_results(
$wpdb->prepare(
'SELECT * FROM `' . $wpdb->prefix . 'woocommerce_order_items` WHERE `order_id` = %d AND `order_item_type` = %s',
$order->ID,
'line_item'
)
);
$quantity = $wpdb->get_results(
$wpdb->prepare(
'SELECT * FROM `' . $wpdb->prefix . 'woocommerce_order_itemmeta` WHERE `order_item_id` = %d AND `meta_key` = %s',
$item[0]->order_item_id,
'_qty'
)
);
$order_quantity += $quantity[0]->meta_value;
}
// If there is a topic in progress then thrown an error
if (count($topics) >= $order_quantity) {
bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: You must purchase another single task before you can create another support ticket. If you have already purchased another tasks your order might still be processing, so please bear with us.', 'bbpress' ) );
return;
}
}
}
add_action( 'woocommerce_add_order_item_meta', 'wps_add_forum', 10, 3);
/**
* Add a new forum when user completes an order
*
* @access public
* @param mixed $order_id
* @return void
*/
function wps_add_forum( $item_id, $values, $cart_item_key ) {
// Get the domain name form the post meta data
foreach ($values['addons'] as $addons) {
$doamin_name = $addons['value'];
}
$topic_data = array(
'post_title' => $doamin_name,
);
// Gold Membership
if ($values['data']->post->ID == GOLD_MEMBERSHIP_PRODUCT_ID) {
// Create a new topic from the domain name
$topic_id = bbp_insert_forum( $topic_data );
//update_post_meta($topic_id, 'groups-groups_read_post', 'gold membership');
}
// Silver Membership
else if ($values['data']->post->ID == SILVER_MEMBERSHIP_PRODUCT_ID) {
// Create a new topic from the domain name
$topic_id = bbp_insert_forum( $topic_data );
//update_post_meta($topic_id, 'groups-groups_read_post', 'silver membership');
}
}
add_filter( 'woocommerce_new_order_data', 'wps_change_post_author_for_order');
/**
* Change the post author to the current user when an order is created
*
* @access public
* @param mixed $order_data
* @return void
*/
function wps_change_post_author_for_order( $order_data ) {
$order_data['post_author'] = get_current_user_id();
return $order_data;
}