Location: PHPKode > scripts > WP Marketplace > wpmarketplace/modules/rating_post_user.php
<?php



/************************************/
function wpmp_posting_form_submit(){
    if($_POST['action']=="wpmp_save_post"){
            global $current_user;
            if($_POST['user_post_title']){
                $settings = get_option('_wpmp_settings');
                if(!empty($settings['fstatus']))$st=$settings['fstatus'];else $st="draft";
            $my_post = array(
                 'post_title' => $_POST['user_post_title'],
                 'post_content' => $_POST['user_post_desc'],
                 'post_status' => $st,
                 'post_author' => $_POST['wpmp_list']['author'],
                 'tax_input' => array("ptype"=>array($_POST['cat'])),
                 'post_type' => 'wpmarketplace'
              );
              if($_REQUEST['task']=="new"){
            // Insert the post into the database
                $postid=wp_insert_post( $my_post );
                //send email to the admin to notify that a product has been added
                $settings = get_option('_wpmp_settings');
                $toemail=explode(",",$settings['admin_emails']);
                if(count($toemail)>0){
                    foreach($toemail as $email){//echo $email;
                    $to= $email;
                    $from= $current_user->user_email;
                    $subject="Notification for new product";
                    $message="A New Product has been added. \r\n Product Info : \r\n Title: ".$_POST['user_post_title']." \r\n Description : ".$_POST['user_post_desc']."\r\n" ;
                    $headers = 'From:  <'.$from.'>' . "\r\n";
                    wp_mail($to, $subject, $message, $headers );
                    }
                }
              }
              if($_REQUEST['task']=="edit"){
              // update the post into the database   
              $my_post['ID']=$_REQUEST['postid'];
              wp_update_post( $my_post );
               $postid= $_REQUEST['postid'];  
            }
            $upload_dir = wp_upload_dir();
           $filenames= $_POST['filename'];

/*            if(is_array($filenames)){
          foreach($filenames as $value){ 
       
              $wp_filetype = wp_check_filetype(basename($value), null );
              $attachment = array(
                 'post_mime_type' => $wp_filetype['type'],
                 'post_title' => preg_replace('/\.[^.]+$/', '', basename($value)),
                 'post_content' => '',
                 'guid' => str_replace(array($_SERVER['DOCUMENT_ROOT'],"\\"),array("","/"),$upload_dir['path'])."/".$value,
                 'post_status' => 'inherit'
              );
              $attach_id = wp_insert_attachment( $attachment, $value, $postid );
              
              set_post_thumbnail( $postid, $attach_id );
              
          }
            }*/
            
            if($_POST['wpmp_list']){
             update_post_meta($postid,"wpmp_list_opts",$_POST['wpmp_list']);  
             foreach($_POST['wpmp_list'] as $k=>$v){
                    update_post_meta($postid,$k,$v);
                 }
             }    
            
             
           //update_post_meta($postid,'user_post_tag',$_POST['user_post_tag']);
           update_post_meta($postid,'user_post_excerpt',$_POST['user_post_excerpt']);
    }
    
        
    }
}
function  wpmp_user_post_list(){
    global $current_user;
    $user_posts=get_posts('post_type=wpmarketplace&author='.$current_user->ID.'&post_status=publish');
    include_once("tpls/user_post_list.php");
}

function wpup_delete_post(){
    if($_REQUEST['task']=="delete"){
       if($_REQUEST['postid']){
           
           //delete post attachment
           $posts=get_posts("post_type=attachment&post_parent=".$_REQUEST['postid']);
           //print_r($posts);
           if(count($posts)>0){
               foreach($posts as $pst){
                   wp_delete_post($pst->ID);
               }
           }
           //delete post
            wp_delete_post($_REQUEST['postid']);
            
        } 
    }
}

function wpup_user_post_form(){
    $concat= get_option("permalink_structure")?"?":"&"; 
     
        
      if($_REQUEST['task']=="edit"){
        $thispost=get_post($_REQUEST['postid']);
         $meta_fields = get_post_meta($thispost->ID, "custom_fields", $single);
         $meta_values = get_post_meta($thispost->ID, "custom_values", $single);
         $post_tag = get_post_meta($thispost->ID, "user_post_tag", $single);
         $user_post_excerpt = get_post_meta($thispost->ID, "user_post_excerpt", $single);
         
        }
     else $_REQUEST['task']="new";
     
    include_once("tpls/post_form.php");
}

/*function wpup_user_earning(){
    $concat= get_option("permalink_structure")?"?":"&"; 
     
        
      if($_REQUEST['task']=="earnings"){
        include_once("tpls/earnings.php");
         
        }
     
     
    
}*/

function wpup_user_menu(){
    
    $concat= get_option("permalink_structure")?"?":"&";
    include("tpls/user-menu.php");
}

function wpmp_user_posts(){    
     global $wp_roles;
     global $current_user;
/*     $userrole= get_option('_wpup_user_role'); 
     //print_r($current_user->roles);
     
     $flag=0;
     for($i=0;$i<count($userrole);$i++){
     if($userrole[$i]=="guest"){ $flag=1;break;}  
     }
     if($flag!=1){
         if(count($current_user->roles)){
             
             for($i=0;$i<count($userrole);$i++){
                
                if($userrole[$i]==$current_user->roles[0]){
                        $flag=1;break;
                    } 
             } 
         }
     }*/
     
    
        if(is_user_logged_in()){  
            
            
            
           if(!(is_single()||is_page())) return;
           
            wpup_user_menu();
            
            switch($_GET['task']){
                
                case 'new':
                case 'edit':
                    wpup_user_post_form();
                    break;
                case 'delete':
                    wpup_delete_post();
                    wpmp_user_post_list();
                    break;
                case 'earnings':
                    include_once("tpls/earnings.php");
                    break;
                case 'media':
                    if($_GET['duty']=="delete"){
                        if($_GET['mediaid']){
                            wp_delete_post($_GET['mediaid']);
                        }
                    }
                    wpup_media_list();
                    break;
                case '':
                default:
                    wpmp_user_post_list();
                    break;
            } 
        }  
        else
        echo "Only logged in user allowed to post";   
    
    }
    
function wpmp_upload_media(){
    if($_REQUEST['task']=="attach_user_media"){
        //die('ok');
        //echo $_POST['postid'];  
        if (!empty($_FILES)) {
        $tempFile = $_FILES['Filedata']['tmp_name'];
        $filename=str_replace(" ","-",$_FILES['Filedata']['name']);
        $targetPath = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
        //$targetPath = $_REQUEST['folder'] . '/';
        $targetFile =  str_replace('//','/',$targetPath) . $filename;
        
        $ext = strtolower(end(explode(".",$filename)));
        if(!in_array($ext,array('jpeg','JPEG','JPG','GIF','jpg','gif','txt','png','doc','xls','zip','rar','swf','mp3','flv'))) die('Invalid file type');
       
            if(move_uploaded_file($tempFile,$targetFile))//echo str_replace($_SERVER['DOCUMENT_ROOT'],'',$targetFile);
                echo $filename;
            else echo false;

          
        }
       die();
    }
}

//withdraw request
function wpmp_insert_withdraw(){
    global $wpdb;
    global $current_user;
    if($_POST['action']=="withdraw_request"){
        $amount=$_POST['amount'];
        $status="pending";
         $wpdb->insert( 
                'wp_mp_withdraw', 
                array( 
                    'amount' => $amount ,
                    'userid' => $current_user->ID , 
                    'date' => time(),
                    'status' => $status  
                ), 
                array( 
                    '%d', 
                    '%d',
                    '%d', 
                    '%s' 
                ) 
            );
            echo $wpdb->insert_id;
            die();
    }
    
}



function wpmp_js(){   
    ?>
    <script language="JavaScript">
    <!--
      jQuery(function(){
          
          
          var star1c = jQuery('#star1').attr('class');
          var star2c = jQuery('#star2').attr('class');
          var star3c = jQuery('#star3').attr('class');
          var star4c = jQuery('#star4').attr('class');
          var star5c = jQuery('#star5').attr('class');
       
          jQuery('.star').click(function(){
              jQuery.post('<?php echo home_url();?>/?rate='+jQuery(this).attr('rate_value')+'&post_id='+jQuery(this).attr('post'),function(res){
                  //...
                   
                  var average = res.average;
                  var total = res.total;
                
                   jQuery('#average_vote').text(average);
                   jQuery('#total_vote').text(total);
                   //alert(parseInt(str1[1]));
                  
                   
                   /*for(var i=1;i<=parseInt(average);i++){
                       
                       jQuery('#star'+i).addClass('star_on');
                        
                   } 
                    for(var i=(parseInt(average)+1);i<=5;i++){
                       jQuery('#star'+i).removeClass('star_on');
                       
                   }     */
                     
                    star1c = "star star1 "+((average>=0)?'star_on':''); //jQuery('#star1').attr('class');
                    star2c = "star star2 "+((average>=1.4)?'star_on':''); //jQuery('#star1').attr('class');
                    star3c = "star star3 "+((average>=2.4)?'star_on':''); //jQuery('#star1').attr('class');
                    star4c = "star star4 "+((average>=3.4)?'star_on':''); //jQuery('#star1').attr('class');
                    star5c = "star star5 "+((average>=4.4)?'star_on':''); //jQuery('#star1').attr('class');
                    /*star2c = jQuery('#star2').attr('class');
                    star3c = jQuery('#star3').attr('class');
                    star4c = jQuery('#star4').attr('class');
                    star5c = jQuery('#star5').attr('class');
                                                                  */
                  //alert(res);
              })
           return false;
          });
          var prev_stat = new Array();
          jQuery('.star').mouseover(function(){
              
              for(var i=0;i<parseInt(jQuery(this).attr('rate_value'));i++){
                  
                  if(jQuery('.star'+i).hasClass('star_on')) { //prev_stat[i] = 1; 
                  }
                  else {//prev_stat[i] = 0;    
                  jQuery('.star'+i).addClass('star_on');   
                  }
                  
              }
              
              for(var i=parseInt(jQuery(this).attr('rate_value'));i<=5;i++){
                  
                  if(jQuery('.star'+i).hasClass('star_on')) { 
                     // prev_stat[i] = 1;  
                     jQuery('.star'+i).removeClass('star_on');   }
                  else {//prev_stat[i] = 0;  
                  }
                  
              }  
              
          });
          
          jQuery('.star').mouseout(function(){
              
              for(var i=0;i<parseInt(jQuery(this).attr('rate_value'));i++){
                  var prev_stat = new Array();
                  if(jQuery('.star'+i).hasClass('star_on')) { prev_stat[i] = 1; }
                  else {prev_stat[i] = 0;    jQuery('.star'+i).addClass('star_on');    }
                  
              }
              
          });  
          
         
         jQuery('#rating_area').mouseout(function(){
            
            jQuery('#star1').removeAttr('class').attr('class',star1c);
            jQuery('#star2').removeAttr('class').attr('class',star2c);
            jQuery('#star3').removeAttr('class').attr('class',star3c);
            jQuery('#star4').removeAttr('class').attr('class',star4c);
            jQuery('#star5').removeAttr('class').attr('class',star5c);
          
      }); 
          
      
      });
      
      
    //-->
    </script>
    <?php
}

function wpmp_addrating(){
      global $current_user; 
      //echo $_REQUEST['rate']; 
      if($_REQUEST['rate']){
        $msg=""; 
         //delete_post_meta($_REQUEST['post_id'],"post_rating");
         $rate=get_post_meta($_REQUEST['post_id'],"post_rating",true);
         $sum=0;
         $count=0;
         $rate[$current_user->ID]= $_REQUEST['rate'];
         $url[$current_user->ID]= $_SERVER['REQUEST_URI'];
         
         update_post_meta($_REQUEST['post_id'],"post_rating",$rate);
         foreach($rate as $key=>$value){
             $count++;
             $sum+=$value;
             /*if($key==$current_user->ID){
                 $flag=1;break; 
             } */
         }
         /*if($flag==0){
              $rate[$current_user->ID]= $_REQUEST['rate'];
              update_post_meta($_REQUEST['post_id'],"post_rating",$rate);
              $msg= "Rating Saved";
         }else
            $msg= "You already rated this.";  */
         
         $avg_rate=number_format($sum/$count,1);
          $data['average'] = $avg_rate;
          $data['total'] = $count;
          //save the average rating for the products to show the top rate products easily
          update_post_meta($_REQUEST['post_id'],"avg_rating",$avg_rate);
          
            header("Content-type: application/json");
            die(json_encode($data)); 
    }
    
       

}

add_action("init","wpmp_upload_media"); 
add_action("init","wpmp_posting_form_submit"); 
add_action("init","wpmp_insert_withdraw"); 
add_shortcode("wpmp_frontend",'wpmp_user_posts');
//add rating to the product content
add_action('wp_head','wpmp_js');
//for saving the rating click
add_action("init",'wpmp_addrating');

/************************************/
?>
Return current item: WP Marketplace