/** * Ads Garbage Collector * * This function will delete all the Ads that were created while posting ad in * the frontend, but user for some reason did not finished posting them. * * @since 0.1 * @return void */ function adverts_event_gc() { // find tmp adverts that were last modified more than 24 hours ago, // by this time it is safe to assume that user will not finish posting the ad. $posts = new WP_Query(array("post_type" => "advert", "post_status" => adverts_tmp_post_status(), "date_query" => array(array("column" => "post_modified_gmt", "before" => date("Y-m-d H:i:s", current_time('timestamp', 1) - 3600 * 24))))); if ($posts->post_count) { foreach ($posts->posts as $post) { // delete all attachements associated with this post. $param = array('post_parent' => $post->ID, 'post_type' => 'attachment'); $children = get_posts($param); if (is_array($children)) { foreach ($children as $attch) { wp_delete_attachment($attch->ID, true); } } // permanently delete the post wp_delete_post($post->ID, true); } } }
/** * Deletes temporary post * * This action is executed when user leaves post an ad form in the frontend * * No Priv * Action: adverts_delete_tmp * * @since 0.1 * @return void */ function adverts_delete_tmp() { // @todo check_ajax_referer( 'my-special-string', 'security' ); $id = adverts_request("id"); $post = get_post($id); if ($post === null || $post->post_status != adverts_tmp_post_status()) { echo json_encode(array('result' => 0, 'error' => __("Post with given ID does not exist.", "adverts"))); exit; } $param = array('post_parent' => $id, 'post_type' => 'attachment'); $children = get_posts($param); if (is_array($children)) { foreach ($children as $attch) { wp_delete_attachment($attch->ID, true); } } wp_delete_post($id, true); echo json_encode(array('result' => 1)); exit; }
/** * Generates HTML for [adverts_add] shortcode * * @param array $atts Shortcode attributes * @since 0.1 * @return string Fully formatted HTML for "post ad" form. */ function shortcode_adverts_add($atts) { wp_enqueue_style('adverts-frontend'); wp_enqueue_style('adverts-icons'); wp_enqueue_style('adverts-icons-animate'); wp_enqueue_script('adverts-frontend'); wp_enqueue_script('adverts-auto-numeric'); extract(shortcode_atts(array('name' => 'default', 'moderate' => false), $atts)); include_once ADVERTS_PATH . 'includes/class-html.php'; include_once ADVERTS_PATH . 'includes/class-form.php'; $form = new Adverts_Form(Adverts::instance()->get("form")); $valid = null; $error = array(); $info = array(); $bind = array(); $content = ""; $adverts_flash = array("error" => array(), "info" => array()); $action = apply_filters('adverts_action', adverts_request("_adverts_action", ""), __FUNCTION__); $post_id = adverts_request("_post_id", null); $post_id = $post_id > 0 ? $post_id : null; // $post_id hijack attempt protection here! if ($post_id > 0 && get_post($post_id)->post_author == get_current_user_id()) { // if post was already saved in DB (for example for preview) then load it. $post = get_post($post_id); // bind data by field name foreach ($form->get_fields() as $f) { $bind[$f["name"]] = get_post_meta($post_id, $f["name"], true); } $bind["post_title"] = $post->post_title; $bind["post_content"] = $post->post_content; $bind["advert_category"] = array(); $terms = get_the_terms($post_id, 'advert_category'); if (is_array($terms)) { foreach ($terms as $term) { $bind["advert_category"][] = $term->term_id; } } } elseif (is_user_logged_in()) { $bind["adverts_person"] = wp_get_current_user()->display_name; $bind["adverts_email"] = wp_get_current_user()->user_email; } if ($action == "") { // show post ad form page wp_enqueue_style('adverts-frontend-add'); $bind["_post_id"] = $post_id; $bind["_adverts_action"] = "preview"; $form->bind($bind); // adverts/templates/add.php ob_start(); include apply_filters("adverts_template_load", ADVERTS_PATH . 'templates/add.php'); $content = ob_get_clean(); } elseif ($action == "preview") { // show preview page wp_enqueue_style('adverts-frontend-add'); $form->bind((array) stripslashes_deep($_POST)); $valid = $form->validate(); $adverts_flash = array("error" => $error, "info" => $info); // Allow to preview only if data in the form is valid. if ($valid) { $init = array("post" => array("ID" => $post_id, "post_name" => sanitize_title($form->get_value("post_title")), "post_type" => "advert", "post_author" => get_current_user_id(), "post_date" => current_time('mysql'), "post_date_gmt" => current_time('mysql', 1), "post_status" => adverts_tmp_post_status(), "guid" => ""), "meta" => array()); if (adverts_config("config.visibility") > 0) { $init["meta"]["_expiration_date"] = array("value" => strtotime(current_time('mysql') . " +" . adverts_config("config.visibility") . " DAYS"), "field" => array("type" => "adverts_field_hidden")); } // Save post as temporary in DB $post_id = Adverts_Post::save($form, $post_id, $init); if (is_wp_error($post_id)) { $error[] = $post_id->get_error_message(); $valid = false; } $adverts_flash = array("error" => $error, "info" => $info); // adverts/templates/add-preview.php ob_start(); include apply_filters("adverts_template_load", ADVERTS_PATH . 'templates/add-preview.php'); $content = ob_get_clean(); } else { $error[] = __("There are errors in your form. Please correct them before proceeding.", "adverts"); $adverts_flash = array("error" => $error, "info" => $info); // adverts/templates/add.php ob_start(); include apply_filters("adverts_template_load", ADVERTS_PATH . 'templates/add.php'); $content = ob_get_clean(); } // endif $valid } elseif ($action == "save") { // Save form in the database $post_id = wp_update_post(array("ID" => $post_id, "post_status" => $moderate == "1" ? 'pending' : 'publish')); $info[] = __("Thank you for submitting your ad!", "adverts"); $adverts_flash = array("error" => $error, "info" => $info); if (!is_user_logged_in() && get_post_meta($post_id, "_adverts_account", true) == 1) { adverts_create_user_from_post_id($post_id, true); } // adverts/templates/add-save.php ob_start(); include apply_filters("adverts_template_load", ADVERTS_PATH . 'templates/add-save.php'); $content = ob_get_clean(); } return apply_filters("adverts_action_{$action}", $content, $form); }