<?php
if(isset($_REQUEST['imtst_button'])){
    $client_name = imtst_returnCheckString(@$_REQUEST['imtst_client_name']);
    $client_job = imtst_returnCheckString(@$_REQUEST['imtst_client_job']);
    $quote = imtst_returnCheckString(@$_REQUEST['imtst_quote']);
    $client_url = imtst_returnCheckString(@$_REQUEST['imtst_client_url']);
    $company = imtst_returnCheckString(@$_REQUEST['imtst_company']);
    $company_url = imtst_returnCheckString(@$_REQUEST['imtst_companyurl']);
    $rating = imtst_returnCheckString(@$_REQUEST['imtst_stars']);

    $err_set = FALSE;
    if(isset($_REQUEST['imtst_req']) && $_REQUEST['imtst_req']!=''){
        if(strpos($_REQUEST['imtst_req'], 'client_image')){
            if(!isset($_FILES) || !isset($_FILES['imtst_client_image']) || $_FILES['imtst_client_image']['size']===0) $err_set = TRUE;
        }
        if( $_REQUEST['imtst_pot_name'] !== '' ) $err_set = TRUE; // pixedelic
        if( imtst_ckreqpostval( $_REQUEST['imtst_req'], 'client_name', $client_name ) ) $err_set = TRUE;
        if( imtst_ckreqpostval( $_REQUEST['imtst_req'], 'client_job', $client_job ) ) $err_set = TRUE;
        if( imtst_ckreqpostval( $_REQUEST['imtst_req'], 'quote', $quote ) ) $err_set = TRUE;
        if( imtst_ckreqpostval( $_REQUEST['imtst_req'], 'client_url', $client_url ) ) $err_set = TRUE;
        if( imtst_ckreqpostval( $_REQUEST['imtst_req'], 'company', $company ) ) $err_set = TRUE;
        if( imtst_ckreqpostval( $_REQUEST['imtst_req'], 'company_url', $company_url ) ) $err_set = TRUE;
        if( imtst_ckreqpostval( $_REQUEST['imtst_req'], 'rating', $rating ) ) $err_set = TRUE;
    }

    if(!$err_set){
        $post_information = array(
            'post_title' => $client_name,
            'post_content' => $quote,
            'post_type' => 'testimonials',
            'post_status' => 'pending'
        );
        $i_post_id = wp_insert_post( $post_information );
        if(isset($i_post_id) && $i_post_id!=false){
            add_post_meta($i_post_id, 'imtst_jobtitle', $client_job, TRUE);
            add_post_meta($i_post_id, 'imtst_clienturl', $client_url, TRUE);
            add_post_meta($i_post_id, 'imtst_company', $company, TRUE);
            add_post_meta($i_post_id, 'imtst_companyurl', $company_url, TRUE);
            add_post_meta($i_post_id, 'imtst_stars', $rating, TRUE);

            //feature image
            if(isset($_FILES) && count($_FILES)>0){
                foreach ($_FILES as $file => $array){
                    if ($_FILES[$file]['error'] === UPLOAD_ERR_OK){
                        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
                        require_once(ABSPATH . "wp-admin" . '/includes/media.php');
                        $attach_id = media_handle_upload( $file, $i_post_id );
                        if ($attach_id) update_post_meta($i_post_id,'_thumbnail_id',$attach_id);
                    }
        	    }
            }

            //email notice
    	    $to = get_option( 'admin_email' );
    	    $subject = 'Notice! New testimonial on ' . get_bloginfo('url');
            $message = '';
            if($client_name!='') $message .= 'From: ' . $client_name . "\n";
            if($client_job!='') $message .= 'Client Job: ' . $client_job . "\n";
            if($client_url!='') $message .= 'Client URL: ' . $client_url . "\n";
            if($company!='') $message .= 'Company: ' . $company . "\n";
            if($company_url!='') $message .= 'Company URL: ' . $company_url . "\n";
            if($quote!='') $message .= 'Quote: ' . $quote . "\n";
            if($rating!='') $message .= 'Rating: ' . $rating . "/5 stars. \n";

    	    $headers = 'From: Indeed Testimonials <'.$to.'>' . "\r\n";
    	    if( $to ) wp_mail( $to, $subject, $message, $headers);

            $save = TRUE;
        }
    }
}
if(!isset($attr)){
///////////////PREVIEW
    $current_path = $_SERVER['SCRIPT_FILENAME'];
    $dir_arr = explode( 'wp-content/', $current_path );
    $dir = $dir_arr[0];
    require_once( $dir . 'wp-load.php' );
    $attr = $_REQUEST;
    $disable_submit = true;
}
$str = ""; // pixedelic start

    $str .= "<fieldset class='imtst_fieldset_pot'><label class='imtst_label'>Pot</label><input type='text' name='imtst_pot_name' value='' class='imtst_input' placeholder='Pot'></fieldset>";

    if(isset($attr['client_name'])){
        $req_sign = imtst_check_req(@$attr['req'], 'client_name');
        $str .= "<fieldset class='imtst_fieldset'><label class='imtst_label'><input type='text' name='imtst_client_name' value='' class='imtst_input' placeholder='" . $attr['client_name'] . $req_sign . "'></label></fieldset>";
    }
    if(isset($attr['client_image'])){
        $req_sign = imtst_check_req(@$attr['req'], 'client_image');
        $str .= "<fieldset class='imtst_fieldset'><label class='imtst_label'><input type='file' name='imtst_client_image' value='' id='imtst_file_input' class='imtst_input' placeholder='" . $attr['client_image'] . $req_sign . "'>" . "</label></fieldset>";
    }
    if(isset($attr['client_job'])){
        $req_sign = imtst_check_req(@$attr['req'], 'client_job');
        $str .= "<fieldset class='imtst_fieldset'><label class='imtst_label'><input type='text' name='imtst_client_job' value='' class='imtst_input' placeholder='" . $attr['client_job'] . $req_sign . "'>" . "</label></fieldset>";
    }
    if(isset($attr['client_url'])){
        $req_sign = imtst_check_req(@$attr['req'], 'client_url');
        $str .= "<fieldset class='imtst_fieldset'><label class='imtst_label'><input type='text' name='imtst_client_url' value='' class='imtst_input' placeholder='" . $attr['client_url'] . $req_sign . "'>" . "</label></fieldset>";
    }
    if(isset($attr['company'])){
        $req_sign = imtst_check_req(@$attr['req'], 'company');
        $str .= "<fieldset class='imtst_fieldset'><label class='imtst_label'><input type='text' name='imtst_company' value='' class='imtst_input' placeholder='" . $attr['company'] . $req_sign . "'>" . "</label></fieldset>";
    }
    if(isset($attr['company_url'])){
        $req_sign = imtst_check_req(@$attr['req'], 'company_url');
        $str .= "<fieldset class='imtst_fieldset'><label class='imtst_label'><input type='text' name='imtst_companyurl' value='' class='imtst_input' placeholder='" . $attr['company_url'] . $req_sign . "'>" . "</label></fieldset>";
    }
    if(isset($attr['quote'])){
        $req_sign = imtst_check_req(@$attr['req'], 'quote');
        $str .= "<fieldset class='imtst_fieldset'><label class='imtst_label'><textarea name='imtst_quote' class='imtst_input' placeholder='" . $attr['quote'] . $req_sign . "'></textarea>" . "</label></fieldset>";
    }
    if(isset($attr['rating'])){
        $req_sign = imtst_check_req(@$attr['req'], 'rating');
        $str .= "<fieldset class='imtst_fieldset'><label class='imtst_label'>" . $attr['rating'] . $req_sign . "</label>". //pixedelic end
                    "<div class='fe_wrapp_stars'>";
                for($i=1;$i<6;$i++){
                   $str .= "<div class='unselected_star' id='client_star_$i' onClick='update_stars($i, \"#client_star_\", \"#clients_stars\", \"unselected_star\", \"selected_star\");' onMouseOver='starHoverSelect($i, \"#client_star_\", \"unselected_star\", \"selected_star\");' onMouseOut='updateStars($i, \"#client_star_\", \"#clients_stars\", \"unselected_star\", \"selected_star\");'></div>";
                }
                $str .= "<div class='clear'></div>
                        </div>
                        <input type='hidden' value='' name='imtst_stars' id='clients_stars' />
                        </fieldset>";
    }
    if($str!=""){
        if(isset($attr['req']) && $attr['req']!='') $str .= "<input type='hidden' value='".$attr['req']."' name='imtst_req' />";
        if(isset($disable_submit)) $disabled = 'disabled="disabled"';
        else $disabled = "";
        $str .= "<div id='imtst_submit_wrap'>" .
                    "<input type='submit' value='Save' name='imtst_button' $disabled />" .
                "</div>";
        $str = "<form id='imtst_form' class='imtst_form' method='post' action='' enctype='multipart/form-data' >" . $str . "</form>";
        if( isset($save) && $save==TRUE ){
            $msg = "Thank you for your submitted testimonial!";
            if(isset($attr['s_msg']) && $attr['s_msg']!='') $msg = $attr['s_msg'];
            $str .= "<div class='imtst_return_msg' >$msg</div>";
        }
        elseif ( isset($err_set) && $err_set==TRUE ){
            $msg = "An Error has Occurred. Please Try Again and complete all the required fields!";
            if(isset($attr['err_msg'])) $msg = $attr['err_msg'];
            $str .= "<div class='imtst_return_msg imtst_msg-err' >$msg</div>";
        }
    }

if(!isset($return_str) || $return_str!=true ) echo $str;// Preview




