/**
 * Save post hook.
 * 
 * @param type $parent_post_id
 * @return string 
 */
function wpcf_pr_admin_save_post_hook($parent_post_id)
{
    if (defined('DOING_AJAX') && !isset($_REQUEST['wpcf_action'])) {
        return array();
    }
    remove_action('save_post', 'wpcf_pr_admin_save_post_hook', 10, 2);
    static $processed = array();
    if (isset($processed[$parent_post_id])) {
        return array();
    }
    $results = array();
    if (!empty($_POST['wpcf_post_relationship'])) {
        $parent_post = get_post($parent_post_id);
        foreach ($_POST['wpcf_post_relationship'] as $post_id => $data) {
            $post_id_sent = $post_id;
            $post_type = $data['post_type'];
            unset($data['post_type']);
            $send_data = array();
            $send_data['wpcf_post_relationship'][$post_id] = $data;
            if (isset($_POST['wpcf_post_relationship_checkbox'][$post_id])) {
                $send_data['wpcf_post_relationship_checkbox'][$post_id] = $_POST['wpcf_post_relationship_checkbox'][$post_id];
            }
            $output = wpcf_pr_admin_save_child_item($parent_post, $post_type, $send_data);
            $results[$post_id_sent] = $output;
        }
    }
    $processed[$parent_post_id] = 1;
    return implode('', $results);
}
/**
 * Save post hook.
 * 
 * @param type $parent_post_id
 * @return string 
 */
function wpcf_pr_admin_save_post_hook($parent_post_id)
{
    // WPML check if it's translation of a child
    // Fix up the parent if it's the child of a related post and it doesn't yet have a parent
    if (function_exists('icl_object_id')) {
        remove_filter('get_post_metadata', 'wpcf_pr_meta_belongs_filter', 10, 4);
        $post = get_post($parent_post_id);
        $original_post_id = icl_object_id($parent_post_id, $post->post_type, false);
        if (!empty($original_post_id)) {
            // it has a translation
            $original_post = get_post($original_post_id);
            if (!empty($original_post)) {
                // look for _wpcf_belongs_xxxx_id fields.
                $metas = get_post_custom($original_post->ID);
                foreach ($metas as $meta_key => $meta) {
                    if (strpos($meta_key, '_wpcf_belongs_') !== false) {
                        $meta_post = get_post($meta[0]);
                        $exists = get_post_meta($parent_post_id, $meta_key, true);
                        if (!empty($meta_post) && empty($exists)) {
                            global $sitepress;
                            $ulanguage = $sitepress->get_language_for_element($parent_post_id, 'post_' . $post->post_type);
                            $meta_translated_id = icl_object_id($meta_post->ID, $meta_post->post_type, false, $ulanguage);
                            // Only force if meta is not already set
                            if (!empty($meta_translated_id)) {
                                update_post_meta($parent_post_id, $meta_key, $meta_translated_id);
                            }
                        }
                    }
                }
            }
        }
        add_filter('get_post_metadata', 'wpcf_pr_meta_belongs_filter', 10, 4);
    }
    // Save child items
    if (defined('DOING_AJAX') && !isset($_REQUEST['wpcf_action'])) {
        return array();
    }
    // ONLY AJAX
    if (!defined('DOING_AJAX') || !isset($_REQUEST['wpcf_action'])) {
        return array();
    }
    remove_action('save_post', 'wpcf_pr_admin_save_post_hook', 10, 2);
    static $processed = array();
    if (isset($processed[$parent_post_id])) {
        return array();
    }
    $parent_post = get_post($parent_post_id);
    $results = array();
    if (!empty($_POST['wpcf_post_relationship'])) {
        foreach ($_POST['wpcf_post_relationship'] as $post_id => $data) {
            $post_id_sent = $post_id;
            $post_type = $data['post_type'];
            unset($data['post_type']);
            $send_data = array();
            $send_data['wpcf_post_relationship'][$post_id] = $data;
            if (isset($_POST['wpcf_post_relationship_checkbox'][$post_id])) {
                $send_data['wpcf_post_relationship_checkbox'][$post_id] = $_POST['wpcf_post_relationship_checkbox'][$post_id];
            }
            extract(wpcf_pr_admin_save_child_item($parent_post, $post_type, $send_data));
            $results[$post_id_sent] = $output;
        }
    }
    if (isset($_POST['wpcf_post_relationship_checkboxes'])) {
        foreach ($_POST['wpcf_post_relationship_checkboxes'] as $post_id => $data) {
            foreach ($data as $temp_slug => $temp_options) {
                if (!isset($_POST['wpcf_post_relationship'][$post_id][$temp_slug])) {
                    $post_type = isset($_POST['post_type_child']) ? $_POST['post_type_child'] : $_POST['post_type'];
                    extract(wpcf_pr_admin_save_child_item($parent_post, $post_type, array('wpcf_post_relationship' => array($new_post_id => array($new_post_id => array())))));
                    $results[$post_id] = $output;
                }
            }
        }
    }
    $processed[$parent_post_id] = 1;
    return implode('', $results);
}