{Wordpress} wp-memberships making content invisible

Discussion in 'PHP' started by HomeBlogger, Jan 14, 2011.

  1. #1
    Hello,

    I actually installed a plugin on one of my wordpress blogs which is called wp-memberships. The thing is that this plugin allows you to choose a category from WP control panel to make its posts visible only to subscribed members or premium members, whatever you call it ... It means they can see the posts that have been posted in the category but when you click on one of them it gives you a message that you ned to pay in order to view this content ...

    What I need to know is how to make a certain category itself visible only to premium members, I mean they can't even see the posts that have been posted in this category, so the private message should appear when you enter this category not when you enter a post in this category.

    Please help! and thanks in advance!

    By the way, the plugin's code is:
    <?php 
    $roleAdmin = "administrator";
    function quizzpot_memberships_install() {
    	global $wpdb;
    	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    	
    	if(get_option("membership_private_msg") == ""){
    		update_option("membership_private_msg",'This is a private content for members only, please <a href="'.get_bloginfo("url").'/wp-login.php?action=register">register</a>.');
    	}
    	
    	$table_members = $wpdb->prefix . "memberships";
    	$table_deposits = $wpdb->prefix . "memberships_deposits";
       
    	
    	$sqlMemebers = "CREATE TABLE " . $table_members . " (
    	  id mediumint(9) NOT NULL AUTO_INCREMENT,
    	  name tinytext NOT NULL,
    	  description text NOT NULL,
    	  months mediumint(9) NOT NULL,
    	  price float NOT NULL,
    	  active bool  NOT NULL,
    	  UNIQUE KEY id (id)
    	);";
    
    	$sqlDeposits = "CREATE TABLE " . $table_deposits . " (
    	  id mediumint(9) NOT NULL AUTO_INCREMENT,
    	  id_membership mediumint(9) NOT NULL,
    	  id_user mediumint(9) NOT NULL,
    	  status tinytext NOT NULL,
    	  price float NOT NULL,
    	  currency tinytext NOT NULL,
    	  order_date datetime NOT NULL,
    	  deposit_date datetime,
    	  end_date datetime,
    	  UNIQUE KEY id (id)
    	);";
    	
    	if($wpdb->get_var("show tables like '$table_members'") != $table_members) {
    		dbDelta($sqlMemebers);
    		dbDelta($sqlDeposits);
    		$insert = "INSERT INTO " . $table_members .
                " (name, description,months,price,active) " .
                "VALUES ('" . $wpdb->escape("One month membership") . "','" . $wpdb->escape("You will only have access for one month to our content.")."',1,9.99,true)";
    
    		$results = $wpdb->query( $insert );
    	}else{
    		dbDelta($sqlMemebers);
    		dbDelta($sqlDeposits);
    	}
    
    }
    function quizzpot_memberships_admin_options(){
    	global $roleAdmin;
    	add_menu_page("Memberships", "Memberships", $roleAdmin,  "quizz-memberships-dashboard" , "quizzpot_memberships_dashboard");
    	add_submenu_page("quizz-memberships-dashboard", "Dashboard", "Dashboard", $roleAdmin, "quizz-memberships-dashboard", "quizzpot_memberships_dashboard");
    	add_submenu_page("quizz-memberships-dashboard", "Membership list", "Memberships", $roleAdmin, "quizz-memberships", "quizzpot_memberships_list");
    	add_submenu_page("quizz-memberships-dashboard", "Add new membership", "Add membership", $roleAdmin, "quizz-memberships-add", "quizzpot_memberships_add");
    	add_submenu_page("quizz-memberships-dashboard", "Paypal account configuration", "Paypal account", $roleAdmin, "quizz-memberships-paypal", "quizzpot_memberships_paypal");
    	add_submenu_page("quizz-memberships-dashboard", "Subscriptions", "Subscriptions", $roleAdmin, "quizz-memberships-subscribers", "quizzpot_memberships_subscribers");
    	add_submenu_page("quizz-memberships-dashboard", "Subscriptions", "Add Subscription", $roleAdmin, "quizz-memberships-subscribers-add", "quizzpot_memberships_subscribers_add");
    	
    	add_menu_page("Members", "Members", "subscriber",  "quizz-members" , "quizzpot_members_home");
    	add_submenu_page("quizz-members", "Members home", "Home", "subscriber", "quizz-members", "quizzpot_members_home");
    	add_submenu_page("quizz-members", "Buy a membership", "Buy", "subscriber", "quizz-members-buy", "quizzpot_members_buy");
    }
    
    function quizzpot_memberships_dashboard(){
    	include_once('admin/wp-memberships-dashboard.php'); 
    }
    
    function quizzpot_memberships_list(){
    	include_once('admin/wp-memberships-list.php');  
    }
    
    function quizzpot_memberships_add(){
    	include_once("admin/wp-memberships-add.php");
    }
    
    function quizzpot_memberships_paypal(){
    	include_once("admin/wp-memberships-paypal.php");
    }
    
    function quizzpot_members_home(){
    	include_once("members/wp-memberships-home.php");
    }
    
    function quizzpot_members_buy(){
    	include_once("members/wp-memberships-buy.php");
    }
    
    function quizzpot_memberships_subscribers(){
    		include_once("admin/wp-memberships-subscribers.php");
    }
    
    function quizzpot_memberships_subscribers_add(){
    		include_once("admin/wp-memberships-subscribers-add.php");
    }
    
    function quizzpot_admin_head() {
    	$siteurl = get_option('siteurl');
    	$urlStyle = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/resources/styles/style.css';
    	$urlUI = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/resources/styles/jquery-ui-1.8.2.css';
    	
    	echo "<link rel='stylesheet' type='text/css' href='$urlStyle' />\n";
    	echo "<link rel='stylesheet' type='text/css' href='$urlUI' />\n";
    	
    }
    
    function quizzpot_memberships_admin_warnings() {
    	$email = get_option('membership_paypal_email');
    	$cur = get_option('membership_paypal_currency');
    	if ( !isset($email) || !isset($cur) || $email == "" || $cur == "") {
    		function quizzpot_memberships_warning() {
    			echo "<div id='akismet-warning' class='updated fade'><p>Your haven't configured your Paypal account yet. Please make sure <a href=\"admin.php?page=quizz-memberships-paypal\">you do it</a> so this plugin can work properly.</p></div>";
    		}
    		add_action('admin_notices', 'quizzpot_memberships_warning');
    	}
    }
    function quizzpot_plus_filter_posts($content){
    	global $current_user,$wpdb,$roleAdmin;
    	$plusCat = get_option("membership_plus_content");
    	
    	if(is_user_logged_in()){
    		get_currentuserinfo();
    		$deposit = $wpdb->get_row("select * from ".$wpdb->prefix."memberships_deposits where status='completed' and id_user=$current_user->ID and end_date >= NOW()");
    		if($deposit){
    			//The user can read the plus content :)
    			//do nothing
    		}else{
    			//the user can´t read the plus content, user needs to pay ;)
    			//remove the plus content from the query
    			if(!isset($current_user->data->wp_capabilities[$roleAdmin])){
    				quizzpot_memberships_filter($content);
    			}
    		}
    	}else{
    		quizzpot_memberships_filter($content);
    	}
    }
    
    function quizzpot_memberships_filter($content){
    	global $wpdb;
    	$plusCat = get_option("membership_plus_content");
    	$post_content = get_option("membership_private_msg");
    	$total = $content->post_count;
    	$result = array();
    	include_once("resources/php/PremiumPost.php");
    	for($i=0;$i<$total;$i++){
    		$post = $content->posts[$i];
    		$row = $wpdb->get_row("SELECT P.ID
    						FROM $wpdb->posts P
    						INNER JOIN $wpdb->term_relationships TR ON TR.object_id = P.ID
    						INNER JOIN $wpdb->term_taxonomy TT ON TT.term_taxonomy_id = TR.term_taxonomy_id
    						INNER JOIN $wpdb->terms T ON T.term_id = TT.term_id
    						AND TT.taxonomy =  'category'
    						AND T.term_id =$plusCat and P.ID=$post->ID");
    		if($row){
    			$p = new PremiumPost();
    			$p->ID = $post->ID;
    			$p->post_title = $post->post_title;
    			$p->post_content = $post_content;
    			$p->post_author = $post->post_author;
    			$p->post_modified = $post->post_modified;
    			$p->post_modified_gmt = $post->post_modified_gmt;
    			array_push($result,$p);
    		}else{
    			array_push($result,$post);
    		}
    	}
    	$content->posts = $result;
    	$content->post_count = count($result);
    	if($content->post_count>0){
    		$content->post = $result[0];
    	}
    }
    
    register_activation_hook(__FILE__,'quizzpot_memberships_install');
    add_action('admin_menu', 'quizzpot_memberships_admin_options');
    add_action('admin_head', 'quizzpot_admin_head');
    add_action('init', 'quizzpot_memberships_admin_warnings');
    add_action('loop_start','quizzpot_plus_filter_posts');
    PHP:
     
    HomeBlogger, Jan 14, 2011 IP