function cp_update_listing()
{
    global $wpdb, $cp_options;
    // check to see if html is allowed
    if (!$cp_options->allow_html) {
        $post_content = appthemes_filter($_POST['post_content']);
    } else {
        $post_content = wp_kses_post($_POST['post_content']);
    }
    // keep only numeric, commas or decimal values
    if (!empty($_POST['cp_price'])) {
        $_POST['cp_price'] = appthemes_clean_price($_POST['cp_price']);
    }
    // keep only values and insert/strip commas if needed and put into an array
    if (!empty($_POST['tags_input'])) {
        $_POST['tags_input'] = appthemes_clean_tags($_POST['tags_input']);
        $new_tags = explode(',', $_POST['tags_input']);
    }
    // put all the ad elements into an array
    // these are the minimum required fields for WP (except tags)
    $update_ad = array();
    $update_ad['ID'] = trim($_POST['ad_id']);
    $update_ad['post_title'] = appthemes_filter($_POST['post_title']);
    $update_ad['post_content'] = trim($post_content);
    if ($cp_options->moderate_edited_ads) {
        $update_ad['post_status'] = 'pending';
    }
    // update the ad and return the ad id
    $post_id = wp_update_post($update_ad);
    if (!$post_id) {
        return false;
    }
    //update post custom taxonomy "ad_tags"
    // keep only values and insert/strip commas if needed and put into an array
    if (!empty($_POST['tags_input'])) {
        $_POST['tags_input'] = appthemes_clean_tags($_POST['tags_input']);
        $new_tags = explode(',', $_POST['tags_input']);
        $settags = wp_set_object_terms($post_id, $new_tags, APP_TAX_TAG);
    }
    // assemble the comma separated hidden fields back into an array so we can save them.
    $metafields = explode(',', $_POST['custom_fields_vals']);
    // loop through all custom meta fields and update values
    foreach ($metafields as $name) {
        if (!isset($_POST[$name])) {
            delete_post_meta($post_id, $name);
        } else {
            if (is_array($_POST[$name])) {
                delete_post_meta($post_id, $name);
                foreach ($_POST[$name] as $checkbox_value) {
                    add_post_meta($post_id, $name, wp_kses_post($checkbox_value));
                }
            } else {
                update_post_meta($post_id, $name, wp_kses_post($_POST[$name]));
            }
        }
    }
    cp_action_update_listing($post_id);
    return $post_id;
}
Example #2
0
function cp_update_listing()
{
    global $wpdb;
    // check to see if html is allowed
    if (get_option('cp_allow_html') != 'yes') {
        $post_content = appthemes_filter($_POST['post_content']);
    } else {
        $post_content = $_POST['post_content'];
    }
    // keep only numeric, commas or decimal values
    if (!empty($_POST['cp_price'])) {
        $_POST['cp_price'] = appthemes_clean_price($_POST['cp_price']);
    }
    // keep only values and insert/strip commas if needed and put into an array
    if (!empty($_POST['tags_input'])) {
        $_POST['tags_input'] = appthemes_clean_tags($_POST['tags_input']);
        $new_tags = explode(',', $_POST['tags_input']);
    }
    // put all the ad elements into an array
    // these are the minimum required fields for WP (except tags)
    $update_ad = array();
    $update_ad['ID'] = trim($_POST['ad_id']);
    $update_ad['post_title'] = appthemes_filter($_POST['post_title']);
    $update_ad['post_content'] = trim($post_content);
    //$update_ad['post_category']   = array((int)appthemes_filter($_POST['cat'])); // maybe use later if we decide to let users change categories
    //print_r($update_ad).' <- new ad array<br>'; // for debugging
    // update the ad and return the ad id
    $post_id = wp_update_post($update_ad);
    if ($post_id) {
        //update post custom taxonomy "ad_tags"
        // keep only values and insert/strip commas if needed and put into an array
        if (!empty($_POST['tags_input'])) {
            $_POST['tags_input'] = appthemes_clean_tags($_POST['tags_input']);
            $new_tags = explode(',', $_POST['tags_input']);
            $settags = wp_set_object_terms($post_id, $new_tags, APP_TAX_TAG);
            //echo 'Update Tags or Erro:'.print_r($settags, true);
        }
        // assemble the comma separated hidden fields back into an array so we can save them.
        $metafields = explode(',', $_POST['custom_fields_vals']);
        // loop through all custom meta fields and update values
        foreach ($metafields as $name) {
            if (!isset($_POST[$name])) {
                delete_post_meta($post_id, $name);
            } else {
                if (is_array($_POST[$name])) {
                    delete_post_meta($post_id, $name);
                    foreach ($_POST[$name] as $checkbox_value) {
                        add_post_meta($post_id, $name, $checkbox_value);
                    }
                } else {
                    update_post_meta($post_id, $name, $_POST[$name]);
                }
            }
        }
        $result = $post_id;
        cp_action_update_listing($post_id);
    } else {
        // the ad wasn't updated
        $result = false;
    }
    return $result;
}