| 1 |
<?php |
| 2 |
/** |
| 3 |
* This adds the meta box for the private groups |
| 4 |
*/ |
| 5 |
/** |
| 6 |
*/ |
| 7 |
|
| 8 |
add_action( 'admin_menu', 'private_groups_create_meta_box' ); |
| 9 |
|
| 10 |
/* Saves the content permissions metabox data to a custom field. */ |
| 11 |
add_action( 'save_post', 'private_groups_save_meta', 1, 2 ); |
| 12 |
|
| 13 |
|
| 14 |
function private_groups_create_meta_box() { |
| 15 |
add_meta_box( 'forum-group-meta-box', __( 'Forum Groups' , 'bbp-private-groups') , 'private_groups_meta_box', 'forum', 'normal', 'high' ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Controls the display of the content permissions meta box. This allows users |
| 20 |
* to select groups that should have access to an individual post/page. |
| 21 |
*/ |
| 22 |
|
| 23 |
function private_groups_meta_box( $object, $box ) { |
| 24 |
global $post ?> |
| 25 |
|
| 26 |
<input type="hidden" name="private_groups_meta_nonce" value="<?php echo wp_create_nonce(plugin_basename(__FILE__)); ?>" /> |
| 27 |
<?php // Set a custom flag so that it only saves if we are in full edit, not bulk edit ?> |
| 28 |
<input type="hidden" name="pg_hidden_flag" value="true" /> |
| 29 |
|
| 30 |
<p> |
| 31 |
<label for="groups"><?php _e('<strong>Groups:</strong> Restrict the content to these groups on the front end of the site. If all boxes are left unchecked, everyone can view the content.', 'bbp-private-groups'); ?></label> |
| 32 |
</p> |
| 33 |
|
| 34 |
<div style="overflow: hidden;"> |
| 35 |
|
| 36 |
|
| 37 |
<?php |
| 38 |
|
| 39 |
/* Get the meta*/ |
| 40 |
|
| 41 |
$meta = get_post_meta( $post->ID, '_private_group', false ); |
| 42 |
global $rpg_groups ; |
| 43 |
global $rpg_disable_groups ; |
| 44 |
|
| 45 |
/* Loop through each of the available roles. */ |
| 46 |
if(empty($rpg_groups)) { |
| 47 |
echo _e('<b>No groups have yet been set up - go to Dashboard>Settings>bbp Private Groups to set</b>', 'bbp-private-groups') ; |
| 48 |
} |
| 49 |
else { |
| 50 |
foreach ( $rpg_groups as $group => $details ) { |
| 51 |
//if it isn't disabled... |
| 52 |
if (empty ( $rpg_disable_groups [$group] )) { |
| 53 |
$checked = false; |
| 54 |
|
| 55 |
/* If the role has been selected, make sure it's checked. */ |
| 56 |
if ( is_array( $meta ) && in_array( $group, $meta ) ) $checked = ' checked="checked" '; ?> |
| 57 |
<p style="width: 32%; float: left; margin-right: 0;"> |
| 58 |
<label for="group-<?php echo $group; ?>"> |
| 59 |
<?php $groupname=__('Group','bbp-private-groups').substr($group,5,strlen($group)) ; ?> |
| 60 |
<input type="checkbox" name=<?php echo $group ; ?> id=<?php echo $group ; ?> <?php echo $checked; ?> value="<?php echo $group; ?>" /> |
| 61 |
<?php echo $groupname." ".$details ; ?> |
| 62 |
</label> |
| 63 |
</p> |
| 64 |
<?php |
| 65 |
} |
| 66 |
} |
| 67 |
?> |
| 68 |
<?php |
| 69 |
global $rpg_topic_permissions ; |
| 70 |
if (!empty ($rpg_topic_permissions['activate']) ) { |
| 71 |
?> |
| 72 |
<div> |
| 73 |
<label for="group_topics"><?php _e('<strong>Group Topic Permsissions:</strong> <i> These will appear for any groups set for this forum when you publish or update this forum.</i>', 'bbp-private-groups'); ?></label></p> |
| 74 |
<p> |
| 75 |
<label for="group_topics"><?php _e('For each group, you can set whether group members are able to view, create topics and replies.', 'bbp-private-groups'); ?></label> |
| 76 |
</p> |
| 77 |
<p> |
| 78 |
<label for="group_topics"><?php |
| 79 |
_e('Please see the ', 'bbp-private-groups') ; |
| 80 |
echo '<a href="' . site_url() . '/wp-admin/options-general.php?page=bbp-private-group-settings&tab=topic_permissions" target="_blank">' ; |
| 81 |
_e ('Topic Permissions settings', 'bbp-private-groups') ; |
| 82 |
echo '</a>' ; |
| 83 |
_e ('to understand the impact of this', 'bbp-private-groups'); ?> |
| 84 |
</label> |
| 85 |
</p> |
| 86 |
<table> |
| 87 |
<?php foreach ( $rpg_groups as $group => $details ) { ?> |
| 88 |
|
| 89 |
<?php |
| 90 |
|
| 91 |
//if this group is selected |
| 92 |
if ( is_array( $meta ) && in_array( $group, $meta ) ) { |
| 93 |
//set up $test, so we can see if we need to display non-logged in section below, it will be null unless at least one group is set, otherwise it looks like |
| 94 |
//an open forum with no grouops can;t be seen by non-logged in, which of course isn't true. |
| 95 |
$test = true ; |
| 96 |
$groupname=__('Group','bbp-private-groups').substr($group,5,strlen($group)) ; |
| 97 |
$tp = '_private_group_'.$group ; |
| 98 |
$valuename1 = __('Only View Topics/Replies', 'bbp-private-groups') ; |
| 99 |
$valuename2 = __('Create/Edit Topics (and view all topics/replies)', 'bbp-private-groups') ; |
| 100 |
$valuename3 = __('Create/Edit Replies (and view all topics/replies)', 'bbp-private-groups') ; |
| 101 |
$valuename4 = __('Create/Edit Topics and Replies (and view all topics/replies)', 'bbp-private-groups') ; |
| 102 |
$valuename5 = __('Create/Edit/view OWN Topics, create/edit Replies to those topics and view any Replies to those topics', 'bbp-private-groups') ; |
| 103 |
$perm = get_post_meta($post->ID , $tp, true) ; |
| 104 |
$value = (!empty ($perm ) ? $perm : '4' ); |
| 105 |
$valuex = 'valuename'.$value ; |
| 106 |
$valuename = $$valuex ; |
| 107 |
?> |
| 108 |
|
| 109 |
<tr> |
| 110 |
<td> |
| 111 |
</td> |
| 112 |
<td> |
| 113 |
</td> |
| 114 |
<td style ="width:150px"> |
| 115 |
<?php echo _e('<b>For this forum...</b>', 'bbp-private-groups'); ?> |
| 116 |
</td> |
| 117 |
</tr> |
| 118 |
<tr> |
| 119 |
<td> <?php echo $groupname ; ?> </td> |
| 120 |
<td style ="width:250px"> <?php echo $details ; ?> </td> |
| 121 |
<td style ="width:150px"> |
| 122 |
<select name="<?php echo $tp ; ?>"> |
| 123 |
<?php echo '<option value="'.esc_html( $value).'">'.esc_html( $valuename) ; |
| 124 |
|
| 125 |
if ($value != 4) echo '<option value="4">'.$valuename4.'</option>' ; |
| 126 |
if ($value != 2) echo '<option value="2">'.$valuename2.'</option>' ; |
| 127 |
if ($value != 3) echo '<option value="3">'.$valuename3.'</option>' ; |
| 128 |
if ($value != 5) echo '<option value="5">'.$valuename5.'</option>' ; |
| 129 |
if ($value != 1) echo '<option value="1">'.$valuename1.'</option>' ; |
| 130 |
?> |
| 131 |
|
| 132 |
|
| 133 |
</select> |
| 134 |
</td> |
| 135 |
</tr> |
| 136 |
|
| 137 |
<?php |
| 138 |
|
| 139 |
} // end of if is array |
| 140 |
|
| 141 |
} // end of foreach |
| 142 |
|
| 143 |
//now we'll work out if we need to allow non-logged in /anonymous viewing or posting |
| 144 |
|
| 145 |
if (!empty($test)) { |
| 146 |
//if $test is not true, then we don't want to display this, as it looks like we are setting to no view |
| 147 |
?> |
| 148 |
<tr> |
| 149 |
<td> |
| 150 |
</td> |
| 151 |
<td> |
| 152 |
</td> |
| 153 |
<td colspan=2> |
| 154 |
<?php echo _e('<b>For this forum...</b>', 'bbp-private-groups'); ?> |
| 155 |
|
| 156 |
|
| 157 |
</td> |
| 158 |
</tr> |
| 159 |
<tr> |
| 160 |
<?php if (bbp_allow_anonymous()) { |
| 161 |
$desc = __('Non-logged in users & Anonymous Users', 'bbp-private-groups'); |
| 162 |
} |
| 163 |
else { |
| 164 |
$desc = __('Non-logged in users', 'bbp-private-groups'); |
| 165 |
} ?> |
| 166 |
<td colspan=2> |
| 167 |
<?php echo $desc; ?> |
| 168 |
<?php $name = '_private_group_nonloggedin' ; |
| 169 |
$item = get_post_meta( $post->ID, '_private_group_nonloggedin', true ); |
| 170 |
if (empty ($item)) $item=0 ; |
| 171 |
?> |
| 172 |
</td> |
| 173 |
|
| 174 |
<?php |
| 175 |
|
| 176 |
//set up the anon paramters |
| 177 |
$tp = '_private_group_nonloggedin' ; |
| 178 |
$valuename0 = __('No visibility or access to this forum', 'bbp-private-groups') ; |
| 179 |
$valuename1 = __('Only View Topics/Replies', 'bbp-private-groups') ; |
| 180 |
$valuename2 = __('Create/Edit Topics (and view all topics/replies)', 'bbp-private-groups') ; |
| 181 |
$valuename3 = __('Create/Edit Replies (and view all topics/replies)', 'bbp-private-groups') ; |
| 182 |
$valuename4 = __('Create/Edit Topics and Replies (and view all topics/replies)', 'bbp-private-groups') ; |
| 183 |
$perm = get_post_meta($post->ID , $tp, true) ; |
| 184 |
$value = (!empty ($perm ) ? $perm : '0' ); |
| 185 |
//now reset value to just view if anon posting not allowed, but this has been set higher previously |
| 186 |
if (!bbp_allow_anonymous()) if ($value>1) $value = 1 ; |
| 187 |
$valuex = 'valuename'.$value ; |
| 188 |
$valuename = $$valuex ; |
| 189 |
?> |
| 190 |
|
| 191 |
|
| 192 |
<td style ="width:250px"> |
| 193 |
<select name="<?php echo $tp ; ?>"> |
| 194 |
<?php echo '<option value="'.esc_html( $value).'">'.esc_html( $valuename) ; |
| 195 |
if ($value != 0) echo '<option value="0">'.$valuename0.'</option>' ; |
| 196 |
if ($value != 1) echo '<option value="1">'.$valuename1.'</option>' ; |
| 197 |
if (bbp_allow_anonymous()) { |
| 198 |
if ($value != 2) echo '<option value="2">'.$valuename2.'</option>' ; |
| 199 |
if ($value != 3) echo '<option value="3">'.$valuename3.'</option>' ; |
| 200 |
if ($value != 4) echo '<option value="4">'.$valuename4.'</option>' ; |
| 201 |
} |
| 202 |
} //end of if $test |
| 203 |
|
| 204 |
?> |
| 205 |
|
| 206 |
|
| 207 |
</select> |
| 208 |
</td> |
| 209 |
|
| 210 |
|
| 211 |
</tr> |
| 212 |
|
| 213 |
|
| 214 |
</table> |
| 215 |
|
| 216 |
</div> |
| 217 |
|
| 218 |
<?php } // end of if $rpg_topic_permissions |
| 219 |
|
| 220 |
} //end of else ?> |
| 221 |
</div> |
| 222 |
|
| 223 |
<?php |
| 224 |
} |
| 225 |
/** |
| 226 |
* Saves the content permissions metabox data to a custom field. |
| 227 |
* |
| 228 |
*/ |
| 229 |
function private_groups_save_meta( $post_id, $post ) { |
| 230 |
//bail of not a forum |
| 231 |
if ( bbp_get_forum_post_type() != $post->post_type ) |
| 232 |
return; |
| 233 |
|
| 234 |
//bail if this creating a new forum and we are entering, so we don't have a post_id yet ! |
| 235 |
if (empty ($post_id)) |
| 236 |
return ; |
| 237 |
|
| 238 |
/* Only allow users that can edit the current post to submit data. */ |
| 239 |
if ( 'post' == $post->post_type && !current_user_can( 'edit_posts', $post_id ) ) |
| 240 |
return; |
| 241 |
|
| 242 |
/* Only allow users that can edit the current page to submit data. */ |
| 243 |
elseif ( 'page' == $post->post_type && !current_user_can( 'edit_pages', $post_id ) ) |
| 244 |
return; |
| 245 |
|
| 246 |
/* Don't save if the post is only a revision. */ |
| 247 |
if ( 'revision' == $post->post_type ) |
| 248 |
return; |
| 249 |
|
| 250 |
// handle the case when the custom post is quick edited |
| 251 |
// otherwise all custom meta fields are cleared out |
| 252 |
// Only do this if our custom flag is present |
| 253 |
if (!isset($_POST['pg_hidden_flag'])) |
| 254 |
return ; |
| 255 |
|
| 256 |
|
| 257 |
global $rpg_groups ; |
| 258 |
/* Loop through each of the site's available groups. */ |
| 259 |
if(!empty($rpg_groups)) { |
| 260 |
|
| 261 |
foreach ( $rpg_groups as $group => $details ){ |
| 262 |
//Set topic permissions for this group |
| 263 |
$tp = '_private_group_'.$group ; |
| 264 |
if (!empty ($_POST[$tp]) ) update_post_meta( $post_id, $tp, $_POST[$tp], false ); |
| 265 |
|
| 266 |
/* Get post metadata for the custom field key 'group'. */ |
| 267 |
$meta = (array)get_post_meta( $post_id, '_private_group', false ); |
| 268 |
|
| 269 |
/* Check if the group was selected. */ |
| 270 |
if ( !empty ($_POST[$group] )){ |
| 271 |
|
| 272 |
/* If selected and already saved, continue looping through the groups and do nothing for this group. */ |
| 273 |
if ( in_array( $group, $meta ) ) |
| 274 |
continue; |
| 275 |
|
| 276 |
/* If the group was selected and not already saved, add the group as a new value to the 'group' custom field. */ |
| 277 |
else |
| 278 |
add_post_meta( $post_id, '_private_group', $group, false ); |
| 279 |
} |
| 280 |
|
| 281 |
/* If role not selected, delete. */ |
| 282 |
else { |
| 283 |
delete_post_meta( $post_id, '_private_group', $group ); |
| 284 |
//and delete topic permissions also |
| 285 |
delete_post_meta( $post_id, $tp); |
| 286 |
//now if we are deleting a group that was previously set, then we need to set a check for later |
| 287 |
if ( in_array( $group, $meta ) ) { |
| 288 |
$subs_check = true ; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
} // End of foreach |
| 293 |
|
| 294 |
//now set nonlogged in permissions |
| 295 |
if (!empty ($_POST['_private_group_nonloggedin'] )) { |
| 296 |
update_post_meta( $post_id, '_private_group_nonloggedin', $_POST['_private_group_nonloggedin'], false ); |
| 297 |
} |
| 298 |
else //delete |
| 299 |
delete_post_meta( $post_id, '_private_group_nonloggedin'); |
| 300 |
|
| 301 |
//now if we need to alter subscriptions |
| 302 |
if ( !empty ($subs_check) && $subs_check == true ) { |
| 303 |
//go through all users and unset any subscriptions for this forum for those users |
| 304 |
$users= get_users () ; |
| 305 |
//loop through all users |
| 306 |
foreach ( $users as $user ) { |
| 307 |
//and within each |
| 308 |
$user_id = $user->ID ; |
| 309 |
rpg_amend_subscriptions ($user_id) ; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
} |
| 315 |
} |
| 316 |
?> |