/** * Handles saving updates from the product editor * * Saves all product related information which includes core product data * and supporting elements such as images, digital downloads, tags, * assigned categories, specs and pricing variations. * * @author Jonathan Davis * @since 1.0 * * @param Product $Product * @return void **/ public function save(ShoppProduct $Product) { check_admin_referer('shopp-save-product'); if (!current_user_can('shopp_products')) { wp_die(__('You do not have sufficient permissions to access this page.')); } ShoppSettings()->saveform(); // Save workflow setting $status = $Product->status; // Set publish date if ('publish' == $_POST['status']) { $publishing = isset($_POST['publish']) ? $_POST['publish'] : array(); $fields = array('month' => '', 'date' => '', 'year' => '', 'hour' => '', 'minute' => '', 'meridiem' => ''); $publishdate = join('', array_merge($fields, $publishing)); if (!empty($publishdate)) { $publish =& $_POST['publish']; if ($publish['meridiem'] == "PM" && $publish['hour'] < 12) { $publish['hour'] += 12; } $publish = mktime($publish['hour'], $publish['minute'], 0, $publish['month'], $publish['date'], $publish['year']); $Product->status = 'future'; unset($_POST['status']); } else { unset($_POST['publish']); // Auto set the publish date if not set (or more accurately, if set to an irrelevant timestamp) if ($Product->publish <= 86400) { $Product->publish = null; } } } else { unset($_POST['publish']); $Product->publish = 0; } // Set a unique product slug if (empty($Product->slug)) { $Product->slug = sanitize_title($_POST['name']); } $Product->slug = wp_unique_post_slug($Product->slug, $Product->id, $Product->status, ShoppProduct::posttype(), 0); $Product->featured = 'off'; if (isset($_POST['content'])) { $_POST['description'] = $_POST['content']; } $Product->updates($_POST, array('meta', 'categories', 'prices', 'tags')); do_action('shopp_pre_product_save'); $Product->save(); // Remove deleted images if (!empty($_POST['deleteImages'])) { $deletes = array(); if (strpos($_POST['deleteImages'], ",") !== false) { $deletes = explode(',', $_POST['deleteImages']); } else { $deletes = array($_POST['deleteImages']); } $Product->delete_images($deletes); } // Update image data if (!empty($_POST['images']) && is_array($_POST['images'])) { $Product->link_images($_POST['images']); $Product->save_imageorder($_POST['images']); if (!empty($_POST['imagedetails'])) { $Product->update_images($_POST['imagedetails']); } } // Update Prices if (!empty($_POST['price']) && is_array($_POST['price'])) { // Delete prices that were marked for removal if (!empty($_POST['deletePrices'])) { $deletes = array(); if (strpos($_POST['deletePrices'], ",")) { $deletes = explode(',', $_POST['deletePrices']); } else { $deletes = array($_POST['deletePrices']); } foreach ($deletes as $option) { $Price = new ShoppPrice($option); $Price->delete(); } } $Product->resum(); // Save prices that there are updates for foreach ($_POST['price'] as $i => $priceline) { if (empty($priceline['id'])) { $Price = new ShoppPrice(); $priceline['product'] = $Product->id; } else { $Price = new ShoppPrice($priceline['id']); } $priceline['sortorder'] = array_search($i, $_POST['sortorder']) + 1; $priceline['shipfee'] = Shopp::floatval($priceline['shipfee']); if (isset($priceline['recurring']['trialprice'])) { $priceline['recurring']['trialprice'] = Shopp::floatval($priceline['recurring']['trialprice']); } if ($Price->stock != $priceline['stocked']) { $priceline['stock'] = (int) $priceline['stocked']; do_action('shopp_stock_product', $priceline['stock'], $Price, $Price->stock, $Price->stocklevel); } else { unset($priceline['stocked']); } $Price->updates($priceline); $Price->save(); // Save 'price' meta records after saving the price record if (isset($priceline['dimensions']) && is_array($priceline['dimensions'])) { $priceline['dimensions'] = array_map(array('Shopp', 'floatval'), $priceline['dimensions']); } $settings = array('donation', 'recurring', 'membership', 'dimensions'); $priceline['settings'] = array(); foreach ($settings as $setting) { if (!isset($priceline[$setting])) { continue; } $priceline['settings'][$setting] = $priceline[$setting]; } if (!empty($priceline['settings'])) { shopp_set_meta($Price->id, 'price', 'settings', $priceline['settings']); } if (!empty($priceline['options'])) { shopp_set_meta($Price->id, 'price', 'options', $priceline['options']); } $Product->sumprice($Price); if (!empty($priceline['download'])) { $Price->attach_download($priceline['download']); } if (!empty($priceline['downloadpath'])) { // Attach file specified by URI/path if (!empty($Price->download->id) || empty($Price->download) && $Price->load_download()) { $File = $Price->download; } else { $File = new ProductDownload(); } $stored = false; $tmpfile = sanitize_path($priceline['downloadpath']); $File->storage = false; $Engine = $File->engine(); // Set engine from storage settings $File->parent = $Price->id; $File->context = "price"; $File->type = "download"; $File->name = !empty($priceline['downloadfile']) ? $priceline['downloadfile'] : basename($tmpfile); $File->filename = $File->name; if ($File->found($tmpfile)) { $File->uri = $tmpfile; $stored = true; } else { $stored = $File->store($tmpfile, 'file'); } if ($stored) { $File->readmeta(); $File->save(); } } // END attach file by path/uri } // END foreach() unset($Price); } // END if (!empty($_POST['price'])) $Product->load_sold($Product->id); // Refresh accurate product sales stats $Product->sumup(); // Update taxonomies after pricing summary is generated // Summary table entry is needed for ProductTaxonomy::recount() to // count properly based on aggregate product inventory, see #2968 foreach (get_object_taxonomies(Product::$posttype) as $taxonomy) { $tags = ''; $taxonomy_obj = get_taxonomy($taxonomy); if (isset($_POST['tax_input']) && isset($_POST['tax_input'][$taxonomy])) { $tags = $_POST['tax_input'][$taxonomy]; if (is_array($tags)) { // array = hierarchical, string = non-hierarchical. $tags = array_filter($tags); } } if (current_user_can($taxonomy_obj->cap->assign_terms)) { wp_set_post_terms($Product->id, $tags, $taxonomy); } } // Ensure taxonomy counts are updated on status changes, see #2968 if ($status != $_POST['status']) { $Post = new StdClass(); $Post->ID = $Product->id; $Post->post_type = ShoppProduct::$posttype; wp_transition_post_status($_POST['status'], $Product->status, $Post); } if (!empty($_POST['meta']['options'])) { $_POST['meta']['options'] = stripslashes_deep($_POST['meta']['options']); } else { $_POST['meta']['options'] = false; } // No variation options at all, delete all variation-pricelines if (!empty($Product->prices) && is_array($Product->prices) && (empty($_POST['meta']['options']['v']) || empty($_POST['meta']['options']['a']))) { foreach ($Product->prices as $priceline) { // Skip if not tied to variation options if ($priceline->optionkey == 0) { continue; } if (empty($_POST['meta']['options']['v']) && $priceline->context == "variation" || empty($_POST['meta']['options']['a']) && $priceline->context == "addon") { $Price = new ShoppPrice($priceline->id); $Price->delete(); } } } // Handle product spec/detail data if (!empty($_POST['details']) || !empty($_POST['deletedSpecs'])) { // Delete specs queued for removal $ids = array(); $deletes = array(); if (!empty($_POST['deletedSpecs'])) { if (strpos($_POST['deleteImages'], ",") !== false) { $deletes = explode(',', $_POST['deleteImages']); } else { $deletes = array($_POST['deletedSpecs']); } $ids = db::escape($_POST['deletedSpecs']); $Spec = new Spec(); db::query("DELETE FROM {$Spec->_table} WHERE id IN ({$ids})"); } if (is_array($_POST['details'])) { foreach ($_POST['details'] as $i => $spec) { if (in_array($spec['id'], $deletes)) { continue; } if (isset($spec['new'])) { $Spec = new Spec(); $spec['id'] = ''; $spec['parent'] = $Product->id; } else { $Spec = new Spec($spec['id']); } $spec['sortorder'] = array_search($i, $_POST['details-sortorder']) + 1; $Spec->updates($spec); $Spec->save(); } } } // Save any meta data if (isset($_POST['meta']) && is_array($_POST['meta'])) { foreach ($_POST['meta'] as $name => $value) { if (isset($Product->meta[$name])) { $Meta = $Product->meta[$name]; if (is_array($Meta)) { $Meta = reset($Product->meta[$name]); } } else { $Meta = new ShoppMetaObject(array('parent' => $Product->id, 'context' => 'product', 'type' => 'meta', 'name' => $name)); } $Meta->parent = $Product->id; $Meta->name = $name; $Meta->value = $value; $Meta->save(); } } $Product->load_data(); // Reload data so everything is fresh for shopp_product_saved do_action_ref_array('shopp_product_saved', array(&$Product)); unset($Product); }
/** * Creates a duplicate product of this product's data * * @author Jonathan Davis * @since 1.0 * @version 1.2 * * @return void **/ public function duplicate() { $original = $this->id; $this->load_data(); // Load everything $this->id = ''; $this->name = $this->name . ' ' . __('copy', 'Shopp'); $slug = sanitize_title_with_dashes($this->name); $this->slug = wp_unique_post_slug($slug, $this->id, $this->status, ShoppProduct::posttype(), 0); $this->created = ''; $this->modified = ''; $this->status = 'draft'; // Set duplicated product to draft status $this->save(); // Copy prices foreach ($this->prices as $price) { $Price = new ShoppPrice(); $Price->copydata($price); $Price->product = $this->id; $Price->save(); // Copy Price record meta entries $meta = array('donation', 'recurring', 'membership', 'dimensions'); $priceline['settings'] = array(); $settings = array(); foreach ($meta as $name) { if (isset($price->{$name})) { $settings[$name] = $price->{$name}; } } shopp_set_meta($Price->id, 'price', 'settings', $settings); shopp_set_meta($Price->id, 'price', 'options', $price->options); } // Copy taxonomy assignments $terms = array(); $taxonomies = get_object_taxonomies(self::$posttype); $assignments = wp_get_object_terms($original, $taxonomies, array('fields' => 'all_with_object_id')); foreach ($assignments as $term) { // Map WP taxonomy data to object meta if (!isset($term->term_id) || empty($term->term_id)) { continue; } // Skip invalid entries if (!isset($term->taxonomy) || empty($term->taxonomy)) { continue; } // Skip invalid entries if (!isset($terms[$term->taxonomy])) { $terms[$term->taxonomy] = array(); } $terms[$term->taxonomy][] = (int) $term->term_id; } foreach ($terms as $taxonomy => $termlist) { wp_set_object_terms($this->id, $termlist, $taxonomy); } $metadata = array('specs', 'images', 'settings', 'meta'); foreach ($metadata as $metaset) { if (!is_array($this->{$metaset})) { continue; } foreach ($this->{$metaset} as $metaobjects) { if (!is_array($metaobjects)) { $metaobjects = array($metaobjects); } foreach ($metaobjects as $meta) { $ObjectClass = get_class($meta); $Meta = new $ObjectClass(); $Meta->copydata($meta); $Meta->parent = $this->id; $Meta->save(); } } } // Duplicate summary (primarily for summary settings data) $Summary = new ProductSummary($original); $Summary->product = $this->id; $Summary->sold = $Summary->grossed = $Summary->stock = 0; $Summary->save(); // Re-summarize product pricing $this->load_data(array('prices', 'summary')); // Duplicate (WP) post meta data foreach (get_post_custom($original) as $key => $values) { foreach ((array) $values as $value) { add_post_meta($this->id, $key, $value); } } }
/** * Interface processor for the category editor * * @author Jonathan Davis * @since 1.0 * @return void **/ public function screen() { global $CategoryImages; $Shopp = Shopp::object(); if (!current_user_can('shopp_categories')) { wp_die(__('You do not have sufficient permissions to access this page.')); } $Category = $this->Model; $Price = new ShoppPrice(); $priceTypes = ShoppPrice::types(); $billPeriods = ShoppPrice::periods(); // Build permalink for slug editor $permalink = trailingslashit(Shopp::url()) . "category/"; $Category->slug = apply_filters('editable_slug', $Category->slug); $pricerange_menu = array("disabled" => __('Price ranges disabled', 'Shopp'), "auto" => __('Build price ranges automatically', 'Shopp'), "custom" => __('Use custom price ranges', 'Shopp')); $uploader = shopp_setting('uploader_pref'); if (!$uploader) { $uploader = 'flash'; } $workflows = array("continue" => __('Continue Editing', 'Shopp'), "close" => __('Categories Manager', 'Shopp'), "new" => __('New Category', 'Shopp'), "next" => __('Edit Next', 'Shopp'), "previous" => __('Edit Previous', 'Shopp')); do_action('add_meta_boxes', ProductCategory::$taxon, $Category); do_action('add_meta_boxes_' . ProductCategory::$taxon, $Category); do_action('do_meta_boxes', ProductCategory::$taxon, 'normal', $Category); do_action('do_meta_boxes', ProductCategory::$taxon, 'advanced', $Category); do_action('do_meta_boxes', ProductCategory::$taxon, 'side', $Category); include $this->ui('category.php'); }
/** * shopp_product_set_addon_options - Creates a complete set of addon product options on a specified product, by letting you * specify the set of options types, and corresponding options. This function will create new addon options in the database and * will attach them to the specified product. * * @api * @since 1.2 * * @param int $product (required) The product id of the product that you wish to add the addon options to. * @param array $options (Description...) A two dimensional array describing the addon options. * The outer array is keyed on the name of the option type (Framing, Matting, Glass, etc.) * The inner contains the corresponding option values. * Ex. $options = array( 'Framing' => array('Wood', 'Gold'), 'Glass' => array('Anti-glare', 'UV Protectant') ); * @return array addon Price objects that have been created on the product. * **/ function shopp_product_set_addon_options($product = false, $options = array(), $summary = 'save') { if (!$product || empty($options)) { shopp_debug(__FUNCTION__ . " failed: Missing required parameters."); return false; } $Product = new ShoppProduct($product); if (empty($Product->id)) { shopp_debug(__FUNCTION__ . " failed: Product not found for product id {$product}."); return false; } $Product->load_data(array('summary')); // clean up old variations $table = ShoppDatabaseObject::tablename(ShoppPrice::$table); db::query("DELETE FROM {$table} WHERE product={$product} AND context='addon'"); $prices = array(); $mapping = array(); foreach ($options as $type => $opts) { foreach ($opts as $index => $option) { $addon = array($type => $option); $Price = new ShoppPrice(); $Price->type = 'Shipped'; $Price->product = $product; $Price->context = 'addon'; $Price->sortorder = $index + 2; // default price sort order is 1, start at minimum 2 #2847 list($Price->optionkey, $Price->options, $Price->label, $mapping) = $Product->optionmap($addon, $options, 'addon'); $Price->save(); shopp_set_meta($Price->id, 'price', 'options', $Price->options); $prices[] = $Price; } } $metaopts = shopp_product_meta($product, 'options'); $metaopts['a'] = array(); $i = 1; foreach ($options as $optname => $option) { if (!isset($metaopts['a'][$i])) { $metaopts['a'][$i] = array('id' => $i, 'name' => $optname, 'options' => array()); } foreach ($option as $value) { $metaopts['a'][$i]['options'][$mapping[$optname][$value]] = array('id' => $mapping[$optname][$value], 'name' => $value, 'linked' => "off"); } $i++; } shopp_set_product_meta($product, 'options', $metaopts); $Product->addons = "on"; if ('save' == $summary) { $Product->sumup(); } return $prices; }
?> ', pluginuri = <?php echo json_encode(SHOPP_PLUGINURI); ?> , rule_groups = <?php echo json_encode($rulegroups); ?> , rule_types = <?php echo json_encode($ruletypes); ?> , bill_periods = <?php echo json_encode(ShoppPrice::periods()); ?> , rules = <?php echo json_encode($MemberPlan->stages); ?> , STAGE_LABEL = <?php _jse('Content Access Rules', 'Shopp'); ?> , STAGES_LABEL = <?php _jse('Step', 'Shopp'); ?> , DELETE_RULE_PROMPT = <?php