/** * Renders default/core config form. * * The page is rendered in wp-admin / Classifieds / Options / Core * * @since 0.1 * @return void */ function adext_core_page_options() { $page_title = __("Core Options", "adverts"); $button_text = __("Update Options", "adverts"); wp_enqueue_style('adverts-admin'); $flash = Adverts_Flash::instance(); $error = array(); $scheme = Adverts::instance()->get("form_core_config"); $form = new Adverts_Form($scheme); if (isset($_POST) && !empty($_POST)) { $form->bind(stripslashes_deep($_POST)); $valid = $form->validate(); if ($valid) { $data = $form->get_values(); $data["module"] = adverts_config('config.module'); update_option("adverts_config", $data); $flash->add_info(__("Settings updated.", "adverts")); } else { $flash->add_error(__("There are errors in your form.", "adverts")); } } else { $form->bind(adverts_config("config.ALL")); } include ADVERTS_PATH . 'addons/core/admin/options.php'; }
/** * AJAX Function renders payment form in [adverts_add] third step. * * This function renders a proper payment form based on $_REQUEST['gateway'] value * and echos it to the browser as a JSON code. * * @since 0.1 * @return void */ function adext_payments_ajax_render() { $gateway_name = adverts_request('gateway'); $gateway = adext_payment_gateway_get($gateway_name); $listing_id = adverts_request("listing_id"); $response = null; $data = array(); $data["page_id"] = adverts_request("page_id"); $data["listing_id"] = adverts_request("listing_id"); $data["object_id"] = adverts_request("object_id"); $data["payment_for"] = "post"; $data["gateway_name"] = $gateway_name; $data["bind"] = array(); foreach (adverts_request('form', array()) as $item) { $data["bind"][$item["name"]] = $item["value"]; } $form = new Adverts_Form(); $form->load($gateway["form"]["payment_form"]); $form->bind($data["bind"]); if (isset($data["bind"]) && !empty($data["bind"])) { $isValid = $form->validate(); if ($isValid) { $pricing = get_post($data["listing_id"]); $price = get_post_meta($listing_id, "adverts_price", true); $payment_data = array('post_title' => $form->get_value("adverts_person"), 'post_content' => '', 'post_status' => 'pending', 'post_type' => 'adverts-payment'); $meta = array("pricing" => array("post_title" => $pricing->post_title, "visible" => get_post_meta($pricing->ID, "adverts_visible", true))); $payment_id = wp_insert_post($payment_data); update_post_meta($payment_id, 'adverts_person', $form->get_value('adverts_person')); update_post_meta($payment_id, 'adverts_email', $form->get_value('adverts_email')); update_post_meta($payment_id, '_adverts_user_ip', adverts_get_ip()); update_post_meta($payment_id, '_adverts_user_id', wp_get_current_user()->ID); update_post_meta($payment_id, '_adverts_object_id', $data["object_id"]); update_post_meta($payment_id, '_adverts_pricing_id', $data["listing_id"]); update_post_meta($payment_id, '_adverts_payment_gateway', $data["gateway_name"]); update_post_meta($payment_id, '_adverts_payment_for', $data["payment_for"]); update_post_meta($payment_id, '_adverts_payment_paid', "0"); update_post_meta($payment_id, '_adverts_payment_total', $price); update_post_meta($payment_id, '_adverts_payment_meta', $meta); $data["price"] = $price; $data["form"] = $form->get_values(); $data["payment_id"] = $payment_id; $data = apply_filters("adverts_payments_order_create", $data); $response = call_user_func($gateway["callback"]["render"], $data); } } if ($response === null) { ob_start(); include ADVERTS_PATH . 'templates/form.php'; $html_form = ob_get_clean(); $response = array("result" => 0, "html" => $html_form, "execute" => null); } echo json_encode($response); exit; }
/** * Renders config Bank Transfer config form. * * The page is rendered in wp-admin / Classifieds / Options / Bank Transfer * * @since 0.1 * @return void */ function adext_bank_transfer_page_options() { wp_enqueue_style('adverts-admin'); $flash = Adverts_Flash::instance(); $error = array(); $scheme = Adverts::instance()->get("form_bank_transfer_config"); $form = new Adverts_Form($scheme); $button_text = __("Update Options", "adverts"); if (isset($_POST) && !empty($_POST)) { $form->bind(stripslashes_deep($_POST)); $valid = $form->validate(); if ($valid) { update_option("adext_bank_transfer_config", $form->get_values()); $flash->add_info(__("Settings updated.", "adverts")); } else { $flash->add_error(__("There are errors in your form.", "adverts")); } } else { $form->bind(get_option("adext_bank_transfer_config", array())); } include ADVERTS_PATH . 'addons/bank-transfer/admin/options.php'; }
/** * Sets default values for Bank Transfer form * * This function checks if current payment form is Bank Transfer. If so and * the $data is empty then we set default values for contact person and email fields. * * @param Adverts_Form $form Instance of form. * @param array $data User submitted form values ( key => value ) * @return Adverts_Form Modified instance of form. */ function adext_bank_transfer_form_bind_defaults(Adverts_Form $form, $data) { $scheme = $form->get_scheme(); if ($scheme["name"] != "adverts-bank-transfer") { return $form; } if (empty($data) && adverts_request("action") == "adext_payments_render") { $ad = get_post(adverts_request("object_id")); $form->set_value("adverts_person", get_post_meta($ad->ID, "adverts_person", true)); $form->set_value("adverts_email", get_post_meta($ad->ID, "adverts_email", true)); } return $form; }
/** * 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'; } }
/** * Adds meta box with additional advert information * * @uses Adverts * @uses Adverts_Form * @see add_meta_box() * * @param WP_Post $post * @since 0.1 * @return void */ function adverts_data_box_content($post) { wp_nonce_field(plugin_basename(__FILE__), 'product_price_box_content_nonce'); $exclude = array("_adverts_account", "advert_category", "post_title", "gallery", "post_content"); // Load form data $form = new Adverts_Form(); $form->load(Adverts::instance()->get("form")); // Get list of fields from post meta table $bind = array(); foreach ($form->get_fields(array("exclude" => $exclude)) as $f) { $bind[$f["name"]] = get_post_meta($post->ID, $f["name"], true); } // Bind data $form->bind($bind); // Validate if message 21 will be displayed, that is if form already failed // validation in adverts_save_post_validator() function if (isset($_GET['message']) && $_GET['message'] == 21) { $form->validate(); } ?> <style type="text/css"> .adverts-data-table th.adverts-data-header { font-size:1.4em; font-weight: normal; font-variant: small-caps; padding: 20px 10px 0px 0px } .adverts-data-table input[type="text"], .adverts-data-table textarea { width: 99%; } </style> <table class="form-table adverts-data-table"> <tbody> <?php foreach ($form->get_fields(array("exclude" => $exclude)) as $field) { ?> <tr class="<?php if (isset($field["error"]) && !empty($field["error"])) { ?> form-invalid<?php } ?> "> <?php if ($field["type"] == "adverts_field_header") { ?> <th scope="row" colspan="2" class="adverts-data-header"> <?php esc_html_e($field["label"]); ?> </th> <?php } else { ?> <th scope="row"> <label for="<?php esc_attr_e($field["name"]); ?> "><?php esc_html_e($field["label"]); ?> </label></th> <td> <?php call_user_func(adverts_field_get_renderer($field), $field); ?> <?php if (isset($field["error"]) && !empty($field["error"])) { ?> <ul> <?php foreach ($field["error"] as $error) { ?> <li><?php esc_html_e($error); ?> </li> <?php } ?> </ul> <?php } ?> </td> <?php } ?> </tr> <?php } ?> </tbody> </table> <?php }
/** * Layout for forms generated by Adverts in wp-admin panel. * * @param Adverts_Form $form * @param array $options * @since 0.1 * @return void */ function adverts_form_layout_config(Adverts_Form $form, $options = array()) { $a = array(); ?> <?php foreach ($form->get_fields(array("type" => array("adverts_field_hidden"))) as $field) { ?> <?php call_user_func(adverts_field_get_renderer($field), $field); ?> <?php } ?> <?php foreach ($form->get_fields($options) as $field) { ?> <?php if ($field["type"] == "adverts_field_header") { ?> <tr valign="top"> <th colspan="2" style="padding-bottom:0px"> <h3 style="border-bottom:1px solid #dfdfdf; line-height:1.4em; font-size:15px"><?php esc_html_e($field["title"]); ?> </h3> </th> </tr> <?php } else { ?> <tr valign="top" class="<?php if (adverts_field_has_errors($field)) { ?> adverts-field-error<?php } ?> "> <th scope="row"> <label <?php if (!in_array($field['type'], $a)) { ?> for="<?php esc_attr_e($field["name"]); ?> "<?php } ?> > <?php esc_html_e($field["label"]); ?> <?php if (adverts_field_has_validator($field, "is_required")) { ?> <span class="adverts-red"> *</span><?php } ?> </label> </th> <td class=""> <?php switch ($field["type"]) { case "adverts_field_text": $field["class"] = (isset($field["class"]) ? $field["class"] : '') . ' regular-text'; break; } ?> <?php call_user_func(adverts_field_get_renderer($field), $field); ?> <?php if (isset($field['hint']) && !empty($field['hint'])) { ?> <br/><span class="description"><?php echo $field['hint']; ?> </span> <?php } ?> <?php if (adverts_field_has_errors($field)) { ?> <ul class="updated adverts-error-list"> <?php foreach ($field["error"] as $k => $v) { ?> <li><?php esc_html_e($v); ?> </li> <?php } ?> </ul> <?php } ?> </td> </tr> <?php } ?> <?php } ?> <?php }
/** * 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'); }
/** * Binds default payment_listing_type value * * @see adext_payments_form_load() Function which adds this function to filters list * @uses adverts_form_bind Filter which exexutes this function * * @since 1.0 * @access public * @param Adverts_Form $form * @return Adverts_Form */ function adext_payments_form_bind(Adverts_Form $form) { if (!$form->get_value("payments_listing_type")) { $form->set_value("payments_listing_type", adverts_config('payments.default_pricing')); } return $form; }
/** * 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; }