Viruthagiri

paid content

Dec 9th, 2011
267
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /** Paid Content Module */
  4.  
  5. cp_module_register(__('Paid Content', 'cp') , 'pcontent' , '1.1', 'CubePoints', 'http://cubepoints.com', 'http://cubepoints.com' , __('This module lets you deduct point from users to view a page or post.', 'cp'), 1);
  6.  
  7. function cp_module_pcontent_install(){
  8.     add_option('cp_module_pcontent_default_points', 10);
  9.     add_option('cp_module_pcontent_default', false);
  10.     add_option('cp_module_pcontent_payauthor', false);
  11.     add_option('cp_module_pcontent_text_pay', __('You need to pay %points% to access this page.', 'cp'));
  12.     add_option('cp_module_pcontent_text_button', __('Pay %points%', 'cp'));
  13.     add_option('cp_module_pcontent_text_logout', __('You must be logged in to access this page.', 'cp'));
  14.     add_option('cp_module_pcontent_text_insufficient', __('You have insufficient points to purchase access for this page.', 'cp'));
  15. }
  16. add_action('cp_module_pcontent_activate','cp_module_pcontent_install');
  17.  
  18. if(cp_module_activated('pcontent')){
  19.  
  20.     /* Config for this module */
  21.     function cp_module_pcontent_config(){
  22.     ?>
  23.         <br />
  24.         <h3><?php _e('Paid Content','cp'); ?></h3>
  25.         <table class="form-table">
  26.  
  27.             <tr valign="top">
  28.                 <th scope="row"><label for="cp_module_pcontent_default_points"><?php _e('Default number of points', 'cp'); ?>:</label></th>
  29.                 <td valign="middle"><input type="text" id="cp_module_pcontent_default_points" name="cp_module_pcontent_default_points" value="<?php echo get_option('cp_module_pcontent_default_points'); ?>" size="30" /></td>
  30.             </tr>
  31.  
  32.             <tr valign="top">
  33.                 <th scope="row"><label for="cp_module_pcontent_default"><?php _e('Enabled by default', 'cp'); ?>:</label></th>
  34.                 <td valign="middle">
  35.                     <input type="radio" id="cp_module_pcontent_default_y" name="cp_module_pcontent_default" value="1" <?php echo (get_option('cp_module_pcontent_default')==true)?'checked="checked"':''; ?> /> <label for="cp_module_pcontent_default_y"><?php _e('Yes', 'cp'); ?></label>
  36.                     <input style="margin-left:15px;" type="radio" id="cp_module_pcontent_default_n" name="cp_module_pcontent_default" value="0" <?php echo (get_option('cp_module_pcontent_default')==false)?'checked="checked"':''; ?> /> <label for="cp_module_pcontent_default_n"><?php _e('No', 'cp'); ?></label>
  37.                 </td>
  38.             </tr>
  39.  
  40.             <tr valign="top">
  41.                 <th scope="row"><label for="cp_module_pcontent_payauthor"><?php _e('Credit post author with points paid by user', 'cp'); ?>:</label></th>
  42.                 <td valign="middle">
  43.                     <input type="radio" id="cp_module_pcontent_payauthor_y" name="cp_module_pcontent_payauthor" value="1" <?php echo (get_option('cp_module_pcontent_payauthor')==true)?'checked="checked"':''; ?> /> <label for="cp_module_pcontent_payauthor_y"><?php _e('Yes', 'cp'); ?></label>
  44.                     <input style="margin-left:15px;" type="radio" id="cp_module_pcontent_payauthor_n" name="cp_module_pcontent_payauthor" value="0" <?php echo (get_option('cp_module_pcontent_payauthor')==false)?'checked="checked"':''; ?> /> <label for="cp_module_pcontent_payauthor_n"><?php _e('No', 'cp'); ?></label>
  45.                 </td>
  46.             </tr>
  47.  
  48.             <tr valign="top">
  49.                 <th scope="row"><label for="cp_module_pcontent_text_pay"><?php _e('Text to ask users to pay for page access with points', 'cp'); ?>:</label></th>
  50.                 <td valign="middle"><input type="text" id="cp_module_pcontent_text_pay" name="cp_module_pcontent_text_pay" value="<?php echo get_option('cp_module_pcontent_text_pay'); ?>" size="30" /></td>
  51.             </tr>
  52.            
  53.             <tr valign="top">
  54.                 <th scope="row"><label for="cp_module_pcontent_text_button"><?php _e('Text to be shown on payment button', 'cp'); ?>:</label></th>
  55.                 <td valign="middle"><input type="text" id="cp_module_pcontent_text_button" name="cp_module_pcontent_text_button" value="<?php echo get_option('cp_module_pcontent_text_button'); ?>" size="30" /></td>
  56.             </tr>
  57.  
  58.             <tr valign="top">
  59.                 <th scope="row"><label for="cp_module_pcontent_text_logout"><?php _e('Text to be shown to users who are not logged in', 'cp'); ?>:</label></th>
  60.                 <td valign="middle"><input type="text" id="cp_module_pcontent_text_logout" name="cp_module_pcontent_text_logout" value="<?php echo get_option('cp_module_pcontent_text_logout'); ?>" size="30" /></td>
  61.             </tr>
  62.  
  63.             <tr valign="top">
  64.                 <th scope="row"><label for="cp_module_pcontent_text_insufficient"><?php _e('Text to be shown when user have insufficient points', 'cp'); ?>:</label></th>
  65.                 <td valign="middle"><input type="text" id="cp_module_pcontent_text_insufficient" name="cp_module_pcontent_text_insufficient" value="<?php echo get_option('cp_module_pcontent_text_insufficient'); ?>" size="30" /></td>
  66.             </tr>
  67.  
  68.         </table>
  69.     <?php
  70.     }
  71.     add_action('cp_config_form','cp_module_pcontent_config');
  72.  
  73.     /* Process and save options */
  74.     function cp_module_pcontent_config_process(){
  75.         $cp_module_pcontent_default_points = (int) $_POST['cp_module_pcontent_default_points'];
  76.         $cp_module_pcontent_default = (bool) $_POST['cp_module_pcontent_default'];
  77.         $cp_module_pcontent_payauthor = (bool) $_POST['cp_module_pcontent_payauthor'];
  78.         $cp_module_pcontent_text_pay = $_POST['cp_module_pcontent_text_pay'];
  79.         $cp_module_pcontent_text_button = $_POST['cp_module_pcontent_text_button'];
  80.         $cp_module_pcontent_text_logout = $_POST['cp_module_pcontent_text_logout'];
  81.         $cp_module_pcontent_text_insufficient = $_POST['cp_module_pcontent_text_insufficient'];
  82.         update_option('cp_module_pcontent_default_points', (($cp_module_pcontent_default_points<=0)?1:$cp_module_pcontent_default_points));
  83.         update_option('cp_module_pcontent_default', $cp_module_pcontent_default);
  84.         update_option('cp_module_pcontent_payauthor', $cp_module_pcontent_payauthor);
  85.         update_option('cp_module_pcontent_text_pay', $cp_module_pcontent_text_pay);
  86.         update_option('cp_module_pcontent_text_button', $cp_module_pcontent_text_button);
  87.         update_option('cp_module_pcontent_text_logout', $cp_module_pcontent_text_logout);
  88.         update_option('cp_module_pcontent_text_insufficient', $cp_module_pcontent_text_insufficient);
  89.     }
  90.     add_action('cp_config_process','cp_module_pcontent_config_process');
  91.  
  92.     /* Define the custom box */
  93.     add_action('admin_init', 'cp_module_pcontent_add_custom_box', 1);
  94.  
  95.     /* Do something with the data entered */
  96.     add_action('save_post', 'cp_module_pcontent_save_postdata');
  97.  
  98.     /* Adds a box to the main column on the Post and Page edit screens */
  99.     function cp_module_pcontent_add_custom_box() {
  100.         add_meta_box( 'cp_module_pcontent_set', 'CubePoints - Paid Content', 'cp_module_pcontent_box', 'post', 'normal', 'high' );
  101.         add_meta_box( 'cp_module_pcontent_set', 'CubePoints - Paid Content', 'cp_module_pcontent_box', 'page', 'normal', 'high' );
  102.     }
  103.  
  104.     /* Prints the box content */
  105.     function cp_module_pcontent_box() {
  106.  
  107.         global $post;
  108.  
  109.         // Use nonce for verification
  110.         wp_nonce_field( plugin_basename(__FILE__), 'cp_module_pcontent_nonce' );
  111.         if($post->post_status == 'auto-draft'){
  112.             $enabled = (get_option('cp_module_pcontent_default')?'checked="yes"':'');
  113.             $points = get_option('cp_module_pcontent_default_points');
  114.         }
  115.         else{
  116.             $enabled = ((bool)(get_post_meta($post->ID , 'cp_pcontent_points_enable', 1))?'checked="yes"':'');
  117.             $points = (int)get_post_meta($post->ID , 'cp_pcontent_points', 1);
  118.         }
  119.         // The actual fields for data entry
  120.         echo '<br /><input type="checkbox" id="cp_module_pcontent_enable" name="cp_module_pcontent_enable" value="1" size="25" '.$enabled.' /> ';
  121.         echo '<label for="cp_module_pcontent_enable">' . __("Enable paid content" , 'cp') . '</label> ';
  122.         echo '<br /><br />';
  123.         echo '<label for="cp_module_pcontent_points">' . __("Number of points to be deducted to view this page / post" , 'cp') . ':</label> ';
  124.         echo '<input type="text" id= "cp_module_pcontent_points" name="cp_module_pcontent_points" value="'.$points.'" size="25" /><br /><br />';
  125.     }
  126.  
  127.     /* When the post is saved, saves our custom data */
  128.     function cp_module_pcontent_save_postdata( $post_id ) {
  129.  
  130.         // get post id from the revision id
  131.         if($parent_id = wp_is_post_revision($post_id)){
  132.             $post_id = $parent_id;
  133.         }
  134.  
  135.         // verify this came from the our screen and with proper authorization,
  136.         // because save_post can be triggered at other times
  137.  
  138.         if ( !wp_verify_nonce( $_POST['cp_module_pcontent_nonce'], plugin_basename(__FILE__) )) {
  139.             return $post_id;
  140.         }
  141.  
  142.         // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
  143.         if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  144.             return $post_id;
  145.  
  146.      
  147.         // Check permissions
  148.         if ( 'page' == $_POST['post_type'] ) {
  149.             if ( !current_user_can( 'edit_page', $post_id ) )
  150.                 return $post_id;
  151.             } else {
  152.                 if ( !current_user_can( 'edit_post', $post_id ) )
  153.                 return $post_id;
  154.         }
  155.  
  156.         // OK, we're authenticated: we need to find and save the data
  157.         $points = (int)$_POST['cp_module_pcontent_points'];
  158.         if($points<1){
  159.             $points = 1;
  160.         }
  161.         update_post_meta($post_id, 'cp_pcontent_points_enable', (int)$_POST['cp_module_pcontent_enable']);
  162.         update_post_meta($post_id, 'cp_pcontent_points', $points);
  163.  
  164.     }
  165.  
  166.     add_action('the_post','cp_module_pcontent_post');
  167.     add_filter( "the_content", "cp_module_pcontent_post_content" );
  168.    
  169.     function cp_module_pcontent_post($p){
  170.         $pcontent_enabled = (bool) get_post_meta($p->ID,'cp_pcontent_points_enable', 1);
  171.         if(!$pcontent_enabled){
  172.             return;
  173.         }
  174.         if(current_user_can( 'read_private_pages' )){
  175.             return;
  176.         }
  177.         $uid = cp_currentUser();
  178.         $pid = $p->ID;
  179.         global $wpdb;
  180.         if( (int) $wpdb->get_var("SELECT COUNT(*) FROM ".CP_DB." WHERE `uid`=$uid AND `data`=$pid AND `type`='pcontent'") != 0 ){
  181.             return;
  182.         }
  183.         global $cp_module_pcontent_hide;
  184.         $cp_module_pcontent_hide[] = $p->ID;
  185.     }
  186.    
  187.     function cp_module_pcontent_post_content($content){
  188.         global $post;
  189.         global $cp_module_pcontent_hide;
  190.         if(!in_array($post->ID,(array)$cp_module_pcontent_hide)){
  191.             return $content;
  192.         }
  193.         $c = '<p>' . get_option('cp_module_pcontent_text_pay') . '</p>';
  194.         $c .= apply_filters('cp_module_pcontent_post_content_'.$post->ID, '');
  195.         $c .= '<form method="post">';
  196.         $c .= '<input type="hidden" name="cp_module_pcontent_pay" value="'.$post->ID.'" />';
  197.         $c .= '<p><input type="submit" value="'.get_option('cp_module_pcontent_text_button').'" /></p>';
  198.         $c .= '</form>';
  199.         if(!is_user_logged_in()){
  200.             $c = get_option('cp_module_pcontent_text_logout');
  201.         }
  202.         $c = str_replace('%points%',cp_formatPoints(get_post_meta($post->ID,'cp_pcontent_points', 1)),$c);
  203.         return $c;
  204.     }
  205.    
  206.     add_action('init', 'cp_module_pcontent_buy');
  207.     function cp_module_pcontent_buy(){
  208.         if($_POST['cp_module_pcontent_pay']=='') return;
  209.         $pcontent_enabled = (bool) get_post_meta($_POST['cp_module_pcontent_pay'],'cp_pcontent_points_enable', 1);
  210.         if(!$pcontent_enabled) return;
  211.         $uid = cp_currentUser();
  212.         global $wpdb;
  213.         $pid = $_POST['cp_module_pcontent_pay'];
  214.         if( (int) $wpdb->get_var("SELECT COUNT(*) FROM ".CP_DB." WHERE `uid`=$uid AND `data`=$pid AND `type`='pcontent'") != 0 ){
  215.             return;
  216.         }
  217.         if(!is_user_logged_in()){
  218.             add_filter('cp_module_pcontent_post_content_'.$_POST['cp_module_pcontent_pay'], create_function('$data', 'return "<p style=\"color:red;\">'.get_option('cp_module_pcontent_text_logout').'</p>";'));
  219.             return;
  220.         }
  221.         if(cp_getPoints(cp_currentUser())<get_post_meta($_POST['cp_module_pcontent_pay'],'cp_pcontent_points', 1)){
  222.             add_filter('cp_module_pcontent_post_content_'.$_POST['cp_module_pcontent_pay'], create_function('$data', 'return "<p style=\"color:red;\">'.get_option('cp_module_pcontent_text_insufficient').'</p>";'));
  223.             return;
  224.         }
  225.         cp_points('pcontent',cp_currentUser(),-get_post_meta($_POST['cp_module_pcontent_pay'],'cp_pcontent_points', 1),$_POST['cp_module_pcontent_pay']);
  226.         if(get_option('cp_module_pcontent_payauthor')){
  227.             $post = get_post($_POST['cp_module_pcontent_pay']);
  228.             cp_points('pcontent_author',$post->post_author,get_post_meta($_POST['cp_module_pcontent_pay'],'cp_pcontent_points', 1),serialize(array($_POST['cp_module_pcontent_pay'],cp_currentUser())));
  229.         }
  230.     }
  231.    
  232.     /** Paid Content Log Hook */
  233.     add_action('cp_logs_description','cp_admin_logs_desc_pcontent', 10, 4);
  234.     function cp_admin_logs_desc_pcontent($type,$uid,$points,$data){
  235.         if($type!='pcontent') { return; }
  236.         $post = get_post($data);
  237.         echo __('Purchased access to', 'cp') . ' "<a href="'.get_permalink( $post ).'">' . $post->post_title . '</a>"';
  238.     }
  239.    
  240.     /** Paid Content Post-author Log Hook */
  241.     add_action('cp_logs_description','cp_admin_logs_desc_pcontent_author', 10, 4);
  242.     function cp_admin_logs_desc_pcontent_author($type,$uid,$points,$data){
  243.         if($type!='pcontent_author') { return; }
  244.         $data = unserialize($data);
  245.         $post = get_post($data[0]);
  246.         $user = get_user_by('id', $data[1]);
  247.         echo  __('User', 'cp') . ' "' . $user->user_login . '" ' . __('purchased access to', 'cp') . ' "<a href="'.get_permalink( $post ).'">' . $post->post_title . '</a>"';
  248.     }
  249.  
  250. }
  251.    
  252. ?>
  253.  
RAW Paste Data