PageRenderTime 65ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

/wp-content/plugins/marketpress/marketpress.php

https://github.com/bfay/maniacal-kitten
PHP | 7392 lines | 5719 code | 1072 blank | 601 comment | 1222 complexity | fe95a7a5d81366f9494cf760be831ea9 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-3.0, LGPL-2.1

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

  1. <?php
  2. /*
  3. Plugin Name: MarketPress
  4. Version: 2.8.1
  5. Plugin URI: http://premium.wpmudev.org/project/e-commerce/
  6. Description: The complete WordPress ecommerce plugin - works perfectly with BuddyPress and Multisite too to create a social marketplace, where you can take a percentage! Activate the plugin, adjust your settings then add some products to your store.
  7. Author: Aaron Edwards (Incsub)
  8. Author URI: http://uglyrobot.com
  9. Text Domain: mp
  10. WDP ID: 144
  11. Copyright 2009-2013 Incsub (http://incsub.com)
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License (Version 2 - GPLv2) as published by
  14. the Free Software Foundation.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. class MarketPress {
  24. var $version = '2.8.1';
  25. var $location;
  26. var $plugin_dir = '';
  27. var $plugin_url = '';
  28. var $product_template;
  29. var $product_taxonomy_template;
  30. var $product_list_template;
  31. var $store_template;
  32. var $checkout_template;
  33. var $orderstatus_template;
  34. var $language = '';
  35. var $checkout_error = false;
  36. var $cart_cache = false;
  37. var $is_shop_page = false;
  38. var $global_cart = false;
  39. var $skip_shipping_notice = false;
  40. var $weight_printed = false;
  41. function MarketPress() {
  42. $this->__construct();
  43. }
  44. function __construct() {
  45. //setup our variables
  46. $this->init_vars();
  47. //install plugin
  48. register_activation_hook( __FILE__, array($this, 'install') );
  49. //load dashboard notice
  50. include_once( $this->plugin_dir . 'dash-notice/wpmudev-dash-notification.php' );
  51. //load template functions
  52. require_once( $this->plugin_dir . 'template-functions.php' );
  53. //load shortcodes
  54. include_once( $this->plugin_dir . 'marketpress-shortcodes.php' );
  55. //load stats
  56. include_once( $this->plugin_dir . 'marketpress-stats.php' );
  57. //load sitewide features if WPMU
  58. if (is_multisite()) {
  59. include_once( $this->plugin_dir . 'marketpress-ms.php' );
  60. $network_settings = get_site_option( 'mp_network_settings' );
  61. if ( $network_settings['global_cart'] )
  62. $this->global_cart = true;
  63. }
  64. //localize the plugin
  65. add_action( 'plugins_loaded', array(&$this, 'localization'), 9 );
  66. //load APIs and plugins
  67. add_action( 'plugins_loaded', array(&$this, 'load_plugins') );
  68. //load importers
  69. add_action( 'plugins_loaded', array(&$this, 'load_importers') );
  70. //custom post type
  71. add_action( 'init', array(&$this, 'register_custom_posts'), 0 ); //super high priority
  72. add_filter( 'request', array(&$this, 'handle_edit_screen_filter') );
  73. //edit products page
  74. add_filter( 'manage_product_posts_columns', array(&$this, 'edit_products_columns') );
  75. add_action( 'manage_posts_custom_column', array(&$this, 'edit_products_custom_columns') );
  76. add_action( 'restrict_manage_posts', array(&$this, 'edit_products_filter') );
  77. add_filter( 'post_row_actions', array(&$this, 'edit_products_custom_row_actions'), 10, 2);
  78. add_filter( 'admin_action_copy-product', array(&$this, 'edit_products_copy_action') );
  79. //manage orders page
  80. add_filter( 'manage_product_page_marketpress-orders_columns', array(&$this, 'manage_orders_columns') );
  81. add_action( 'manage_posts_custom_column', array(&$this, 'manage_orders_custom_columns') );
  82. //Plug admin pages
  83. add_action( 'admin_menu', array(&$this, 'add_menu_items') );
  84. add_action( 'admin_print_styles', array(&$this, 'admin_css') );
  85. add_action( 'admin_print_scripts', array(&$this, 'admin_script_post') );
  86. add_action( 'admin_notices', array(&$this, 'admin_nopermalink_warning') );
  87. add_filter( 'plugin_action_links_' . plugin_basename(__FILE__), array(&$this, 'plugin_action_link'), 10, 2);
  88. add_action( 'wp_ajax_mp-hide-help', array(&$this, 'hide_help') );
  89. //Meta boxes
  90. add_action( 'add_meta_boxes_product', array(&$this, 'meta_boxes') );
  91. add_action( 'wp_insert_post', array(&$this, 'save_product_meta'), 10, 2 );
  92. add_filter( 'enter_title_here', array(&$this, 'filter_title') );
  93. //Templates and Rewrites
  94. add_action( 'wp', array(&$this, 'load_store_templates') );
  95. add_action( 'template_redirect', array(&$this, 'load_store_theme') );
  96. add_action( 'pre_get_posts', array(&$this, 'remove_canonical') );
  97. add_filter( 'rewrite_rules_array', array(&$this, 'add_rewrite_rules') );
  98. add_filter( 'query_vars', array(&$this, 'add_queryvars') );
  99. add_action( 'option_rewrite_rules', array(&$this, 'check_rewrite_rules') );
  100. add_action( 'init', array(&$this, 'flush_rewrite_check') );
  101. if ( !defined('MP_HIDE_MENUS') ) { //allows you to hide MP menus
  102. add_filter( 'wp_list_pages', array(&$this, 'filter_list_pages'), 10, 2 );
  103. add_filter( 'wp_nav_menu_objects', array(&$this, 'filter_nav_menu'), 10, 2 );
  104. }
  105. //Payment gateway returns
  106. add_action( 'pre_get_posts', array(&$this, 'handle_gateway_returns'), 1 );
  107. //Store cart handling
  108. add_action( 'template_redirect', array(&$this, 'store_script') ); //only on front pages
  109. /* use both actions so logged in and not logged in users can send this AJAX request */
  110. add_action( 'wp_ajax_nopriv_mp-update-cart', array(&$this, 'update_cart') );
  111. add_action( 'wp_ajax_mp-update-cart', array(&$this, 'update_cart') );
  112. add_action( 'wp_ajax_mp-province-field', 'mp_province_field' ); //province field callback for shipping form
  113. add_action( 'wp_ajax_nopriv_mp-province-field', 'mp_province_field' );
  114. add_action( 'wp_ajax_mp-orders-export', array(&$this, 'export_orders_csv') );
  115. add_action( 'wp_logout', array(&$this, 'logout_clear_session') ); //see http://premium.wpmudev.org/forums/topic/security-issue-with-marketpress
  116. add_action( 'wp_ajax_nopriv_get_products_list', array(&$this, 'get_products_list') );
  117. add_action( 'wp_ajax_get_products_list', array(&$this, 'get_products_list') );
  118. //Relies on post thumbnails for products
  119. add_action( 'after_setup_theme', array(&$this, 'post_thumbnails'), 9999 );
  120. //Add widgets
  121. if (!$this->get_setting('disable_cart', 0))
  122. add_action( 'widgets_init', create_function('', 'return register_widget("MarketPress_Shopping_Cart");') );
  123. add_action( 'widgets_init', create_function('', 'return register_widget("MarketPress_Product_List");') );
  124. add_action( 'widgets_init', create_function('', 'return register_widget("MarketPress_Categories_Widget");') );
  125. add_action( 'widgets_init', create_function('', 'return register_widget("MarketPress_Tag_Cloud_Widget");') );
  126. // Edit profile
  127. add_action( 'profile_update', array(&$this, 'user_profile_update') );
  128. add_action( 'edit_user_profile', array(&$this, 'user_profile_fields') );
  129. add_action( 'show_user_profile', array(&$this, 'user_profile_fields') );
  130. //update install script if necessary
  131. if (get_option('mp_version') != $this->version) {
  132. $this->install();
  133. }
  134. }
  135. function install() {
  136. $old_settings = get_option('mp_settings');
  137. $old_version = get_option('mp_version');
  138. //our default settings
  139. $default_settings = array (
  140. 'base_country' => 'US',
  141. 'tax' => array (
  142. 'rate' => 0,
  143. 'tax_shipping' => 1,
  144. 'tax_inclusive' => 0
  145. ),
  146. 'currency' => 'USD',
  147. 'curr_symbol_position' => 1,
  148. 'curr_decimal' => 1,
  149. 'disable_cart' => 0,
  150. 'hide_popup' => 0,
  151. 'inventory_threshhold' => 3,
  152. 'max_downloads' => 5,
  153. 'force_login' => 0,
  154. 'ga_ecommerce' => 'none',
  155. 'special_instructions' => 0,
  156. 'store_theme' => 'icons',
  157. 'show_img' => 1,
  158. 'product_img_height' => 150,
  159. 'product_img_width' => 150,
  160. 'list_img_height' => 150,
  161. 'list_img_width' => 150,
  162. 'show_excerpt' => 1,
  163. 'per_page' => 20,
  164. 'order_by' => 'title',
  165. /* Translators: change default slugs here */
  166. 'slugs' => array (
  167. 'store' => __('store', 'mp'),
  168. 'products' => __('products', 'mp'),
  169. 'cart' => __('shopping-cart', 'mp'),
  170. 'orderstatus' => __('order-status', 'mp'),
  171. 'category' => __('category', 'mp'),
  172. 'tag' => __('tag', 'mp')
  173. ),
  174. 'product_button_type' => 'addcart',
  175. 'show_quantity' => 1,
  176. 'product_img_size' => 'medium',
  177. 'show_lightbox' => 1,
  178. 'list_view' => 'grid',
  179. 'list_button_type' => 'addcart',
  180. 'show_thumbnail' => 1,
  181. 'list_img_size' => 'thumbnail',
  182. 'paginate' => 1,
  183. 'show_filters' => 1,
  184. 'order' => 'DESC',
  185. 'show_purchase_breadcrumbs' => 1,
  186. 'shipping' => array (
  187. 'allowed_countries' => array ('CA', 'US'),
  188. 'method' => 'flat-rate',
  189. 'system' => 'english'
  190. ),
  191. 'gateways' => array (
  192. 'paypal-express' => array (
  193. 'locale' => 'US',
  194. 'currency' => 'USD',
  195. 'mode' => 'sandbox'
  196. ),
  197. 'paypal-chained' => array (
  198. 'currency' => 'USD',
  199. 'mode' => 'sandbox'
  200. )
  201. ),
  202. 'msg' => array (
  203. 'product_list' => '',
  204. 'order_status' => __('<p>If you have any questions about your order please do not hesitate to contact us.</p>', 'mp'),
  205. 'cart' => '',
  206. 'shipping' => __('<p>Please enter your shipping information in the form below to proceed with your order.</p>', 'mp'),
  207. 'checkout' => '',
  208. 'confirm_checkout' => __('<p>You are almost done! Please do a final review of your order to make sure everything is correct then click the "Confirm Payment" button.</p>', 'mp'),
  209. 'success' => __('<p>Thank you for your order! We appreciate your business, and please come back often to check out our new products.</p>', 'mp')
  210. ),
  211. 'store_email' => get_option("admin_email"),
  212. 'email' => array (
  213. 'new_order_subject' => __('Your Order Confirmation (ORDERID)', 'mp'),
  214. 'new_order_txt' => __("Thank you for your order CUSTOMERNAME!
  215. Your order has been received, and any items to be shipped will be processed as soon as possible. Please refer to your Order ID (ORDERID) whenever contacting us.
  216. Here is a confirmation of your order details:
  217. Order Information:
  218. ORDERINFO
  219. Shipping Information:
  220. SHIPPINGINFO
  221. Payment Information:
  222. PAYMENTINFO
  223. ORDERNOTES
  224. You can track the latest status of your order here: TRACKINGURL
  225. Thanks again!", 'mp'),
  226. 'shipped_order_subject' => __('Your Order Has Been Shipped! (ORDERID)', 'mp'),
  227. 'shipped_order_txt' => __("Dear CUSTOMERNAME,
  228. Your order has been shipped! Depending on the shipping method and your location it should be arriving shortly. Please refer to your Order ID (ORDERID) whenever contacting us.
  229. Here is a confirmation of your order details:
  230. Order Information:
  231. ORDERINFO
  232. Shipping Information:
  233. SHIPPINGINFO
  234. Payment Information:
  235. PAYMENTINFO
  236. ORDERNOTES
  237. You can track the latest status of your order here: TRACKINGURL
  238. Thanks again!", 'mp')
  239. )
  240. );
  241. //filter default settings
  242. $default_settings = apply_filters( 'mp_default_settings', $default_settings );
  243. $settings = wp_parse_args( (array)$old_settings, $default_settings );
  244. update_option( 'mp_settings', $settings );
  245. //2.1.4 update
  246. if ( version_compare($old_version, '2.1.4', '<') )
  247. $this->update_214();
  248. //only run these on first install
  249. if ( empty($old_settings) ) {
  250. //define settings that don't need to autoload for efficiency
  251. add_option( 'mp_coupons', '', '', 'no' );
  252. add_option( 'mp_store_page', '', '', 'no' );
  253. //create store page
  254. add_action( 'admin_init', array(&$this, 'create_store_page') );
  255. //add cart widget to first sidebar
  256. add_action( 'widgets_init', array(&$this, 'add_default_widget'), 11 );
  257. }
  258. //add action to flush rewrite rules after we've added them for the first time
  259. update_option('mp_flush_rewrite', 1);
  260. update_option( 'mp_version', $this->version );
  261. }
  262. //run on 2.1.4 update to fix price sorts
  263. function update_214() {
  264. global $wpdb;
  265. $posts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product'");
  266. foreach ($posts as $post_id) {
  267. $meta = get_post_custom($post_id);
  268. //unserialize
  269. foreach ($meta as $key => $val) {
  270. $meta[$key] = maybe_unserialize($val[0]);
  271. if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link" && $key != "mp_file" && $key != "mp_price_sort")
  272. $meta[$key] = array($meta[$key]);
  273. }
  274. //fix price sort field if missing
  275. if ( empty($meta["mp_price_sort"]) && is_array($meta["mp_price"]) ) {
  276. if ( $meta["mp_is_sale"] && $meta["mp_sale_price"][0] )
  277. $sort_price = $meta["mp_sale_price"][0];
  278. else
  279. $sort_price = $meta["mp_price"][0];
  280. update_post_meta($post_id, 'mp_price_sort', $sort_price);
  281. }
  282. }
  283. }
  284. function localization() {
  285. // Load up the localization file if we're using WordPress in a different language
  286. // Place it in this plugin's "languages" folder and name it "mp-[value in wp-config].mo"
  287. if ($this->location == 'mu-plugins')
  288. load_muplugin_textdomain( 'mp', '/marketpress-includes/languages/' );
  289. else if ($this->location == 'subfolder-plugins')
  290. load_plugin_textdomain( 'mp', false, '/marketpress/marketpress-includes/languages/' );
  291. else if ($this->location == 'plugins')
  292. load_plugin_textdomain( 'mp', false, '/marketpress-includes/languages/' );
  293. //setup language code for jquery datepicker translation
  294. $temp_locales = explode('_', get_locale());
  295. $this->language = ($temp_locales[0]) ? $temp_locales[0] : 'en';
  296. }
  297. function init_vars() {
  298. //setup proper directories
  299. if (defined('WP_PLUGIN_URL') && defined('WP_PLUGIN_DIR') && file_exists(WP_PLUGIN_DIR . '/marketpress/' . basename(__FILE__))) {
  300. $this->location = 'subfolder-plugins';
  301. $this->plugin_dir = WP_PLUGIN_DIR . '/marketpress/marketpress-includes/';
  302. $this->plugin_url = plugins_url( '/marketpress-includes/', __FILE__ );
  303. } else if (defined('WP_PLUGIN_URL') && defined('WP_PLUGIN_DIR') && file_exists(WP_PLUGIN_DIR . '/' . basename(__FILE__))) {
  304. $this->location = 'plugins';
  305. $this->plugin_dir = WP_PLUGIN_DIR . '/marketpress-includes/';
  306. $this->plugin_url = plugins_url( '/marketpress-includes/', __FILE__ );
  307. } else if (is_multisite() && defined('WPMU_PLUGIN_URL') && defined('WPMU_PLUGIN_DIR') && file_exists(WPMU_PLUGIN_DIR . '/' . basename(__FILE__))) {
  308. $this->location = 'mu-plugins';
  309. $this->plugin_dir = WPMU_PLUGIN_DIR . '/marketpress-includes/';
  310. $this->plugin_url = WPMU_PLUGIN_URL . '/marketpress-includes/';
  311. } else {
  312. wp_die(__('There was an issue determining where MarketPress is installed. Please reinstall.', 'mp'));
  313. }
  314. //load data structures
  315. require_once( $this->plugin_dir . 'marketpress-data.php' );
  316. }
  317. /* Only load code that needs BuddyPress to run once BP is loaded and initialized. */
  318. function load_bp_features() {
  319. include_once( $this->plugin_dir . 'marketpress-bp.php' );
  320. }
  321. function load_importers() {
  322. include_once( $this->plugin_dir . 'marketpress-importers.php' );
  323. }
  324. function load_plugins() {
  325. if (is_network_admin() || !$this->get_setting('disable_cart')) {
  326. //load shipping plugin API
  327. require_once( $this->plugin_dir . 'marketpress-shipping.php' );
  328. $this->load_shipping_plugins();
  329. //load gateway plugin API
  330. require_once( $this->plugin_dir . 'marketpress-gateways.php' );
  331. $this->load_gateway_plugins();
  332. }
  333. }
  334. function load_shipping_plugins() {
  335. //save shipping method. Put here to be before plugin is loaded
  336. if (isset($_POST['shipping_settings'])) {
  337. $settings = get_option('mp_settings');
  338. $settings['shipping']['method'] = $_POST['mp']['shipping']['method'];
  339. $settings['shipping']['calc_methods'] = isset($_POST['mp']['shipping']['calc_methods']) ? $_POST['mp']['shipping']['calc_methods'] : array();
  340. update_option('mp_settings', $settings);
  341. }
  342. //get shipping plugins dir
  343. $dir = $this->plugin_dir . 'plugins-shipping/';
  344. //search the dir for files
  345. $shipping_plugins = array();
  346. if ( !is_dir( $dir ) )
  347. return;
  348. if ( ! $dh = opendir( $dir ) )
  349. return;
  350. while ( ( $plugin = readdir( $dh ) ) !== false ) {
  351. if ( substr( $plugin, -4 ) == '.php' )
  352. $shipping_plugins[] = $dir . $plugin;
  353. }
  354. closedir( $dh );
  355. sort( $shipping_plugins );
  356. //include them suppressing errors
  357. foreach ($shipping_plugins as $file)
  358. @include_once( $file );
  359. //allow plugins from an external location to register themselves
  360. do_action('mp_load_shipping_plugins');
  361. //load chosen plugin class
  362. global $mp_shipping_plugins, $mp_shipping_active_plugins;
  363. $shipping = $this->get_setting('shipping');
  364. if ($this->get_setting('shipping->method') == 'calculated') {
  365. //load just the calculated ones
  366. foreach ((array)$mp_shipping_plugins as $code => $plugin) {
  367. if ($plugin[2]) {
  368. if ( isset($shipping['calc_methods'][$code]) && class_exists($plugin[0]) && !$plugin[3] )
  369. $mp_shipping_active_plugins[$code] = new $plugin[0];
  370. }
  371. }
  372. } else {
  373. //load only and all calculated ones
  374. $class = $mp_shipping_plugins[$shipping['method']][0];
  375. if (class_exists($class))
  376. $mp_shipping_active_plugins[$shipping['method']] = new $class;
  377. }
  378. }
  379. function load_gateway_plugins() {
  380. //save settings from screen. Put here to be before plugin is loaded
  381. if (isset($_POST['gateway_settings'])) {
  382. $settings = get_option('mp_settings');
  383. //see if there are checkboxes checked
  384. if ( isset( $_POST['mp']['gateways']['allowed'] ) ) {
  385. $settings['gateways']['allowed'] = $_POST['mp']['gateways']['allowed'];
  386. } else {
  387. //blank array if no checkboxes
  388. $settings['gateways']['allowed'] = array();
  389. }
  390. update_option('mp_settings', $settings);
  391. }
  392. //get gateway plugins dir
  393. $dir = $this->plugin_dir . 'plugins-gateway/';
  394. //search the dir for files
  395. $gateway_plugins = array();
  396. if ( !is_dir( $dir ) )
  397. return;
  398. if ( ! $dh = opendir( $dir ) )
  399. return;
  400. while ( ( $plugin = readdir( $dh ) ) !== false ) {
  401. if ( substr( $plugin, -4 ) == '.php' )
  402. $gateway_plugins[] = $dir . '/' . $plugin;
  403. }
  404. closedir( $dh );
  405. sort( $gateway_plugins );
  406. //include them suppressing errors
  407. foreach ($gateway_plugins as $file)
  408. include( $file );
  409. //allow plugins from an external location to register themselves
  410. do_action('mp_load_gateway_plugins');
  411. //load chosen plugin classes
  412. global $mp_gateway_plugins, $mp_gateway_active_plugins;
  413. $gateways = $this->get_setting('gateways');
  414. $network_settings = get_site_option( 'mp_network_settings' );
  415. foreach ((array)$mp_gateway_plugins as $code => $plugin) {
  416. $class = $plugin[0];
  417. //if global cart is enabled force it
  418. if ( $this->global_cart ) {
  419. if ( $code == $network_settings['global_gateway'] && class_exists($class) ) {
  420. $mp_gateway_active_plugins[] = new $class;
  421. break;
  422. }
  423. } else {
  424. if ( isset( $gateways['allowed'] ) && in_array($code, (array)$gateways['allowed']) && class_exists($class) && !$plugin[3] )
  425. $mp_gateway_active_plugins[] = new $class;
  426. }
  427. }
  428. }
  429. /*
  430. * function get_setting
  431. * @param string $key A setting key, or -> separated list of keys to go multiple levels into an array
  432. * @param mixed $default Returns when setting is not set
  433. *
  434. * an easy way to get to our settings array without undefined indexes
  435. */
  436. function get_setting($key, $default = null) {
  437. $settings = get_option( 'mp_settings' );
  438. $keys = explode('->', $key);
  439. array_map('trim', $keys);
  440. if (count($keys) == 1)
  441. $setting = isset($settings[$keys[0]]) ? $settings[$keys[0]] : $default;
  442. else if (count($keys) == 2)
  443. $setting = isset($settings[$keys[0]][$keys[1]]) ? $settings[$keys[0]][$keys[1]] : $default;
  444. else if (count($keys) == 3)
  445. $setting = isset($settings[$keys[0]][$keys[1]][$keys[2]]) ? $settings[$keys[0]][$keys[1]][$keys[2]] : $default;
  446. else if (count($keys) == 4)
  447. $setting = isset($settings[$keys[0]][$keys[1]][$keys[2]][$keys[3]]) ? $settings[$keys[0]][$keys[1]][$keys[2]][$keys[3]] : $default;
  448. return apply_filters( "mp_setting_".implode('', $keys), $setting, $default );
  449. }
  450. function update_setting($key, $value) {
  451. $settings = get_option( 'mp_settings' );
  452. $settings[$key] = $value;
  453. return update_option('mp_settings', $settings);
  454. }
  455. function handle_gateway_returns($wp_query) {
  456. if ( is_admin() ) return;
  457. //listen for gateway IPN returns and tie them in to proper gateway plugin
  458. if(!empty($wp_query->query_vars['paymentgateway'])) {
  459. do_action( 'mp_handle_payment_return_' . $wp_query->query_vars['paymentgateway'] );
  460. // exit();
  461. }
  462. }
  463. function remove_canonical($wp_query) {
  464. if ( is_admin() ) return;
  465. //stop canonical problems with virtual pages redirecting
  466. $page = get_query_var('pagename');
  467. if ($page == 'cart' || $page == 'orderstatus' || $page == 'product_list') {
  468. remove_action('template_redirect', 'redirect_canonical');
  469. }
  470. }
  471. function admin_nopermalink_warning() {
  472. //warns admins if permalinks are not enabled on the blog
  473. if ( current_user_can('manage_options') && !get_option('permalink_structure') )
  474. echo '<div class="error"><p>'.__('You must enable Pretty Permalinks</a> to use MarketPress - <a href="options-permalink.php">Enable now &raquo;</a>', 'mp').'</p></div>';
  475. }
  476. function plugin_action_link($links, $file) {
  477. // the anchor tag and href to the URL we want. For a "Settings" link, this needs to be the url of your settings page
  478. $settings_link = '<a href="' . admin_url('edit.php?post_type=product&page=marketpress') . '">' . __('Settings', 'mp') . '</a>';
  479. // add the link to the list
  480. array_unshift($links, $settings_link);
  481. return $links;
  482. }
  483. function add_menu_items() {
  484. //only process the manage orders page for editors and above and if orders hasn't been disabled
  485. if (current_user_can('edit_others_posts') && !$this->get_setting('disable_cart')) {
  486. $num_posts = wp_count_posts('mp_order'); //get pending order count
  487. $count = $num_posts->order_received + $num_posts->order_paid;
  488. if ( $count > 0 )
  489. $count_output = '&nbsp;<span class="update-plugins"><span class="updates-count count-' . $count . '">' . $count . '</span></span>';
  490. else
  491. $count_output = '';
  492. $orders_page = add_submenu_page('edit.php?post_type=product', __('Manage Orders', 'mp'), __('Manage Orders', 'mp') . $count_output, 'edit_others_posts', 'marketpress-orders', array(&$this, 'orders_page'));
  493. }
  494. $page = add_submenu_page('edit.php?post_type=product', __('Store Settings', 'mp'), __('Store Settings', 'mp'), 'manage_options', 'marketpress', array(&$this, 'admin_page'));
  495. add_action( 'admin_print_scripts-' . $page, array(&$this, 'admin_script_settings') );
  496. add_action( 'admin_print_styles-' . $page, array(&$this, 'admin_css_settings') );
  497. add_action( "load-{$page}", array( &$this, 'add_help_tab' ) );
  498. }
  499. function add_help_tab() {
  500. get_current_screen()->add_help_tab( array(
  501. 'id' => 'marketpress-help',
  502. 'title' => 'MarketPress Instructions',
  503. 'content' => '<iframe src="http://premium.wpmudev.org/wdp-un.php?action=help&id=144" width="100%" height="600px"></iframe>'
  504. ) );
  505. }
  506. function admin_css() {
  507. wp_enqueue_style( 'mp-admin-css', $this->plugin_url . 'css/marketpress.css', false, $this->version);
  508. }
  509. //enqeue js on custom post edit screen
  510. function admin_script_post() {
  511. global $current_screen;
  512. if ($current_screen->id == 'product')
  513. wp_enqueue_script( 'mp-post', $this->plugin_url . 'js/post-screen.js', array('jquery'), $this->version);
  514. }
  515. //enqeue css on product settings screen
  516. function admin_css_settings() {
  517. wp_enqueue_style( 'jquery-datepicker-css', $this->plugin_url . 'datepicker/css/ui-lightness/datepicker.css', false, $this->version);
  518. wp_enqueue_style( 'jquery-colorpicker-css', $this->plugin_url . 'colorpicker/css/colorpicker.css', false, $this->version);
  519. }
  520. //enqeue js on product settings screen
  521. function admin_script_settings() {
  522. wp_enqueue_script( 'jquery-colorpicker', $this->plugin_url . 'colorpicker/js/colorpicker.js', array('jquery'), $this->version);
  523. wp_enqueue_script( 'jquery-datepicker', $this->plugin_url . 'datepicker/js/datepicker.min.js', array('jquery', 'jquery-ui-core'), $this->version);
  524. //only load languages for datepicker if not english (or it will show Chinese!)
  525. if ($this->language != 'en')
  526. wp_enqueue_script( 'jquery-datepicker-i18n', $this->plugin_url . 'datepicker/js/datepicker-i18n.min.js', array('jquery', 'jquery-ui-core', 'jquery-datepicker'), $this->version);
  527. if (intval($this->get_setting('hide_popup')) < 3) {
  528. wp_enqueue_script( 'mp-need-help', $this->plugin_url . 'js/need-help.js', array('jquery'), $this->version);
  529. $new_count = intval($this->get_setting('hide_popup')) + 1;
  530. $this->update_setting('hide_popup', $new_count);
  531. }
  532. }
  533. //ties into the ajax request to disable help popup if clicked
  534. function hide_help() {
  535. $this->update_setting('hide_popup', 3);
  536. }
  537. //ajax cart handling for store frontend
  538. function store_script() {
  539. //setup ajax cart javascript
  540. wp_enqueue_script( 'mp-ajax-js', $this->plugin_url . 'js/ajax-cart.js', array('jquery'), $this->version );
  541. // declare the variables we need to access in js
  542. wp_localize_script( 'mp-ajax-js', 'MP_Ajax', array( 'ajaxUrl' => admin_url( 'admin-ajax.php', (is_ssl() ? 'https': 'http') ), 'emptyCartMsg' => __('Are you sure you want to remove all items from your cart?', 'mp'), 'successMsg' => __('Item(s) Added!', 'mp'), 'imgUrl' => $this->plugin_url.'images/loading.gif', 'addingMsg' => __('Adding to your cart...', 'mp'), 'outMsg' => __('In Your Cart', 'mp'), 'show_filters' => $this->get_setting('show_filters') ) );
  543. }
  544. //loads the jquery lightbox plugin
  545. function enqueue_lightbox() {
  546. if ( !$this->get_setting('show_lightbox') )
  547. return;
  548. wp_enqueue_style( 'jquery-lightbox', $this->plugin_url . 'lightbox/style/lumebox.css', false, $this->version );
  549. wp_enqueue_script( 'jquery-lightbox', $this->plugin_url . 'lightbox/js/jquery.lumebox.min.js', array('jquery'), $this->version, true );
  550. // declare the variables we need to access in js
  551. $js_vars = array( 'graphicsDir' => $this->plugin_url . 'lightbox/style/' );
  552. wp_localize_script( 'jquery-lightbox', 'lumeboxOptions', $js_vars );
  553. }
  554. //if cart widget is not in a sidebar, add it to the top of the first sidebar. Only runs at initial install
  555. function add_default_widget() {
  556. if (!is_active_widget(false, false, 'mp_cart_widget')) {
  557. $sidebars_widgets = wp_get_sidebars_widgets();
  558. if ( is_array($sidebars_widgets) ) {
  559. foreach ( $sidebars_widgets as $sidebar => $widgets ) {
  560. if ( 'wp_inactive_widgets' == $sidebar )
  561. continue;
  562. if ( is_array($widgets) ) {
  563. array_unshift($widgets, 'mp_cart_widget-1');
  564. $sidebars_widgets[$sidebar] = $widgets;
  565. wp_set_sidebars_widgets( $sidebars_widgets );
  566. $settings = array();
  567. $settings[1] = array( 'title' => __('Shopping Cart', 'mp'), 'custom_text' => '', 'show_thumbnail' => 1, 'size' => 25 );
  568. $settings['_multiwidget'] = 1;
  569. update_option( 'widget_mp_cart_widget', $settings );
  570. return true;
  571. }
  572. }
  573. }
  574. }
  575. }
  576. //creates the store page on install and updates
  577. function create_store_page($old_slug = false) {
  578. global $wpdb;
  579. //remove old page if updating
  580. if ($old_slug && $old_slug != $this->get_setting('slugs->store')) {
  581. $old_post_id = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '$old_slug' AND post_type = 'page'");
  582. $old_post = get_post($old_post_id);
  583. $old_post->post_name = $this->get_setting('slugs->store');
  584. wp_update_post($old_post);
  585. }
  586. //insert new page if not existing
  587. $page_count = $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->posts . " WHERE post_name = '" . $this->get_setting('slugs->store') . "' AND post_type = 'page'");
  588. if ( !$page_count ) {
  589. //default page content
  590. $content = '<p>' . __('Welcome to our online store! Feel free to browse around:', 'mp') . '</p>';
  591. $content .= '[mp_store_navigation]';
  592. $content .= '<p>' . __('Check out our most popular products:', 'mp') . '</p>';
  593. $content .= '[mp_popular_products]';
  594. $content .= '<p>' . __('Browse by category:', 'mp') . '</p>';
  595. $content .= '[mp_list_categories]';
  596. $content .= '<p>' . __('Browse by tag:', 'mp') . '</p>';
  597. $content .= '[mp_tag_cloud]';
  598. $id = wp_insert_post( array('post_title' => __('Store', 'mp'), 'post_name' => $this->get_setting('slugs->store'), 'post_status' => 'publish', 'post_type' => 'page', 'post_content' => $content ) );
  599. update_option('mp_store_page', $id);
  600. }
  601. }
  602. function register_custom_posts() {
  603. ob_start();
  604. // Register custom taxonomy
  605. register_taxonomy( 'product_category', 'product', apply_filters( 'mp_register_product_category', array("hierarchical" => true, 'label' => __('Product Categories', 'mp'), 'singular_label' => __('Product Category', 'mp'), 'rewrite' => array('slug' => $this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->products') . '/' . $this->get_setting('slugs->category'))) ) );
  606. register_taxonomy( 'product_tag', 'product', apply_filters( 'mp_register_product_tag', array("hierarchical" => false, 'label' => __('Product Tags', 'mp'), 'singular_label' => __('Product Tag', 'mp'), 'rewrite' => array('slug' => $this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->products') . '/' . $this->get_setting('slugs->tag'))) ) );
  607. // Register custom product post type
  608. $supports = array( 'title', 'editor', 'author', 'excerpt', 'revisions', 'thumbnail' );
  609. $args = array (
  610. 'labels' => array('name' => __('Products', 'mp'),
  611. 'singular_name' => __('Product', 'mp'),
  612. 'add_new' => __('Create New', 'mp'),
  613. 'add_new_item' => __('Create New Product', 'mp'),
  614. 'edit_item' => __('Edit Products', 'mp'),
  615. 'edit' => __('Edit', 'mp'),
  616. 'new_item' => __('New Product', 'mp'),
  617. 'view_item' => __('View Product', 'mp'),
  618. 'search_items' => __('Search Products', 'mp'),
  619. 'not_found' => __('No Products Found', 'mp'),
  620. 'not_found_in_trash' => __('No Products found in Trash', 'mp'),
  621. 'view' => __('View Product', 'mp')
  622. ),
  623. 'description' => __('Products for your MarketPress store.', 'mp'),
  624. 'menu_icon' => $this->plugin_url . 'images/marketpress-icon.png',
  625. 'public' => true,
  626. 'show_ui' => true,
  627. 'publicly_queryable' => true,
  628. 'capability_type' => 'page',
  629. 'hierarchical' => false,
  630. 'rewrite' => array('slug' => $this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->products'), 'with_front' => false), // Permalinks format
  631. 'query_var' => true,
  632. 'supports' => $supports
  633. );
  634. register_post_type( 'product' , apply_filters( 'mp_register_post_type', $args ) );
  635. //register the orders post type
  636. register_post_type( 'mp_order', array(
  637. 'labels' => array('name' => __('Orders', 'mp'),
  638. 'singular_name' => __('Order', 'mp'),
  639. 'edit' => __('Edit', 'mp'),
  640. 'view_item' => __('View Order', 'mp'),
  641. 'search_items' => __('Search Orders', 'mp'),
  642. 'not_found' => __('No Orders Found', 'mp')
  643. ),
  644. 'description' => __('Orders from your MarketPress store.', 'mp'),
  645. 'public' => false,
  646. 'show_ui' => false,
  647. 'capability_type' => apply_filters( 'mp_orders_capability', 'page' ),
  648. 'hierarchical' => false,
  649. 'rewrite' => false,
  650. 'query_var' => false,
  651. 'supports' => array()
  652. ) );
  653. //register custom post statuses for our orders
  654. register_post_status( 'order_received', array(
  655. 'label' => __('Received', 'mp'),
  656. 'label_count' => array( __('Received <span class="count">(%s)</span>', 'mp'), __('Received <span class="count">(%s)</span>', 'mp') ),
  657. 'post_type' => 'mp_order',
  658. 'public' => false
  659. ) );
  660. register_post_status( 'order_paid', array(
  661. 'label' => __('Paid', 'mp'),
  662. 'label_count' => array( __('Paid <span class="count">(%s)</span>', 'mp'), __('Paid <span class="count">(%s)</span>', 'mp') ),
  663. 'post_type' => 'mp_order',
  664. 'public' => false
  665. ) );
  666. register_post_status( 'order_shipped', array(
  667. 'label' => __('Shipped', 'mp'),
  668. 'label_count' => array( __('Shipped <span class="count">(%s)</span>', 'mp'), __('Shipped <span class="count">(%s)</span>', 'mp') ),
  669. 'post_type' => 'mp_order',
  670. 'public' => false
  671. ) );
  672. register_post_status( 'order_closed', array(
  673. 'label' => __('Closed', 'mp'),
  674. 'label_count' => array( __('Closed <span class="count">(%s)</span>', 'mp'), __('Closed <span class="count">(%s)</span>', 'mp') ),
  675. 'post_type' => 'mp_order',
  676. 'public' => false
  677. ) );
  678. register_post_status( 'trash', array(
  679. 'label' => _x( 'Trash', 'post' ),
  680. 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ),
  681. 'show_in_admin_status_list' => true,
  682. 'post_type' => 'mp_order',
  683. 'public' => false
  684. ) );
  685. }
  686. //necessary to mod array directly rather than with add_theme_support() to play nice with other themes. See http://www.wptavern.com/forum/plugins-hacks/1751-need-help-enabling-post-thumbnails-custom-post-type.html
  687. function post_thumbnails() {
  688. global $_wp_theme_features;
  689. if( !isset( $_wp_theme_features['post-thumbnails'] ) )
  690. $_wp_theme_features['post-thumbnails'] = array( array( 'product' ) );
  691. else if ( is_array( $_wp_theme_features['post-thumbnails'] ) )
  692. $_wp_theme_features['post-thumbnails'][0][] = 'product';
  693. }
  694. // This function clears the rewrite rules and forces them to be regenerated
  695. function flush_rewrite_check() {
  696. if ( get_option('mp_flush_rewrite') ) {
  697. flush_rewrite_rules();
  698. delete_option('mp_flush_rewrite');
  699. }
  700. }
  701. function add_rewrite_rules($rules) {
  702. $new_rules = array();
  703. //product list
  704. $new_rules[$this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->products') . '/?$'] = 'index.php?pagename=product_list';
  705. $new_rules[$this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->products') . '/page/?([0-9]{1,})/?$'] = 'index.php?pagename=product_list&paged=$matches[1]';
  706. //checkout page
  707. $new_rules[$this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->cart') . '/?$'] = 'index.php?pagename=cart';
  708. $new_rules[$this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->cart') . '/([^/]+)/?$'] = 'index.php?pagename=cart&checkoutstep=$matches[1]';
  709. //order status page
  710. $new_rules[$this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->orderstatus') . '/?$'] = 'index.php?pagename=orderstatus';
  711. $new_rules[$this->get_setting('slugs->store') . '/' . $this->get_setting('slugs->orderstatus') . '/([^/]+)/?$'] = 'index.php?pagename=orderstatus&order_id=$matches[1]';
  712. //ipn handling for payment gateways
  713. $new_rules[$this->get_setting('slugs->store') . '/payment-return/(.+)'] = 'index.php?paymentgateway=$matches[1]';
  714. return array_merge($new_rules, $rules);
  715. }
  716. //unfortunately some plugins flush rewrites before the init hook so they kill custom post type rewrites. This function verifies they are in the final array and flushes if not
  717. function check_rewrite_rules($value) {
  718. //prevent an infinite loop by only
  719. if ( ! post_type_exists( 'product' ) )
  720. return $value;
  721. if ( is_array($value) && !in_array('index.php?product=$matches[1]&paged=$matches[2]', $value) ) {
  722. flush_rewrite_rules();
  723. } else {
  724. return $value;
  725. }
  726. }
  727. function add_queryvars($vars) {
  728. // This function add the checkout queryvars to the list that WordPress is looking for.
  729. if(!in_array('checkoutstep', $vars))
  730. $vars[] = 'checkoutstep';
  731. if(!in_array('order_id', $vars))
  732. $vars[] = 'order_id';
  733. if(!in_array('paymentgateway', $vars))
  734. $vars[] = 'paymentgateway';
  735. return $vars;
  736. }
  737. function start_session() {
  738. //start the sessions for cart handling
  739. if (session_id() == "")
  740. session_start();
  741. }
  742. function logout_clear_session() {
  743. $this->start_session();
  744. //clear personal info
  745. unset($_SESSION['mp_shipping_info']);
  746. unset($_SESSION['mp_billing_info']);
  747. //remove coupon code
  748. if (is_multisite()) {
  749. global $blog_id;
  750. unset($_SESSION['mp_cart_coupon_' . $blog_id]);
  751. } else {
  752. unset($_SESSION['mp_cart_coupon']);
  753. }
  754. }
  755. //scans post type at template_redirect to apply custom themeing to products
  756. function load_store_templates() {
  757. global $wp_query, $mp_wpmu, $mp_gateway_active_plugins;
  758. //only filter public side
  759. if (is_admin()) return;
  760. //load proper theme for single product page display
  761. if ($wp_query->is_single && $wp_query->query_vars['post_type'] == 'product') {
  762. //check for custom theme templates
  763. $product_name = get_query_var('product');
  764. $product_id = (int) $wp_query->get_queried_object_id();
  765. //serve download if it exists
  766. $this->serve_download($product_id);
  767. $templates = array();
  768. if ( $product_name )
  769. $templates[] = "mp_product-$product_name.php";
  770. if ( $product_id )
  771. $templates[] = "mp_product-$product_id.php";
  772. $templates[] = "mp_product.php";
  773. //if custom template exists load it
  774. if ($this->product_template = locate_template($templates)) {
  775. add_filter( 'template_include', array(&$this, 'custom_product_template') );
  776. } else {
  777. //otherwise load the page template and use our own theme
  778. $wp_query->is_single = null;
  779. $wp_query->is_page = 1;
  780. add_filter( 'the_content', array(&$this, 'product_theme'), 99 );
  781. //genesis fixes
  782. remove_action( 'genesis_post_content', 'genesis_do_post_image' );
  783. remove_action( 'genesis_post_content', 'genesis_do_post_content' );
  784. add_action('genesis_post_content', 'the_content');
  785. }
  786. $this->is_shop_page = true;
  787. //enqueue lightbox on single product page
  788. $this->enqueue_lightbox();
  789. }
  790. //load proper theme for main store page
  791. $slugs = $this->get_setting('slugs');
  792. if ($wp_query->query_vars['pagename'] == $this->get_setting('slugs->store')) {
  793. //check for custom theme template
  794. $templates = array("mp_store.php");
  795. //if custom template exists load it
  796. if ($this->store_template = locate_template($templates)) {
  797. add_filter( 'template_include', array(&$this, 'custom_store_template') );
  798. } else {
  799. //otherwise load the page template and use our own theme
  800. add_filter( 'the_content', array(&$this, 'store_theme'), 99 );
  801. }
  802. $this->is_shop_page = true;
  803. }
  804. //load proper theme for checkout page
  805. if ($wp_query->query_vars['pagename'] == 'cart') {
  806. //init session for store pages
  807. $this->start_session();
  808. //process cart updates
  809. $this->update_cart();
  810. //if global cart is on forward to main site checkout
  811. if ( $this->global_cart && is_object($mp_wpmu) && !$mp_wpmu->is_main_site() ) {
  812. wp_redirect( mp_cart_link(false, true) );
  813. exit;
  814. }
  815. // Redirect to https if forced to use SSL by a payment gateway
  816. if (get_query_var('checkoutstep')) {
  817. foreach ((array)$mp_gateway_active_plugins as $plugin) {
  818. if ($plugin->force_ssl) {
  819. if ( !is_ssl() ) {
  820. wp_redirect('https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  821. exit();
  822. }
  823. }
  824. }
  825. }
  826. //force login if required
  827. if (!is_user_logged_in() && $this->get_setting('force_login') && get_query_var('checkoutstep')) {
  828. wp_redirect( wp_login_url( mp_checkout_step_url( get_query_var('checkoutstep') ) ) );
  829. exit();
  830. }
  831. //setup shopping cart javascript
  832. wp_enqueue_script( 'mp-store-js', $this->plugin_url . 'js/store.js', array('jquery'), $this->version );
  833. //check for custom theme template
  834. $templates = array("mp_cart.php");
  835. //if custom template exists load it
  836. if ($this->checkout_template = locate_template($templates)) {
  837. add_filter( 'template_include', array(&$this, 'custom_checkout_template') );
  838. add_filter( 'single_post_title', array(&$this, 'page_title_output'), 99 );
  839. add_filter( 'bp_page_title', array(&$this, 'page_title_output'), 99 );
  840. add_filter( 'wp_title', array(&$this, 'wp_title_output'), 19, 3 );
  841. } else {
  842. //otherwise load the page template and use our own theme
  843. add_filter( 'single_post_title', array(&$this, 'page_title_output'), 99 );
  844. add_filter( 'the_title', array(&$this, 'page_title_output'), 99 );
  845. add_filter( 'bp_page_title', array(&$this, 'page_title_output'), 99 );
  846. add_filter( 'wp_title', array(&$this, 'wp_title_output'), 19, 3 );
  847. add_filter( 'the_content', array(&$this, 'checkout_theme'), 99 );
  848. }
  849. $wp_query->is_page = 1;
  850. $wp_query->is_singular = 1;
  851. $wp_query->is_404 = null;
  852. $wp_query->post_count = 1;
  853. $this->is_shop_page = true;
  854. }
  855. //load proper theme for order status page
  856. if ($wp_query->query_vars['pagename'] == 'orderstatus') {
  857. //check for custom theme template
  858. $templates = array("mp_orderstatus.php");
  859. //if custom template exists load it
  860. if ($this->orderstatus_template = locate_template($templates)) {
  861. add_filter( 'template_include', array(&$this, 'custom_orderstatus_template') );
  862. add_filter( 'single_post_title', array(&$this, 'page_title_output'), 99 );
  863. add_filter( 'bp_page_title', array(&$this, 'page_title_output'), 99 );
  864. add_filter( 'wp_title', array(&$this, 'wp_title_output'), 19, 3 );
  865. } else {
  866. //otherwise load the page template and use our own theme
  867. add_filter( 'single_post_title', array(&$this, 'page_title_output'), 99 );
  868. add_filter( 'the_title', array(&$this, 'page_title_output'), 99 );
  869. add_filter( 'bp_page_title', array(&$this, 'page_title_output'), 99 );
  870. add_filter( 'wp_title', array(&$this, 'wp_title_output'), 19, 3 );
  871. add_filter( 'the_content', array(&$this, 'orderstatus_theme'), 99 );
  872. }
  873. $wp_query->is_page = 1;
  874. $wp_query->is_singular = 1;
  875. $wp_query->is_404 = null;
  876. $wp_query->post_count = 1;
  877. $this->is_shop_page = true;
  878. }
  879. //load proper theme for product listings
  880. if ($wp_query->query_vars['pagename'] == 'product_list') {
  881. //check for custom theme template
  882. $templates = array("mp_productlist.php");
  883. //if custom template exists load it
  884. if ($this->product_list_template = locate_template($templates)) {
  885. //call a custom query posts for this listing
  886. //setup pagination
  887. if ($this->get_setting('paginate')) {
  888. //figure out perpage
  889. $paginate_query = '&posts_per_page='.$this->get_setting('per_page');
  890. //figure out page
  891. if ($wp_query->query_vars['paged'])
  892. $paginate_query .= '&paged='.intval($wp_query->query_vars['paged']);
  893. } else {
  894. $paginate_query = '&nopaging=true';
  895. }
  896. //get order by
  897. if ($this->get_setting('order_by') == 'price')
  898. $order_by_query = '&meta_key=mp_price&orderby=mp_price';
  899. else if ($this->get_setting('order_by') == 'sales')
  900. $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';
  901. else
  902. $order_by_query = '&orderby='.$this->get_setting('order_by');
  903. //get order direction
  904. $order_query = '&order='.$this->get_setting('order');
  905. //The Query
  906. query_posts('post_type=product' . $paginate_query . $order_by_query . $order_query);
  907. add_filter( 'template_include', array(&$this, 'custom_product_list_template') );
  908. add_filter( 'single_post_title', array(&$this, 'page_title_output'), 99 );
  909. add_filter( 'bp_page_title', array(&$this, 'page_title_output'), 99 );
  910. add_filter( 'wp_title', array(&$this, 'wp_title_output'), 19, 3 );
  911. } else {
  912. //otherwise load the page template and use our own theme
  913. add_filter( 'single_post_title', array(&$this, 'page_title_output'), 99 );
  914. add_filter( 'the_title', array(&$this, 'page_title_output'), 99 );
  915. add_filter( 'bp_page_title', array(&$this, 'page_title_output'), 99 );
  916. add_filter( 'wp_title', array(&$this, 'wp_title_output'), 19, 3 );
  917. add_filter( 'the_content', array(&$this, 'product_list_theme'), 99 );
  918. add_filter( 'the_excerpt', array(&$this, 'product_list_theme'), 99 );
  919. //genesis fixes
  920. remove_action( 'genesis_post_content', 'genesis_do_post_image' );
  921. remove_action( 'genesis_post_content', 'genesis_do_post_content' );
  922. add_action('genesis_post_content', 'the_content');
  923. }
  924. $wp_query->is_page = 1;
  925. //$wp_query->is_singular = 1;
  926. $wp_query->is_404 = null;
  927. $wp_query->post_count = 1;
  928. $this->is_shop_page = true;
  929. }
  930. //load proper theme for product category or tag listings
  931. if ( isset( $wp_query->query_vars['taxonomy'] ) && ( $wp_query->query_vars['taxonomy'] == 'product_category' || $wp_query->query_vars['taxonomy'] == 'product_tag' ) ) {
  932. $templates = array();
  933. if ($wp_query->query_vars['taxonomy'] == 'product_category') {
  934. $cat_name = get_query_var('product_category');
  935. $cat_id = absint( $wp_query->get_queried_object_id() );
  936. if ( $cat_name )
  937. $templates[] = "mp_category-$cat_name.php";
  938. if ( $cat_id )
  939. $templates[] = "mp_category-$cat_id.php";
  940. $templates[] = "mp_category.php";
  941. } else if ($wp_query->query_vars['taxonomy'] == 'product_tag') {
  942. $tag_name = get_query_var('product_tag');
  943. $tag_id = absint( $wp_query->get_queried_object_id() );
  944. if ( $tag_name )
  945. $templates[] = "mp_tag-$tag_name.php";
  946. if ( $tag_id )
  947. $templates[] = "mp_tag-$tag_id.php";
  948. $templates[] = "mp_tag.php";
  949. }
  950. //defaults
  951. $templates[] = "mp_taxonomy.php";
  952. $templates[] = "mp_productlist.php";
  953. if ( !is_admin() && isset($_GET['product_category']) && is_numeric($_GET['product_category']) ) {
  954. $link = get_term_link( (int)get_query_var($wp_query->query_vars['taxonomy']), $wp_query->query_vars['taxonomy'] );
  955. wp_redirect($link);
  956. exit;
  957. }
  958. //if custom template exists load it
  959. if ($this->product_taxonomy_template = locate_template($templates)) {
  960. //call a custom query posts for this listing
  961. $taxonomy_query = '&' . $wp_query->query_vars['taxonomy'] . '=' . get_query_var($wp_query->query_vars['taxonomy']);
  962. //setup pagination
  963. if ($this->get_setting('paginate')) {
  964. //figure out perpage
  965. $paginate_query = '&posts_per_page='.$this->get_setting('per_page');
  966. //figure out page
  967. if ($wp_query->query_vars['paged'])
  968. $paginate_query .= '&paged='.intval($wp_query->query_vars['paged']);
  969. } else {
  970. $paginate_query = '&nopaging=true';
  971. }
  972. //get order by
  973. if ($this->get_setting('order_by') == 'price')
  974. $order_by_query = '&meta_key=mp_price&orderby=mp_price';
  975. else if ($this->get_setting('order_by') == 'sales')
  976. $order_by_query = '&meta_key=mp_sales_count&orderby=mp_sales_count';
  977. else
  978. $order_by_query = '&orderby='.$this->get_setting('order_by');
  979. //get order direction
  980. $order_query = '&order='.$this->get_setting('order');
  981. //The Query
  982. query_posts('post_type=product' . $taxonomy_query . $paginate_query . $order_by_query . $order_query);
  983. add_filter( 'template_include', array(&$this, 'custom_product_taxonomy_template'));
  984. add_filter( 'single_post_title', array(&$this, 'page_title_output'), 99 );
  985. add_filter( 'bp_page_title', array(&$this, 'page_title_output'), 99 );
  986. add_filter( 'wp_title', array(&$this, 'wp_title_output'), 19, 3 );
  987. } else {
  988. //otherwise load the page template and use our own list theme. We don't use theme's taxonomy as not enough control
  989. $wp_query->is_page = 1;
  990. //$wp_query->is_singular = 1;
  991. $wp_query->is_404 = n

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