Пример #1
0
/**
 * MRP_save_relationships - Save the relations
 *
 * @param int   $post_id - The id of the post being saved
 * @param array $related_posts - A list of post_id's
 */
function MRP_save_relationships($post_id, $related_posts)
{
    global $wpdb;
    # First delete the relationships that were there before
    MRP_delete_relationships($post_id);
    # Now add/update the relations
    if ($related_posts) {
        $options = get_option("MRP_options");
        if ($options['display_reciprocal']) {
            $query = "SELECT * FROM " . $wpdb->prefix . "post_relationships WHERE post1_id=" . $post_id . " OR post2_id=" . $post_id . ";";
            $existing_ones = $wpdb->get_results($query);
            $query = "";
            foreach ($related_posts as $rel_cct_list) {
                $order_counter = 0;
                foreach ($rel_cct_list as $rel_post) {
                    $not_updatable = true;
                    foreach ($existing_ones as $k => $v) {
                        if ($rel_post == $v->post1_id or $rel_post == $v->post2_id) {
                            if ($v->post1_id == $post_id) {
                                $left_q = "post1_id=" . $post_id;
                                $right_q = "post2_id=" . $v->post2_id;
                                $relation_order = "position1=" . $order_counter;
                            } else {
                                $left_q = "post1_id=" . $post_id;
                                $right_q = "post1_id=" . $v->post1_id;
                                $relation_order = "position2=" . $order_counter;
                            }
                            $query = "UPDATE " . $wpdb->prefix . "post_relationships SET {$relation_order} WHERE {$left_q} AND {$right_q};";
                            $result = $wpdb->query($query);
                            $existing_ones[$k]->remains = true;
                            $not_updatable = false;
                            break;
                        } else {
                            $not_updatable = true;
                        }
                    }
                    if ($not_updatable) {
                        $query = "INSERT INTO " . $wpdb->prefix . "post_relationships VALUES ({$post_id},{$rel_post},{$order_counter},0);";
                        $result = $wpdb->query($query);
                    }
                    $order_counter++;
                }
            }
            foreach ($existing_ones as $k => $v) {
                if (!$v->remains) {
                    if ($v->post1_id == $post_id) {
                        $side = "post1_id";
                        $post_in_relation = "post2_id=" . $v->post2_id;
                    } else {
                        $side = "post2_id";
                        $post_in_relation = "post1_id=" . $v->post1_id;
                    }
                    $query = "DELETE FROM " . $wpdb->prefix . "post_relationships WHERE " . $side . "=" . $post_id . " AND " . $post_in_relation . ";";
                    $result = $wpdb->query($query);
                }
            }
        } else {
            MRP_delete_relationships($post_id);
            foreach ($related_posts as $related_post_sub_list) {
                $counter = 0;
                foreach ($related_post_sub_list as $related_post) {
                    $related_post = (int) $related_post;
                    $new_count = $counter++;
                    $query = "INSERT INTO " . $wpdb->prefix . "post_relationships VALUES( {$post_id}, {$related_post} , 0, {$new_count} )";
                    $result = $wpdb->query($query);
                }
            }
        }
    }
}
/**
 * MRP_save_relationships - Save the relations
 *
 * @param int   $post_id - The id of the post being saved
 * @param array $related_posts - A list of associative arrays with keys 'id', 'rel_type', and 'rel_strength'
 */
function MRP_save_relationships($post_id, $related_posts)
{
    global $wpdb;
    # First delete the relationships that were there before
    MRP_delete_relationships($post_id);
    # Now add/update the relations
    if ($related_posts) {
        foreach ($related_posts as $related_post) {
            $id = (int) $related_post['id'];
            $rel_type = $related_post['rel_type'];
            $rel_strength = $related_post['rel_strength'];
            $query = "INSERT INTO " . $wpdb->prefix . "post_relationships VALUES ( {$post_id}, {$id}, '{$rel_type}', {$rel_strength} )";
            $result = $wpdb->query($query);
        }
    }
}