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); }
public function processMainContentShortcode($post_id, $tpl_html, $item) { // use latest post_content from product $post = get_post($item['post_id']); $item['post_content'] = $post->post_content; // handle variations $variations_html = ''; if (ProductWrapper::hasVariations($item['post_id'])) { // generate variations table $variations_html = $this->getVariationsHTML($item); // add variations table to item description if (isset($item['profile_data']['details']['add_variations_table']) && $item['profile_data']['details']['add_variations_table']) { $item['post_content'] .= $variations_html; } } // replace shortcodes $tpl_html = str_replace('[[product_variations]]', $variations_html, $tpl_html); // handle split variations - get description from parent post_id if (ProductWrapper::isSingleVariation($post_id)) { $post = get_post($item['parent_id']); $item['post_content'] = $post->post_content; } // handle addons // generate addons table $addons_html = $this->getAddonsHTML($item); // add addons table to item description if (isset($item['profile_data']['details']['add_variations_table']) && $item['profile_data']['details']['add_variations_table']) { $item['post_content'] .= $addons_html; } // replace shortcodes $tpl_html = str_replace('[[product_addons]]', $addons_html, $tpl_html); // remove ALL links from post content by default if ('default' == get_option('wplister_remove_links', 'default')) { /* $item['post_content'] = preg_replace('#<a.*?>([^<]*)</a>#i', '$1', $item['post_content'] ); */ // regex improved to work in cases like <a ...><b>text</b></a> /* $item['post_content'] = preg_replace('#<a.*?>(.*)</a>#iU', '$1', $item['post_content'] ); */ // improved for multiple links per line case $item['post_content'] = preg_replace('#<a.*?>(.*?)</a>#i', ' $1 ', $item['post_content']); } // fixed whitespace pasted from ms word // details: http://stackoverflow.com/questions/1431034/can-anyone-tell-me-what-this-ascii-character-is $whitespace = chr(194) . chr(160); $item['post_content'] = str_replace($whitespace, ' ', $item['post_content']); // process and insert main content if ('off' == get_option('wplister_process_shortcodes', 'content')) { // off - do nothing, except wpautop() for proper paragraphs $tpl_html = str_replace('[[product_content]]', wpautop($item['post_content']), $tpl_html); } elseif ('remove' == get_option('wplister_process_shortcodes', 'content')) { // remove - remove all shortcodes from product description $post_content = $item['post_content']; // find and remove all placeholders if (preg_match_all('/\\[([^\\]]+)\\]/', $post_content, $matches)) { foreach ($matches[0] as $placeholder) { $post_content = str_replace($placeholder, '', $post_content); } } // insert content into template html $tpl_html = str_replace('[[product_content]]', wpautop($post_content), $tpl_html); } else { // make sure, WooCommerce template functions are loaded (WC2.2) if (!function_exists('woocommerce_product_loop_start') && version_compare(WC_VERSION, '2.2', '>=')) { // WC()->include_template_functions(); // won't work unless is_admin() == true include_once dirname(WC_PLUGIN_FILE) . '/includes/wc-template-functions.php'; } // default - apply the_content filter to make description look the same as in WP $tpl_html = str_replace('[[product_content]]', apply_filters('the_content', $item['post_content']), $tpl_html); } return $tpl_html; }
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; }