PageRenderTime 69ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-e-commerce/wpsc-core/wpsc-functions.php

https://github.com/AaronFernandes/aquestionof
PHP | 1198 lines | 799 code | 161 blank | 238 comment | 146 complexity | ecc578f7d060d81b3dd6a92b719648fa MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * WP eCommerce core functions
  4. *
  5. * These are core functions for wp-eCommerce
  6. * Things like registering custom post types and taxonomies, rewrite rules, wp_query modifications, link generation and some basic theme finding code is located here
  7. *
  8. * @package wp-e-commerce
  9. * @since 3.8
  10. */
  11. add_filter( 'intermediate_image_sizes_advanced', 'wpsc_intermediate_image_sizes_advanced', 10, 1 );
  12. function wpsc_intermediate_image_sizes_advanced($sizes){
  13. $sizes['small-product-thumbnail']=array(
  14. "width" => get_option( 'product_image_width' ),
  15. "height" => get_option( 'product_image_height' ),
  16. "crop" => get_option( 'wpsc_crop_thumbnails', false )
  17. );
  18. $sizes['medium-single-product']=array(
  19. "width" => get_option( 'single_view_image_width' ),
  20. "height" => get_option( 'single_view_image_height' ),
  21. "crop" => get_option( 'wpsc_crop_thumbnails', false )
  22. );
  23. $sizes['featured-product-thumbnails']=array(
  24. "width" => 425,
  25. "height" => 215,
  26. "crop" => get_option( 'wpsc_crop_thumbnails', true )
  27. );
  28. $sizes['admin-product-thumbnails']=array(
  29. "width" => 38,
  30. "height" => 38,
  31. "crop" => get_option( 'wpsc_crop_thumbnails', true )
  32. );
  33. $sizes['product-thumbnails']=array(
  34. "width" => get_option( 'product_image_width' ),
  35. "height" => get_option( 'product_image_height' ),
  36. "crop" => get_option( 'wpsc_crop_thumbnails', false )
  37. );
  38. $sizes['gold-thumbnails']=array(
  39. "width" => get_option( 'wpsc_gallery_image_width' ),
  40. "height" => get_option( 'wpsc_gallery_image_height' ),
  41. "crop" => get_option( 'wpsc_crop_thumbnails', false )
  42. );
  43. return $sizes;
  44. }
  45. /**
  46. *
  47. * wpsc_core_load_thumbnail_sizes()
  48. *
  49. * Load up the WPEC core thumbnail sizes
  50. * @todo Remove hardcoded sizes
  51. */
  52. function wpsc_core_load_thumbnail_sizes() {
  53. // Add image sizes for products
  54. add_image_size( 'product-thumbnails', get_option( 'product_image_width' ), get_option( 'product_image_height' ), get_option( 'wpsc_crop_thumbnails', false ) );
  55. add_image_size( 'gold-thumbnails', get_option( 'wpsc_gallery_image_width' ), get_option( 'wpsc_gallery_image_height' ), get_option( 'wpsc_crop_thumbnails', false ) );
  56. add_image_size( 'admin-product-thumbnails', 38, 38, get_option( 'wpsc_crop_thumbnails', true ) );
  57. add_image_size( 'featured-product-thumbnails', 425, 215, get_option( 'wpsc_crop_thumbnails', true ) );
  58. add_image_size( 'small-product-thumbnail', get_option( 'product_image_width' ), get_option( 'product_image_height' ), get_option( 'wpsc_crop_thumbnails', false ) );
  59. add_image_size( 'medium-single-product', get_option( 'single_view_image_width' ), get_option( 'single_view_image_height' ), get_option( 'wpsc_crop_thumbnails', false) );
  60. }
  61. /**
  62. * wpsc_core_load_checkout_data()
  63. *
  64. *
  65. */
  66. function wpsc_core_load_checkout_data() {
  67. $form_types = Array(
  68. "Text" => "text",
  69. "Email Address" => "email",
  70. "Street Address" => "address",
  71. "City" => "city",
  72. "Country" => "country",
  73. "Delivery Address" => "delivery_address",
  74. "Delivery City" => "delivery_city",
  75. "Delivery Country" => "delivery_country",
  76. "Text Area" => "textarea",
  77. "Heading" => "heading",
  78. "Select" => "select",
  79. "Radio Button" => "radio",
  80. "Checkbox" => "checkbox"
  81. );
  82. $form_types = apply_filters('wpsc_add_form_types' , $form_types);
  83. update_option('wpsc_checkout_form_fields', $form_types);
  84. $unique_names = Array(
  85. 'billingfirstname',
  86. 'billinglastname',
  87. 'billingaddress',
  88. 'billingcity',
  89. 'billingstate',
  90. 'billingcountry',
  91. 'billingemail',
  92. 'billingphone',
  93. 'billingpostcode',
  94. 'delivertoafriend' ,
  95. 'shippingfirstname' ,
  96. 'shippinglastname' ,
  97. 'shippingaddress' ,
  98. 'shippingcity' ,
  99. 'shippingstate' ,
  100. 'shippingcountry' ,
  101. 'shippingpostcode'
  102. );
  103. $unique_names = apply_filters('wpsc_add_unique_names' , $unique_names);
  104. update_option('wpsc_checkout_unique_names', $unique_names);
  105. }
  106. /**
  107. * wpsc_core_load_purchase_log_statuses()
  108. *
  109. * @global array $wpsc_purchlog_statuses
  110. */
  111. function wpsc_core_load_purchase_log_statuses() {
  112. global $wpsc_purchlog_statuses;
  113. $wpsc_purchlog_statuses = array(
  114. array(
  115. 'internalname' => 'incomplete_sale',
  116. 'label' => __( 'Incomplete Sale', 'wpsc' ),
  117. 'order' => 1,
  118. ),
  119. array(
  120. 'internalname' => 'order_received',
  121. 'label' => __( 'Order Received', 'wpsc' ),
  122. 'order' => 2,
  123. ),
  124. array(
  125. 'internalname' => 'accepted_payment',
  126. 'label' => __( 'Accepted Payment', 'wpsc' ),
  127. 'is_transaction' => true,
  128. 'order' => 3,
  129. ),
  130. array(
  131. 'internalname' => 'job_dispatched',
  132. 'label' => __( 'Job Dispatched', 'wpsc' ),
  133. 'is_transaction' => true,
  134. 'order' => 4,
  135. ),
  136. array(
  137. 'internalname' => 'closed_order',
  138. 'label' => __( 'Closed Order', 'wpsc' ),
  139. 'is_transaction' => true,
  140. 'order' => 5,
  141. ),
  142. array(
  143. 'internalname' => 'declined_payment',
  144. 'label' => __( 'Payment Declined', 'wpsc' ),
  145. 'order' => 6,
  146. ),
  147. );
  148. $wpsc_purchlog_statuses = apply_filters('wpsc_set_purchlog_statuses',$wpsc_purchlog_statuses);
  149. }
  150. /**
  151. * wpsc_core_load_page_titles()
  152. *
  153. * Load the WPEC page titles
  154. *
  155. * @global array $wpsc_page_titles
  156. */
  157. function wpsc_core_load_page_titles() {
  158. global $wpsc_page_titles;
  159. $wpsc_page_titles = wpsc_get_page_post_names();
  160. }
  161. /***
  162. * wpsc_core_load_gateways()
  163. *
  164. * Gets the merchants from the merchants directory and eeds to search the
  165. * merchants directory for merchants, the code to do this starts here.
  166. *
  167. * @todo Come up with a better way to do this than a global $num value
  168. */
  169. function wpsc_core_load_gateways() {
  170. global $nzshpcrt_gateways, $num, $wpsc_gateways,$gateway_checkout_form_fields;
  171. $gateway_directory = WPSC_FILE_PATH . '/wpsc-merchants';
  172. $nzshpcrt_merchant_list = wpsc_list_dir( $gateway_directory );
  173. $num = 0;
  174. foreach ( $nzshpcrt_merchant_list as $nzshpcrt_merchant ) {
  175. if ( stristr( $nzshpcrt_merchant, '.php' ) ) {
  176. require( WPSC_FILE_PATH . '/wpsc-merchants/' . $nzshpcrt_merchant );
  177. }
  178. $num++;
  179. }
  180. unset( $nzshpcrt_merchant );
  181. $nzshpcrt_gateways = apply_filters( 'wpsc_merchants_modules', $nzshpcrt_gateways );
  182. uasort( $nzshpcrt_gateways, 'wpsc_merchant_sort' );
  183. // make an associative array of references to gateway data.
  184. $wpsc_gateways = array();
  185. foreach ( (array)$nzshpcrt_gateways as $key => $gateway )
  186. $wpsc_gateways[$gateway['internalname']] = &$nzshpcrt_gateways[$key];
  187. unset( $key, $gateway );
  188. }
  189. /***
  190. * wpsc_core_load_shipping_modules()
  191. *
  192. * Gets the shipping modules from the shipping directory and needs to search
  193. * the shipping directory for modules.
  194. */
  195. function wpsc_core_load_shipping_modules() {
  196. global $wpsc_shipping_modules;
  197. $shipping_directory = WPSC_FILE_PATH . '/wpsc-shipping';
  198. $nzshpcrt_shipping_list = wpsc_list_dir( $shipping_directory );
  199. foreach ( $nzshpcrt_shipping_list as $nzshpcrt_shipping ) {
  200. if ( stristr( $nzshpcrt_shipping, '.php' ) ) {
  201. require( WPSC_FILE_PATH . '/wpsc-shipping/' . $nzshpcrt_shipping );
  202. }
  203. }
  204. $wpsc_shipping_modules = apply_filters( 'wpsc_shipping_modules', $wpsc_shipping_modules );
  205. }
  206. /**
  207. * Update Notice
  208. *
  209. * Displays an update message below the auto-upgrade link in the WordPress admin
  210. * to notify users that they should check the upgrade information and changelog
  211. * before upgrading in case they need to may updates to their theme files.
  212. *
  213. * @package wp-e-commerce
  214. * @since 3.7.6.1
  215. */
  216. function wpsc_update_notice() {
  217. $info_title = __( 'Please backup your website before updating!', 'wpsc' );
  218. $info_text = __( 'Lots of things have changed in this version. Before updating please backup your database and files in case anything goes wrong.', 'wpsc' );
  219. echo '<div style="border-top:1px solid #CCC; margin-top:3px; padding-top:3px; font-weight:normal;"><strong style="color:#CC0000">' . strip_tags( $info_title ) . '</strong> ' . strip_tags( $info_text, '<br><a><strong><em><span>' ) . '</div>';
  220. }
  221. if ( is_admin() )
  222. add_action( 'in_plugin_update_message-' . WPSC_DIR_NAME . '/wp-shopping-cart.php', 'wpsc_update_notice' );
  223. function wpsc_add_product_price_to_rss() {
  224. global $post;
  225. $price = get_post_meta( $post->ID, '_wpsc_price', true );
  226. echo '<price>' . $price . '</price>';
  227. }
  228. add_action( 'rss2_item', 'wpsc_add_product_price_to_rss' );
  229. add_action( 'rss_item', 'wpsc_add_product_price_to_rss' );
  230. add_action( 'rdf_item', 'wpsc_add_product_price_to_rss' );
  231. /**
  232. * wpsc_register_post_types()
  233. *
  234. * The meat of this whole operation, this is where we register our post types
  235. *
  236. * @global array $wpsc_page_titles
  237. */
  238. function wpsc_register_post_types() {
  239. global $wpsc_page_titles;
  240. $labels = array(
  241. 'name' => _x( 'Products', 'post type name', 'wpsc' ),
  242. 'singular_name' => _x( 'Product', 'post type singular name', 'wpsc' ),
  243. 'add_new' => _x( 'Add New', 'admin menu: add new product', 'wpsc' ),
  244. 'add_new_item' => __('Add New Product', 'wpsc' ),
  245. 'edit_item' => __('Edit Product', 'wpsc' ),
  246. 'new_item' => __('New Product', 'wpsc' ),
  247. 'view_item' => __('View Product', 'wpsc' ),
  248. 'search_items' => __('Search Products', 'wpsc' ),
  249. 'not_found' => __('No products found', 'wpsc' ),
  250. 'not_found_in_trash' => __( 'No products found in Trash', 'wpsc' ),
  251. 'parent_item_colon' => '',
  252. 'menu_name' => __( 'Products', 'wpsc' )
  253. );
  254. // Products
  255. register_post_type( 'wpsc-product', array(
  256. 'capability_type' => 'post',
  257. 'hierarchical' => true,
  258. 'exclude_from_search' => false,
  259. 'public' => true,
  260. 'show_ui' => true,
  261. 'show_in_nav_menus' => true,
  262. 'menu_icon' => WPSC_CORE_IMAGES_URL . "/credit_cards.png",
  263. 'labels' => $labels,
  264. 'query_var' => true,
  265. 'register_meta_box_cb' => 'wpsc_meta_boxes',
  266. 'rewrite' => array(
  267. 'slug' => $wpsc_page_titles['products'] . '/%wpsc_product_category%',
  268. 'with_front' => false
  269. )
  270. ) );
  271. // Purchasable product files
  272. register_post_type( 'wpsc-product-file', array(
  273. 'capability_type' => 'post',
  274. 'hierarchical' => false,
  275. 'exclude_from_search' => true,
  276. 'rewrite' => false
  277. ) );
  278. // Product tags
  279. $labels = array( 'name' => _x( 'Product Tags', 'taxonomy general name', 'wpsc' ),
  280. 'singular_name' => _x( 'Product Tag', 'taxonomy singular name', 'wpsc' ),
  281. 'search_items' => __( 'Product Search Tags', 'wpsc' ),
  282. 'all_items' => __( 'All Product Tags' , 'wpsc'),
  283. 'edit_item' => __( 'Edit Tag', 'wpsc' ),
  284. 'update_item' => __( 'Update Tag', 'wpsc' ),
  285. 'add_new_item' => __( 'Add new Product Tag', 'wpsc' ),
  286. 'new_item_name' => __( 'New Product Tag Name', 'wpsc' ) );
  287. register_taxonomy( 'product_tag', 'wpsc-product', array(
  288. 'hierarchical' => false,
  289. 'labels' => $labels,
  290. 'rewrite' => array(
  291. 'slug' => '/' . sanitize_title_with_dashes( _x( 'tagged', 'slug, part of url', 'wpsc' ) ),
  292. 'with_front' => false )
  293. ) );
  294. // Product categories, is heirarchical and can use permalinks
  295. $labels = array(
  296. 'name' => _x( 'Categories', 'taxonomy general name', 'wpsc' ),
  297. 'singular_name' => _x( 'Product Category', 'taxonomy singular name', 'wpsc' ),
  298. 'search_items' => __( 'Search Product Categories', 'wpsc' ),
  299. 'all_items' => __( 'All Product Categories', 'wpsc' ),
  300. 'parent_item' => __( 'Parent Product Category', 'wpsc' ),
  301. 'parent_item_colon' => __( 'Parent Product Category:', 'wpsc' ),
  302. 'edit_item' => __( 'Edit Product Category', 'wpsc' ),
  303. 'update_item' => __( 'Update Product Category', 'wpsc' ),
  304. 'add_new_item' => __( 'Add New Product Category', 'wpsc' ),
  305. 'new_item_name' => __( 'New Product Category Name', 'wpsc' )
  306. );
  307. register_taxonomy( 'wpsc_product_category', 'wpsc-product', array(
  308. 'hierarchical' => true,
  309. 'rewrite' => array(
  310. 'slug' => $wpsc_page_titles['products'],
  311. 'with_front' => false
  312. ),
  313. 'labels' => $labels
  314. ) );
  315. $labels = array(
  316. 'name' => _x( 'Variations', 'taxonomy general name', 'wpsc' ),
  317. 'singular_name' => _x( 'Variation', 'taxonomy singular name', 'wpsc' ),
  318. 'search_items' => __( 'Search Variations', 'wpsc' ),
  319. 'all_items' => __( 'All Variations', 'wpsc' ),
  320. 'parent_item' => __( 'Parent Variation', 'wpsc' ),
  321. 'parent_item_colon' => __( 'Parent Variations:', 'wpsc' ),
  322. 'edit_item' => __( 'Edit Variation', 'wpsc' ),
  323. 'update_item' => __( 'Update Variation', 'wpsc' ),
  324. 'add_new_item' => __( 'Add New Variation/Set', 'wpsc' ),
  325. 'new_item_name' => __( 'New Variation Name', 'wpsc' ),
  326. );
  327. // Product Variations, is internally heirarchical, externally, two separate types of items, one containing the other
  328. register_taxonomy( 'wpsc-variation', 'wpsc-product', array(
  329. 'hierarchical' => true,
  330. 'query_var' => 'variations',
  331. 'rewrite' => false,
  332. 'public' => true,
  333. 'labels' => $labels
  334. ) );
  335. $role = get_role( 'administrator' );
  336. $role->add_cap( 'read_wpsc-product' );
  337. $role->add_cap( 'read_wpsc-product-file' );
  338. }
  339. add_action( 'init', 'wpsc_register_post_types', 8 );
  340. function wpsc_check_thumbnail_support() {
  341. if ( !current_theme_supports( 'post-thumbnails' ) ) {
  342. add_theme_support( 'post-thumbnails' );
  343. add_action( 'init', 'wpsc_remove_post_type_thumbnail_support' );
  344. }
  345. }
  346. add_action( 'after_setup_theme', 'wpsc_check_thumbnail_support', 99 );
  347. function wpsc_remove_post_type_thumbnail_support() {
  348. remove_post_type_support( 'post', 'thumbnail' );
  349. remove_post_type_support( 'page', 'thumbnail' );
  350. }
  351. /**
  352. * This serializes the shopping cart variable as a backup in case the
  353. * unserialized one gets butchered by various things
  354. */
  355. function wpsc_serialize_shopping_cart() {
  356. global $wpdb, $wpsc_start_time, $wpsc_cart;
  357. if ( is_object( $wpsc_cart ) )
  358. $wpsc_cart->errors = array( );
  359. $_SESSION['wpsc_cart'] = serialize( $wpsc_cart );
  360. return true;
  361. }
  362. add_action( 'shutdown', 'wpsc_serialize_shopping_cart' );
  363. /**
  364. * wpsc_start_the_query
  365. */
  366. function wpsc_start_the_query() {
  367. global $wpsc_page_titles, $wp_query, $wpsc_query, $wpsc_query_vars;
  368. $is_404 = false;
  369. if(isset($wp_query->query_vars['term']) && in_array($wp_query->query_vars['term'], $wpsc_page_titles)){
  370. $wp_query = new WP_Query( 'pagename='.$wpsc_page_titles['products'].'/'.$wp_query->query_vars['term'] );
  371. global $post;
  372. $post = $wp_query->post;
  373. setup_postdata( $post );
  374. }elseif ( null == $wpsc_query ) {
  375. if( ( $wp_query->is_404 && !empty($wp_query->query_vars['paged']) ) || (isset( $wp_query->query['pagename']) && strpos( $wp_query->query['pagename'] , $wpsc_page_titles['products'] ) !== false ) && !isset($wp_query->post)){
  376. //what was this for?
  377. global $post;
  378. $is_404 = true;
  379. if( !isset( $wp_query->query_vars['wpsc_product_category'] ) )
  380. $wp_query = new WP_Query('post_type=wpsc-product&name='.$wp_query->query_vars['name']);
  381. if(isset($wp_query->post->ID))
  382. $post = $wp_query->post;
  383. else
  384. $wpsc_query_vars['wpsc_product_category'] = $wp_query->query_vars['name'];
  385. }
  386. if ( count( $wpsc_query_vars ) <= 1 ) {
  387. $wpsc_query_vars = array(
  388. 'post_status' => 'publish, locked, private',
  389. 'post_parent' => 0,
  390. 'order' => apply_filters('wpsc_product_order','ASC')
  391. );
  392. if($wp_query->query_vars['preview'])
  393. $wpsc_query_vars['post_status'] = 'any';
  394. if( isset( $_GET['product_order'] ) )
  395. $wpsc_query_vars['order'] = $_GET['product_order'];
  396. if(isset($wp_query->query_vars['product_tag'])){
  397. $wpsc_query_vars['product_tag'] = $wp_query->query_vars['product_tag'];
  398. $wpsc_query_vars['taxonomy'] = $wp_query->query_vars['taxonomy'];
  399. $wpsc_query_vars['term'] = $wp_query->query_vars['term'];
  400. }elseif( isset($wp_query->query_vars['wpsc_product_category']) ){
  401. $wpsc_query_vars['wpsc_product_category'] = $wp_query->query_vars['wpsc_product_category'];
  402. $wpsc_query_vars['taxonomy'] = $wp_query->query_vars['taxonomy'];
  403. $wpsc_query_vars['term'] = $wp_query->query_vars['term'];
  404. }else{
  405. $wpsc_query_vars['post_type'] = 'wpsc-product';
  406. $wpsc_query_vars['pagename'] = 'products-page';
  407. }
  408. if(1 == get_option('use_pagination')){
  409. $wpsc_query_vars['nopaging'] = false;
  410. $wpsc_query_vars['posts_per_page'] = get_option('wpsc_products_per_page');
  411. $wpsc_query_vars['paged'] = get_query_var('paged');
  412. if(isset($wpsc_query_vars['paged']) && empty($wpsc_query_vars['paged'])){
  413. $wpsc_query_vars['paged'] = get_query_var('page');
  414. }
  415. }
  416. $orderby = get_option( 'wpsc_sort_by' );
  417. if( isset( $_GET['product_order'] ) )
  418. $orderby = 'title';
  419. switch ( $orderby ) {
  420. case "dragndrop":
  421. $wpsc_query_vars["orderby"] = 'menu_order';
  422. break;
  423. case "name":
  424. $wpsc_query_vars["orderby"] = 'title';
  425. break;
  426. //This only works in WP 3.0.
  427. case "price":
  428. add_filter( 'posts_join', 'wpsc_add_meta_table' );
  429. add_filter( 'posts_where', 'wpsc_add_meta_table_where' );
  430. $wpsc_query_vars["meta_key"] = '_wpsc_price';
  431. $wpsc_query_vars["orderby"] = 'meta_value_num';
  432. break;
  433. case "id":
  434. $wpsc_query_vars["orderby"] = 'ID';
  435. break;
  436. }
  437. add_filter( 'pre_get_posts', 'wpsc_generate_product_query', 11 );
  438. $wpsc_query = new WP_Query( $wpsc_query_vars );
  439. //for 3.1 :|
  440. if(empty($wpsc_query->posts) && isset($wpsc_query->tax_query) && isset($wp_query->query_vars['wpsc_product_category'])){
  441. $wpsc_query_vars = array();
  442. $wpsc_query_vars['wpsc_product_category'] = $wp_query->query_vars['wpsc_product_category'];
  443. if(1 == get_option('use_pagination')){
  444. $wpsc_query_vars['posts_per_page'] = get_option('wpsc_products_per_page');
  445. $wpsc_query_vars['paged'] = get_query_var('paged');
  446. if(empty($wpsc_query_vars['paged']))
  447. $wpsc_query_vars['paged'] = get_query_var('page');
  448. }
  449. $wpsc_query = new WP_Query( $wpsc_query_vars );
  450. }
  451. }
  452. }
  453. if( $is_404 || ( ( isset($wpsc_query->post_count) && $wpsc_query->post_count == 0 ) && isset($wpsc_query_vars['wpsc_product_category'] ) )){
  454. $args = array_merge($wp_query->query, array('posts_per_page' => get_option('wpsc_products_per_page')));
  455. $wp_query = new WP_Query($args);
  456. if( empty( $wp_query->posts ) ){
  457. $product_page_id = wpec_get_the_post_id_by_shortcode('[productspage]');
  458. $wp_query = new WP_Query( 'page_id='.$product_page_id);
  459. }
  460. }
  461. if ( isset( $wp_query->post->ID ) )
  462. $post_id = $wp_query->post->ID;
  463. else
  464. $post_id = 0;
  465. if ( get_permalink( $post_id ) == get_option( 'shopping_cart_url' ) )
  466. $_SESSION['wpsc_has_been_to_checkout'] = true;
  467. }
  468. add_action( 'template_redirect', 'wpsc_start_the_query', 8 );
  469. /**
  470. * add meta table where section for ordering by price
  471. *
  472. */
  473. function wpsc_add_meta_table_where($where){
  474. global $wpdb;
  475. remove_filter( 'posts_where', 'wpsc_add_meta_table_where' );
  476. return $where . ' AND ' . $wpdb->postmeta . '.meta_key = "_wpsc_price"';
  477. }
  478. /**
  479. * add meta table join section for ordering by price
  480. *
  481. */
  482. function wpsc_add_meta_table($join){
  483. global $wpdb;
  484. remove_filter( 'posts_join', 'wpsc_add_meta_table' );
  485. if(strpos($join, "INNER JOIN ON (".$wpdb->posts.".ID = ".$wpdb->postmeta.".post_id)") !== false){
  486. return ' JOIN ' . $wpdb->postmeta . ' ON ' . $wpdb->posts. '.ID = ' . $wpdb->postmeta . '.post_id';
  487. }else{
  488. return $join;
  489. }
  490. }
  491. /**
  492. * wpsc_taxonomy_rewrite_rules function.
  493. * Adds in new rewrite rules for categories, products, category pages, and ambiguities (either categories or products)
  494. * Also modifies the rewrite rules for product URLs to add in the post type.
  495. *
  496. * @since 3.8
  497. * @access public
  498. * @param array $rewrite_rules
  499. * @return array - the modified rewrite rules
  500. */
  501. function wpsc_taxonomy_rewrite_rules( $rewrite_rules ) {
  502. global $wpsc_page_titles;
  503. $products_page = $wpsc_page_titles['products'];
  504. $checkout_page = $wpsc_page_titles['checkout'];
  505. $target_string = "index.php?product";
  506. $replacement_string = "index.php?post_type=wpsc-product&product";
  507. $target_rule_set_query_var = 'products';
  508. $target_rule_set = array( );
  509. foreach ( $rewrite_rules as $rewrite_key => $rewrite_query ) {
  510. if ( stristr( $rewrite_query, "index.php?product" ) ) {
  511. $rewrite_rules[$rewrite_key] = str_replace( $target_string, $replacement_string, $rewrite_query );
  512. }
  513. if ( stristr( $rewrite_query, "$target_rule_set_query_var=" ) ) {
  514. $target_rule_set[] = $rewrite_key;
  515. }
  516. }
  517. $new_rewrite_rules[$products_page . '/(.+?)/product/([^/]+)/comment-page-([0-9]{1,})/?$'] = 'index.php?post_type=wpsc-product&products=$matches[1]&name=$matches[2]&cpage=$matches[3]';
  518. $new_rewrite_rules[$products_page . '/(.+?)/product/([^/]+)/?$'] = 'index.php?post_type=wpsc-product&products=$matches[1]&name=$matches[2]';
  519. $new_rewrite_rules[$products_page . '/(.+?)/([^/]+)/comment-page-([0-9]{1,})/?$'] = 'index.php?post_type=wpsc-product&products=$matches[1]&wpsc_item=$matches[2]&cpage=$matches[3]';
  520. $new_rewrite_rules[$products_page . '/(.+?)/([^/]+)?$'] = 'index.php?post_type=wpsc-product&products=$matches[1]&wpsc_item=$matches[2]';
  521. $last_target_rule = array_pop( $target_rule_set );
  522. $rebuilt_rewrite_rules = array( );
  523. foreach ( $rewrite_rules as $rewrite_key => $rewrite_query ) {
  524. if ( $rewrite_key == $last_target_rule ) {
  525. $rebuilt_rewrite_rules = array_merge( $rebuilt_rewrite_rules, $new_rewrite_rules );
  526. }
  527. $rebuilt_rewrite_rules[$rewrite_key] = $rewrite_query;
  528. }
  529. return $rebuilt_rewrite_rules;
  530. }
  531. add_filter( 'rewrite_rules_array', 'wpsc_taxonomy_rewrite_rules' );
  532. /**
  533. * wpsc_query_vars function.
  534. * adds in the post_type and wpsc_item query vars
  535. *
  536. * @since 3.8
  537. * @access public
  538. * @param mixed $vars
  539. * @return void
  540. */
  541. function wpsc_query_vars( $vars ) {
  542. // post_type is used to specify that we are looking for products
  543. $vars[] = "post_type";
  544. // wpsc_item is used to find items that could be either a product or a product category, it defaults to category, then tries products
  545. $vars[] = "wpsc_item";
  546. return $vars;
  547. }
  548. add_filter( 'query_vars', 'wpsc_query_vars' );
  549. /**
  550. * wpsc_query_modifier function.
  551. *
  552. * @since 3.8
  553. * @access public
  554. * @param object - reference to $wp_query
  555. * @return $query
  556. */
  557. function wpsc_split_the_query( $query ) {
  558. global $wpsc_page_titles, $wpsc_query, $wpsc_query_vars;
  559. // These values are to be dynamically defined
  560. $products_page = $wpsc_page_titles['products'];
  561. $checkout_page = $wpsc_page_titles['checkout'];
  562. $userlog_page = $wpsc_page_titles['userlog'];
  563. $transaction_results_page = $wpsc_page_titles['transaction_results'];
  564. // otherwise, check if we are looking at a product, if so, duplicate the query and swap the old one out for a products page request
  565. // JS - 6.4.1020 - Added is_admin condition, as the products condition broke categories in backend
  566. if ( !empty($query->query_vars['pagename']) && ($query->query_vars['pagename'] == $products_page) || isset( $query->query_vars['products'] ) && !is_admin() ) {
  567. // store a copy of the wordpress query
  568. $wpsc_query_data = $query->query;
  569. // wipe and replace the query vars
  570. $query->query = array();
  571. $query->query['pagename'] = "$products_page";
  572. $query->query_vars['pagename'] = "$products_page";
  573. $query->query_vars['name'] = '';
  574. $query->query_vars['post_type'] = '';
  575. $query->queried_object = & get_page_by_path( $query->query['pagename'] );
  576. if ( !empty( $query->queried_object ) )
  577. $query->queried_object_id = (int)$query->queried_object->ID;
  578. else
  579. unset( $query->queried_object );
  580. unset( $query->query_vars['products'] );
  581. unset( $query->query_vars['name'] );
  582. unset( $query->query_vars['taxonomy'] );
  583. unset( $query->query_vars['term'] );
  584. unset( $query->query_vars['wpsc_item'] );
  585. $query->is_singular = true;
  586. $query->is_page = true;
  587. $query->is_tax = false;
  588. $query->is_archive = false;
  589. $query->is_single = false;
  590. if ( ($wpsc_query_vars == null ) ) {
  591. unset( $wpsc_query_data['pagename'] );
  592. $wpsc_query_vars = $wpsc_query_data;
  593. }
  594. }
  595. add_filter( 'redirect_canonical', 'wpsc_break_canonical_redirects', 10, 2 );
  596. remove_filter( 'pre_get_posts', 'wpsc_split_the_query', 8 );
  597. }
  598. /**
  599. * wpsc_generate_product_query function.
  600. *
  601. * @access public
  602. * @param mixed $query
  603. * @return void
  604. */
  605. function wpsc_generate_product_query( $query ) {
  606. global $wp_query;
  607. $prod_page = wpec_get_the_post_id_by_shortcode('[productspage]');
  608. $prod_page = get_post($prod_page);
  609. remove_filter( 'pre_get_posts', 'wpsc_generate_product_query', 11 );
  610. $query->query_vars['taxonomy'] = null;
  611. $query->query_vars['term'] = null;
  612. // default product selection
  613. if ( $query->query_vars['pagename'] != '' ) {
  614. $query->query_vars['post_type'] = 'wpsc-product';
  615. $query->query_vars['pagename'] = '';
  616. $query->is_page = false;
  617. $query->is_tax = false;
  618. $query->is_archive = true;
  619. $query->is_singular = false;
  620. $query->is_single = false;
  621. }
  622. // If wpsc_item is not null, we are looking for a product or a product category, check for category
  623. if ( isset( $query->query_vars['wpsc_item'] ) && ($query->query_vars['wpsc_item'] != '') ) {
  624. $test_term = get_term_by( 'slug', $query->query_vars['wpsc_item'], 'wpsc_product_category' );
  625. if ( $test_term->slug == $query->query_vars['wpsc_item'] ) {
  626. // if category exists (slug matches slug), set products to value of wpsc_item
  627. $query->query_vars['products'] = $query->query_vars['wpsc_item'];
  628. } else {
  629. // otherwise set name to value of wpsc_item
  630. $query->query_vars['name'] = $query->query_vars['wpsc_item'];
  631. }
  632. }
  633. if ( isset( $query->query_vars['products'] ) && ($query->query_vars['products'] != null) && ($query->query_vars['name'] != null) ) {
  634. unset( $query->query_vars['taxonomy'] );
  635. unset( $query->query_vars['term'] );
  636. $query->query_vars['post_type'] = 'wpsc-product';
  637. $query->is_tax = false;
  638. $query->is_archive = true;
  639. $query->is_singular = false;
  640. $query->is_single = false;
  641. }
  642. if( isset($wp_query->query_vars['wpsc_product_category']) && !isset($wp_query->query_vars['wpsc-product'])){
  643. $query->query_vars['wpsc_product_category'] = $wp_query->query_vars['wpsc_product_category'];
  644. $query->query_vars['taxonomy'] = $wp_query->query_vars['taxonomy'];
  645. $query->query_vars['term'] = $wp_query->query_vars['term'];
  646. }elseif( '' != ($default_category = get_option('wpsc_default_category')) && !isset($wp_query->query_vars['wpsc-product'])){
  647. $default_term = get_term($default_category,'wpsc_product_category');
  648. if(!empty($default_term) && empty($wp_query->query_vars['category_name'])){
  649. $query->query_vars['taxonomy'] = 'wpsc_product_category';
  650. $query->query_vars['term'] = $default_term->slug;
  651. $query->is_tax = true;
  652. }elseif(isset($wp_query->query_vars['name']) && $wp_query->is_404 && $wp_query->query_vars['category_name'] != $prod_page->post_name){
  653. unset( $query->query_vars['taxonomy'] );
  654. unset( $query->query_vars['term'] );
  655. $query->query_vars['wpsc-product'] = $wp_query->query_vars['name'];
  656. $query->query_vars['name'] = $wp_query->query_vars['name'];
  657. }else{
  658. $query->is_tax = true;
  659. $term = get_term_by('slug',$wp_query->query_vars['name'], 'wpsc_product_category' );
  660. if(!empty($term)){
  661. $query->query_vars['taxonomy'] = 'wpsc_product_category';
  662. $query->query_vars['wpsc_product_category__in'] = array($term->term_taxonomy_id);
  663. $query->query_vars['wpsc_product_category'] = $wp_query->query_vars['name'];
  664. $query->query_vars['term'] = $wp_query->query_vars['name'];
  665. }elseif(is_numeric($default_category)){
  666. $query->query_vars['taxonomy'] = 'wpsc_product_category';
  667. }else{
  668. $query->is_tax = false;
  669. }
  670. }
  671. }
  672. //If Product Tag Taxonomy
  673. if (isset($wp_query->query_vars['product_tag']) && $wp_query->query_vars['product_tag']){
  674. $query->query_vars['product_tag'] = $wp_query->query_vars['product_tag'];
  675. $query->query_vars['term'] = $wp_query->query_vars['term'];
  676. $query->query_vars['taxonomy'] = 'product_tag';
  677. $query->is_tax = true;
  678. }
  679. if(1 == get_option('use_pagination')){
  680. $query->query_vars['posts_per_page'] = get_option('wpsc_products_per_page');
  681. if( isset( $_GET['items_per_page'] ) ) {
  682. if ( is_numeric( $_GET['items_per_page'] ) )
  683. $query->query_vars['posts_per_page'] = (int) $_GET['items_per_page'];
  684. elseif ( $_GET['items_per_page'] == 'all' )
  685. $query->query_vars['posts_per_page'] = -1;
  686. }
  687. } else {
  688. $query->query_vars['posts_per_page'] = '-1';
  689. }
  690. if ( $query->is_tax == true )
  691. new wpsc_products_by_category( $query );
  692. return $query;
  693. }
  694. function wpsc_mark_product_query( $query ) {
  695. if ( isset( $query->query_vars['post_type'] ) && ($query->query_vars['post_type'] == 'wpsc-product') )
  696. $query->is_product = true;
  697. return $query;
  698. }
  699. add_filter( 'pre_get_posts', 'wpsc_split_the_query', 8 );
  700. add_filter( 'parse_query', 'wpsc_mark_product_query', 12 );
  701. /**
  702. * wpsc_products_by_category class.
  703. *
  704. */
  705. class wpsc_products_by_category {
  706. var $sql_components = array( );
  707. /**
  708. * wpsc_products_by_category function.
  709. *
  710. * @access public
  711. * @param mixed $query
  712. * @return void
  713. */
  714. function wpsc_products_by_category( $query ) {
  715. global $wpdb;
  716. $q = $query->query_vars;
  717. // Category stuff for nice URLs
  718. if ( !empty( $q['wpsc_product_category'] ) && !$query->is_singular ) {
  719. $q['taxonomy'] = 'wpsc_product_category';
  720. $q['term'] = $q['wpsc_product_category'];
  721. $in_cats = '';
  722. $join = " INNER JOIN $wpdb->term_relationships
  723. ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)
  724. INNER JOIN $wpdb->term_taxonomy
  725. ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
  726. ";
  727. if(isset($q['meta_key']))
  728. $join .= " INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) ";
  729. $whichcat = " AND $wpdb->term_taxonomy.taxonomy = '{$q['taxonomy']}' ";
  730. $term_data = get_term_by( 'slug', $q['term'], $q['taxonomy'] );
  731. if( is_object( $term_data ) )
  732. $in_cats = array( $term_data->term_id );
  733. if('0' != get_option('show_subcatsprods_in_cat') && is_object($term_data)){
  734. $term_children_data = get_term_children( $term_data->term_id, $q['taxonomy'] );
  735. $in_cats = array_reverse( array_merge( $in_cats, $term_children_data ) );
  736. }
  737. if( is_array( $in_cats ) ){
  738. $in_cats = "'" . implode( "', '", $in_cats ) . "'";
  739. $whichcat .= "AND $wpdb->term_taxonomy.term_id IN ($in_cats)";
  740. }
  741. $whichcat .= " AND $wpdb->posts.post_status IN ('publish', 'locked', 'private') ";
  742. $groupby = "{$wpdb->posts}.ID";
  743. $this->sql_components['join'] = $join;
  744. $this->sql_components['fields'] = "{$wpdb->posts}.*, {$wpdb->term_taxonomy}.term_id";
  745. $this->sql_components['group_by'] = $groupby;
  746. //what about ordering by price
  747. if(isset($q['meta_key']) && '_wpsc_price' == $q['meta_key']){
  748. $whichcat .= " AND $wpdb->postmeta.meta_key = '_wpsc_price'";
  749. }else{
  750. $this->sql_components['order_by'] = "{$wpdb->term_taxonomy}.term_id";
  751. }
  752. $this->sql_components['where'] = $whichcat;
  753. add_filter( 'posts_join', array( &$this, 'join_sql' ) );
  754. add_filter( 'posts_where', array( &$this, 'where_sql' ) );
  755. add_filter( 'posts_fields', array( &$this, 'fields_sql' ) );
  756. add_filter( 'posts_orderby', array( &$this, 'order_by_sql' ) );
  757. add_filter( 'posts_groupby', array( &$this, 'group_by_sql' ) );
  758. }
  759. }
  760. function join_sql( $sql ) {
  761. if ( isset( $this->sql_components['join'] ) )
  762. $sql = $this->sql_components['join'];
  763. remove_filter( 'posts_join', array( &$this, 'join_sql' ) );
  764. return $sql;
  765. }
  766. function where_sql( $sql ) {
  767. if ( isset( $this->sql_components['where'] ) )
  768. $sql = $this->sql_components['where'];
  769. remove_filter( 'posts_where', array( &$this, 'where_sql' ) );
  770. return $sql;
  771. }
  772. function order_by_sql( $sql ) {
  773. $order_by_parts = array( );
  774. $order_by_parts[] = $sql;
  775. if ( isset( $this->sql_components['order_by'] ) )
  776. $order_by_parts[] = $this->sql_components['order_by'];
  777. $order_by_parts = array_reverse( $order_by_parts );
  778. $sql = implode( ',', $order_by_parts );
  779. remove_filter( 'posts_orderby', array( &$this, 'order_by_sql' ) );
  780. return $sql;
  781. }
  782. function fields_sql( $sql ) {
  783. if ( isset( $this->sql_components['fields'] ) )
  784. $sql = $this->sql_components['fields'];
  785. remove_filter( 'posts_fields', array( &$this, 'fields_sql' ) );
  786. return $sql;
  787. }
  788. function group_by_sql( $sql ) {
  789. if ( isset( $this->sql_components['group_by'] ) )
  790. $sql = $this->sql_components['group_by'];
  791. remove_filter( 'posts_groupby', array( &$this, 'group_by_sql' ) );
  792. return $sql;
  793. }
  794. function request_sql( $sql ) {
  795. echo $sql . "<br />";
  796. remove_filter( 'posts_request', array( &$this, 'request_sql' ) );
  797. return $sql;
  798. }
  799. }
  800. function wpsc_break_canonical_redirects( $redirect_url, $requested_url ) {
  801. global $wp_query;
  802. if ( ( isset( $wp_query->query_vars['products'] ) && ($wp_query->query_vars['products'] != '') ) || ( isset( $wp_query->query_vars['products'] ) && $wp_query->query_vars['products'] != 'wpsc_item') )
  803. return false;
  804. if ( stristr( $requested_url, $redirect_url ) )
  805. return false;
  806. return $redirect_url;
  807. }
  808. /**
  809. * wpsc_is_product function.
  810. *
  811. * @since 3.8
  812. * @access public
  813. * @return boolean
  814. */
  815. function wpsc_is_product() {
  816. global $wp_query, $rewrite_rules;
  817. $tmp = false;
  818. if ( isset( $wp_query->is_product ) )
  819. $tmp = $wp_query->is_product;
  820. return $tmp;
  821. }
  822. /**
  823. * wpsc_is_product function.
  824. *
  825. * @since 3.8
  826. * @access public
  827. * @return boolean
  828. */
  829. function wpsc_is_checkout() {
  830. global $wp_query, $rewrite_rules;
  831. $tmp = false;
  832. if ( isset( $wp_query->is_checkout ) )
  833. $tmp = $wp_query->is_checkout;
  834. return $tmp;
  835. }
  836. /**
  837. * wpsc_product_link function.
  838. * Gets the product link, hooks into post_link
  839. * Uses the currently selected, only associated or first listed category for the term URL
  840. * If the category slug is the same as the product slug, it prefixes the product slug with "product/" to counteract conflicts
  841. *
  842. * @access public
  843. * @return void
  844. */
  845. function wpsc_product_link( $permalink, $post, $leavename ) {
  846. global $wp_query, $wpsc_page_titles;
  847. $term_url = '';
  848. $rewritecode = array(
  849. '%wpsc_product_category%',
  850. $leavename ? '' : '%postname%',
  851. );
  852. if ( is_object( $post ) ) {
  853. // In wordpress 2.9 we got a post object
  854. $post_id = $post->ID;
  855. } else {
  856. // In wordpress 3.0 we get a post ID
  857. $post_id = $post;
  858. $post = get_post( $post_id );
  859. }
  860. // Only applies to WPSC products, don't stop on permalinks of other CPTs
  861. // Fixes http://code.google.com/p/wp-e-commerce/issues/detail?id=271
  862. if ($post->post_type != 'wpsc-product')
  863. return $permalink;
  864. $permalink_structure = get_option( 'permalink_structure' );
  865. // This may become customiseable later
  866. $our_permalink_structure = $wpsc_page_titles['products'] . "/%wpsc_product_category%/%postname%/";
  867. // Mostly the same conditions used for posts, but restricted to items with a post type of "wpsc-product "
  868. if ( '' != $permalink_structure && !in_array( $post->post_status, array( 'draft', 'pending' ) ) ) {
  869. $product_categories = wp_get_object_terms( $post_id, 'wpsc_product_category' );
  870. $product_category_slugs = array( );
  871. foreach ( $product_categories as $product_category ) {
  872. $product_category_slugs[] = $product_category->slug;
  873. }
  874. // If the product is associated with multiple categories, determine which one to pick
  875. if ( count( $product_categories ) == 0 ) {
  876. $category_slug = 'uncategorized';
  877. } elseif ( count( $product_categories ) > 1 ) {
  878. if ( (isset( $wp_query->query_vars['products'] ) && $wp_query->query_vars['products'] != null) && in_array( $wp_query->query_vars['products'], $product_category_slugs ) ) {
  879. $product_category = $wp_query->query_vars['products'];
  880. } else {
  881. if(isset($wp_query->query_vars['wpsc_product_category']))
  882. $link = $wp_query->query_vars['wpsc_product_category'];
  883. else
  884. $link = $product_categories[0]->slug;
  885. $product_category = $link;
  886. }
  887. $category_slug = $product_category;
  888. $term_url = get_term_link( $category_slug, 'wpsc_product_category' );
  889. } else {
  890. // If the product is associated with only one category, we only have one choice
  891. if ( !isset( $product_categories[0] ) )
  892. $product_categories[0] = '';
  893. $product_category = $product_categories[0];
  894. if ( !is_object( $product_category ) )
  895. $product_category = new stdClass();
  896. if ( !isset( $product_category->slug ) )
  897. $product_category->slug = null;
  898. $category_slug = $product_category->slug;
  899. $term_url = get_term_link( $category_slug, 'wpsc_product_category' );
  900. }
  901. $post_name = $post->post_name;
  902. /*
  903. if ( in_array( $post_name, $product_category_slugs ) )
  904. $post_name = "product/{$post_name}";
  905. */
  906. if(isset($category_slug) && empty($category_slug)) $category_slug = 'product';
  907. $rewritereplace = array(
  908. $category_slug,
  909. $post_name
  910. );
  911. $permalink = str_replace( $rewritecode, $rewritereplace, $our_permalink_structure );
  912. $permalink = user_trailingslashit( $permalink, 'single' );
  913. $permalink = home_url( $permalink );
  914. }
  915. return $permalink;
  916. }
  917. add_filter( 'post_type_link', 'wpsc_product_link', 10, 3 );
  918. /**
  919. * wpsc_get_product_template function.
  920. *
  921. * @since 3.8
  922. * @access public
  923. * @return void
  924. */
  925. function wpsc_get_template( $template ) {
  926. return get_query_template( $template );
  927. }
  928. /**
  929. * wpsc_product_template_fallback function.
  930. *
  931. * @since 3.8
  932. * @access public
  933. * @param mixed $template_path
  934. * @return string - the corrected template path
  935. */
  936. function wpsc_template_fallback( $template_path ) {
  937. $prospective_file_name = basename( "{$template_path}.php" );
  938. $prospective_file_path = trailingslashit( WPSC_CORE_THEME_PATH ) . $prospective_file_name;
  939. if ( !file_exists( $prospective_file_path ) )
  940. exit( $prospective_file_path );
  941. return $prospective_file_path;
  942. }
  943. function wpsc_products_template_fallback() {
  944. return wpsc_template_fallback( 'products' );
  945. }
  946. function wpsc_checkout_template_fallback() {
  947. return wpsc_template_fallback( 'checkout' );
  948. }
  949. /**
  950. * wpsc_get_page_post_names function.
  951. * Seems that using just one SQL query and then processing the results is probably going to be around as efficient as just doing three separate queries
  952. * But using three queries is a hell of a lot simpler to write and easier to read.
  953. * @since 3.8
  954. * @access public
  955. * @return void
  956. */
  957. function wpsc_get_page_post_names() {
  958. global $wpdb;
  959. $wpsc_page['products'] = $wpdb->get_var( "SELECT post_name FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%[productspage]%' AND `post_type` = 'page' LIMIT 1" );
  960. $wpsc_page['checkout'] = $wpdb->get_var( "SELECT post_name FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%[shoppingcart]%' AND `post_type` = 'page' LIMIT 1" );
  961. $wpsc_page['transaction_results'] = $wpdb->get_var( "SELECT post_name FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%[transactionresults]%' AND `post_type` = 'page' LIMIT 1" );
  962. $wpsc_page['userlog'] = $wpdb->get_var( "SELECT post_name FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%[userlog]%' AND `post_type` = 'page' LIMIT 1" );
  963. return $wpsc_page;
  964. }
  965. /**
  966. * wpsc_template_loader function.
  967. *
  968. * @since 3.8
  969. * @access public
  970. * @return void
  971. */
  972. function wpsc_template_loader() {
  973. global $wp_query;
  974. if ( wpsc_is_product() && $template = wpsc_get_template( 'products' ) ) {
  975. include( $template );
  976. exit();
  977. }
  978. if ( wpsc_is_checkout() && $template = wpsc_get_template( 'checkout' ) ) {
  979. include( $template );
  980. exit();
  981. }
  982. }
  983. /**
  984. * select_wpsc_theme_functions function, provides a place to override the e-commece theme path
  985. * add to switch "theme's functions file
  986. * Š with xiligroup dev
  987. */
  988. function wpsc_select_theme_functions() {
  989. $selected_theme = get_option( 'wpsc_selected_theme' );
  990. if ( !empty( $selected_theme ) && file_exists( WPSC_CORE_THEME_PATH . '/' . WPSC_THEME_DIR . '.php' ) )
  991. include_once( WPSC_CORE_THEME_PATH . '/' . WPSC_THEME_DIR . '.php' );
  992. }
  993. add_action( 'wp', 'wpsc_select_theme_functions', 10, 1 );
  994. /**
  995. * if the user is on a checkout page, force SSL if that option is so set
  996. */
  997. function wpsc_force_ssl() {
  998. global $wp_query;
  999. if ( get_option( 'wpsc_force_ssl' ) && !is_ssl() && strpos( $wp_query->post->post_content, '[shoppingcart]' ) !== FALSE ) {
  1000. $sslurl = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
  1001. header( 'Location: ' . $sslurl );
  1002. echo 'Redirecting';
  1003. }
  1004. }
  1005. add_action( 'get_header', 'wpsc_force_ssl' );
  1006. /**
  1007. * Disable SSL validation for Curl. Added/removed on a per need basis, like so:
  1008. *
  1009. * add_filter('http_api_curl', 'wpsc_curl_ssl');
  1010. * remove_filter('http_api_curl', 'wpsc_curl_ssl');
  1011. *
  1012. * @param resource $ch
  1013. * @return resource $ch
  1014. **/
  1015. function wpsc_curl_ssl($ch) {
  1016. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  1017. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  1018. return $ch;
  1019. }
  1020. /**
  1021. * wpsc_add_https_to_page_url_options( $url )
  1022. *
  1023. * Forces SSL onto option URLs
  1024. *
  1025. * @param string $url
  1026. * @return string
  1027. */
  1028. function wpsc_add_https_to_page_url_options( $url ) {
  1029. return str_replace( 'http://', 'https://', $url );
  1030. }
  1031. if ( is_ssl() ) {
  1032. add_filter( 'option_product_list_url', 'wpsc_add_https_to_page_url_options' );
  1033. add_filter( 'option_shopping_cart_url', 'wpsc_add_https_to_page_url_options' );
  1034. add_filter( 'option_transact_url', 'wpsc_add_https_to_page_url_options' );
  1035. add_filter( 'option_user_account_url', 'wpsc_add_https_to_page_url_options' );
  1036. }
  1037. ?>