function ft_save_meta_box_data($post_id)
{
    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */
    // Check if our nonce is set.
    if (!isset($_POST['ft_meta_box_nonce'])) {
        return;
    }
    // Verify that the nonce is valid.
    if (!wp_verify_nonce($_POST['ft_meta_box_nonce'], 'ft_meta_box')) {
        return;
    }
    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // Check the user's permissions.
    if (isset($_POST['post_type']) && 'page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return;
        }
    } else {
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
    }
    /* OK, it's safe for us to save the data now. */
    // Sanitize user input.
    update_post_meta($post_id, 'family_tree', $_POST['family_tree']);
    update_post_meta($post_id, 'member1', $_POST['member1']);
    update_post_meta($post_id, 'member2', $_POST['member2']);
    update_post_meta($post_id, 'relation_type', $_POST['relation_type']);
    update_post_meta($post_id, 'member1_role', $_POST['member1_role']);
    update_post_meta($post_id, 'member2_role', $_POST['member2_role']);
    $my_post = array('ID' => $post_id, 'post_title' => ft_get_member_name($_POST['member1']) . ' is ' . ft_get_role_title($_POST['member1_role']) . ' of ' . ft_get_member_name($_POST['member2']));
    // unhook this function so it doesn't loop infinitely
    remove_action('save_post', 'ft_save_meta_box_data');
    // update the post, which calls save_post again
    wp_update_post($my_post);
    // re-hook this function
    add_action('save_post', 'ft_save_meta_box_data');
    write_family_json_file($_POST['family_tree']);
}
Пример #2
0
function ft_get_member_merried_to_name($member_id, $sex)
{
    global $wpdb;
    $married_id = get_page_by_title('Married', '', 'ft_relation_type');
    if ($sex == "F") {
        $args = array('post_type' => 'ft_relation', 'posts_per_page' => 1, 'order' => 'ASC', 'orderby' => 'post_title', 'meta_query' => array('relation' => 'AND', array('key' => 'member2', 'value' => $member_id, 'type' => 'numeric', 'compare' => '='), array('key' => 'relation_type', 'value' => $married_id->ID, 'type' => 'numeric', 'compare' => '=')));
    } else {
        $args = array('post_type' => 'ft_relation', 'posts_per_page' => 1, 'order' => 'ASC', 'orderby' => 'post_title', 'meta_query' => array('relation' => 'AND', array('key' => 'member1', 'value' => $member_id, 'type' => 'numeric', 'compare' => '='), array('key' => 'relation_type', 'value' => $married_id->ID, 'type' => 'numeric', 'compare' => '=')));
    }
    $the_query = new WP_Query($args);
    if ($the_query->have_posts()) {
        while ($the_query->have_posts()) {
            $the_query->the_post();
            if ($sex == "F") {
                return '<br>with ' . ft_get_member_name(get_post_meta(get_the_ID(), 'member1', true));
            } else {
                return '<br>with ' . ft_get_member_name(get_post_meta(get_the_ID(), 'member2', true));
            }
        }
        wp_reset_postdata();
    } else {
        return '';
    }
}