Skip to content

Instantly share code, notes, and snippets.

@Vheissu
Created July 6, 2013 13:51
Show Gist options
  • Save Vheissu/5939950 to your computer and use it in GitHub Desktop.
Save Vheissu/5939950 to your computer and use it in GitHub Desktop.
Custom post type support for the WPMU Dev Membership plugin
class M_Recipes extends M_Rule {
var $name = 'recipes';
var $label = 'Recipes';
var $description = 'Allows specific recipes to be protected.';
var $rulearea = 'public';
function admin_main($data) {
if(!$data) $data = array();
?>
<div class='level-operation' id='main-recipes'>
<h2 class='sidebar-name'><?php _e('Recipes', 'membership');?><span><a href='#remove' id='remove-recipes' class='removelink' title='<?php _e("Remove Recipes from this rules area.",'membership'); ?>'><?php _e('Remove','membership'); ?></a></span></h2>
<div class='inner-operation'>
<p><?php _e('Select the posts to be covered by this rule by checking the box next to the relevant Recipes title.','membership'); ?></p>
<?php
$args = array(
'numberposts' => MEMBERSHIP_POST_COUNT,
'offset' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'recipes',
'post_status' => 'publish'
);
$posts = get_posts($args);
if($posts) {
?>
<table cellspacing="0" class="widefat fixed">
<thead>
<tr>
<th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th>
<th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Recipes title', 'membership'); ?></th>
<th style="" class="manage-column column-date" id="date" scope="col"><?php _e('Recipes date', 'membership'); ?></th>
</tr>
</thead>
<tfoot>
<tr>
<th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th>
<th style="" class="manage-column column-name" id="name" scope="col"><?php _e('Recipes title', 'membership'); ?></th>
<th style="" class="manage-column column-date" id="date" scope="col"><?php _e('Recipes date', 'membership'); ?></th>
</tr>
</tfoot>
<tbody>
<?php
foreach($posts as $key => $post) {
?>
<tr valign="middle" class="alternate" id="recipe-<?php echo $post->ID; ?>">
<th class="check-column" scope="row">
<input type="checkbox" value="<?php echo $post->ID; ?>" name="recipes[]" <?php if(in_array($post->ID, $data)) echo 'checked="checked"'; ?>>
</th>
<td class="column-name">
<strong><?php echo esc_html($post->post_title); ?></strong>
</td>
<td class="column-date">
<?php
echo date("Y/m/d", strtotime($post->post_date));
?>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
<?php
}
?>
<p class='description'><?php echo sprintf(__("Only the most recent %d recipes are shown above, if you have more than that then you should consider using categories instead.",'membership'), MEMBERSHIP_POST_COUNT); ?></p>
</div>
</div>
<?php
}
function redirect() {
membership_redirect_to_protected();
}
function get_group() {
global $wpdb;
$sql = $wpdb->prepare( "SELECT id FROM " . membership_db_prefix($wpdb, 'urlgroups') . " WHERE groupname = %s ORDER BY id DESC LIMIT 0,1", '_posts-' . $this->level_id );
$results = $wpdb->get_var( $sql );
if(!empty($results)) {
return $results;
} else {
return false;
}
}
function on_positive($data) {
$this->data = $data;
add_action('pre_get_posts', array(&$this, 'add_viewable_recipes'), 99 );
add_filter( 'the_posts', array(&$this, 'check_positive_recipes'));
}
function on_negative($data) {
$this->data = $data;
add_action('pre_get_posts', array(&$this, 'add_unviewable_recipes'), 99 );
add_filter( 'the_posts', array(&$this, 'check_negative_recipes'));
}
function add_viewable_recipes($wp_query) {
global $M_options;
if( !$wp_query->is_singular && empty($wp_query->query_vars['pagename']) && (!isset($wp_query->query_vars['post_type']) || in_array($wp_query->query_vars['post_type'], array('recipes','')))) {
// We are in a list rather than on a single post
foreach( (array) $this->data as $key => $value ) {
$wp_query->query_vars['post__in'][] = $value;
}
$wp_query->query_vars['post__in'] = array_unique($wp_query->query_vars['post__in']);
} else {
// We are on a single post - wait until we get to the_posts
}
}
function add_unviewable_recipes($wp_query) {
global $M_options;
if( !$wp_query->is_singular && empty($wp_query->query_vars['pagename']) && (!isset($wp_query->query_vars['post_type']) || in_array($wp_query->query_vars['post_type'], array('recipes','')))) {
// We are on a list rather than on a single post
foreach( (array) $this->data as $key => $value ) {
$wp_query->query_vars['post__not_in'][] = $value;
}
$wp_query->query_vars['post__not_in'] = array_unique($wp_query->query_vars['post__not_in']);
} else {
// We are on a single post - wait until we get to the_posts
}
}
function check_negative_recipes( $posts ) {
global $wp_query, $M_options;
if( !$wp_query->is_singular || count($posts) > 1) {
return $posts;
}
if(!empty($posts) && count($posts) == 1) {
// we may be on a restricted post so check the URL and redirect if needed
$redirect = false;
$url = '';
$exclude = array();
if(!empty($M_options['registration_page'])) {
$exclude[] = get_permalink( (int) $M_options['registration_page'] );
$exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] ));
}
if(!empty($M_options['account_page'])) {
$exclude[] = get_permalink( (int) $M_options['account_page'] );
$exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] ));
}
if(!empty($M_options['nocontent_page'])) {
$exclude[] = get_permalink( (int) $M_options['nocontent_page'] );
$exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] ));
}
if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) {
$exclude[] = $host;
$exclude[] = untrailingslashit($host);
}
foreach($posts as $post) {
if($post->post_type != 'recipes') {
continue;
}
if(!in_array(strtolower( get_permalink($post->ID) ), $exclude)) {
$url = get_permalink($post->ID);
}
}
// Check if we have a url available to check
if(empty($url)) {
return $posts;
}
// we have the current page / url - get the groups selected
$group_id = $this->get_group();
if($group_id) {
$group = new M_Urlgroup( $group_id );
if( !empty($url) && $group->url_matches( $url ) ) {
$redirect = true;
}
}
if($redirect === true && !empty($M_options['nocontent_page'])) {
// we need to redirect
$this->redirect();
} else {
return $posts;
}
}
return $posts;
}
function check_positive_recipes( $posts ) {
global $wp_query, $M_options;
if( !$wp_query->is_singular || count($posts) > 1) {
return $posts;
}
if(!empty($posts) && count($posts) == 1) {
// we may be on a restricted post so check the URL and redirect if needed
$redirect = false;
$found = false;
$url = '';
$exclude = array();
if(!empty($M_options['registration_page'])) {
$exclude[] = get_permalink( (int) $M_options['registration_page'] );
$exclude[] = untrailingslashit(get_permalink( (int) $M_options['registration_page'] ));
}
if(!empty($M_options['account_page'])) {
$exclude[] = get_permalink( (int) $M_options['account_page'] );
$exclude[] = untrailingslashit(get_permalink( (int) $M_options['account_page'] ));
}
if(!empty($M_options['nocontent_page'])) {
$exclude[] = get_permalink( (int) $M_options['nocontent_page'] );
$exclude[] = untrailingslashit(get_permalink( (int) $M_options['nocontent_page'] ));
}
if(!empty($wp_query->query_vars['protectedfile']) && !$forceviewing) {
$exclude[] = $host;
$exclude[] = untrailingslashit($host);
}
foreach($posts as $post) {
if($post->post_type != 'recipes') {
continue;
}
if(!in_array(strtolower( get_permalink($post->ID) ), $exclude)) {
$url = get_permalink($post->ID);
}
}
// Check if we have a url available to check
if(empty($url)) {
return $posts;
}
// we have the current page / url - get the groups selected
$group_id = $this->get_group();
if($group_id) {
$group = new M_Urlgroup( $group_id );
if( $group->url_matches( $url ) ) {
$found = true;
}
}
if($found !== true && !empty($M_options['nocontent_page'])) {
// we need to redirect
$this->redirect();
} else {
return $posts;
}
}
return $posts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment