/**
 * Save contact fields to database
 *
 * @global array $company_fields List of contact fields
 * @return string|boolean Post id if succesful and false if on error
 * @since 0.1
 */
function _rolo_save_contact_fields()
{
    global $contact_fields;
    //TODO - Check whether the current use is logged in or not
    //TODO - Check for nounce
    $post_id = 0;
    if (isset($_POST['contact_id'])) {
        $old_post = array();
        $post_id = $_POST['contact_id'];
        $old_post['post_title'] = $_POST['rolo_contact_first_name'];
        if (isset($_POST['rolo_contact_last_name'])) {
            $old_post['post_title'] .= ' ' . $_POST['rolo_contact_last_name'];
        }
        $old_post['ID'] = $post_id;
        $post_id = wp_update_post($old_post);
    } else {
        $new_post = array();
        $new_post['post_title'] = $_POST['rolo_contact_first_name'];
        if (isset($_POST['rolo_contact_last_name'])) {
            $new_post['post_title'] .= ' ' . $_POST['rolo_contact_last_name'];
        }
        $new_post['post_type'] = 'post';
        $new_post['post_status'] = 'publish';
        $post_id = wp_insert_post($new_post);
    }
    // Store only first name and last name as seperate custom fields
    update_post_meta($post_id, 'rolo_contact_first_name', $_POST['rolo_contact_first_name']);
    update_post_meta($post_id, 'rolo_contact_last_name', $_POST['rolo_contact_last_name']);
    wp_set_post_terms($post_id, $_POST['rolo_contact_post_tag']);
    if ($post_id) {
        $new_contact = array();
        foreach ($contact_fields as $contact_field) {
            if (function_exists($contact_field['save_function'])) {
                call_user_func_array($contact_field['save_function'], array($contact_field['name'], $post_id, &$new_contact));
            } else {
                $data = $_POST['rolo_contact_' . $contact_field['name']];
                //            TODO - Validate data
                $new_contact['rolo_contact_' . $contact_field['name']] = $data;
                //                update_post_meta($post_id, 'rolo_contact_' . $contact_field['name'], $data);
            }
        }
        // store the array as post meta
        update_post_meta($post_id, 'rolo_contact', $new_contact);
        // photo
        update_post_meta($post_id, 'rolo_contact_filename', $_POST['filename']);
        // ------------------------------------------
        if (!empty($_POST['filename'])) {
            // como ja fizemos o upload por ajax agora basta ir buscar as imagens ah directoria temp
            $upload = wp_upload_bits($_POST['filename'], null, file_get_contents(ABSPATH . "wp-content/themes/rolopress-core/library/includes/" . $_POST['filename']));
            //	print_r($upload);
            $type = '';
            if (!empty($upload['type'])) {
                $type = $upload['type'];
            } else {
                $mime = wp_check_filetype($upload['file']);
                if ($mime) {
                    $type = $mime['type'];
                }
            }
            $attachment = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_parent' => $post_id, 'post_mime_type' => $type, 'guid' => $upload['url']);
            require_once "wp-admin/includes/image.php";
            // Save the data
            $id = wp_insert_attachment($attachment, $upload['file'], $post_id);
            wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
            // Set as featured
            set_featured_foto($post_id, $id);
        }
        // ------------------------------------------
        // frame
        update_post_meta($post_id, 'rolo_contact_framename', $_POST['framename']);
        // importance
        update_post_meta($post_id, 'rolo_contact_importance', $_POST['importance']);
        // Set the custom taxonmy for the post
        wp_set_post_terms($post_id, 'Contact', 'type');
    } else {
        //        TODO - handle error
    }
    //handle pictures
    // print_r($_POST);
    return $post_id;
}
Exemple #2
0
function handle_images($post_id, $uploadedfotos, $featuredfoto)
{
    //upload images
    $arr_files = split(" ", $uploadedfotos);
    $possible_featured = 0;
    foreach ($arr_files as $file) {
        // if the file has been uploaded already break
        $uploaded = already_uploaded($post_id, $file);
        if ($uploaded) {
            //continues after checking if featured changed
            if ($featuredfoto == $file) {
                set_featured_foto($post_id, $uploaded->ID);
            }
            continue;
        }
        if (!file_exists("wp-content/themes/genesis/uploads/" . $file)) {
            continue;
        }
        if (empty($file)) {
            continue;
        }
        // como ja fizemos o upload por ajax agora basta ir buscar as imagens ah directoria temp
        $upload = wp_upload_bits($file, null, file_get_contents(ABSPATH . "wp-content/themes/genesis/uploads/" . $file));
        $type = '';
        if (!empty($upload['type'])) {
            $type = $upload['type'];
        } else {
            $mime = wp_check_filetype($upload['file']);
            if ($mime) {
                $type = $mime['type'];
            }
        }
        $attachment = array('post_title' => basename($upload['file']), 'post_content' => '', 'post_type' => 'attachment', 'post_parent' => $post_id, 'post_mime_type' => $type, 'guid' => $upload['url']);
        require_once "wp-admin/includes/image.php";
        // Save the data
        $id = wp_insert_attachment($attachment, $upload['file'], $post_id);
        if ($possible_featured == 0) {
            $possible_featured = $id;
        }
        wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file']));
        // Set as featured
        if ($_POST['featuredfoto'] == $file) {
            set_featured_foto($post_id, $id);
        }
    }
    if ($_POST['featuredfoto'] == 'removefeatured') {
        $featuredid = get_post_thumbnail_id($post_id);
        wp_delete_attachment($featuredid);
    }
    //no featured photo was selected
    if (empty($_POST['featuredfoto']) && $possible_featured != 0 && $post_id != 0) {
        $meta = get_post_meta($post_id, '_thumbnail_id');
        //checks if also the post_id does not have any featured image
        if (empty($meta)) {
            set_featured_foto($post_id, $possible_featured);
        }
    }
}