PageRenderTime 40ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/wpsc-includes/display.functions.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 562 lines | 441 code | 74 blank | 47 comment | 134 complexity | a7d2d6cad544043be4ff49b3fcb7d53a MD5 | raw file
  1. <?php
  2. /**
  3. * WP eCommerce display functions
  4. *
  5. * These are functions for the wp-eCommerce theme engine, template tags and shortcodes
  6. *
  7. * @package wp-e-commerce
  8. * @since 3.7
  9. */
  10. /**
  11. * wpsc buy now button code products function
  12. * Sorry about the ugly code, this is just to get the functionality back, buy now will soon be overhauled, and this function will then be completely different
  13. * @return string - html displaying one or more products
  14. */
  15. function wpsc_buy_now_button($product_id, $replaced_shortcode = false) {
  16. global $wpdb, $wpsc_query, $wpsc_cart;
  17. $temp_wpsc_query = new WPSC_query(array('product_id' =>$product_id));
  18. list($wpsc_query, $temp_wpsc_query) = array($temp_wpsc_query, $wpsc_query); // swap the wpsc_query objects
  19. // exit('<pre>'.print_r($temp_wpsc_query, true).'</pre>');
  20. $selected_gateways = get_option('custom_gateway_options');
  21. if (in_array('google', (array)$selected_gateways)) {
  22. $output .= google_buynow($product['id']);
  23. } else if (in_array('paypal_multiple', (array)$selected_gateways)) {
  24. if ($product_id > 0){
  25. //$output .= "<pre>".print_r($wpsc_query,true)."</pre>";
  26. while (wpsc_have_products()) :
  27. wpsc_the_product();
  28. $price = calculate_product_price($wpsc_query->product['id'], $wpsc_query->first_variations);
  29. $shipping = $wpsc_query->product['pnp'];
  30. if(wpsc_uses_shipping()){$handling = get_option('base_local_shipping');}else{$handling = $shipping;}
  31. $output .= "<form onsubmit='log_paypal_buynow(this)' target='paypal' action='".get_option('paypal_multiple_url')."' method='post' />
  32. <input type='hidden' name='business' value='".get_option('paypal_multiple_business')."' />
  33. <input type='hidden' name='cmd' value='_xclick' />
  34. <input type='hidden' name='item_name' value='".wpsc_the_product_title()."' />
  35. <input type='hidden' id='item_number' name='item_number' value='".wpsc_the_product_id()."' />
  36. <input type='hidden' id='amount' name='amount' value='".($price+$pnp)."' />
  37. <input type='hidden' id='unit' name='unit' value='".$price."' />
  38. <input type='hidden' id='shipping' name='ship11' value='".$shipping."' />
  39. <input type='hidden' name='handling' value='".$handling."' />
  40. <input type='hidden' name='currency_code' value='".get_option('paypal_curcode')."' />";
  41. if(get_option('multi_add') == 1){
  42. $output .="<label for='quantity'>".__('Quantity','wpsc')."</label>";
  43. $output .="<input type='text' size='4' id='quantity' name='quantity' value='' /><br />";
  44. }else{
  45. $output .="<input type='hidden' name='undefined_quantity' value='0' />";
  46. }
  47. $output .="<input type='image' name='submit' border='0' src='https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif' alt='PayPal - The safer, easier way to pay online' />
  48. <img alt='' border='0' width='1' height='1' src='https://www.paypal.com/en_US/i/scr/pixel.gif' />
  49. </form>\n\r";
  50. endwhile;
  51. }
  52. }
  53. list($temp_wpsc_query, $wpsc_query) = array($wpsc_query, $temp_wpsc_query); // swap the wpsc_query objects back
  54. if($replaced_shortcode == true) {
  55. return $output;
  56. } else {
  57. echo $output;
  58. }
  59. }
  60. function wpsc_also_bought($product_id) {
  61. /*
  62. * Displays products that were bought aling with the product defined by $product_id
  63. * most of it scarcely needs describing
  64. */
  65. global $wpdb;
  66. $siteurl = get_option('siteurl');
  67. if(get_option('wpsc_also_bought') == 0) {
  68. //returns nothing if this is off
  69. return '';
  70. }
  71. // to be made customiseable in a future release
  72. $also_bought_limit = 3;
  73. $element_widths = 96;
  74. $image_display_height = 96;
  75. $image_display_width = 96;
  76. $also_bought = $wpdb->get_results("SELECT `".WPSC_TABLE_PRODUCT_LIST."`.* FROM `".WPSC_TABLE_ALSO_BOUGHT."`, `".WPSC_TABLE_PRODUCT_LIST."` WHERE `selected_product`='".$product_id."' AND `".WPSC_TABLE_ALSO_BOUGHT."`.`associated_product` = `".WPSC_TABLE_PRODUCT_LIST."`.`id` AND `".WPSC_TABLE_PRODUCT_LIST."`.`active` IN('1') AND `".WPSC_TABLE_PRODUCT_LIST."`.`publish` IN ('1')ORDER BY `".WPSC_TABLE_ALSO_BOUGHT."`.`quantity` DESC LIMIT $also_bought_limit",ARRAY_A);
  77. if(count($also_bought) > 0) {
  78. $output = "<h2 class='prodtitles wpsc_also_bought' >".__('People who bought this item also bought', 'wpsc')."</h2>";
  79. $output .= "<div class='wpsc_also_bought'>";
  80. foreach((array)$also_bought as $also_bought_data) {
  81. $output .= "<div class='wpsc_also_bought_item' style='width: ".$element_widths."px;'>";
  82. if(get_option('show_thumbnails') == 1) {
  83. if($also_bought_data['image'] !=null) {
  84. $output .= "<a href='".wpsc_product_url($also_bought_data['id'])."' class='preview_link' rel='".str_replace(" ", "_",$also_bought_data['name'])."'>";
  85. $image_path = "index.php?productid=" . $also_bought_data['id'] . "&amp;width=" . $image_display_width."&amp;height=" . $image_display_height. "";
  86. $output .= "<img src='$image_path' id='product_image_".$also_bought_data['id']."' class='product_image' style='margin-top: ".$margin_top."px'/>";
  87. $output .= "</a>";
  88. } else {
  89. if(get_option('product_image_width') != '') {
  90. $output .= "<img src='".WPSC_URL."/images/no-image-uploaded.gif' title='".$also_bought_data['name']."' alt='".$also_bought_data['name']."' width='$image_display_height' height='$image_display_height' id='product_image_".$also_bought_data['id']."' class='product_image' />";
  91. } else {
  92. $output .= "<img src='".WPSC_URL."/images/no-image-uploaded.gif' title='".$also_bought_data['name']."' alt='".htmlentities(stripslashes($product['name']), ENT_QUOTES, 'UTF-8')."' id='product_image_".$also_bought_data['id']."' class='product_image' />";
  93. }
  94. }
  95. }
  96. $variations_processor = new nzshpcrt_variations;
  97. $variations_output = $variations_processor->display_product_variations($also_bought_data['id'],true, false, true);
  98. //$output .= $variations_output[0];
  99. if($variations_output[1] !== null) {
  100. $also_bought_data['price'] = $variations_output[1];
  101. $also_bought_data['special_price'] = 0;
  102. }
  103. $output .= "<a class='wpsc_product_name' href='".wpsc_product_url($also_bought_data['id'])."'>".$also_bought_data['name']."</a>";
  104. $output .= nzshpcrt_currency_display(($also_bought_data['price'] - $also_bought_data['special_price']), $also_bought_data['notax'],false,$also_bought_data['id']);
  105. //$output .= "<a href='".wpsc_product_url($also_bought_data['id'])."'>".$also_bought_data['name']."</a>";
  106. $output .= "</div>";
  107. }
  108. $output .= "</div>";
  109. $output .= "<br clear='all' />";
  110. }
  111. return $output;
  112. }
  113. function fancy_notifications() {
  114. global $wpdb;
  115. if(get_option('fancy_notifications') == 1) {
  116. $output = "";
  117. $output .= "<div id='fancy_notification'>\n\r";
  118. $output .= " <div id='loading_animation'>\n\r";
  119. $output .= '<img id="fancy_notificationimage" title="Loading" alt="Loading" src="'.WPSC_URL.'/images/indicator.gif" />'.__('Updating', 'wpsc')."...\n\r";
  120. $output .= " </div>\n\r";
  121. $output .= " <div id='fancy_notification_content'>\n\r";
  122. $output .= " </div>\n\r";
  123. $output .= "</div>\n\r";
  124. }
  125. return $output;
  126. }
  127. function fancy_notification_content($cart_messages) {
  128. global $wpdb;
  129. $siteurl = get_option('siteurl');
  130. foreach((array)$cart_messages as $cart_message) {
  131. $output .= "<span>".$cart_message."</span>";
  132. }
  133. $output .= "<a href='".get_option('shopping_cart_url')."' class='go_to_checkout'>".__('Go to Checkout', 'wpsc')."</a>";
  134. $output .= "<a href='#' onclick='jQuery(\"#fancy_notification\").css(\"display\", \"none\"); return false;' class='continue_shopping'>".__('Continue Shopping', 'wpsc')."</a>";
  135. return $output;
  136. }
  137. function wpsc_product_url($product_id, $category_id = null, $escape = true) {
  138. global $wpdb, $wp_rewrite, $wp_query;
  139. if(!is_numeric($category_id) || ($category_id < 1)) {
  140. if(is_numeric($wp_query->query_vars['product_category'])) {
  141. $category_id = $wp_query->query_vars['product_category'];
  142. } else {
  143. $category_list = $wpdb->get_row("SELECT `".WPSC_TABLE_PRODUCT_CATEGORIES."`.`id`, IF((`".WPSC_TABLE_PRODUCT_CATEGORIES."`.`id` = '".get_option('wpsc_default_category')."'), 0, 1) AS `order_state` FROM `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."` , `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."`.`product_id` IN ('".$product_id."') AND `".WPSC_TABLE_ITEM_CATEGORY_ASSOC."`.`category_id` = `".WPSC_TABLE_PRODUCT_CATEGORIES."`.`id` AND `".WPSC_TABLE_PRODUCT_CATEGORIES."`.`active` IN('1') LIMIT 1",ARRAY_A);
  144. $category_id = $category_list['id'];
  145. }
  146. }
  147. // exit('<pre>'.print_r($wp_query, true).'</pre>');
  148. if((($wp_rewrite->rules != null) && ($wp_rewrite != null)) || (get_option('rewrite_rules') != null)) {
  149. $url_name = get_product_meta($product_id, 'url_name', true);
  150. $url_name = htmlentities(stripslashes($url_name), ENT_QUOTES, 'UTF-8');
  151. $product_url =wpsc_category_url($category_id).$url_name."/";
  152. } else {
  153. if(!stristr(get_option('product_list_url'), "?")) {
  154. $initial_seperator = "?";
  155. } else {
  156. $initial_seperator = ($escape) ? "&amp;" : "&";
  157. }
  158. if(is_numeric($category_id) && ($category_id > 0)) {
  159. $product_url = get_option('product_list_url').$initial_seperator."category=".$category_id.(($escape) ? "&amp;" : "&")."product_id=".$product_id;
  160. } else {
  161. $product_url = get_option('product_list_url').$initial_seperator."product_id=".$product_id;
  162. }
  163. }
  164. return $product_url;
  165. }
  166. function google_buynow($product_id) {
  167. global $wpdb;
  168. $output = "";
  169. if ($product_id > 0){
  170. $product_sql = "SELECT * FROM ".WPSC_TABLE_PRODUCT_LIST." WHERE id = ".$product_id." LIMIT 1";
  171. $product_info = $wpdb->get_results($product_sql, ARRAY_A);
  172. $variation_sql = "SELECT * FROM ".WPSC_TABLE_VARIATION_PROPERTIES." WHERE product_id = ".$product_id;
  173. $variation_info = $wpdb->get_results($variation_sql, ARRAY_A);
  174. if (count($variation_info) > 0) {
  175. $variation = 1;
  176. $price = $variation_info[0]['price'];
  177. }
  178. if (get_option('google_server_type')=='production') {
  179. $action_target = "https://checkout.google.com/cws/v2/Merchant/".get_option('google_id')."/checkoutForm";
  180. } else {
  181. $action_target = "https://sandbox.google.com/checkout/cws/v2/Merchant/".get_option('google_id')."/checkoutForm";
  182. }
  183. $product_info = $product_info[0];
  184. $output .= "<form id='BB_BuyButtonForm".$product_id."' onsubmit='log_buynow(this);return true;' action= '".$action_target."' method='post' name='BB_BuyButtonForm".$product_id."'>";
  185. $output .= "<input name='product_id' type='hidden' value='".$product_id."'>";
  186. $output .= "<input name='item_name_1' type='hidden' value='".$product_info['name']."'>";
  187. $output .= "<input name='item_description_1' type='hidden' value='".$product_info['description']."'>";
  188. $output .= "<input name='item_quantity_1' type='hidden' value='1'>";
  189. if ($variation == 1) {
  190. $output .= "<input id='item_price' name='item_price_1' type='hidden' value='".$price."'>";
  191. } else {
  192. if ($product_info['special']=='0') {
  193. $output .= "<input id='item_price' name='item_price_1' type='hidden' value='".$product_info['price']."'>";
  194. } else {
  195. $output .= "<input name='item_price_1' type='hidden' value='".$product_info['special_price']."'>";
  196. }
  197. }
  198. $output .= "<input name='item_currency_1' type='hidden' value='".get_option('google_cur')."'>";
  199. $output .= "<input type='hidden' name='checkout-flow-support.merchant-checkout-flow-support.continue-shopping-url' value='".get_option('product_list_url')."'>";
  200. $output .= "<input type='hidden' name='checkout-flow-support.merchant-checkout-flow-support.edit-cart-url' value='".get_option('shopping_cart_url')."'>";
  201. $output .= "<input alt='' src=' https://checkout.google.com/buttons/buy.gif?merchant_id=".get_option('google_id')."&w=117&h=48&style=trans&variant=text&loc=en_US' type='image'/>";
  202. $output .="</form>";
  203. }
  204. return $output;
  205. }
  206. function external_link($product_id) {
  207. global $wpdb;
  208. $link = get_product_meta($product_id,'external_link',true);
  209. if (!stristr($link,'http://')) {
  210. $link = 'http://'.$link;
  211. }
  212. $output .= "<input type='button' value='".__('Buy Now', 'wpsc')."' onclick='gotoexternallink(\"$link\")'>";
  213. return $output;
  214. }
  215. // displays error messages if the category setup is odd in some way
  216. // needs to be in a function because there are at least three places where this code must be used.
  217. function wpsc_odd_category_setup() {
  218. get_currentuserinfo();
  219. global $userdata;
  220. $output = '';
  221. if(($userdata->wp_capabilities['administrator'] ==1) || ($userdata->user_level >=9)) {
  222. if(get_option('wpsc_default_category') == 1) {
  223. $output = "<p>".__('You are using the example product group as your default group and it has no products in it, you should set the default group to something else, you can do so from your Shop Settings page.', 'wpsc')."</p>";
  224. } else {
  225. $output = "<p>".__('This group is set as your default product group, you should either add some items to it or switch your default product group to one that does contain items.', 'wpsc')."</p>";
  226. }
  227. }
  228. return $output;
  229. }
  230. function wpsc_product_image_html($image_name, $product_id) {
  231. global $wpdb, $wp_query;
  232. if(is_numeric($wp_query->query_vars['product_category'])) {
  233. $category_id = (int)$wp_query->query_vars['product_category'];
  234. } else if (is_numeric($_GET['category'])) {
  235. $category_id = (int)$_GET['category'];
  236. } else {
  237. $category_id = (int)get_option('wpsc_default_category');
  238. }
  239. $product['height'] = get_product_meta($id, 'thumbnail_height');
  240. $product['width'] = get_product_meta($id, 'thumbnail_width');
  241. $use_thumbnail_image = 'false';
  242. if(($product['height'] > $category['height']) || ($product['width'] > $category['width'])) {
  243. $use_thumbnail_image = 'true';
  244. }
  245. //list($category['height'], $category['width']) =
  246. if($category_id > 0) {
  247. $category = $wpdb->get_row("SELECT `image_height` AS `height`, `image_width` AS `width` FROM `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `id` IN ('{$category_id}') LIMIT 1", ARRAY_A);
  248. }
  249. // if there is a height, width, and imagePNG function
  250. if(($category['height'] != null) && ($category['width'] != null) && (function_exists('ImagePNG'))) {
  251. $image_path = "index.php?productid=".$product_id."&amp;thumbnail=".$use_thumbnail_image."&amp;width=".$category['width']."&amp;height=".$category['height']."";
  252. } else {
  253. $image_path = WPSC_THUMBNAIL_URL.$image_name;
  254. if(is_ssl()) {
  255. $image_path = str_replace("http://", "https://", $image_path);
  256. }
  257. }
  258. return $image_path;
  259. }
  260. /* 19-02-09
  261. * add cart button function used for php template tags and shortcodes
  262. */
  263. function wpsc_add_to_cart_button($product_id, $replaced_shortcode = false) {
  264. global $wpdb;
  265. if ($product_id > 0){
  266. if(function_exists('wpsc_theme_html')) {
  267. $product = $wpdb->get_row("SELECT * FROM ".WPSC_TABLE_PRODUCT_LIST." WHERE id = ".$product_id." LIMIT 1", ARRAY_A);
  268. //this needs the results from the product_list table passed to it, does not take just an ID
  269. $wpsc_theme = wpsc_theme_html($product);
  270. }
  271. // grab the variation form fields here
  272. $variations_processor = new nzshpcrt_variations;
  273. $variations_output = $variations_processor->display_product_variations($product_id,false, false, false);
  274. $output .= "<form onsubmit='submitform(this);return false;' action='' method='post'>";
  275. if($variations_output != '') { //will always be set, may sometimes be an empty string
  276. $output .= " <p>".$variations_output."</p>";
  277. }
  278. $output .= "<input type='hidden' name='wpsc_ajax_action' value='add_to_cart' />";
  279. $output .= "<input type='hidden' name='product_id' value='".$product_id."' />";
  280. $output .= "<input type='hidden' name='item' value='".$product_id."' />";
  281. if(isset($wpsc_theme) && is_array($wpsc_theme) && ($wpsc_theme['html'] !='')) {
  282. $output .= $wpsc_theme['html'];
  283. } else {
  284. $output .= "<input type='submit' id='product_".$product['id']."_submit_button' class='wpsc_buy_button' name='Buy' value='".__('Add To Cart', 'wpsc')."' />";
  285. }
  286. $output .= '</form>';
  287. if($replaced_shortcode == true) {
  288. return $output;
  289. } else {
  290. echo $output;
  291. }
  292. }
  293. }
  294. function wpsc_refresh_page_urls($content) {
  295. global $wpdb;
  296. $wpsc_pageurl_option['product_list_url'] = '[productspage]';
  297. $wpsc_pageurl_option['shopping_cart_url'] = '[shoppingcart]';
  298. $check_chekout = $wpdb->get_var("SELECT `guid` FROM `{$wpdb->posts}` WHERE `post_content` LIKE '%[checkout]%' AND `post_type` NOT IN('revision') LIMIT 1");
  299. if($check_chekout != null) {
  300. $wpsc_pageurl_option['checkout_url'] = '[checkout]';
  301. } else {
  302. $wpsc_pageurl_option['checkout_url'] = '[checkout]';
  303. }
  304. $wpsc_pageurl_option['transact_url'] = '[transactionresults]';
  305. $wpsc_pageurl_option['user_account_url'] = '[userlog]';
  306. $changes_made = false;
  307. foreach($wpsc_pageurl_option as $option_key => $page_string) {
  308. $post_id = $wpdb->get_var("SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` IN('page','post') AND `post_content` LIKE '%$page_string%' AND `post_type` NOT IN('revision') LIMIT 1");
  309. $the_new_link = get_permalink($post_id);
  310. if(stristr(get_option($option_key), "https://")) {
  311. $the_new_link = str_replace('http://', "https://",$the_new_link);
  312. }
  313. update_option($option_key, $the_new_link);
  314. }
  315. return $content;
  316. }
  317. function wpsc_product_permalinks($rewrite_rules) {
  318. global $wpdb, $wp_rewrite;
  319. $page_details = $wpdb->get_row("SELECT * FROM `".$wpdb->posts."` WHERE `post_content` LIKE '%[productspage]%' AND `post_type` NOT IN('revision') LIMIT 1", ARRAY_A);
  320. $is_index = false;
  321. if((get_option('page_on_front') == $page_details['ID']) && (get_option('show_on_front') == 'page')) {
  322. $is_index = true;
  323. }
  324. $first_post_name = $page_details['post_name'];
  325. $page_name_array[] = $page_details['post_name'];
  326. if($page_details['post_parent'] > 0) {
  327. $count = 0;
  328. while(($page_details['post_parent'] > 0) && ($count <= 20)) {
  329. $page_details = $wpdb->get_row("SELECT * FROM `".$wpdb->posts."` WHERE `ID` IN('{$page_details['post_parent']}') AND `post_type` NOT IN('revision') LIMIT 1", ARRAY_A);
  330. $page_name_array[] = $page_details['post_name'];
  331. $count ++;
  332. }
  333. }
  334. $page_name_array = array_reverse($page_name_array);
  335. $page_name = implode("/",$page_name_array);
  336. if(!function_exists('wpsc_rewrite_categories')) { // to stop this function from being declared multiple times
  337. /*
  338. * This is the function for making the e-commerce rewrite rules, it is recursive
  339. */
  340. function wpsc_rewrite_categories($page_name, $id = null, $level = 0, $parent_categories = array(), $is_index = false) {
  341. global $wpdb,$category_data;
  342. if($is_index == true) {
  343. $rewrite_page_name = '';
  344. } else {
  345. $rewrite_page_name = $page_name.'/';
  346. }
  347. if(is_numeric($id)) {
  348. $category_sql = "SELECT * FROM `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `active`='1' AND `category_parent` = '".$id."' ORDER BY `id`";
  349. $category_list = $wpdb->get_results($category_sql,ARRAY_A);
  350. } else {
  351. $category_sql = "SELECT * FROM `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `active`='1' AND `category_parent` = '0' ORDER BY `id`";
  352. $category_list = $wpdb->get_results($category_sql,ARRAY_A);
  353. }
  354. if($category_list != null) {
  355. foreach($category_list as $category) {
  356. if($level === 0) {
  357. $parent_categories = array();
  358. }
  359. $parent_categories[] = $category['nice-name'];
  360. $new_rules[($rewrite_page_name.implode($parent_categories,"/").'/?$')] = 'index.php?pagename='.$page_name.'&category_id='.$category['id'];
  361. $new_rules[($rewrite_page_name.implode($parent_categories,"/").'/(.+?)/?$')] = 'index.php?pagename='.$page_name.'&category_id='.$category['id'].'&product_url_name=$matches[1]';
  362. $new_rules[($rewrite_page_name.implode($parent_categories,"/").'/page/([0-9]+)/?$')] = 'index.php?pagename='.$page_name.'&category_id='.$category['id'].'&wpsc_page=$matches[1]';
  363. // recurses here
  364. $sub_rules = wpsc_rewrite_categories($page_name, $category['id'], ($level+1), $parent_categories, $is_index);
  365. array_pop($parent_categories);
  366. $new_rules = array_merge((array)$new_rules, (array)$sub_rules);
  367. }
  368. }
  369. return $new_rules;
  370. }
  371. }
  372. $new_rules = wpsc_rewrite_categories($page_name, null, 0, null, $is_index);
  373. $new_rules = array_reverse((array)$new_rules);
  374. $new_rules[($first_post_name.'/page/([0-9]+)/?$')] = 'index.php?pagename='.$page_name.'&wpsc_page=$matches[1]';
  375. $new_rules[$page_name.'/tag/([A-Za-z0-9\-]+)?$'] = 'index.php?pagename='.$page_name.'&ptag=$matches[1]';
  376. $new_rewrite_rules = array_merge((array)$new_rules,(array)$rewrite_rules);
  377. return $new_rewrite_rules;
  378. }
  379. function wpsc_query_vars($vars) {
  380. // $vars[] = "product_category";
  381. // $vars[] = "product_name";
  382. $vars[] = "category_id";
  383. $vars[] = "product_url_name";
  384. $vars[] = "wpsc_page";
  385. return $vars;
  386. }
  387. add_filter('query_vars', 'wpsc_query_vars');
  388. // using page_rewrite_rules makes it so that odd permalink structures like /%category%/%postname%.htm do not override the plugin permalinks.
  389. add_filter('page_rewrite_rules', 'wpsc_product_permalinks');
  390. /**
  391. * wpsc_obtain_the_title function, for replaacing the page title with the category or product
  392. * @return string - the new page title
  393. */
  394. function wpsc_obtain_the_title() {
  395. global $wpdb, $wp_query, $wpsc_title_data;
  396. $output = null;
  397. if(is_numeric($wp_query->query_vars['category_id'])) {
  398. $category_id = $wp_query->query_vars['category_id'];
  399. } else if($_GET['category']) {
  400. $category_id = absint($_GET['category']);
  401. }
  402. if($category_id > 0) {
  403. if(isset($wpsc_title_data['category'][$category_id])) {
  404. $output = $wpsc_title_data['category'][$category_id];
  405. } else {
  406. $output = $wpdb->get_var("SELECT `name` FROM `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `id`='{$category_id}' LIMIT 1");
  407. $wpsc_title_data['category'][$category_id] = $output;
  408. }
  409. }
  410. if(isset($wp_query->query_vars['product_url_name']) || is_numeric($_GET['product_id'])) {
  411. $product_name = $wp_query->query_vars['product_url_name'];
  412. if(isset($wpsc_title_data['product'][$product_name])) {
  413. $product_list = array();
  414. $full_product_name = $wpsc_title_data['product'][$product_name];
  415. } else if($product_name != '') {
  416. $product_id = $wpdb->get_var("SELECT `product_id` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN ( 'url_name' ) AND `meta_value` IN ( '{$wp_query->query_vars['product_url_name']}' ) ORDER BY `id` DESC LIMIT 1");
  417. $full_product_name = $wpdb->get_var("SELECT `name` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='{$product_id}' LIMIT 1");
  418. $wpsc_title_data['product'][$product_name] = $full_product_name;
  419. } else {
  420. $product_id = absint($_GET['product_id']);
  421. $product_name = $wpdb->get_var("SELECT `meta_value` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN ( 'url_name' ) AND `product_id` IN ( '{$product_id}' ) LIMIT 1");
  422. $full_product_name = $wpdb->get_var("SELECT `name` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='{$product_id}' LIMIT 1");
  423. $wpsc_title_data['product'][$product_name] = $full_product_name;
  424. }
  425. }
  426. //exit("<pre>".print_r($wp_query,true)."</pre>");
  427. if(isset($full_product_name ) && ($full_product_name != null)) {
  428. $output = htmlentities(stripslashes($full_product_name), ENT_QUOTES, 'UTF-8');
  429. }
  430. return $output;
  431. }
  432. function wpsc_obtain_the_description() {
  433. global $wpdb, $wp_query, $wpsc_title_data;
  434. $output = null;
  435. //exit("<pre>".print_r($wp_query,true)."</pre>");
  436. if(is_numeric($wp_query->query_vars['category_id'])) {
  437. $category_id = $wp_query->query_vars['category_id'];
  438. } else if($_GET['category']) {
  439. $category_id = absint($_GET['category']);
  440. }
  441. if(is_numeric($category_id)) {
  442. $output = $wpdb->get_var("SELECT `description` FROM `".WPSC_TABLE_PRODUCT_CATEGORIES."` WHERE `id`='{$category_id}' LIMIT 1");
  443. }
  444. if(isset($wp_query->query_vars['product_url_name'])) {
  445. $product_name = $wp_query->query_vars['product_url_name'];
  446. $product_id = $wpdb->get_var("SELECT `product_id` FROM `".WPSC_TABLE_PRODUCTMETA."` WHERE `meta_key` IN ( 'url_name' ) AND `meta_value` IN ( '{$wp_query->query_vars['product_url_name']}' ) ORDER BY `id` DESC LIMIT 1");
  447. $output = $wpdb->get_var("SELECT `description` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='{$product_id}' LIMIT 1");
  448. } else if(is_numeric($_GET['product_id'])) {
  449. $product_id = absint($_GET['product_id']);
  450. $output = $wpdb->get_var("SELECT `description` FROM `".WPSC_TABLE_PRODUCT_LIST."` WHERE `id`='{$product_id}' LIMIT 1");
  451. }
  452. return $output;
  453. }
  454. function wpsc_replace_the_title($input) {
  455. global $wpdb, $wp_query;
  456. $output = wpsc_obtain_the_title();
  457. if($output != null) {
  458. $backtrace = debug_backtrace();
  459. if($backtrace[3]['function'] == 'get_the_title') {
  460. return $output;
  461. }
  462. }
  463. return $input;
  464. }
  465. function wpsc_replace_wp_title($input) {
  466. global $wpdb, $wp_query;
  467. $output = wpsc_obtain_the_title();
  468. if($output != null) {
  469. return $output;
  470. }
  471. return $input;
  472. }
  473. function wpsc_replace_bloginfo_title($input, $show) {
  474. global $wpdb, $wp_query;
  475. if($show == 'description') {
  476. $output = wpsc_obtain_the_title();
  477. if($output != null) {
  478. return $output;
  479. }
  480. }
  481. return $input;
  482. }
  483. if(get_option('wpsc_replace_page_title') == 1) {
  484. add_filter('the_title', 'wpsc_replace_the_title', 10, 2);
  485. add_filter('wp_title', 'wpsc_replace_wp_title', 10, 2);
  486. //add_filter('bloginfo', 'wpsc_replace_bloginfo_title', 10, 2);
  487. }
  488. ?>