/** * Generates HTML for ad edit form (in [adverts_manage] shortcode) * * @param array $atts Shortcode attributes * @since 0.1 * @return void * @access private */ function _adverts_manage_edit($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(); $action = apply_filters('adverts_action', adverts_request("_adverts_action", ""), __FUNCTION__); $post_id = adverts_request("advert_id", null); // $post_id hijack attempt protection here! $post = get_post($post_id); if ($post === null) { $error[] = __("Ad does not exist.", "adverts"); adverts_flash(array("error" => $error)); return; } if ($post->post_author != get_current_user_id()) { $error[] = __("You do not own this Ad.", "adverts"); adverts_flash(array("error" => $error)); return; } $slist = apply_filters("adverts_sh_manage_list_statuses", array('publish', 'expired', 'pending', 'draft')); if (!in_array($post->post_status, $slist)) { $error[] = sprintf(__("Incorrect post status [%s].", "adverts"), $post->post_status); adverts_flash(array("error" => $error)); return; } foreach ($form->get_fields() as $f) { $bind[$f["name"]] = get_post_meta($post_id, $f["name"], true); } $bind["_adverts_action"] = "update"; $bind["_post_id"] = $post_id; $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; } } $form->bind($bind); if ($action == "update") { $form->bind((array) stripslashes_deep($_POST)); $valid = $form->validate(); if ($valid) { $post_id = Adverts_Post::save($form, $post_id); if (is_wp_error($post_id)) { $error[] = $post_id->get_error_message(); } else { $info[] = __("Post has been updated.", "adverts"); } } else { $error[] = __("Cannot update. There are errors in your form.", "adverts"); } } $adverts_flash = array("error" => $error, "info" => $info); // adverts/templates/manage-edit.php include apply_filters("adverts_template_load", ADVERTS_PATH . 'templates/manage-edit.php'); }
/** * Renders Payments History List and Edit Page. * * The page is rendered in wp-admin / Classifieds / Payments History panel * * @global wpdb $wpdb * @global wp_locale $wp_locale * * @since 0.1 * @return void */ function adext_payments_page_history() { global $wpdb, $wp_locale; wp_enqueue_style('adverts-admin'); $flash = Adverts_Flash::instance(); if (adverts_request("add")) { // Do nothing currently users cannot add pricing manually. } elseif (adverts_request("edit")) { // Display payment edit page. $payment = get_post(adverts_request("edit")); $form = new Adverts_Form(); $form->load(Adverts::instance()->get("form_payments_history")); $form->bind(Adverts_Post::to_array($payment)); $gateway_name = get_post_meta($payment->ID, '_adverts_payment_gateway', true); $gateway = adext_payment_gateway_get($gateway_name); if (!$gateway) { $msg = sprintf(__("Payment Method [%s] assigned to this Payment does not exist or was disabled.", "adverts"), $gateway_name); $flash->add_error($msg); } if (isset($_POST) && !empty($_POST)) { $form->bind(stripslashes_deep($_POST)); $valid = $form->validate(); if ($valid) { $status_new = $form->get_value("post_status"); $status_old = $payment->post_status; $post_id = Adverts_Post::save($form, $payment); if (is_numeric($post_id) && $post_id > 0 && $status_old != $status_new) { do_action("adverts_payment_" . $status_new, $payment); $text = __('<strong>%1$s</strong> changed payment status to <strong>%2$s</strong>', 'adverts'); $message = sprintf($text, wp_get_current_user()->user_login, $status_new); adext_payments_log($post_id, $message); $payment = get_post($post_id); } if (is_wp_error($post_id)) { $flash->add_error($post_id->get_error_message()); } elseif ($post_id === 0) { $flash->add_error(__("There was an error while saving pricing in database.", "adverts")); } else { $flash->add_info(__("Payment updated.", "adverts")); } } else { $flash->add_error(__("There are errors in your form.", "adverts")); } } include ADVERTS_PATH . 'addons/payments/admin/payment-history-edit.php'; } elseif (adverts_request('delete')) { // Delete Payment $post = get_post(adverts_request('delete')); $i = 0; if (!$post || $post->post_type != 'adverts-payment') { wp_die(__('Adverts Payment with given ID does not exist.', 'adverts')); } foreach (get_children($post) as $child) { wp_delete_attachment($child->ID, true); $i++; } $flash->add_info(sprintf(_n("1 Payment deleted.", "%s Payments deleted.", $i, "adverts"), $i)); wp_delete_post($post->ID, true); wp_redirect(remove_query_arg(array('delete', 'noheader', 'pg'))); exit; } elseif (adverts_request('filter_action')) { // Apply filters and return to payments history list $url = remove_query_arg(array('delete', 'noheader', 'pg')); $url = add_query_arg(array('month' => adverts_request('month')), $url); wp_redirect($url); exit; } elseif (adverts_request('action') || adverts_request('action2')) { // Apply bulk actions and return to payments history list $action = adverts_request('action'); if (empty($action)) { $action = adverts_request('action2'); } $item = adverts_request('item'); foreach ($item as $id) { if ($action == "delete") { foreach (get_children($id) as $child) { wp_delete_attachment($child->ID, true); } wp_delete_post($id, true); $flash->add_info(__("Payments deleted.", "adverts")); } elseif (stripos($action, "set-status-") === 0) { $status_old = get_post_status($id); $status_new = str_replace("set-status-", "", $action); $status_obj = get_post_status_object($status_new); if ($status_old != $status_new) { wp_update_post(array("ID" => $id, "post_status" => $status_new)); do_action("adverts_payment_" . $status_new, $payment); } $flash->add_info(sprintf(__("Status for selected Payments was changed to: %s"), $status_obj->label)); } } wp_redirect(remove_query_arg(array('delete', 'noheader', 'pg'))); exit; } else { // Display Payments History $status_list = array("pending" => 0, "completed" => 0, "failed" => 0, "refunded" => 0); foreach ($status_list as $k => $v) { $sql = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = %s AND post_type = 'adverts-payment'"; $status_list[$k] = (int) $wpdb->get_var($wpdb->prepare($sql, $k)); } $sql = "SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month FROM {$wpdb->posts} WHERE post_type = %s ORDER BY post_date DESC"; $months_list = $wpdb->get_results($wpdb->prepare($sql, 'adverts-payment')); $months = array(); foreach ($months_list as $m) { $months[] = array("value" => $m->year . "-" . $m->month, "label" => sprintf(__('%1$s %2$d'), $wp_locale->get_month($m->month), $m->year)); } $month = adverts_request("month", ""); $filter = adverts_request("status", ""); $loop_params = array('post_type' => 'adverts-payment', 'posts_per_page' => 20, 'paged' => adverts_request('pg', 1)); if ($filter) { $loop_params['post_status'] = $filter; } if ($month == "this-month") { $before = date('Y-m-d H:i:s', strtotime('last day of this month', current_time('timestamp'))); $after = date('Y-m-d H:i:s', strtotime('first day of this month', current_time('timestamp'))); $loop_params['date_query'] = array(array('before' => $before, 'after' => $after, 'inclusive' => true)); } elseif ($month == "last-month") { $before = date('Y-m-d H:i:s', strtotime('last day of last month', current_time('timestamp'))); $after = date('Y-m-d H:i:s', strtotime('first day of last month', current_time('timestamp'))); $loop_params['date_query'] = array(array('before' => $before, 'after' => $after, 'inclusive' => true)); } elseif (!empty($month)) { $time = strtotime($month . "-10"); $before = date('Y-m-d', strtotime('last day of this month', $time)); $after = date('Y-m-d H:i:s', strtotime('first day of this month', $time)); $loop_params['date_query'] = array(array('before' => '', 'after' => '', 'inclusive' => true)); } $loop = new WP_Query($loop_params); include ADVERTS_PATH . 'addons/payments/admin/payment-history-list.php'; } }
/** * Saves data in DB * * @param Adverts_Form $form * @param WP_Post $post * @param array $defaults */ public static function save(Adverts_Form $form, $post = null, $init = array()) { if (is_numeric($post)) { $post = get_post($post); } // Change Post GUID using post_guid filter if the post is already saved in DB // (otherwise guid will be set automatically) if (isset($init["post"]["ID"]) && isset($init["post"]["guid"]) && $init["post"]["guid"] == "") { self::$_tmp_guid = get_post_permalink($init["post"]["ID"]); add_filter("post_guid", array(__CLASS__, "tmp_guid")); } $data = array(); $meta = array(); $taxo = array(); // Set default values if (isset($init["post"]) && is_array($init["post"])) { $data = $init["post"]; } if (isset($init["meta"]) && is_array($init["meta"])) { $meta = $init["meta"]; } if (isset($init["taxo"]) && is_array($init["taxo"])) { $taxo = $init["taxo"]; } // Merge defaults with data from the Adverts_Form foreach ($form->get_fields() as $field) { if (property_exists("WP_Post", $field["name"])) { $data[$field["name"]] = $field["value"]; } elseif (taxonomy_exists($field["name"])) { $taxo[$field["name"]] = $field["value"]; } elseif (isset($field["value"])) { $meta[$field["name"]] = array("field" => $field, "value" => $field["value"]); } } if ($post && $post->ID > 0) { // Post already exists, update only. $data["ID"] = $post->ID; $post_id = wp_update_post(apply_filters("adverts_update_post", $data)); } else { // Post does not exist, insert it. $data["comment_status"] = "closed"; $post_id = wp_insert_post(apply_filters("adverts_insert_post", $data)); } if (is_wp_error($post_id)) { return $post_id; } // Save meta data values $fields = Adverts::instance()->get("form_field"); foreach ($meta as $key => $data) { $field = $data["field"]; $field_type = $field["type"]; $value = $data["value"]; $callback_save = $fields[$field_type]["callback_save"]; if (is_callable($callback_save)) { call_user_func($callback_save, $post_id, $key, $value); } } // Save taxonomies foreach ($taxo as $key => $tax) { wp_set_post_terms($post_id, $tax, $key); } if (self::$_tmp_guid) { // After save tmp_guid filter is no longer needed, remove it. self::$_tmp_guid = null; remove_filter("post_guid", array(__CLASS__, "tmp_guid")); } return $post_id; }