PageRenderTime 66ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wpsc-includes/theme.functions.php

https://bitbucket.org/igorstama/wp-e-commerce
PHP | 1489 lines | 962 code | 235 blank | 292 comment | 283 complexity | 75ad7842ede579aab7663d2cd79220a8 MD5 | raw file

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

  1. <?php
  2. /**
  3. * WP eCommerce theme functions
  4. *
  5. * These are the functions for the wp-eCommerce theme engine
  6. *
  7. * @package wp-e-commerce
  8. * @since 3.7
  9. */
  10. /**
  11. * wpsc_register_theme_file( $file_name )
  12. *
  13. * Adds a file name to a global list of
  14. *
  15. * @param string $file_name Name of file to add to global list of files
  16. */
  17. function wpsc_register_theme_file( $file_name ) {
  18. global $wpec_theme_files;
  19. if ( !in_array( $file_name, (array)$wpec_theme_files ) )
  20. $wpec_theme_files[] = $file_name;
  21. }
  22. /**
  23. * wpsc_get_theme_files()
  24. *
  25. * Returns the global wpec_theme_files
  26. *
  27. * @global array $wpec_theme_files
  28. * @return array
  29. */
  30. function wpsc_get_theme_files() {
  31. global $wpec_theme_files;
  32. if ( empty( $wpec_theme_files ) )
  33. return array();
  34. else
  35. return apply_filters( 'wpsc_get_theme_files', (array)array_values( $wpec_theme_files ) );
  36. }
  37. /**
  38. * wpsc_register_core_theme_files()
  39. *
  40. * Registers the core WPEC files into the global array
  41. */
  42. function wpsc_register_core_theme_files() {
  43. wpsc_register_theme_file( 'wpsc-single_product.php' );
  44. wpsc_register_theme_file( 'wpsc-grid_view.php' );
  45. wpsc_register_theme_file( 'wpsc-list_view.php' );
  46. wpsc_register_theme_file( 'wpsc-products_page.php' );
  47. wpsc_register_theme_file( 'wpsc-shopping_cart_page.php' );
  48. wpsc_register_theme_file( 'wpsc-transaction_results.php' );
  49. wpsc_register_theme_file( 'wpsc-user-log.php' );
  50. wpsc_register_theme_file( 'wpsc-cart_widget.php' );
  51. wpsc_register_theme_file( 'wpsc-featured_product.php' );
  52. wpsc_register_theme_file( 'wpsc-category-list.php' );
  53. wpsc_register_theme_file( 'wpsc-category_widget.php' );
  54. // Let other plugins register their theme files
  55. do_action( 'wpsc_register_core_theme_files' );
  56. }
  57. add_action( 'init', 'wpsc_register_core_theme_files' );
  58. /**
  59. * wpsc_flush_theme_transients()
  60. *
  61. * This function will delete the temporary values stored in WordPress transients
  62. * for all of the additional WPEC theme files and their locations. This is
  63. * mostly used when the active theme changes, or when files are moved around. It
  64. * does a complete flush of all possible path/url combinations of files.
  65. *
  66. * @uses wpsc_get_theme_files
  67. */
  68. function wpsc_flush_theme_transients( $force = false ) {
  69. if ( true === $force || isset( $_REQUEST['wpsc_flush_theme_transients'] ) && !empty( $_REQUEST['wpsc_flush_theme_transients'] ) ) {
  70. // Loop through current theme files and remove transients
  71. if ( $theme_files = wpsc_get_theme_files() ) {
  72. foreach( $theme_files as $file ) {
  73. delete_transient( WPEC_TRANSIENT_THEME_PATH_PREFIX . $file );
  74. delete_transient( WPEC_TRANSIENT_THEME_URL_PREFIX . $file );
  75. }
  76. delete_transient( 'wpsc_theme_path' );
  77. return true;
  78. }
  79. }
  80. // No files were registered so return false
  81. return false;
  82. }
  83. add_action( 'wpsc_move_theme', 'wpsc_flush_theme_transients', 10, true );
  84. add_action( 'wpsc_switch_theme', 'wpsc_flush_theme_transients', 10, true );
  85. add_action( 'switch_theme', 'wpsc_flush_theme_transients', 10, true );
  86. /**
  87. * wpsc_check_theme_location()
  88. *
  89. * Check theme location, compares the active theme and the themes within WPSC_CORE_THEME_PATH
  90. * finds files of the same name.
  91. *
  92. * @access public
  93. * @since 3.8
  94. * @param null
  95. * @return $results (Array) of Files OR false if no similar files are found
  96. */
  97. function wpsc_check_theme_location() {
  98. // Get the current theme
  99. $current_theme = get_stylesheet_directory();
  100. // Load up the files in the current theme
  101. $current_theme_files = wpsc_list_product_templates( $current_theme . '/' );
  102. // Load up the files in the wpec themes folder
  103. $wpsc_template_files = wpsc_list_product_templates( WPSC_CORE_THEME_PATH );
  104. // Compare the two
  105. $results = array_intersect( $current_theme_files, $wpsc_template_files );
  106. // Return the differences
  107. if ( count( $results ) > 0 )
  108. return $results;
  109. // No differences so return false
  110. else
  111. return false;
  112. }
  113. /**
  114. * wpsc_list_product_templates( $path = '' )
  115. *
  116. * Lists the files within the WPSC_CORE_THEME_PATH directory
  117. *
  118. * @access public
  119. * @since 3.8
  120. * @param $path - you can provide a path to find the files within it
  121. * @return $templates (Array) List of files
  122. */
  123. function wpsc_list_product_templates( $path = '' ) {
  124. $selected_theme = get_option( 'wpsc_selected_theme' );
  125. // If no path, then try to make some assuptions
  126. if ( empty( $path ) ) {
  127. if ( file_exists( WPSC_OLD_THEMES_PATH . $selected_theme . '/' . $selected_theme . '.css' ) ) {
  128. $path = WPSC_OLD_THEMES_PATH . $selected_theme . '/';
  129. } else {
  130. $path = WPSC_CORE_THEME_PATH;
  131. }
  132. }
  133. // Open the path and get the file names
  134. $dh = opendir( $path );
  135. while ( ( $file = readdir( $dh ) ) !== false ) {
  136. if ( $file != "." && $file != ".." && !strstr( $file, ".svn" ) && !strstr( $file, "images" ) && is_file( $path . $file ) ) {
  137. $templates[] = $file;
  138. }
  139. }
  140. // Return template names
  141. return $templates;
  142. }
  143. /**
  144. * Displays the theme upgrade notice
  145. * @access public
  146. *
  147. * @since 3.8
  148. * @param null
  149. * @return null
  150. */
  151. function wpsc_theme_upgrade_notice() { ?>
  152. <div id="message" class="updated fade">
  153. <p><?php printf( __( '<strong>WP e-Commerce is ready</strong>. If you plan on editing the look of your site, you should <a href="%1s">update your active theme</a> to include the additional WP e-Commerce files. <a href="%2s">Click here</a> to ignore and remove this box.', 'wpsc' ), admin_url( 'admin.php?page=wpsc-settings&tab=presentation' ), admin_url( 'admin.php?page=wpsc-settings&tab=presentation&wpsc_notices=theme_ignore' ) ) ?></p>
  154. </div>
  155. <?php
  156. }
  157. /**
  158. * Displays the database update notice
  159. * @access public
  160. *
  161. * @since 3.8
  162. * @param null
  163. * @return null
  164. */
  165. function wpsc_database_update_notice() { ?>
  166. <div class="error fade">
  167. <p><?php printf( __( '<strong>Your WP e-Commerce data needs to be updated</strong>. You\'ve upgraded from a previous version of the WP e-Commerce plugin, and your store needs updating.<br>You should <a href="%1s">update your database</a> for your store to continue working.', 'wpsc' ), admin_url( 'index.php?page=wpsc-update' ) ) ?></p>
  168. </div>
  169. <?php
  170. }
  171. function wpsc_theme_admin_notices() {
  172. // Database update notice is most important
  173. if ( get_option ( 'wpsc_version' ) < 3.8 ) {
  174. add_action ( 'admin_notices', 'wpsc_database_update_notice' );
  175. // If that's not an issue check if theme updates required
  176. } else {
  177. if ( get_option('wpsc_ignore_theme','') == '' ) {
  178. add_option('wpsc_ignore_theme',false);
  179. }
  180. if (!get_option('wpsc_ignore_theme')) {
  181. add_action( 'admin_notices', 'wpsc_theme_upgrade_notice' );
  182. }
  183. }
  184. // Flag config inconsistencies
  185. if ( 1 == get_option( 'require_register' ) && 1 != get_option( 'users_can_register' )) {
  186. add_action( 'admin_notices', 'wpsc_turn_on_wp_register' );
  187. }
  188. }
  189. add_action('admin_init','wpsc_theme_admin_notices');
  190. function wpsc_turn_on_wp_register() {?>
  191. <div id="message" class="updated fade">
  192. <p><?php printf( __( '<strong>Store Settings</strong>: You have set \'users must register before checkout\', for this to work you need to check \'Anyone can register\' in your WordPress <a href="%1s">General Settings</a>.', 'wpsc' ), admin_url( 'options-general.php' ) ) ?></p>
  193. </div>
  194. <?php
  195. }
  196. if ( isset( $_REQUEST['wpsc_notices'] ) && $_REQUEST['wpsc_notices'] == 'theme_ignore' ) {
  197. update_option( 'wpsc_ignore_theme', true );
  198. wp_redirect( remove_query_arg( 'wpsc_notices' ) );
  199. }
  200. /**
  201. * wpsc_get_template_file_url( $file )
  202. *
  203. * Checks the active theme folder for the particular file, if it exists then
  204. * return the active theme url, otherwise return the global wpsc_theme_url
  205. *
  206. * @access public
  207. * @since 3.8
  208. * @param $file string filename
  209. * @return PATH to the file
  210. */
  211. function wpsc_get_template_file_url( $file = '' ) {
  212. // If we're not looking for a file, do not proceed
  213. if ( empty( $file ) )
  214. return;
  215. // No cache, so find one and set it
  216. if ( false === ( $file_url = get_transient( WPEC_TRANSIENT_THEME_URL_PREFIX . $file ) ) ) {
  217. // Look for file in stylesheet
  218. if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  219. $file_url = get_stylesheet_directory_uri() . '/' . $file;
  220. // Look for file in template
  221. } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
  222. $file_url = get_template_directory_uri() . '/' . $file;
  223. // Backwards compatibility
  224. } else {
  225. // Look in old theme url
  226. $selected_theme_check = WPSC_OLD_THEMES_PATH . get_option( 'wpsc_selected_theme' ) . '/' . str_ireplace( 'wpsc-', '', $file );
  227. // Check the selected theme
  228. if ( file_exists( $selected_theme_check ) ) {
  229. $file_url = WPSC_OLD_THEMES_URL . get_option( 'wpsc_selected_theme' ) . '/' . str_ireplace( 'wpsc-', '', $file );
  230. // Use the bundled theme CSS
  231. } else {
  232. $file_url = WPSC_CORE_THEME_URL . $file;
  233. }
  234. }
  235. // Save the transient and update it every 12 hours
  236. if ( !empty( $file_url ) )
  237. set_transient( WPEC_TRANSIENT_THEME_URL_PREFIX . $file, $file_url, 60 * 60 * 12 );
  238. }else{
  239. delete_transient(WPEC_TRANSIENT_THEME_URL_PREFIX . $file);
  240. wpsc_get_template_file_url($file);
  241. }
  242. if( is_ssl() && !strstr( $file_url, 'https' ) ) $file_url = str_replace('http', 'https', $file_url);
  243. // Return filtered result
  244. return apply_filters( WPEC_TRANSIENT_THEME_URL_PREFIX . $file, $file_url );
  245. }
  246. /**
  247. * Checks the active theme folder for the particular file, if it exists then return the active theme directory otherwise
  248. * return the global wpsc_theme_path
  249. * @access public
  250. *
  251. * @since 3.8
  252. * @param $file string filename
  253. * @return PATH to the file
  254. */
  255. function wpsc_get_template_file_path( $file = '' ){
  256. // If we're not looking for a file, do not proceed
  257. if ( empty( $file ) )
  258. return;
  259. // No cache, so find one and set it
  260. if ( false === ( $file_path = get_transient( WPEC_TRANSIENT_THEME_PATH_PREFIX . $file ) ) ) {
  261. // Look for file in stylesheet
  262. if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
  263. $file_path = get_stylesheet_directory() . '/' . $file;
  264. // Look for file in template
  265. } elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
  266. $file_path = get_template_directory() . '/' . $file;
  267. // Backwards compatibility
  268. } else {
  269. // Look in old theme path
  270. $selected_theme_check = WPSC_OLD_THEMES_PATH . get_option( 'wpsc_selected_theme' ) . '/' . str_ireplace( 'wpsc-', '', $file );
  271. // Check the selected theme
  272. if ( file_exists( $selected_theme_check ) ) {
  273. $file_path = $selected_theme_check;
  274. // Use the bundled file
  275. } else {
  276. $file_path = WPSC_CORE_THEME_PATH . '/' . $file;
  277. }
  278. }
  279. // Save the transient and update it every 12 hours
  280. if ( !empty( $file_path ) )
  281. set_transient( WPEC_TRANSIENT_THEME_PATH_PREFIX . $file, $file_path, 60 * 60 * 12 );
  282. }elseif(!file_exists($file_path)){
  283. delete_transient(WPEC_TRANSIENT_THEME_PATH_PREFIX . $file);
  284. wpsc_get_template_file_path($file);
  285. }
  286. // Return filtered result
  287. return apply_filters( WPEC_TRANSIENT_THEME_PATH_PREFIX . $file, $file_path );
  288. }
  289. /**
  290. * Get the Product Category ID by either slug or name
  291. * @access public
  292. *
  293. * @since 3.8
  294. * @param $slug (string) to be searched
  295. * @param $type (string) column to search, i.e name or slug
  296. * @return $category_id (int) Category ID
  297. */
  298. function wpsc_get_the_category_id($slug, $type = 'name'){
  299. global $wpdb,$wp_query;
  300. if(isset($wp_query->query_vars['taxonomy']))
  301. $taxonomy = $wp_query->query_vars['taxonomy'];
  302. else
  303. $taxonomy = 'wpsc_product_category';
  304. $category = get_term_by($type,$slug,$taxonomy);
  305. return empty( $category ) ? false : $category->term_id;
  306. }
  307. /**
  308. * Checks the category slug for a display type, if none set returns default
  309. * << May need reworking to be more specific to the taxonomy type >>
  310. * @access public
  311. *
  312. * @since 3.8
  313. * @param $slug(string)
  314. * @return $slug either from db or 'default' if none set
  315. */
  316. function wpsc_get_the_category_display($slug){
  317. global $wpdb;
  318. $default_display_type = get_option('product_view');
  319. if ( !empty($slug) && is_string($slug) ) {
  320. $category_id = wpsc_get_the_category_id($slug , 'slug');
  321. $display_type = wpsc_get_categorymeta( $category_id, 'display_type' );
  322. }
  323. if(!empty($display_type))
  324. return $display_type;
  325. else
  326. return $default_display_type;
  327. }
  328. /**
  329. * Checks if wpsc-single_product.php has been moved to the active theme, if it has then include the
  330. * template from the active theme.
  331. * @access public
  332. *
  333. * @since 3.8
  334. * @param $content content of the page
  335. * @return $content with wpsc-single_product content if its a single product
  336. */
  337. function wpsc_single_template( $content ) {
  338. global $wpdb, $post, $wp_query, $wpsc_query;
  339. //if we dont belong here exit out straight away
  340. if((!isset($wp_query->is_product)) && !isset($wp_query->query_vars['wpsc_product_category']))return $content;
  341. // If we are a single products page
  342. if ( 'wpsc-product' == $wp_query->post->post_type && !is_archive() && $wp_query->post_count <= 1 ) {
  343. remove_filter( "the_content", "wpsc_single_template", 12 );
  344. $single_theme_path = wpsc_get_template_file_path( 'wpsc-single_product.php' );
  345. if( isset( $wp_query->query_vars['preview'] ) && $wp_query->query_vars['preview'])
  346. $is_preview = 'true';
  347. else
  348. $is_preview = 'false';
  349. $wpsc_temp_query = new WP_Query( array( 'p' => $wp_query->post->ID , 'post_type' => 'wpsc-product','posts_per_page'=>1, 'preview' => $is_preview ) );
  350. list( $wp_query, $wpsc_temp_query ) = array( $wpsc_temp_query, $wp_query ); // swap the wpsc_query object
  351. ob_start();
  352. include( $single_theme_path );
  353. $content = ob_get_contents();
  354. ob_end_clean();
  355. list( $wp_query, $wpsc_temp_query ) = array( $wpsc_temp_query, $wp_query ); // swap the wpsc_query objects back
  356. }
  357. return $content;
  358. }
  359. function wpsc_is_viewable_taxonomy(){
  360. global $wp_query;
  361. if(isset($wp_query->query_vars['taxonomy']) && ('wpsc_product_category' == $wp_query->query_vars['taxonomy'] || 'product_tag' == $wp_query->query_vars['taxonomy'] ) || isset($wp_query->query_vars['wpsc_product_category']))
  362. return true;
  363. else
  364. return false;
  365. }
  366. /**
  367. * Checks and replaces the Page title with the category title if on a category page
  368. * @access public
  369. *
  370. * @since 3.8
  371. * @param $title (string) The Page Title
  372. * @param $id (int) The Page ID
  373. * @return $title (string) the new title
  374. */
  375. function wpsc_the_category_title($title='', $id=''){
  376. global $wp_query;
  377. $post = get_post($id);
  378. // If its the category page
  379. if( wpsc_is_viewable_taxonomy() && isset( $wp_query->posts[0] ) && $wp_query->posts[0]->post_title == $post->post_title && $wp_query->is_archive && !is_admin() && isset($wp_query->query_vars['wpsc_product_category'])){
  380. $category = get_term_by('slug',$wp_query->query_vars['wpsc_product_category'],'wpsc_product_category');
  381. remove_filter('the_title','wpsc_the_category_title');
  382. }
  383. // If its the product_tag page
  384. if( isset($wp_query->query_vars['taxonomy']) && 'product_tag' == $wp_query->query_vars['taxonomy'] && $wp_query->posts[0]->post_title == $post->post_title ){
  385. $category = get_term_by('slug',$wp_query->query_vars['term'],'product_tag');
  386. remove_filter('the_title','wpsc_the_category_title');
  387. }
  388. //if this is paginated products_page
  389. if( $wp_query->in_the_loop && empty($category->name) && isset( $wp_query->query_vars['paged'] ) && $wp_query->query_vars['paged'] && isset( $wp_query->query_vars['page'] ) && $wp_query->query_vars['page'] && 'wpsc-product' == $wp_query->query_vars['post_type']){
  390. $post_id = wpec_get_the_post_id_by_shortcode('[productspage]');
  391. $post = get_post($post_id);
  392. $title = $post->post_title;
  393. remove_filter('the_title','wpsc_the_category_title');
  394. }
  395. if(!empty($category->name))
  396. return $category->name;
  397. else
  398. return $title;
  399. }
  400. /**
  401. * wpsc_the_category_template swaps the template used for product categories with pageif archive template is being used use
  402. * @access public
  403. *
  404. * @since 3.8
  405. * @param $template (string) template path
  406. * @return $template (string)
  407. */
  408. function wpsc_the_category_template($template){
  409. global $wp_query;
  410. //this bit of code makes sure we use a nice standard page template for our products
  411. if(wpsc_is_viewable_taxonomy() && false !== strpos($template,'archive'))
  412. return str_ireplace('archive', 'page',$template);
  413. else
  414. return $template;
  415. }
  416. /**
  417. * wpsc_form_action
  418. *
  419. * Echo the form action for use in the template files
  420. *
  421. * @global <type> $wpec_form_action
  422. * @return <type>
  423. */
  424. function wpsc_form_action() {
  425. echo wpsc_get_form_action();
  426. }
  427. /**
  428. * wpsc_get_form_action
  429. *
  430. * Return the form action for use in the template files
  431. *
  432. * @global <type> $wpec_form_action
  433. * @return <type>
  434. */
  435. function wpsc_get_form_action() {
  436. global $wpec_form_action;
  437. $product_id = wpsc_the_product_id();
  438. // Function has already ran in this page load
  439. if ( isset( $wpec_form_action ) ) {
  440. $action = $wpec_form_action;
  441. // No global so figure it out
  442. } else {
  443. // Use external if set
  444. if ( wpsc_is_product_external() ) {
  445. $action = wpsc_product_external_link( $product_id );
  446. // Otherwise use this page
  447. } else {
  448. $action = wpsc_this_page_url();
  449. }
  450. }
  451. // Return form action
  452. return $action;
  453. }
  454. /**
  455. * wpsc_user_enqueues products function,
  456. * enqueue all javascript and CSS for wp ecommerce
  457. */
  458. function wpsc_enqueue_user_script_and_css() {
  459. global $wp_styles, $wpsc_theme_url, $wp_query;
  460. /**
  461. * added by xiligroup.dev to be compatible with touchshop
  462. */
  463. if ( has_filter( 'wpsc_enqueue_user_script_and_css' ) && apply_filters( 'wpsc_mobile_scripts_css_filters', false ) ) {
  464. do_action( 'wpsc_enqueue_user_script_and_css' );
  465. } else {
  466. /**
  467. * end of added by xiligroup.dev to be compatible with touchshop
  468. */
  469. $version_identifier = WPSC_VERSION . "." . WPSC_MINOR_VERSION;
  470. $category_id = '';
  471. if (isset( $wp_query ) && isset( $wp_query->query_vars['taxonomy'] ) && ('wpsc_product_category' == $wp_query->query_vars['taxonomy'] ) || is_numeric( get_option( 'wpsc_default_category' ) )
  472. ) {
  473. if ( isset($wp_query->query_vars['term']) && is_string( $wp_query->query_vars['term'] ) ) {
  474. $category_id = wpsc_get_category_id($wp_query->query_vars['term'], 'slug');
  475. } else {
  476. $category_id = get_option( 'wpsc_default_category' );
  477. }
  478. }
  479. $remote_protocol = is_ssl() ? 'https://' : 'http://';
  480. if( get_option( 'wpsc_share_this' ) == 1 )
  481. wp_enqueue_script( 'sharethis', $remote_protocol . 'w.sharethis.com/button/buttons.js', array(), false, true );
  482. wp_enqueue_script( 'jQuery' );
  483. wp_enqueue_script( 'wp-e-commerce', WPSC_CORE_JS_URL . '/wp-e-commerce.js', array( 'jquery' ), $version_identifier );
  484. wp_enqueue_script( 'infieldlabel', WPSC_CORE_JS_URL . '/jquery.infieldlabel.min.js', array( 'jquery' ), $version_identifier );
  485. wp_enqueue_script( 'wp-e-commerce-ajax-legacy', WPSC_CORE_JS_URL . '/ajax.js', false, $version_identifier );
  486. wp_enqueue_script( 'wp-e-commerce-dynamic', site_url( '/index.php?wpsc_user_dynamic_js=true' ), false, $version_identifier );
  487. wp_localize_script( 'wp-e-commerce-dynamic', 'wpsc_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
  488. wp_enqueue_script( 'livequery', WPSC_URL . '/wpsc-admin/js/jquery.livequery.js', array( 'jquery' ), '1.0.3' );
  489. if( get_option( 'product_ratings' ) == 1 )
  490. wp_enqueue_script( 'jquery-rating', WPSC_CORE_JS_URL . '/jquery.rating.js', array( 'jquery' ), $version_identifier );
  491. wp_enqueue_script( 'wp-e-commerce-legacy', WPSC_CORE_JS_URL . '/user.js', array( 'jquery' ), WPSC_VERSION . WPSC_MINOR_VERSION );
  492. if ( get_option( 'show_thumbnails_thickbox' ) == 1 ){
  493. $lightbox = get_option('wpsc_lightbox', 'thickbox');
  494. if( $lightbox == 'thickbox' ) {
  495. wp_enqueue_script( 'wpsc-thickbox', WPSC_CORE_JS_URL . '/thickbox.js', array( 'jquery' ), 'Instinct_e-commerce' );
  496. wp_enqueue_style( 'wpsc-thickbox', WPSC_CORE_JS_URL . '/thickbox.css', false, $version_identifier, 'all' );
  497. } elseif( $lightbox == 'colorbox' ) {
  498. wp_enqueue_script( 'colorbox-min', WPSC_CORE_JS_URL . '/jquery.colorbox-min.js', array( 'jquery' ), 'Instinct_e-commerce' );
  499. wp_enqueue_script( 'wpsc_colorbox', WPSC_CORE_JS_URL . '/wpsc_colorbox.js', array( 'jquery', 'colorbox-min' ), 'Instinct_e-commerce' );
  500. wp_enqueue_style( 'wpsc-colorbox-css', WPSC_CORE_JS_URL . '/wpsc_colorbox.css', false, $version_identifier, 'all' );
  501. }
  502. }
  503. wp_enqueue_style( 'wpsc-theme-css', wpsc_get_template_file_url( 'wpsc-' . get_option( 'wpsc_selected_theme' ) . '.css' ), false, $version_identifier, 'all' );
  504. wp_enqueue_style( 'wpsc-theme-css-compatibility', WPSC_CORE_THEME_URL . 'compatibility.css', false, $version_identifier, 'all' );
  505. if( get_option( 'product_ratings' ) == 1 )
  506. wp_enqueue_style( 'wpsc-product-rater', WPSC_CORE_JS_URL . '/product_rater.css', false, $version_identifier, 'all' );
  507. wp_enqueue_style( 'wp-e-commerce-dynamic', site_url( "/index.php?wpsc_user_dynamic_css=true&category=$category_id" ), false, $version_identifier, 'all' );
  508. }
  509. if ( !defined( 'WPSC_MP3_MODULE_USES_HOOKS' ) && function_exists( 'listen_button' ) ) {
  510. function wpsc_legacy_add_mp3_preview( $product_id, &$product_data ) {
  511. global $wpdb;
  512. if ( function_exists( 'listen_button' ) ) {
  513. $file_data = $wpdb->get_row( "SELECT * FROM `" . WPSC_TABLE_PRODUCT_FILES . "` WHERE `id`='" . $product_data['file'] . "' LIMIT 1", ARRAY_A );
  514. if ( $file_data != null ) {
  515. echo listen_button( $file_data['idhash'], $file_data['id'] );
  516. }
  517. }
  518. }
  519. add_action( 'wpsc_product_before_description', 'wpsc_legacy_add_mp3_preview', 10, 2 );
  520. }
  521. }
  522. if ( !is_admin() )
  523. add_action( 'init', 'wpsc_enqueue_user_script_and_css' );
  524. function wpsc_product_list_rss_feed() {
  525. $rss_url = get_option('siteurl');
  526. $rss_url = add_query_arg( 'wpsc_action', 'rss', $rss_url );
  527. $rss_url = str_replace('&', '&amp;', $rss_url);
  528. $rss_url = esc_url( $rss_url ); // URL santization - IMPORTANT!
  529. echo "<link rel='alternate' type='application/rss+xml' title='" . get_option( 'blogname' ) . " Product List RSS' href='{$rss_url}'/>";
  530. }
  531. add_action( 'wp_head', 'wpsc_product_list_rss_feed' );
  532. function wpsc_user_dynamic_js() {
  533. header( 'Content-Type: text/javascript' );
  534. header( 'Expires: ' . gmdate( 'r', mktime( 0, 0, 0, date( 'm' ), (date( 'd' ) + 12 ), date( 'Y' ) ) ) . '' );
  535. header( 'Cache-Control: public, must-revalidate, max-age=86400' );
  536. header( 'Pragma: public' );
  537. $siteurl = get_option( 'siteurl' );
  538. ?>
  539. jQuery.noConflict();
  540. /* base url */
  541. var base_url = "<?php echo $siteurl; ?>";
  542. var WPSC_URL = "<?php echo WPSC_URL; ?>";
  543. var WPSC_IMAGE_URL = "<?php echo WPSC_IMAGE_URL; ?>";
  544. var WPSC_DIR_NAME = "<?php echo WPSC_DIR_NAME; ?>";
  545. var WPSC_CORE_IMAGES_URL = "<?php echo WPSC_CORE_IMAGES_URL; ?>";
  546. /* LightBox Configuration start*/
  547. var fileLoadingImage = "<?php echo WPSC_CORE_IMAGES_URL; ?>/loading.gif";
  548. var fileBottomNavCloseImage = "<?php echo WPSC_CORE_IMAGES_URL; ?>/closelabel.gif";
  549. var fileThickboxLoadingImage = "<?php echo WPSC_CORE_IMAGES_URL; ?>/loadingAnimation.gif";
  550. var resizeSpeed = 9; // controls the speed of the image resizing (1=slowest and 10=fastest)
  551. var borderSize = 10; //if you adjust the padding in the CSS, you will need to update this variable
  552. <?php
  553. exit();
  554. }
  555. if ( isset( $_GET['wpsc_user_dynamic_js'] ) && ($_GET['wpsc_user_dynamic_js'] == 'true') )
  556. add_action( "init", 'wpsc_user_dynamic_js' );
  557. function wpsc_user_dynamic_css() {
  558. global $wpdb;
  559. header( 'Content-Type: text/css' );
  560. header( 'Expires: ' . gmdate( 'r', mktime( 0, 0, 0, date( 'm' ), (date( 'd' ) + 12 ), date( 'Y' ) ) ) . '' );
  561. header( 'Cache-Control: public, must-revalidate, max-age=86400' );
  562. header( 'Pragma: public' );
  563. $category_id = absint( $_GET['category'] );
  564. if ( !defined( 'WPSC_DISABLE_IMAGE_SIZE_FIXES' ) || (constant( 'WPSC_DISABLE_IMAGE_SIZE_FIXES' ) != true) ) {
  565. $thumbnail_width = get_option( 'product_image_width' );
  566. if ( $thumbnail_width <= 0 ) {
  567. $thumbnail_width = 96;
  568. }
  569. $thumbnail_height = get_option( 'product_image_height' );
  570. if ( $thumbnail_height <= 0 ) {
  571. $thumbnail_height = 96;
  572. }
  573. $single_thumbnail_width = get_option( 'single_view_image_width' );
  574. $single_thumbnail_height = get_option( 'single_view_image_height' );
  575. if ( $single_thumbnail_width <= 0 ) {
  576. $single_thumbnail_width = 128;
  577. }
  578. $category_height = get_option('category_image_height');
  579. $category_width = get_option('category_image_width');
  580. ?>
  581. /*
  582. * Default View Styling
  583. */
  584. div.default_product_display div.textcol{
  585. margin-left: <?php echo $thumbnail_width + 10; ?>px !important;
  586. min-height: <?php echo $thumbnail_height; ?>px;
  587. _height: <?php echo $thumbnail_height; ?>px;
  588. }
  589. div.default_product_display div.textcol div.imagecol{
  590. position:absolute;
  591. top:0px;
  592. left: 0px;
  593. margin-left: -<?php echo $thumbnail_width + 10; ?>px !important;
  594. }
  595. div.default_product_display div.textcol div.imagecol a img {
  596. width: <?php echo $thumbnail_width; ?>px;
  597. height: <?php echo $thumbnail_height; ?>px;
  598. }
  599. .wpsc_category_grid_item {
  600. display:block;
  601. float:left;
  602. width: <?php echo $category_width; ?>px;
  603. height: <?php echo $category_height; ?>px;
  604. }
  605. .wpsc_category_grid_item span{
  606. position:relative;
  607. top:<?php echo ($thumbnail_height - 2)/9; ?>px;
  608. }
  609. div.default_product_display div.item_no_image a {
  610. width: <?php echo $thumbnail_width - 2; ?>px;
  611. }
  612. div.default_product_display .imagecol img.no-image, #content div.default_product_display .imagecol img.no-image {
  613. width: <?php echo $thumbnail_width; ?>px;
  614. height: <?php echo $thumbnail_height; ?>px;
  615. }
  616. /*
  617. * Grid View Styling
  618. */
  619. div.product_grid_display div.item_no_image {
  620. width: <?php echo $thumbnail_width - 2; ?>px;
  621. height: <?php echo $thumbnail_height - 2; ?>px;
  622. }
  623. div.product_grid_display div.item_no_image a {
  624. width: <?php echo $thumbnail_width - 2; ?>px;
  625. }
  626. .product_grid_display .product_grid_item {
  627. width: <?php echo $thumbnail_width; ?>px;
  628. }
  629. .product_grid_display .product_grid_item img.no-image, #content .product_grid_display .product_grid_item img.no-image {
  630. width: <?php echo $thumbnail_width; ?>px;
  631. height: <?php echo $thumbnail_height; ?>px;
  632. }
  633. <?php if(get_option('show_images_only') == 1): ?>
  634. .product_grid_display .product_grid_item {
  635. min-height:0 !important;
  636. width: <?php echo $thumbnail_width; ?>px;
  637. height: <?php echo $thumbnail_height; ?>px;
  638. }
  639. <?php endif; ?>
  640. /*
  641. * Single View Styling
  642. */
  643. div.single_product_display div.item_no_image {
  644. width: <?php echo $single_thumbnail_width - 2; ?>px;
  645. height: <?php echo $single_thumbnail_height - 2; ?>px;
  646. }
  647. div.single_product_display div.item_no_image a {
  648. width: <?php echo $single_thumbnail_width - 2; ?>px;
  649. }
  650. div.single_product_display div.textcol{
  651. margin-left: <?php echo $single_thumbnail_width + 10; ?>px !important;
  652. min-height: <?php echo $single_thumbnail_height; ?>px;
  653. _height: <?php echo $single_thumbnail_height; ?>px;
  654. }
  655. div.single_product_display div.textcol div.imagecol{
  656. position:absolute;
  657. margin-left: -<?php echo $single_thumbnail_width + 10; ?>px !important;
  658. }
  659. div.single_product_display div.textcol div.imagecol a img {
  660. width: <?php echo $single_thumbnail_width; ?>px;
  661. height: <?php echo $single_thumbnail_height; ?>px;
  662. }
  663. <?php
  664. if (isset($product_image_size_list)) {
  665. foreach ( (array)$product_image_size_list as $product_image_sizes ) {
  666. $individual_thumbnail_height = $product_image_sizes['height'];
  667. $individual_thumbnail_width = $product_image_sizes['width'];
  668. $product_id = $product_image_sizes['id'];
  669. if ( $individual_thumbnail_height > $thumbnail_height ) {
  670. echo " div.default_product_display.product_view_$product_id div.textcol{\n\r";
  671. echo " min-height: " . ($individual_thumbnail_height + 10) . "px !important;\n\r";
  672. echo " _height: " . ($individual_thumbnail_height + 10) . "px !important;\n\r";
  673. echo " }\n\r";
  674. }
  675. if ( $individual_thumbnail_width > $thumbnail_width ) {
  676. echo " div.default_product_display.product_view_$product_id div.textcol{\n\r";
  677. echo " margin-left: " . ($individual_thumbnail_width + 10) . "px !important;\n\r";
  678. echo " }\n\r";
  679. echo " div.default_product_display.product_view_$product_id div.textcol div.imagecol{\n\r";
  680. echo " position:absolute;\n\r";
  681. echo " top:0px;\n\r";
  682. echo " left: 0px;\n\r";
  683. echo " margin-left: -" . ($individual_thumbnail_width + 10) . "px !important;\n\r";
  684. echo " }\n\r";
  685. }
  686. if ( ($individual_thumbnail_width > $thumbnail_width) || ($individual_thumbnail_height > $thumbnail_height) ) {
  687. echo " div.default_product_display.product_view_$product_id div.textcol div.imagecol a img{\n\r";
  688. echo " width: " . $individual_thumbnail_width . "px;\n\r";
  689. echo " height: " . $individual_thumbnail_height . "px;\n\r";
  690. echo " }\n\r";
  691. }
  692. }
  693. }
  694. exit();
  695. }
  696. if ( (isset($_GET['brand']) && is_numeric( $_GET['brand'] )) || (get_option( 'show_categorybrands' ) == 3) ) {
  697. $brandstate = 'block';
  698. $categorystate = 'none';
  699. } else {
  700. $brandstate = 'none';
  701. $categorystate = 'block';
  702. }
  703. ?>
  704. div#categorydisplay{
  705. display: <?php echo $categorystate; ?>;
  706. }
  707. div#branddisplay{
  708. display: <?php echo $brandstate; ?>;
  709. }
  710. <?php
  711. exit();
  712. }
  713. if ( isset( $_GET['wpsc_user_dynamic_css'] ) && ($_GET['wpsc_user_dynamic_css'] == 'true') )
  714. add_action( "init", 'wpsc_user_dynamic_css' );
  715. function wpsc_get_the_new_id($prod_id){
  716. global $wpdb;
  717. $post_id = (int)$wpdb->get_var($wpdb->prepare( "SELECT `post_id` FROM `{$wpdb->postmeta}` WHERE meta_key = %s AND `meta_value` = %d LIMIT 1", '_wpsc_original_id', $prod_id ));
  718. return $post_id;
  719. }
  720. // Template tags
  721. /**
  722. * wpsc display products function
  723. * @return string - html displaying one or more products
  724. */
  725. function wpsc_display_products_page( $query ) {
  726. global $wpdb, $wpsc_query,$wp_query;
  727. remove_filter('the_title','wpsc_the_category_title');
  728. // If the data is coming from a shortcode parse the values into the args variable,
  729. // I did it this was to preserve backwards compatibility
  730. if(!empty($query)){
  731. $args['post_type'] = 'wpsc-product';
  732. if(!empty($query['product_id']) && is_array($query['product_id'])){
  733. $args['post__in'] = $query['product_id'];
  734. }elseif(is_string($query['product_id'])){
  735. $args['post__in'] = (array)$query['product_id'];
  736. }
  737. if(!empty($query['old_product_id'])){
  738. $post_id = wpsc_get_the_new_id($query['old_product_id']);
  739. $args['post__in'] = (array)$post_id;
  740. }
  741. if(!empty($query['price']) && 'sale' != $query['price']){
  742. $args['meta_key'] = '_wpsc_price';
  743. $args['meta_value'] = $query['price'];
  744. }elseif(!empty($query['price']) && 'sale' == $query['price']){
  745. $args['meta_key'] = '_wpsc_special_price';
  746. $args['meta_compare'] = '>=';
  747. $args['meta_value'] = '1';
  748. }
  749. if(!empty($query['product_name'])){
  750. $args['pagename'] = $query['product_name'];
  751. }
  752. if(!empty($query['category_id'])){
  753. $term = get_term($query['category_id'],'wpsc_product_category');
  754. $id = wpsc_get_meta($query['category_id'], 'category_id','wpsc_old_category');
  755. if( !empty($id)){
  756. $term = get_term($id,'wpsc_product_category');
  757. $args['wpsc_product_category'] = $term->slug;
  758. $args['wpsc_product_category__in'] = $term->term_id;
  759. }else{
  760. $args['wpsc_product_category'] = $term->slug;
  761. $args['wpsc_product_category__in'] = $term->term_id;
  762. }
  763. }
  764. if(!empty($query['category_url_name'])){
  765. $args['wpsc_product_category'] = $query['category_url_name'];
  766. }
  767. $orderby = ( !empty($query['sort_order']) ) ? $query['sort_order'] : null;
  768. $args = array_merge( $args, wpsc_product_sort_order_query_vars($orderby) );
  769. if(!empty($query['order'])){
  770. $args['order'] = $query['order'];
  771. }
  772. if(!empty($query['limit_of_items']) && '1' == get_option('use_pagination')){
  773. $args['posts_per_page'] = $query['limit_of_items'];
  774. }
  775. if(!empty($query['number_per_page']) && '1' == get_option('use_pagination')){
  776. $args['posts_per_page'] = $query['number_per_page'];
  777. }
  778. if( '0' == get_option('use_pagination') ){
  779. $args['nopaging'] = true;
  780. $args['posts_per_page'] = '-1';
  781. }
  782. if(!empty($query['tag'])){
  783. $args['product_tag'] = $query['tag'];
  784. }
  785. $temp_wpsc_query = new WP_Query($args);
  786. }
  787. // swap the wpsc_query objects
  788. list( $wp_query, $temp_wpsc_query ) = array( $temp_wpsc_query, $wp_query );
  789. $GLOBALS['nzshpcrt_activateshpcrt'] = true;
  790. // Pretty sure this single_product code is legacy...but fixing it up just in case.
  791. // get the display type for the selected category
  792. if(!empty($temp_wpsc_query->query_vars['term']))
  793. $display_type = wpsc_get_the_category_display($temp_wpsc_query->query_vars['term']);
  794. elseif( !empty( $args['wpsc_product_category'] ) )
  795. $display_type = wpsc_get_the_category_display($args['wpsc_product_category']);
  796. else
  797. $display_type = 'default';
  798. if ( isset( $_SESSION['wpsc_display_type'] ) )
  799. $display_type = $_SESSION['wpsc_display_type'];
  800. ob_start();
  801. if( 'wpsc-product' == $wp_query->post->post_type && !is_archive() && $wp_query->post_count <= 1 )
  802. include( wpsc_get_template_file_path( 'wpsc-single_product.php' ) );
  803. else
  804. wpsc_include_products_page_template($display_type);
  805. $is_single = false;
  806. $output = ob_get_contents();
  807. ob_end_clean();
  808. $output = str_replace('\$','$', $output);
  809. list($temp_wpsc_query, $wp_query) = array( $wp_query, $temp_wpsc_query ); // swap the wpsc_query objects back
  810. if ( $is_single == false ) {
  811. $GLOBALS['post'] = $wp_query->post;
  812. }
  813. return $output;
  814. }
  815. /**
  816. * This switched between the 3 view types on category and products pages and includes the necessary tempalte part
  817. * @access public
  818. *
  819. * @since 3.8
  820. * @param $display_type
  821. * @return NULL
  822. */
  823. function wpsc_include_products_page_template($display_type = 'default'){
  824. if ( isset( $_GET['view_type'] ) && get_option( 'show_search' ) && get_option( 'show_advanced_search' ) ) {
  825. switch ( $_GET['view_type'] ) {
  826. case 'grid':
  827. $display_type = 'grid';
  828. $_SESSION['wpsc_display_type'] = $display_type;
  829. break;
  830. case 'list':
  831. $display_type = 'list';
  832. $_SESSION['wpsc_display_type'] = $display_type;
  833. break;
  834. case 'default':
  835. $display_type = 'default';
  836. $_SESSION['wpsc_display_type'] = $display_type;
  837. break;
  838. default:
  839. break;
  840. }
  841. }
  842. // switch the display type, based on the display type variable...
  843. switch ( $display_type ) {
  844. case "grid":
  845. include( wpsc_get_template_file_path( 'wpsc-grid_view.php' ) );
  846. break; // only break if we have the function;
  847. case "list":
  848. include( wpsc_get_template_file_path( 'wpsc-list_view.php' ) );
  849. break; // only break if we have the file;
  850. default:
  851. include( wpsc_get_template_file_path( 'wpsc-products_page.php' ) );
  852. break;
  853. }
  854. }
  855. //handles replacing the tags in the pages
  856. function wpsc_products_page( $content = '' ) {
  857. global $wpdb, $wp_query, $wpsc_query, $wpsc_query_vars;
  858. $output = '';
  859. if ( preg_match( "/\[productspage\]/", $content ) ) {
  860. global $more;
  861. $more = 0;
  862. remove_filter( 'the_content', 'wpautop' );
  863. list($wp_query, $wpsc_query) = array( $wpsc_query, $wp_query ); // swap the wpsc_query object
  864. $GLOBALS['nzshpcrt_activateshpcrt'] = true;
  865. // get the display type for the productspage
  866. $display_type = get_option('product_view');
  867. if ( isset( $_SESSION['wpsc_display_type'] ) )
  868. $display_type = $_SESSION['wpsc_display_type'];
  869. ob_start();
  870. wpsc_include_products_page_template($display_type);
  871. $is_single = false;
  872. $output .= ob_get_contents();
  873. ob_end_clean();
  874. $output = str_replace( '$', '\$', $output );
  875. $product_id = $wp_query->post->ID;
  876. $product_meta = get_post_meta( $product_id, '_wpsc_product_metadata', true );
  877. list($wp_query, $wpsc_query) = array( $wpsc_query, $wp_query ); // swap the wpsc_query objects back
  878. if ( ($is_single == false) || ($product_meta['enable_comments'] == '0') )
  879. $GLOBALS['post'] = $wp_query->post;
  880. $wp_query->current_post = $wp_query->post_count;
  881. return preg_replace( "/(<p>)*\[productspage\](<\/p>)*/", $output, $content );
  882. } elseif(is_archive() && wpsc_is_viewable_taxonomy()){
  883. remove_filter( 'the_content', 'wpautop' );
  884. return wpsc_products_page('[productspage]');
  885. } else {
  886. return $content;
  887. }
  888. }
  889. function wpsc_thesis_compat( $loop ) {
  890. $loop[1] = 'page';
  891. return $loop;
  892. }
  893. function wpsc_all_products_on_page(){
  894. global $wp_query,$wpsc_query;
  895. do_action('wpsc_swap_the_template');
  896. $products_page_id = wpec_get_the_post_id_by_shortcode('[productspage]');
  897. $term = get_query_var( 'wpsc_product_category' );
  898. $tax_term = get_query_var ('product_tag' );
  899. $obj = $wp_query->get_queried_object();
  900. $id = isset( $obj->ID ) ? $obj->ID : null;
  901. if( get_query_var( 'post_type' ) == 'wpsc-product' || $term || $tax_term || ( $id == $products_page_id )){
  902. $templates = array();
  903. if ( $term && ! is_single() ) {
  904. array_push( $templates, "taxonomy-wpsc_product_category-{$term}.php", 'taxonomy-wpsc_product_category.php' );
  905. }
  906. if ( $tax_term && ! is_single() ) {
  907. array_push( $templates, "taxonomy-product_tag-{$tax_term}.php", 'taxonomy-product_tag.php' );
  908. }
  909. // Attempt to use the [productspage]'s custom page template as a higher priority than the normal page.php template
  910. if ( false !== $productspage_page_template = get_post_meta($products_page_id, '_wp_page_template', true) )
  911. array_push( $templates, $productspage_page_template );
  912. array_push( $templates, 'page.php', 'single.php' );
  913. if ( is_single() )
  914. array_unshift( $templates, 'single-wpsc-product.php' );
  915. // have to pass 'page' as the template type. This is lame, btw, and needs a rewrite in 4.0
  916. if ( ! $template = get_query_template( 'page', $templates ) )
  917. $template = get_index_template();
  918. add_filter( 'thesis_custom_loop', 'wpsc_thesis_compat' );
  919. include( $template );
  920. exit;
  921. }
  922. }
  923. add_action('template_redirect', 'wpsc_all_products_on_page');
  924. /**
  925. * wpsc_count_themes_in_uploads_directory, does exactly what the name says
  926. */
  927. function wpsc_count_themes_in_uploads_directory() {
  928. $uploads_dir = false;
  929. if ( is_dir( WPSC_OLD_THEMES_PATH.get_option('wpsc_selected_theme').'/' ) )
  930. $uploads_dir = @opendir( WPSC_OLD_THEMES_PATH.get_option('wpsc_selected_theme').'/' ); // might cause problems if dir doesnt exist
  931. if ( !$uploads_dir )
  932. return false;
  933. $file_names = array( );
  934. while ( ($file = @readdir( $uploads_dir )) !== false ) {
  935. if ( is_dir( WPSC_OLD_THEMES_PATH . get_option('wpsc_selected_theme') . '/' . $file ) && ($file != "..") && ($file != ".") && ($file != ".svn") )
  936. $file_names[] = $file;
  937. }
  938. @closedir( $uploads_dir );
  939. return count( $file_names );
  940. }
  941. function wpsc_place_shopping_cart( $content = '' ) {
  942. if ( preg_match( "/\[shoppingcart\]/", $content ) ) {
  943. $GLOBALS['nzshpcrt_activateshpcrt'] = true;
  944. define( 'DONOTCACHEPAGE', true );
  945. ob_start();
  946. include( wpsc_get_template_file_path( 'wpsc-shopping_cart_page.php' ) );
  947. $output = ob_get_contents();
  948. ob_end_clean();
  949. $output = str_replace( '$', '\$', $output );
  950. return preg_replace( "/(<p>)*\[shoppingcart\](<\/p>)*/", $output, $content );
  951. } else {
  952. return $content;
  953. }
  954. }
  955. function wpsc_transaction_results( $content = '' ) {
  956. if ( preg_match( "/\[transactionresults\]/", $content ) ) {
  957. define( 'DONOTCACHEPAGE', true );
  958. ob_start();
  959. include( wpsc_get_template_file_path( 'wpsc-transaction_results.php' ) );
  960. $output = ob_get_contents();
  961. ob_end_clean();
  962. return preg_replace( "/(<p>)*\[transactionresults\](<\/p>)*/", $output, $content );
  963. } else {
  964. return $content;
  965. }
  966. }
  967. function wpsc_user_log( $content = '' ) {
  968. if ( preg_match( "/\[userlog\]/", $content ) ) {
  969. define( 'DONOTCACHEPAGE', true );
  970. ob_start();
  971. include( wpsc_get_template_file_path('wpsc-user-log.php') );
  972. $output = ob_get_clean();
  973. $content = preg_replace( "/(<p>)*\[userlog\](<\/p>)*/", '[userlog]', $content );
  974. return str_replace( '[userlog]', $output, $content );
  975. } else {
  976. return $content;
  977. }
  978. }
  979. //displays a list of categories when the code [showcategories] is present in a post or page.
  980. function wpsc_show_categories( $content ) {
  981. ob_start();
  982. include( wpsc_get_template_file_path( 'wpsc-category-list.php' ) );
  983. $output = ob_get_contents();
  984. ob_end_clean();
  985. return $output;
  986. }
  987. add_shortcode('showcategories', 'wpsc_show_categories');
  988. function wpec_get_the_post_id_by_shortcode($shortcode){
  989. global $wpdb;
  990. $sql = "SELECT `ID` FROM `{$wpdb->posts}` WHERE `post_type` IN('page','post') AND `post_content` LIKE '%$shortcode%' LIMIT 1";
  991. $page_id = $wpdb->get_var($sql);
  992. return apply_filters( 'wpec_get_the_post_id_by_shortcode', $page_id );
  993. }
  994. function wpec_remap_shop_subpages($vars) {
  995. if(empty($vars))
  996. return $vars;
  997. $reserved_names = array('[shoppingcart]','[userlog]','[transactionresults]');
  998. foreach($reserved_names as $reserved_name){
  999. $page_id = wpec_get_the_post_id_by_shortcode($reserved_name);
  1000. $page = get_post($page_id);
  1001. if (isset($vars['taxonomy']) && $vars['taxonomy'] == 'wpsc_product_category') {
  1002. if (isset($vars['term']) && $vars['term'] == $page->post_name) {
  1003. return array('page_id' => $page->ID);
  1004. }
  1005. }
  1006. }
  1007. return $vars;
  1008. }
  1009. add_filter('request','wpec_remap_shop_subpages');
  1010. function wpsc_remove_page_from_query_string($query_string)
  1011. {
  1012. if ( isset($query_string['name']) && $query_string['name'] == 'page' && isset($query_string['page']) ) {
  1013. unset($query_string['name']);
  1014. list($delim, $page_index) = split('/', $query_string['page']);
  1015. $query_string['paged'] = $page_index;
  1016. }
  1017. if ( isset($query_string['wpsc-product']) && 'page' == $query_string['wpsc-product'] )
  1018. $query_string['wpsc-product'] = '';
  1019. if ( isset($query_string['name']) && is_numeric($query_string['name']) ) {
  1020. $query_string['paged'] = $query_string['name'];
  1021. $query_string['page'] = '/'.$query_string['name'];
  1022. $query_string['posts_per_page'] = get_option('wpsc_products_per_page');
  1023. }
  1024. if ( isset($query_string['wpsc-product']) && is_numeric($query_string['wpsc-product']) )
  1025. unset( $query_string['wpsc-product'] );
  1026. if ( isset($query_string['wpsc_product_category']) && 'page' == $query_string['wpsc_product_category'] )
  1027. unset( $query_string['wpsc_product_category'] );
  1028. if ( isset($query_string['name']) && is_numeric($query_string['name']) )
  1029. unset( $query_string['name'] );
  1030. if ( isset($query_string['term']) && 'page' == $query_string['term'] ) {
  1031. unset( $query_string['term'] );
  1032. unset( $query_string['taxonomy'] );
  1033. }
  1034. return $query_string;
  1035. }
  1036. add_filter('request', 'wpsc_remove_page_from_query_string');
  1037. /* 19-02-09
  1038. * add to cart shortcode function used for shortcodes calls the function in
  1039. * product_display_functions.php
  1040. */
  1041. function add_to_cart_shortcode( $content = '' ) {
  1042. static $fancy_notification_output = false;
  1043. if ( preg_match_all( "/\[add_to_cart=([\d]+)\]/", $content, $matches ) ) {
  1044. foreach ( $matches[1] as $key => $product_id ) {
  1045. $original_string = $matches[0][$key];
  1046. $output = wpsc_add_to_cart_button( $product_id, true );
  1047. $content = str_replace( $original_string, $output, $content );
  1048. }
  1049. if ( ! $fancy_notification_output ) {
  1050. $content .= wpsc_fancy_notifications( true );
  1051. $fancy_notification_output = true;
  1052. }
  1053. }
  1054. return $content;
  1055. }
  1056. function wpsc_enable_page_filters( $excerpt = '' ) {
  1057. add_filter( 'the_content', 'add_to_cart_shortcode', 12 ); //Used for add_to_cart_button shortcode
  1058. add_filter( 'the_content', 'wpsc_products_page', 1 );
  1059. add_filter( 'the_content', 'wpsc_single_template',12 );
  1060. add_filter( 'archive_template','wpsc_the_category_template');
  1061. add_filter( 'the_title', 'wpsc_the_category_title',10,2 );
  1062. add_filter( 'the_content', 'wpsc_place_shopping_cart', 12 );
  1063. add_filter( 'the_content', 'wpsc_transaction_results', 12 );
  1064. add_filter( 'the_content', 'wpsc_user_log', 12 );
  1065. return $excerpt;
  1066. }
  1067. wpsc_enable_page_filters();
  1068. /**
  1069. * Body Class Filter
  1070. * @modified: 2009-10-14 by Ben
  1071. * @description: Adds additional wpsc classes to the body tag.
  1072. * @param: $classes = Array of body classes
  1073. * @return: (Array) of classes
  1074. */
  1075. function wpsc_body_class( $classes ) {
  1076. global $wp_query, $wpsc_query;
  1077. $post_id = 0;
  1078. if ( isset( $wp_query->post->ID ) )
  1079. $post_id = $wp_query->post->ID;
  1080. $page_url = get_permalink( $post_id );
  1081. // If on a product or category page...
  1082. if ( get_option( 'product_list_url' ) == $page_url ) {
  1083. $classes[] = 'wpsc';
  1084. if ( !is_array( $wpsc_query->query ) )
  1085. $classes[] = 'wpsc-home';
  1086. if ( wpsc_is_single_product ( ) ) {
  1087. $classes[] = 'wpsc-single-product';
  1088. if ( absint( $wpsc_query->products[0]['id'] ) > 0 ) {
  1089. $classes[] = 'wpsc-single-product-' . $wpsc_query->products[0]['id'];
  1090. }
  1091. }
  1092. if ( wpsc_is_in_category() && !wpsc_is_single_product() )
  1093. $classes[] = 'wpsc-category';
  1094. if ( isset( $wpsc_query->query_vars['category_id'] ) && absint( $wpsc_query->query_vars['category_id'] ) > 0 )
  1095. $classes[] = 'wpsc-category-' . $wpsc_query->query_vars['category_id'];
  1096. }
  1097. // If viewing the shopping cart...
  1098. if ( get_option( 'shopping_cart_url' ) == $page_url ) {
  1099. $classes[] = 'wpsc';
  1100. $classes[] = 'wpsc-shopping-cart';
  1101. }
  1102. // If viewing the transaction...
  1103. if ( get_option( 'transact_url' ) == $page_url ) {
  1104. $classes[] = 'wpsc';
  1105. $classes[] = 'wpsc-transaction-details';
  1106. }
  1107. // If viewing your account...
  1108. if ( get_option( 'user_account_url' ) == $page_url ) {
  1109. $classes[] = 'wpsc';
  1110. $classes[] = 'wpsc-user-account';
  1111. }
  1112. return $classes;
  1113. }
  1114. add_filter( 'body_class', 'wpsc_body_class' );
  1115. /**
  1116. * Featured Product
  1117. *
  1118. * Refactoring Featured Product Plugin to utilize Sticky Post Status, available since WP 2.7
  1119. * also utilizes Featured Image functionality, available as post_thumbnail since 2.9, Featured Image since 3.0
  1120. * Main differences - Removed 3.8 conditions, removed meta box from admin, changed meta_values
  1121. * Removes shortcode, as it automatically ties in to top_of_page hook if sticky AND featured product exists.
  1122. *
  1123. * @package wp-e-commerce
  1124. * @since 3.8
  1125. */
  1126. function wpsc_the_sticky_image( $product_id ) {
  1127. $attached_images = (array)get_posts( array(
  1128. 'post_type' => 'attachment',
  1129. 'numberposts' => 1,
  1130. 'post_status' => null,
  1131. 'post_parent' => $product_id,
  1132. 'orderby' => 'menu_order',
  1133. 'order' => 'ASC'
  1134. ) );
  1135. if ( has_post_thumbnail( $product_id ) ) {
  1136. add_image_size( 'featured-product-thumbnails', 540, 260, TRUE );
  1137. $image = get_the_post_thumbnail( $product_id, 'featured-product-thumbnails' );
  1138. return $image;
  1139. } elseif ( !empty( $attached_images ) ) {
  1140. $attached_image = $attached_images[0];
  1141. $image_link = wpsc_product_image( $attached_image->ID, 540, 260 );
  1142. return '<img src="' . $image_link . '" alt="" />';
  1143. } else {
  1144. return false;
  1145. }
  1146. }
  1147. function is_products_page(){
  1148. global $post;
  1149. $product_page_id = wpec_get_the_post_id_by_shortcode('[productspage]');
  1150. if($post->ID == $product_page_id)
  1151. return true;
  1152. else
  1153. return false;
  1154. }
  1155. /**
  1156. * wpsc_display_products_page function.
  1157. *
  1158. * @access public
  1159. * @param mixed $query
  1160. * @return void
  1161. */
  1162. function wpsc_display_featured_products_page() {
  1163. global $wp_query;
  1164. $sticky_array = get_option( 'sticky_products' );
  1165. if ( (is_front_page() || is_home() || is_products_page() ) && !empty( $sticky_array ) && $wp_query->post_count > 1) {
  1166. $query = get_posts( array(
  1167. 'post__in' => $sticky_array,
  1168. 'post_type' => 'wpsc-product',
  1169. 'orderby' => 'rand',
  1170. 'numberposts' => 1,
  1171. 'posts_per_page' => 1
  1172. ) );
  1173. if ( count( $query ) > 0 ) {
  1174. $GLOBALS['nzshpcrt_activateshpcrt'] = true;
  1175. $image_width = get_option( 'product_image_width' );
  1176. $image_height = get_option( 'product_image_height' );
  1177. $featured_product_theme_path = wpsc_get_template_file_path( 'wpsc-featured_product.php' );
  1178. ob_start();
  1179. include_once($featured_product_theme_path);
  1180. $is_single = false;
  1181. $output .= ob_get_contents();
  1182. ob_end_clean();
  1183. //Begin outputting featured product. We can worry about templating later, or folks can just CSS it up.
  1184. echo $output;
  1185. //End output
  1186. }
  1187. }
  1188. }
  1189. if(get_option( 'wpsc_hide_featured_products' ) == 1){
  1190. add_action( 'wpsc_top_of_products_page', 'wpsc_display_featured_products_page', 12 );
  1191. }
  1192. /**
  1193. * wpsc_display_products_page class
  1194. *
  1195. * Shows only products from current category, but not from subcategories.
  1196. *
  1197. * @access public
  1198. * @return void
  1199. */
  1200. class WPSC_Hide_subcatsprods_in_cat {
  1201. var $q;
  1202. function get_posts( &$q ) {
  1203. $this->q =& $q;
  1204. if ( ( !isset($q->query_vars['taxonomy']) || ( "wpsc_product_category" != $q->query_vars['taxonomy'] )) )
  1205. return false;
  1206. add_action( 'posts_where', array( &$this, 'where' ) );
  1207. add_action( 'posts_join', array( &$this, 'join' ) );
  1208. }
  1209. function where( $where ) {
  1210. global $wpdb;
  1211. remove_action( 'posts_where', array( &$this, 'where' ) );
  1212. $term_id=$wpdb->get_var($wpdb->prepare('SELECT term_id FROM '.$wpdb->terms.' WHERE slug = %s ', $this->q->query_vars['term']));
  1213. if ( !is_numeric( $term_id ) || $term_id < 1 )
  1214. return $where;
  1215. $term_taxonomy_id = $wpdb->get_var($wpdb->prepare('SELECT term_taxonomy_id FROM '.$wpdb->term_taxonomy.' WHERE term_id = %d and taxonomy = %s', $term_id, $this->q->query_vars['taxonomy']));
  1216. if ( !is_numeric($term_taxonomy_id) || $term_taxonomy_id < 1)
  1217. return $where;
  1218. $field = preg_quote( "$wpdb->term_relationships.term_taxonomy_id", '#' );
  1219. $just_one = $wpdb->prepare( " AND $wpdb->term_relationships.term_taxonomy_id = %d ", $term_taxonomy_id );
  1220. if ( preg_match( "#AND\s+$field\s+IN\s*\(\s*(?:['\"]?\d+['\"]?\s*,\s*)*['\"]?\d+['\"]?\s*\)#", $where, $matches ) )
  1221. $where = str_replace( $matches[0], $just_one, $where );
  1222. else
  1223. $where .= $just_one;
  1224. return $where;
  1225. }
  1226. function join($join){
  1227. global $wpdb;
  1228. remove_action( 'posts_where', array( &$this, 'where' ) );
  1229. remove_action( 'posts_join', array( &$this, 'join' ) );
  1230. if( strpos($join, "JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id)" ) ){
  1231. return $join;

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