/** * Create a new element for a entity type into database * @param string $entity_type The type of element to create * @param string $name The element name to create * @param string $description A description for the element to create * @param array $attributes A list containing all attributes defining the element to create * @param array $extra_params A list of extra parameters for the element creation * @return array The new entity identifier AND the status of attribute save with a messaege in case the save action failed */ public static function create_new_entity($entity_type, $name, $description, $attributes = array(), $extra_params = array()) { global $wpdb; /** Check if user is already connected */ $user_id = function_exists('is_user_logged_in') && is_user_logged_in() ? get_current_user_id() : 'NaN'; /** The arguments needed for a entity (post) creation */ $entity_args = array('post_type' => $entity_type, 'post_title' => $name, 'post_status' => 'publish', 'post_excerpt' => $description, 'post_content' => $description, 'post_author' => $user_id, 'comment_status' => 'closed'); /** Add the new product */ $entity_id = wp_insert_post($entity_args); do_action('wps_entity_more_action', $entity_id, $attributes); /** Update the attribute set id for the current product */ if (!empty($extra_params['attribute_set_id'])) { $attribute_set_id = $extra_params['attribute_set_id']; } else { $query = $wpdb->prepare("SELECT id FROM " . WPSHOP_DBT_ATTRIBUTE_SET . " WHERE status = %s AND entity_id = %d AND default_set = %s", 'valid', wpshop_entities::get_entity_identifier_from_code($entity_type), 'yes'); $attribute_set_id = $wpdb->get_var($query); } update_post_meta($entity_id, '_' . $entity_type . '_attribute_set_id', $attribute_set_id); $response = wpshop_attributes::setAttributesValuesForItem($entity_id, $attributes, true); return array($response, $entity_id); }
/** Treat the differents fields of form and classified them by form * @return boolean */ function save_address_infos($attribute_set_id) { global $wpdb; $current_item_edited = !empty($_POST['attribute'][$attribute_set_id]['item_id']) ? (int) wpshop_tools::varSanitizer($_POST['attribute'][$attribute_set_id]['item_id']) : null; // Create or update the post address $post_parent = ''; $post_author = get_current_user_id(); if (!empty($_REQUEST['user']['customer_id'])) { $post_parent = $_REQUEST['user']['customer_id']; $post_author = $_REQUEST['user']['customer_id']; } elseif (!empty($_REQUEST['post_ID'])) { $post_parent = $_REQUEST['post_ID']; } else { $post_parent = get_current_user_id(); } $post_address = array('post_author' => $post_author, 'post_title' => !empty($_POST['attribute'][$attribute_set_id]['varchar']['address_title']) ? $_POST['attribute'][$attribute_set_id]['varchar']['address_title'] : '', 'post_status' => 'draft', 'post_name' => WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS, 'post_parent' => $post_parent); $_POST['edit_other_thing'] = true; if (empty($current_item_edited) && (empty($_POST['current_attribute_set_id']) || $_POST['current_attribute_set_id'] != $attribute_set_id)) { $current_item_edited = wp_insert_post($post_address); if (is_admin()) { $_POST['attribute'][$attribute_set_id]['item_id'] = $current_item_edited; } } else { $post_address['ID'] = $current_item_edited; wp_update_post($post_address); } //Update the post_meta of address update_post_meta($current_item_edited, WPSHOP_ADDRESS_ATTRIBUTE_SET_ID_META_KEY, $attribute_set_id); foreach ($_POST['attribute'][$attribute_set_id] as $type => $type_content) { $attribute_not_to_do = array(); if (is_array($type_content)) { foreach ($type_content as $code => $value) { $attribute_def = wpshop_attributes::getElement($code, "'valid'", 'code'); if (!empty($attribute_def->_need_verification) && $attribute_def->_need_verification == 'yes') { $code_verif = $code . '2'; $attribute_not_to_do[] = $code_verif; if (!empty($attributes[$code_verif])) { unset($attributes[$code_verif]); } } if (!in_array($code, $attribute_not_to_do)) { $attributes[$code] = $value; } } } } $attributes = apply_filters('wps-address-coordinate-calculation', $attributes); $result = wpshop_attributes::setAttributesValuesForItem($current_item_edited, $attributes, false, ''); $result['current_id'] = $current_item_edited; if (!empty($result['current_id'])) { // Update $_SESSION[address type] $billing_option = get_option('wpshop_billing_address'); if (!empty($billing_option) && !empty($billing_option['choice']) && $billing_option['choice'] == $attribute_set_id) { $_SESSION['billing_address'] = $result['current_id']; } else { $_SESSION['shipping_address'] = $result['current_id']; } } return $result; }