Advertisement
Guest User

Items4Points Module

a guest
Apr 16th, 2012
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.40 KB | None | 0 0
  1. <?php
  2.  
  3. /** Items for Points Module */
  4.  
  5. cp_module_register(__('Items for Points', 'cp') , 'items4points' , '1.0', 'sl21', '', '' , __('This module allows your members to buy virtual Items with points', 'cp'), 1);
  6.  
  7. if(cp_module_activated('items4points')){
  8.  
  9. /* Based on the Answer Question for Points Module by Tosh Hatch, http://www.SlySpyder.com */
  10.  
  11. /* ****************** Shortcode for Items for Points ************ */
  12.  
  13. function items4points($atts) {
  14.  
  15.     extract(shortcode_atts(array(
  16.         'item' => '',
  17.         'points' => '',
  18.         'limit' => '',
  19.         'limituser' => '',
  20.  
  21.     ), $atts));
  22.    
  23.     if (isset($_POST[$atts[item]])) {
  24.        
  25.         get_currentuserinfo();
  26.         if ( !is_user_logged_in() ) {
  27.             $output .= 'please login';
  28.             return $output;
  29.         }
  30.                
  31.         global $wpdb, $post;
  32.         $multiple = $_POST['multiple'];
  33.         $getitemno = strtolower($_POST[$atts[item]]);
  34.         $item = strtolower($item);
  35.         $neg = '-';
  36.         $negpoints = str_replace( $neg , '' , ($points * $multiple));
  37.         $current_user = wp_get_current_user();
  38.         $allusers_times_bought = $wpdb->get_var("SELECT COUNT(*) FROM wp_cp WHERE type = 'shop_item' AND data = '".$item."'");
  39.         $user_times_bought = $wpdb->get_var("SELECT COUNT(*) FROM wp_cp WHERE type = 'shop_item' AND uid = '".$current_user->ID."' AND data = '".$item."'");
  40.         $pointsavailable = $wpdb->get_var("SELECT meta_value FROM wp_usermeta WHERE meta_key = 'cpoints' AND user_id = '".$current_user->ID."'");
  41.         $wpdb->query("INSERT IGNORE INTO wp_cp_shop (user_id) VALUES (".$current_user->ID.")");
  42.        
  43.         if (($user_times_bought + $multiple) > ($atts[limituser])) { // In the short code userlimit="10" means user can buy this item 10 times
  44.            
  45.             $output .= '<p><strong>you reached your personal limit for this item!</strong></p><br />';
  46.            
  47.         } else {
  48.             if ($pointsavailable + ($points * $multiple) < 0) // Check if user has enough coins to buy item
  49.             {$output .= '<p><strong>not enough coins!</strong></p><br />';}
  50.         else{
  51.             if ($getitemno == $item) {
  52.                
  53.                
  54.                     if (($allusers_times_bought + $multiple) <= $limit and ($points * $multiple) < 0)  { //  limit="10" means only first 10 user can buy item
  55.                                                                             // +check if coins reduced or awarded  
  56.                         $output .= '<p><strong>you paid '.$negpoints.' coins for item: '.$item.' x'.$multiple.'</strong></p><br />';
  57.                           for($i=1;$i<=$multiple;$i++){ //multiple single entrys in db for correct limit check
  58.                         cp_points('shop_item', $current_user->ID, $points, $getitemno);
  59.                           }
  60.                         $wpdb->query("UPDATE wp_cp_shop SET $item = $item + $multiple WHERE user_id = '".$current_user->ID."'");
  61.                         }
  62.                        
  63.                     elseif (($allusers_times_bought + $multiple) <= $limit and ($points * $multiple) > 0)  { //  limit="10" means only first 10 user can buy item
  64.                                                                                 // +check if coins reduced or awarded
  65.                         $output .= '<p><strong>you got '.($points * $multiple).' coins and item: '.$item.' x'.$multiple.'</strong></p><br />';
  66.                           for($i=1;$i<=$multiple ;$i++){ //multiple single entrys in db for correct limit check
  67.                         cp_points('shop_item', $current_user->ID, $points, $getitemno);
  68.                              }
  69.                         $wpdb->query("UPDATE wp_cp_shop SET $item = $item + 1 WHERE user_id = '".$current_user->ID."'");
  70.    
  71.                    
  72.                     } else {
  73.                         $output .= '<p><strong>item is no longer available!</strong></p>'; 
  74.                     }
  75.  
  76.             } else {
  77.                 $url = htmlspecialchars($_SERVER['HTTP_REFERER']);
  78.                 $output .= '';
  79.             }
  80.         }
  81.         }
  82.         return $output;
  83.         }
  84.        
  85.         $output .= '<form name="question" method="post" action="">
  86.         <img src="https://yourdomain.com/wp-content/uploads/images/'.$atts[item].'.png">
  87.                   <p><label><select name="multiple" onchange="showControls(this.value)">
  88.                             <option value="1">1x</option>
  89.                             <option value="2">2x</option>
  90.                             <option value="3">3x</option>
  91.                             <option value="4">4x</option>
  92.                             <option value="5">5x</option>
  93.                             </select>
  94.                             <input type="submit" name="button" id="button" value="Buy Now"></label></p>
  95.                   <p><label><input type="hidden" value="'.$atts[item].'" name="'.$atts[item].'" id="'.$atts[item].'" /></label></p>
  96.  
  97.                 </form>';
  98.                
  99.     return $output;
  100.    
  101. }
  102. add_shortcode('items4points', 'items4points');
  103. add_action('cp_logs_description','logit', 10, 4); // Log it
  104. function logit($type,$uid,$points,$data){
  105.     if($type != 'shop_item') { return; }
  106.     echo 'Shop Item: '.$data;
  107. }
  108. /* ****************** Shortcode for Items for Points ************ */
  109.  
  110.    
  111.  
  112. }
  113. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement