Example #1
0
/**
 * Function and action for duplicating products,
 * Refactored for 3.8
 * Purposely not duplicating stick post status (logically, products are most often duplicated because they share many attributes, where products are generally 'featured' uniquely.)
 */
function wpsc_duplicate_product()
{
    if (!wpsc_is_store_admin()) {
        return;
    }
    // Get the original post
    $id = absint($_GET['product']);
    $post = get_post($id);
    // Copy the post and insert it
    if (isset($post) && $post != null) {
        $duplicate = new WPSC_Duplicate_Product($post->ID);
        $new_id = $duplicate->duplicate_product_process();
        $duplicated = true;
        $sendback = wp_get_referer();
        $sendback = add_query_arg('duplicated', (int) $duplicated, $sendback);
        wp_redirect(esc_url_raw($sendback));
        exit;
    } else {
        wp_die(__('Sorry, for some reason, we couldn\'t duplicate this product because it could not be found in the database, check there for this ID: ', 'wp-e-commerce') . $id);
    }
}
/**
 * Duplicates a product image.
 *
 * Uses a portion of code from media_sideload_image() in `wp-admin/includes/media.php`
 * to check file before downloading from URL.
 *
 * @since 3.9.0
 *
 * @uses  WPSC_Duplicate_Product  Duplicate product class.
 *
 * @param   object    $post           The post object.
 * @param   bool      $new_parent_id  The parent post id.
 * @return  int|bool                  Attachment ID or false.
 *
 * @deprecated  since 4.0
 */
function wpsc_duplicate_product_image_process($child_post, $new_parent_id)
{
    _wpsc_deprecated_function(__FUNCTION__, '4.0', 'WPSC_Duplicate_Product->duplicate_product_image_process()');
    $duplicate = new WPSC_Duplicate_Product($child_post->ID, $new_parent_id);
    $duplicate->duplicate_product_image_process();
}
 /**
  * Duplicates product children and meta
  *
  * @since  4.0
  *
  * @uses  get_posts()  Gets an array of posts given array of arguments.
  *
  * @return  array  Array mapping old child IDs to duplicated child IDs.
  */
 private function duplicate_children()
 {
     $new_parent_id = $this->get_new_post_id();
     // Map duplicate child IDs
     $converted_child_ids = array();
     if ($new_parent_id) {
         // Get children products and duplicate them
         $child_posts = get_posts(array('post_parent' => $this->get_post_id(), 'post_type' => 'any', 'post_status' => 'any', 'numberposts' => -1, 'order' => 'ASC'));
         // Duplicate product images and child posts
         foreach ($child_posts as $child_post) {
             $duplicate_child = new WPSC_Duplicate_Product($child_post->ID, $new_parent_id);
             // Duplicate image or post
             if ('attachment' == get_post_type($child_post)) {
                 $new_child_id = $duplicate_child->duplicate_product_image_process();
             } else {
                 $new_child_id = $duplicate_child->duplicate_product_process();
             }
             // Map child ID to new child ID
             if ($new_child_id && !is_wp_error($new_child_id)) {
                 $converted_child_ids[$child_post->ID] = $new_child_id;
             }
             do_action('wpsc_duplicate_product_child', $child_post, $new_parent_id, $new_child_id);
         }
     }
     return $converted_child_ids;
 }