Esempio n. 1
0
 function collect_updated_products($post_id, $post)
 {
     WPLE()->logger->info("CSV: collect_updated_products( {$post_id} )");
     if (!$_POST) {
         return $post_id;
     }
     // if ( is_int( wp_is_post_revision( $post_id ) ) ) return;
     // if( is_int( wp_is_post_autosave( $post_id ) ) ) return;
     // if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
     if (!current_user_can('edit_post', $post_id)) {
         return $post_id;
     }
     if (!in_array($post->post_type, array('product', 'product_variation'))) {
         return $post_id;
     }
     // if this is a single variation use parent_id
     // if ( $parent_id = ProductWrapper::getVariationParent( $post_id ) ) {
     if ($post->post_type == 'product_variation') {
         $parent_id = ProductWrapper::getVariationParent($post_id);
         // WPLE()->logger->info("single variation found - use parent $parent_id for $post_id");
         $post_id = $parent_id;
     }
     // get queue
     $collected_products = get_option('wplister_updated_products_queue', array());
     if (!is_array($collected_products)) {
         $collected_products = array();
     }
     // add product_id to queue - if it doesn't exist
     if (!in_array($post_id, $collected_products)) {
         $collected_products[] = $post_id;
     }
     // WPLE()->logger->info("collected products: ".print_r($collected_products,1));
     // update queue
     update_option('wplister_updated_products_queue', $collected_products);
 }
Esempio n. 2
0
 public function applyProfileToItem($profile, $item, $update_title = true)
 {
     global $wpdb;
     // get item data
     $id = $item['id'];
     $post_id = $item['post_id'];
     $status = WPLE_ListingQueryHelper::getStatus($id);
     $ebay_id = self::getEbayIDFromID($id);
     $post_title = get_the_title($item['post_id']);
     WPLE()->logger->info("applyProfileToItem() - listing_id: {$id} / post_id: {$post_id}");
     // WPLE()->logger->callStack( debug_backtrace() );
     // skip ended auctions - or not, if you want to relist them...
     // if ( $status == 'ended' ) return;
     // use parent title for single (split) variation
     if (ProductWrapper::isSingleVariation($post_id)) {
         $parent_id = ProductWrapper::getVariationParent($post_id);
         $post_title = get_the_title($parent_id);
         // check if parent product has a custom eBay title set
         if (get_post_meta($parent_id, '_ebay_title', true)) {
             $post_title = trim(get_post_meta($parent_id, '_ebay_title', true));
         }
         // get variations
         $variations = ProductWrapper::getVariations($parent_id);
         // find this variation in all variations of this parent
         foreach ($variations as $var) {
             if ($var['post_id'] == $post_id) {
                 // append attribute values to title
                 $post_title = self::processSingleVariationTitle($post_title, $var['variation_attributes']);
             }
         }
     }
     // gather profile data
     $data = array();
     $data['profile_id'] = $profile['profile_id'];
     $data['account_id'] = $profile['account_id'];
     $data['site_id'] = $profile['site_id'];
     $data['auction_type'] = $profile['type'];
     $data['listing_duration'] = $profile['listing_duration'];
     $data['template'] = $profile['details']['template'];
     $data['quantity'] = $profile['details']['quantity'];
     $data['date_created'] = date('Y-m-d H:i:s');
     $data['profile_data'] = self::encodeObject($profile);
     // echo "<pre>";print_r($data);echo"</pre>";die();
     // add prefix and suffix to product title
     if ($update_title) {
         // append space to prefix, prepend space to suffix
         // TODO: make this an option
         $title_prefix = trim($profile['details']['title_prefix']) . ' ';
         $title_suffix = ' ' . trim($profile['details']['title_suffix']);
         // custom post meta fields override profile values
         if (get_post_meta($post_id, 'ebay_title_prefix', true)) {
             $title_prefix = trim(get_post_meta($post_id, 'ebay_title_prefix', true)) . ' ';
         }
         if (get_post_meta($post_id, 'ebay_title_suffix', true)) {
             $title_prefix = trim(get_post_meta($post_id, 'ebay_title_suffix', true)) . ' ';
         }
         $data['auction_title'] = trim($title_prefix . $post_title . $title_suffix);
         // custom post meta title override
         if (get_post_meta($post_id, '_ebay_title', true)) {
             $data['auction_title'] = trim(get_post_meta($post_id, '_ebay_title', true));
         } elseif (get_post_meta($post_id, 'ebay_title', true)) {
             $data['auction_title'] = trim(get_post_meta($post_id, 'ebay_title', true));
         }
         // process attribute shortcodes in title - like [[attribute_Brand]]
         if (strpos($data['auction_title'], ']]') > 0) {
             $templatesModel = new TemplatesModel();
             WPLE()->logger->info('auction_title before processing: ' . $data['auction_title'] . '');
             $data['auction_title'] = $templatesModel->processAllTextShortcodes($item['post_id'], $data['auction_title'], 80);
         }
         WPLE()->logger->info('auction_title after processing : ' . $data['auction_title'] . '');
         // trim title to 255 characters - longer titles will break $wpdb->update()
         if (strlen($data['auction_title']) > 255) {
             $data['auction_title'] = self::mb_substr($data['auction_title'], 0, 80);
             // eBay titles can not be longer than 80 characters
         }
     }
     // apply profile price
     $data['price'] = ProductWrapper::getPrice($post_id);
     $data['price'] = self::applyProfilePrice($data['price'], $profile['details']['start_price']);
     // fetch product stock if no quantity set in profile - and apply max_quantity limit
     if (intval($data['quantity']) == 0) {
         $max = isset($profile['details']['max_quantity']) && intval($profile['details']['max_quantity']) > 0 ? $profile['details']['max_quantity'] : PHP_INT_MAX;
         $data['quantity'] = min($max, intval(ProductWrapper::getStock($post_id)));
         // update listing quantity properly - using setListingQuantity() which regards current quantity_sold
         self::setListingQuantity($post_id, $data['quantity']);
         unset($data['quantity']);
     }
     // default new status is 'prepared'
     $data['status'] = 'prepared';
     // except for already published items where it is 'changed'
     if (intval($ebay_id) > 0) {
         $data['status'] = 'changed';
     }
     // ended items stay 'ended' and sold items stay sold
     if ($status == 'ended') {
         $data['status'] = 'ended';
     }
     if ($status == 'sold') {
         $data['status'] = 'sold';
     }
     if ($status == 'archived') {
         $data['status'] = 'archived';
     }
     // locked items simply keep their status
     if (@$item['locked']) {
         $data['status'] = $status;
     }
     // but if apply_changes_to_all_locked checkbox is ticked, even locked published items will be marked as 'changed'
     if (@$item['locked'] && $status == 'published' && isset($_POST['wpl_e2e_apply_changes_to_all_locked'])) {
         $data['status'] = 'changed';
     }
     // except for selected items which shouldn't be locked in the first place
     if ($status == 'selected') {
         $data['status'] = 'prepared';
     }
     // and reselected items which have already been 'ended'
     if ($status == 'reselected') {
         $data['status'] = 'ended';
     }
     // and items which have already been 'changed' and now had a new profile applied
     if ($status == 'changed_profile') {
         $data['status'] = 'changed';
     }
     // debug
     if ($status != $data['status']) {
         WPLE()->logger->info('applyProfileToItem(' . $id . ') old status: ' . $status);
         WPLE()->logger->info('applyProfileToItem(' . $id . ') new status: ' . $data['status']);
     }
     // update auctions table
     $wpdb->update($this->tablename, $data, array('id' => $id));
     // WPLE()->logger->info('updating listing ID '.$id);
     // WPLE()->logger->info('data: '.print_r($data,1));
     // WPLE()->logger->info('sql: '.$wpdb->last_query);
     // WPLE()->logger->info('error: '.$wpdb->last_error);
 }
Esempio n. 3
0
 public function processAttributeShortcodes($post_id, $tpl_html, $max_length = false)
 {
     // check for attribute shortcodes
     if (preg_match_all("/\\[\\[attribute_(.*)\\]\\]/uUsm", $tpl_html, $matches)) {
         // attribute shortcodes i.e. [[attribute_Brand]]
         $product_attributes = ProductWrapper::getAttributes($post_id);
         WPLE()->logger->debug('processAttributeShortcodes() - product_attributes: ' . print_r($product_attributes, 1));
         // parent attribute for split child variations
         $parent_post_id = ProductWrapper::getVariationParent($post_id);
         $parent_attributes = $parent_post_id ? ProductWrapper::getAttributes($parent_post_id) : array();
         WPLE()->logger->debug('processAttributeShortcodes() - parent_attributes: ' . print_r($parent_attributes, 1));
         // process each found shortcode
         foreach ($matches[1] as $attribute) {
             // check product and parent attributes
             if (isset($product_attributes[$attribute])) {
                 $attribute_value = $product_attributes[$attribute];
             } elseif (isset($parent_attributes[$attribute])) {
                 $attribute_value = $parent_attributes[$attribute];
             } else {
                 $attribute_value = '';
             }
             // format multiple attribute values
             $attribute_value = str_replace('|', '<br/>', $attribute_value);
             // replace placeholder
             $processed_html = str_replace('[[attribute_' . $attribute . ']]', $attribute_value, $tpl_html);
             // check if string exceeds max_length after processing shortcode
             if ($max_length && $this->mb_strlen($processed_html) > $max_length) {
                 $attribute_value = '';
                 $processed_html = str_replace('[[attribute_' . $attribute . ']]', $attribute_value, $tpl_html);
             }
             $tpl_html = $processed_html;
         }
     }
     return $tpl_html;
 }
Esempio n. 4
0
 public function getProductImagesURL($id)
 {
     global $wpdb;
     $results = $wpdb->get_results($wpdb->prepare(" \n\t\t\tSELECT id, guid \n\t\t\tFROM {$wpdb->prefix}posts\n\t\t\tWHERE post_type = 'attachment' \n\t\t\t  AND post_parent = %s\n\t\t\tORDER BY menu_order\n\t\t", $id));
     WPLE()->logger->debug("getProductImagesURL( {$id} ) : " . print_r($results, 1));
     #echo "<pre>";print_r($results);echo"</pre>";#die();
     // fetch images using default size
     $size = get_option('wplister_default_image_size', 'full');
     $images = array();
     foreach ($results as $row) {
         // $url = wp_get_attachment_url( $row->id, $size );
         $url = $row->guid ? $row->guid : wp_get_attachment_url($row->id, $size);
         $images[] = $url;
     }
     // support for WooCommerce 2.0 Product Gallery
     if (get_option('wplister_wc2_gallery_fallback', 'none') == 'none') {
         $images = array();
     }
     // discard images if fallback is disabled
     $product_image_gallery = get_post_meta($id, '_product_image_gallery', true);
     // use parent product for single (split) variation
     if (ProductWrapper::isSingleVariation($id)) {
         $parent_id = ProductWrapper::getVariationParent($id);
         $product_image_gallery = get_post_meta($parent_id, '_product_image_gallery', true);
     }
     if ($product_image_gallery) {
         // build clean array with main image as first item
         $images = array();
         $images[] = $this->getProductMainImageURL($id);
         $image_ids = explode(',', $product_image_gallery);
         foreach ($image_ids as $image_id) {
             $url = wp_get_attachment_url($image_id, $size);
             if ($url && !in_array($url, $images)) {
                 $images[] = $url;
             }
         }
         WPLE()->logger->info("found WC2 product gallery images for product #{$id} " . print_r($images, 1));
     }
     $product_images = array();
     foreach ($images as $imageurl) {
         $product_images[] = $this->removeHttpsFromUrl($imageurl);
     }
     // call wplister_product_images filter
     // hook into this from your WP theme's functions.php - this won't work in listing templates!
     $product_images = apply_filters('wplister_product_images', $product_images, $id);
     return $product_images;
 }