<?php

function save_application_form($cf7) {
    global $wpdb;
    
    if(isset($cf7->posted_data["tu-mensaje"])) {
        
        $uploads = wp_upload_dir();
        $old_filename = ABSPATH.'webcam/images/'.$cf7->posted_data["id"].'.jpg';
        $new_filename = $uploads['path'].'/'.$cf7->posted_data["id"].'.jpg';
        
        $result = rename($old_filename, $new_filename);
        
        if ($result) {
        //$table_name = $wpdb->prefix."posts";
        /*$results = $wpdb->insert(
                        $table_name, 
                        array('group_id' => 1,
                              'testimonial' => $cf7->posted_data["tu-mensaje"],
                              'client_name' => $cf7->posted_data["tu-nombre"],
                              'client_pic' => 'http://localhost/lqdvi/webcam/images/'.$cf7->posted_data["id"],
                              'client_website' => $cf7->posted_data["tu-email"],
                              'client_company' => '',
                              'show_in_widget' => 0,
                              'order' => 0));*/
        /*$result = $wpdb->insert(
                $table_name,
                array(  'post_type' => 'slides',
                        'post_title' => $cf7->posted_data["tu-nombre"],
                        'post_content' => $cf7->posted_data["tu-mensaje"],
                        'post_status' => 'pending',
                        'post_date' => current_time('mysql'),
                        'post_date_gmt' => current_time('mysql',1)
                    
                    )
                   
                );*/
        
          /*$filename = 'http://localhost/lqdvi/webcam/images/'.$cf7->posted_data["id"];
          $wp_filetype = wp_check_filetype(basename($filename), null );
          $attachment = array(
             'post_mime_type' => $wp_filetype['type'],
             'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
             'post_content' => '',
             'post_status' => 'inherit'
          );
          $attach_id = wp_insert_attachment( $attachment, $filename, 37 );
          // you must first include the image.php file
          // for the function wp_generate_attachment_metadata() to work
          require_once(ABSPATH . 'wp-admin/includes/image.php');
          $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
          wp_update_attachment_metadata( $attach_id, $attach_data );*/
        

        // Create post object
          $my_post_data = array(
             'post_type' => 'slides',
             'post_title' => $cf7->posted_data["tu-nombre"],
             'post_content' => $cf7->posted_data["tu-mensaje"],              
             'post_status' => 'pending',
             'post_date' => current_time('mysql'),
             'post_date_gmt' => current_time('mysql',1)
          );          
          $post_id = wp_insert_post( $my_post_data ); 

          $filename = $new_filename;
          $wp_filetype = wp_check_filetype(basename($filename), null );
          $attachment = array(
             'post_mime_type' => $wp_filetype['type'],
             'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
             'post_content' => '',
             'post_status' => 'inherit'
          );
          $attach_id = wp_insert_attachment( $attachment, $filename, $post_id );
          // you must first include the image.php file
          // for the function wp_generate_attachment_metadata() to work
          require_once(ABSPATH . 'wp-admin/includes/image.php');
          $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
          wp_update_attachment_metadata( $attach_id, $attach_data );          
          add_post_meta($post_id, '_thumbnail_id', $attach_id, true);
        }
        return $result;
    }
}


add_action( 'wpcf7_before_send_mail', 'save_application_form');
?>
