Пример #1
0
/**
 * Setup current user object and customer ID as well as cart.
 *
 * @uses  do_action() Calls 'wpsc_setup_customer' after customer data is ready
 * @access private
 * @since  3.8.13
 * @return int visitor id
 *
 */
function _wpsc_action_setup_customer()
{
    /////////////////////////////////////////////////////////////////////////
    // Setting up the customer happens after WPEC is initialized AND after
    // WordPress has loaded.  The reason for this is that the conditional
    // query tags are checked to see if the request is a 404 or a feed or
    // some other request that should not create a visitor profile.  The
    // conditional query tags are not available until after the
    // posts_selection hook is processed.  The 'wp' action is fired after
    // the 'posts_selection' hook.
    /////////////////////////////////////////////////////////////////////////
    if (!did_action('init')) {
        _wpsc_doing_it_wrong(__FUNCTION__, __('Customer cannot be reliably setup until at least the "init" hook as been fired during AJAX processing.', 'wpsc'), '3.8.14');
    }
    // if the customer cookie is invalid, unset it
    $visitor_id_from_cookie = _wpsc_validate_customer_cookie();
    if ($visitor_id_from_cookie && is_user_logged_in()) {
        $id_from_wp_user = get_user_meta(get_current_user_id(), _wpsc_get_visitor_meta_key('visitor_id'), true);
        if (empty($id_from_wp_user)) {
            _wpsc_update_wp_user_visitor_id(get_current_user_id(), $visitor_id_from_cookie);
        } elseif ($visitor_id_from_cookie != $id_from_wp_user) {
            // save the old visitor id so the merge cart function can do its work
            wpsc_update_customer_meta('merge_cart_vistor_id', $visitor_id_from_cookie);
            // make the current customer cookie match the cookie that is in the WordPress user meta
            _wpsc_create_customer_id_cookie($id_from_wp_user);
            // merging cart requires the taxonomies to have been initialized
            if (did_action('wpsc_register_taxonomies_after')) {
                _wpsc_merge_cart();
            } else {
                add_action('wpsc_register_taxonomies_after', '_wpsc_merge_cart', 1);
            }
        }
    }
    // initialize customer ID if it's not already there
    $visitor_id = wpsc_get_current_customer_id();
    // if there wasn't a visitor id in the cookies we set it now
    if ($visitor_id && empty($visitor_id_from_cookie) && is_user_logged_in()) {
        _wpsc_create_customer_id_cookie($visitor_id);
    }
    // setup the cart and restore its items
    wpsc_core_setup_cart();
    do_action('wpsc_setup_customer', $visitor_id);
}
/**
 * Creates a WPEC visitor
 *
 * @since 3.8.14
 * @access public
 * @param array (optional) visitor attributes to use when creating new visitor
 * @return int | boolean visitor id or false on failure
 */
function wpsc_create_visitor($args = null)
{
    global $wpdb;
    if (!_wpsc_visitor_database_ready()) {
        return false;
    }
    $new_visitor_id = false;
    // set user id
    if (!is_array($args) || empty($args)) {
        $args = array('user_id' => null);
    }
    // set last active time
    if (!isset($args['last_active'])) {
        $args['last_active'] = date('Y-m-d H:i:s');
    }
    // set created time
    if (!isset($args['created'])) {
        $args['created'] = date('Y-m-d H:i:s');
    }
    // new visitor profiles expire in two hours
    if (!isset($args['user_id']) && !isset($args['expires'])) {
        $args['expires'] = $timestamp = date('Y-m-d H:i:s', time() + 2 * HOUR_IN_SECONDS);
    }
    // visitor profiles associated with wordpress user never expire
    if (isset($args['user_id']) && isset($args['expires'])) {
        unset($args['expires']);
    }
    // create a visitor record and get the row id
    $result = $wpdb->insert($wpdb->wpsc_visitors, $args);
    if ($result !== false) {
        $new_visitor_id = $wpdb->insert_id;
        // create a security id, we store this in meta because meta has caching courtesy of wordpress!
        $security_id = '_' . wp_generate_password(12, false, false);
        wpsc_update_visitor_meta($new_visitor_id, _wpsc_get_visitor_meta_key('key'), $security_id);
    }
    if (isset($args['user_id']) && is_numeric($args['user_id']) && $args['user_id'] != 0) {
        $wp_user_id = intval($args['user_id']);
        _wpsc_update_wp_user_visitor_id($wp_user_id, $new_visitor_id);
    }
    do_action('wpsc_created_visitor', $new_visitor_id, $args);
    return $new_visitor_id;
}