PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/wpsc-includes/product-template.php

https://github.com/AaronFernandes/aquestionof
PHP | 1821 lines | 1139 code | 268 blank | 414 comment | 354 complexity | b68b4389d726f013939c2811ac0267d4 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * WP eCommerce product functions and product utility function.
  4. *
  5. * This is the wpsc equivalent of post-template.php
  6. *
  7. * @package wp-e-commerce
  8. * @since 3.8
  9. * @subpackage wpsc-template-functions
  10. */
  11. function wpsc_has_pages_top(){
  12. if(wpsc_has_pages() && ((get_option('wpsc_page_number_position') == 1) || (get_option('wpsc_page_number_position') == 3)))
  13. return true;
  14. else
  15. return false;
  16. }
  17. function wpsc_has_pages_bottom(){
  18. if(wpsc_has_pages() && ((get_option('wpsc_page_number_position') == 2) || (get_option('wpsc_page_number_position') == 3)))
  19. return true;
  20. else
  21. return false;
  22. }
  23. /**
  24. * Used in wpsc_pagination to generate the links in pagination
  25. * @access public
  26. *
  27. * @since 3.8
  28. * @param $page int
  29. * @return $output string, url
  30. */
  31. function wpsc_a_page_url($page=null) {
  32. global $wp_query;
  33. $output = '';
  34. $curpage = $wp_query->query_vars['paged'];
  35. if($page != '')
  36. $wp_query->query_vars['paged'] = $page;
  37. if($wp_query->is_single === true) {
  38. $wp_query->query_vars['paged'] = $curpage;
  39. return wpsc_product_url($wp_query->post->ID);
  40. } else {
  41. if( 1 < $wp_query->query_vars['paged']) {
  42. if(get_option('permalink_structure'))
  43. $output .= "paged/{$wp_query->query_vars['paged']}/";
  44. else
  45. $output = add_query_arg('paged', '', $output);
  46. }
  47. return $output;
  48. }
  49. }
  50. /**
  51. * wpsc_pagination generates and returns the urls for pagination
  52. * @access public
  53. *
  54. * @since 3.8
  55. * @param $totalpages (INT) Number of pages,
  56. * @param $per_page (INT) Number of products per page
  57. * @param $current_page (INT) Current Product page number
  58. * @param $page_link (STRING) URL of Page
  59. *
  60. * @return
  61. */
  62. function wpsc_pagination($totalpages = '', $per_page = '', $current_page = '', $page_link = '') {
  63. global $wp_query;
  64. $num_paged_links = 4; //amount of links to show on either side of current page
  65. $additional_links = '';
  66. //additional links, items per page and products order
  67. if( get_option('permalink_structure') != '' ){
  68. $additional_links_separator = '?';
  69. }else{
  70. $additional_links_separator = '&';
  71. }
  72. if( !empty( $_GET['items_per_page'] ) ){
  73. $additional_links = $additional_links_separator . 'items_per_page=' . $_GET['items_per_page'];
  74. $additional_links_separator = '&';
  75. }
  76. if( !empty( $_GET['product_order'] ) )
  77. $additional_links .= $additional_links_separator . 'product_order=' . $_GET['product_order'];
  78. $additional_links = apply_filters('wpsc_pagination_additional_links', $additional_links);
  79. //end of additional links
  80. if(empty($totalpages)){
  81. $totalpages = $wp_query->max_num_pages;
  82. }
  83. if(empty($per_page))
  84. $per_page = (int)get_option('wpsc_products_per_page');
  85. $current_page = absint( get_query_var('paged') );
  86. if($current_page == 0)
  87. $current_page = 1;
  88. if(empty($page_link))
  89. $page_link = wpsc_a_page_url();
  90. //if there is no pagination
  91. if(!get_option('permalink_structure')) {
  92. $category = '?';
  93. if(isset($wp_query->query_vars['wpsc_product_category']))
  94. $category = '?wpsc_product_category='.$wp_query->query_vars['wpsc_product_category'];
  95. if(isset($wp_query->query_vars['wpsc_product_category']) && is_string($wp_query->query_vars['wpsc_product_category'])){
  96. $page_link = get_option('blogurl').$category.'&amp;paged';
  97. }else{
  98. $page_link = get_option('product_list_url').$category.'&amp;paged';
  99. }
  100. $separator = '=';
  101. }else{
  102. // This will need changing when we get product categories sorted
  103. if(isset($wp_query->query_vars['wpsc_product_category']))
  104. $page_link = trailingslashit(get_option('product_list_url')).$wp_query->query_vars['wpsc_product_category'].'/';
  105. else
  106. $page_link = trailingslashit(get_option('product_list_url'));
  107. $separator = 'page/';
  108. }
  109. // If there's only one page, return now and don't bother
  110. if($totalpages == 1)
  111. return;
  112. // Pagination Prefix
  113. $output = __('Pages: ','wpsc');
  114. if(get_option('permalink_structure')){
  115. // Should we show the FIRST PAGE link?
  116. if($current_page > 1)
  117. $output .= "<a href=\"". esc_url( $page_link . $additional_links ) . "\" title=\"" . __('First Page', 'wpsc') . "\">" . __('&laquo; First', 'wpsc') . "</a>";
  118. // Should we show the PREVIOUS PAGE link?
  119. if($current_page > 1) {
  120. $previous_page = $current_page - 1;
  121. if( $previous_page == 1 )
  122. $output .= " <a href=\"". esc_url( $page_link . $additional_links ) . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
  123. else
  124. $output .= " <a href=\"". esc_url( $page_link .$separator. $previous_page . $additional_links ) . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
  125. }
  126. $i =$current_page - $num_paged_links;
  127. $count = 1;
  128. if($i <= 0) $i =1;
  129. while($i < $current_page){
  130. if($count <= $num_paged_links){
  131. if($count == 1)
  132. $output .= " <a href=\"". esc_url( $page_link . $additional_links ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
  133. else
  134. $output .= " <a href=\"". esc_url( $page_link .$separator. $i . $additional_links ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
  135. }
  136. $i++;
  137. $count++;
  138. }
  139. // Current Page Number
  140. if($current_page > 0)
  141. $output .= "<span class='current'>$current_page</span>";
  142. //Links after Current Page
  143. $i = $current_page + $num_paged_links;
  144. $count = 1;
  145. if($current_page < $totalpages){
  146. while(($i) > $current_page){
  147. if($count < $num_paged_links && ($count+$current_page) <= $totalpages){
  148. $output .= " <a href=\"". esc_url( $page_link .$separator. ($count+$current_page) .$additional_links ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), ($count+$current_page) ) . "\">".($count+$current_page)."</a>";
  149. $i++;
  150. }else{
  151. break;
  152. }
  153. $count ++;
  154. }
  155. }
  156. if($current_page < $totalpages) {
  157. $next_page = $current_page + 1;
  158. $output .= "<a href=\"". esc_url( $page_link .$separator. $next_page . $additional_links ) . "\" title=\"" . __('Next Page', 'wpsc') . "\">" . __('Next &gt;', 'wpsc') . "</a>";
  159. }
  160. // Should we show the LAST PAGE link?
  161. if($current_page < $totalpages) {
  162. $output .= "<a href=\"". esc_url( $page_link .$separator. $totalpages . $additional_links ) . "\" title=\"" . __('Last Page', 'wpsc') . "\">" . __('Last &raquo;', 'wpsc') . "</a>";
  163. }
  164. } else {
  165. // Should we show the FIRST PAGE link?
  166. if($current_page > 1)
  167. $output .= "<a href=\"". remove_query_arg('paged' ) . "\" title=\"" . __('First Page', 'wpsc') . "\">" . __('&laquo; First', 'wpsc') . "</a>";
  168. // Should we show the PREVIOUS PAGE link?
  169. if($current_page > 1) {
  170. $previous_page = $current_page - 1;
  171. if( $previous_page == 1 )
  172. $output .= " <a href=\"". remove_query_arg( 'paged' ) . $additional_links . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
  173. else
  174. $output .= " <a href=\"". add_query_arg( 'paged', ($current_page - 1) ) . $additional_links . "\" title=\"" . __('Previous Page', 'wpsc') . "\">" . __('&lt; Previous', 'wpsc') . "</a>";
  175. }
  176. $i =$current_page - $num_paged_links;
  177. $count = 1;
  178. if($i <= 0) $i =1;
  179. while($i < $current_page){
  180. if($count <= $num_paged_links){
  181. if($i == 1)
  182. $output .= " <a href=\"". remove_query_arg('paged' ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
  183. else
  184. $output .= " <a href=\"". add_query_arg('paged', $i ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), $i ) . " \">".$i."</a>";
  185. }
  186. $i++;
  187. $count++;
  188. }
  189. // Current Page Number
  190. if($current_page > 0)
  191. $output .= "<span class='current'>$current_page</span>";
  192. //Links after Current Page
  193. $i = $current_page + $num_paged_links;
  194. $count = 1;
  195. if($current_page < $totalpages){
  196. while(($i) > $current_page){
  197. if($count < $num_paged_links && ($count+$current_page) <= $totalpages){
  198. $output .= " <a href=\"". add_query_arg( 'paged', ($count+$current_page) ) . "\" title=\"" . sprintf( __('Page %s', 'wpsc'), ($count+$current_page) ) . "\">".($count+$current_page)."</a>";
  199. $i++;
  200. }else{
  201. break;
  202. }
  203. $count ++;
  204. }
  205. }
  206. if($current_page < $totalpages) {
  207. $next_page = $current_page + 1;
  208. $output .= "<a href=\"". add_query_arg( 'paged', $next_page ) . "\" title=\"" . __('Next Page', 'wpsc') . "\">" . __('Next &gt;', 'wpsc') . "</a>";
  209. }
  210. // Should we show the LAST PAGE link?
  211. if($current_page < $totalpages) {
  212. $output .= "<a href=\"". add_query_arg( 'paged', $totalpages ) . "\" title=\"" . __('Last Page', 'wpsc') . "\">" . __('Last &raquo;', 'wpsc') . "</a>";
  213. }
  214. }
  215. // Return the output.
  216. echo $output;
  217. }
  218. /**
  219. * wpsc_show_stock_availability
  220. *
  221. * Checks to see whether stock symbols need to be shown
  222. * @return boolean - true is the option has been checked false otherwise
  223. */
  224. function wpsc_show_stock_availability(){
  225. if( get_option('list_view_quantity') == 1 )
  226. return true;
  227. else
  228. return false;
  229. }
  230. /**
  231. * wpsc product image function
  232. *
  233. * if no parameters are passed, the image is not resized, otherwise it is resized to the specified dimensions
  234. *
  235. * @param integer attachment_ID
  236. * @param integer width
  237. * @param integer height
  238. * @return string - the product image URL, or the URL of the resized version
  239. */
  240. function wpsc_product_image( $attachment_id = 0, $width = null, $height = null ) {
  241. // Do some dancing around the image size
  242. if ( ( ( $width >= 10 ) && ( $height >= 10 ) ) && ( ( $width <= 1024 ) && ( $height <= 1024 ) ) )
  243. $intermediate_size = "wpsc-{$width}x{$height}";
  244. // Get image url if we have enough info
  245. if ( ( $attachment_id > 0 ) && ( !empty( $intermediate_size ) ) ) {
  246. // Get all the required information about the attachment
  247. $uploads = wp_upload_dir();
  248. $image_meta = get_post_meta( $attachment_id, '' );
  249. $file_path = get_attached_file( $attachment_id );
  250. // Clean up the meta array
  251. foreach ( $image_meta as $meta_name => $meta_value )
  252. $image_meta[$meta_name] = maybe_unserialize( array_pop( $meta_value ) );
  253. $attachment_metadata = $image_meta['_wp_attachment_metadata'];
  254. // Determine if we already have an image of this size
  255. if ( isset( $attachment_metadata['sizes'] ) && (count( $attachment_metadata['sizes'] ) > 0) && ( isset( $attachment_metadata['sizes'][$intermediate_size] ) ) ) {
  256. $intermediate_image_data = image_get_intermediate_size( $attachment_id, $intermediate_size );
  257. $image_url = $intermediate_image_data['url'];
  258. } else {
  259. $image_url = "index.php?wpsc_action=scale_image&amp;attachment_id={$attachment_id}&amp;width=$width&amp;height=$height";
  260. }
  261. // Not enough info so attempt to fallback
  262. } else {
  263. if ( !empty( $attachment_id ) ) {
  264. $image_url = "index.php?wpsc_action=scale_image&amp;attachment_id={$attachment_id}&amp;width=$width&amp;height=$height";
  265. } else {
  266. $image_url = false;
  267. }
  268. }
  269. if(empty($image_url) && !empty($file_path)){
  270. $image_meta = get_post_meta( $attachment_id, '_wp_attached_file' );
  271. if ( ! empty( $image_meta ) )
  272. $image_url = $uploads['baseurl'].'/'.$image_meta[0];
  273. }
  274. if( is_ssl() ) str_replace('http://', 'https://', $image_url);
  275. return apply_filters( 'wpsc_product_image', $image_url );
  276. }
  277. function wpsc_product_no_image_fallback( $image_url = '' ) {
  278. if ( !empty( $image_url ) )
  279. return $image_url;
  280. else
  281. return apply_filters( 'wpsc_product_noimage', WPSC_CORE_THEME_URL . 'wpsc-images/noimage.png' );
  282. }
  283. add_filter( 'wpsc_product_image', 'wpsc_product_no_image_fallback' );
  284. /**
  285. * wpsc show pnp function
  286. * @return boolean - true if display_pnp is 1 false otherwise
  287. */
  288. function wpsc_show_pnp(){
  289. global $post;
  290. if(1 == get_option('display_pnp'))
  291. return true;
  292. return false;
  293. }
  294. /**
  295. * wpsc_product_variation_price_available function
  296. * Checks for the lowest price of a products variations
  297. *
  298. * @return $price (string) number formatted price
  299. */
  300. function wpsc_product_variation_price_available($product_id){
  301. global $wpdb;
  302. $sql = $wpdb->prepare( "
  303. SELECT pm.meta_value
  304. FROM {$wpdb->posts} AS p
  305. INNER JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.id AND pm.meta_key = '_wpsc_price'
  306. INNER JOIN {$wpdb->postmeta} AS pm2 ON pm2.post_id = p.id AND pm2.meta_key = '_wpsc_stock' AND pm2.meta_value != '0'
  307. WHERE
  308. p.post_type = 'wpsc-product'
  309. AND
  310. p.post_parent = %d
  311. ORDER BY CAST(pm.meta_value AS DECIMAL(10, 2)) ASC
  312. LIMIT 1
  313. ", $product_id );
  314. $price = (float) $wpdb->get_var( $sql );
  315. $sql = $wpdb->prepare("
  316. SELECT pm.meta_value
  317. FROM {$wpdb->posts} AS p
  318. INNER JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.id AND pm.meta_key = '_wpsc_special_price' AND pm.meta_value != '0' AND pm.meta_value != ''
  319. INNER JOIN {$wpdb->postmeta} AS pm2 ON pm2.post_id = p.id AND pm2.meta_key = '_wpsc_stock' AND pm2.meta_value != '0'
  320. WHERE
  321. p.post_type = 'wpsc-product'
  322. AND
  323. p.post_parent = %d
  324. ORDER BY CAST(pm.meta_value AS DECIMAL(10, 2)) ASC
  325. LIMIT 1
  326. ", $product_id);
  327. $special_price = (float) $wpdb->get_var( $sql );
  328. if ( ! empty( $special_price ) && $special_price < $price ) {
  329. $price = $special_price;
  330. }
  331. $price = wpsc_currency_display($price, array('display_as_html' => false));
  332. return $price;
  333. }
  334. /**
  335. * wpsc product price function
  336. * @return string - the product price
  337. */
  338. function wpsc_the_product_price( $no_decimals = false ) {
  339. global $wpsc_query, $wpsc_variations, $wpdb;
  340. if ( isset($wpsc_variations->first_variations) && count( $wpsc_variations->first_variations ) > 0 ) {
  341. //select the variation ID with lowest price
  342. $output = wpsc_product_variation_price_available(get_the_ID());
  343. $from = ' from ';
  344. } else {
  345. $product_id = get_the_ID();
  346. $from = '';
  347. $full_price = get_post_meta( $product_id, '_wpsc_price', true );
  348. $special_price = get_post_meta( $product_id, '_wpsc_special_price', true );
  349. $price = $full_price;
  350. if ( ($full_price > $special_price) && ($special_price > 0) )
  351. $price = $special_price;
  352. if ( $no_decimals == true )
  353. $price = array_shift( explode( ".", $price ) );
  354. $args = array(
  355. 'display_as_html' => false,
  356. 'display_decimal_point' => !$no_decimals
  357. );
  358. $output = wpsc_currency_display( $price,$args );
  359. }
  360. //if product has variations - add 'from'
  361. $from = apply_filters('wpsc_product_variation_text',$from);
  362. if ( isset($wpsc_variations->first_variations) && count( $wpsc_variations->first_variations ) > 0 && !empty($from))
  363. $output = sprintf(__(' from %s', 'wpsc'), $output);
  364. return $output;
  365. }
  366. function wpsc_calculate_price( $product_id, $variations = null, $special = true ) {
  367. global $wpdb;
  368. $p_id = $product_id;
  369. if ( count( $variations ) > 0 ){
  370. if(!isset($variations) || is_array($variations) && in_array(0,$variations,true)) return;
  371. $product_id = wpsc_get_child_object_in_terms( $product_id, $variations, 'wpsc-variation' );
  372. }else if ( !$product_id )
  373. $product_id = get_the_ID();
  374. if( !$product_id && count( $variations ) > 0){
  375. $product_ids = wpsc_get_child_object_in_select_terms( $p_id, $variations, 'wpsc_variation' );
  376. $sql = "SELECT `post_id` FROM ".$wpdb->postmeta." WHERE `meta_key` = '_wpsc_stock' AND `meta_value` != '0' AND `post_id` IN (".implode(',' , $product_ids).")";
  377. $stock_available = $wpdb->get_col($sql);
  378. $sql = "SELECT `post_id` FROM ".$wpdb->postmeta." WHERE `meta_key` = '_wpsc_price' AND `post_id` IN (".implode(',',$stock_available).") ORDER BY `meta_value` ASC LIMIT 1";
  379. $product_id = $wpdb->get_var($sql);
  380. }
  381. if ( $special ) {
  382. $full_price = get_post_meta( $product_id, '_wpsc_price', true );
  383. $special_price = get_post_meta( $product_id, '_wpsc_special_price', true );
  384. $price = $full_price;
  385. if ( ($full_price > $special_price) && ($special_price > 0) ) {
  386. $price = $special_price;
  387. }
  388. } else {
  389. $price = get_post_meta( $product_id, '_wpsc_price', true );
  390. }
  391. return $price;
  392. }
  393. /**
  394. * wpsc display categories function
  395. * Used to determine whether to display products on the page
  396. * @return boolean - true for yes, false for no
  397. */
  398. function wpsc_display_categories() {
  399. global $wp_query;
  400. $output = false;
  401. if ( !is_numeric( get_option( 'wpsc_default_category' ) ) && ! get_query_var( 'product_tag' ) ) {
  402. if ( isset( $wp_query->query_vars['products'] ) )
  403. $category_id = $wp_query->query_vars['products'];
  404. else if ( isset( $_GET['products'] ) )
  405. $category_id = $_GET['products'];
  406. // if we have no categories, and no search, show the group list
  407. if ( is_numeric( get_option( 'wpsc_default_category' ) ) || (isset( $product_id ) && is_numeric( $product_id )) )
  408. $output = true;
  409. if ( (get_option( 'wpsc_default_category' ) == 'all+list'))
  410. $output = true;
  411. if (get_option( 'wpsc_default_category' ) == 'list' && (!isset($wp_query->query_vars['wpsc_product_category']) || !isset($wp_query->query_vars['product_tag']) && get_option('wpsc_display_categories')))
  412. $output = true;
  413. }
  414. if ( isset( $category_id ) && $category_id > 0 )
  415. $output = false;
  416. if ( get_option( 'wpsc_display_categories' ))
  417. $output = true;
  418. return $output;
  419. }
  420. /**
  421. * wpsc display products function
  422. * Used to determine whether to display products on the page
  423. * @return boolean - true for yes, false for no
  424. */
  425. function wpsc_display_products() {
  426. global $post;
  427. $product_page_id = wpec_get_the_post_id_by_shortcode('[productspage]');
  428. //we have to display something, if we are not displaying categories, then we must display products
  429. $output = true;
  430. if ( wpsc_display_categories ( ) ) {
  431. if ( get_option( 'wpsc_default_category' ) == 'list' && $post->ID == $product_page_id )
  432. $output = false;
  433. if ( isset( $_GET['range'] ) || isset( $_GET['category'] ) )
  434. $output = true;
  435. }
  436. return $output;
  437. }
  438. /**
  439. * this page url function, returns the URL of this page
  440. * @return string - the URL of the current page
  441. */
  442. function wpsc_this_page_url() {
  443. global $wpsc_query, $wp_query;
  444. if ( $wpsc_query->is_single === true ) {
  445. $output = wpsc_product_url( $wp_query->post->ID );
  446. } else if ( isset( $wpsc_query->category ) && $wpsc_query->category != null ) {
  447. $output = wpsc_category_url( $wpsc_query->category );
  448. if ( $wpsc_query->query_vars['page'] > 1 ) {
  449. if ( get_option( 'permalink_structure' ) ) {
  450. $output .= "page/{$wpsc_query->query_vars['page']}/";
  451. } else {
  452. $output = add_query_arg( 'page_number', $wpsc_query->query_vars['page'], $output );
  453. }
  454. }
  455. } elseif ( isset( $id ) ) {
  456. $output = get_permalink( $id );
  457. } else {
  458. $output = get_permalink( get_the_ID() );
  459. }
  460. return $output;
  461. }
  462. /**
  463. * is single product function, determines if we are viewing a single product
  464. * @return boolean - true, or false...
  465. */
  466. function wpsc_is_single_product() {
  467. global $wp_query;
  468. if ( $wp_query->is_single === 1 )
  469. $state = true;
  470. else
  471. $state = false;
  472. return $state;
  473. }
  474. /**
  475. * category class function, categories can have a specific class, this gets that
  476. * @return string - the class of the selected category
  477. */
  478. function wpsc_category_class() {
  479. global $wpdb, $wp_query;
  480. $category_nice_name = '';
  481. if ( 'wpsc_product_category' == $wp_query->query_vars['taxonomy'] ) {
  482. $catid = wpsc_get_the_category_id($wp_query->query_vars['term'],'slug');
  483. } else {
  484. $catid = get_option( 'wpsc_default_category' );
  485. if ( $catid == 'all+list' ) {
  486. $catid = 'all';
  487. }
  488. }
  489. if ( (int)$catid > 0 ){
  490. $term = get_term($catid, 'wpsc_product_category');
  491. $category_nice_name = $term->slug;
  492. }else if ( $catid == 'all' ){
  493. $category_nice_name = 'all-categories';
  494. }
  495. return $category_nice_name;
  496. }
  497. /**
  498. * category transition function, finds the transition between categories
  499. * @return string - the class of the selected category
  500. */
  501. function wpsc_current_category_name() {
  502. global $wp_query;
  503. $term_data = get_term( $wp_query->post->term_id, 'wpsc_product_category' );
  504. return $term_data->name;
  505. }
  506. /**
  507. * category transition function, finds the transition between categories
  508. * @return string - the class of the selected category
  509. */
  510. function wpsc_category_transition() {
  511. //removed because it was not working in 3.8 RC2 see first changest after
  512. //http://plugins.trac.wordpress.org/changeset/357529/wp-e-commerce/
  513. return false;
  514. }
  515. /**
  516. * wpsc show fb like function, check whether to show facebook like
  517. * @return boolean true if option is on, otherwise, false
  518. */
  519. function wpsc_show_fb_like(){
  520. if('on' == get_option('wpsc_facebook_like'))
  521. return true;
  522. else
  523. return false;
  524. }
  525. /**
  526. * wpsc have products function, the product loop
  527. * @return boolean true while we have products, otherwise, false
  528. */
  529. function wpsc_have_products() {
  530. return have_posts();
  531. }
  532. /**
  533. * wpsc the product function, gets the next product,
  534. * @return nothing
  535. */
  536. function wpsc_the_product() {
  537. global $wpsc_custom_meta, $wpsc_variations;
  538. the_post();
  539. $wpsc_custom_meta = new wpsc_custom_meta( get_the_ID() );
  540. $wpsc_variations = new wpsc_variations( get_the_ID() );
  541. }
  542. /**
  543. * wpsc in the loop function,
  544. * @return boolean - true if we are in the loop
  545. */
  546. function wpsc_in_the_loop() {
  547. _deprecated_function( __FUNCTION__, '3.8', 'the updated ' . __FUNCTION__ . '' );
  548. global $wpsc_query;
  549. return $wpsc_query->in_the_loop;
  550. }
  551. /**
  552. * wpsc rewind products function, rewinds back to the first product
  553. * @return nothing
  554. */
  555. function wpsc_rewind_products() {
  556. _deprecated_function( __FUNCTION__, '3.8', 'the updated ' . __FUNCTION__ . '' );
  557. global $wpsc_query;
  558. return $wpsc_query->rewind_posts();
  559. }
  560. /**
  561. * wpsc the product id function,
  562. * @return integer - the product ID
  563. */
  564. function wpsc_the_product_id() {
  565. return get_the_ID();
  566. }
  567. /**
  568. * wpsc edit the product link function
  569. * @return string - a link to edit this product
  570. */
  571. function wpsc_edit_the_product_link( $link = null, $before = '', $after = '', $id = 0 ) {
  572. global $wpsc_query, $current_user, $table_prefix, $wp_query;
  573. if ( $link == null )
  574. $link = __( 'Edit', 'wpsc' );
  575. $product_id = $wp_query->post->ID;
  576. if ( $id > 0 )
  577. $product_id = $id;
  578. $siteurl = get_option( 'siteurl' );
  579. $output = '';
  580. if(is_user_logged_in()){
  581. get_currentuserinfo();
  582. if ( $current_user->{$table_prefix . 'capabilities'}['administrator'] == 1 )
  583. $output = $before . "<a class='wpsc_edit_product' href='{$siteurl}/wp-admin/post.php?action=edit&amp;post={$product_id}'>" . $link . "</a>" . $after;
  584. }
  585. return $output;
  586. }
  587. /**
  588. * wpsc the product title function
  589. * @return string - the product title
  590. */
  591. function wpsc_the_product_title() {
  592. return get_the_title();
  593. }
  594. /**
  595. * wpsc product description function
  596. * @return string - the product description
  597. */
  598. function wpsc_the_product_description() {
  599. $content = get_the_content( __( 'Read the rest of this entry &raquo;', 'wpsc' ) );
  600. return do_shortcode( wpautop( $content,1 ) );
  601. }
  602. /**
  603. * wpsc additional product description function
  604. * TODO make this work with the tabbed multiple product descriptions, may require another loop
  605. * @return string - the additional description
  606. */
  607. function wpsc_the_product_additional_description() {
  608. global $post;
  609. if ( !empty( $post->post_excerpt ) )
  610. return $post->post_excerpt;
  611. else
  612. return false;
  613. }
  614. /**
  615. * wpsc product permalink function
  616. * @return string - the URL to the single product page for this product
  617. */
  618. function wpsc_the_product_permalink() {
  619. global $wp_query;
  620. return get_permalink();
  621. }
  622. /**
  623. * wpsc external link function
  624. * @return string - the product price
  625. */
  626. function wpsc_product_external_link( $id = null ) {
  627. if ( is_numeric( $id ) && ( $id > 0 ) )
  628. $id = absint( $id );
  629. else
  630. $id = get_the_ID();
  631. $product_meta = get_post_meta( $id, '_wpsc_product_metadata', true );
  632. if ( isset( $product_meta['external_link'] ) ) {
  633. $external_link = $product_meta['external_link'];
  634. return esc_url( $external_link );
  635. }
  636. }
  637. /**
  638. * wpsc external link text function
  639. * @return string - the product external link text
  640. */
  641. function wpsc_product_external_link_text( $id = null, $default = null ) {
  642. if ( is_numeric( $id ) && ( $id > 0 ) )
  643. $id = absint( $id );
  644. else
  645. $id = get_the_ID();
  646. $external_link_text = __( 'Buy Now', 'wpsc' );
  647. if ( $default != null ) {
  648. $external_link_text = $default;
  649. }
  650. $product_meta = get_post_meta( $id, '_wpsc_product_metadata', true );
  651. if ( isset( $product_meta['external_link_text'] ) && !empty( $product_meta['external_link_text'] ) ) {
  652. $external_link_text = $product_meta['external_link_text'];
  653. }
  654. return esc_html( $external_link_text );
  655. }
  656. /**
  657. * wpsc external link target function
  658. * @return string - the product external link target
  659. */
  660. function wpsc_product_external_link_target( $id = null, $external_link_target = '' ) {
  661. if ( is_numeric( $id ) && ( $id > 0 ) )
  662. $id = absint( $id );
  663. else
  664. $id = get_the_ID();
  665. $product_meta = get_post_meta( $id, '_wpsc_product_metadata', true );
  666. if ( isset( $product_meta['external_link_target'] ) && !empty( $product_meta['external_link_target'] ) ) {
  667. $external_link_target = $product_meta['external_link_target'];
  668. }
  669. return esc_attr( $external_link_target );
  670. }
  671. /**
  672. * wpsc product sku function
  673. * @return string - the product price
  674. */
  675. function wpsc_product_sku( $id = null ) {
  676. if ( is_numeric( $id ) && ( $id > 0 ) )
  677. $id = absint( $id );
  678. else
  679. $id = get_the_ID();
  680. $product_sku = get_post_meta( $id, '_wpsc_sku', true );
  681. return esc_attr( $product_sku );
  682. }
  683. /**
  684. * wpsc product creation time function
  685. * @return string - the product price
  686. */
  687. function wpsc_product_creation_time( $format = null ) {
  688. global $wpsc_query;
  689. if ( $format == null )
  690. $format = "Y-m-d H:i:s";
  691. return mysql2date( $format, $wpsc_query->product['date_added'] );
  692. }
  693. /**
  694. * wpsc check variation stock availability function
  695. * @return string - the product price
  696. */
  697. function wpsc_check_variation_stock_availability( $product_id, $variations ) {
  698. global $wpdb;
  699. $selected_post = (array)get_posts( array(
  700. 'post_parent' => $product_id,
  701. 'post_type' => "wpsc-product",
  702. 'post_status' => 'all',
  703. 'suppress_filters' => true
  704. ) );
  705. foreach ( $selected_post as $variation ) {
  706. $matches = 0;
  707. $terms = wp_get_object_terms( $variation->ID, 'wpsc-variation' );
  708. foreach ( $terms as $term ) {
  709. if ( in_array( $term->term_id, $variations ) )
  710. $matches++;
  711. if ( $matches == count( $variations ) ) {
  712. $the_selected_product = $variation->ID;
  713. }
  714. }
  715. }
  716. if ( wpsc_product_has_stock( $the_selected_product ) ) {
  717. $stock = get_product_meta( $the_selected_product, "stock", true );
  718. $stock = apply_filters( 'wpsc_product_variation_stock', $stock, $product_id );
  719. if ( 0 < $stock )
  720. return $stock;
  721. }else{
  722. $stock = get_product_meta( $the_selected_product, "stock", true );
  723. $stock = apply_filters( 'wpsc_product_variation_stock', $stock, $product_id );
  724. if ( is_numeric($stock) ){
  725. $claimed_stock = $wpdb->get_var("SELECT SUM(`stock_claimed`) FROM `".WPSC_TABLE_CLAIMED_STOCK."` WHERE `product_id` IN('$the_selected_product')");
  726. $stock = ($stock - $claimed_stock);
  727. return $stock;
  728. }
  729. }
  730. return '';
  731. }
  732. /**
  733. * wpsc product has stock function
  734. * @return boolean - true if the product has stock or does not use stock, false if it does not
  735. */
  736. function wpsc_product_has_stock( $id = null ) {
  737. global $wpdb;
  738. // maybe do wpsc_clear_stock_claims first?
  739. if ( is_numeric( $id ) && ( $id > 0 ) )
  740. $id = absint( $id );
  741. else
  742. $id = get_the_ID();
  743. $stock = get_post_meta( $id, '_wpsc_stock', true );
  744. $stock = apply_filters( 'wpsc_product_stock', $stock, $id );
  745. $variations = get_children( array( "post_type" => "wpsc-product", "post_parent" => $id ) );
  746. if ( is_numeric( $stock ) ) {
  747. if ( $stock > 0 && empty($variations)) {
  748. $claimed_stock = $wpdb->get_var("SELECT SUM(`stock_claimed`) FROM `".WPSC_TABLE_CLAIMED_STOCK."` WHERE `product_id` IN($id)");
  749. if($stock - $claimed_stock > 0)
  750. return true;
  751. }
  752. if ( count( $variations ) ) {
  753. foreach ( $variations as $variation ) {
  754. $stock = get_post_meta( $variation->ID, '_wpsc_stock', true );
  755. $claimed_stock = $wpdb->get_var("SELECT SUM(`stock_claimed`) FROM `".WPSC_TABLE_CLAIMED_STOCK."` WHERE `product_id` IN($variation->ID)");
  756. if ( is_numeric( $stock ) && ($stock - $claimed_stock) > 0 ) {
  757. return true;
  758. }
  759. }
  760. }
  761. } else {
  762. return true;
  763. }
  764. }
  765. /**
  766. * wpsc_is_product_external( $product_id = 0 )
  767. *
  768. * Checks if current product is external.
  769. *
  770. * @param int $product_id
  771. */
  772. function wpsc_is_product_external( $product_id = 0 ) {
  773. // Get product ID if incorrect value was passed
  774. if ( empty( $product_id ) || !is_numeric( $product_id ) )
  775. $product_id = wpsc_the_product_id();
  776. // Get external link
  777. $external_link = wpsc_product_external_link( $product_id );
  778. // Use external if set
  779. if ( !empty( $external_link ) )
  780. return true;
  781. else
  782. return false;
  783. }
  784. /**
  785. * wpsc product remaining stock function
  786. * @return integer - the amount of remaining stock, or null if product is stockless
  787. */
  788. function wpsc_product_remaining_stock( $id = null ) {
  789. if ( is_numeric( $id ) && ($id > 0) )
  790. $id = absint( $id );
  791. else
  792. $id = get_the_ID();
  793. $is_limited_stock = get_post_meta( $id, '_wpsc_stock', true );
  794. if ( is_numeric( $is_limited_stock ) ) {
  795. $product_stock = get_post_meta( $id, '_wpsc_stock', true );
  796. return absint( $product_stock );
  797. } else {
  798. return null;
  799. }
  800. }
  801. /**
  802. * wpsc is donation function
  803. * @return boolean - true if it is a donation, otherwise false
  804. */
  805. function wpsc_product_is_donation( $id = null ) {
  806. if ( is_numeric( $id ) && ($id > 0) )
  807. $id = absint( $id );
  808. else
  809. $id = get_the_ID();
  810. $is_donation = get_post_meta( $id, '_wpsc_is_donation', true );
  811. if ( $is_donation == 1 )
  812. return true;
  813. else
  814. return false;
  815. }
  816. /**
  817. * wpsc product on special function
  818. * @return boolean - true if the product is on special, otherwise false
  819. */
  820. function wpsc_product_on_special() {
  821. global $wpsc_query, $wpdb;
  822. $price = get_product_meta( get_the_ID(), 'price', true );
  823. // don't rely on product sales price if it has variations
  824. if ( wpsc_have_variations() ) {
  825. $sql = $wpdb->prepare("
  826. SELECT MIN(pm.meta_value)
  827. FROM {$wpdb->posts} AS p
  828. INNER JOIN {$wpdb->postmeta} AS pm ON pm.post_id = p.id AND pm.meta_key = '_wpsc_special_price' AND pm.meta_value != '0' AND pm.meta_value != ''
  829. INNER JOIN {$wpdb->postmeta} AS pm2 ON pm2.post_id = p.id AND pm2.meta_key = '_wpsc_stock' AND pm2.meta_value != '0'
  830. WHERE
  831. p.post_type = 'wpsc-product'
  832. AND
  833. p.post_parent = %d
  834. ORDER BY CAST(pm.meta_value AS DECIMAL(10, 2)) ASC
  835. LIMIT 1
  836. ", get_the_id() );
  837. $special_price = (int) $wpdb->get_var( $sql );
  838. } else {
  839. $special_price = get_product_meta( get_the_ID(), 'special_price', true );
  840. }
  841. if ( ($special_price > 0) && (($price - $special_price) > 0) )
  842. return true;
  843. else
  844. return false;
  845. }
  846. /**
  847. * wpsc product has file function
  848. * @return boolean - true if the product has a file
  849. */
  850. function wpsc_product_has_file() {
  851. _deprecated_function( __FUNCTION__, '3.8', 'the updated ' . __FUNCTION__ . '' );
  852. global $wpsc_query, $wpdb;
  853. if ( is_numeric( $wpsc_query->product['file'] ) && ($wpsc_query->product['file'] > 0) )
  854. return true;
  855. return false;
  856. }
  857. /**
  858. * wpsc product is modifiable function
  859. * @return boolean - true if the product has a file
  860. */
  861. function wpsc_product_is_customisable() {
  862. global $wpsc_query, $wpdb;
  863. $id = get_the_ID();
  864. $product_meta = get_post_meta( $id, '_wpsc_product_metadata', true );
  865. if ( (isset($product_meta['engraved']) && $product_meta['engraved'] == true) || (isset($product_meta['can_have_uploaded_image']) && $product_meta['can_have_uploaded_image'] == true) )
  866. return true;
  867. return false;
  868. }
  869. /**
  870. * wpsc product has personal text function
  871. * @return boolean - true if the product has a file
  872. */
  873. function wpsc_product_has_personal_text() {
  874. global $wpsc_query, $wpdb;
  875. $id = get_the_ID();
  876. $product_meta = get_post_meta( $id, '_wpsc_product_metadata', true );
  877. if ( isset($product_meta['engraved']) && $product_meta['engraved'] == true )
  878. return true;
  879. return false;
  880. }
  881. /**
  882. * wpsc product has personal file function
  883. * @return boolean - true if the product has a file
  884. */
  885. function wpsc_product_has_supplied_file() {
  886. global $wpsc_query, $wpdb;
  887. $id = get_the_ID();
  888. $product_meta = get_post_meta( $id, '_wpsc_product_metadata', true );
  889. if ( isset($product_meta['can_have_uploaded_image']) && $product_meta['can_have_uploaded_image'] == true )
  890. return true;
  891. return false;
  892. }
  893. /**
  894. * wpsc product postage and packaging function
  895. * @return string - currently only valid for flat rate
  896. */
  897. function wpsc_product_postage_and_packaging() {
  898. if ( isset( $id ) && is_numeric( $id ) && ($id > 0) )
  899. $id = absint( $id );
  900. else
  901. $id = get_the_ID();
  902. $product_meta = get_post_meta( $id, '_wpsc_product_metadata', true );
  903. if ( isset( $product_meta['shipping'] ) && is_array( $product_meta['shipping'] ) && 1 != $product_meta['no_shipping'])
  904. return wpsc_currency_display( $product_meta['shipping']['local'] );
  905. else
  906. return wpsc_currency_display( 0 );
  907. }
  908. /**
  909. * wpsc normal product price function
  910. * TODO determine why this function is here
  911. * @return string - returns some form of product price
  912. */
  913. function wpsc_product_normal_price($forRSS = false) {
  914. global $wpsc_query, $wpdb, $wpsc_variations;
  915. if ( is_object($wpsc_variations) && count( $wpsc_variations->first_variations ) > 0 ) {
  916. //select the variation ID with lovest price
  917. $product_id = $wpdb->get_var('SELECT `posts`.`id` FROM ' . $wpdb->posts . ' `posts` JOIN ' . $wpdb->postmeta . ' `postmeta` ON `posts`.`id` = `postmeta`.`post_id` WHERE `posts`.`post_parent` = ' . get_the_ID() . ' AND `posts`.`post_type` = "wpsc-product" AND `posts`.`post_status` = "inherit" AND `postmeta`.`meta_key`="_wpsc_price" ORDER BY (`postmeta`.`meta_value`)+0 ASC LIMIT 1');
  918. $from = ' from ';
  919. } else {
  920. $product_id = get_the_ID();
  921. $from = '';
  922. }
  923. $from = apply_filters('wpsc_product_variation_text',$from);
  924. $price = get_product_meta( $product_id, 'price', true );
  925. if($forRSS)
  926. $output = $from.wpsc_currency_display( $price, array( 'display_as_html' => false ) );
  927. else
  928. $output = $from.wpsc_currency_display( $price );
  929. return $output;
  930. }
  931. /**
  932. * wpsc product image function
  933. * @return string - the URL to the thumbnail image
  934. */
  935. function wpsc_the_product_image( $width='', $height='', $product_id='' ) {
  936. if ( empty( $product_id ) )
  937. $product_id = get_the_ID();
  938. $product = get_post( $product_id );
  939. if ( $product->post_parent > 0 )
  940. $product_id = $product->post_parent;
  941. $attached_images = (array)get_posts( array(
  942. 'post_type' => 'attachment',
  943. 'numberposts' => 1,
  944. 'post_status' => null,
  945. 'post_parent' => $product_id,
  946. 'orderby' => 'menu_order',
  947. 'order' => 'ASC'
  948. ) );
  949. $post_thumbnail_id = get_post_thumbnail_id( $product_id );
  950. $src = wp_get_attachment_image_src( $post_thumbnail_id, 'large' );
  951. if ( ! empty( $src ) && is_string( $src[0] ) ) {
  952. $src = $src[0];
  953. } elseif ( ! empty( $attached_images ) ) {
  954. $attached_image = wp_get_attachment_image_src( $attached_images[0]->ID, 'large' );
  955. $src = $attached_image[0];
  956. } else {
  957. $src = false;
  958. }
  959. if ( is_ssl() && ! empty( $src ) )
  960. $src = str_replace( 'http://', 'https://', $src );
  961. $src = apply_filters( 'wpsc_product_image', $src );
  962. return $src;
  963. }
  964. /**
  965. * wpsc check display type
  966. *
  967. * Check the display view for the selected category
  968. *
  969. * @return string - display type
  970. */
  971. function wpsc_check_display_type(){
  972. global $wpsc_query, $post;
  973. if(isset($wpsc_query->query_vars['taxonomy']) && 'wpsc_product_category' == $wpsc_query->query_vars['taxonomy'] && is_string($wpsc_query->query_vars['term']) && 1 < $wpsc_query->post_count)
  974. $display_type = wpsc_get_the_category_display($wpsc_query->query_vars['term']);
  975. else
  976. $display_type = get_option('product_view');
  977. return $display_type;
  978. }
  979. /**
  980. * wpsc product thumbnail function
  981. *
  982. * Show the thumbnail image for the product
  983. *
  984. * @return string - the URL to the thumbnail image
  985. */
  986. function wpsc_the_product_thumbnail( $width = null, $height = null, $product_id = 0, $page = 'products-page' ) {
  987. $thumbnail = false;
  988. $display = wpsc_check_display_type();
  989. // Get the product ID if none was passed
  990. if ( empty( $product_id ) )
  991. $product_id = get_the_ID();
  992. // Load the product
  993. $product = get_post( $product_id );
  994. // Get ID of parent product if one exists
  995. if ( !empty( $product->post_parent ) )
  996. $product_id = $product->post_parent;
  997. // Load image proportions if none were passed
  998. if ( ( $width < 10 ) || ( $height < 10 ) ) {
  999. $width = get_option( 'product_image_width' );
  1000. $height = get_option( 'product_image_height' );
  1001. }
  1002. // Use product thumbnail
  1003. if ( has_post_thumbnail( $product_id ) ) {
  1004. $thumbnail_id = get_post_thumbnail_id( $product_id );
  1005. // Use first product image
  1006. } else {
  1007. // Get all attached images to this product
  1008. $attached_images = (array)get_posts( array(
  1009. 'post_type' => 'attachment',
  1010. 'numberposts' => 1,
  1011. 'post_status' => null,
  1012. 'post_parent' => $product_id ,
  1013. 'orderby' => 'menu_order',
  1014. 'order' => 'ASC'
  1015. ) );
  1016. if ( !empty( $attached_images ) )
  1017. $thumbnail_id = $attached_images[0]->ID;
  1018. }
  1019. //Overwrite height & width if custom dimensions exist for thumbnail_id
  1020. if ( 'grid' != $display && 'products-page' == $page && isset($thumbnail_id)) {
  1021. $custom_width = get_post_meta( $thumbnail_id, '_wpsc_custom_thumb_w', true );
  1022. $custom_height = get_post_meta( $thumbnail_id, '_wpsc_custom_thumb_h', true );
  1023. if ( !empty( $custom_width ) && !empty( $custom_height ) ) {
  1024. $width = $custom_width;
  1025. $height = $custom_height;
  1026. }
  1027. } elseif( $page == 'single' && isset($thumbnail_id)) {
  1028. $custom_thumbnail = get_post_meta( $thumbnail_id, '_wpsc_selected_image_size', true );
  1029. if ( !$custom_thumbnail ) {
  1030. $custom_thumbnail = 'medium-single-product';
  1031. // regenerate size metadata in case it's missing
  1032. if ( ! image_get_intermediate_size( $thumbnail_id, $custom_thumbnail ) ) {
  1033. require_once( ABSPATH . 'wp-admin/includes/image.php' );
  1034. $metadata = wp_get_attachment_metadata( $thumbnail_id );
  1035. $file = get_attached_file( $thumbnail_id );
  1036. $metadata = array_merge( wp_generate_attachment_metadata( $thumbnail_id, $file ), $metadata );
  1037. wp_update_attachment_metadata( $thumbnail_id, $metadata );
  1038. }
  1039. }
  1040. $src = wp_get_attachment_image_src( $thumbnail_id, $custom_thumbnail );
  1041. if ( !empty( $src ) && is_string( $src[0] ) ) {
  1042. $thumbnail = $src[0];
  1043. }
  1044. }
  1045. if ( ! $thumbnail && isset( $thumbnail_id ) )
  1046. $thumbnail = wpsc_product_image( $thumbnail_id, $width, $height );
  1047. if ( ! empty( $thumbnail ) && is_ssl() )
  1048. $thumbnail = str_replace( 'http://', 'https://', $thumbnail );
  1049. return $thumbnail;
  1050. }
  1051. /**
  1052. * Return the class(es) that should be applied to a product image's <a> tag.
  1053. *
  1054. * If the thickbox effect is enabled for product images (presentation setting), the thickbox class name is included
  1055. *
  1056. * This function is called from theme files when outputting product img tags
  1057. *
  1058. * @since 3.8
  1059. * @return string space-separated list of class names (for use in a class="") attribute
  1060. */
  1061. function wpsc_the_product_image_link_classes() {
  1062. $classes = array( );
  1063. if ( get_option( 'show_thumbnails_thickbox' ) )
  1064. $classes[] = 'thickbox';
  1065. $classes[] = 'preview_link';
  1066. $classes = apply_filters( 'wpsc_the_product_image_link_classes', $classes );
  1067. return implode( ' ', $classes );
  1068. }
  1069. /**
  1070. * wpsc product comment link function
  1071. * @return string - javascript required to make the intense debate link work
  1072. */
  1073. function wpsc_product_comment_link() {
  1074. // add the product comment link
  1075. global $wpsc_query;
  1076. if ( get_option( 'wpsc_enable_comments' ) == 1 ) {
  1077. $enable_for_product = get_product_meta( get_the_ID(), 'enable_comments' );
  1078. if ( (get_option( 'wpsc_comments_which_products' ) == 1 && $enable_for_product == '') || $enable_for_product == 'yes' ) {
  1079. $original = array( "&", "'", ":", "/", "@", "?", "=" );
  1080. $entities = array( "%26", "%27", "%3A", "%2F", "%40", "%3F", "%3D" );
  1081. $output = "<div class=\"clear comments\">
  1082. <script src='https://www.intensedebate.com/js/getCommentLink.php?acct=" . get_option( "wpsc_intense_debate_account_id" ) . "&postid=product_" . $wpsc_query->product['id'] . "&posttitle=" . urlencode( get_the_title() ) . "&posturl=" . str_replace( $original, $entities, wpsc_product_url( get_the_ID(), null, false ) ) . "&posttime=" . urlencode( date( 'Y-m-d h:i:s', time() ) ) . "&postauthor=author_" . get_the_ID() . "' type='text/javascript' defer='defer'></script>
  1083. </div>";
  1084. }
  1085. }
  1086. return $output;
  1087. }
  1088. /**
  1089. * wpsc product comments function
  1090. * @return string - javascript for the intensedebate comments
  1091. */
  1092. function wpsc_product_comments() {
  1093. global $wpsc_query;
  1094. $output = '';
  1095. // add the product comments
  1096. if ( get_option( 'wpsc_enable_comments' ) == 1 ) {
  1097. $enable_for_product = get_product_meta( $wpsc_query->product['id'], 'enable_comments' );
  1098. if ( (get_option( 'wpsc_comments_which_products' ) == 1 && $enable_for_product == '') || $enable_for_product == 'yes' ) {
  1099. $output = "<script>
  1100. var idcomments_acct = '" . esc_js( get_option( 'wpsc_intense_debate_account_id' ) ) . "';
  1101. var idcomments_post_id = 'product_" . $wpsc_query->product['id'] . "';
  1102. var idcomments_post_url = encodeURIComponent('" . wpsc_product_url( $wpsc_query->product['id'], null, false ) . "');
  1103. </script>
  1104. <span id=\"IDCommentsPostTitle\" style=\"display:none\"></span>
  1105. <script type='text/javascript' src='https://www.intensedebate.com/js/genericCommentWrapperV2.js'></script>
  1106. ";
  1107. }
  1108. }
  1109. return $output;
  1110. }
  1111. /**
  1112. * wpsc have custom meta function
  1113. * @return boolean - true while we have custom meta to display
  1114. */
  1115. function wpsc_have_custom_meta() {
  1116. global $wpsc_custom_meta;
  1117. return esc_html( $wpsc_custom_meta->have_custom_meta() );
  1118. }
  1119. /**
  1120. * wpsc the custom meta function
  1121. * @return nothing - iterate through the custom meta vallues
  1122. */
  1123. function wpsc_the_custom_meta() {
  1124. global $wpsc_custom_meta;
  1125. return esc_html( $wpsc_custom_meta->the_custom_meta() );
  1126. }
  1127. /**
  1128. * wpsc custom meta name function
  1129. * @return string - the custom metal name
  1130. */
  1131. function wpsc_custom_meta_name() {
  1132. global $wpsc_custom_meta;
  1133. return esc_html( $wpsc_custom_meta->custom_meta_values['meta_key'] );
  1134. }
  1135. /**
  1136. * wpsc custom meta value function
  1137. * @return string - the custom meta value
  1138. */
  1139. function wpsc_custom_meta_value() {
  1140. global $wpsc_custom_meta;
  1141. return esc_html( $wpsc_custom_meta->custom_meta_values['meta_value'] );
  1142. }
  1143. /**
  1144. * wpsc have variation groups function
  1145. * @return boolean - true while we have variation groups
  1146. */
  1147. function wpsc_have_variation_groups() {
  1148. global $wpsc_variations;
  1149. return $wpsc_variations->have_variation_groups();
  1150. }
  1151. /**
  1152. * wpsc the variation group function
  1153. * @return nothing - iterate through the variation groups
  1154. */
  1155. function wpsc_the_variation_group() {
  1156. global $wpsc_variations;
  1157. $wpsc_variations->the_variation_group();
  1158. }
  1159. /**
  1160. * wpsc have variations function
  1161. * @return boolean - true while we have variations
  1162. */
  1163. function wpsc_have_variations() {
  1164. global $wpsc_variations;
  1165. return $wpsc_variations->have_variations();
  1166. }
  1167. /**
  1168. * wpsc the variation function
  1169. * @return nothing - iterate through the variations
  1170. */
  1171. function wpsc_the_variation() {
  1172. global $wpsc_variations;
  1173. $wpsc_variations->the_variation();
  1174. }
  1175. function wpsc_product_has_multicurrency() {
  1176. global $wpdb, $wpsc_query;
  1177. $currency = get_product_meta(get_the_ID(),'currency',true);
  1178. if ( count( $currency ) > 0 )
  1179. return true;
  1180. else
  1181. return false;
  1182. }
  1183. function wpsc_display_product_multicurrency() {
  1184. global $wpdb, $wpsc_query;
  1185. $results = get_product_meta(get_the_ID(),'currency',true);
  1186. if ( count( $results ) > 0 ) {
  1187. foreach ( (array)$results as $isocode => $curr ) {
  1188. $currency_data = $wpdb->get_row( "SELECT `symbol`,`symbol_html`,`code` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `isocode`='" . $isocode . "' LIMIT 1", ARRAY_A );
  1189. if ( $currency_data['symbol'] != '' )
  1190. $currency_sign = $currency_data['symbol_html'];
  1191. else
  1192. $currency_sign = $currency_data['code'];
  1193. if ( !empty( $currency_sign ) )
  1194. echo '<span class="wpscsmall pricefloatright pricedisplay">' . $isocode . ' ' . wpsc_currency_display( $curr["meta_value"] ) . '</span><br />';
  1195. }
  1196. }
  1197. return false;
  1198. }
  1199. /**
  1200. * wpsc variation group name function
  1201. * @return string - the variaton group name
  1202. */
  1203. function wpsc_the_vargrp_name() {
  1204. // get the variation group name;
  1205. global $wpsc_variations;
  1206. return $wpsc_variations->variation_group->name;
  1207. }
  1208. /**
  1209. * wpsc variation group form ID function
  1210. * @return string - the variation group form id, for labels and the like
  1211. */
  1212. function wpsc_vargrp_form_id() {
  1213. // generate the variation group form ID;
  1214. global $wpsc_variations, $wpsc_variations;
  1215. $product_id = get_the_ID();
  1216. $form_id = "variation_select_{$product_id}_{$wpsc_variations->variation_group->term_id}";
  1217. return $form_id;
  1218. }
  1219. /**
  1220. * wpsc variation group ID function
  1221. * @return integer - the variation group ID
  1222. */
  1223. function wpsc_vargrp_id() {
  1224. global $wpsc_variations;
  1225. return $wpsc_variations->variation_group->term_id;
  1226. }
  1227. /**
  1228. * wpsc the variation name function
  1229. * @return string - the variation name
  1230. */
  1231. function wpsc_the_variation_name() {
  1232. global $wpsc_variations;
  1233. return stripslashes( $wpsc_variations->variation->name );
  1234. }
  1235. /**
  1236. * wpsc the variation ID function
  1237. * @return integer - the variation ID
  1238. */
  1239. function wpsc_the_variation_id() {
  1240. global $wpsc_variations;
  1241. return $wpsc_variations->variation->term_id;
  1242. }
  1243. /**
  1244. * wpsc the variation out_of_stock function
  1245. * @return string - HTML attribute to disable select options and radio buttons
  1246. */
  1247. function wpsc_the_variation_out_of_stock() {
  1248. global $wpsc_query, $wpdb, $wpsc_variations;
  1249. $out_of_stock = false;
  1250. // If there is more than one variation group we cannot determine a stock status for individual variations
  1251. // Also, if the item is not stock limited, there is no need to check variation stock status
  1252. $product_id = get_the_ID();
  1253. $stock = get_product_meta( $product_id, 'stock', true );
  1254. if ( ($wpsc_variations->variation_group_count == 1) && is_numeric( $stock ) && isset( $wpsc_variations->variation->slug ) ) {
  1255. $product_id = get_the_ID();
  1256. $variation_group_id = $wpsc_variations->variation_group->term_id;
  1257. $variation_id = $wpsc_variations->variation->term_id;
  1258. $wpq = array( 'variations' => $wpsc_variations->variation->slug,
  1259. 'post_status' => 'inherit',
  1260. 'post_type' => 'wpsc-product',
  1261. 'post_parent' => $product_id );
  1262. $query = new WP_Query( $wpq );
  1263. if ( $query->post_count != 1 ) {
  1264. // Should never happen
  1265. return FALSE;
  1266. }
  1267. $variation_product_id = $query->posts[0]->ID;
  1268. $stock = get_product_meta( $variation_product_id, "stock" );
  1269. $stock = $stock[0];
  1270. if ( $stock < 1 ) {
  1271. $out_of_stock = true;
  1272. }
  1273. }
  1274. if ( $out_of_stock == true )
  1275. return "disabled='disabled'";
  1276. else
  1277. return '';
  1278. }
  1279. /**
  1280. * wpsc product rater function
  1281. * @return string - HTML to display the product rater
  1282. */
  1283. function wpsc_product_rater() {
  1284. global $wpsc_query;
  1285. $product_id = get_the_ID();
  1286. $output = '';
  1287. if ( get_option( 'product_ratings' ) == 1 ) {
  1288. $output .= "<div class='product_footer'>";
  1289. $output .= "<div class='product_average_vote'>";
  1290. $output .= "<strong>" . __( 'Avg. Customer Rating', 'wpsc' ) . ":</strong>";
  1291. $output .= wpsc_product_existing_rating( $product_id );
  1292. $output .= "</div>";
  1293. $output .= "<div class='product_user_vote'>";
  1294. $output .= "<strong><span id='rating_" . $product_id . "_text'>" . __( 'Your Rating', 'wpsc' ) . ":</span>";
  1295. $output .= "<span class='rating_saved' id='saved_" . $product_id . "_text'> " . __( 'Saved', 'wpsc' ) . "</span>";
  1296. $output .= "</strong>";
  1297. $output .= wpsc_product_new_rating( $product_id );
  1298. $output .= "</div>";
  1299. $output .= "</div>";
  1300. }
  1301. return $output;
  1302. }
  1303. function wpsc_product_existing_rating( $product_id ) {
  1304. global $wpdb;
  1305. $get_average = $wpdb->get_results( "SELECT AVG(`rated`) AS `average`, COUNT(*) AS `count` FROM `" . WPSC_TABLE_PRODUCT_RATING . "` WHERE `productid`='" . $product_id . "'", ARRAY_A );
  1306. $average = floor( $get_average[0]['average'] );
  1307. $count = $get_average[0]['count'];
  1308. $output = " <span class='votetext'>";
  1309. for ( $l = 1; $l <= $average; ++$l ) {
  1310. $output .= "<img class='goldstar' src='" . WPSC_CORE_IMAGES_URL . "/gold-star.gif' alt='$l' title='$l' />";
  1311. }
  1312. $remainder = 5 - $average;
  1313. for ( $l = 1; $l <= $remainder; ++$l ) {
  1314. $output .= "<img class='goldstar' src='" . WPSC_CORE_IMAGES_URL . "/grey-star.gif' alt='$l' title='$l' />";
  1315. }
  1316. $output .= "<span class='vote_total'>&nbsp;(<span id='vote_total_{$product_id}'>" . $count . "</span>)</span> \r\n";
  1317. $output .= "</span> \r\n";
  1318. return $output;
  1319. }
  1320. function wpsc_product_new_rating( $product_id ) {
  1321. global $wpdb;
  1322. $cookie_data = '';
  1323. if (isset($_COOKIE['voting_cookie'][$product_id])) {
  1324. $cookie_data = explode( ",", $_COOKIE['voting_cookie'][$product_id] );
  1325. }
  1326. $vote_id = 0;
  1327. if ( isset($cookie_data[0]) && is_numeric( $cookie_data[0] ) )
  1328. $vote_id = absint( $cookie_data[0] );
  1329. $previous_vote = 1;
  1330. if ( $vote_id > 0 )
  1331. $previous_vote = $wpdb->get_var( "SELECT `rated` FROM `" . WPSC_TABLE_PRODUCT_RATING . "` WHERE `id`='" . $vote_id . "' LIMIT 1" );
  1332. $output = "<form class='wpsc_product_rating' method='post'>\n";
  1333. $output .= " <input type='hidden' name='wpsc_ajax_action' value='rate_product' />\n";
  1334. $output .= " <input type='hidden' class='wpsc_rating_product_id' name='product_id' value='{$product_id}' />\n";
  1335. $output .= " <select class='wpsc_select_product_rating' name='product_rating'>\n";
  1336. $output .= " <option " . (($previous_vote == '1') ? "selected='selected'" : '') . " value='1'>1</option>\n";
  1337. $output .= " <option " . (($previous_vote == '2') ? "selected='selected'" : '') . " value='2'>2</option>\n";
  1338. $output .= " <option " . (($previous_vote == '3') ? "selected='selected'" : '') . " value='3'>3</option>\n";
  1339. $output .= " <option " . (($previous_vote == '4') ? "selected='selected'" : '') . " value='4'>4</option>\n";
  1340. $output .= " <option " . (($previous_vote == '5') ? "selected='selected'" : '') . " value='5'>5</option>\n";
  1341. $output .= " </select>\n";
  1342. $output .= " <input type='submit' value='" . __( 'Save', 'wpsc' ) . "'>";
  1343. $output .= " </form>";
  1344. return $output;
  1345. }
  1346. /**
  1347. * wpsc currency sign function
  1348. * @return string - the selected currency sign for the store
  1349. */
  1350. function wpsc_currency_sign() {
  1351. _deprecated_function( __FUNCTION__, '3.8', 'the up…

Large files files are truncated, but you can click here to view the full file