Пример #1
0
/**
 * 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);
}
Пример #2
0
/**
 * Payment action
 * 
 * This function is executed when "payment" action is run shortcode_adverts_add
 * 
 * @see shortcode_adverts_add()
 * @since 1.0
 * 
 * $param string $content 
 * @param Adverts_Form $form
 * @return null
 */
function adext_payments_action_payment($content, Adverts_Form $form)
{
    $info[] = __("Thank you for submitting your ad!", "adverts");
    $error = array();
    wp_enqueue_script('adext-payments');
    $adverts_flash = array("error" => $error, "info" => $info);
    $post_id = adverts_request("_post_id");
    $post = get_post($post_id);
    wp_update_post(array("ID" => $post_id, "post_status" => "advert-pending"));
    if (!is_user_logged_in() && get_post_meta($post_id, "_adverts_account", true) == 1) {
        adverts_create_user_from_post_id($post_id, true);
    }
    $listing_id = get_post_meta($post_id, "payments_listing_type", true);
    $listing = get_post($listing_id);
    $price = get_post_meta($listing_id, 'adverts_price', true);
    ob_start();
    include ADVERTS_PATH . 'addons/payments/templates/add-payment.php';
    return ob_get_clean();
}