Example #1
0
/**
 * Add new entity element from anywhere
 */
function ajax_wpshop_add_entity()
{
    global $wpdb;
    check_ajax_referer('wpshop_add_new_entity_ajax_nonce', 'wpshop_ajax_nonce');
    $attributes = array();
    /** Get the attribute to create	*/
    $attribute_to_reload = null;
    if (!empty($_POST['attribute']['new_value_creation']) && is_array($_POST['attribute']['new_value_creation'])) {
        foreach ($_POST['attribute']['new_value_creation'] as $attribute_code => $value) {
            $query = $wpdb->prepare('SELECT * FROM ' . WPSHOP_DBT_ATTRIBUTE . ' WHERE code = %s', $attribute_code);
            $attribute_def = $wpdb->get_row($query);
            if ($value != "") {
                if ($attribute_def->data_type_to_use == 'internal') {
                    $attribute_default_value = unserialize($attribute_def->default_value);
                    if ($attribute_default_value['default_value'] == WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS) {
                        $user_id = wp_create_user(sanitize_user($value), wp_generate_password(12, false));
                        $query = $wpdb->prepare("SELECT ID FROM " . $wpdb->posts . " WHERE post_type = %s AND post_author = %d", WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS, $user_id);
                        $attribute_option_id = $wpdb->get_var($query);
                    } else {
                        $entity_args = array('post_type' => $attribute_default_value['default_value'], 'post_title' => $value, 'post_author' => function_exists('is_user_logged_in') && is_user_logged_in() ? get_current_user_id() : 'NaN', 'comment_status' => 'closed');
                        $attribute_option_id = wp_insert_post($entity_args);
                    }
                } else {
                    $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'position' => 1, 'attribute_id' => $attribute_def->id, 'value' => $value, 'label' => $value));
                    $attribute_option_id = $wpdb->insert_id;
                }
                foreach ($_POST['attribute'] as $attribute => $val) {
                    foreach ($val as $k => $v) {
                        if ($k == $attribute_code) {
                            $_POST['attribute'][$attribute][$k] = $attribute_option_id;
                        }
                    }
                }
            }
        }
    }
    /** Store send attribute into a new array for save purpose	*/
    if (is_array($_POST['attribute'])) {
        foreach ($_POST['attribute'] as $attribute_type => $attribute) {
            foreach ($attribute as $attribute_code => $attribute_value) {
                if (!isset($attributes[$attribute_code])) {
                    $attributes[$attribute_code] = $attribute_value;
                }
            }
        }
    }
    /** Save the new entity into database */
    $result = wpshop_entities::create_new_entity($_POST['entity_type'], $_POST['wp_fields']['post_title'], '', $attributes, array('attribute_set_id' => $_POST['attribute_set_id']));
    $new_entity_id = $result[1];
    if (!empty($new_entity_id)) {
        /**	Save address for current entity	*/
        if (!empty($_POST['type_of_form']) && !empty($_POST['attribute'][$_POST['type_of_form']])) {
            global $wpshop_account;
            $result = wps_address::wps_address($_POST['type_of_form']);
            update_post_meta($new_entity_id, '_wpshop_attached_address', $result['current_id']);
        }
        /** Make price calculation if entity is a product	*/
        if ($_POST['entity_type'] == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) {
            $wpshop_prices_attribute = unserialize(WPSHOP_ATTRIBUTE_PRICES);
            $calculate_price = false;
            foreach ($wpshop_prices_attribute as $attribute_price_code) {
                if (array_key_exists($attribute_price_code, $attributes)) {
                    $calculate_price = true;
                }
            }
            if ($calculate_price) {
                wpshop_products::calculate_price($new_entity_id);
            }
        }
        /** Add picture if a file has been send	*/
        if (!empty($_FILES)) {
            $wp_upload_dir = wp_upload_dir();
            $final_dir = $wp_upload_dir['path'] . '/';
            if (!is_dir($final_dir)) {
                mkdir($final_dir, 0755, true);
            }
            foreach ($_FILES as $file) {
                $tmp_name = $file['tmp_name']['post_thumbnail'];
                $name = $file['name']['post_thumbnail'];
                $filename = $final_dir . $name;
                @move_uploaded_file($tmp_name, $filename);
                $wp_filetype = wp_check_filetype(basename($filename), null);
                $attachment = array('guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path($filename), 'post_mime_type' => $wp_filetype['type'], 'post_title' => preg_replace('/\\.[^.]+$/', '', basename($filename)), 'post_content' => '', 'post_status' => 'inherit');
                $attach_id = wp_insert_attachment($attachment, $filename, $new_entity_id);
                require_once ABSPATH . 'wp-admin/includes/image.php';
                $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
                wp_update_attachment_metadata($attach_id, $attach_data);
                add_post_meta($new_entity_id, '_thumbnail_id', $attach_id, true);
            }
        }
        echo json_encode(array(true, __('Element has been saved', 'wpshop'), $attribute_to_reload, $new_entity_id));
    } else {
        echo json_encode(array(false, __('An error occured while adding your element', 'wpshop')));
    }
    die;
}
 /**
  * Add a product into the db. This function is used for the EDI
  * @param $name Name of the product
  * @param $description Description of the product
  * @param $attrs List of the attributes and values of the product
  * @return boolean
  */
 function addProduct($name, $description, $attrs = array())
 {
     $new_product = wpshop_entities::create_new_entity(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, $name, $description, $attrs);
     return $new_product[0];
 }