| 1 |
<?php |
| 2 |
/** |
| 3 |
* This adds the meta box for the permissions component. This allows restriction to bbpress groups |
| 4 |
*/ |
| 5 |
/** |
| 6 |
*/ |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
add_action( 'admin_menu', 'private_groups_create_meta_box' ); |
| 11 |
|
| 12 |
/* Saves the content permissions metabox data to a custom field. */ |
| 13 |
add_action( 'save_post', 'private_groups_save_meta', 1, 2 ); |
| 14 |
|
| 15 |
|
| 16 |
function private_groups_create_meta_box() { |
| 17 |
add_meta_box( 'forum-group-meta-box', 'Forum Groups', 'private_groups_meta_box', 'forum', 'normal', 'high' ); |
| 18 |
//add_meta_box( 'forum-group-meta-box', 'Forum Groups', 'private_groups_meta_box', 'topic', 'normal', 'high' ); |
| 19 |
//add_meta_box( 'forum-group-meta-box', 'Forum Groups', 'private_groups_meta_box', 'reply', 'normal', 'high' ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Controls the display of the content permissions meta box. This allows users |
| 24 |
* to select groups that should have access to an individual post/page. |
| 25 |
*/ |
| 26 |
|
| 27 |
function private_groups_meta_box( $object, $box ) { |
| 28 |
global $post ?> |
| 29 |
|
| 30 |
<input type="hidden" name="private_groups_meta_nonce" value="<?php echo wp_create_nonce(plugin_basename(__FILE__)); ?>" /> |
| 31 |
|
| 32 |
<p> |
| 33 |
<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.', 'private_groups'); ?></label> |
| 34 |
</p> |
| 35 |
|
| 36 |
<div style="overflow: hidden;"> |
| 37 |
|
| 38 |
<?php |
| 39 |
|
| 40 |
/* Get the meta*/ |
| 41 |
|
| 42 |
$meta = get_post_meta( $post->ID, '_private_group', false ); |
| 43 |
global $rpg_groups ; |
| 44 |
|
| 45 |
/* Loop through each of the available roles. */ |
| 46 |
if(empty($rpg_groups)) { |
| 47 |
echo '<b>No groups have yet been set up - go to Dashboard>Settings>bbp Private Groups to set</b>' ; |
| 48 |
} |
| 49 |
else { |
| 50 |
foreach ( $rpg_groups as $group => $details ) { |
| 51 |
$checked = false; |
| 52 |
|
| 53 |
/* If the role has been selected, make sure it's checked. */ |
| 54 |
if ( is_array( $meta ) && in_array( $group, $meta ) ) |
| 55 |
$checked = ' checked="checked" '; ?> |
| 56 |
|
| 57 |
<p style="width: 32%; float: left; margin-right: 0;"> |
| 58 |
<label for="group-<?php echo $group; ?>"> |
| 59 |
<input type="checkbox" name=<?php echo $group ; ?> id=<?php echo $group ; ?> <?php echo $checked; ?> value="<?php echo $group; ?>" /> |
| 60 |
<?php echo $group." ".$details ; ?> |
| 61 |
</label> |
| 62 |
</p> |
| 63 |
|
| 64 |
<?php }} ?> |
| 65 |
|
| 66 |
</div><?php |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Saves the content permissions metabox data to a custom field. |
| 71 |
* |
| 72 |
*/ |
| 73 |
function private_groups_save_meta( $post_id, $post ) { |
| 74 |
global $rpg_groups ; |
| 75 |
|
| 76 |
/* Only allow users that can edit the current post to submit data. */ |
| 77 |
if ( 'post' == $post->post_type && !current_user_can( 'edit_posts', $post_id ) ) |
| 78 |
return; |
| 79 |
|
| 80 |
/* Only allow users that can edit the current page to submit data. */ |
| 81 |
elseif ( 'page' == $post->post_type && !current_user_can( 'edit_pages', $post_id ) ) |
| 82 |
return; |
| 83 |
|
| 84 |
/* Don't save if the post is only a revision. */ |
| 85 |
if ( 'revision' == $post->post_type ) |
| 86 |
return; |
| 87 |
|
| 88 |
/* Loop through each of the site's available roles. */ |
| 89 |
if(!empty($rpg_groups)) { |
| 90 |
foreach ( $rpg_groups as $group => $details ){ |
| 91 |
|
| 92 |
|
| 93 |
/* Get post metadata for the custom field key 'group'. */ |
| 94 |
$meta = (array)get_post_meta( $post_id, '_private_group', false ); |
| 95 |
|
| 96 |
/* Check if the group was selected. */ |
| 97 |
if ( $_POST[$group] ) { |
| 98 |
|
| 99 |
/* If selected and already saved, continue looping through the roles and do nothing for this role. */ |
| 100 |
if ( in_array( $group, $meta ) ) |
| 101 |
continue; |
| 102 |
|
| 103 |
/* If the group was selected and not already saved, add the group as a new value to the 'group' custom field. */ |
| 104 |
else |
| 105 |
add_post_meta( $post_id, '_private_group', $group, false ); |
| 106 |
} |
| 107 |
|
| 108 |
/* If role not selected, delete. */ |
| 109 |
else |
| 110 |
delete_post_meta( $post_id, '_private_group', $group ); |
| 111 |
|
| 112 |
} // End loop through site's groups. |
| 113 |
} |
| 114 |
} |
| 115 |
?> |