PageRenderTime 118ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
  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 = null;
  992. $wp_query->post_count = 1;
  993. //var_dump($wp_query);
  994. $wp_query->queried_object_id = get_option('mp_store_page');
  995. add_filter( 'single_post_title', array(&$this, 'page_title_output'), 99 );
  996. add_filter( 'bp_page_title', array(&$this, 'page_title_output'), 99 );
  997. add_filter( 'wp_title', array(&$this, 'wp_title_output'), 19, 3 );
  998. add_filter( 'the_title', array(&$this, 'page_title_output'), 99, 2 );
  999. add_filter( 'the_content', array(&$this, 'product_taxonomy_list_theme'), 99 );
  1000. add_filter( 'the_excerpt', array(&$this, 'product_taxonomy_list_theme'), 99 );
  1001. //genesis fixes
  1002. remove_action( 'genesis_post_content', 'genesis_do_post_image' );
  1003. remove_action( 'genesis_post_content', 'genesis_do_post_content' );
  1004. add_action('genesis_post_content', 'the_content');
  1005. }
  1006. $this->is_shop_page = true;
  1007. }
  1008. //load shop specific items
  1009. if ($this->is_shop_page) {
  1010. //fixes a nasty bug in BP theme's functions.php file which always loads the activity stream if not a normal page
  1011. remove_all_filters('page_template');
  1012. //prevents 404 for virtual pages
  1013. status_header( 200 );
  1014. }
  1015. }
  1016. //loads the selected theme css files
  1017. function load_store_theme() {
  1018. if ( $this->get_setting('store_theme') == 'none' || current_theme_supports('mp_style') ) {
  1019. return;
  1020. } else if (file_exists($this->plugin_dir . 'themes/' . $this->get_setting('store_theme') . '.css')) {
  1021. wp_enqueue_style( 'mp-store-theme', $this->plugin_url . 'themes/' . $this->get_setting('store_theme') . '.css', false, $this->version );
  1022. } else if (file_exists(WP_CONTENT_DIR . '/marketpress-styles/' . $this->get_setting('store_theme') . '.css')) {
  1023. wp_enqueue_style( 'mp-store-theme', WP_CONTENT_URL . '/marketpress-styles/' . $this->get_setting('store_theme') . '.css', false, $this->version );
  1024. }
  1025. }
  1026. //list store themes in dropdown
  1027. function store_themes_select() {
  1028. //get theme dir
  1029. $theme_dir = $this->plugin_dir . 'themes/';
  1030. //scan directory for theme css files
  1031. $theme_list = array();
  1032. if ($handle = @opendir($theme_dir)) {
  1033. while (false !== ($file = readdir($handle))) {
  1034. if (($pos = strrpos($file, '.css')) !== false) {
  1035. $value = substr($file, 0, $pos);
  1036. if (is_readable("$theme_dir/$file")) {
  1037. $theme_data = get_file_data( "$theme_dir/$file", array('name' => 'MarketPress Style') );
  1038. if (is_array($theme_data))
  1039. $theme_list[$value] = $theme_data['name'];
  1040. }
  1041. }
  1042. }
  1043. @closedir($handle);
  1044. }
  1045. //scan wp-content/marketpress-styles/ directory for theme css files
  1046. $theme_dir = WP_CONTENT_DIR . '/marketpress-styles/';
  1047. if ($handle = @opendir($theme_dir)) {
  1048. while (false !== ($file = readdir($handle))) {
  1049. if (($pos = strrpos($file, '.css')) !== false) {
  1050. $value = substr($file, 0, $pos);
  1051. if (is_readable("$theme_dir/$file")) {
  1052. $theme_data = get_file_data( "$theme_dir/$file", array('name' => 'MarketPress Style') );
  1053. if (is_array($theme_data))
  1054. $theme_list[$value] = $theme_data['name'];
  1055. }
  1056. }
  1057. }
  1058. @closedir($handle);
  1059. }
  1060. //sort the themes
  1061. asort($theme_list);
  1062. //check network permissions
  1063. if (is_multisite()) {
  1064. $allowed_list = array();
  1065. $network_settings = get_site_option( 'mp_network_settings' );
  1066. foreach ($theme_list as $value => $name) {
  1067. if ($network_settings['allowed_themes'][$value] == 'full')
  1068. $allowed_list[$value] = $name;
  1069. else if ($network_settings['allowed_themes'][$value] == 'supporter' && function_exists('is_pro_site') && is_pro_site(false, $network_settings['themes_pro_level'][$value]))
  1070. $allowed_list[$value] = $name;
  1071. else if (is_super_admin()) //super admins can access all installed themes
  1072. $allowed_list[$value] = $name;
  1073. }
  1074. $theme_list = $allowed_list;
  1075. }
  1076. echo '<select name="mp[store_theme]">';
  1077. foreach ($theme_list as $value => $name) {
  1078. ?><option value="<?php echo $value ?>"<?php selected($this->get_setting('store_theme'), $value) ?>><?php echo $name ?></option><?php
  1079. }
  1080. ?>
  1081. <option value="none"<?php selected($this->get_setting('store_theme'), 'none') ?>><?php _e('None - Custom theme template', 'mp') ?></option>
  1082. </select>
  1083. <?php
  1084. }
  1085. //filter the custom single product template
  1086. function custom_product_template($template) {
  1087. return $this->product_template;
  1088. }
  1089. //filter the custom store template
  1090. function custom_store_template($template) {
  1091. return $this->store_template;
  1092. }
  1093. //filter the custom checkout template
  1094. function custom_checkout_template($template) {
  1095. return $this->checkout_template;
  1096. }
  1097. //filter the custom orderstatus template
  1098. function custom_orderstatus_template($template) {
  1099. return $this->orderstatus_template;
  1100. }
  1101. //filter the custom product taxonomy template
  1102. function custom_product_taxonomy_template($template) {
  1103. return $this->product_taxonomy_template;
  1104. }
  1105. //filter the custom product list template
  1106. function custom_product_list_template($template) {
  1107. return $this->product_list_template;
  1108. }
  1109. //adds our links to theme nav menus using wp_list_pages()
  1110. function filter_list_pages($list, $args) {
  1111. if ($args['depth'] == 1)
  1112. return $list;
  1113. $temp_break = strpos($list, mp_store_link(false, true) . '"');
  1114. //if we can't find the page for some reason skip
  1115. if ($temp_break === false)
  1116. return $list;
  1117. $break = strpos($list, '</a>', $temp_break) + 4;
  1118. $nav = substr($list, 0, $break);
  1119. if ( !$this->get_setting('disable_cart') ) {
  1120. $nav .= '<ul class="children"><li class="page_item'. ((get_query_var('pagename') == 'product_list') ? ' current_page_item' : '') . '"><a href="' . mp_products_link(false, true) . '" title="' . __('Products', 'mp') . '">' . __('Products', 'mp') . '</a></li>';
  1121. $nav .= '<li class="page_item'. ((get_query_var('pagename') == 'cart') ? ' current_page_item' : '') . '"><a href="' . mp_cart_link(false, true) . '" title="' . __('Shopping Cart', 'mp') . '">' . __('Shopping Cart', 'mp') . '</a></li>';
  1122. $nav .= '<li class="page_item'. ((get_query_var('pagename') == 'orderstatus') ? ' current_page_item' : '') . '"><a href="' . mp_orderstatus_link(false, true) . '" title="' . __('Order Status', 'mp') . '">' . __('Order Status', 'mp') . '</a></li>
  1123. </ul>
  1124. ';
  1125. } else {
  1126. $nav .= '
  1127. <ul>
  1128. <li class="page_item'. ((get_query_var('pagename') == 'product_list') ? ' current_page_item' : '') . '"><a href="' . mp_products_link(false, true) . '" title="' . __('Products', 'mp') . '">' . __('Products', 'mp') . '</a></li>
  1129. </ul>
  1130. ';
  1131. }
  1132. $nav .= substr($list, $break);
  1133. return $nav;
  1134. }
  1135. //adds our links to custom theme nav menus using wp_nav_menu()
  1136. function filter_nav_menu($list, $args = array()) {
  1137. $store_object = false;
  1138. if ($args->depth == 1)
  1139. return $list;
  1140. //find store page
  1141. $store_url = mp_store_link(false, true);
  1142. $store_page = get_option('mp_store_page');
  1143. foreach($list as $menu_item) {
  1144. if ($menu_item->object_id == $store_page || $menu_item->url == $store_url) {
  1145. $store_object = $menu_item;
  1146. break;
  1147. }
  1148. }
  1149. if ($store_object) {
  1150. $obj_products = clone $store_object;
  1151. $obj_products->title = __('Products', 'mp');
  1152. $obj_products->menu_item_parent = $store_object->ID;
  1153. $obj_products->ID = '99999999999';
  1154. $obj_products->db_id = '99999999999';
  1155. $obj_products->post_name = '99999999999';
  1156. $obj_products->url = mp_products_link(false, true);
  1157. $obj_products->current = (get_query_var('pagename') == 'product_list') ? true : false;
  1158. $obj_products->current_item_ancestor = (get_query_var('pagename') == 'product_list') ? true : false;
  1159. $list[] = $obj_products;
  1160. //if cart disabled return only the products menu item
  1161. if ($this->get_setting('disable_cart'))
  1162. return $list;
  1163. $obj_cart = clone $store_object;
  1164. $obj_cart->title = __('Shopping Cart', 'mp');
  1165. $obj_cart->menu_item_parent = $store_object->ID;
  1166. $obj_cart->ID = '99999999999';
  1167. $obj_cart->db_id = '99999999999';
  1168. $obj_cart->post_name = '99999999999';
  1169. $obj_cart->url = mp_cart_link(false, true);
  1170. $obj_cart->current = (get_query_var('pagename') == 'cart') ? true : false;
  1171. $obj_cart->current_item_ancestor = (get_query_var('pagename') == 'cart') ? true : false;
  1172. $list[] = $obj_cart;
  1173. $obj_order = clone $store_object;
  1174. $obj_order->title = __('Order Status', 'mp');
  1175. $obj_order->menu_item_parent = $store_object->ID;
  1176. $obj_order->ID = '99999999999';
  1177. $obj_order->db_id = '99999999999';
  1178. $obj_order->post_name = '99999999999';
  1179. $obj_order->url = mp_orderstatus_link(false, true);
  1180. $obj_order->current = (get_query_var('pagename') == 'orderstatus') ? true : false;
  1181. $obj_order->current_item_ancestor = (get_query_var('pagename') == 'orderstatus') ? true : false;
  1182. $list[] = $obj_order;
  1183. }
  1184. return $list;
  1185. }
  1186. function wp_title_output($title, $sep, $seplocation) {
  1187. // Determines position of the separator and direction of the breadcrumb
  1188. if ( 'right' == $seplocation )
  1189. return $this->page_title_output($title, true) . " $sep ";
  1190. else
  1191. return " $sep " . $this->page_title_output($title, true);
  1192. }
  1193. //filters the titles for our custom pages
  1194. function page_title_output($title, $id = false) {
  1195. global $wp_query;
  1196. //filter out nav titles
  1197. if (!empty($title) && $id === false)
  1198. return $title;
  1199. //taxonomy pages
  1200. if (isset($wp_query->query_vars['taxonomy']) && ($wp_query->query_vars['taxonomy'] == 'product_category' || $wp_query->query_vars['taxonomy'] == 'product_tag') && $wp_query->post->ID == $id) {
  1201. if ($wp_query->query_vars['taxonomy'] == 'product_category') {
  1202. $term = get_term_by('slug', get_query_var('product_category'), 'product_category');
  1203. return sprintf( __('Product Category: %s', 'mp'), $term->name );
  1204. } else if ($wp_query->query_vars['taxonomy'] == 'product_tag') {
  1205. $term = get_term_by('slug', get_query_var('product_tag'), 'product_tag');
  1206. return sprintf( __('Product Tag: %s', 'mp'), $term->name );
  1207. }
  1208. }
  1209. switch ($wp_query->query_vars['pagename']) {
  1210. case 'cart':
  1211. if ( isset($wp_query->query_vars['checkoutstep']) ) {
  1212. if ($wp_query->query_vars['checkoutstep'] == 'shipping')
  1213. return $this->download_only_cart($this->get_cart_contents()) ? __('Checkout Information', 'mp') : __('Shipping Information', 'mp');
  1214. else if ($wp_query->query_vars['checkoutstep'] == 'checkout')
  1215. return __('Payment Information', 'mp');
  1216. else if ($wp_query->query_vars['checkoutstep'] == 'confirm-checkout')
  1217. return __('Confirm Your Purchase', 'mp');
  1218. else if ($wp_query->query_vars['checkoutstep'] == 'confirmation')
  1219. return __('Order Confirmation', 'mp');
  1220. else
  1221. return __('Your Shopping Cart', 'mp');
  1222. } else {
  1223. return __('Your Shopping Cart', 'mp');
  1224. }
  1225. break;
  1226. case 'orderstatus':
  1227. return __('Track Your Order', 'mp');
  1228. break;
  1229. case 'product_list':
  1230. return __('Products', 'mp');
  1231. break;
  1232. default:
  1233. return $title;
  1234. }
  1235. }
  1236. //this is the default theme added to single product listings
  1237. function product_theme($content) {
  1238. global $post;
  1239. //don't filter outside of the loop
  1240. if ( !in_the_loop() )
  1241. return $content;
  1242. //add thumbnail
  1243. if ($this->get_setting('show_img')) {
  1244. $content = mp_product_image( false, 'single' ) . $content;
  1245. }
  1246. $content .= '<div class="mp_product_meta">';
  1247. $content .= mp_product_price(false);
  1248. $content .= mp_buy_button(false, 'single');
  1249. $content .= '</div>';
  1250. $content .= mp_category_list($post->ID, '<div class="mp_product_categories">' . __( 'Categorized in ', 'mp' ), ', ', '</div>');
  1251. //$content .= mp_tag_list($post->ID, '<div class="mp_product_tags">', ', ', '</div>');
  1252. return $content;
  1253. }
  1254. //this is the default theme added to the checkout page
  1255. function store_theme($content) {
  1256. //don't filter outside of the loop
  1257. if ( !in_the_loop() )
  1258. return $content;
  1259. return $content;
  1260. }
  1261. //this is the default theme added to the checkout page
  1262. function checkout_theme($content) {
  1263. global $wp_query;
  1264. //don't filter outside of the loop
  1265. if ( !in_the_loop() )
  1266. return $content;
  1267. $content = mp_show_cart('checkout', null, false);
  1268. return $content;
  1269. }
  1270. //this is the default theme added to the order status page
  1271. function orderstatus_theme($content) {
  1272. //don't filter outside of the loop
  1273. if ( !in_the_loop() )
  1274. return $content;
  1275. mp_order_status();
  1276. return $content;
  1277. }
  1278. //this is the default theme added to product listings
  1279. function product_list_theme($content) {
  1280. //don't filter outside of the loop
  1281. if ( !in_the_loop() )
  1282. return $content;
  1283. $msgs = $this->get_setting('msg');
  1284. $content .= do_shortcode($msgs['product_list']);
  1285. $content .= $this->get_setting('show_filters')==1 ? mp_products_filter() : '';
  1286. $content .= mp_list_products(false);
  1287. $content .= '<div id="mp_product_nav">' . get_posts_nav_link() . '</div>';
  1288. return $content;
  1289. }
  1290. //this is the default theme added to product taxonomies
  1291. function product_taxonomy_list_theme($content) {
  1292. //don't filter outside of the loop
  1293. if ( !in_the_loop() )
  1294. return $content;
  1295. $msgs = $this->get_setting('msg');
  1296. $content = do_shortcode($msgs['product_list']);
  1297. $content .= $this->get_setting('show_filters')==1 ? mp_products_filter() : '';
  1298. $content .= mp_list_products(false);
  1299. $content .= '<div id="mp_product_nav">' . get_posts_nav_link() . '</div>';
  1300. return $content;
  1301. }
  1302. /**
  1303. * ajax handler
  1304. * @return string html of products list, and optionally pagination
  1305. */
  1306. function get_products_list(){
  1307. global $wp_query;
  1308. $ret = array('products'=>false, 'pagination'=>false);
  1309. extract(array(
  1310. 'paginate' => '',
  1311. 'page' => 1,
  1312. 'per_page' => '',
  1313. 'order_by' => '',
  1314. 'order' => '',
  1315. 'category' => '',
  1316. 'tag' => '',
  1317. 'list_view'=> false));
  1318. if( isset($_POST['order']) ){
  1319. $o = explode('-',$_POST['order']);
  1320. // column
  1321. if(isset($o[0]) && in_array($o[0], array('date','title','price','sales'))){
  1322. $order_by = $o[0];
  1323. }
  1324. // direction
  1325. if(isset($o[1]) && in_array($o[1], array('asc','desc'))){
  1326. $order = strtoupper($o[1]);
  1327. }
  1328. }
  1329. if( isset($_POST['filter-term']) && is_numeric($_POST['filter-term']) && $_POST['filter-term']!=-1){
  1330. $term = get_term_by( 'id', $_POST['filter-term'], 'product_category' );
  1331. $category = $term->slug;
  1332. }
  1333. if( isset($_POST['page']) && is_numeric($_POST['page']) ){
  1334. $page = $_POST['page'];
  1335. }
  1336. $ret['products'] = mp_list_products(false, $paginate, $page, $per_page, $order_by, $order, $category, $tag, $list_view);
  1337. if($this->get_setting('paginate')){
  1338. // get_posts_nav_link() does not work with ajax
  1339. $max = $wp_query->max_num_pages;
  1340. $prev=$next='';
  1341. if($max > 1){
  1342. if( $page != $max ){
  1343. $next='<a href="#paged='.($page+1).'">'.__('Next Page &raquo;').'</a>';
  1344. }
  1345. if($page != 1){
  1346. $prev='<a href="#paged='.($page-1).'">'.__('&laquo; Previous Page').'</a>';
  1347. }
  1348. $ret['pagination'] = '<div id="mp_product_nav">' . $prev . (strlen($prev)>0 && strlen($next)>0?' &#8212; ':'') . $next . '</div>';
  1349. }
  1350. }
  1351. header('Content-type: application/json');
  1352. echo json_encode($ret);
  1353. exit;
  1354. }
  1355. //adds the "filter by product category" to the edit products screen
  1356. function edit_products_filter() {
  1357. global $current_screen;
  1358. if ( $current_screen->id == 'edit-product' ) {
  1359. $selected_category = !empty( $_GET['product_category'] ) ? $_GET['product_category'] : null;
  1360. $dropdown_options = array('taxonomy' => 'product_category', 'show_option_all' => __('View all categories'), 'hide_empty' => 0, 'hierarchical' => 1,
  1361. 'show_count' => 0, 'orderby' => 'name', 'name' => 'product_category', 'selected' => $selected_category );
  1362. wp_dropdown_categories($dropdown_options);
  1363. }
  1364. }
  1365. //adjusts the query vars on the products/order management screens.
  1366. function handle_edit_screen_filter($request) {
  1367. if ( is_admin() ) {
  1368. global $current_screen;
  1369. if ( $current_screen->id == 'edit-product' ) {
  1370. //Switches the product_category ids to slugs as you can't query custom taxonomys with ids
  1371. if ( !empty( $request['product_category'] ) ) {
  1372. $cat = get_term_by('id', $request['product_category'], 'product_category');
  1373. $request['product_category'] = $cat->slug;
  1374. }
  1375. } else if ( $current_screen->id == 'product_page_marketpress-orders' && !isset($_GET['post_status']) ) {
  1376. //set the post status when on "All" to everything but closed
  1377. $request['post_status'] = 'order_received,order_paid,order_shipped';
  1378. }
  1379. }
  1380. return $request;
  1381. }
  1382. //adds our custom column headers to edit products screen
  1383. function edit_products_columns($old_columns) {
  1384. global $post_status;
  1385. $columns['cb'] = '<input type="checkbox" />';
  1386. $columns['thumbnail'] = __('Thumbnail', 'mp');
  1387. $columns['title'] = __('Product Name', 'mp');
  1388. $columns['variations'] = __('Variations', 'mp');
  1389. $columns['sku'] = __('SKU', 'mp');
  1390. $columns['pricing'] = __('Price', 'mp');
  1391. if ( !$this->get_setting('disable_cart') ) {
  1392. $columns['stock'] = __('Stock', 'mp');
  1393. $columns['sales'] = __('Sales', 'mp');
  1394. }
  1395. $columns['product_categories'] = __('Product Categories', 'mp');
  1396. $columns['product_tags'] = __('Product Tags', 'mp');
  1397. /*
  1398. if ( !in_array( $post_status, array('pending', 'draft', 'future') ) )
  1399. $columns['reviews'] = __('Reviews', 'mp');
  1400. //*/
  1401. return $columns;
  1402. }
  1403. //adds our custom column content
  1404. function edit_products_custom_columns($column) {
  1405. global $post;
  1406. //$screen = get_current_screen();
  1407. //echo "screen->id=[". $screen->id ."]<br />";
  1408. //apply_filters( 'bulk_actions-' . $screen->id, $this->_actions );
  1409. $meta = get_post_custom();
  1410. //unserialize
  1411. foreach ($meta as $key => $val) {
  1412. $meta[$key] = maybe_unserialize($val[0]);
  1413. if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link")
  1414. $meta[$key] = array($meta[$key]);
  1415. }
  1416. switch ($column) {
  1417. case "thumbnail":
  1418. echo '<a href="' . get_edit_post_link() . '" title="' . __('Edit &raquo;') . '">';
  1419. if (has_post_thumbnail()) {
  1420. the_post_thumbnail(array(50,50), array('title' => ''));
  1421. } else {
  1422. echo '<img width="50" height="50" src="'.apply_filters('mp_default_product_img', $this->plugin_url.'images/default-product.png').'">';
  1423. }
  1424. echo '</a>';
  1425. break;
  1426. case "variations":
  1427. if (isset($meta["mp_var_name"]) && is_array($meta["mp_var_name"]) && count($meta["mp_var_name"]) > 1) {
  1428. foreach ($meta["mp_var_name"] as $value) {
  1429. echo esc_attr($value) . '<br />';
  1430. }
  1431. } else {
  1432. _e('N/A', 'mp');
  1433. }
  1434. break;
  1435. case "sku":
  1436. if (isset($meta["mp_var_name"]) && is_array($meta["mp_var_name"])) {
  1437. foreach ((array)$meta["mp_sku"] as $value) {
  1438. echo esc_attr($value) . '<br />';
  1439. }
  1440. } else {
  1441. _e('N/A', 'mp');
  1442. }
  1443. break;
  1444. case "pricing":
  1445. if (isset($meta["mp_price"]) && is_array($meta["mp_price"])) {
  1446. foreach ($meta["mp_price"] as $key => $value) {
  1447. if (isset($meta["mp_is_sale"]) && $meta["mp_is_sale"] && isset($meta["mp_sale_price"][$key])) {
  1448. echo '<del>'.$this->format_currency('', $value).'</del> ';
  1449. echo $this->format_currency('', $meta["mp_sale_price"][$key]) . '<br />';
  1450. } else {
  1451. echo $this->format_currency('', $value) . '<br />';
  1452. }
  1453. }
  1454. } else {
  1455. echo $this->format_currency('', 0);
  1456. }
  1457. break;
  1458. case "sales":
  1459. echo number_format_i18n(isset($meta["mp_sales_count"][0]) ? $meta["mp_sales_count"][0] : 0);
  1460. break;
  1461. case "stock":
  1462. if (isset($meta["mp_track_inventory"]) && $meta["mp_track_inventory"]) {
  1463. foreach ((array)$meta["mp_inventory"] as $value) {
  1464. $inventory = ($value) ? $value : 0;
  1465. if ($inventory == 0)
  1466. $class = 'mp-inv-out';
  1467. else if ($inventory <= $this->get_setting('inventory_threshhold'))
  1468. $class = 'mp-inv-warn';
  1469. else
  1470. $class = 'mp-inv-full';
  1471. echo '<span class="' . $class . '">' . number_format_i18n($inventory) . '</span><br />';
  1472. }
  1473. } else {
  1474. _e('N/A', 'mp');
  1475. }
  1476. break;
  1477. case "product_categories":
  1478. echo mp_category_list();
  1479. break;
  1480. case "product_tags":
  1481. echo mp_tag_list();
  1482. break;
  1483. case "reviews":
  1484. echo '<div class="post-com-count-wrapper">
  1485. <a href="edit-comments.php?p=913" title="0 pending" class="post-com-count"><span class="comment-count">0</span></a>
  1486. </div>';
  1487. break;
  1488. }
  1489. }
  1490. // Adds a custom row action for Copying/Cloning a Product
  1491. function edit_products_custom_row_actions($actions, $post) {
  1492. $action = 'copy-product';
  1493. if ( ($post->post_type == "product") && (!isset($actions[$action])) ) {
  1494. $post_type_object = get_post_type_object( $post->post_type );
  1495. if ( $post_type_object ) {
  1496. if ( current_user_can('edit_pages') ) {
  1497. $copy_link = add_query_arg( 'action', $action );
  1498. $copy_link = add_query_arg( 'post', $post->ID, $copy_link );
  1499. $copy_link = wp_nonce_url( $copy_link, "{$action}-{$post->post_type}_{$post->ID}" );
  1500. $actions[$action] = '<a href="'. $copy_link .'">'. __('Copy', 'mp') .'</a>';
  1501. }
  1502. }
  1503. }
  1504. return $actions;
  1505. }
  1506. function edit_products_copy_action() {
  1507. $action = 'copy-product';
  1508. if ((isset($_GET['action'])) && ($_GET['action'] == "copy-product")) {
  1509. $sendback_href = remove_query_arg( array('_wpnonce', 'mp-action', 'post', 'trashed', 'untrashed', 'deleted', 'ids'), wp_get_referer() );
  1510. if (isset($_GET['post']))
  1511. $product_id = intval($_GET['post']);
  1512. else
  1513. wp_redirect($sendback_href);
  1514. if (isset($_GET['post_type']))
  1515. $post_type = esc_attr($_GET['post_type']);
  1516. else
  1517. wp_redirect($sendback_href);
  1518. if ( (!isset($_GET['_wpnonce'])) || !wp_verify_nonce($_GET['_wpnonce'], "{$action}-{$post_type}_{$product_id}") )
  1519. wp_redirect($sendback_href);
  1520. $product = (array)get_post_to_edit( $product_id );
  1521. $product['ID'] = 0; // Zero out the Product ID to force insert of new item
  1522. $product['post_status'] = 'draft';
  1523. $product['post_author'] = get_current_user_id();
  1524. $new_product_id = wp_insert_post($product);
  1525. if (($new_product_id) && (!is_wp_error($$new_product_id))) {
  1526. //If we have the a valid new product ID we copy the product meta...
  1527. $product_meta_keys = get_post_custom_keys($product_id);
  1528. if (!empty($product_meta_keys)) {
  1529. foreach ($product_meta_keys as $meta_key) {
  1530. $meta_values = get_post_custom_values($meta_key, $product_id);
  1531. foreach ($meta_values as $meta_value) {
  1532. $meta_value = maybe_unserialize($meta_value);
  1533. add_post_meta($new_product_id, $meta_key, $meta_value);
  1534. }
  1535. }
  1536. }
  1537. // ... thne we copy the product taxonomy terms
  1538. $product_taxonomies = get_object_taxonomies($post_type);
  1539. if (!empty($product_taxonomies)) {
  1540. foreach ($product_taxonomies as $product_taxonomy) {
  1541. $product_terms = wp_get_object_terms($product_id, $product_taxonomy, array( 'orderby' => 'term_order' ));
  1542. if (($product_terms) && (count($product_terms))) {
  1543. $terms = array();
  1544. foreach($product_terms as $product_term)
  1545. $terms[] = $product_term->slug;
  1546. }
  1547. wp_set_object_terms($new_product_id, $terms, $product_taxonomy);
  1548. }
  1549. }
  1550. }
  1551. }
  1552. wp_redirect($sendback_href);
  1553. die();
  1554. }
  1555. //adds our custom column headers
  1556. function manage_orders_columns($old_columns) {
  1557. global $post_status;
  1558. $columns['cb'] = '<input type="checkbox" />';
  1559. $columns['mp_orders_status'] = __('Status', 'mp');
  1560. $columns['mp_orders_id'] = __('Order ID', 'mp');
  1561. $columns['mp_orders_date'] = __('Order Date', 'mp');
  1562. $columns['mp_orders_name'] = __('From', 'mp');
  1563. $columns['mp_orders_items'] = __('Items', 'mp');
  1564. $columns['mp_orders_shipping'] = __('Shipping', 'mp');
  1565. $columns['mp_orders_tax'] = __('Tax', 'mp');
  1566. $columns['mp_orders_discount'] = __('Discount', 'mp');
  1567. $columns['mp_orders_total'] = __('Total', 'mp');
  1568. return $columns;
  1569. }
  1570. //adds our custom column content
  1571. function manage_orders_custom_columns($column) {
  1572. global $post;
  1573. $meta = get_post_custom();
  1574. //unserialize
  1575. foreach ($meta as $key => $val)
  1576. $meta[$key] = array_map('maybe_unserialize', $val);
  1577. switch ($column) {
  1578. case "mp_orders_status":
  1579. if ($post->post_status == 'order_received')
  1580. $text = __('Received', 'mp');
  1581. else if ($post->post_status == 'order_paid')
  1582. $text = __('Paid', 'mp');
  1583. else if ($post->post_status == 'order_shipped')
  1584. $text = __('Shipped', 'mp');
  1585. else if ($post->post_status == 'order_closed')
  1586. $text = __('Closed', 'mp');
  1587. else if ($post->post_status == 'trash')
  1588. $text = __('Trashed', 'mp');
  1589. ?><a class="mp_order_status" href="edit.php?post_type=product&page=marketpress-orders&order_id=<?php echo $post->ID; ?>" title="<?php echo __('View Order Details', 'mp'); ?>"><?php echo $text ?></a><?php
  1590. break;
  1591. case "mp_orders_date":
  1592. $t_time = get_the_time(__('Y/m/d g:i:s A'));
  1593. $m_time = $post->post_date;
  1594. $time = get_post_time('G', true, $post);
  1595. $time_diff = time() - $time;
  1596. if ( $time_diff > 0 && $time_diff < 24*60*60 )
  1597. $h_time = sprintf( __('%s ago'), human_time_diff( $time ) );
  1598. else
  1599. $h_time = mysql2date(__('Y/m/d'), $m_time);
  1600. echo '<abbr title="' . $t_time . '">' . $h_time . '</abbr>';
  1601. break;
  1602. case "mp_orders_id":
  1603. $title = _draft_or_post_title();
  1604. ?>
  1605. <strong><a class="row-title" href="edit.php?post_type=product&page=marketpress-orders&order_id=<?php echo $post->ID; ?>" title="<?php echo esc_attr(sprintf(__('View &#8220;%s&#8221;', 'mp'), $title)); ?>"><?php echo $title ?></a></strong>
  1606. <?php
  1607. $actions = array();
  1608. if ($post->post_status == 'order_received') {
  1609. $actions['paid'] = "<a title='" . esc_attr(__('Mark as Paid', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=paid&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Paid', 'mp') . "</a>";
  1610. $actions['shipped'] = "<a title='" . esc_attr(__('Mark as Shipped', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=shipped&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Shipped', 'mp') . "</a>";
  1611. $actions['closed'] = "<a title='" . esc_attr(__('Mark as Closed', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=closed&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Closed', 'mp') . "</a>";
  1612. } else if ($post->post_status == 'order_paid') {
  1613. $actions['shipped'] = "<a title='" . esc_attr(__('Mark as Shipped', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=shipped&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Shipped', 'mp') . "</a>";
  1614. $actions['closed'] = "<a title='" . esc_attr(__('Mark as Closed', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=closed&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Closed', 'mp') . "</a>";
  1615. } else if ($post->post_status == 'order_shipped') {
  1616. $actions['closed'] = "<a title='" . esc_attr(__('Mark as Closed', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=closed&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Closed', 'mp') . "</a>";
  1617. } else if ($post->post_status == 'order_closed') {
  1618. $actions['received'] = "<a title='" . esc_attr(__('Mark as Received', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=received&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Received', 'mp') . "</a>";
  1619. $actions['paid'] = "<a title='" . esc_attr(__('Mark as Paid', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=paid&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Paid', 'mp') . "</a>";
  1620. $actions['shipped'] = "<a title='" . esc_attr(__('Mark as Shipped', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=shipped&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Shipped', 'mp') . "</a>";
  1621. }
  1622. if ((isset($_GET['post_status'])) && ($_GET['post_status'] == "trash")) {
  1623. $actions['delete'] = "<a title='" . esc_attr(__('Delete', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=delete&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Delete Permanently', 'mp') . "</a>";
  1624. } else {
  1625. $actions['trash'] = "<a title='" . esc_attr(__('Trash', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=trash&amp;post=' . $post->ID), 'update-order-status' ) . "'>" . __('Trash', 'mp') . "</a>";
  1626. }
  1627. $action_count = count($actions);
  1628. $i = 0;
  1629. echo '<div class="row-actions">';
  1630. foreach ( $actions as $action => $link ) {
  1631. ++$i;
  1632. ( $i == $action_count ) ? $sep = '' : $sep = ' | ';
  1633. echo "<span class='$action'>$link$sep</span>";
  1634. }
  1635. echo '</div>';
  1636. break;
  1637. case "mp_orders_name":
  1638. echo esc_attr($meta["mp_shipping_info"][0]['name']) . ' (<a href="mailto:' . urlencode($meta["mp_shipping_info"][0]['name']) . ' &lt;' . esc_attr($meta["mp_shipping_info"][0]['email']) . '&gt;?subject=' . urlencode(sprintf(__('Regarding Your Order (%s)', 'mp'), $post->post_title)) . '">' . esc_attr($meta["mp_shipping_info"][0]['email']) . '</a>)';
  1639. break;
  1640. case "mp_orders_items":
  1641. echo number_format_i18n($meta["mp_order_items"][0]);
  1642. break;
  1643. case "mp_orders_shipping":
  1644. echo $this->format_currency('', $meta["mp_shipping_total"][0]);
  1645. break;
  1646. case "mp_orders_tax":
  1647. echo $this->format_currency('', $meta["mp_tax_total"][0]);
  1648. break;
  1649. case "mp_orders_discount":
  1650. if (isset($meta["mp_discount_info"][0]) && $meta["mp_discount_info"][0])
  1651. echo $meta["mp_discount_info"][0]['discount'];
  1652. else
  1653. _e('N/A', 'mp');
  1654. break;
  1655. case "mp_orders_total":
  1656. echo $this->format_currency('', $meta["mp_order_total"][0]);
  1657. break;
  1658. }
  1659. }
  1660. //filters label in new product title field
  1661. function filter_title($post) {
  1662. global $post_type;
  1663. if ($post_type != 'product')
  1664. return $post;
  1665. return __( 'Enter Product title here', 'mp' );
  1666. }
  1667. //adds our custom meta boxes the the product edit screen
  1668. function meta_boxes() {
  1669. global $wp_meta_boxes;
  1670. add_meta_box('mp-meta-details', __('Product Details', 'mp'), array(&$this, 'meta_details'), 'product', 'normal', 'high');
  1671. //only add these boxes if orders are enabled
  1672. if (!$this->get_setting('disable_cart')) {
  1673. //only display metabox if shipping plugin ties into it
  1674. if ( has_action('mp_shipping_metabox') )
  1675. add_meta_box('mp-meta-shipping', __('Shipping', 'mp'), array(&$this, 'meta_shipping'), 'product', 'normal', 'high');
  1676. //for product downloads
  1677. add_meta_box('mp-meta-download', __('Product Download', 'mp'), array(&$this, 'meta_download'), 'product', 'normal', 'high');
  1678. }
  1679. //all this junk is to reorder the metabox array to move the featured image box to the top right below submit box. User order will override
  1680. if ( isset( $wp_meta_boxes['product']['side']['low']['postimagediv'] ) ) {
  1681. $imagediv = $wp_meta_boxes['product']['side']['low']['postimagediv'];
  1682. unset( $wp_meta_boxes['product']['side']['low'] );
  1683. $submitdiv = $wp_meta_boxes['product']['side']['core']['submitdiv'];
  1684. unset( $wp_meta_boxes['product']['side']['core']['submitdiv'] );
  1685. $new_core['submitdiv'] = $submitdiv;
  1686. $new_core['postimagediv'] = $imagediv;
  1687. $wp_meta_boxes['product']['side']['core'] = array_merge( $new_core, $wp_meta_boxes['product']['side']['core']) ;
  1688. //filter title
  1689. $wp_meta_boxes['product']['side']['core']['postimagediv']['title'] = __('Product Image', 'mp');
  1690. }
  1691. }
  1692. //Save our post meta when a product is created or updated
  1693. function save_product_meta($post_id, $post = null) {
  1694. //skip quick edit
  1695. if ( defined('DOING_AJAX') )
  1696. return;
  1697. if ( $post->post_type == "product" && isset( $_POST['mp_product_meta'] ) ) {
  1698. $meta = get_post_custom($post_id);
  1699. foreach ($meta as $key => $val) {
  1700. $meta[$key] = maybe_unserialize($val[0]);
  1701. if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link")
  1702. $meta[$key] = array($meta[$key]);
  1703. }
  1704. //price function
  1705. $func_curr = '$price = round(preg_replace("/[^0-9.]/", "", $price), 2);return ($price) ? $price : 0;';
  1706. //sku function
  1707. $func_sku = 'return preg_replace("/[^a-zA-Z0-9_-]/", "", $value);';
  1708. update_post_meta($post_id, 'mp_var_name', $_POST['mp_var_name']);
  1709. update_post_meta($post_id, 'mp_sku', array_map(create_function('$value', $func_sku), (array)$_POST['mp_sku']));
  1710. update_post_meta($post_id, 'mp_price', array_map(create_function('$price', $func_curr), (array)$_POST['mp_price']));
  1711. update_post_meta($post_id, 'mp_is_sale', isset($_POST['mp_is_sale']) ? 1 : 0);
  1712. update_post_meta($post_id, 'mp_sale_price', array_map(create_function('$price', $func_curr), (array)$_POST['mp_sale_price']));
  1713. update_post_meta($post_id, 'mp_track_inventory', isset($_POST['mp_track_inventory']) ? 1 : 0);
  1714. update_post_meta($post_id, 'mp_inventory', array_map('intval', (array)$_POST['mp_inventory']));
  1715. //personalization fields
  1716. $mp_has_custom_field = $mp_custom_field_required = array();
  1717. foreach ((array)$_POST['mp_price'] as $key => $null) {
  1718. $mp_has_custom_field[$key] = isset($_POST['mp_has_custom_field'][$key]) ? 1 : 0;
  1719. $mp_custom_field_required[$key] = isset($_POST['mp_custom_field_required'][$key]) ? 1 : 0;
  1720. }
  1721. update_post_meta($post_id, 'mp_has_custom_field', $mp_has_custom_field);
  1722. update_post_meta($post_id, 'mp_custom_field_required', $mp_custom_field_required);
  1723. if (isset($_POST['mp_custom_field_per'])) {
  1724. update_post_meta($post_id, 'mp_custom_field_per', (array)$_POST['mp_custom_field_per']);
  1725. }
  1726. if (isset($_POST['mp_custom_field_label'])) {
  1727. update_post_meta($post_id, 'mp_custom_field_label', array_map('trim', (array)$_POST['mp_custom_field_label']));
  1728. }
  1729. //save true first variation price for sorting
  1730. if ( isset($_POST['mp_is_sale']) )
  1731. $sort_price = round($_POST['mp_sale_price'][0], 2);
  1732. else
  1733. $sort_price = round($_POST['mp_price'][0], 2);
  1734. update_post_meta($post_id, 'mp_price_sort', $sort_price);
  1735. //if changing delete flag so emails will be sent again
  1736. if ( $_POST['mp_inventory'] != $meta['mp_inventory'] )
  1737. delete_post_meta($product_id, 'mp_stock_email_sent');
  1738. update_post_meta( $post_id, 'mp_product_link', esc_url_raw($_POST['mp_product_link']) );
  1739. update_post_meta($post_id, 'mp_is_special_tax', isset($_POST['mp_is_special_tax']) ? 1 : 0);
  1740. $tax_rate = round(preg_replace("/[^0-9.]/", "", $_POST['mp_special_tax']), 3) * .01;
  1741. update_post_meta($post_id, 'mp_special_tax', $tax_rate);
  1742. //set sales count to zero if none set
  1743. $sale_count = ($meta["mp_sales_count"][0]) ? $meta["mp_sales_count"][0] : 0;
  1744. update_post_meta($post_id, 'mp_sales_count', $sale_count);
  1745. //for shipping plugins to save their meta values
  1746. $mp_shipping = maybe_unserialize($meta["mp_shipping"][0]);
  1747. if ( !is_array($mp_shipping) )
  1748. $mp_shipping = array();
  1749. update_post_meta( $post_id, 'mp_shipping', apply_filters('mp_save_shipping_meta', $mp_shipping) );
  1750. //download url
  1751. update_post_meta( $post_id, 'mp_file', esc_url_raw($_POST['mp_file']) );
  1752. //for any other plugin to hook into
  1753. do_action( 'mp_save_product_meta', $post_id, $meta );
  1754. }
  1755. }
  1756. // Make sure that product meta keys are present, set to sensible defaults
  1757. function get_meta_details( $post_id ) {
  1758. $meta = get_post_custom($post_id);
  1759. //unserialize
  1760. foreach ($meta as $key => $val) {
  1761. $meta[$key] = maybe_unserialize($val[0]);
  1762. if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link" && $key != "mp_file" && $key != "mp_is_special_tax" && $key != "mp_special_tax")
  1763. $meta[$key] = array($meta[$key]);
  1764. }
  1765. $defaults = array(
  1766. 'mp_is_sale' => '',
  1767. 'mp_track_inventory' => '',
  1768. 'mp_price' => array(),
  1769. 'mp_product_link' => '',
  1770. 'mp_is_special_tax' => '',
  1771. 'mp_file' => '',
  1772. 'mp_shipping' => array('')
  1773. );
  1774. //Set default value if key is not already set.
  1775. return wp_parse_args($meta, $defaults);
  1776. }
  1777. //The Product Details meta box
  1778. function meta_details() {
  1779. global $post;
  1780. $meta = $this->get_meta_details( $post->ID );
  1781. ?>
  1782. <input type="hidden" name="mp_product_meta" value="1" />
  1783. <table class="widefat" id="mp_product_variations_table">
  1784. <thead>
  1785. <tr>
  1786. <th scope="col" class="mp_var_col"><?php _e('Variation Name', 'mp') ?></th>
  1787. <th scope="col" class="mp_sku_col" title="<?php _e('Stock Keeping Unit - Your custom Product ID number', 'mp'); ?>"><?php _e('SKU', 'mp') ?></th>
  1788. <th scope="col" class="mp_price_col"><?php _e('Price', 'mp') ?></th>
  1789. <th scope="col" class="mp_sale_col"><label title="<?php _e('When checked these override the normal price.', 'mp'); ?>"><input type="checkbox" id="mp_is_sale" name="mp_is_sale" value="1"<?php checked($meta["mp_is_sale"], '1'); ?> /> <?php _e('Sale Price', 'mp') ?></label></th>
  1790. <th scope="col" class="mp_inv_col"><label title="<?php _e('When checked inventory tracking will be enabled.', 'mp'); ?>"><input type="checkbox" id="mp_track_inventory" name="mp_track_inventory" value="1"<?php checked($meta["mp_track_inventory"], '1'); ?> /> <?php _e('Inventory', 'mp') ?></label></th>
  1791. <th scope="col" class="mp_custom_field_col" title="<?php _e('Should this product be personalizable by the customer?', 'mp'); ?>"><?php _e('Personalize', 'mp') ?></th>
  1792. <th scope="col" class="mp_var_remove"></th>
  1793. </tr>
  1794. </thead>
  1795. <tbody>
  1796. <?php
  1797. if (isset($meta["mp_price"]) && $meta["mp_price"]) {
  1798. //if download enabled only show first variation
  1799. $meta["mp_price"] = (empty($meta["mp_file"]) && empty($meta["mp_product_link"])) ? $meta["mp_price"] : array($meta["mp_price"][0]);
  1800. $count = 1;
  1801. $last = count($meta["mp_price"]);
  1802. foreach ($meta["mp_price"] as $key => $price) {
  1803. ?>
  1804. <tr class="variation">
  1805. <td class="mp_var_col"><input type="text" name="mp_var_name[]" value="<?php echo esc_attr($meta["mp_var_name"][$key]); ?>" /></td>
  1806. <td class="mp_sku_col"><input type="text" name="mp_sku[]" value="<?php echo esc_attr($meta["mp_sku"][$key]); ?>" /></td>
  1807. <td class="mp_price_col"><?php echo $this->format_currency(); ?><input type="text" name="mp_price[]" value="<?php echo isset($meta["mp_price"][$key]) ? $this->display_currency($meta["mp_price"][$key]) : '0.00'; ?>" /></td>
  1808. <td class="mp_sale_col"><?php echo $this->format_currency(); ?><input type="text" name="mp_sale_price[]" value="<?php echo isset($meta["mp_sale_price"][$key]) ? $this->display_currency($meta["mp_sale_price"][$key]) : $this->display_currency($meta["mp_price"][$key]); ?>" disabled="disabled" /></td>
  1809. <td class="mp_inv_col"><input type="text" name="mp_inventory[]" value="<?php echo isset($meta["mp_inventory"][$key]) ? intval($meta["mp_inventory"][$key]) : 0; ?>" disabled="disabled" /></td>
  1810. <td class="mp_custom_field_col"><input type="checkbox" class="mp_has_custom_field" name="mp_has_custom_field[<?php echo $key; ?>]" value="1" <?php checked(isset($meta['mp_has_custom_field'][$key]) && $meta['mp_has_custom_field'][$key]); ?> /></td>
  1811. <td class="mp_var_remove">
  1812. <?php if ($count == $last) { ?><a href="#mp_product_variations_table" title="<?php _e('Remove Variation', 'mp'); ?>">x</a><?php } ?>
  1813. </td>
  1814. </tr>
  1815. <tr class="variation-custom-field <?php echo (!isset($meta['mp_has_custom_field'][$key]) || !$meta['mp_has_custom_field'][$key]) ? ' variation-custom-field-hidden' : ''; ?>">
  1816. <td class="mp_custom_label_col" colspan="1">
  1817. <input type="hidden" class="mp_custom_field_type" name="mp_custom_field_type[<?php echo $key; ?>]" value="input" />
  1818. <input type="hidden" class="mp_custom_field_per" name="mp_custom_field_per[<?php echo $key; ?>]" value="quantity" />
  1819. <label class="mp_custom_field_label"><?php _e('Description:', 'mp'); ?></label>
  1820. <input type="text" class="mp_custom_field_value" name="mp_custom_field_label[<?php echo $key; ?>]" value="<?php echo isset($meta['mp_custom_field_label'][$key]) ? esc_attr($meta['mp_custom_field_label'][$key]) : ''; ?>" />
  1821. <label class="mp_custom_field_required_label">
  1822. <input type="checkbox" class="mp_custom_field_required" name="mp_custom_field_required[<?php echo $key; ?>]" value="1" <?php echo checked(isset($meta['mp_custom_field_required'][$key]) && $meta['mp_custom_field_required'][$key]); ?> /> <?php _e('Required', 'mp'); ?></label>
  1823. </td>
  1824. <td>&nbsp;</td>
  1825. </tr>
  1826. <?php
  1827. $count++;
  1828. }
  1829. } else {
  1830. ?>
  1831. <tr class="variation">
  1832. <td class="mp_var_col"><input type="text" name="mp_var_name[]" value="" /></td>
  1833. <td class="mp_sku_col"><input type="text" name="mp_sku[]" value="" /></td>
  1834. <td class="mp_price_col"><?php echo $this->format_currency(); ?><input type="text" name="mp_price[]" value="0.00" /></td>
  1835. <td class="mp_sale_col"><?php echo $this->format_currency(); ?><input type="text" name="mp_sale_price[]" value="0.00" disabled="disabled" /></td>
  1836. <td class="mp_inv_col"><input type="text" name="mp_inventory[]" value="0" disabled="disabled" /></td>
  1837. <td class="mp_custom_field_col"><input type="checkbox" class="mp_has_custom_field" name="mp_has_custom_field[]" value="1" /></td>
  1838. <td class="mp_var_remove"><a href="#mp_product_variations_table" title="<?php _e('Remove Variation', 'mp'); ?>">x</a></td>
  1839. </tr>
  1840. <tr class="variation-custom-field variation-custom-field-hidden">
  1841. <td class="mp_custom_label_col" colspan="5">
  1842. <input type="hidden" class="mp_custom_field_type" name="mp_custom_field_type[]" value="input" />
  1843. <input type="hidden" class="mp_custom_field_per" name="mp_custom_field_per[]" value="quantity" />
  1844. <label class="mp_custom_field_label"><?php _e('Description:', 'mp'); ?></label> <input type="text" class="mp_custom_field_value" name="mp_custom_field_label[]" value="" />
  1845. <input type="checkbox" class="mp_custom_field_required" name="mp_custom_field_required[]" value="1" /> <label class="mp_custom_field_required_label"><?php _e('Required:', 'mp'); ?></label>
  1846. </td>
  1847. <td>&nbsp;</td>
  1848. </tr>
  1849. <?php
  1850. }
  1851. ?>
  1852. </tbody>
  1853. </table>
  1854. <?php if (empty($meta["mp_file"]) && empty($meta["mp_product_link"])) { ?>
  1855. <div id="mp_add_vars"><a href="#mp_product_variations_table"><?php _e('Add Variation', 'mp'); ?></a></div>
  1856. <?php } else { ?>
  1857. <span class="description" id="mp_variation_message"><?php _e('Product variations are not allowed for Downloadable or Externally Linked products.', 'mp') ?></span>
  1858. <?php } ?>
  1859. <div id="mp_product_link_div">
  1860. <label title="<?php _e('Some examples are linking to a song/album in iTunes, or linking to a product on another site with your own affiliate link.', 'mp'); ?>"><?php _e('External Link', 'mp'); ?>:<br /><small><?php _e('When set this overrides the purchase button with a link to this URL.', 'mp'); ?></small><br />
  1861. <input type="text" style="width: 100%;" id="mp_product_link" name="mp_product_link" value="<?php echo esc_url($meta["mp_product_link"]); ?>" /></label>
  1862. </div>
  1863. <div id="mp_tax_rate_div">
  1864. <label title="<?php esc_attr_e('Depending on local tax laws, some items are tax-free or a have different sales tax rate. You can set that here.', 'mp'); ?>"><input type="checkbox" id="mp_is_special_tax" name="mp_is_special_tax" value="1" <?php checked($meta["mp_is_special_tax"]); ?>/> <?php _e('Special Tax Rate?', 'mp'); ?></label>
  1865. <label id="mp_special_tax"<?php echo ($meta["mp_is_special_tax"]) ? '' : ' style="display:none;"'; ?>><?php _e('Rate:', 'mp'); ?> <input type="text" size="2" name="mp_special_tax" value="<?php echo isset($meta["mp_special_tax"]) ? round($meta["mp_special_tax"] * 100, 3) : 0; ?>" />%</label>
  1866. </div>
  1867. <?php do_action( 'mp_details_metabox' ); ?>
  1868. <div class="clear"></div>
  1869. <?php
  1870. }
  1871. //The Shipping meta box
  1872. function meta_shipping() {
  1873. global $post;
  1874. $settings = get_option('mp_settings');
  1875. $mp_shipping = get_post_meta($post->ID, 'mp_shipping', true);
  1876. $mp_shipping = $mp_shipping ? maybe_unserialize($mp_shipping) : array();
  1877. //tie in for shipping plugins
  1878. do_action( 'mp_shipping_metabox', $mp_shipping, $settings );
  1879. }
  1880. //The Product Download meta box
  1881. function meta_download() {
  1882. global $post;
  1883. $file = get_post_meta($post->ID, 'mp_file', true);
  1884. ?>
  1885. <label><?php _e('File URL', 'mp'); ?>:<br /><input type="text" size="50" id="mp_file" class="mp_file" name="mp_file" value="<?php echo esc_attr($file); ?>" /></label>
  1886. <input id="mp_upload_button" type="button" value="<?php _e('Upload File', 'mp'); ?>" /><br />
  1887. <?php
  1888. //display allowed filetypes if WPMU
  1889. if (is_multisite()) {
  1890. echo '<span class="description">Allowed Filetypes: '.implode(', ', explode(' ', get_site_option('upload_filetypes'))).'</span>';
  1891. if (is_super_admin()) {
  1892. echo '<p>Super Admin: You can change allowed filetypes for your network <a href="' . network_admin_url('settings.php#upload_filetypes') . '">here &raquo;</a></p>';
  1893. }
  1894. }
  1895. do_action( 'mp_download_metabox' );
  1896. }
  1897. //returns the calculated price adjusted for sales, formatted or not
  1898. function product_price($product_id, $variation = 0, $format = false) {
  1899. $meta = get_post_custom($product_id);
  1900. //unserialize
  1901. foreach ($meta as $key => $val) {
  1902. $meta[$key] = maybe_unserialize($val[0]);
  1903. if (!is_array($meta[$key]) && $key != "mp_is_sale" && $key != "mp_track_inventory" && $key != "mp_product_link")
  1904. $meta[$key] = array($meta[$key]);
  1905. }
  1906. if (is_array($meta["mp_price"])) {
  1907. if ($meta["mp_is_sale"]) {
  1908. $price = $meta["mp_sale_price"][$variation];
  1909. } else {
  1910. $price = $meta["mp_price"][$variation];
  1911. }
  1912. }
  1913. $price = ($price) ? $price : 0;
  1914. $price = $this->display_currency($price);
  1915. $price = apply_filters( 'mp_product_price', $price, $product_id );
  1916. if ($format)
  1917. return $this->format_currency('', $price);
  1918. else
  1919. return $price;
  1920. }
  1921. //returns the calculated price for shipping. Returns False if shipping address is not available
  1922. function shipping_price($format = false, $cart = false) {
  1923. global $mp_shipping_active_plugins;
  1924. //grab cart for just this blog
  1925. if (!$cart)
  1926. $cart = $this->get_cart_contents();
  1927. //get total after any coupons
  1928. $totals = array();
  1929. foreach ($cart as $product_id => $variations) {
  1930. foreach ($variations as $variation => $data) {
  1931. $totals[] = $this->before_tax_price($data['price'], $product_id) * $data['quantity'];
  1932. }
  1933. }
  1934. $total = array_sum($totals);
  1935. $coupon_code = $this->get_coupon_code();
  1936. if ( $coupon = $this->coupon_value($coupon_code, $total) )
  1937. $total = $coupon['new_total'];
  1938. //get address
  1939. $meta = get_user_meta(get_current_user_id(), 'mp_shipping_info', true);
  1940. $address1 = isset($_SESSION['mp_shipping_info']['address1']) ? $_SESSION['mp_shipping_info']['address1'] : (isset($meta['address1']) ? $meta['address1'] : '');
  1941. $address2 = isset($_SESSION['mp_shipping_info']['address2']) ? $_SESSION['mp_shipping_info']['address2'] : (isset($meta['address2']) ? $meta['address2'] : '');
  1942. $city = isset($_SESSION['mp_shipping_info']['city']) ? $_SESSION['mp_shipping_info']['city'] : (isset($meta['city']) ? $meta['city'] : '');
  1943. $state = isset($_SESSION['mp_shipping_info']['state']) ? $_SESSION['mp_shipping_info']['state'] : (isset($meta['state']) ? $meta['state'] : '');
  1944. $zip = isset($_SESSION['mp_shipping_info']['zip']) ? $_SESSION['mp_shipping_info']['zip'] : (isset($meta['zip']) ? $meta['zip'] : '');
  1945. $country = isset($_SESSION['mp_shipping_info']['country']) ? $_SESSION['mp_shipping_info']['country'] : (isset($meta['country']) ? $meta['country'] : '');
  1946. $selected_option = isset($_SESSION['mp_shipping_info']['shipping_sub_option']) ? $_SESSION['mp_shipping_info']['shipping_sub_option'] : null;
  1947. //check required fields
  1948. if ( empty($address1) || empty($city) || empty($zip) || empty($country) || !(is_array($cart) && count($cart)) )
  1949. return false;
  1950. //don't charge shipping if only digital products
  1951. if ( $this->download_only_cart($cart) ) {
  1952. $price = 0;
  1953. } else if ( $this->get_setting('shipping->method') == 'calculated' && isset($_SESSION['mp_shipping_info']['shipping_option']) && isset($mp_shipping_active_plugins[$_SESSION['mp_shipping_info']['shipping_option']]) ) {
  1954. //shipping plugins tie into this to calculate their shipping cost
  1955. $price = apply_filters( 'mp_calculate_shipping_'.$_SESSION['mp_shipping_info']['shipping_option'], 0, $total, $cart, $address1, $address2, $city, $state, $zip, $country, $selected_option );
  1956. } else {
  1957. //shipping plugins tie into this to calculate their shipping cost
  1958. $price = apply_filters( 'mp_calculate_shipping_'.$this->get_setting('shipping->method'), 0, $total, $cart, $address1, $address2, $city, $state, $zip, $country, $selected_option );
  1959. }
  1960. //calculate extra shipping
  1961. $extras = array();
  1962. foreach ($cart as $product_id => $variations) {
  1963. $shipping_meta = get_post_meta($product_id, 'mp_shipping', true);
  1964. foreach ($variations as $variation => $data) {
  1965. if (!$data['download'])
  1966. $extras[] = $shipping_meta['extra_cost'] * $data['quantity'];
  1967. }
  1968. }
  1969. $extra = array_sum($extras);
  1970. //merge
  1971. $price = round($price + $extra, 2);
  1972. //boot if shipping plugin didn't return at least 0
  1973. if (empty($price))
  1974. return false;
  1975. if ($format)
  1976. return $this->format_currency('', $price);
  1977. else
  1978. return $price;
  1979. }
  1980. //returns the calculated price for shipping after tax. For display only.
  1981. function shipping_tax_price($shipping_price) {
  1982. if ( !$this->get_setting('tax->tax_shipping') || !$this->get_setting('tax->tax_inclusive') )
  1983. return $shipping_price;
  1984. //get address
  1985. $meta = get_user_meta(get_current_user_id(), 'mp_shipping_info', true);
  1986. if (!isset($meta['state'])) {
  1987. $meta['state'] = '';
  1988. }
  1989. if (!isset($meta['country'])) {
  1990. $meta['country'] = '';
  1991. }
  1992. $state = isset($_SESSION['mp_shipping_info']['state']) ? $_SESSION['mp_shipping_info']['state'] : $meta['state'];
  1993. $country = isset($_SESSION['mp_shipping_info']['country']) ? $_SESSION['mp_shipping_info']['country'] : $meta['country'];
  1994. //if we've skipped the shipping page and no address is set, use base for tax calculation
  1995. if ($this->get_setting('tax->tax_inclusive') || $this->get_setting('shipping->method') == 'none') {
  1996. if (empty($country))
  1997. $country = $this->get_setting('base_country');
  1998. if (empty($state))
  1999. $state = $this->get_setting('base_province');
  2000. }
  2001. //check required fields
  2002. if ( empty($country) || $shipping_price <= 0 ) {
  2003. return false;
  2004. }
  2005. switch ($this->get_setting('base_country')) {
  2006. case 'US':
  2007. //USA taxes are only for orders delivered inside the state
  2008. if ($country == 'US' && $state == $this->get_setting('base_province'))
  2009. $price = round(($shipping_price * $this->get_setting('tax->rate')), 2);
  2010. break;
  2011. case 'CA':
  2012. //Canada tax is for all orders in country, based on province shipped to. We're assuming the rate is a combination of GST/PST/etc.
  2013. if ( $country == 'CA' && array_key_exists($state, $this->canadian_provinces) ) {
  2014. if (!is_null($this->get_setting("tax->canada_rate->$state")))
  2015. $price = round(($shipping_price * $this->get_setting("tax->canada_rate->$state")), 2);
  2016. else //backwards compat with pre 2.2 if per province rates are not set
  2017. $price = round(($shipping_price * $this->get_setting('tax->rate')), 2);
  2018. }
  2019. break;
  2020. case 'AU':
  2021. //Australia taxes orders in country
  2022. if ($country == 'AU')
  2023. $price = round(($shipping_price * $this->get_setting('tax->rate')), 2);
  2024. break;
  2025. default:
  2026. //EU countries charge VAT within the EU
  2027. if ( in_array($this->get_setting('base_country'), $this->eu_countries) ) {
  2028. if (in_array($country, $this->eu_countries))
  2029. $price = round(($shipping_price * $this->get_setting('tax->rate')), 2);
  2030. } else {
  2031. //all other countries use the tax outside preference
  2032. if ($this->get_setting('tax->tax_outside') || (!$this->get_setting('tax->tax_outside') && $country == $this->get_setting('base_country')))
  2033. $price = round(($shipping_price * $this->get_setting('tax->rate')), 2);
  2034. }
  2035. break;
  2036. }
  2037. if (empty($price))
  2038. $price = 0;
  2039. $price = apply_filters( 'mp_shipping_tax_price', $price, $shipping_price, $country, $state );
  2040. $price += $shipping_price;
  2041. return $price;
  2042. }
  2043. function get_display_shipping($order) {
  2044. return (float)(isset($order->mp_shipping_with_tax) ? $order->mp_shipping_with_tax : $order->mp_tax_shipping);
  2045. }
  2046. //returns the calculated price for taxes based on a bunch of foreign tax laws.
  2047. function tax_price($format = false, $cart = false) {
  2048. //grab cart for just this blog
  2049. if (!$cart)
  2050. $cart = $this->get_cart_contents();
  2051. //get address
  2052. $meta = get_user_meta(get_current_user_id(), 'mp_shipping_info', true);
  2053. if (!isset($meta['state'])) {
  2054. $meta['state'] = '';
  2055. }
  2056. if (!isset($meta['country'])) {
  2057. $meta['country'] = '';
  2058. }
  2059. $state = isset($_SESSION['mp_shipping_info']['state']) ? $_SESSION['mp_shipping_info']['state'] : $meta['state'];
  2060. $country = isset($_SESSION['mp_shipping_info']['country']) ? $_SESSION['mp_shipping_info']['country'] : $meta['country'];
  2061. //if we've skipped the shipping page and no address is set, use base for tax calculation
  2062. if ($this->download_only_cart($cart) || $this->get_setting('tax->tax_inclusive') || $this->get_setting('shipping->method') == 'none') {
  2063. if (empty($country))
  2064. $country = $this->get_setting('base_country');
  2065. if (empty($state))
  2066. $state = $this->get_setting('base_province');
  2067. }
  2068. //TODO calculate all taxes per product before rounding
  2069. //get total after any coupons
  2070. $totals = array();
  2071. $special_totals = array();
  2072. foreach ($cart as $product_id => $variations) {
  2073. //check for special rate
  2074. $special = (bool)get_post_meta($product_id, 'mp_is_special_tax', true);
  2075. if ($special)
  2076. $special_rate = get_post_meta($product_id, 'mp_special_tax', true);
  2077. foreach ($variations as $variation => $data) {
  2078. if ($special)
  2079. $special_totals[] = ($this->before_tax_price($data['price'], $product_id) * $data['quantity']) * $special_rate;
  2080. else
  2081. $totals[] = $this->before_tax_price($data['price'], false) * $data['quantity'];
  2082. }
  2083. }
  2084. $total = array_sum($totals);
  2085. $special_total = array_sum($special_totals);
  2086. $coupon_code = $this->get_coupon_code();
  2087. if ( $coupon = $this->coupon_value($coupon_code, $total) )
  2088. $total = $coupon['new_total'];
  2089. //add in shipping?
  2090. if ( $this->get_setting('tax->tax_shipping') && ($shipping_price = $this->shipping_price()) )
  2091. $total += $shipping_price;
  2092. //check required fields
  2093. if ( empty($country) || !(is_array($cart) && count($cart)) || ($total + $special_total) <= 0 ) {
  2094. return false;
  2095. }
  2096. switch ($this->get_setting('base_country')) {
  2097. case 'US':
  2098. //USA taxes are only for orders delivered inside the state
  2099. if ($country == 'US' && $state == $this->get_setting('base_province'))
  2100. $price = round(($total * $this->get_setting('tax->rate')) + $special_total, 2);
  2101. break;
  2102. case 'CA':
  2103. //Canada tax is for all orders in country, based on province shipped to. We're assuming the rate is a combination of GST/PST/etc.
  2104. if ( $country == 'CA' && array_key_exists($state, $this->canadian_provinces) ) {
  2105. if (!is_null($this->get_setting("tax->canada_rate->$state")))
  2106. $price = round(($total * $this->get_setting("tax->canada_rate->$state")) + $special_total, 2);
  2107. else //backwards compat with pre 2.2 if per province rates are not set
  2108. $price = round(($total * $this->get_setting('tax->rate')) + $special_total, 2);
  2109. }
  2110. break;
  2111. case 'AU':
  2112. //Australia taxes orders in country
  2113. if ($country == 'AU')
  2114. $price = round(($total * $this->get_setting('tax->rate')) + $special_total, 2);
  2115. break;
  2116. default:
  2117. //EU countries charge VAT within the EU
  2118. if ( in_array($this->get_setting('base_country'), $this->eu_countries) ) {
  2119. if (in_array($country, $this->eu_countries))
  2120. $price = round(($total * $this->get_setting('tax->rate')) + $special_total, 2);
  2121. } else {
  2122. //all other countries use the tax outside preference
  2123. if ($this->get_setting('tax->tax_outside') || (!$this->get_setting('tax->tax_outside') && $country == $this->get_setting('base_country')))
  2124. $price = round(($total * $this->get_setting('tax->rate')) + $special_total, 2);
  2125. }
  2126. break;
  2127. }
  2128. if (empty($price))
  2129. $price = 0;
  2130. $price = apply_filters( 'mp_tax_price', $price, $total, $cart, $country, $state );
  2131. if ($format)
  2132. return $this->format_currency('', $price);
  2133. else
  2134. return $price;
  2135. }
  2136. //returns the before tax price for a given amount based on a bunch of foreign tax laws.
  2137. function before_tax_price($tax_price, $product_id = false) {
  2138. //if tax inclusve pricing is turned off just return given price
  2139. if (!$this->get_setting('tax->tax_inclusive'))
  2140. return $tax_price;
  2141. if ($product_id && get_post_meta($product_id, 'mp_is_special_tax', true)) {
  2142. $rate = get_post_meta($product_id, 'mp_special_tax', true);
  2143. } else {
  2144. //figure out rate in case its based on a canadian base province
  2145. $rate = ('CA' == $this->get_setting('tax->base_country')) ? $this->get_setting('tax->canada_rate'.$this->get_setting('base_province')) : $this->get_setting('tax->rate');
  2146. }
  2147. return round($tax_price / ($rate + 1), 2);
  2148. }
  2149. //returns contents of shopping cart cookie
  2150. function get_cart_cookie($global = false) {
  2151. global $blog_id;
  2152. $blog_id = (is_multisite()) ? $blog_id : 1;
  2153. $cookie_id = 'mp_globalcart_' . COOKIEHASH;
  2154. if (isset($_COOKIE[$cookie_id])) {
  2155. $global_cart = unserialize($_COOKIE[$cookie_id]);
  2156. } else {
  2157. $global_cart = array($blog_id => array());
  2158. }
  2159. if ($global) {
  2160. return $global_cart;
  2161. } else {
  2162. if (isset($global_cart[$blog_id])) {
  2163. return $global_cart[$blog_id];
  2164. } else {
  2165. return array();
  2166. }
  2167. }
  2168. }
  2169. //saves global cart array to cookie
  2170. function set_global_cart_cookie($global_cart) {
  2171. $cookie_id = 'mp_globalcart_' . COOKIEHASH;
  2172. //set cookie
  2173. $expire = time() + 2592000; //1 month expire
  2174. setcookie($cookie_id, serialize($global_cart), $expire, COOKIEPATH, COOKIE_DOMAIN);
  2175. // Set the cookie variable as well, sometimes updating the cache doesn't work
  2176. $_COOKIE[$cookie_id] = serialize($global_cart);
  2177. //mark cache for updating
  2178. $this->cart_cache = false;
  2179. }
  2180. //saves cart array to cookie
  2181. function set_cart_cookie($cart) {
  2182. global $blog_id, $mp_gateway_active_plugins;
  2183. $blog_id = (is_multisite()) ? $blog_id : 1;
  2184. $global_cart = $this->get_cart_cookie(true);
  2185. if ($this->global_cart && count($global_cart = $this->get_cart_cookie(true)) >= $mp_gateway_active_plugins[0]->max_stores && !isset($global_cart[$blog_id])) {
  2186. $this->cart_checkout_error(sprintf(__("Sorry, currently it's not possible to checkout with items from more than %s stores.", 'mp'), $mp_gateway_active_plugins[0]->max_stores));
  2187. } else {
  2188. $global_cart[$blog_id] = $cart;
  2189. }
  2190. //update cache
  2191. $this->set_global_cart_cookie($global_cart);
  2192. }
  2193. //returns the full array of cart contents
  2194. function get_cart_contents($global = false) {
  2195. global $blog_id;
  2196. $blog_id = (is_multisite()) ? $blog_id : 1;
  2197. $current_blog_id = $blog_id;
  2198. //check cache
  2199. if ($this->cart_cache) {
  2200. if ($global) {
  2201. return $this->cart_cache;
  2202. } else {
  2203. if (isset($this->cart_cache[$blog_id])) {
  2204. return $this->cart_cache[$blog_id];
  2205. } else {
  2206. return array();
  2207. }
  2208. }
  2209. }
  2210. $global_cart = $this->get_cart_cookie(true);
  2211. if (!is_array($global_cart))
  2212. return array();
  2213. $full_cart = array();
  2214. foreach ($global_cart as $bid => $cart) {
  2215. if (is_multisite())
  2216. switch_to_blog($bid);
  2217. $full_cart[$bid] = array();
  2218. foreach ($cart as $product_id => $variations) {
  2219. $product = get_post($product_id);
  2220. if ( empty($product) ) {
  2221. continue;
  2222. }
  2223. $full_cart[$bid][$product_id] = array();
  2224. foreach ($variations as $variation => $quantity) {
  2225. //check stock
  2226. if (get_post_meta($product_id, 'mp_track_inventory', true)) {
  2227. $stock = maybe_unserialize(get_post_meta($product_id, 'mp_inventory', true));
  2228. if (!is_array($stock))
  2229. $stock[0] = $stock;
  2230. if ($stock[$variation] < $quantity) {
  2231. $this->cart_checkout_error( sprintf(__("Sorry, we don't have enough of %1$s in stock. Your cart quantity has been changed to %2$s.", 'mp'), $product->post_title, number_format_i18n($stock[$variation])) );
  2232. $quantity = $stock[$variation];
  2233. }
  2234. }
  2235. //check limit if tracking on or downloadable
  2236. if (get_post_meta($product_id, 'mp_track_limit', true) || $file = get_post_meta($product_id, 'mp_file', true)) {
  2237. $limit = empty($file) ? maybe_unserialize(get_post_meta($product_id, 'mp_limit', true)) : array($variation => 1);
  2238. if ($limit[$variation] && $limit[$variation] < $quantity) {
  2239. $this->cart_checkout_error( sprintf(__('Sorry, there is a per order limit of %1$s for "%2$s". Your cart quantity has been changed to %3$s.', 'mp'), number_format_i18n($limit[$variation]), $product->post_title, number_format_i18n($limit[$variation])) );
  2240. $quantity = $limit[$variation];
  2241. }
  2242. }
  2243. $skus = maybe_unserialize(get_post_meta($product_id, 'mp_sku', true));
  2244. if (!is_array($skus))
  2245. $skus[0] = $skus;
  2246. $var_names = maybe_unserialize(get_post_meta($product_id, 'mp_var_name', true));
  2247. if (is_array($var_names) && count($var_names) > 1)
  2248. $name = $product->post_title . ': ' . $var_names[$variation];
  2249. else
  2250. $name = $product->post_title;
  2251. //get if downloadable
  2252. if ( $download_url = get_post_meta($product_id, 'mp_file', true) )
  2253. $download = array('url' => $download_url, 'downloaded' => 0);
  2254. else
  2255. $download = false;
  2256. $full_cart[$bid][$product_id][$variation] = array('SKU' => $skus[$variation], 'name' => $name, 'url' => get_permalink($product_id), 'price' => $this->product_price($product_id, $variation), 'quantity' => $quantity, 'download' => $download);
  2257. }
  2258. }
  2259. }
  2260. if (is_multisite())
  2261. switch_to_blog($current_blog_id);
  2262. //save to cache
  2263. $this->cart_cache = $full_cart;
  2264. if ($global) {
  2265. return $full_cart;
  2266. } else {
  2267. if (isset($full_cart[$blog_id])) {
  2268. return $full_cart[$blog_id];
  2269. } else {
  2270. return array();
  2271. }
  2272. }
  2273. }
  2274. //receives a post and updates cookie variables for cart
  2275. function update_cart() {
  2276. global $blog_id, $mp_gateway_active_plugins;
  2277. $blog_id = (is_multisite()) ? $blog_id : 1;
  2278. $current_blog_id = $blog_id;
  2279. $cart = $this->get_cart_cookie();
  2280. if (isset($_POST['empty_cart'])) { //empty cart contents
  2281. //clear all blog products only if global checkout enabled
  2282. if ($this->global_cart)
  2283. $this->set_global_cart_cookie(array());
  2284. else
  2285. $this->set_cart_cookie(array());
  2286. if (defined('DOING_AJAX') && DOING_AJAX) {
  2287. ?>
  2288. <div class="mp_cart_empty">
  2289. <?php _e('There are no items in your cart.', 'mp') ?>
  2290. </div>
  2291. <div id="mp_cart_actions_widget">
  2292. <a class="mp_store_link" href="<?php mp_products_link(true, true); ?>"><?php _e('Browse Products &raquo;', 'mp') ?></a>
  2293. </div>
  2294. <?php
  2295. exit;
  2296. }
  2297. } else if (isset($_POST['product_id'])) { //add a product to cart
  2298. //if not valid product_id return
  2299. $product_id = apply_filters('mp_product_id_add_to_cart', intval($_POST['product_id']));
  2300. $product = get_post($product_id);
  2301. if (!$product || $product->post_type != 'product' || $product->post_status != 'publish')
  2302. return false;
  2303. //get quantity
  2304. $quantity = (isset($_POST['quantity'])) ? intval(abs($_POST['quantity'])) : 1;
  2305. //get variation
  2306. $variation = (isset($_POST['variation'])) ? intval(abs($_POST['variation'])) : 0;
  2307. //check max stores
  2308. if ($this->global_cart && count($global_cart = $this->get_cart_cookie(true)) >= $mp_gateway_active_plugins[0]->max_stores && !isset($global_cart[$blog_id])) {
  2309. if (defined('DOING_AJAX') && DOING_AJAX) {
  2310. echo 'error||' . sprintf(__("Sorry, currently it's not possible to checkout with items from more than %s stores.", 'mp'), $mp_gateway_active_plugins[0]->max_stores);
  2311. exit;
  2312. } else {
  2313. $this->cart_checkout_error(sprintf(__("Sorry, currently it's not possible to checkout with items from more than %s stores.", 'mp'), $mp_gateway_active_plugins[0]->max_stores));
  2314. return false;
  2315. }
  2316. }
  2317. //calculate new quantity
  2318. $new_quantity = $cart[$product_id][$variation] + $quantity;
  2319. //check stock
  2320. if (get_post_meta($product_id, 'mp_track_inventory', true)) {
  2321. $stock = maybe_unserialize(get_post_meta($product_id, 'mp_inventory', true));
  2322. if (!is_array($stock))
  2323. $stock[0] = $stock;
  2324. if ($stock[$variation] < $new_quantity) {
  2325. if (defined('DOING_AJAX') && DOING_AJAX) {
  2326. echo 'error||' . sprintf(__("Sorry, we don't have enough of this item in stock. (%s remaining)", 'mp'), number_format_i18n($stock[$variation]-$cart[$product_id][$variation]));
  2327. exit;
  2328. } else {
  2329. $this->cart_checkout_error( sprintf(__("Sorry, we don't have enough of this item in stock. (%s remaining)", 'mp'), number_format_i18n($stock[$variation]-$cart[$product_id][$variation])) );
  2330. return false;
  2331. }
  2332. }
  2333. //send ajax leftover stock
  2334. if (defined('DOING_AJAX') && DOING_AJAX) {
  2335. $return = array_sum($stock)-$new_quantity . '||';
  2336. }
  2337. } else {
  2338. //send ajax always stock if stock checking turned off
  2339. if (defined('DOING_AJAX') && DOING_AJAX) {
  2340. $return = 1 . '||';
  2341. }
  2342. }
  2343. //check limit if tracking on or downloadable
  2344. if (get_post_meta($product_id, 'mp_track_limit', true) || $file = get_post_meta($product_id, 'mp_file', true)) {
  2345. $limit = empty($file) ? maybe_unserialize(get_post_meta($product_id, 'mp_limit', true)) : array($variation => 1);
  2346. if ($limit[$variation] && $limit[$variation] < $new_quantity) {
  2347. if (defined('DOING_AJAX') && DOING_AJAX) {
  2348. echo 'error||' . sprintf(__('Sorry, there is a per order limit of %1$s for "%2$s".', 'mp'), number_format_i18n($limit[$variation]), $product->post_title);
  2349. exit;
  2350. } else {
  2351. $this->cart_checkout_error( sprintf(__('Sorry, there is a per order limit of %1$s for "%2$s".', 'mp'), number_format_i18n($limit[$variation]), $product->post_title) );
  2352. return false;
  2353. }
  2354. }
  2355. }
  2356. $cart[$product_id][$variation] = $new_quantity;
  2357. //save items to cookie
  2358. $this->set_cart_cookie($cart);
  2359. //if running via ajax return updated cart and die
  2360. if (defined('DOING_AJAX') && DOING_AJAX) {
  2361. $return .= mp_show_cart('widget', false, false);
  2362. echo $return;
  2363. exit;
  2364. }
  2365. } else if (isset($_POST['update_cart_submit'])) { //update cart contents
  2366. $global_cart = $this->get_cart_cookie(true);
  2367. //process quantity updates
  2368. if (is_array($_POST['quant'])) {
  2369. foreach ($_POST['quant'] as $pbid => $quant) {
  2370. list($bid, $product_id, $variation) = split(':', $pbid);
  2371. if (is_multisite())
  2372. switch_to_blog($bid);
  2373. $quant = intval(abs($quant));
  2374. if ($quant) {
  2375. //check stock
  2376. if (get_post_meta($product_id, 'mp_track_inventory', true)) {
  2377. $stock = maybe_unserialize(get_post_meta($product_id, 'mp_inventory', true));
  2378. if (!is_array($stock))
  2379. $stock[0] = $stock;
  2380. if ($stock[$variation] < $quant) {
  2381. $left = (($stock[$variation]-intval($global_cart[$bid][$product_id][$variation])) < 0) ? 0 : ($stock[$variation]-intval($global_cart[$bid][$product_id][$variation]));
  2382. $this->cart_checkout_error( sprintf(__('Sorry, there is not enough stock for "%s". (%s remaining)', 'mp'), get_the_title($product_id), number_format_i18n($left)) );
  2383. continue;
  2384. }
  2385. }
  2386. //check limit if tracking on or downloadable
  2387. if (get_post_meta($product_id, 'mp_track_limit', true) || $file = get_post_meta($product_id, 'mp_file', true)) {
  2388. $limit = empty($file) ? maybe_unserialize(get_post_meta($product_id, 'mp_limit', true)) : array($variation => 1);
  2389. if ($limit[$variation] && $limit[$variation] < $quant) {
  2390. $this->cart_checkout_error( sprintf(__('Sorry, there is a per order limit of %1$s for "%2$s".', 'mp'), number_format_i18n($limit[$variation]), get_the_title($product_id)) );
  2391. continue;
  2392. }
  2393. }
  2394. $global_cart[$bid][$product_id][$variation] = $quant;
  2395. } else {
  2396. unset($global_cart[$bid][$product_id][$variation]);
  2397. }
  2398. }
  2399. if (is_multisite())
  2400. switch_to_blog($current_blog_id);
  2401. }
  2402. //remove items
  2403. if (isset($_POST['remove']) && is_array($_POST['remove'])) {
  2404. foreach ($_POST['remove'] as $pbid) {
  2405. list($bid, $product_id, $variation) = split(':', $pbid);
  2406. unset($global_cart[$bid][$product_id][$variation]);
  2407. }
  2408. $this->cart_update_message( __('Item(s) Removed', 'mp') );
  2409. }
  2410. //check for empty blogid carts and unset them to avoid errors on global cart
  2411. foreach ($global_cart as $bid => $data) {
  2412. foreach ($data as $product_id => $product) {
  2413. if (!count($product))
  2414. unset($global_cart[$bid][$product_id]);
  2415. }
  2416. if (!count($global_cart[$bid]))
  2417. unset($global_cart[$bid]);
  2418. }
  2419. //save items to cookie
  2420. $this->set_global_cart_cookie($global_cart);
  2421. //add coupon code
  2422. if (!empty($_POST['coupon_code'])) {
  2423. if ($this->check_coupon($_POST['coupon_code'])) {
  2424. //get coupon code
  2425. if (is_multisite()) {
  2426. global $blog_id;
  2427. $_SESSION['mp_cart_coupon_' . $blog_id] = $_POST['coupon_code'];
  2428. } else {
  2429. $_SESSION['mp_cart_coupon'] = $_POST['coupon_code'];
  2430. }
  2431. $this->cart_update_message( __('Coupon Successfully Applied', 'mp') );
  2432. } else {
  2433. $this->cart_checkout_error( __('Invalid Coupon Code', 'mp') );
  2434. }
  2435. }
  2436. } else if (isset($_GET['remove_coupon'])) {
  2437. //remove coupon code
  2438. if (is_multisite()) {
  2439. global $blog_id;
  2440. unset($_SESSION['mp_cart_coupon_' . $blog_id]);
  2441. } else {
  2442. unset($_SESSION['mp_cart_coupon']);
  2443. }
  2444. $this->cart_update_message( __('Coupon Removed', 'mp') );
  2445. } else if (isset($_POST['mp_shipping_submit'])) { //save shipping info
  2446. //check checkout info
  2447. if (!is_email($_POST['email']))
  2448. $this->cart_checkout_error( __('Please enter a valid Email Address.', 'mp'), 'email');
  2449. //only require these fields if not a download only cart
  2450. if (!$this->download_only_cart($this->get_cart_contents()) && $this->get_setting('shipping->method') != 'none') {
  2451. if (empty($_POST['name']))
  2452. $this->cart_checkout_error( __('Please enter your Full Name.', 'mp'), 'name');
  2453. if (empty($_POST['address1']))
  2454. $this->cart_checkout_error( __('Please enter your Street Address.', 'mp'), 'address1');
  2455. if (empty($_POST['city']))
  2456. $this->cart_checkout_error( __('Please enter your City.', 'mp'), 'city');
  2457. if (($_POST['country'] == 'US' || $_POST['country'] == 'CA') && empty($_POST['state']))
  2458. $this->cart_checkout_error( __('Please enter your State/Province/Region.', 'mp'), 'state');
  2459. if ($_POST['country'] == 'US' && !array_key_exists(strtoupper($_POST['state']), $this->usa_states))
  2460. $this->cart_checkout_error( __('Please enter a valid two-letter State abbreviation.', 'mp'), 'state');
  2461. else if ($_POST['country'] == 'CA' && !array_key_exists(strtoupper($_POST['state']), $this->canadian_provinces))
  2462. $this->cart_checkout_error( __('Please enter a valid two-letter Canadian Province abbreviation.', 'mp'), 'state');
  2463. else
  2464. $_POST['state'] = strtoupper($_POST['state']);
  2465. if (empty($_POST['zip']))
  2466. $this->cart_checkout_error( __('Please enter your Zip/Postal Code.', 'mp'), 'zip');
  2467. if (empty($_POST['country']) || strlen($_POST['country']) != 2)
  2468. $this->cart_checkout_error( __('Please enter your Country.', 'mp'), 'country');
  2469. if ($_POST['no_shipping_options'] == '1'){
  2470. $this->cart_checkout_error( __('No valid shipping options found. Please check your address carefully.', 'mp' ), 'no_shipping_options');
  2471. }
  2472. }
  2473. // Process Personalization
  2474. if (isset($_POST['mp_custom_fields']) && count($_POST['mp_custom_fields'])) {
  2475. foreach($_POST['mp_custom_fields'] as $cf_key => $cf_items) {
  2476. list($bid, $product_id, $variation) = split(':', $cf_key);
  2477. if (!isset($product_id)) continue;
  2478. if (!isset($variation)) continue;
  2479. $mp_has_custom_field = get_post_meta(intval($product_id), 'mp_has_custom_field', true);
  2480. if (isset($mp_has_custom_field) && isset($mp_has_custom_field[intval($variation)]) && $mp_has_custom_field[intval($variation)]) {
  2481. $mp_custom_field_required = get_post_meta(intval($product_id), 'mp_custom_field_required', true);
  2482. if (isset($mp_custom_field_required) && isset($mp_custom_field_required[intval($variation)]) && $mp_custom_field_required[intval($variation)]) {
  2483. foreach($cf_items as $idx => $cf_item) {
  2484. if (empty($cf_item)) {
  2485. $this->cart_checkout_error( __('Required product extra information.', 'mp' ), 'custom_fields_'. $product_id .'_'. $variation);
  2486. break;
  2487. } else {
  2488. $cf_items[$idx] = trim(strip_tags(stripslashes($cf_item)));
  2489. }
  2490. }
  2491. $_POST['mp_custom_fields'][$cf_key] = $cf_items;
  2492. }
  2493. }
  2494. }
  2495. }
  2496. //save to session
  2497. global $current_user;
  2498. $meta = get_user_meta($current_user->ID, 'mp_shipping_info', true);
  2499. $_SESSION['mp_shipping_info']['email'] = ($_POST['email']) ? trim(stripslashes($_POST['email'])) : (isset($meta['email']) ? $meta['email']: $current_user->user_email);
  2500. $_SESSION['mp_shipping_info']['name'] = ($_POST['name']) ? trim(stripslashes($_POST['name'])) : (isset($meta['name']) ? $meta['name'] : $current_user->user_firstname . ' ' . $current_user->user_lastname);
  2501. $_SESSION['mp_shipping_info']['address1'] = ($_POST['address1']) ? trim(stripslashes($_POST['address1'])) : $meta['address1'];
  2502. $_SESSION['mp_shipping_info']['address2'] = ($_POST['address2']) ? trim(stripslashes($_POST['address2'])) : $meta['address2'];
  2503. $_SESSION['mp_shipping_info']['city'] = ($_POST['city']) ? trim(stripslashes($_POST['city'])) : $meta['city'];
  2504. $_SESSION['mp_shipping_info']['state'] = ($_POST['state']) ? trim(stripslashes($_POST['state'])) : $meta['state'];
  2505. $_SESSION['mp_shipping_info']['zip'] = ($_POST['zip']) ? trim(stripslashes($_POST['zip'])) : $meta['zip'];
  2506. $_SESSION['mp_shipping_info']['country'] = ($_POST['country']) ? trim($_POST['country']) : $meta['country'];
  2507. $_SESSION['mp_shipping_info']['phone'] = ($_POST['phone']) ? preg_replace('/[^0-9-\(\) ]/', '', trim($_POST['phone'])) : $meta['phone'];
  2508. if (isset($_POST['special_instructions']))
  2509. $_SESSION['mp_shipping_info']['special_instructions'] = trim(stripslashes($_POST['special_instructions']));
  2510. //Handle and store Product Custom field data
  2511. if (isset($_POST['mp_custom_fields']))
  2512. $_SESSION['mp_shipping_info']['mp_custom_fields'] = $_POST['mp_custom_fields'];
  2513. //for checkout plugins
  2514. do_action( 'mp_shipping_process' );
  2515. //save to user meta
  2516. if ($current_user->ID)
  2517. update_user_meta($current_user->ID, 'mp_shipping_info', $_SESSION['mp_shipping_info']);
  2518. //if no errors send to next checkout step
  2519. if ($this->checkout_error == false) {
  2520. //check for $0 checkout to skip gateways
  2521. //loop through cart items
  2522. $global_cart = $this->get_cart_contents(true);
  2523. if (!$this->global_cart) //get subset if needed
  2524. $selected_cart[$blog_id] = $global_cart[$blog_id];
  2525. else
  2526. $selected_cart = $global_cart;
  2527. $totals = array();
  2528. $shipping_prices = array();
  2529. $tax_prices = array();
  2530. foreach ($selected_cart as $bid => $cart) {
  2531. if (is_multisite())
  2532. switch_to_blog($bid);
  2533. foreach ($cart as $product_id => $variations) {
  2534. foreach ($variations as $data) {
  2535. $totals[] = $data['price'] * $data['quantity'];
  2536. }
  2537. }
  2538. if ( ($shipping_price = $this->shipping_price()) !== false )
  2539. $shipping_prices[] = $shipping_price;
  2540. if ( ($tax_price = $this->tax_price()) !== false )
  2541. $tax_prices[] = $tax_price;
  2542. }
  2543. //go back to original blog
  2544. if (is_multisite())
  2545. switch_to_blog($current_blog_id);
  2546. $total = array_sum($totals);
  2547. //coupon line
  2548. if ( $coupon = $this->coupon_value($this->get_coupon_code(), $total) )
  2549. $total = $coupon['new_total'];
  2550. //shipping
  2551. if ( $shipping_price = array_sum($shipping_prices) )
  2552. $total = $total + $shipping_price;
  2553. //tax line
  2554. if ( $tax_price = array_sum($tax_prices) )
  2555. $total = $total + $tax_price;
  2556. if ($total > 0) {
  2557. $network_settings = get_site_option( 'mp_network_settings' );
  2558. //can we skip the payment form page?
  2559. if ( $this->global_cart ) {
  2560. $skip = apply_filters('mp_payment_form_skip_' . $network_settings['global_gateway'], false);
  2561. } else {
  2562. $skip = apply_filters('mp_payment_form_skip_' . $this->get_setting('gateways->allowed->0'), false);
  2563. }
  2564. if ( (!$this->global_cart && count($this->get_setting('gateways->allowed', array())) > 1) || !$skip ) {
  2565. wp_safe_redirect(mp_checkout_step_url('checkout'));
  2566. exit;
  2567. } else {
  2568. if ( $this->global_cart )
  2569. $_SESSION['mp_payment_method'] = $network_settings['global_gateway'];
  2570. else
  2571. $_SESSION['mp_payment_method'] = $this->get_setting('gateways->allowed->0');
  2572. do_action( 'mp_payment_submit_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info'] );
  2573. //if no errors send to next checkout step
  2574. if ($this->checkout_error == false) {
  2575. wp_safe_redirect(mp_checkout_step_url('confirm-checkout'));
  2576. exit;
  2577. } else {
  2578. wp_safe_redirect(mp_checkout_step_url('checkout'));
  2579. exit;
  2580. }
  2581. }
  2582. } else { //empty price, create order already
  2583. //loop through and create orders
  2584. foreach ($selected_cart as $bid => $cart) {
  2585. $totals = array();
  2586. if (is_multisite())
  2587. switch_to_blog($bid);
  2588. foreach ($cart as $product_id => $variations) {
  2589. foreach ($variations as $data) {
  2590. $totals[] = $data['price'] * $data['quantity'];
  2591. }
  2592. }
  2593. $total = array_sum($totals);
  2594. //coupon line
  2595. if ( $coupon = $this->coupon_value($this->get_coupon_code(), $total) )
  2596. $total = $coupon['new_total'];
  2597. //shipping
  2598. if ( ($shipping_price = $this->shipping_price()) !== false )
  2599. $total = $total + $shipping_price;
  2600. //tax line
  2601. if ( ($tax_price = $this->tax_price()) !== false )
  2602. $total = $total + $tax_price;
  2603. //setup our payment details
  2604. $timestamp = time();
  2605. $payment_info['gateway_public_name'] = __('Manual Checkout', 'mp');
  2606. $payment_info['gateway_private_name'] = __('Manual Checkout', 'mp');
  2607. $payment_info['method'] = __('N/A - Free order', 'mp');
  2608. $payment_info['transaction_id'] = __('N/A', 'mp');
  2609. $payment_info['status'][$timestamp] = __('Completed', 'mp');
  2610. $payment_info['total'] = $total;
  2611. $payment_info['currency'] = $this->get_setting('currency');
  2612. $this->create_order(false, $cart, $_SESSION['mp_shipping_info'], $payment_info, true);
  2613. }
  2614. //go back to original blog
  2615. if (is_multisite())
  2616. switch_to_blog($current_blog_id);
  2617. $_SESSION['mp_payment_method'] = 'manual'; //so we don't get an error message on confirmation page
  2618. //redirect to final page
  2619. wp_safe_redirect(mp_checkout_step_url('confirmation'));
  2620. exit;
  2621. }
  2622. }
  2623. } else if (isset($_POST['mp_choose_gateway'])) { //check and save payment info
  2624. $_SESSION['mp_payment_method'] = $_POST['mp_choose_gateway'];
  2625. //processing script is only for selected gateway plugin
  2626. do_action( 'mp_payment_submit_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info'] );
  2627. //if no errors send to next checkout step
  2628. if ($this->checkout_error == false) {
  2629. wp_safe_redirect(mp_checkout_step_url('confirm-checkout'));
  2630. exit;
  2631. }
  2632. } else if (isset($_POST['mp_payment_confirm'])) { //create order and process payment
  2633. do_action( 'mp_payment_confirm_' . $_SESSION['mp_payment_method'], $this->get_cart_contents($this->global_cart), $_SESSION['mp_shipping_info'] );
  2634. //if no errors send to next checkout step
  2635. if ($this->checkout_error == false) {
  2636. wp_safe_redirect(mp_checkout_step_url('confirmation'));
  2637. exit;
  2638. }
  2639. }
  2640. }
  2641. function cart_update_message($msg) {
  2642. $content = 'return "<div id=\"mp_cart_updated_msg\">' . $msg . '</div>";';
  2643. add_filter( 'mp_cart_updated_msg', create_function('', $content) );
  2644. }
  2645. function cart_checkout_error($msg, $context = 'checkout') {
  2646. $msg = str_replace('"', '\"', $msg); //prevent double quotes from causing errors.
  2647. $content = 'return "<div class=\"mp_checkout_error\">' . $msg . '</div>";';
  2648. add_action( 'mp_checkout_error_' . $context, create_function('', $content) );
  2649. $this->checkout_error = true;
  2650. }
  2651. //returns any coupon code saved in $_SESSION. Will only reliably work on checkout pages
  2652. function get_coupon_code() {
  2653. //get coupon code
  2654. if (is_multisite()) {
  2655. global $blog_id;
  2656. $coupon_code = isset($_SESSION['mp_cart_coupon_' . $blog_id]) ? $_SESSION['mp_cart_coupon_' . $blog_id] : false;
  2657. } else {
  2658. $coupon_code = isset($_SESSION['mp_cart_coupon']) ? $_SESSION['mp_cart_coupon'] : false;
  2659. }
  2660. return $coupon_code;
  2661. }
  2662. //checks a coupon code for validity. Return boolean
  2663. function check_coupon($code) {
  2664. $coupon_code = preg_replace('/[^A-Z0-9_-]/', '', strtoupper($code));
  2665. //empty code
  2666. if (!$coupon_code)
  2667. return false;
  2668. $coupons = get_option('mp_coupons');
  2669. //allow short circuit of coupon codes
  2670. $return = apply_filters('mp_coupon_check', null, $coupon_code, $coupons);
  2671. if ( !is_null($return) )
  2672. return $return;
  2673. //no record for code
  2674. if (!isset($coupons[$coupon_code]) || !is_array($coupons[$coupon_code]))
  2675. return false;
  2676. //start date not valid yet
  2677. if (time() < $coupons[$coupon_code]['start'])
  2678. return false;
  2679. //if end date and expired
  2680. if ($coupons[$coupon_code]['end'] && time() > $coupons[$coupon_code]['end'])
  2681. return false;
  2682. //check remaining uses
  2683. if ($coupons[$coupon_code]['uses'] && (intval($coupons[$coupon_code]['uses']) - intval(@$coupons[$coupon_code]['used'])) <= 0)
  2684. return false;
  2685. //everything passed so it's valid
  2686. return true;
  2687. }
  2688. //get coupon value. Returns array(discount, new_total) or false for invalid code
  2689. function coupon_value($code, $total) {
  2690. if ($this->check_coupon($code)) {
  2691. $coupons = get_option('mp_coupons');
  2692. $coupon_code = preg_replace('/[^A-Z0-9_-]/', '', strtoupper($code));
  2693. if ($coupons[$coupon_code]['discount_type'] == 'amt') {
  2694. $new_total = round($total - $coupons[$coupon_code]['discount'], 2);
  2695. $new_total = ($new_total < 0) ? 0.00 : $new_total;
  2696. $discount = '-' . $this->format_currency('', $coupons[$coupon_code]['discount']);
  2697. $return = array('discount' => $discount, 'new_total' => $new_total);
  2698. } else {
  2699. $new_total = round($total - ($total * ($coupons[$coupon_code]['discount'] * 0.01)), 2);
  2700. $new_total = ($new_total < 0) ? 0.00 : $new_total;
  2701. $discount = '-' . $coupons[$coupon_code]['discount'] . '%';
  2702. $return = array('discount' => $discount, 'new_total' => $new_total);
  2703. }
  2704. return apply_filters('mp_coupon_value', $return, $code, $total);
  2705. } else {
  2706. return false;
  2707. }
  2708. }
  2709. //record coupon use. Returns boolean successful
  2710. function use_coupon($code) {
  2711. if ($this->check_coupon($code)) {
  2712. $coupons = get_option('mp_coupons');
  2713. $coupon_code = preg_replace('/[^A-Z0-9_-]/', '', strtoupper($code));
  2714. //increment count
  2715. if ( isset($coupons[$coupon_code]) ) {
  2716. $coupons[$coupon_code]['used']++;
  2717. update_option('mp_coupons', $coupons);
  2718. }
  2719. do_action('mp_coupon_use', $coupon_code);
  2720. return true;
  2721. } else {
  2722. return false;
  2723. }
  2724. }
  2725. //returns a new unique order id.
  2726. function generate_order_id() {
  2727. global $wpdb;
  2728. $count = true;
  2729. while ($count) { //make sure it's unique
  2730. $order_id = substr(sha1(uniqid('')), rand(1, 24), 12);
  2731. $count = $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->posts . " WHERE post_title = '" . $order_id . "' AND post_type = 'mp_order'");
  2732. }
  2733. $order_id = apply_filters( 'mp_order_id', $order_id ); //Very important to make sure order numbers are unique and not sequential if filtering
  2734. //save it to session
  2735. $_SESSION['mp_order'] = $order_id;
  2736. return $order_id;
  2737. }
  2738. //called on checkout to create a new order
  2739. function create_order($order_id, $cart, $shipping_info, $payment_info, $paid, $user_id = false, $shipping_total = false, $tax_total = false, $coupon_code = false) {
  2740. //order id can be null
  2741. if (empty($order_id))
  2742. $order_id = $this->generate_order_id();
  2743. else if ($this->get_order($order_id)) //don't continue if the order exists
  2744. return false;
  2745. //insert post type
  2746. $order = array();
  2747. $order['post_title'] = $order_id;
  2748. $order['post_name'] = $order_id;
  2749. $order['post_content'] = serialize($cart).serialize($shipping_info); //this is purely so you can search by cart contents
  2750. $order['post_status'] = ($paid) ? 'order_paid' : 'order_received';
  2751. $order['post_type'] = 'mp_order';
  2752. $post_id = wp_insert_post($order);
  2753. /* add post meta */
  2754. //filter tax included products in cart
  2755. $filtered_cart = $cart;
  2756. foreach ($cart as $product_id => $variations) {
  2757. foreach ($variations as $variation => $data) {
  2758. // store orignal price
  2759. // if tax_inclusive==true, before_tax_price() rounds to two decimal places
  2760. // the original price cannot be accurately calculated on the order tracking page with just the before_tax_price value
  2761. $filtered_cart[$product_id][$variation]['price_db'] = $data['price'];
  2762. $filtered_cart[$product_id][$variation]['price'] = $this->before_tax_price($data['price'], $product_id);
  2763. }
  2764. }
  2765. //cart info
  2766. add_post_meta($post_id, 'mp_cart_info', $filtered_cart, true);
  2767. //shipping info
  2768. add_post_meta($post_id, 'mp_shipping_info', $shipping_info, true);
  2769. //payment info
  2770. add_post_meta($post_id, 'mp_payment_info', $payment_info, true);
  2771. //loop through cart items
  2772. foreach ($cart as $product_id => $variations) {
  2773. foreach ($variations as $variation => $data) {
  2774. $items[] = $data['quantity'];
  2775. //adjust product stock quantities
  2776. if (get_post_meta($product_id, 'mp_track_inventory', true)) {
  2777. $stock = maybe_unserialize(get_post_meta($product_id, 'mp_inventory', true));
  2778. if (!is_array($stock))
  2779. $stock[0] = $stock;
  2780. $stock[$variation] = $stock[$variation] - $data['quantity'];
  2781. update_post_meta($product_id, 'mp_inventory', $stock);
  2782. if ($stock[$variation] <= $this->get_setting('inventory_threshold')) {
  2783. $this->low_stock_notification($product_id, $variation, $stock[$variation]);
  2784. }
  2785. }//check stock
  2786. //update sales count
  2787. $count = get_post_meta($product_id, 'mp_sales_count', true);
  2788. $count = $count + $data['quantity'];
  2789. update_post_meta($product_id, 'mp_sales_count', $count);
  2790. //for plugins into product sales
  2791. do_action( 'mp_product_sale', $product_id, $variation, $data, $paid );
  2792. }
  2793. }
  2794. $item_count = array_sum($items);
  2795. //coupon info
  2796. $code = $coupon_code ? $coupon_code : $this->get_coupon_code();
  2797. if ( $coupon = $this->coupon_value($code, 9999999999) ) {
  2798. add_post_meta($post_id, 'mp_discount_info', array('code' => $code, 'discount' => $coupon['discount']), true);
  2799. //mark coupon as used
  2800. $this->use_coupon($code);
  2801. }
  2802. //payment info
  2803. add_post_meta($post_id, 'mp_order_total', $payment_info['total'], true);
  2804. $mp_shipping_total = ($shipping_total ? $shipping_total : $this->shipping_price(false, $cart));
  2805. add_post_meta($post_id, 'mp_shipping_total', $mp_shipping_total, true);
  2806. add_post_meta($post_id, 'mp_shipping_with_tax', $this->shipping_tax_price($mp_shipping_total), true);
  2807. add_post_meta($post_id, 'mp_tax_total', ($tax_total ? $tax_total : $this->tax_price(false, $cart)), true);
  2808. add_post_meta($post_id, 'mp_order_items', $item_count, true);
  2809. add_post_meta($post_id, 'mp_tax_inclusive', $this->get_setting('tax->tax_inclusive'), true);
  2810. add_post_meta($post_id, 'mp_tax_shipping', $this->get_setting('tax->tax_shipping'), true);
  2811. $timestamp = time();
  2812. add_post_meta($post_id, 'mp_received_time', $timestamp, true);
  2813. //set paid time if we already have a confirmed payment
  2814. if ($paid) {
  2815. add_post_meta($post_id, 'mp_paid_time', $timestamp, true);
  2816. do_action( 'mp_order_paid', $this->get_order($order_id) );
  2817. }
  2818. //empty cart cookie
  2819. $this->set_cart_cookie(array());
  2820. //clear coupon code
  2821. if (is_multisite()) {
  2822. global $blog_id;
  2823. unset($_SESSION['mp_cart_coupon_' . $blog_id]);
  2824. } else {
  2825. unset($_SESSION['mp_cart_coupon']);
  2826. }
  2827. //save order history
  2828. if (!$user_id)
  2829. $user_id = get_current_user_id();
  2830. if ($user_id) { //save to user_meta if logged in
  2831. if (is_multisite()) {
  2832. global $blog_id;
  2833. $meta_id = 'mp_order_history_' . $blog_id;
  2834. } else {
  2835. $meta_id = 'mp_order_history';
  2836. }
  2837. $orders = get_user_meta($user_id, $meta_id, true);
  2838. $timestamp = time();
  2839. $orders[$timestamp] = array('id' => $order_id, 'total' => $payment_info['total']);
  2840. update_user_meta($user_id, $meta_id, $orders);
  2841. } else { //save to cookie instead
  2842. if (is_multisite()) {
  2843. global $blog_id;
  2844. $cookie_id = 'mp_order_history_' . $blog_id . '_' . COOKIEHASH;
  2845. } else {
  2846. $cookie_id = 'mp_order_history_' . COOKIEHASH;
  2847. }
  2848. if (isset($_COOKIE[$cookie_id]))
  2849. $orders = unserialize($_COOKIE[$cookie_id]);
  2850. $timestamp = time();
  2851. $orders[$timestamp] = array('id' => $order_id, 'total' => $payment_info['total']);
  2852. //set cookie
  2853. $expire = time() + 31536000; //1 year expire
  2854. setcookie($cookie_id, serialize($orders), $expire, COOKIEPATH, COOKIEDOMAIN);
  2855. }
  2856. //hook for new orders
  2857. do_action( 'mp_new_order', $this->get_order($order_id) );
  2858. //send new order email
  2859. $this->order_notification($order_id);
  2860. //if paid and the cart is only digital products mark it shipped
  2861. if ($paid && $this->download_only_cart($cart)) {
  2862. $this->skip_shipping_notice = true;
  2863. $this->update_order_status($order_id, 'shipped');
  2864. }
  2865. return $order_id;
  2866. }
  2867. //returns the full order details as an object
  2868. function get_order($order_id) {
  2869. $id = (is_int($order_id)) ? $order_id : $this->order_to_post_id($order_id);
  2870. if (empty($id))
  2871. return false;
  2872. $order = get_post($id);
  2873. if (!$order)
  2874. return false;
  2875. $meta = get_post_custom($id);
  2876. //unserialize a and add to object
  2877. foreach ($meta as $key => $val)
  2878. $order->$key = maybe_unserialize($meta[$key][0]);
  2879. return $order;
  2880. }
  2881. //serves the 'order_paid' : 'order_received'
  2882. function export_orders_csv() {
  2883. global $wpdb;
  2884. //check permissions
  2885. $post_type_object = get_post_type_object('mp_order');
  2886. if ( !current_user_can($post_type_object->cap->edit_posts) )
  2887. wp_die(__('Cheatin&#8217; uh?'));
  2888. $query = "SELECT ID, post_title, post_date, post_status FROM {$wpdb->posts} WHERE post_type = 'mp_order'";
  2889. if (isset($_POST['order_status']) && $_POST['order_status'] != 'all')
  2890. $query .= $wpdb->prepare(' AND post_status = %s', $_POST['order_status']);
  2891. // If a month is specified in the querystring, load that month
  2892. if ( isset($_POST['m']) && $_POST['m'] > 0 ) {
  2893. $_POST['m'] = '' . preg_replace('|[^0-9]|', '', $_POST['m']);
  2894. $query .= " AND YEAR($wpdb->posts.post_date)=" . substr($_POST['m'], 0, 4);
  2895. if ( strlen($_POST['m']) > 5 )
  2896. $query .= " AND MONTH($wpdb->posts.post_date)=" . substr($_POST['m'], 4, 2);
  2897. if ( strlen($_POST['m']) > 7 )
  2898. $query .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($_POST['m'], 6, 2);
  2899. if ( strlen($_POST['m']) > 9 )
  2900. $query .= " AND HOUR($wpdb->posts.post_date)=" . substr($_POST['m'], 8, 2);
  2901. if ( strlen($_POST['m']) > 11 )
  2902. $query .= " AND MINUTE($wpdb->posts.post_date)=" . substr($_POST['m'], 10, 2);
  2903. if ( strlen($_POST['m']) > 13 )
  2904. $query .= " AND SECOND($wpdb->posts.post_date)=" . substr($_POST['m'], 12, 2);
  2905. }
  2906. $query .= " ORDER BY post_date DESC";
  2907. $orders = $wpdb->get_results($query);
  2908. // Keep up to 12MB in memory, if becomes bigger write to temp file
  2909. $file = fopen('php://temp/maxmemory:'. (12*1024*1024), 'r+');
  2910. fputcsv( $file, array('order_id', 'status', 'received_date', 'paid_date', 'shipped_date', 'tax', 'shipping', 'total', 'coupon_discount', 'coupon_code', 'item_count', 'items', 'email', 'name', 'address1', 'address2', 'city', 'state', 'zipcode', 'country', 'phone', 'shipping_method', 'shipping_method_option', 'special_instructions', 'gateway', 'gateway_method', 'payment_currency', 'transaction_id' ) );
  2911. //loop through orders and add rows
  2912. foreach ($orders as $order) {
  2913. $meta = get_post_custom($order->ID);
  2914. //unserialize a and add to object
  2915. foreach ($meta as $key => $val)
  2916. $order->$key = maybe_unserialize($meta[$key][0]);
  2917. $fields = array();
  2918. $fields['order_id'] = $order->post_title;
  2919. $fields['status'] = $order->post_status;
  2920. $fields['received_date'] = $order->post_date;
  2921. $fields['paid_date'] = isset($order->mp_paid_time) ? date('Y-m-d H:i:s', $order->mp_paid_time) : null;
  2922. $fields['shipped_date'] = isset($order->mp_shipped_time) ? date('Y-m-d H:i:s', $order->mp_paid_time) : null;
  2923. $fields['tax'] = $order->mp_tax_total;
  2924. $fields['shipping'] = $order->mp_shipping_total;
  2925. $fields['total'] = $order->mp_order_total;
  2926. $fields['coupon_discount'] = @$order->mp_discount_info['discount'];
  2927. $fields['coupon_code'] = @$order->mp_discount_info['code'];
  2928. $fields['item_count'] = $order->mp_order_items;
  2929. //items
  2930. if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
  2931. foreach ($order->mp_cart_info as $product_id => $variations) {
  2932. foreach ($variations as $variation => $data) {
  2933. if (!empty($fields['items']))
  2934. $fields['items'] .= "\r\n";
  2935. if (!empty($data['SKU']))
  2936. $fields['items'] .= '[' . $data['SKU'] . '] ';
  2937. $fields['items'] .= $data['name'] . ': ' . number_format_i18n($data['quantity']) . ' * ' . number_format_i18n($data['price'], 2) . ' ' . $order->mp_payment_info['currency'];
  2938. /*
  2939. $cf_key = $bid .':'. $product_id .':'. $variation;
  2940. if (isset($order->mp_shipping_info['mp_custom_fields'][$cf_key])) {
  2941. $cf_items = $order->mp_shipping_info['mp_custom_fields'][$cf_key];
  2942. $mp_custom_field_label = get_post_meta($product_id, 'mp_custom_field_label', true);
  2943. if (isset($mp_custom_field_label[$variation]))
  2944. $label_text = esc_attr($mp_custom_field_label[$variation]);
  2945. else
  2946. $label_text = __('Product Personalization', 'mp');
  2947. $fields['items'] .= "\r\n\t" . $label_text .": ";
  2948. foreach($cf_items as $idx => $cf_item) {
  2949. $item_cnt = intval($idx)+1;
  2950. $fields['items'] .= $cf_item;
  2951. }
  2952. }
  2953. */
  2954. }
  2955. }
  2956. } else {
  2957. $fields['items'] = 'N/A';
  2958. }
  2959. $fields['email'] = @$order->mp_shipping_info['email'];
  2960. $fields['name'] = @$order->mp_shipping_info['name'];
  2961. $fields['address1'] = @$order->mp_shipping_info['address1'];
  2962. $fields['address2'] = @$order->mp_shipping_info['address2'];
  2963. $fields['city'] = @$order->mp_shipping_info['city'];
  2964. $fields['state'] = @$order->mp_shipping_info['state'];
  2965. $fields['zipcode'] = @$order->mp_shipping_info['zip'];
  2966. $fields['country'] = @$order->mp_shipping_info['country'];
  2967. $fields['phone'] = @$order->mp_shipping_info['phone'];
  2968. $fields['shipping_method'] = @$order->mp_shipping_info['shipping_option'];
  2969. $fields['shipping_method_option'] = @$order->mp_shipping_info['shipping_sub_option'];
  2970. $fields['special_instructions'] = @$order->mp_shipping_info['special_instructions'];
  2971. $fields['gateway'] = @$order->mp_payment_info['gateway_private_name'];
  2972. $fields['gateway_method'] = @$order->mp_payment_info['method'];
  2973. $fields['payment_currency'] = @$order->mp_payment_info['currency'];
  2974. $fields['transaction_id'] = @$order->mp_payment_info['transaction_id'];
  2975. fputcsv( $file, $fields );
  2976. }
  2977. //create our filename
  2978. $filename = 'orders_export';
  2979. $filename .= isset($_POST['m']) ? '_' . $_POST['m'] : '';
  2980. $filename .= '_' . time() . '.csv';
  2981. //serve the file
  2982. rewind($file);
  2983. ob_end_clean(); //kills any buffers set by other plugins
  2984. header('Content-Description: File Transfer');
  2985. header('Content-Type: text/csv');
  2986. header('Content-Disposition: attachment; filename="'.$filename.'"');
  2987. header('Content-Transfer-Encoding: binary');
  2988. header('Expires: 0');
  2989. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  2990. header('Pragma: public');
  2991. $output = stream_get_contents($file);
  2992. $output = "\xEF\xBB\xBF" . $output; // UTF-8 BOM
  2993. header('Content-Length: ' . strlen($output));
  2994. fclose($file);
  2995. die($output);
  2996. }
  2997. //converts the pretty order id to an actual post ID
  2998. function order_to_post_id($order_id) {
  2999. global $wpdb;
  3000. return $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_name = %s AND post_type = 'mp_order'", $order_id));
  3001. }
  3002. //$new_status can be 'received', 'paid', 'shipped', 'closed'
  3003. function update_order_status($order_id, $new_status) {
  3004. global $wpdb;
  3005. $statuses = array('received' => 'order_received', 'paid' => 'order_paid', 'shipped' => 'order_shipped', 'closed' => 'order_closed', 'trash' => 'trash', 'delete' => 'delete');
  3006. if (!array_key_exists($new_status, $statuses))
  3007. return false;
  3008. //get the order
  3009. $order = $this->get_order($order_id);
  3010. if (!$order)
  3011. return false;
  3012. // If we are transitioning the status from 'trash' to some other vlaue we want to decrement the product variation quantities.
  3013. if (($order->post_status == "trash") && ($new_status != "delete") && ($new_status != 'trash')) {
  3014. if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
  3015. foreach ($order->mp_cart_info as $product_id => $variations) {
  3016. if (!get_post_meta($product_id, 'mp_track_inventory', true))
  3017. continue;
  3018. $mp_inventory = get_post_meta($product_id, 'mp_inventory', true);
  3019. if (!$mp_inventory)
  3020. continue;
  3021. $_PRODUCT_INVENTORY_CHANGED = false;
  3022. foreach ($variations as $variation => $data) {
  3023. if (array_key_exists($variation, $mp_inventory)) {
  3024. $mp_inventory[$variation] -= $data['quantity'];
  3025. $_PRODUCT_INVENTORY_CHANGED = true;
  3026. }
  3027. }
  3028. if ($_PRODUCT_INVENTORY_CHANGED) {
  3029. update_post_meta($product_id, 'mp_inventory', $mp_inventory);
  3030. }
  3031. }
  3032. }
  3033. }
  3034. switch ($new_status) {
  3035. case 'paid':
  3036. //update paid time, can't be adjusted as we don't want to loose gateway info
  3037. if (!get_post_meta($order->ID, 'mp_paid_time', true)) {
  3038. update_post_meta($order->ID, 'mp_paid_time', time());
  3039. do_action( 'mp_order_paid', $order );
  3040. }
  3041. break;
  3042. case 'shipped':
  3043. //update paid time if paid step was skipped
  3044. if (!get_post_meta($order->ID, 'mp_paid_time', true)) {
  3045. update_post_meta($order->ID, 'mp_paid_time', time());
  3046. do_action( 'mp_order_paid', $order );
  3047. }
  3048. //update shipped time, can be adjusted
  3049. update_post_meta($order->ID, 'mp_shipped_time', time());
  3050. do_action( 'mp_order_shipped', $order );
  3051. //send email
  3052. $this->order_shipped_notification($order->ID);
  3053. break;
  3054. case 'closed':
  3055. //update paid time if paid step was skipped
  3056. if (!get_post_meta($order->ID, 'mp_paid_time', true)) {
  3057. update_post_meta($order->ID, 'mp_paid_time', time());
  3058. do_action( 'mp_order_paid', $order );
  3059. }
  3060. //update shipped time if shipped step was skipped
  3061. if (!get_post_meta($order->ID, 'mp_shipped_time', true)) {
  3062. update_post_meta($order->ID, 'mp_shipped_time', time());
  3063. do_action( 'mp_order_shipped', $order );
  3064. }
  3065. //update closed
  3066. update_post_meta($order->ID, 'mp_closed_time', time());
  3067. do_action( 'mp_order_closed', $order );
  3068. break;
  3069. case 'trash':
  3070. if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
  3071. foreach ($order->mp_cart_info as $product_id => $variations) {
  3072. if (!get_post_meta($product_id, 'mp_track_inventory', true))
  3073. continue;
  3074. $mp_inventory = get_post_meta($product_id, 'mp_inventory', true);
  3075. if (!$mp_inventory)
  3076. continue;
  3077. $_PRODUCT_INVENTORY_CHANGED = false;
  3078. foreach ($variations as $variation => $data) {
  3079. if (array_key_exists($variation, $mp_inventory)) {
  3080. $mp_inventory[$variation] += $data['quantity'];
  3081. $_PRODUCT_INVENTORY_CHANGED = true;
  3082. }
  3083. }
  3084. if ($_PRODUCT_INVENTORY_CHANGED) {
  3085. update_post_meta($product_id, 'mp_inventory', $mp_inventory);
  3086. }
  3087. }
  3088. }
  3089. break;
  3090. case 'delete':
  3091. wp_delete_post( $order_id );
  3092. break;
  3093. }
  3094. if ( $statuses[$new_status] == $order->post_status )
  3095. return;
  3096. $wpdb->update( $wpdb->posts, array( 'post_status' => $statuses[$new_status] ), array( 'ID' => $order->ID ) );
  3097. $old_status = $order->post_status;
  3098. $order->post_status = $statuses[$new_status];
  3099. wp_transition_post_status($statuses[$new_status], $old_status, $order);
  3100. }
  3101. //checks if a given cart is only downloadable products
  3102. function download_only_cart($cart) {
  3103. foreach ((array)$cart as $product_id => $variations) {
  3104. foreach ((array)$variations as $variation => $data) {
  3105. if (!is_array($data['download']))
  3106. return false;
  3107. }
  3108. }
  3109. return true;
  3110. }
  3111. //returns formatted download url for a given product. Returns false if no download
  3112. function get_download_url($product_id, $order_id) {
  3113. $url = get_post_meta($product_id, 'mp_file', true);
  3114. if (!$url)
  3115. return false;
  3116. return get_permalink($product_id) . "?orderid=$order_id";
  3117. }
  3118. //serves a downloadble product file
  3119. function serve_download($product_id) {
  3120. if (!isset($_GET['orderid']))
  3121. return false;
  3122. //get the order
  3123. $order = $this->get_order($_GET['orderid']);
  3124. if (!$order)
  3125. wp_die( __('Sorry, the link is invalid for this download.', 'mp') );
  3126. //check that order is paid
  3127. if ($order->post_status == 'order_received')
  3128. wp_die( __('Sorry, your order has been marked as unpaid.', 'mp') );
  3129. $url = get_post_meta($product_id, 'mp_file', true);
  3130. //get cart count
  3131. if (isset($order->mp_cart_info[$product_id][0]['download']))
  3132. $download = $order->mp_cart_info[$product_id][0]['download'];
  3133. //if new url is not set try to grab it from the order history
  3134. if (!$url && isset($download['url']))
  3135. $url = $download['url'];
  3136. else if (!$url)
  3137. wp_die( __('Whoops, we were unable to find the file for this download. Please contact us for help.', 'mp') );
  3138. //check for too many downloads
  3139. $max_downloads = $this->get_setting('max_downloads', 5);
  3140. if (intval($download['downloaded']) >= $max_downloads)
  3141. wp_die( sprintf( __("Sorry, our records show you've downloaded this file %d out of %d times allowed. Please contact us if you still need help.", 'mp'), intval($download['downloaded']), $max_downloads ) );
  3142. //for plugins to hook into the download script. Don't forget to increment the download count, then exit!
  3143. do_action('mp_serve_download', $url, $order, $download);
  3144. //allows you to simply filter the url
  3145. $url = apply_filters('mp_download_url', $url, $order, $download);
  3146. //if your getting out of memory errors with large downloads, you can use a redirect instead, it's not so secure though
  3147. if ( defined('MP_LARGE_DOWNLOADS') && MP_LARGE_DOWNLOADS ) {
  3148. //attempt to record a download attempt
  3149. if (isset($download['downloaded'])) {
  3150. $order->mp_cart_info[$product_id][0]['download']['downloaded'] = $download['downloaded'] + 1;
  3151. update_post_meta($order->ID, 'mp_cart_info', $order->mp_cart_info);
  3152. }
  3153. wp_redirect($url);
  3154. exit;
  3155. } else {
  3156. //create unique filename
  3157. $ext = ltrim(strrchr(basename($url), '.'), '.');
  3158. $filename = sanitize_file_name( strtolower( get_the_title($product_id) ) . '.' . $ext );
  3159. // Determine if this file is in our server
  3160. $dirs = wp_upload_dir();
  3161. $location = str_replace($dirs['baseurl'], $dirs['basedir'], $url);
  3162. if ( file_exists($location) ) {
  3163. $tmp = $location;
  3164. $not_delete = true;
  3165. } else {
  3166. require_once(ABSPATH . '/wp-admin/includes/file.php');
  3167. $tmp = download_url($url); //we download the url so we can serve it via php, completely obfuscating original source
  3168. if ( is_wp_error($tmp) ) {
  3169. @unlink($tmp);
  3170. trigger_error("MarketPress was unable to download the file $url for serving as download: ".$tmp->get_error_message(), E_USER_WARNING);
  3171. wp_die( __('Whoops, there was a problem loading up this file for your download. Please contact us for help.', 'mp') );
  3172. }
  3173. }
  3174. if (file_exists($tmp)) {
  3175. ob_end_clean(); //kills any buffers set by other plugins
  3176. header('Content-Description: File Transfer');
  3177. header('Content-Type: application/octet-stream');
  3178. header('Content-Disposition: attachment; filename="'.$filename.'"');
  3179. header('Content-Transfer-Encoding: binary');
  3180. header('Expires: 0');
  3181. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  3182. header('Pragma: public');
  3183. header('Content-Length: ' . filesize($tmp));
  3184. //readfile($tmp); //seems readfile chokes on large files
  3185. $chunksize = 1 * (1024 * 1024); // how many bytes per chunk
  3186. $buffer = '';
  3187. $cnt = 0;
  3188. $handle = fopen( $tmp, 'rb' );
  3189. if ( $handle === false ) {
  3190. trigger_error("MarketPress was unable to read the file $tmp for serving as download.", E_USER_WARNING);
  3191. return false;
  3192. }
  3193. while ( !feof( $handle ) ) {
  3194. $buffer = fread( $handle, $chunksize );
  3195. echo $buffer;
  3196. ob_flush();
  3197. flush();
  3198. if ( $retbytes ) {
  3199. $cnt += strlen( $buffer );
  3200. }
  3201. }
  3202. fclose( $handle );
  3203. if (!$not_delete)
  3204. @unlink($tmp);
  3205. }
  3206. //attempt to record a download attempt
  3207. if (isset($download['downloaded'])) {
  3208. $order->mp_cart_info[$product_id][0]['download']['downloaded'] = $download['downloaded'] + 1;
  3209. update_post_meta($order->ID, 'mp_cart_info', $order->mp_cart_info);
  3210. }
  3211. exit;
  3212. }
  3213. return false;
  3214. }
  3215. // Update profile fields
  3216. function user_profile_update() {
  3217. $user_id = $_REQUEST['user_id'];
  3218. // Billing Info
  3219. $meta = get_user_meta($user_id, 'mp_billing_info', true);
  3220. if (!isset($_POST['mp_billing_info']['email'])) {
  3221. $meta['email'] = '';
  3222. }
  3223. if (!isset($_POST['mp_billing_info']['name'])) {
  3224. $meta['name'] = '';
  3225. }
  3226. if (!isset($_POST['mp_billing_info']['address1'])) {
  3227. $meta['address1'] = '';
  3228. }
  3229. if (!isset($_POST['mp_billing_info']['address2'])) {
  3230. $meta['address2'] = '';
  3231. }
  3232. if (!isset($_POST['mp_billing_info']['city'])) {
  3233. $meta['city'] = '';
  3234. }
  3235. if (!isset($_POST['mp_billing_info']['state'])) {
  3236. $meta['state'] = '';
  3237. }
  3238. if (!isset($_POST['mp_billing_info']['zip'])) {
  3239. $meta['zip'] = '';
  3240. }
  3241. if (!isset($_POST['mp_billing_info']['country'])) {
  3242. $meta['country'] = '';
  3243. }
  3244. if (!isset($_POST['mp_billing_info']['phone'])) {
  3245. $meta['phone'] = '';
  3246. }
  3247. $email = isset($_POST['mp_billing_info']['email']) ? $_POST['mp_billing_info']['email'] : $meta['email'];
  3248. $name = isset($_POST['mp_billing_info']['name']) ? $_POST['mp_billing_info']['name'] : $meta['name'];
  3249. $address1 = isset($_POST['mp_billing_info']['address1']) ? $_POST['mp_billing_info']['address1'] : $meta['address1'];
  3250. $address2 = isset($_POST['mp_billing_info']['address2']) ? $_POST['mp_billing_info']['address2'] : $meta['address2'];
  3251. $city = isset($_POST['mp_billing_info']['city']) ? $_POST['mp_billing_info']['city'] : $meta['city'];
  3252. $state = isset($_POST['mp_billing_info']['state']) ? $_POST['mp_billing_info']['state'] : $meta['state'];
  3253. $zip = isset($_POST['mp_billing_info']['zip']) ? $_POST['mp_billing_info']['zip'] : $meta['zip'];
  3254. $country = isset($_POST['mp_billing_info']['country']) ? $_POST['mp_billing_info']['country'] : $meta['country'];
  3255. $phone = isset($_POST['mp_billing_info']['phone']) ? $_POST['mp_billing_info']['phone'] : $meta['phone'];
  3256. $billing_meta = array('email' => $email,
  3257. 'name' => $name,
  3258. 'address1' => $address1,
  3259. 'address2' => $address2,
  3260. 'city' => $city,
  3261. 'state' => $state,
  3262. 'zip' => $zip,
  3263. 'country' => $country,
  3264. 'phone' => $phone);
  3265. update_user_meta($user_id, 'mp_billing_info', $billing_meta);
  3266. // Shipping Info
  3267. $meta = get_user_meta($user_id, 'mp_shipping_info', true);
  3268. if (!isset($_POST['mp_shipping_info']['email'])) {
  3269. $meta['email'] = '';
  3270. }
  3271. if (!isset($_POST['mp_shipping_info']['name'])) {
  3272. $meta['name'] = '';
  3273. }
  3274. if (!isset($_POST['mp_shipping_info']['address1'])) {
  3275. $meta['address1'] = '';
  3276. }
  3277. if (!isset($_POST['mp_shipping_info']['address2'])) {
  3278. $meta['address2'] = '';
  3279. }
  3280. if (!isset($_POST['mp_shipping_info']['city'])) {
  3281. $meta['city'] = '';
  3282. }
  3283. if (!isset($_POST['mp_shipping_info']['state'])) {
  3284. $meta['state'] = '';
  3285. }
  3286. if (!isset($_POST['mp_shipping_info']['zip'])) {
  3287. $meta['zip'] = '';
  3288. }
  3289. if (!isset($_POST['mp_shipping_info']['country'])) {
  3290. $meta['country'] = '';
  3291. }
  3292. $email = isset($_POST['mp_shipping_info']['email']) ? $_POST['mp_shipping_info']['email'] : $meta['email'];
  3293. $name = isset($_POST['mp_shipping_info']['name']) ? $_POST['mp_shipping_info']['name'] : $meta['name'];
  3294. $address1 = isset($_POST['mp_shipping_info']['address1']) ? $_POST['mp_shipping_info']['address1'] : $meta['address1'];
  3295. $address2 = isset($_POST['mp_shipping_info']['address2']) ? $_POST['mp_shipping_info']['address2'] : $meta['address2'];
  3296. $city = isset($_POST['mp_shipping_info']['city']) ? $_POST['mp_shipping_info']['city'] : $meta['city'];
  3297. $state = isset($_POST['mp_shipping_info']['state']) ? $_POST['mp_shipping_info']['state'] : $meta['state'];
  3298. $zip = isset($_POST['mp_shipping_info']['zip']) ? $_POST['mp_shipping_info']['zip'] : $meta['zip'];
  3299. $country = isset($_POST['mp_shipping_info']['country']) ? $_POST['mp_shipping_info']['country'] : $meta['country'];
  3300. $phone = isset($_POST['mp_shipping_info']['phone']) ? $_POST['mp_shipping_info']['phone'] : $meta['phone'];
  3301. $shipping_meta = array('email' => $email,
  3302. 'name' => $name,
  3303. 'address1' => $address1,
  3304. 'address2' => $address2,
  3305. 'city' => $city,
  3306. 'state' => $state,
  3307. 'zip' => $zip,
  3308. 'country' => $country,
  3309. 'phone' => $phone);
  3310. update_user_meta($user_id, 'mp_shipping_info', $shipping_meta);
  3311. }
  3312. function user_profile_fields() {
  3313. global $current_user;
  3314. if (isset($_REQUEST['user_id'])) {
  3315. $user_id = $_REQUEST['user_id'];
  3316. } else {
  3317. $user_id = $current_user->ID;
  3318. }
  3319. $meta = get_user_meta($user_id, 'mp_billing_info', true);
  3320. $email = (!empty($_SESSION['mp_billing_info']['email'])) ? $_SESSION['mp_billing_info']['email'] : $meta['email'];
  3321. $name = (!empty($_SESSION['mp_billing_info']['name'])) ? $_SESSION['mp_billing_info']['name'] : $meta['name'];
  3322. $address1 = (!empty($_SESSION['mp_billing_info']['address1'])) ? $_SESSION['mp_billing_info']['address1'] : $meta['address1'];
  3323. $address2 = (!empty($_SESSION['mp_billing_info']['address2'])) ? $_SESSION['mp_billing_info']['address2'] : $meta['address2'];
  3324. $city = (!empty($_SESSION['mp_billing_info']['city'])) ? $_SESSION['mp_billing_info']['city'] : $meta['city'];
  3325. $state = (!empty($_SESSION['mp_billing_info']['state'])) ? $_SESSION['mp_billing_info']['state'] : $meta['state'];
  3326. $zip = (!empty($_SESSION['mp_billing_info']['zip'])) ? $_SESSION['mp_billing_info']['zip'] : $meta['zip'];
  3327. $country = (!empty($_SESSION['mp_billing_info']['country'])) ? $_SESSION['mp_billing_info']['country'] : $meta['country'];
  3328. if (!$country)
  3329. $country = $this->get_setting('base_country');
  3330. $phone = (!empty($_SESSION['mp_billing_info']['phone'])) ? $_SESSION['mp_billing_info']['phone'] : $meta['phone'];
  3331. ?>
  3332. <h3><?php _e('Billing Info', 'mp'); ?></h3>
  3333. <a name="mp_billing_info"></a>
  3334. <table class="form-table">
  3335. <tr>
  3336. <th align="right"><label for="mp_billing_info_email"><?php _e('Email:', 'mp'); ?>&nbsp;</label></th><td>
  3337. <?php echo apply_filters( 'mp_billing_info_error_email', ''); ?>
  3338. <input size="35" id="mp_billing_info_email" name="mp_billing_info[email]" type="text" value="<?php echo esc_attr($email); ?>" /></td>
  3339. </tr>
  3340. <tr>
  3341. <th align="right"><label for="mp_billing_info_name"><?php _e('Full Name:', 'mp'); ?>&nbsp;</label></th><td>
  3342. <?php echo apply_filters( 'mp_billing_info_error_name', ''); ?>
  3343. <input size="35" id="mp_billing_info_name" name="mp_billing_info[name]" type="text" value="<?php echo esc_attr($name); ?>" /> </td>
  3344. </tr>
  3345. <tr>
  3346. <th align="right"><label for="mp_billing_info_address1"><?php _e('Address:', 'mp'); ?>&nbsp;</label></th><td>
  3347. <?php echo apply_filters( 'mp_billing_info_error_address1', ''); ?>
  3348. <input size="45" id="mp_billing_info_address1" name="mp_billing_info[address1]" type="text" value="<?php echo esc_attr($address1); ?>" /><br />
  3349. <small><em><?php _e('Street address, P.O. box, company name, c/o', 'mp'); ?></em></small>
  3350. </td>
  3351. </tr>
  3352. <tr>
  3353. <th align="right"><label for="mp_billing_info_address2"><?php _e('Address 2:', 'mp'); ?>&nbsp;</label></th><td>
  3354. <?php echo apply_filters( 'mp_billing_info_error_address2', ''); ?>
  3355. <input size="45" id="mp_billing_info_address2" name="mp_billing_info[address2]" type="text" value="<?php echo esc_attr($address2); ?>" /><br />
  3356. <small><em><?php _e('Apartment, suite, unit, building, floor, etc.', 'mp'); ?></em></small>
  3357. </td>
  3358. </tr>
  3359. <tr>
  3360. <th align="right"><label for="mp_billing_info_city"><?php _e('City:', 'mp'); ?>&nbsp;</label></th><td>
  3361. <?php echo apply_filters( 'mp_billing_info_error_city', ''); ?>
  3362. <input size="25" id="mp_billing_info_city" name="mp_billing_info[city]" type="text" value="<?php echo esc_attr($city); ?>" /></td>
  3363. </tr>
  3364. <tr>
  3365. <th align="right"><label for="mp_billing_info_state"><?php _e('State/Province/Region:', 'mp'); ?>&nbsp;</label></th><td>
  3366. <?php echo apply_filters( 'mp_billing_info_error_state', ''); ?>
  3367. <input size="15" id="mp_billing_info_state" name="mp_billing_info[state]" type="text" value="<?php echo esc_attr($state); ?>" /></td>
  3368. </tr>
  3369. <tr>
  3370. <th align="right"><label for="mp_billing_info_zip"><?php _e('Postal/Zip Code:', 'mp'); ?>&nbsp;</label></th><td>
  3371. <?php echo apply_filters( 'mp_billing_info_error_zip', ''); ?>
  3372. <input size="10" id="mp_billing_info_zip" name="mp_billing_info[zip]" type="text" value="<?php echo esc_attr($zip); ?>" /></td>
  3373. </tr>
  3374. <tr>
  3375. <th align="right"><label for="mp_billing_info_country"><?php _e('Country:', 'mp'); ?>&nbsp;</label></th><td>
  3376. <?php echo apply_filters( 'mp_billing_info_error_country', ''); ?>
  3377. <select id="mp_billing_info_country" name="mp_billing_info[country]">
  3378. <?php
  3379. foreach ($this->get_setting('shipping->allowed_countries') as $code) {
  3380. ?><option value="<?php echo $code; ?>"<?php selected($country, $code); ?>><?php echo esc_attr($this->countries[$code]); ?></option><?php
  3381. }
  3382. ?>
  3383. </select>
  3384. </td>
  3385. </tr>
  3386. <tr>
  3387. <th align="right"><label for="mp_billing_info_phone"><?php _e('Phone Number:', 'mp'); ?>&nbsp;</label></th><td>
  3388. <?php echo apply_filters( 'mp_billing_info_error_phone', ''); ?>
  3389. <input size="20" id="mp_billing_info_phone" name="mp_billing_info[phone]" type="text" value="<?php echo esc_attr($phone); ?>" /></td>
  3390. </tr>
  3391. </table>
  3392. <?php
  3393. $meta = get_user_meta($user_id, 'mp_shipping_info', true);
  3394. $email = (!empty($_SESSION['mp_shipping_info']['email'])) ? $_SESSION['mp_shipping_info']['email'] : (!empty($meta['email'])?$meta['email']:$_SESSION['mp_shipping_info']['email']);
  3395. $name = (!empty($_SESSION['mp_shipping_info']['name'])) ? $_SESSION['mp_shipping_info']['name'] : (!empty($meta['name'])?$meta['name']:$_SESSION['mp_shipping_info']['name']);
  3396. $address1 = (!empty($_SESSION['mp_shipping_info']['address1'])) ? $_SESSION['mp_shipping_info']['address1'] : (!empty($meta['address1'])?$meta['address1']:$_SESSION['mp_shipping_info']['address1']);
  3397. $address2 = (!empty($_SESSION['mp_shipping_info']['address2'])) ? $_SESSION['mp_shipping_info']['address2'] : (!empty($meta['address2'])?$meta['address2']:$_SESSION['mp_shipping_info']['address2']);
  3398. $city = (!empty($_SESSION['mp_shipping_info']['city'])) ? $_SESSION['mp_shipping_info']['city'] : (!empty($meta['city'])?$meta['city']:$_SESSION['mp_shipping_info']['city']);
  3399. $state = (!empty($_SESSION['mp_shipping_info']['state'])) ? $_SESSION['mp_shipping_info']['state'] : (!empty($meta['state'])?$meta['state']:$_SESSION['mp_shipping_info']['state']);
  3400. $zip = (!empty($_SESSION['mp_shipping_info']['zip'])) ? $_SESSION['mp_shipping_info']['zip'] : (!empty($meta['zip'])?$meta['zip']:$_SESSION['mp_shipping_info']['zip']);
  3401. $country = (!empty($_SESSION['mp_shipping_info']['country'])) ? $_SESSION['mp_shipping_info']['country'] : (!empty($meta['country'])?$meta['country']:$_SESSION['mp_shipping_info']['country']);
  3402. if (!$country)
  3403. $country = $this->get_setting('base_country');
  3404. $phone = (!empty($_SESSION['mp_shipping_info']['phone'])) ? $_SESSION['mp_shipping_info']['phone'] : (!empty($meta['phone'])?$meta['phone']:$_SESSION['mp_shipping_info']['phone']);
  3405. ?>
  3406. <h3><?php _e('Shipping Info', 'mp'); ?></h3>
  3407. <a name="mp_shipping_info"></a>
  3408. <span class="mp_action" ><a href="javascript:mp_copy_billing('mp_shipping_info');"><?php _e('Same as Billing', 'mp'); ?></a></span>
  3409. <table class="form-table">
  3410. <tr>
  3411. <th align="right"><label for="mp_shipping_info_email"><?php _e('Email:', 'mp'); ?>&nbsp;</label></th><td>
  3412. <?php echo apply_filters( 'mp_shipping_info_error_email', ''); ?>
  3413. <input size="35" id="mp_shipping_info_email" name="mp_shipping_info[email]" type="text" value="<?php echo esc_attr($email); ?>" /></td>
  3414. </tr>
  3415. <tr>
  3416. <th align="right"><label for="mp_shipping_info_name"><?php _e('Full Name:', 'mp'); ?>&nbsp;</label></th><td>
  3417. <?php echo apply_filters( 'mp_checkout_error_name', ''); ?>
  3418. <input size="35" id="mp_shipping_info_name" name="mp_shipping_info[name]" type="text" value="<?php echo esc_attr($name); ?>" /> </td>
  3419. </tr>
  3420. <tr>
  3421. <th align="right"><label for="mp_shipping_info_address1"><?php _e('Address:', 'mp'); ?>&nbsp;</label></th><td>
  3422. <?php echo apply_filters( 'mp_shipping_info_error_address1', ''); ?>
  3423. <input size="45" id="mp_shipping_info_address1" name="mp_shipping_info[address1]" type="text" value="<?php echo esc_attr($address1); ?>" /><br />
  3424. <small><em><?php _e('Street address, P.O. box, company name, c/o', 'mp'); ?></em></small>
  3425. </td>
  3426. </tr>
  3427. <tr>
  3428. <th align="right"><label for="mp_shipping_info_address2"><?php _e('Address 2:', 'mp'); ?>&nbsp;</label></th><td>
  3429. <?php echo apply_filters( 'mp_shipping_info_error_address2', ''); ?>
  3430. <input size="45" id="mp_shipping_info_address2" name="mp_shipping_info[address2]" type="text" value="<?php echo esc_attr($address2); ?>" /><br />
  3431. <small><em><?php _e('Apartment, suite, unit, building, floor, etc.', 'mp'); ?></em></small>
  3432. </td>
  3433. </tr>
  3434. <tr>
  3435. <th align="right"><label for="mp_shipping_info_city"><?php _e('City:', 'mp'); ?>&nbsp;</label></th><td>
  3436. <?php echo apply_filters( 'mp_shipping_info_error_city', ''); ?>
  3437. <input size="25" id="mp_shipping_info_city" name="mp_shipping_info[city]" type="text" value="<?php echo esc_attr($city); ?>" /></td>
  3438. </tr>
  3439. <tr>
  3440. <th align="right"><label for="mp_shipping_info_state"><?php _e('State/Province/Region:', 'mp'); ?>&nbsp;</label></th><td>
  3441. <?php echo apply_filters( 'mp_shipping_info_error_state', ''); ?>
  3442. <input size="15" id="mp_shipping_info_state" name="mp_shipping_info[state]" type="text" value="<?php echo esc_attr($state); ?>" /></td>
  3443. </tr>
  3444. <tr>
  3445. <th align="right"><label for="mp_shipping_info_zip"><?php _e('Postal/Zip Code:', 'mp'); ?>&nbsp;</label></th><td>
  3446. <?php echo apply_filters( 'mp_shipping_info_error_zip', ''); ?>
  3447. <input size="10" id="mp_shipping_info_zip" name="mp_shipping_info[zip]" type="text" value="<?php echo esc_attr($zip); ?>" /></td>
  3448. </tr>
  3449. <tr>
  3450. <th align="right"><label for="mp_shipping_info_country"><?php _e('Country:', 'mp'); ?>&nbsp;</label></th><td>
  3451. <?php echo apply_filters( 'mp_shipping_info_error_country', ''); ?>
  3452. <select id="mp_shipping_info_country" name="mp_shipping_info[country]">
  3453. <?php
  3454. foreach ($this->get_setting('shipping->allowed_countries') as $code) {
  3455. ?><option value="<?php echo $code; ?>"<?php selected($country, $code); ?>><?php echo esc_attr($this->countries[$code]); ?></option><?php
  3456. }
  3457. ?>
  3458. </select>
  3459. </td>
  3460. </tr>
  3461. <tr>
  3462. <th align="right"><label for="mp_shipping_info_phone"><?php _e('Phone Number:', 'mp'); ?>&nbsp;</label></th><td>
  3463. <?php echo apply_filters( 'mp_shipping_info_error_phone', ''); ?>
  3464. <input size="20" id="mp_shipping_info_phone" name="mp_shipping_info[phone]" type="text" value="<?php echo esc_attr($phone); ?>" /></td>
  3465. </tr>
  3466. </table>
  3467. <script type="text/javascript">
  3468. function mp_copy_billing(prefix) {
  3469. _mp_profile_billing_fields = ['emal', 'name', 'address1', 'address2', 'city', 'state', 'zip', 'country', 'phone'];
  3470. for (_i=0; _i<_mp_profile_billing_fields.length; _i++) {
  3471. jQuery('form #'+prefix+'_'+_mp_profile_billing_fields[_i]).val(jQuery('form #mp_billing_info_'+_mp_profile_billing_fields[_i]).val());
  3472. }
  3473. }
  3474. </script>
  3475. <?php
  3476. }
  3477. //called by payment gateways to update order statuses
  3478. function update_order_payment_status($order_id, $status, $paid) {
  3479. //get the order
  3480. $order = $this->get_order($order_id);
  3481. if (!$order)
  3482. return false;
  3483. //get old status
  3484. $payment_info = $order->mp_payment_info;
  3485. $timestamp = time();
  3486. $payment_info['status'][$timestamp] = $status;
  3487. //update post meta
  3488. update_post_meta($order->ID, 'mp_payment_info', $payment_info);
  3489. if ($paid) {
  3490. if ($order->post_status == 'order_received') {
  3491. $this->update_order_status($order->ID, 'paid');
  3492. //if paid and the cart is only digital products mark it shipped
  3493. if (is_array($order->mp_cart_info) && $this->download_only_cart($order->mp_cart_info))
  3494. $this->update_order_status($order->ID, 'shipped');
  3495. } else {
  3496. //update payment time if somehow it was skipped
  3497. if (!get_post_meta($order->ID, 'mp_paid_time', true))
  3498. update_post_meta($order->ID, 'mp_paid_time', time());
  3499. }
  3500. } else {
  3501. $this->update_order_status($order->ID, 'received');
  3502. }
  3503. //return merged payment info
  3504. return $payment_info;
  3505. }
  3506. //filters wp_mail headers
  3507. function mail($to, $subject, $msg) {
  3508. //remove any other filters
  3509. remove_all_filters( 'wp_mail_from' );
  3510. remove_all_filters( 'wp_mail_from_name' );
  3511. //add our own filters
  3512. add_filter( 'wp_mail_from_name', create_function('', 'return get_bloginfo("name");') );
  3513. add_filter( 'wp_mail_from', create_function('', '$settings = get_option("mp_settings");return isset($settings["store_email"]) ? $settings["store_email"] : get_option("admin_email");') );
  3514. return wp_mail($to, $subject, $msg);
  3515. }
  3516. //replaces shortcodes in email msgs with dynamic content
  3517. function filter_email($order, $text) {
  3518. global $blog_id;
  3519. $bid = (is_multisite()) ? $blog_id : 1;
  3520. //// order info
  3521. if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
  3522. $order_info = __('Items:', 'mp') . "\n";
  3523. foreach ($order->mp_cart_info as $product_id => $variations) {
  3524. foreach ($variations as $variation => $data) {
  3525. $price = get_display_price($order, $data);
  3526. $order_info .= "\t" . $data['name'] . ': ' . number_format_i18n($data['quantity']) . ' * ' . number_format_i18n($price, 2) . ' = '. number_format_i18n($price * $data['quantity'], 2) . ' ' . $order->mp_payment_info['currency'] . "\n";
  3527. //show download link if set
  3528. if ($order->post_status != 'order_received' && $download_url = $this->get_download_url($product_id, $order->post_title))
  3529. $order_info .= "\t\t" . __('Download: ', 'mp') . $download_url . "\n";
  3530. // FPM: Product Custom Fields
  3531. $cf_key = $bid .':'. $product_id .':'. $variation;
  3532. if (isset($order->mp_shipping_info['mp_custom_fields'][$cf_key])) {
  3533. $cf_items = $order->mp_shipping_info['mp_custom_fields'][$cf_key];
  3534. $mp_custom_field_label = get_post_meta($product_id, 'mp_custom_field_label', true);
  3535. if (isset($mp_custom_field_label[$variation]))
  3536. $label_text = esc_attr($mp_custom_field_label[$variation]);
  3537. else
  3538. $label_text = __('Product Personalization: ', 'mp');
  3539. $order_info .= "\t\t" . $label_text ."\n";
  3540. foreach($cf_items as $idx => $cf_item) {
  3541. $item_cnt = intval($idx)+1;
  3542. $order_info .= "\t\t\t" . $item_cnt .". ". $cf_item ."\n";
  3543. }
  3544. }
  3545. $order_info .= "\n";
  3546. }
  3547. }
  3548. $order_info .= "\n";
  3549. }
  3550. //coupon line
  3551. if ( $order->mp_discount_info ) {
  3552. $order_info .= "\n" . __('Coupon Discount:', 'mp') . ' ' . str_replace('%', __(' Percent', 'mp'), $order->mp_discount_info['discount']); //have to escape % sign so sprintf doesn't choke
  3553. }
  3554. //shipping line
  3555. if ( $order->mp_shipping_total ) {
  3556. $order_info .= "\n" . __('Shipping:', 'mp') . ' ' . number_format_i18n($this->get_display_shipping($order), 2) . ' ' . $order->mp_payment_info['currency'];
  3557. }
  3558. //tax line
  3559. if ( $order->mp_tax_total ) {
  3560. $order_info .= "\n" . __('Taxes:', 'mp') . ' ' . number_format_i18n((float)$order->mp_tax_total, 2) . ' ' . $order->mp_payment_info['currency'];
  3561. }
  3562. //total line
  3563. $order_info .= "\n" . __('Order Total:', 'mp') . ' ' . number_format_i18n((float)$order->mp_order_total, 2) . ' ' . $order->mp_payment_info['currency'];
  3564. //// Shipping Info
  3565. if ((is_array($order->mp_cart_info) && $this->download_only_cart($order->mp_cart_info)) || $this->get_setting('shipping->method') == 'none') { //if the cart is only digital products
  3566. $shipping_info = __('No shipping required for this order.', 'mp');
  3567. } else {
  3568. $shipping_info = __('Full Name:', 'mp') . ' ' . $order->mp_shipping_info['name'];
  3569. $shipping_info .= "\n" . __('Address:', 'mp') . ' ' . $order->mp_shipping_info['address1'];
  3570. if ($order->mp_shipping_info['address2'])
  3571. $shipping_info .= "\n" . __('Address 2:', 'mp') . ' ' . $order->mp_shipping_info['address2'];
  3572. $shipping_info .= "\n" . __('City:', 'mp') . ' ' . $order->mp_shipping_info['city'];
  3573. if (!empty($order->mp_shipping_info['state']))
  3574. $shipping_info .= "\n" . __('State/Province/Region:', 'mp') . ' ' . $order->mp_shipping_info['state'];
  3575. $shipping_info .= "\n" . __('Postal/Zip Code:', 'mp') . ' ' . $order->mp_shipping_info['zip'];
  3576. $shipping_info .= "\n" . __('Country:', 'mp') . ' ' . $order->mp_shipping_info['country'];
  3577. if (!empty($order->mp_shipping_info['phone']))
  3578. $shipping_info .= "\n" . __('Phone Number:', 'mp') . ' ' . $order->mp_shipping_info['phone'];
  3579. // If actually shipped show method, else customer's shipping choice.
  3580. if (isset($order->mp_shipping_info['method']) && $order->mp_shipping_info['method'] != 'other')
  3581. $shipping_info .= "\n" . __('Shipping Method:', 'mp') . ' ' . $order->mp_shipping_info['method'];
  3582. elseif (! empty($order->mp_shipping_info['shipping_option']) )
  3583. $shipping_info .= "\n" . __('Shipping Method:', 'mp') . ' ' . strtoupper($order->mp_shipping_info['shipping_option']) . ' ' .$order->mp_shipping_info['shipping_sub_option'] ;
  3584. if (!empty($order->mp_shipping_info['tracking_num']))
  3585. $shipping_info .= "\n" . __('Tracking Number:', 'mp') . ' ' . $order->mp_shipping_info['tracking_num'];
  3586. }
  3587. if (!empty($order->mp_shipping_info['special_instructions']))
  3588. $shipping_info .= "\n" . __('Special Instructions:', 'mp') . ' ' . $order->mp_shipping_info['special_instructions'];
  3589. $order_notes = '';
  3590. if (!empty($order->mp_order_notes))
  3591. $order_notes = __('Order Notes:', 'mp') . "\n" . $order->mp_order_notes;
  3592. //// Payment Info
  3593. $payment_info = __('Payment Method:', 'mp') . ' ' . $order->mp_payment_info['gateway_public_name'];
  3594. if ($order->mp_payment_info['method'])
  3595. $payment_info .= "\n" . __('Payment Type:', 'mp') . ' ' . $order->mp_payment_info['method'];
  3596. if ($order->mp_payment_info['transaction_id'])
  3597. $payment_info .= "\n" . __('Transaction ID:', 'mp') . ' ' . $order->mp_payment_info['transaction_id'];
  3598. $payment_info .= "\n" . __('Payment Total:', 'mp') . ' ' . number_format_i18n((float)$order->mp_payment_info['total'], 2) . ' ' . $order->mp_payment_info['currency'];
  3599. $payment_info .= "\n\n";
  3600. if ($order->post_status == 'order_received') {
  3601. $payment_info .= __('Your payment for this order is not yet complete. Here is the latest status:', 'mp') . "\n";
  3602. $statuses = $order->mp_payment_info['status'];
  3603. krsort($statuses); //sort with latest status at the top
  3604. $status = reset($statuses);
  3605. $timestamp = key($statuses);
  3606. $payment_info .= date_i18n(get_option('date_format') . ' - ' . get_option('time_format'), $timestamp) . ': ' . $status;
  3607. } else {
  3608. $payment_info .= __('Your payment for this order is complete.', 'mp');
  3609. }
  3610. //total
  3611. $order_total = number_format_i18n((float)$order->mp_payment_info['total'], 2) . ' ' . $order->mp_payment_info['currency'];
  3612. //tracking URL
  3613. $tracking_url = apply_filters('wpml_marketpress_tracking_url', mp_orderstatus_link(false, true) . $order->post_title . '/');
  3614. //setup filters
  3615. $search = array('CUSTOMERNAME', 'ORDERID', 'ORDERINFO', 'SHIPPINGINFO', 'PAYMENTINFO', 'TOTAL', 'TRACKINGURL', 'ORDERNOTES');
  3616. $replace = array($order->mp_shipping_info['name'], $order->post_title, $order_info, $shipping_info, $payment_info, $order_total, $tracking_url, $order_notes);
  3617. //replace
  3618. $text = str_replace($search, $replace, $text);
  3619. return $text;
  3620. }
  3621. //sends email for new orders
  3622. function order_notification($order_id) {
  3623. //get the order
  3624. $order = $this->get_order($order_id);
  3625. if (!$order)
  3626. return false;
  3627. $subject = apply_filters('mp_order_notification_subject', $this->filter_email($order, stripslashes($this->get_setting('email->new_order_subject'))), $order);
  3628. $msg = apply_filters('mp_order_notification_body', $this->filter_email($order, stripslashes($this->get_setting('email->new_order_txt'))), $order);
  3629. $msg = apply_filters('mp_order_notification_' . $_SESSION['mp_payment_method'], $msg, $order );
  3630. $this->mail($order->mp_shipping_info['email'], $subject, $msg);
  3631. //send message to admin
  3632. $subject = __('New Order Notification: ORDERID', 'mp');
  3633. $msg = __("A new order (ORDERID) was created in your store:
  3634. Order Information:
  3635. ORDERINFO
  3636. Shipping Information:
  3637. SHIPPINGINFO
  3638. Email: %s
  3639. Payment Information:
  3640. PAYMENTINFO
  3641. You can manage this order here: %s", 'mp');
  3642. $subject = $this->filter_email($order, $subject);
  3643. $subject = apply_filters( 'mp_order_notification_admin_subject', $subject, $order );
  3644. $msg = $this->filter_email($order, $msg);
  3645. $msg = sprintf($msg, $order->mp_shipping_info['email'], admin_url('edit.php?post_type=product&page=marketpress-orders&order_id=') . $order->ID);
  3646. $msg = apply_filters( 'mp_order_notification_admin_msg', $msg, $order );
  3647. $store_email = $this->get_setting('store_email') ? $this->get_setting('store_email') : get_option("admin_email");
  3648. $this->mail($store_email, $subject, $msg);
  3649. }
  3650. //sends email for orders marked as shipped
  3651. function order_shipped_notification($order_id) {
  3652. //get the order
  3653. $order = $this->get_order($order_id);
  3654. if (!$order)
  3655. return false;
  3656. //skip notice for paid download only carts
  3657. if ($this->skip_shipping_notice)
  3658. return false;
  3659. $subject = apply_filters('mp_shipped_order_notification_subject', stripslashes($this->get_setting('email->shipped_order_subject')), $order);
  3660. $subject = $this->filter_email($order, $subject);
  3661. $msg = apply_filters( 'mp_shipped_order_notification_body', stripslashes($this->get_setting('email->shipped_order_txt')), $order );
  3662. $msg = $this->filter_email($order, $msg);
  3663. $msg = apply_filters( 'mp_shipped_order_notification', $msg, $order );
  3664. $this->mail($order->mp_shipping_info['email'], $subject, $msg);
  3665. }
  3666. //sends email to admin for low stock notification
  3667. function low_stock_notification($product_id, $variation, $stock) {
  3668. //skip if sent already and not 0
  3669. if ( get_post_meta($product_id, 'mp_stock_email_sent', true) && $stock > 0 )
  3670. return;
  3671. $var_names = maybe_unserialize(get_post_meta($product_id, 'mp_var_name', true));
  3672. if (is_array($var_names) && count($var_names) > 1)
  3673. $name = get_the_title($product_id) . ': ' . $var_names[$variation];
  3674. else
  3675. $name = get_the_title($product_id);
  3676. $subject = __('Low Product Inventory Notification', 'mp');
  3677. $msg = __('This message is being sent to notify you of low stock of a product in your online store according to your preferences.
  3678. Product: %s
  3679. Current Inventory: %s
  3680. Link: %s
  3681. Edit Product: %s
  3682. Notification Preferences: %s', 'mp');
  3683. $msg = sprintf($msg, $name, number_format_i18n($stock), get_permalink($product_id), get_edit_post_link($product_id), admin_url('edit.php?post_type=product&page=marketpress#mp-inventory-setting'));
  3684. $msg = apply_filters( 'mp_low_stock_notification', $msg, $product_id );
  3685. $store_email = $this->get_setting('store_email') ? $this->get_setting('store_email') : get_option("admin_email");
  3686. $this->mail($store_email, $subject, $msg);
  3687. //save so we don't send an email every time
  3688. update_post_meta($product_id, 'mp_stock_email_sent', 1);
  3689. }
  3690. //round and display currency with padded zeros
  3691. function display_currency( $amount ) {
  3692. if ( $this->get_setting('curr_decimal') === '0' )
  3693. return number_format( round( $amount ), 0, '.', '');
  3694. else
  3695. return number_format( round( $amount, 2 ), 2, '.', '');
  3696. }
  3697. //display currency symbol
  3698. function format_currency($currency = '', $amount = false) {
  3699. if (!$currency)
  3700. $currency = $this->get_setting('currency', 'USD');
  3701. // get the currency symbol
  3702. $symbol = $this->currencies[$currency][1];
  3703. // if many symbols are found, rebuild the full symbol
  3704. $symbols = explode(', ', $symbol);
  3705. if (is_array($symbols)) {
  3706. $symbol = "";
  3707. foreach ($symbols as $temp) {
  3708. $symbol .= '&#x'.$temp.';';
  3709. }
  3710. } else {
  3711. $symbol = '&#x'.$symbol.';';
  3712. }
  3713. //check decimal option
  3714. if ( $this->get_setting('curr_decimal') === '0' ) {
  3715. $decimal_place = 0;
  3716. $zero = '0';
  3717. } else {
  3718. $decimal_place = 2;
  3719. $zero = '0.00';
  3720. }
  3721. //format currency amount according to preference
  3722. if ($amount) {
  3723. if ($this->get_setting('curr_symbol_position') == 1 || !$this->get_setting('curr_symbol_position'))
  3724. return $symbol . number_format_i18n($amount, $decimal_place);
  3725. else if ($this->get_setting('curr_symbol_position') == 2)
  3726. return $symbol . ' ' . number_format_i18n($amount, $decimal_place);
  3727. else if ($this->get_setting('curr_symbol_position') == 3)
  3728. return number_format_i18n($amount, $decimal_place) . $symbol;
  3729. else if ($this->get_setting('curr_symbol_position') == 4)
  3730. return number_format_i18n($amount, $decimal_place) . ' ' . $symbol;
  3731. } else if ($amount === false) {
  3732. return $symbol;
  3733. } else {
  3734. if ($this->get_setting('curr_symbol_position') == 1 || !$this->get_setting('curr_symbol_position'))
  3735. return $symbol . $zero;
  3736. else if ($this->get_setting('curr_symbol_position') == 2)
  3737. return $symbol . ' ' . $zero;
  3738. else if ($this->get_setting('curr_symbol_position') == 3)
  3739. return $zero . $symbol;
  3740. else if ($this->get_setting('curr_symbol_position') == 4)
  3741. return $zero . ' ' . $symbol;
  3742. }
  3743. }
  3744. //replaces wp_trim_excerpt in our custom loops
  3745. function product_excerpt($excerpt, $content, $product_id, $excerpt_more = null) {
  3746. if (is_null($excerpt_more))
  3747. $excerpt_more = ' <a class="mp_product_more_link" href="' . get_permalink($product_id) . '">' . __('More Info &raquo;', 'mp') . '</a>';
  3748. if ($excerpt) {
  3749. return apply_filters('get_the_excerpt', $excerpt) . $excerpt_more;
  3750. } else {
  3751. $text = strip_shortcodes( $content );
  3752. //$text = apply_filters('the_content', $text);
  3753. $text = str_replace(']]>', ']]&gt;', $text);
  3754. $text = strip_tags($text);
  3755. $excerpt_length = apply_filters('excerpt_length', 55);
  3756. $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
  3757. if ( count($words) > $excerpt_length ) {
  3758. array_pop($words);
  3759. $text = implode(' ', $words);
  3760. $text = $text . $excerpt_more;
  3761. } else {
  3762. $text = implode(' ', $words);
  3763. }
  3764. }
  3765. return $text;
  3766. }
  3767. //returns the js needed to record ecommerce transactions. $project should be an array of id, title
  3768. function create_ga_ecommerce($order) {
  3769. if (!is_object($order))
  3770. return false;
  3771. if ($this->get_setting('ga_ecommerce') == 'old') {
  3772. $js = '<script type="text/javascript">
  3773. try{
  3774. pageTracker._addTrans(
  3775. "'.esc_js($order->post_title).'", // order ID - required
  3776. "'.esc_js(get_bloginfo('blogname')).'", // affiliation or store name
  3777. "'.$order->mp_order_total.'", // total - required
  3778. "'.$order->mp_tax_total.'", // tax
  3779. "'.$order->mp_shipping_total.'", // shipping
  3780. "'.esc_js($order->mp_shipping_info['city']).'", // city
  3781. "'.esc_js($order->mp_shipping_info['state']).'", // state or province
  3782. "'.esc_js($order->mp_shipping_info['country']).'" // country
  3783. );';
  3784. if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
  3785. foreach ($order->mp_cart_info as $product_id => $variations) {
  3786. foreach ($variations as $variation => $data) {
  3787. $sku = !empty($data['SKU']) ? esc_js($data['SKU']) : $product_id;
  3788. $js .= 'pageTracker._addItem(
  3789. "'.esc_js($order->post_title).'", // order ID - necessary to associate item with transaction
  3790. "'.$sku.'", // SKU/code - required
  3791. "'.esc_js($data['name']).'", // product name
  3792. "'.$data['price'].'", // unit price - required
  3793. "'.$data['quantity'].'" // quantity - required
  3794. );';
  3795. }
  3796. }
  3797. }
  3798. $js .= 'pageTracker._trackTrans(); //submits transaction to the Analytics servers
  3799. } catch(err) {}
  3800. </script>
  3801. ';
  3802. } else if ($this->get_setting('ga_ecommerce') == 'new') {
  3803. $js = '<script type="text/javascript">
  3804. _gaq.push(["_addTrans",
  3805. "'.esc_attr($order->post_title).'", // order ID - required
  3806. "'.esc_attr(get_bloginfo('blogname')).'", // affiliation or store name
  3807. "'.$order->mp_order_total.'", // total - required
  3808. "'.$order->mp_tax_total.'", // tax
  3809. "'.$order->mp_shipping_total.'", // shipping
  3810. "'.esc_attr($order->mp_shipping_info['city']).'", // city
  3811. "'.esc_attr($order->mp_shipping_info['state']).'", // state or province
  3812. "'.esc_attr($order->mp_shipping_info['country']).'" // country
  3813. ]);';
  3814. if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
  3815. foreach ($order->mp_cart_info as $product_id => $variations) {
  3816. foreach ($variations as $variation => $data) {
  3817. $sku = !empty($data['SKU']) ? esc_attr($data['SKU']) : $product_id;
  3818. $js .= '_gaq.push(["_addItem",
  3819. "'.esc_attr($order->post_title).'", // order ID - necessary to associate item with transaction
  3820. "'.$sku.'", // SKU/code - required
  3821. "'.esc_attr($data['name']).'", // product name
  3822. "", // category
  3823. "'.$data['price'].'", // unit price - required
  3824. "'.$data['quantity'].'" // quantity - required
  3825. ]);';
  3826. }
  3827. }
  3828. }
  3829. $js .= '_gaq.push(["_trackTrans"]);
  3830. </script>
  3831. ';
  3832. //add info for subblog if our GA plugin is installed
  3833. if (class_exists('Google_Analytics_Async')) {
  3834. $js = '<script type="text/javascript">
  3835. _gaq.push(["b._addTrans",
  3836. "'.esc_attr($order->post_title).'", // order ID - required
  3837. "'.esc_attr(get_bloginfo('blogname')).'", // affiliation or store name
  3838. "'.$order->mp_order_total.'", // total - required
  3839. "'.$order->mp_tax_total.'", // tax
  3840. "'.$order->mp_shipping_total.'", // shipping
  3841. "'.esc_attr($order->mp_shipping_info['city']).'", // city
  3842. "'.esc_attr($order->mp_shipping_info['state']).'", // state or province
  3843. "'.esc_attr($order->mp_shipping_info['country']).'" // country
  3844. ]);';
  3845. if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
  3846. foreach ($order->mp_cart_info as $product_id => $variations) {
  3847. foreach ($variations as $variation => $data) {
  3848. $sku = !empty($data['SKU']) ? esc_attr($data['SKU']) : $product_id;
  3849. $js .= '_gaq.push(["b._addItem",
  3850. "'.esc_attr($order->post_title).'", // order ID - necessary to associate item with transaction
  3851. "'.$sku.'", // SKU/code - required
  3852. "'.esc_attr($data['name']).'", // product name
  3853. "", // category
  3854. "'.$data['price'].'", // unit price - required
  3855. "'.$data['quantity'].'" // quantity - required
  3856. ]);';
  3857. }
  3858. }
  3859. }
  3860. $js .= '_gaq.push(["b._trackTrans"]);
  3861. </script>
  3862. ';
  3863. }
  3864. }
  3865. //add to footer
  3866. if ( !empty($js) ) {
  3867. $function = "echo '$js';";
  3868. add_action( 'wp_footer', create_function('', $function), 99999 );
  3869. }
  3870. }
  3871. //displays the detail page of an order
  3872. function single_order_page() {
  3873. $order = $this->get_order((int)$_GET['order_id']);
  3874. if ( !$order )
  3875. wp_die(__('Invalid Order ID', 'mp'));
  3876. $max_downloads = $this->get_setting('max_downloads', 5);
  3877. //save tracking number
  3878. if (isset($_POST['mp_tracking_number'])) {
  3879. $order->mp_shipping_info['tracking_num'] = stripslashes(trim($_POST['mp_tracking_number']));
  3880. $order->mp_shipping_info['method'] = stripslashes(trim($_POST['mp_shipping_method']));
  3881. update_post_meta($order->ID, 'mp_shipping_info', $order->mp_shipping_info);
  3882. if (isset($_POST['add-tracking-shipped'])) {
  3883. $this->update_order_status($order->ID, 'shipped');
  3884. $order->post_status = 'order_shipped';
  3885. ?><div class="updated fade"><p><?php _e('This order has been marked as Shipped.', 'mp'); ?></p></div><?php
  3886. }
  3887. if (!current_user_can('unfiltered_html'))
  3888. $_POST['mp_order_notes'] = wp_filter_post_kses(trim(stripslashes($_POST['mp_order_notes'])));
  3889. $order->mp_order_notes = stripslashes($_POST['mp_order_notes']);
  3890. update_post_meta($order->ID, 'mp_order_notes', $_POST['mp_order_notes']);
  3891. ?><div class="updated fade"><p><?php _e('Order details have been saved!', 'mp'); ?></p></div><?php
  3892. }
  3893. ?>
  3894. <div class="wrap">
  3895. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/shopping-cart.png'; ?>" /></div>
  3896. <h2><?php echo sprintf(__('Order Details (%s)', 'mp'), esc_attr($order->post_title)); ?></h2>
  3897. <div id="poststuff" class="metabox-holder mp-settings has-right-sidebar">
  3898. <div id="side-info-column" class="inner-sidebar">
  3899. <div id='side-sortables' class='meta-box-sortables'>
  3900. <div id="submitdiv" class="postbox mp-order-actions">
  3901. <h3 class='hndle'><span><?php _e('Order Actions', 'mp'); ?></span></h3>
  3902. <div class="inside">
  3903. <div id="submitpost" class="submitbox">
  3904. <div class="misc-pub-section"><strong><?php _e('Change Order Status:', 'mp'); ?></strong></div>
  3905. <?php
  3906. $actions = array();
  3907. if ($order->post_status == 'order_received') {
  3908. $actions['received current'] = __('Received', 'mp');
  3909. $actions['paid'] = "<a title='" . esc_attr(__('Mark as Paid', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=paid&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Paid', 'mp') . "</a>";
  3910. $actions['shipped'] = "<a title='" . esc_attr(__('Mark as Shipped', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=shipped&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Shipped', 'mp') . "</a>";
  3911. $actions['closed'] = "<a title='" . esc_attr(__('Mark as Closed', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=closed&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Closed', 'mp') . "</a>";
  3912. $actions['trash'] = "<a title='" . esc_attr(__('Trash', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=trash&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Trash', 'mp') . "</a>";
  3913. } else if ($order->post_status == 'order_paid') {
  3914. $actions['received'] = __('Received', 'mp');
  3915. $actions['paid current'] = __('Paid', 'mp');
  3916. $actions['shipped'] = "<a title='" . esc_attr(__('Mark as Shipped', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=shipped&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Shipped', 'mp') . "</a>";
  3917. $actions['closed'] = "<a title='" . esc_attr(__('Mark as Closed', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=closed&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Closed', 'mp') . "</a>";
  3918. $actions['trash'] = "<a title='" . esc_attr(__('Trash', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=trash&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Trash', 'mp') . "</a>";
  3919. } else if ($order->post_status == 'order_shipped') {
  3920. $actions['received'] = __('Received', 'mp');
  3921. $actions['paid'] = __('Paid', 'mp');
  3922. $actions['shipped current'] = __('Shipped', 'mp');
  3923. $actions['closed'] = "<a title='" . esc_attr(__('Mark as Closed', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=closed&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Closed', 'mp') . "</a>";
  3924. $actions['trash'] = "<a title='" . esc_attr(__('Trash', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=trash&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Trash', 'mp') . "</a>";
  3925. } else if ($order->post_status == 'order_closed') {
  3926. $actions['received'] = "<a title='" . esc_attr(__('Mark as Received', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=received&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Received', 'mp') . "</a>";
  3927. $actions['paid'] = "<a title='" . esc_attr(__('Mark as Paid', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=paid&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Paid', 'mp') . "</a>";
  3928. $actions['shipped'] = "<a title='" . esc_attr(__('Mark as Shipped', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=shipped&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Shipped', 'mp') . "</a>";
  3929. $actions['closed current'] = __('Closed', 'mp');
  3930. $actions['trash'] = "<a title='" . esc_attr(__('Trash', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=trash&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Trash', 'mp') . "</a>";
  3931. } else if ($order->post_status == "trash") {
  3932. $actions['received'] = "<a title='" . esc_attr(__('Mark as Received', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=received&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Received', 'mp') . "</a>";
  3933. $actions['paid'] = "<a title='" . esc_attr(__('Mark as Paid', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=paid&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Paid', 'mp') . "</a>";
  3934. $actions['shipped'] = "<a title='" . esc_attr(__('Mark as Shipped', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=shipped&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Shipped', 'mp') . "</a>";
  3935. $actions['closed'] = "<a title='" . esc_attr(__('Mark as Closed', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=closed&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Closed', 'mp') . "</a>";
  3936. $actions['delete'] = "<a title='" . esc_attr(__('Delete', 'mp')) . "' href='" . wp_nonce_url( admin_url( 'edit.php?post_type=product&amp;page=marketpress-orders&amp;action=delete&amp;post=' . $order->ID), 'update-order-status' ) . "'>" . __('Delete', 'mp') . "</a>";
  3937. }
  3938. $action_count = count($actions);
  3939. $i = 0;
  3940. echo '<div id="mp-single-statuses" class="misc-pub-section">';
  3941. foreach ( $actions as $action => $link ) {
  3942. ++$i;
  3943. ( $i == $action_count ) ? $sep = '' : $sep = ' &raquo; ';
  3944. echo "<span class='$action'>$link</span>$sep";
  3945. }
  3946. echo '</div>';
  3947. ?>
  3948. <div id="major-publishing-actions">
  3949. <form id="mp-single-order-form" action="<?php echo admin_url('edit.php'); ?>" method="get">
  3950. <div id="mp-single-order-buttons">
  3951. <input type="hidden" name="post_type" class="post_status_page" value="product" />
  3952. <input type="hidden" name="page" class="post_status_page" value="marketpress-orders" />
  3953. <input name="save" class="button-primary" id="publish" tabindex="1" value="<?php _e('&laquo; Back', 'mp'); ?>" type="submit" />
  3954. </div>
  3955. </form>
  3956. <div class="clear"></div>
  3957. </div>
  3958. </div>
  3959. </div>
  3960. </div>
  3961. <div id="mp-order-status" class="postbox">
  3962. <h3 class='hndle'><span><?php _e('Current Status', 'mp'); ?></span></h3>
  3963. <div class="inside">
  3964. <?php
  3965. //get times
  3966. $received = date_i18n(get_option('date_format') . ' - ' . get_option('time_format'), $order->mp_received_time);
  3967. if (isset($order->mp_paid_time) && $order->mp_paid_time)
  3968. $paid = date_i18n(get_option('date_format') . ' - ' . get_option('time_format'), $order->mp_paid_time);
  3969. if (isset($order->mp_shipped_time) && $order->mp_shipped_time)
  3970. $shipped = date_i18n(get_option('date_format') . ' - ' . get_option('time_format'), $order->mp_shipped_time);
  3971. if (isset($order->mp_closed_time) && $order->mp_closed_time)
  3972. $closed = date_i18n(get_option('date_format') . ' - ' . get_option('time_format'), $order->mp_closed_time);
  3973. if ($order->post_status == 'order_received') {
  3974. echo '<div id="major-publishing-actions" class="misc-pub-section">' . __('Received:', 'mp') . ' <strong>' . $received . '</strong></div>';
  3975. } else if ($order->post_status == 'order_paid') {
  3976. echo '<div id="major-publishing-actions" class="misc-pub-section">' . __('Paid:', 'mp') . ' <strong>' . $paid . '</strong></div>';
  3977. echo '<div class="misc-pub-section">' . __('Received:', 'mp') . ' <strong>' . $received . '</strong></div>';
  3978. } else if ($order->post_status == 'order_shipped') {
  3979. echo '<div id="major-publishing-actions" class="misc-pub-section">' . __('Shipped:', 'mp') . ' <strong>' . $shipped . '</strong></div>';
  3980. echo '<div class="misc-pub-section">' . __('Paid:', 'mp') . ' <strong>' . $paid . '</strong></div>';
  3981. echo '<div class="misc-pub-section">' . __('Received:', 'mp') . ' <strong>' . $received . '</strong></div>';
  3982. } else if ($order->post_status == 'order_closed') {
  3983. echo '<div id="major-publishing-actions" class="misc-pub-section">' . __('Closed:', 'mp') . ' <strong>' . $closed . '</strong></div>';
  3984. echo '<div class="misc-pub-section">' . __('Shipped:', 'mp') . ' <strong>' . $shipped . '</strong></div>';
  3985. echo '<div class="misc-pub-section">' . __('Paid:', 'mp') . ' <strong>' . $paid . '</strong></div>';
  3986. echo '<div class="misc-pub-section">' . __('Received:', 'mp') . ' <strong>' . $received . '</strong></div>';
  3987. } else if ($order->post_status == 'trash') {
  3988. echo '<div id="major-publishing-actions" class="misc-pub-section">' . __('Trashed', 'mp') . '</div>';
  3989. }
  3990. ?>
  3991. </div>
  3992. </div>
  3993. <div id="mp-order-payment" class="postbox">
  3994. <h3 class='hndle'><span><?php _e('Payment Information', 'mp'); ?></span></h3>
  3995. <div class="inside">
  3996. <div id="mp_payment_gateway" class="misc-pub-section">
  3997. <?php _e('Payment Gateway:', 'mp'); ?>
  3998. <strong><?php echo $order->mp_payment_info['gateway_private_name']; ?></strong>
  3999. </div>
  4000. <?php if ($order->mp_payment_info['method']) { ?>
  4001. <div id="mp_payment_method" class="misc-pub-section">
  4002. <?php _e('Payment Type:', 'mp'); ?>
  4003. <strong><?php echo $order->mp_payment_info['method']; ?></strong>
  4004. </div>
  4005. <?php } ?>
  4006. <?php if ($order->mp_payment_info['transaction_id']) { ?>
  4007. <div id="mp_transaction" class="misc-pub-section">
  4008. <?php _e('Transaction ID:', 'mp'); ?>
  4009. <strong><?php echo $order->mp_payment_info['transaction_id']; ?></strong>
  4010. </div>
  4011. <?php } ?>
  4012. <div id="major-publishing-actions" class="misc-pub-section">
  4013. <?php _e('Payment Total:', 'mp'); ?>
  4014. <strong><?php echo $this->format_currency($order->mp_payment_info['currency'], $order->mp_payment_info['total']) . ' ' . $order->mp_payment_info['currency']; ?></strong>
  4015. </div>
  4016. </div>
  4017. </div>
  4018. <?php if (is_array($order->mp_payment_info['status']) && count($order->mp_payment_info['status'])) { ?>
  4019. <div id="mp-order-payment-history" class="postbox">
  4020. <h3 class='hndle'><span><?php _e('Payment Transaction History', 'mp'); ?></span></h3>
  4021. <div class="inside">
  4022. <?php
  4023. $statuses = $order->mp_payment_info['status'];
  4024. krsort($statuses); //sort with latest status at the top
  4025. $first = true;
  4026. foreach ($statuses as $timestamp => $status) {
  4027. if ($first) {
  4028. echo '<div id="major-publishing-actions" class="misc-pub-section">';
  4029. $first = false;
  4030. } else {
  4031. echo '<div id="mp_payment_gateway" class="misc-pub-section">';
  4032. }
  4033. ?>
  4034. <strong><?php echo date_i18n(get_option('date_format') . ' - ' . get_option('time_format'), $timestamp); ?>:</strong>
  4035. <?php echo esc_html($status); ?>
  4036. </div>
  4037. <?php } ?>
  4038. </div>
  4039. </div>
  4040. <?php } ?>
  4041. </div></div>
  4042. <div id="post-body">
  4043. <div id="post-body-content">
  4044. <div id='normal-sortables' class='meta-box-sortables'>
  4045. <div id="mp-order-products" class="postbox">
  4046. <h3 class='hndle'><span><?php _e('Order Information', 'mp'); ?></span></h3>
  4047. <div class="inside">
  4048. <table id="mp-order-product-table" class="widefat">
  4049. <thead><tr>
  4050. <th class="mp_cart_col_thumb">&nbsp;</th>
  4051. <th class="mp_cart_col_sku"><?php _e('SKU', 'mp'); ?></th>
  4052. <th class="mp_cart_col_product"><?php _e('Item', 'mp'); ?></th>
  4053. <th class="mp_cart_col_quant"><?php _e('Quantity', 'mp'); ?></th>
  4054. <th class="mp_cart_col_price"><?php _e('Price', 'mp'); ?></th>
  4055. <th class="mp_cart_col_subtotal"><?php _e('Subtotal', 'mp'); ?></th>
  4056. <th class="mp_cart_col_downloads"><?php _e('Downloads', 'mp'); ?></th>
  4057. </tr></thead>
  4058. <tbody>
  4059. <?php
  4060. global $blog_id;
  4061. $bid = (is_multisite()) ? $blog_id : 1; // FPM
  4062. if (is_array($order->mp_cart_info) && count($order->mp_cart_info)) {
  4063. foreach ($order->mp_cart_info as $product_id => $variations) {
  4064. //for compatibility for old orders from MP 1.0
  4065. if (isset($variations['name'])) {
  4066. $data = $variations;
  4067. echo '<tr>';
  4068. echo ' <td class="mp_cart_col_thumb">' . mp_product_image( false, 'widget', $product_id ) . '</td>';
  4069. echo ' <td class="mp_cart_col_sku">' . esc_attr($data['SKU']) . '</td>';
  4070. echo ' <td class="mp_cart_col_product"><a href="' . get_permalink($product_id) . '">' . esc_attr($data['name']) . '</a></td>';
  4071. echo ' <td class="mp_cart_col_quant">' . number_format_i18n($data['quantity']) . '</td>';
  4072. echo ' <td class="mp_cart_col_price">' . $this->format_currency('', $data['price']) . '</td>';
  4073. echo ' <td class="mp_cart_col_subtotal">' . $this->format_currency('', $data['price'] * $data['quantity']) . '</td>';
  4074. echo ' <td class="mp_cart_col_downloads">' . __('N/A', 'mp') . '</td>';
  4075. echo '</tr>';
  4076. } else {
  4077. foreach ($variations as $variation => $data) {
  4078. echo '<tr>';
  4079. echo ' <td class="mp_cart_col_thumb">' . mp_product_image( false, 'widget', $product_id ) . '</td>';
  4080. echo ' <td class="mp_cart_col_sku">' . esc_attr($data['SKU']) . '</td>';
  4081. echo ' <td class="mp_cart_col_product"><a href="' . get_permalink($product_id) . '">' . esc_attr($data['name']) . '</a>';
  4082. //Output product custom field information
  4083. $cf_key = $bid .':'. $product_id .':'. $variation;
  4084. if (isset($order->mp_shipping_info['mp_custom_fields'][$cf_key])) {
  4085. $cf_item = $order->mp_shipping_info['mp_custom_fields'][$cf_key];
  4086. $mp_custom_field_label = get_post_meta($product_id, 'mp_custom_field_label', true);
  4087. if (isset($mp_custom_field_label[$variation]))
  4088. $label_text = esc_attr($mp_custom_field_label[$variation]);
  4089. else
  4090. $label_text = __('Product Personalization:', 'mp');
  4091. echo '<div class="mp_cart_custom_fields">'. $label_text .'<ol>';
  4092. foreach ($cf_item as $item) {
  4093. echo '<li>'. $item .'</li>';
  4094. }
  4095. echo '</ol></div>';
  4096. }
  4097. echo '</td>';
  4098. echo ' <td class="mp_cart_col_quant">' . number_format_i18n($data['quantity']) . '</td>';
  4099. echo ' <td class="mp_cart_col_price">' . $this->format_currency('', $data['price']) . '</td>';
  4100. echo ' <td class="mp_cart_col_subtotal">' . $this->format_currency('', $data['price'] * $data['quantity']) . '</td>';
  4101. if (is_array($data['download']))
  4102. echo ' <td class="mp_cart_col_downloads">' . number_format_i18n($data['download']['downloaded']) . (($data['download']['downloaded'] >= $max_downloads) ? __(' (Limit Reached)', 'mp') : '') . '</td>';
  4103. else
  4104. echo ' <td class="mp_cart_col_downloads">' . __('N/A', 'mp') . '</td>';
  4105. echo '</tr>';
  4106. }
  4107. }
  4108. }
  4109. } else {
  4110. echo '<tr><td colspan="7">' . __('No products could be found for this order', 'mp') . '</td></tr>';
  4111. }
  4112. ?>
  4113. </tbody>
  4114. </table><br />
  4115. <?php //coupon line
  4116. if ( isset($order->mp_discount_info) ) { ?>
  4117. <h3><?php _e('Coupon Discount:', 'mp'); ?></h3>
  4118. <p><?php echo $order->mp_discount_info['discount']; ?> (<?php echo $order->mp_discount_info['code']; ?>)</p>
  4119. <?php } ?>
  4120. <?php //shipping line
  4121. if ( $order->mp_shipping_total ) { ?>
  4122. <h3><?php _e('Shipping:', 'mp'); ?></h3>
  4123. <p><?php echo $this->format_currency('', $order->mp_shipping_total) . ' ( ' . strtoupper(isset($order->mp_shipping_info['shipping_option']) ? $order->mp_shipping_info['shipping_option'] : '') . ' ' . (isset($order->mp_shipping_info['shipping_sub_option']) ? $order->mp_shipping_info['shipping_sub_option'] : '') . ' )'; ?></p>
  4124. <?php } ?>
  4125. <?php //tax line
  4126. if ( $order->mp_tax_total ) { ?>
  4127. <h3><?php _e('Taxes:', 'mp'); ?></h3>
  4128. <p><?php echo $this->format_currency('', $order->mp_tax_total); ?></p>
  4129. <?php } ?>
  4130. <h3><?php _e('Cart Total:', 'mp'); ?></h3>
  4131. <p><?php echo $this->format_currency('', $order->mp_order_total); ?></p>
  4132. <?php //special instructions line
  4133. if ( !empty($order->mp_shipping_info['special_instructions']) ) { ?>
  4134. <h3><?php _e('Special Instructions:', 'mp'); ?></h3>
  4135. <p><?php echo wpautop(esc_html($order->mp_shipping_info['special_instructions'])); ?></p>
  4136. <?php } ?>
  4137. </div>
  4138. </div>
  4139. <form id="mp-shipping-form" action="" method="post">
  4140. <div id="mp-order-shipping-info" class="postbox">
  4141. <h3 class='hndle'><span><?php _e('Shipping Information', 'mp'); ?></span></h3>
  4142. <div class="inside">
  4143. <h3><?php _e('Address:', 'mp'); ?></h3>
  4144. <table>
  4145. <tr>
  4146. <td align="right"><?php _e('Full Name:', 'mp'); ?></td><td>
  4147. <?php esc_attr_e($order->mp_shipping_info['name']); ?></td>
  4148. </tr>
  4149. <tr>
  4150. <td align="right"><?php _e('Email:', 'mp'); ?></td><td>
  4151. <?php esc_attr_e($order->mp_shipping_info['email']); ?></td>
  4152. </tr>
  4153. <tr>
  4154. <td align="right"><?php _e('Address:', 'mp'); ?></td>
  4155. <td><?php esc_attr_e($order->mp_shipping_info['address1']); ?></td>
  4156. </tr>
  4157. <?php if ($order->mp_shipping_info['address2']) { ?>
  4158. <tr>
  4159. <td align="right"><?php _e('Address 2:', 'mp'); ?></td>
  4160. <td><?php esc_attr_e($order->mp_shipping_info['address2']); ?></td>
  4161. </tr>
  4162. <?php } ?>
  4163. <tr>
  4164. <td align="right"><?php _e('City:', 'mp'); ?></td>
  4165. <td><?php esc_attr_e($order->mp_shipping_info['city']); ?></td>
  4166. </tr>
  4167. <?php if ($order->mp_shipping_info['state']) { ?>
  4168. <tr>
  4169. <td align="right"><?php _e('State/Province/Region:', 'mp'); ?></td>
  4170. <td><?php esc_attr_e($order->mp_shipping_info['state']); ?></td>
  4171. </tr>
  4172. <?php } ?>
  4173. <tr>
  4174. <td align="right"><?php _e('Postal/Zip Code:', 'mp'); ?></td>
  4175. <td><?php esc_attr_e($order->mp_shipping_info['zip']); ?></td>
  4176. </tr>
  4177. <tr>
  4178. <td align="right"><?php _e('Country:', 'mp'); ?></td>
  4179. <td><?php echo $this->countries[$order->mp_shipping_info['country']]; ?></td>
  4180. </tr>
  4181. <?php if ($order->mp_shipping_info['phone']) { ?>
  4182. <tr>
  4183. <td align="right"><?php _e('Phone Number:', 'mp'); ?></td>
  4184. <td><?php esc_attr_e($order->mp_shipping_info['phone']); ?></td>
  4185. </tr>
  4186. <?php } ?>
  4187. </table>
  4188. <h3><?php _e('Cost:', 'mp'); ?></h3>
  4189. <p><?php echo $this->format_currency('', $order->mp_shipping_total) . ' ( ' . strtoupper(isset($order->mp_shipping_info['shipping_option']) ? $order->mp_shipping_info['shipping_option'] : '') . ' ' . (isset($order->mp_shipping_info['shipping_sub_option']) ? $order->mp_shipping_info['shipping_sub_option'] : '') . ' )'; ?></p>
  4190. <h3><?php _e('Shipping Method & Tracking Number:', 'mp'); ?></h3>
  4191. <p>
  4192. <select name="mp_shipping_method">
  4193. <option value="other"><?php _e('Choose Method:', 'mp'); ?></option>
  4194. <option value="UPS"<?php selected(@$order->mp_shipping_info['method'], 'UPS'); ?>>UPS</option>
  4195. <option value="FedEx"<?php selected(@$order->mp_shipping_info['method'], 'FedEx'); ?>>FedEx</option>
  4196. <option value="USPS"<?php selected(@$order->mp_shipping_info['method'], 'USPS'); ?>>USPS</option>
  4197. <option value="DHL"<?php selected(@$order->mp_shipping_info['method'], 'DHL'); ?>>DHL</option>
  4198. <option value="other"<?php selected(@$order->mp_shipping_info['method'], 'other'); ?>><?php _e('Other', 'mp'); ?></option>
  4199. <?php do_action('mp_shipping_tracking_select', @$order->mp_shipping_info['method']); ?>
  4200. </select>
  4201. <input type="text" name="mp_tracking_number" value="<?php esc_attr_e(isset($order->mp_shipping_info['tracking_num']) ? $order->mp_shipping_info['tracking_num'] : ''); ?>" size="25" />
  4202. <input type="submit" class="button-secondary" name="add-tracking" value="<?php _e('Save &raquo;', 'mp'); ?>" /><?php if ($order->post_status == 'order_received' ||$order->post_status == 'order_paid') { ?> <input type="submit" class="button-secondary" name="add-tracking-shipped" value="<?php _e('Save & Mark as Shipped &raquo;', 'mp'); ?>" /><?php } ?>
  4203. </p>
  4204. <?php //note line if set by gateway
  4205. if ( $order->mp_payment_info['note'] ) { ?>
  4206. <h3><?php _e('Special Note:', 'mp'); ?></h3>
  4207. <p><?php esc_html_e($order->mp_payment_info['note']); ?></p>
  4208. <?php } ?>
  4209. <?php do_action('mp_single_order_display_shipping', $order); ?>
  4210. </div>
  4211. </div>
  4212. <div id="mp-order-notes" class="postbox">
  4213. <h3 class='hndle'><span><?php _e('Order Notes', 'mp'); ?></span> - <span class="description"><?php _e('These notes will be displayed on the order status page', 'mp'); ?></span></h3>
  4214. <div class="inside">
  4215. <p>
  4216. <textarea name="mp_order_notes" rows="5" style="width: 100%;"><?php echo esc_textarea(isset($order->mp_order_notes) ? $order->mp_order_notes : ''); ?></textarea><br />
  4217. <input type="submit" class="button-secondary" name="save-note" value="<?php _e('Save &raquo;', 'mp'); ?>" />
  4218. </p>
  4219. </div>
  4220. </div>
  4221. </form>
  4222. <?php do_action('mp_single_order_display_box', $order); ?>
  4223. </div>
  4224. <div id='advanced-sortables' class='meta-box-sortables'>
  4225. </div>
  4226. </div>
  4227. </div>
  4228. <br class="clear" />
  4229. </div><!-- /poststuff -->
  4230. </div><!-- /wrap -->
  4231. <?php
  4232. }
  4233. function orders_page() {
  4234. //load single order view if id is set
  4235. if (isset($_GET['order_id'])) {
  4236. $this->single_order_page();
  4237. return;
  4238. }
  4239. //force post type
  4240. global $wpdb, $post_type, $wp_query, $wp_locale, $current_screen;
  4241. $post_type = 'mp_order';
  4242. $_GET['post_type'] = $post_type;
  4243. $post_type_object = get_post_type_object($post_type);
  4244. if ( !current_user_can($post_type_object->cap->edit_posts) )
  4245. wp_die(__('Cheatin&#8217; uh?'));
  4246. $pagenum = isset( $_GET['paged'] ) ? absint( $_GET['paged'] ) : 0;
  4247. if ( empty($pagenum) )
  4248. $pagenum = 1;
  4249. $per_page = 'edit_' . $post_type . '_per_page';
  4250. $per_page = (int) get_user_option( $per_page );
  4251. if ( empty( $per_page ) || $per_page < 1 )
  4252. $per_page = 15;
  4253. // @todo filter based on type
  4254. $per_page = apply_filters( 'edit_posts_per_page', $per_page );
  4255. // Handle bulk actions
  4256. if ( isset($_GET['doaction']) || isset($_GET['doaction2']) || isset($_GET['bulk_edit']) || isset($_GET['action'])
  4257. || (isset($_GET['delete_all'])) || (isset($_GET['delete_all2'])) ) {
  4258. check_admin_referer('update-order-status');
  4259. $sendback = remove_query_arg( array('received', 'paid', 'shipped', 'closed', 'trash', 'delete', 'ids', 'delete_all', 'delete_all2'), wp_get_referer() );
  4260. if ( ( $_GET['action'] != -1 || $_GET['action2'] != -1 ) && ( isset($_GET['post']) || isset($_GET['ids']) ) ) {
  4261. $post_ids = isset($_GET['post']) ? array_map( 'intval', (array) $_GET['post'] ) : explode(',', $_GET['ids']);
  4262. $doaction = ($_GET['action'] != -1) ? $_GET['action'] : $_GET['action2'];
  4263. } else if ( isset( $_GET['delete_all'] ) || isset( $_GET['delete_all2'] ) )
  4264. $doaction = 'delete_all';
  4265. switch ( $doaction ) {
  4266. case 'received':
  4267. $received = 0;
  4268. foreach( (array) $post_ids as $post_id ) {
  4269. $this->update_order_status($post_id, 'received');
  4270. $received++;
  4271. }
  4272. $msg = sprintf( _n( '%s order marked as Received.', '%s orders marked as Received.', $received, 'mp' ), number_format_i18n( $received ) );
  4273. break;
  4274. case 'paid':
  4275. $paid = 0;
  4276. foreach( (array) $post_ids as $post_id ) {
  4277. $this->update_order_status($post_id, 'paid');
  4278. $paid++;
  4279. }
  4280. $msg = sprintf( _n( '%s order marked as Paid.', '%s orders marked as Paid.', $paid, 'mp' ), number_format_i18n( $paid ) );
  4281. break;
  4282. case 'shipped':
  4283. $shipped = 0;
  4284. foreach( (array) $post_ids as $post_id ) {
  4285. $this->update_order_status($post_id, 'shipped');
  4286. $shipped++;
  4287. }
  4288. $msg = sprintf( _n( '%s order marked as Shipped.', '%s orders marked as Shipped.', $shipped, 'mp' ), number_format_i18n( $shipped ) );
  4289. break;
  4290. case 'closed':
  4291. $closed = 0;
  4292. foreach( (array) $post_ids as $post_id ) {
  4293. $this->update_order_status($post_id, 'closed');
  4294. $closed++;
  4295. }
  4296. $msg = sprintf( _n( '%s order Closed.', '%s orders Closed.', $closed, 'mp' ), number_format_i18n( $closed ) );
  4297. break;
  4298. case 'trash':
  4299. $trashed = 0;
  4300. foreach( (array) $post_ids as $post_id ) {
  4301. $this->update_order_status($post_id, 'trash');
  4302. $trashed++;
  4303. }
  4304. $msg = sprintf( _n( '%s order moved to Trash.', '%s orders moved to Trash.', $trashed, 'mp' ), number_format_i18n( $trashed ) );
  4305. break;
  4306. case 'delete':
  4307. $deleted = 0;
  4308. foreach( (array) $post_ids as $post_id ) {
  4309. $this->update_order_status($post_id, 'delete');
  4310. $deleted++;
  4311. }
  4312. $msg = sprintf( _n( '%s order Deleted.', '%s orders Deleted.', $deleted, 'mp' ), number_format_i18n( $deleted ) );
  4313. break;
  4314. case 'delete_all':
  4315. $mp_orders = get_posts('post_type=mp_order&post_status=trash&numberposts=-1');
  4316. if ($mp_orders) {
  4317. $deleted = 0;
  4318. foreach($mp_orders as $mp_order) {
  4319. $this->update_order_status($mp_order->ID, 'delete');
  4320. $deleted++;
  4321. }
  4322. $msg = sprintf( _n( '%s order Deleted.', '%s orders Deleted.', $deleted, 'mp' ), number_format_i18n( $deleted ) );
  4323. }
  4324. break;
  4325. }
  4326. }
  4327. $avail_post_stati = wp_edit_posts_query();
  4328. $num_pages = $wp_query->max_num_pages;
  4329. $mode = 'list';
  4330. ?>
  4331. <div class="wrap">
  4332. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/shopping-cart.png'; ?>" /></div>
  4333. <h2><?php _e('Manage Orders', 'mp');
  4334. if ( isset($_GET['s']) && $_GET['s'] )
  4335. printf( '<span class="subtitle">' . __('Search results for &#8220;%s&#8221;') . '</span>', get_search_query() ); ?>
  4336. </h2>
  4337. <?php if ( isset($msg) ) { ?>
  4338. <div class="updated fade"><p>
  4339. <?php echo $msg; ?>
  4340. </p></div>
  4341. <?php } ?>
  4342. <form id="posts-filter" action="<?php echo admin_url('edit.php'); ?>" method="get">
  4343. <ul class="subsubsub">
  4344. <?php
  4345. if ( empty($locked_post_status) ) :
  4346. $status_links = array();
  4347. $num_posts = wp_count_posts( $post_type, 'readable' );
  4348. $class = '';
  4349. $allposts = '';
  4350. $total_posts = array_sum( (array) $num_posts );
  4351. // Subtract post types that are not included in the admin all list.
  4352. foreach ( get_post_stati( array('show_in_admin_all_list' => false) ) as $state )
  4353. $total_posts -= $num_posts->$state;
  4354. $class = empty($class) && empty($_GET['post_status']) ? ' class="current"' : '';
  4355. $status_links[] = "<li><a href='edit.php?page=marketpress-orders&post_type=product{$allposts}'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_posts, 'posts' ), number_format_i18n( $total_posts ) ) . '</a>';
  4356. foreach ( get_post_stati(array(), 'objects') as $status_key => $status ) {
  4357. $class = '';
  4358. $status_name = $status->name;
  4359. if ( !in_array( $status_name, $avail_post_stati ) )
  4360. continue;
  4361. if ( empty( $num_posts->$status_name ) )
  4362. continue;
  4363. if ( isset($_GET['post_status']) && $status_name == $_GET['post_status'] )
  4364. $class = ' class="current"';
  4365. $status_links[$status_key] = "<li><a href='edit.php?page=marketpress-orders&amp;post_status=$status_name&amp;post_type=product'$class>" . sprintf( _n( $status->label_count[0], $status->label_count[1], $num_posts->$status_name ), number_format_i18n( $num_posts->$status_name ) ) . '</a>';
  4366. }
  4367. // Kludge. There has to be a better way to order stati. If present we want to 'trash' key always at the end.
  4368. // Maybe if we were properly inheriting WP_List_Table.
  4369. if (isset($status_links['trash'])) {
  4370. $trash_item = $status_links['trash'];
  4371. unset($status_links['trash']);
  4372. $status_links['trash'] = $trash_item;
  4373. }
  4374. echo implode( " |</li>\n", $status_links ) . '</li>';
  4375. unset( $status_links );
  4376. endif;
  4377. ?>
  4378. </ul>
  4379. <p class="search-box">
  4380. <label class="screen-reader-text" for="post-search-input"><?php _e('Search Orders', 'mp'); ?>:</label>
  4381. <input type="text" id="post-search-input" name="s" value="<?php the_search_query(); ?>" />
  4382. <input type="submit" value="<?php _e('Search Orders', 'mp'); ?>" class="button" />
  4383. </p>
  4384. <input type="hidden" name="post_type" class="post_status_page" value="product" />
  4385. <input type="hidden" name="page" class="post_status_page" value="marketpress-orders" />
  4386. <?php if (!empty($_GET['post_status'])) { ?>
  4387. <input type="hidden" name="post_status" class="post_status_page" value="<?php echo esc_attr($_GET['post_status']); ?>" />
  4388. <?php } ?>
  4389. <?php if ( have_posts() ) { ?>
  4390. <div class="tablenav">
  4391. <?php
  4392. $page_links = paginate_links( array(
  4393. 'base' => add_query_arg( 'paged', '%#%' ),
  4394. 'format' => '',
  4395. 'prev_text' => __('&laquo;'),
  4396. 'next_text' => __('&raquo;'),
  4397. 'total' => $num_pages,
  4398. 'current' => $pagenum
  4399. ));
  4400. ?>
  4401. <div class="alignleft actions">
  4402. <select name="action">
  4403. <option value="-1" selected="selected"><?php _e('Change Status', 'mp'); ?></option>
  4404. <option value="received"><?php _e('Received', 'mp'); ?></option>
  4405. <option value="paid"><?php _e('Paid', 'mp'); ?></option>
  4406. <option value="shipped"><?php _e('Shipped', 'mp'); ?></option>
  4407. <option value="closed"><?php _e('Closed', 'mp'); ?></option>
  4408. <?php if ((isset($_GET['post_status'])) && ($_GET['post_status'] == 'trash')) { ?>
  4409. <option value="delete"><?php _e('Delete', 'mp'); ?></option>
  4410. <?php } else { ?>
  4411. <option value="trash"><?php _e('Trash', 'mp'); ?></option>
  4412. <?php } ?>
  4413. </select>
  4414. <input type="submit" value="<?php esc_attr_e('Apply'); ?>" name="doaction" id="doaction" class="button-secondary action" />
  4415. <?php wp_nonce_field('update-order-status'); ?>
  4416. <?php // view filters
  4417. if ( !is_singular() ) {
  4418. $arc_query = $wpdb->prepare("SELECT DISTINCT YEAR(post_date) AS yyear, MONTH(post_date) AS mmonth FROM $wpdb->posts WHERE post_type = %s ORDER BY post_date DESC", $post_type);
  4419. $arc_result = $wpdb->get_results( $arc_query );
  4420. $month_count = count($arc_result);
  4421. if ( $month_count && !( 1 == $month_count && 0 == $arc_result[0]->mmonth ) ) {
  4422. $m = isset($_GET['m']) ? (int)$_GET['m'] : 0;
  4423. ?>
  4424. <select name='m'>
  4425. <option<?php selected( $m, 0 ); ?> value='0'><?php _e('Show all dates'); ?></option>
  4426. <?php
  4427. foreach ($arc_result as $arc_row) {
  4428. if ( $arc_row->yyear == 0 )
  4429. continue;
  4430. $arc_row->mmonth = zeroise( $arc_row->mmonth, 2 );
  4431. if ( $arc_row->yyear . $arc_row->mmonth == $m )
  4432. $default = ' selected="selected"';
  4433. else
  4434. $default = '';
  4435. echo "<option$default value='" . esc_attr("$arc_row->yyear$arc_row->mmonth") . "'>";
  4436. echo $wp_locale->get_month($arc_row->mmonth) . " $arc_row->yyear";
  4437. echo "</option>\n";
  4438. }
  4439. ?>
  4440. </select>
  4441. <?php } ?>
  4442. <input type="submit" id="post-query-submit" value="<?php esc_attr_e('Filter'); ?>" class="button-secondary" />
  4443. <?php } ?>
  4444. <?php
  4445. if ((isset($_GET['post_status'])) && ($_GET['post_status'] == 'trash')) {
  4446. submit_button( __( 'Empty Trash' ), 'button-secondary apply', 'delete_all', false );
  4447. }
  4448. ?>
  4449. </div>
  4450. <?php if ( $page_links ) { ?>
  4451. <div class="tablenav-pages"><?php
  4452. $count_posts = $post_type_object->hierarchical ? $wp_query->post_count : $wp_query->found_posts;
  4453. $page_links_text = sprintf( '<span class="displaying-num">' . __( 'Displaying %s&#8211;%s of %s' ) . '</span>%s',
  4454. number_format_i18n( ( $pagenum - 1 ) * $per_page + 1 ),
  4455. number_format_i18n( min( $pagenum * $per_page, $count_posts ) ),
  4456. number_format_i18n( $count_posts ),
  4457. $page_links
  4458. );
  4459. echo $page_links_text;
  4460. ?></div>
  4461. <?php } ?>
  4462. <div class="clear"></div>
  4463. </div>
  4464. <div class="clear"></div>
  4465. <table class="widefat <?php echo $post_type_object->hierarchical ? 'page' : 'post'; ?> fixed" cellspacing="0">
  4466. <thead>
  4467. <tr>
  4468. <?php print_column_headers( $current_screen ); ?>
  4469. </tr>
  4470. </thead>
  4471. <tfoot>
  4472. <tr>
  4473. <?php print_column_headers($current_screen, false); ?>
  4474. </tr>
  4475. </tfoot>
  4476. <tbody>
  4477. <?php
  4478. if ( function_exists('post_rows') ) {
  4479. post_rows();
  4480. } else {
  4481. $wp_list_table = _get_list_table('WP_Posts_List_Table');
  4482. $wp_list_table->display_rows();
  4483. }
  4484. ?>
  4485. </tbody>
  4486. </table>
  4487. <div class="tablenav">
  4488. <?php
  4489. if ( $page_links )
  4490. echo "<div class='tablenav-pages'>$page_links_text</div>";
  4491. ?>
  4492. <div class="alignleft actions">
  4493. <select name="action2">
  4494. <option value="-1" selected="selected"><?php _e('Change Status', 'mp'); ?></option>
  4495. <option value="received"><?php _e('Received', 'mp'); ?></option>
  4496. <option value="paid"><?php _e('Paid', 'mp'); ?></option>
  4497. <option value="shipped"><?php _e('Shipped', 'mp'); ?></option>
  4498. <option value="closed"><?php _e('Closed', 'mp'); ?></option>
  4499. <?php if ((isset($_GET['post_status'])) && ($_GET['post_status'] == 'trash')) { ?>
  4500. <option value="delete"><?php _e('Delete', 'mp'); ?></option>
  4501. <?php } else { ?>
  4502. <option value="trash"><?php _e('Trash', 'mp'); ?></option>
  4503. <?php } ?>
  4504. </select>
  4505. <input type="submit" value="<?php esc_attr_e('Apply'); ?>" name="doaction2" id="doaction2" class="button-secondary action" />
  4506. <?php
  4507. if ((isset($_GET['post_status'])) && ($_GET['post_status'] == 'trash')) {
  4508. submit_button( __( 'Empty Trash' ), 'button-secondary apply', 'delete_all2', false );
  4509. }
  4510. ?>
  4511. <br class="clear" />
  4512. </div>
  4513. <br class="clear" />
  4514. </div>
  4515. <?php } else { // have_posts() ?>
  4516. <div class="clear"></div>
  4517. <p><?php _e('No Orders Yet', 'mp'); ?></p>
  4518. <?php } ?>
  4519. </form>
  4520. <?php if (!isset($_GET['post_status']) || $_GET['post_status'] != 'trash') { ?>
  4521. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/download.png'; ?>" /></div>
  4522. <h2><?php _e('Export Orders', 'mp'); ?></h2>
  4523. <form action="<?php echo admin_url('admin-ajax.php?action=mp-orders-export'); ?>" method="post">
  4524. <?php
  4525. $months = $wpdb->get_results( $wpdb->prepare( "
  4526. SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
  4527. FROM $wpdb->posts
  4528. WHERE post_type = %s
  4529. ORDER BY post_date DESC
  4530. ", 'mp_order' ) );
  4531. $month_count = count( $months );
  4532. if ( !$month_count || ( 1 == $month_count && 0 == $months[0]->month ) )
  4533. return;
  4534. $m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
  4535. ?>
  4536. <select name='m'>
  4537. <option<?php selected( $m, 0 ); ?> value='0'><?php _e( 'Show all dates' ); ?></option>
  4538. <?php
  4539. foreach ( $months as $arc_row ) {
  4540. if ( 0 == $arc_row->year )
  4541. continue;
  4542. $month = zeroise( $arc_row->month, 2 );
  4543. $year = $arc_row->year;
  4544. printf( "<option %s value='%s'>%s</option>\n",
  4545. selected( $m, $year . $month, false ),
  4546. esc_attr( $arc_row->year . $month ),
  4547. $wp_locale->get_month( $month ) . " $year"
  4548. );
  4549. }
  4550. $status = isset( $_GET['post_status'] ) ? $_GET['post_status'] : 'all';
  4551. ?>
  4552. </select>
  4553. <select name="order_status">
  4554. <option<?php selected( $status, 'all' ); ?> value="all" selected="selected"><?php _e('All Statuses', 'mp'); ?></option>
  4555. <option<?php selected( $status, 'order_received' ); ?> value="order_received"><?php _e('Received', 'mp'); ?></option>
  4556. <option<?php selected( $status, 'order_paid' ); ?> value="order_paid"><?php _e('Paid', 'mp'); ?></option>
  4557. <option<?php selected( $status, 'order_shipped' ); ?> value="order_shipped"><?php _e('Shipped', 'mp'); ?></option>
  4558. <option<?php selected( $status, 'order_closed' ); ?> value="order_closed"><?php _e('Closed', 'mp'); ?></option>
  4559. </select>
  4560. <input type="submit" value="<?php _e('Download &raquo;', 'mp'); ?>" name="export_orders" class="button-secondary" />
  4561. </form>
  4562. <br class="clear">
  4563. <?php } ?>
  4564. </div>
  4565. <?php
  4566. }
  4567. function admin_page() {
  4568. global $wpdb;
  4569. //double-check rights
  4570. if(!current_user_can('manage_options')) {
  4571. echo "<p>" . __('Nice Try...', 'mp') . "</p>"; //If accessed properly, this message doesn't appear.
  4572. return;
  4573. }
  4574. $settings = get_option('mp_settings');
  4575. ?>
  4576. <div class="wrap">
  4577. <h3 class="nav-tab-wrapper">
  4578. <?php
  4579. $tab = ( !empty($_GET['tab']) ) ? $_GET['tab'] : 'main';
  4580. if (!$this->get_setting('disable_cart')) {
  4581. $tabs = array(
  4582. 'coupons' => __('Coupons', 'mp'),
  4583. 'presentation' => __('Presentation', 'mp'),
  4584. 'messages' => __('Messages', 'mp'),
  4585. 'shipping' => __('Shipping', 'mp'),
  4586. 'gateways' => __('Payments', 'mp'),
  4587. 'shortcodes' => __('Shortcodes', 'mp'),
  4588. 'importers' => __('Importers', 'mp')
  4589. );
  4590. } else {
  4591. $tabs = array(
  4592. 'presentation' => __('Presentation', 'mp'),
  4593. 'shortcodes' => __('Shortcodes', 'mp'),
  4594. 'importers' => __('Importers', 'mp')
  4595. );
  4596. }
  4597. $tabhtml = array();
  4598. // If someone wants to remove or add a tab
  4599. $tabs = apply_filters( 'marketpress_tabs', $tabs );
  4600. $class = ( 'main' == $tab ) ? ' nav-tab-active' : '';
  4601. $tabhtml[] = ' <a href="' . admin_url( 'edit.php?post_type=product&amp;page=marketpress' ) . '" class="nav-tab'.$class.'">' . __('General', 'mp') . '</a>';
  4602. foreach ( $tabs as $stub => $title ) {
  4603. $class = ( $stub == $tab ) ? ' nav-tab-active' : '';
  4604. $tabhtml[] = ' <a href="' . admin_url( 'edit.php?post_type=product&amp;page=marketpress&amp;tab=' . $stub ) . '" class="nav-tab'.$class.'">'.$title.'</a>';
  4605. }
  4606. echo implode( "\n", $tabhtml );
  4607. ?>
  4608. </h3>
  4609. <div class="clear"></div>
  4610. <?php
  4611. switch( $tab ) {
  4612. //---------------------------------------------------//
  4613. case "main":
  4614. //save settings
  4615. if (isset($_POST['marketplace_settings'])) {
  4616. //allow plugins to verify settings before saving
  4617. if (isset($_POST['mp']['tax']['canada_rate'])) {
  4618. foreach ($_POST['mp']['tax']['canada_rate'] as $key => $rate) {
  4619. $tax_rate = $rate * .01;
  4620. $_POST['mp']['tax']['canada_rate'][$key] = ($tax_rate < 1 && $tax_rate >= 0) ? $tax_rate : 0;
  4621. }
  4622. } else {
  4623. $tax_rate = $_POST['mp']['tax']['rate'] * .01;
  4624. $_POST['mp']['tax']['rate'] = ($tax_rate < 1 && $tax_rate >= 0) ? $tax_rate : 0;
  4625. }
  4626. $settings = array_merge($settings, apply_filters('mp_main_settings_filter', $_POST['mp']));
  4627. update_option('mp_settings', $settings);
  4628. echo '<div class="updated fade"><p>'.__('Settings saved.', 'mp').'</p></div>';
  4629. }
  4630. ?>
  4631. <script type="text/javascript">
  4632. jQuery(document).ready(function($) {
  4633. $("#mp-country-select, #mp-currency-select").change(function() {
  4634. $("#mp-main-form").submit();
  4635. });
  4636. });
  4637. </script>
  4638. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/settings.png'; ?>" /></div>
  4639. <h2><?php _e('General Settings', 'mp'); ?></h2>
  4640. <div id="poststuff" class="metabox-holder mp-settings">
  4641. <form id="mp-main-form" method="post" action="edit.php?post_type=product&amp;page=marketpress&amp;tab=main">
  4642. <input type="hidden" name="marketplace_settings" value="1" />
  4643. <div class="postbox">
  4644. <h3 class='hndle'><span><?php _e('Location Settings', 'mp') ?></span></h3>
  4645. <div class="inside">
  4646. <span class="description"><?php _e('This is the base location that shipping and tax rates will be calculated from.', 'mp') ?></span>
  4647. <table class="form-table">
  4648. <tr>
  4649. <th scope="row"><?php _e('Base Country', 'mp') ?></th>
  4650. <td>
  4651. <select id="mp-country-select" name="mp[base_country]">
  4652. <?php
  4653. foreach ($this->countries as $key => $value) {
  4654. ?><option value="<?php echo $key; ?>"<?php selected($this->get_setting('base_country'), $key); ?>><?php echo esc_attr($value); ?></option><?php
  4655. }
  4656. ?>
  4657. </select>
  4658. </td>
  4659. </tr>
  4660. <?php
  4661. switch ($this->get_setting('base_country')) {
  4662. case 'US':
  4663. $list = $this->usa_states;
  4664. break;
  4665. case 'CA':
  4666. $list = $this->canadian_provinces;
  4667. break;
  4668. case 'GB':
  4669. $list = $this->uk_counties;
  4670. break;
  4671. case 'AU':
  4672. $list = $this->australian_states;
  4673. break;
  4674. default:
  4675. $list = false;
  4676. }
  4677. //only show if correct country
  4678. if (is_array($list)) {
  4679. ?>
  4680. <tr>
  4681. <th scope="row"><?php _e('Base State/Province/Region', 'mp') ?></th>
  4682. <td>
  4683. <select name="mp[base_province]">
  4684. <?php
  4685. foreach ($list as $key => $value) {
  4686. ?><option value="<?php echo esc_attr($key); ?>"<?php selected($this->get_setting('base_province'), $key); ?>><?php echo esc_attr($value); ?></option><?php
  4687. }
  4688. ?>
  4689. </select>
  4690. </td>
  4691. </tr>
  4692. <?php }
  4693. //only show if correct country or US province
  4694. if ( is_array($list) || in_array( $this->get_setting('base_country'), array('UM','AS','FM','GU','MH','MP','PW','PR','PI') ) ) {
  4695. ?>
  4696. <tr>
  4697. <th scope="row"><?php _e('Base Zip/Postal Code', 'mp') ?></th>
  4698. <td>
  4699. <input value="<?php echo esc_attr($this->get_setting('base_zip')); ?>" size="10" name="mp[base_zip]" type="text" />
  4700. </td>
  4701. </tr>
  4702. <?php } ?>
  4703. </table>
  4704. </div>
  4705. </div>
  4706. <div class="postbox">
  4707. <h3 class='hndle'><span><?php _e('Tax Settings', 'mp') ?></span></h3>
  4708. <div class="inside">
  4709. <table class="form-table">
  4710. <?php
  4711. switch ($this->get_setting('base_country')) {
  4712. case 'US':
  4713. ?>
  4714. <tr>
  4715. <th scope="row"><?php echo sprintf(__('%s Tax Rate', 'mp'), esc_attr($this->usa_states[$this->get_setting('base_province')])); ?></th>
  4716. <td>
  4717. <input value="<?php echo $this->get_setting('tax->rate') * 100; ?>" size="3" name="mp[tax][rate]" type="text" style="text-align:right;" />%
  4718. </td>
  4719. </tr>
  4720. <?php
  4721. break;
  4722. case 'CA':
  4723. ?>
  4724. <tr>
  4725. <th scope="row"><a href="http://sbinfocanada.about.com/od/pst/a/PSTecommerce.htm" target="_blank"><?php _e('Total Tax Rates (VAT,GST,PST,HST)', 'mp'); ?></a></th>
  4726. <td>
  4727. <span class="description"><a href="http://en.wikipedia.org/wiki/Sales_taxes_in_Canada" target="_blank"><?php _e('Current Rates &raquo;', 'mp'); ?></a></span>
  4728. <table cellspacing="0" cellpadding="0">
  4729. <?php foreach ($this->canadian_provinces as $key => $label) { ?>
  4730. <tr>
  4731. <td style="padding: 0 5px;"><label for="mp_tax_<?php echo $key; ?>"><?php echo esc_attr($label); ?></label></td>
  4732. <td style="padding: 0 5px;"><input value="<?php echo $this->get_setting("tax->canada_rate->$key") * 100; ?>" size="3" id="mp_tax_<?php echo $key; ?>" name="mp[tax][canada_rate][<?php echo $key; ?>]" type="text" style="text-align:right;" />%</td>
  4733. </tr>
  4734. <?php } ?>
  4735. </table>
  4736. </td>
  4737. </tr>
  4738. <?php
  4739. break;
  4740. case 'GB':
  4741. ?>
  4742. <tr>
  4743. <th scope="row"><?php _e('VAT Tax Rate', 'mp') ?></th>
  4744. <td>
  4745. <input value="<?php echo $this->get_setting('tax->rate') * 100; ?>" size="3" name="mp[tax][rate]" type="text" style="text-align:right;" />%
  4746. </td>
  4747. </tr>
  4748. <?php
  4749. break;
  4750. case 'AU':
  4751. ?>
  4752. <tr>
  4753. <th scope="row"><?php _e('GST Tax Rate', 'mp') ?></th>
  4754. <td>
  4755. <input value="<?php echo $this->get_setting('tax->rate') * 100; ?>" size="3" name="mp[tax][rate]" type="text" style="text-align:right;" />%
  4756. </td>
  4757. </tr>
  4758. <?php
  4759. break;
  4760. default:
  4761. //in european union
  4762. if ( in_array($this->get_setting('base_country'), $this->eu_countries) ) {
  4763. ?>
  4764. <tr>
  4765. <th scope="row"><?php _e('VAT Tax Rate', 'mp') ?></th>
  4766. <td>
  4767. <input value="<?php echo $this->get_setting('tax->rate') * 100; ?>" size="3" name="mp[tax][rate]" type="text" style="text-align:right;" />%
  4768. </td>
  4769. </tr>
  4770. <?php
  4771. } else { //all other countries
  4772. ?>
  4773. <tr>
  4774. <th scope="row"><?php _e('Country Total Tax Rate (VAT, GST, Etc.)', 'mp') ?></th>
  4775. <td>
  4776. <input value="<?php echo $this->get_setting('tax->rate') * 100; ?>" size="3" name="mp[tax][rate]" type="text" style="text-align:right;" />%
  4777. </td>
  4778. </tr>
  4779. <tr>
  4780. <th scope="row"><?php _e('Tax Orders Outside Your Base Country?', 'mp'); ?></th>
  4781. <td>
  4782. <label><input value="1" name="mp[tax][tax_outside]" type="radio"<?php checked($this->get_setting('tax->tax_outside'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  4783. <label><input value="0" name="mp[tax][tax_outside]" type="radio"<?php checked($this->get_setting('tax->tax_outside'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  4784. </td>
  4785. </tr>
  4786. <?php
  4787. }
  4788. break;
  4789. }
  4790. ?>
  4791. <tr>
  4792. <th scope="row"><?php _e('Apply Tax To Shipping Fees?', 'mp') ?></th>
  4793. <td>
  4794. <label><input value="1" name="mp[tax][tax_shipping]" type="radio"<?php checked($this->get_setting('tax->tax_shipping'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  4795. <label><input value="0" name="mp[tax][tax_shipping]" type="radio"<?php checked($this->get_setting('tax->tax_shipping'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  4796. <br /><span class="description"><?php _e('Please see your local tax laws. Most areas charge tax on shipping fees.', 'mp') ?></span>
  4797. </td>
  4798. </tr>
  4799. <tr>
  4800. <th scope="row"><?php _e('Enter Prices Inclusive of Tax?', 'mp') ?></th>
  4801. <td>
  4802. <label><input value="1" name="mp[tax][tax_inclusive]" type="radio"<?php checked($this->get_setting('tax->tax_inclusive'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  4803. <label><input value="0" name="mp[tax][tax_inclusive]" type="radio"<?php checked($this->get_setting('tax->tax_inclusive'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  4804. <br /><span class="description"><?php _e('Enabling this option allows you to enter and show all prices inclusive of tax, while still listing the tax total as a line item in shopping carts. Please see your local tax laws.', 'mp') ?></span>
  4805. </td>
  4806. </tr>
  4807. </table>
  4808. </div>
  4809. </div>
  4810. <div class="postbox">
  4811. <h3 class='hndle'><span><?php _e('Currency Settings', 'mp') ?></span></h3>
  4812. <div class="inside">
  4813. <span class="description"><?php _e('These preferences affect display only. Your payment gateway of choice may not support every currency listed here.', 'mp') ?></span>
  4814. <table class="form-table">
  4815. <tr valign="top">
  4816. <th scope="row"><?php _e('Store Currency', 'mp') ?></th>
  4817. <td>
  4818. <select id="mp-currency-select" name="mp[currency]">
  4819. <?php
  4820. foreach ($this->currencies as $key => $value) {
  4821. ?><option value="<?php echo $key; ?>"<?php selected($this->get_setting('currency'), $key); ?>><?php echo esc_attr($value[0]) . ' - ' . $this->format_currency($key); ?></option><?php
  4822. }
  4823. ?>
  4824. </select>
  4825. </td>
  4826. </tr>
  4827. <tr valign="top">
  4828. <th scope="row"><?php _e('Currency Symbol Position', 'mp') ?></th>
  4829. <td>
  4830. <label><input value="1" name="mp[curr_symbol_position]" type="radio"<?php checked($this->get_setting('curr_symbol_position'), 1); ?>>
  4831. <?php echo $this->format_currency($this->get_setting('currency')); ?>100</label><br />
  4832. <label><input value="2" name="mp[curr_symbol_position]" type="radio"<?php checked($this->get_setting('curr_symbol_position'), 2); ?>>
  4833. <?php echo $this->format_currency($this->get_setting('currency')); ?> 100</label><br />
  4834. <label><input value="3" name="mp[curr_symbol_position]" type="radio"<?php checked($this->get_setting('curr_symbol_position'), 3); ?>>
  4835. 100<?php echo $this->format_currency($this->get_setting('currency')); ?></label><br />
  4836. <label><input value="4" name="mp[curr_symbol_position]" type="radio"<?php checked($this->get_setting('curr_symbol_position'), 4); ?>>
  4837. 100 <?php echo $this->format_currency($this->get_setting('currency')); ?></label>
  4838. </td>
  4839. </tr>
  4840. <tr valign="top">
  4841. <th scope="row"><?php _e('Show Decimal in Prices', 'mp') ?></th>
  4842. <td>
  4843. <label><input value="1" name="mp[curr_decimal]" type="radio"<?php checked( ( ($this->get_setting('curr_decimal') !== 0) ? 1 : 0 ), 1); ?>>
  4844. <?php _e('Yes', 'mp') ?></label>
  4845. <label><input value="0" name="mp[curr_decimal]" type="radio"<?php checked($this->get_setting('curr_decimal'), 0); ?>>
  4846. <?php _e('No', 'mp') ?></label>
  4847. </td>
  4848. </tr>
  4849. </table>
  4850. </div>
  4851. </div>
  4852. <div class="postbox">
  4853. <h3 class='hndle'><span><?php _e('Miscellaneous Settings', 'mp') ?></span></h3>
  4854. <div class="inside">
  4855. <table class="form-table">
  4856. <tr id="mp-inventory-setting">
  4857. <th scope="row"><?php _e('Inventory Warning Threshold', 'mp') ?></th>
  4858. <td>
  4859. <span class="description"><?php _e('At what low stock count do you want to be warned for products you have enabled inventory tracking for?', 'mp') ?></span><br />
  4860. <select name="mp[inventory_threshhold]">
  4861. <?php
  4862. $inventory_threshhold = $this->get_setting('inventory_threshhold', 3);
  4863. for ($i=0; $i<=100; $i++) {
  4864. $selected = ($inventory_threshhold == $i) ? ' selected="selected"' : '';
  4865. echo '<option value="' . $i . '"' . $selected . '">' . $i . '</option>';
  4866. }
  4867. ?>
  4868. </select>
  4869. </td>
  4870. </tr>
  4871. <tr id="mp-downloads-setting">
  4872. <th scope="row"><?php _e('Maximum Downloads', 'mp') ?></th>
  4873. <td>
  4874. <span class="description"><?php _e('How many times may a customer download a file they have purchased? (It\'s best to set this higher than one in case they have any problems downloading)', 'mp') ?></span><br />
  4875. <select name="mp[max_downloads]">
  4876. <?php
  4877. $max_downloads = $this->get_setting('max_downloads', 5);
  4878. for ($i=1; $i<=100; $i++) {
  4879. $selected = ($max_downloads == $i) ? ' selected="selected"' : '';
  4880. echo '<option value="' . $i . '"' . $selected . '">' . $i . '</option>';
  4881. }
  4882. ?>
  4883. </select>
  4884. </td>
  4885. </tr>
  4886. <tr>
  4887. <th scope="row"><?php _e('Force Login', 'mp') ?></th>
  4888. <td>
  4889. <?php $force_login = ($this->get_setting('force_login')) ? 1 : 0; ?>
  4890. <label><input value="1" name="mp[force_login]" type="radio"<?php checked($force_login, 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  4891. <label><input value="0" name="mp[force_login]" type="radio"<?php checked($force_login, 0) ?> /> <?php _e('No', 'mp') ?></label>
  4892. <br /><span class="description"><?php _e('Whether or not customers must be registered and logged in to checkout. (Not recommended: Enabling this can lower conversions)', 'mp') ?></span>
  4893. </td>
  4894. </tr>
  4895. <tr>
  4896. <th scope="row"><?php _e('Product Listings Only', 'mp') ?></th>
  4897. <td>
  4898. <label><input value="1" name="mp[disable_cart]" type="radio"<?php checked($this->get_setting('disable_cart'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  4899. <label><input value="0" name="mp[disable_cart]" type="radio"<?php checked($this->get_setting('disable_cart'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  4900. <br /><span class="description"><?php _e('This option turns MarketPress into more of a product listing plugin, disabling shopping carts, checkout, and order management. This is useful if you simply want to list items you can buy in a store somewhere else, optionally linking the "Buy Now" buttons to an external site. Some examples are a car dealership, or linking to songs/albums in itunes, or linking to products on another site with your own affiliate links.', 'mp') ?></span>
  4901. </td>
  4902. </tr>
  4903. <tr>
  4904. <th scope="row"><?php _e('Google Analytics Ecommerce Tracking', 'mp') ?></th>
  4905. <td>
  4906. <select name="mp[ga_ecommerce]">
  4907. <option value="none"<?php selected($this->get_setting('ga_ecommerce'), 'none') ?>><?php _e('None', 'mp') ?></option>
  4908. <option value="new"<?php selected($this->get_setting('ga_ecommerce'), 'new') ?>><?php _e('Asynchronous Tracking Code', 'mp') ?></option>
  4909. <option value="old"<?php selected($this->get_setting('ga_ecommerce'), 'old') ?>><?php _e('Old Tracking Code', 'mp') ?></option>
  4910. </select>
  4911. <br /><span class="description"><?php _e('If you already use Google Analytics for your website, you can track detailed ecommerce information by enabling this setting. Choose whether you are using the new asynchronous or old tracking code. Before Google Analytics can report ecommerce activity for your website, you must enable ecommerce tracking on the profile settings page for your website. Also keep in mind that some gateways do not reliably show the receipt page, so tracking may not be accurate in those cases. It is recommended to use the PayPal gateway for the most accurate data. <a href="http://analytics.blogspot.com/2009/05/how-to-use-ecommerce-tracking-in-google.html" target="_blank">More information &raquo;</a>', 'mp') ?></span>
  4912. </td>
  4913. </tr>
  4914. <tr>
  4915. <th scope="row"><?php _e('Special Instructions Field', 'mp') ?></th>
  4916. <td>
  4917. <label><input value="1" name="mp[special_instructions]" type="radio"<?php checked($this->get_setting('special_instructions'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  4918. <label><input value="0" name="mp[special_instructions]" type="radio"<?php checked($this->get_setting('special_instructions'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  4919. <br /><span class="description"><?php printf(__('Enabling this field will display a textbox on the shipping checkout page for users to enter special instructions for their order. Useful for product personalization, etc. Note you may want to <a href="%s">adjust the message on the shipping page.', 'mp'), admin_url('edit.php?post_type=product&page=marketpress&tab=messages#mp_msgs_shipping')); ?></span>
  4920. </td>
  4921. </tr>
  4922. </table>
  4923. </div>
  4924. </div>
  4925. <?php
  4926. //for adding additional settings for a shipping module
  4927. do_action('mp_general_settings');
  4928. ?>
  4929. <p class="submit">
  4930. <input class="button-primary" type="submit" name="submit_settings" value="<?php _e('Save Changes', 'mp') ?>" />
  4931. </p>
  4932. </form>
  4933. </div>
  4934. <?php
  4935. break;
  4936. //---------------------------------------------------//
  4937. case "coupons":
  4938. $coupons = get_option('mp_coupons');
  4939. if (!is_array($coupons)) $coupons = array();
  4940. //delete checked coupons
  4941. if (isset($_POST['allcoupon_delete'])) {
  4942. //check nonce
  4943. check_admin_referer('mp_coupons');
  4944. if (is_array($_POST['coupons_checks'])) {
  4945. //loop through and delete
  4946. foreach ($_POST['coupons_checks'] as $del_code)
  4947. unset($coupons[$del_code]);
  4948. update_option('mp_coupons', $coupons);
  4949. //display message confirmation
  4950. echo '<div class="updated fade"><p>'.__('Coupon(s) succesfully deleted.', 'mp').'</p></div>';
  4951. }
  4952. }
  4953. //save or add coupon
  4954. if (isset($_POST['submit_settings'])) {
  4955. //check nonce
  4956. check_admin_referer('mp_coupons');
  4957. $error = false;
  4958. $new_coupon_code = preg_replace('/[^A-Z0-9_-]/', '', strtoupper($_POST['coupon_code']));
  4959. if (empty($new_coupon_code))
  4960. $error[] = __('Please enter a valid Coupon Code', 'mp');
  4961. $coupons[$new_coupon_code]['discount'] = round($_POST['discount'], 2);
  4962. if ($coupons[$new_coupon_code]['discount'] <= 0)
  4963. $error[] = __('Please enter a valid Discount Amount', 'mp');
  4964. $coupons[$new_coupon_code]['discount_type'] = $_POST['discount_type'];
  4965. if ($coupons[$new_coupon_code]['discount_type'] != 'amt' && $coupons[$new_coupon_code]['discount_type'] != 'pct')
  4966. $error[] = __('Please choose a valid Discount Type', 'mp');
  4967. $coupons[$new_coupon_code]['start'] = strtotime($_POST['start']);
  4968. if ($coupons[$new_coupon_code]['start'] === false)
  4969. $error[] = __('Please enter a valid Start Date', 'mp');
  4970. $coupons[$new_coupon_code]['end'] = strtotime($_POST['end']);
  4971. if ($coupons[$new_coupon_code]['end'] && $coupons[$new_coupon_code]['end'] < $coupons[$new_coupon_code]['start'])
  4972. $error[] = __('Please enter a valid End Date not earlier than the Start Date', 'mp');
  4973. $coupons[$new_coupon_code]['uses'] = (is_numeric($_POST['uses'])) ? (int)$_POST['uses'] : '';
  4974. if (!$error) {
  4975. update_option('mp_coupons', $coupons);
  4976. $new_coupon_code = '';
  4977. echo '<div class="updated fade"><p>'.__('Coupon succesfully saved.', 'mp').'</p></div>';
  4978. }
  4979. }
  4980. //if editing a coupon
  4981. $new_coupon_code = isset($_GET['code']) ? $_GET['code'] : null;
  4982. ?>
  4983. <script type="text/javascript">
  4984. jQuery(document).ready(function ($) {
  4985. jQuery.datepicker.setDefaults(jQuery.datepicker.regional['<?php echo $this->language; ?>']);
  4986. jQuery('.pickdate').datepicker({dateFormat: 'yy-mm-dd', changeMonth: true, changeYear: true, minDate: 0, firstDay: <?php echo (get_option('start_of_week')=='0') ? 7 : get_option('start_of_week'); ?>});
  4987. });
  4988. </script>
  4989. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/service.png'; ?>" /></div>
  4990. <h2><?php _e('Coupons', 'mp') ?></h2>
  4991. <p><?php _e('You can create, delete, or update coupon codes for your store here.', 'mp') ?></p>
  4992. <?php
  4993. $apage = isset( $_GET['apage'] ) ? intval( $_GET['apage'] ) : 1;
  4994. $num = isset( $_GET['num'] ) ? intval( $_GET['num'] ) : 10;
  4995. $coupon_list = get_option('mp_coupons');
  4996. $total = (is_array($coupon_list)) ? count($coupon_list) : 0;
  4997. if ($total)
  4998. $coupon_list = array_slice($coupon_list, intval(($apage-1) * $num), intval($num), true);
  4999. $coupon_navigation = paginate_links( array(
  5000. 'base' => add_query_arg( 'apage', '%#%' ),
  5001. // 'base' => add_query_arg( 'apage', '%#%' ).$url2, //@todo: What is $url2???
  5002. 'format' => '',
  5003. 'total' => ceil($total / $num),
  5004. 'current' => $apage
  5005. ));
  5006. $page_link = ($apage > 1) ? '&amp;apage='.$apage : '';
  5007. ?>
  5008. <form id="form-coupon-list" action="edit.php?post_type=product&amp;page=marketpress&amp;tab=coupons<?php echo $page_link; ?>" method="post">
  5009. <?php wp_nonce_field('mp_coupons') ?>
  5010. <div class="tablenav">
  5011. <?php if ( $coupon_navigation ) echo "<div class='tablenav-pages'>$coupon_navigation</div>"; ?>
  5012. <div class="alignleft">
  5013. <input type="submit" value="<?php _e('Delete', 'mp') ?>" name="allcoupon_delete" class="button-secondary delete" />
  5014. <br class="clear" />
  5015. </div>
  5016. </div>
  5017. <br class="clear" />
  5018. <?php
  5019. // define the columns to display, the syntax is 'internal name' => 'display name'
  5020. $posts_columns = array(
  5021. 'code' => __('Coupon Code', 'mp'),
  5022. 'discount' => __('Discount', 'mp'),
  5023. 'start' => __('Start Date', 'mp'),
  5024. 'end' => __('Expire Date', 'mp'),
  5025. 'used' => __('Used', 'mp'),
  5026. 'remaining' => __('Remaining Uses', 'mp'),
  5027. 'edit' => __('Edit', 'mp')
  5028. );
  5029. ?>
  5030. <table width="100%" cellpadding="3" cellspacing="3" class="widefat">
  5031. <thead>
  5032. <tr>
  5033. <th scope="col" class="check-column"><input type="checkbox" /></th>
  5034. <?php foreach($posts_columns as $column_id => $column_display_name) {
  5035. $col_url = $column_display_name;
  5036. ?>
  5037. <th scope="col"><?php echo $col_url ?></th>
  5038. <?php } ?>
  5039. </tr>
  5040. </thead>
  5041. <tbody id="the-list">
  5042. <?php
  5043. if ( is_array($coupon_list) && count($coupon_list) ) {
  5044. $bgcolor = $class = '';
  5045. foreach ($coupon_list as $coupon_code => $coupon) {
  5046. $class = ('alternate' == $class) ? '' : 'alternate';
  5047. //assign classes based on coupon availability
  5048. $class = ($this->check_coupon($coupon_code)) ? $class . ' coupon-active' : $class . ' coupon-inactive';
  5049. echo '<tr class="'.$class.' blog-row">
  5050. <th scope="row" class="check-column">
  5051. <input type="checkbox" name="coupons_checks[]"" value="'.$coupon_code.'" />
  5052. </th>';
  5053. foreach( $posts_columns as $column_name=>$column_display_name ) {
  5054. switch($column_name) {
  5055. case 'code': ?>
  5056. <th scope="row">
  5057. <?php echo $coupon_code; ?>
  5058. </th>
  5059. <?php
  5060. break;
  5061. case 'discount': ?>
  5062. <th scope="row">
  5063. <?php
  5064. if ($coupon['discount_type'] == 'pct') {
  5065. echo $coupon['discount'].'%';
  5066. } else if ($coupon['discount_type'] == 'amt') {
  5067. echo $this->format_currency('', $coupon['discount']);
  5068. }
  5069. ?>
  5070. </th>
  5071. <?php
  5072. break;
  5073. case 'start': ?>
  5074. <th scope="row">
  5075. <?php echo date_i18n( get_option('date_format'), $coupon['start'] ); ?>
  5076. </th>
  5077. <?php
  5078. break;
  5079. case 'end': ?>
  5080. <th scope="row">
  5081. <?php echo ($coupon['end']) ? date_i18n( get_option('date_format'), $coupon['end'] ) : __('No End', 'mp'); ?>
  5082. </th>
  5083. <?php
  5084. break;
  5085. case 'used': ?>
  5086. <th scope="row">
  5087. <?php echo isset($coupon['used']) ? number_format_i18n($coupon['used']) : 0; ?>
  5088. </th>
  5089. <?php
  5090. break;
  5091. case 'remaining': ?>
  5092. <th scope="row">
  5093. <?php
  5094. if ($coupon['uses'])
  5095. echo number_format_i18n(intval($coupon['uses']) - intval(@$coupon['used']));
  5096. else
  5097. _e('Unlimited', 'mp');
  5098. ?>
  5099. </th>
  5100. <?php
  5101. break;
  5102. case 'edit': ?>
  5103. <th scope="row">
  5104. <a href="edit.php?post_type=product&amp;page=marketpress&amp;tab=coupons<?php echo $page_link; ?>&amp;code=<?php echo $coupon_code; ?>#add_coupon"><?php _e('Edit', 'mp') ?>&raquo;</a>
  5105. </th>
  5106. <?php
  5107. break;
  5108. }
  5109. }
  5110. ?>
  5111. </tr>
  5112. <?php
  5113. }
  5114. } else {
  5115. $bgcolor = ''; ?>
  5116. <tr style='background-color: <?php echo $bgcolor; ?>'>
  5117. <td colspan="7"><?php _e('No coupons yet.', 'mp') ?></td>
  5118. </tr>
  5119. <?php
  5120. } // end if coupons
  5121. ?>
  5122. </tbody>
  5123. <tfoot>
  5124. <tr>
  5125. <th scope="col" class="check-column"><input type="checkbox" /></th>
  5126. <?php foreach($posts_columns as $column_id => $column_display_name) {
  5127. $col_url = $column_display_name;
  5128. ?>
  5129. <th scope="col"><?php echo $col_url ?></th>
  5130. <?php } ?>
  5131. </tr>
  5132. </tfoot>
  5133. </table>
  5134. <div class="tablenav">
  5135. <?php if ( $coupon_navigation ) echo "<div class='tablenav-pages'>$coupon_navigation</div>"; ?>
  5136. </div>
  5137. <div id="poststuff" class="metabox-holder mp-settings">
  5138. <div class="postbox">
  5139. <h3 class='hndle'><span>
  5140. <?php
  5141. if ( isset($_GET['code']) || (isset( $error ) && $error) ) {
  5142. _e('Edit Coupon', 'mp');
  5143. } else {
  5144. _e('Add Coupon', 'mp');
  5145. }
  5146. ?></span></h3>
  5147. <div class="inside">
  5148. <?php
  5149. //display error message if it exists
  5150. if ( isset( $error ) && $error ) {
  5151. ?><div class="error"><p><?php echo implode("<br>\n", $error); ?></p></div><?php
  5152. }
  5153. //setup defaults
  5154. if (isset($coupons[$new_coupon_code]))
  5155. $discount = (isset($coupons[$new_coupon_code]['discount']) && isset($coupons[$new_coupon_code]['discount_type']) && $coupons[$new_coupon_code]['discount_type'] == 'amt') ? round($coupons[$new_coupon_code]['discount'], 2) : $coupons[$new_coupon_code]['discount'];
  5156. else
  5157. $discount = '';
  5158. $discount_type = isset($coupons[$new_coupon_code]['discount_type']) ? $coupons[$new_coupon_code]['discount_type'] : '';
  5159. $start = !empty($coupons[$new_coupon_code]['start']) ? date('Y-m-d', $coupons[$new_coupon_code]['start']) : date('Y-m-d');
  5160. $end = !empty($coupons[$new_coupon_code]['end']) ? date('Y-m-d', $coupons[$new_coupon_code]['end']) : '';
  5161. $uses = isset($coupons[$new_coupon_code]['uses']) ? $coupons[$new_coupon_code]['uses'] : '';
  5162. ?>
  5163. <table id="add_coupon">
  5164. <thead>
  5165. <tr>
  5166. <th>
  5167. <?php _e('Coupon Code', 'mp') ?><br />
  5168. <small style="font-weight: normal;"><?php _e('Letters and Numbers only', 'mp') ?></small>
  5169. </th>
  5170. <th><?php _e('Discount', 'mp') ?></th>
  5171. <th><?php _e('Start Date', 'mp') ?></th>
  5172. <th>
  5173. <?php _e('Expire Date', 'mp') ?><br />
  5174. <small style="font-weight: normal;"><?php _e('No end if blank', 'mp') ?></small>
  5175. </th>
  5176. <th>
  5177. <?php _e('Allowed Uses', 'mp') ?><br />
  5178. <small style="font-weight: normal;"><?php _e('Unlimited if blank', 'mp') ?></small>
  5179. </th>
  5180. </tr>
  5181. </thead>
  5182. <tbody>
  5183. <tr>
  5184. <td>
  5185. <input value="<?php echo $new_coupon_code ?>" name="coupon_code" type="text" style="text-transform: uppercase;" />
  5186. </td>
  5187. <td>
  5188. <input value="<?php echo $discount; ?>" size="3" name="discount" type="text" />
  5189. <select name="discount_type">
  5190. <option value="amt"<?php selected($discount_type, 'amt') ?>><?php echo $this->format_currency(); ?></option>
  5191. <option value="pct"<?php selected($discount_type, 'pct') ?>>%</option>
  5192. </select>
  5193. </td>
  5194. <td>
  5195. <input value="<?php echo $start; ?>" class="pickdate" size="11" name="start" type="text" />
  5196. </td>
  5197. <td>
  5198. <input value="<?php echo $end; ?>" class="pickdate" size="11" name="end" type="text" />
  5199. </td>
  5200. <td>
  5201. <input value="<?php echo $uses; ?>" size="4" name="uses" type="text" />
  5202. </td>
  5203. </tr>
  5204. </tbody>
  5205. </table>
  5206. <p class="submit">
  5207. <input class="button-primary" type="submit" name="submit_settings" value="<?php _e('Save Coupon', 'mp') ?>" />
  5208. </p>
  5209. </div>
  5210. </div>
  5211. </div>
  5212. </form>
  5213. <?php
  5214. break;
  5215. //---------------------------------------------------//
  5216. case "presentation":
  5217. //save settings
  5218. if (isset($_POST['marketplace_settings'])) {
  5219. //get old store slug
  5220. $old_slug = $this->get_setting('slugs->store');
  5221. //filter slugs
  5222. $_POST['mp']['slugs'] = array_map('sanitize_title', (array)$_POST['mp']['slugs']);
  5223. // Fixing http://premium.wpmudev.org/forums/topic/store-page-content-overwritten
  5224. $new_slug = $_POST['mp']['slugs']['store'];
  5225. $new_post_id = $wpdb->get_var("SELECT ID FROM " . $wpdb->posts . " WHERE post_name = '$new_slug' AND post_type = 'page'");
  5226. if ($new_slug != $old_slug && $new_post_id != 0) {
  5227. echo '<div class="error fade"><p>'.__('Store base URL conflicts with another page', 'mp').'</p></div>';
  5228. } else {
  5229. $settings = array_merge($settings, apply_filters('mp_presentation_settings_filter', $_POST['mp']));
  5230. update_option('mp_settings', $settings);
  5231. $this->create_store_page($old_slug);
  5232. //schedule flush rewrite rules due to product slugs on next page load (too late to do it here)
  5233. update_option('mp_flush_rewrite', 1);
  5234. echo '<div class="updated fade"><p>'.__('Settings saved.', 'mp').'</p></div>';
  5235. }
  5236. }
  5237. ?>
  5238. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/my_work.png'; ?>" /></div>
  5239. <h2><?php _e('Presentation Settings', 'mp'); ?></h2>
  5240. <div id="poststuff" class="metabox-holder mp-settings">
  5241. <form method="post" action="edit.php?post_type=product&amp;page=marketpress&amp;tab=presentation">
  5242. <input type="hidden" name="marketplace_settings" value="1" />
  5243. <div class="postbox">
  5244. <h3 class='hndle'><span><?php _e('General Settings', 'mp') ?></span></h3>
  5245. <div class="inside">
  5246. <table class="form-table">
  5247. <tr>
  5248. <th scope="row"><?php _e('Store Style', 'mp') ?></th>
  5249. <td>
  5250. <?php $this->store_themes_select(); ?>
  5251. <br /><span class="description"><?php _e('This option changes the built-in css styles for store pages.', 'mp') ?></span>
  5252. <?php if ((is_multisite() && is_super_admin()) || !is_multisite()) { ?>
  5253. <br /><span class="description"><?php printf(__('For a custom css style, save your css file with the "MarketPress Style: NAME" header in the "%s/marketpress-styles/" folder and it will appear in this list so you may select it. You can also select "None" and create custom theme templates and css to make your own completely unique store design. More information on that <a href="%sthemes/Themeing_MarketPress.txt">here &raquo;</a>', 'mp'), WP_CONTENT_DIR, $this->plugin_url); ?></span>
  5254. <h4><?php _e('Full-featured MarketPress Themes:', 'mp') ?></h4>
  5255. <div class="mp-theme-preview"><a title="<?php _e('Download Now &raquo;', 'mp') ?>" href="http://premium.wpmudev.org/project/frame-market-theme"><img alt="FrameMarket Theme" src="http://premium.wpmudev.org/wp-content/projects/219/listing-image-thumb.png" />
  5256. <strong><?php _e('FrameMarket/GridMarket', 'mp') ?></strong></a><br />
  5257. <?php _e('The ultimate MarkePress theme brings visual perfection to WordPress e-commerce. This professional front-end does all the work for you!', 'mp') ?></div>
  5258. <div class="mp-theme-preview"><a title="<?php _e('Download Now &raquo;', 'mp') ?>" href="http://premium.wpmudev.org/project/simplemarket"><img alt="SimpleMarket Theme" src="http://premium.wpmudev.org/wp-content/projects/237/listing-image-thumb.png" />
  5259. <strong><?php _e('SimpleMarket', 'mp') ?></strong></a><br />
  5260. <?php _e('The FREE SimpleMarket Theme uses an HTML 5 responsive design so your e-commerce site looks great across all screen-sizes and devices such as smartphones or tablets!', 'mp') ?></div>
  5261. <?php } ?>
  5262. </td>
  5263. </tr>
  5264. <tr>
  5265. <th scope="row"><?php _e('Show breadcrumbs for purchase process?', 'mp') ?></th>
  5266. <td>
  5267. <label><input value="1" name="mp[show_purchase_breadcrumbs]" type="radio"<?php checked($this->get_setting('show_purchase_breadcrumbs'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  5268. <label><input value="0" name="mp[show_purchase_breadcrumbs]" type="radio"<?php checked($this->get_setting('show_purchase_breadcrumbs'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  5269. <br /><span class="description"><?php _e('Show previous, current and next steps when a customer is purchasing their cart, shown below the title.', 'mp') ?></span>
  5270. </td>
  5271. </tr>
  5272. </table>
  5273. </div>
  5274. </div>
  5275. <div class="postbox">
  5276. <h3 class='hndle'><span><?php _e('Single Product Settings', 'mp') ?></span></h3>
  5277. <div class="inside">
  5278. <table class="form-table">
  5279. <tr>
  5280. <th scope="row"><?php _e('Checkout Button Type', 'mp') ?></th>
  5281. <td>
  5282. <label><input value="addcart" name="mp[product_button_type]" type="radio"<?php checked($this->get_setting('product_button_type'), 'addcart') ?> /> <?php _e('Add To Cart', 'mp') ?></label><br />
  5283. <label><input value="buynow" name="mp[product_button_type]" type="radio"<?php checked($this->get_setting('product_button_type'), 'buynow') ?> /> <?php _e('Buy Now', 'mp') ?></label>
  5284. </td>
  5285. </tr>
  5286. <tr>
  5287. <th scope="row"><?php _e('Show Quantity Option', 'mp') ?></th>
  5288. <td>
  5289. <label><input value="1" name="mp[show_quantity]" type="radio"<?php checked($this->get_setting('show_quantity'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  5290. <label><input value="0" name="mp[show_quantity]" type="radio"<?php checked($this->get_setting('show_quantity'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  5291. </td>
  5292. </tr>
  5293. <tr>
  5294. <th scope="row"><?php _e('Show Product Image', 'mp') ?></th>
  5295. <td>
  5296. <label><input value="1" name="mp[show_img]" type="radio"<?php checked($this->get_setting('show_img'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  5297. <label><input value="0" name="mp[show_img]" type="radio"<?php checked($this->get_setting('show_img'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  5298. </td>
  5299. </tr>
  5300. <tr>
  5301. <th scope="row"><?php _e('Product Image Size', 'mp') ?></th>
  5302. <td>
  5303. <label><input value="thumbnail" name="mp[product_img_size]" type="radio"<?php checked($this->get_setting('product_img_size'), 'thumbnail') ?> /> <a href="options-media.php"><?php _e('WP Thumbnail size', 'mp') ?></a></label><br />
  5304. <label><input value="medium" name="mp[product_img_size]" type="radio"<?php checked($this->get_setting('product_img_size'), 'medium') ?> /> <a href="options-media.php"><?php _e('WP Medium size', 'mp') ?></a></label><br />
  5305. <label><input value="large" name="mp[product_img_size]" type="radio"<?php checked($this->get_setting('product_img_size'), 'large') ?> /> <a href="options-media.php"><?php _e('WP Large size', 'mp') ?></a></label><br />
  5306. <label><input value="custom" name="mp[product_img_size]" type="radio"<?php checked($this->get_setting('product_img_size'), 'custom') ?> /> <?php _e('Custom', 'mp') ?></label>:&nbsp;&nbsp;
  5307. <label><?php _e('Height', 'mp') ?><input size="3" name="mp[product_img_height]" value="<?php echo esc_attr($this->get_setting('product_img_height')) ?>" type="text" /></label>&nbsp;
  5308. <label><?php _e('Width', 'mp') ?><input size="3" name="mp[product_img_width]" value="<?php echo esc_attr($this->get_setting('product_img_width')) ?>" type="text" /></label>
  5309. </td>
  5310. </tr>
  5311. <tr>
  5312. <th scope="row"><?php _e('Show Image Lightbox', 'mp') ?></th>
  5313. <td>
  5314. <label><input value="1" name="mp[show_lightbox]" type="radio"<?php checked($this->get_setting('show_lightbox'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  5315. <label><input value="0" name="mp[show_lightbox]" type="radio"<?php checked($this->get_setting('show_lightbox'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  5316. <br /><span class="description"><?php _e('Makes clicking the single product image open an instant zoomed preview.', 'mp') ?></span>
  5317. </td>
  5318. </tr>
  5319. </table>
  5320. </div>
  5321. </div>
  5322. <div class="postbox">
  5323. <h3 class='hndle'><span><?php _e('Product List Settings', 'mp') ?></span></h3>
  5324. <div class="inside">
  5325. <table class="form-table">
  5326. <tr>
  5327. <th scope="row"><?php _e('Product List View', 'mp') ?></th>
  5328. <td>
  5329. <label><input value="list" name="mp[list_view]" type="radio"<?php checked($this->get_setting('list_view', 'grid'), 'list') ?> /> <?php _e('List View', 'mp') ?></label><br />
  5330. <label><input value="grid" name="mp[list_view]" type="radio"<?php checked($this->get_setting('list_view', 'grid'), 'grid') ?> /> <?php _e('Grid View', 'mp') ?></label>
  5331. </td>
  5332. </tr>
  5333. <tr>
  5334. <th scope="row"><?php _e('Checkout Button Type', 'mp') ?></th>
  5335. <td>
  5336. <label><input value="addcart" name="mp[list_button_type]" type="radio"<?php checked($this->get_setting('list_button_type'), 'addcart') ?> /> <?php _e('Add To Cart', 'mp') ?></label><br />
  5337. <label><input value="buynow" name="mp[list_button_type]" type="radio"<?php checked($this->get_setting('list_button_type'), 'buynow') ?> /> <?php _e('Buy Now', 'mp') ?></label>
  5338. </td>
  5339. </tr>
  5340. <tr>
  5341. <th scope="row"><?php _e('Show Product Thumbnail', 'mp') ?></th>
  5342. <td>
  5343. <label><input value="1" name="mp[show_thumbnail]" type="radio"<?php checked($this->get_setting('show_thumbnail'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  5344. <label><input value="0" name="mp[show_thumbnail]" type="radio"<?php checked($this->get_setting('show_thumbnail'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  5345. </td>
  5346. </tr>
  5347. <tr>
  5348. <th scope="row"><?php _e('Product Thumbnail Size', 'mp') ?></th>
  5349. <td>
  5350. <label><input value="thumbnail" name="mp[list_img_size]" type="radio"<?php checked($this->get_setting('list_img_size'), 'thumbnail') ?> /> <a href="options-media.php"><?php _e('WP Thumbnail size', 'mp') ?></a></label><br />
  5351. <label><input value="medium" name="mp[list_img_size]" type="radio"<?php checked($this->get_setting('list_img_size'), 'medium') ?> /> <a href="options-media.php"><?php _e('WP Medium size', 'mp') ?></a></label><br />
  5352. <label><input value="large" name="mp[list_img_size]" type="radio"<?php checked($this->get_setting('list_img_size'), 'large') ?> /> <a href="options-media.php"><?php _e('WP Large size', 'mp') ?></a></label><br />
  5353. <label><input value="custom" name="mp[list_img_size]" type="radio"<?php checked($this->get_setting('list_img_size'), 'custom') ?> /> <?php _e('Custom', 'mp') ?></label>:&nbsp;&nbsp;
  5354. <label><?php _e('Height', 'mp') ?><input size="3" name="mp[list_img_height]" value="<?php echo esc_attr($this->get_setting('list_img_height')) ?>" type="text" /></label>&nbsp;
  5355. <label><?php _e('Width', 'mp') ?><input size="3" name="mp[list_img_width]" value="<?php echo esc_attr($this->get_setting('list_img_width')) ?>" type="text" /></label>
  5356. </td>
  5357. </tr>
  5358. <tr>
  5359. <th scope="row"><?php _e('Show Excerpts', 'mp') ?></th>
  5360. <td>
  5361. <label><input value="1" name="mp[show_excerpt]" type="radio"<?php checked($this->get_setting('show_excerpt'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  5362. <label><input value="0" name="mp[show_excerpt]" type="radio"<?php checked($this->get_setting('show_excerpt'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  5363. </td>
  5364. </tr>
  5365. <tr>
  5366. <th scope="row"><?php _e('Paginate Products', 'mp') ?></th>
  5367. <td>
  5368. <label><input value="1" name="mp[paginate]" type="radio"<?php checked($this->get_setting('paginate'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  5369. <label><input value="0" name="mp[paginate]" type="radio"<?php checked($this->get_setting('paginate'), 0) ?> /> <?php _e('No', 'mp') ?></label>&nbsp;&nbsp;
  5370. <label><input value="<?php echo esc_attr($this->get_setting('per_page', 20)) ?>" name="mp[per_page]" type="text" size="2" /> <?php _e('Products per page', 'mp') ?></label>
  5371. </td>
  5372. </tr>
  5373. <tr>
  5374. <th scope="row"><?php _e('Order Products By', 'mp') ?></th>
  5375. <td>
  5376. <select name="mp[order_by]">
  5377. <option value="title"<?php selected($this->get_setting('order_by'), 'title') ?>><?php _e('Product Name', 'mp') ?></option>
  5378. <option value="date"<?php selected($this->get_setting('order_by'), 'date') ?>><?php _e('Publish Date', 'mp') ?></option>
  5379. <option value="ID"<?php selected($this->get_setting('order_by'), 'ID') ?>><?php _e('Product ID', 'mp') ?></option>
  5380. <option value="author"<?php selected($this->get_setting('order_by'), 'author') ?>><?php _e('Product Author', 'mp') ?></option>
  5381. <option value="sales"<?php selected($this->get_setting('order_by'), 'sales') ?>><?php _e('Number of Sales', 'mp') ?></option>
  5382. <option value="price"<?php selected($this->get_setting('order_by'), 'price') ?>><?php _e('Product Price', 'mp') ?></option>
  5383. <option value="rand"<?php selected($this->get_setting('order_by'), 'rand') ?>><?php _e('Random', 'mp') ?></option>
  5384. </select>
  5385. <label><input value="DESC" name="mp[order]" type="radio"<?php checked($this->get_setting('order'), 'DESC') ?> /> <?php _e('Descending', 'mp') ?></label>
  5386. <label><input value="ASC" name="mp[order]" type="radio"<?php checked($this->get_setting('order'), 'ASC') ?> /> <?php _e('Ascending', 'mp') ?></label>
  5387. </td>
  5388. </tr>
  5389. <tr>
  5390. <td scope="row"><?php _e('Show Product Filters', 'mp') ?></td>
  5391. <td>
  5392. <label><input value="1" name="mp[show_filters]" type="radio"<?php checked($this->get_setting('show_filters'), 1) ?> /> <?php _e('Yes', 'mp') ?></label>
  5393. <label><input value="0" name="mp[show_filters]" type="radio"<?php checked($this->get_setting('show_filters'), 0) ?> /> <?php _e('No', 'mp') ?></label>
  5394. <br />
  5395. <span class="description"><?php _e('Show "Product Category" and "Order By" filters at the top of listings pages. Uses AJAX for instant updates based on user selection.', 'mp') ?></span>
  5396. </td>
  5397. </tr>
  5398. </table>
  5399. </div>
  5400. </div>
  5401. <div class="postbox">
  5402. <h3 class='hndle'><span><?php _e('Store URL Slugs', 'mp') ?></span> - <span class="description"><?php _e('Customizes the url structure of your store', 'mp') ?></span></h3>
  5403. <div class="inside">
  5404. <table class="form-table">
  5405. <tr valign="top">
  5406. <th scope="row"><?php _e('Store Base', 'mp') ?></th>
  5407. <td>/<input type="text" name="mp[slugs][store]" value="<?php echo esc_attr($this->get_setting('slugs->store')); ?>" size="20" maxlength="50" />/<br />
  5408. <span class="description"><?php _e('This page will be created so you can change it\'s content and the order in which it appears in navigation menus if your theme supports it.', 'mp') ?></span></td>
  5409. </tr>
  5410. <tr valign="top">
  5411. <th scope="row"><?php _e('Products List', 'mp') ?></th>
  5412. <td>/<?php echo esc_attr($this->get_setting('slugs->store')); ?>/<input type="text" name="mp[slugs][products]" value="<?php echo esc_attr($this->get_setting('slugs->products')); ?>" size="20" maxlength="50" />/</td>
  5413. </tr>
  5414. <tr valign="top">
  5415. <th scope="row"><?php _e('Shopping Cart Page', 'mp') ?></th>
  5416. <td>/<?php echo esc_attr($this->get_setting('slugs->store')); ?>/<input type="text" name="mp[slugs][cart]" value="<?php echo esc_attr($this->get_setting('slugs->cart')); ?>" size="20" maxlength="50" />/</td>
  5417. </tr>
  5418. <tr valign="top">
  5419. <th scope="row"><?php _e('Order Status Page', 'mp') ?></th>
  5420. <td>/<?php echo esc_attr($this->get_setting('slugs->store')); ?>/<input type="text" name="mp[slugs][orderstatus]" value="<?php echo esc_attr($this->get_setting('slugs->orderstatus')); ?>" size="20" maxlength="50" />/</td>
  5421. </tr>
  5422. <tr valign="top">
  5423. <th scope="row"><?php _e('Product Category', 'mp') ?></th>
  5424. <td>/<?php echo esc_attr($this->get_setting('slugs->store')); ?>/<?php echo esc_attr($this->get_setting('slugs->products')); ?>/<input type="text" name="mp[slugs][category]" value="<?php echo esc_attr($this->get_setting('slugs->category')); ?>" size="20" maxlength="50" />/</td>
  5425. </tr>
  5426. <tr valign="top">
  5427. <th scope="row"><?php _e('Product Tag', 'mp') ?></th>
  5428. <td>/<?php echo esc_attr($this->get_setting('slugs->store')); ?>/<?php echo esc_attr($this->get_setting('slugs->products')); ?>/<input type="text" name="mp[slugs][tag]" value="<?php echo esc_attr($this->get_setting('slugs->tag')); ?>" size="20" maxlength="50" />/</td>
  5429. </tr>
  5430. </table>
  5431. </div>
  5432. </div>
  5433. <?php do_action('mp_presentation_settings'); ?>
  5434. <p class="submit">
  5435. <input class="button-primary" type="submit" name="submit_settings" value="<?php _e('Save Changes', 'mp') ?>" />
  5436. </p>
  5437. </form>
  5438. </div>
  5439. <?php
  5440. break;
  5441. //---------------------------------------------------//
  5442. case "messages":
  5443. //save settings
  5444. if (isset($_POST['messages_settings'])) {
  5445. //remove html from emails
  5446. $_POST['mp']['email'] = array_map('wp_filter_nohtml_kses', (array)$_POST['mp']['email']);
  5447. //filter msg inputs if necessary
  5448. if (!current_user_can('unfiltered_html')) {
  5449. $_POST['mp']['msg'] = array_map('wp_kses_post', (array)$_POST['mp']['msg']);
  5450. }
  5451. //strip slashes
  5452. $_POST['mp']['msg'] = array_map('stripslashes', (array)$_POST['mp']['msg']);
  5453. $_POST['mp']['email'] = array_map('stripslashes', (array)$_POST['mp']['email']);
  5454. $settings = array_merge($settings, apply_filters('mp_messages_settings_filter', $_POST['mp']));
  5455. update_option('mp_settings', $settings);
  5456. echo '<div class="updated fade"><p>'.__('Settings saved.', 'mp').'</p></div>';
  5457. }
  5458. ?>
  5459. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/messages.png'; ?>" /></div>
  5460. <h2><?php _e('Messages Settings', 'mp'); ?></h2>
  5461. <div id="poststuff" class="metabox-holder mp-settings">
  5462. <form id="mp-messages-form" method="post" action="edit.php?post_type=product&amp;page=marketpress&amp;tab=messages">
  5463. <input type="hidden" name="messages_settings" value="1" />
  5464. <div class="postbox">
  5465. <h3 class='hndle'><span><?php _e('Email Notifications', 'mp') ?></span></h3>
  5466. <div class="inside">
  5467. <table class="form-table">
  5468. <tr>
  5469. <th scope="row"><?php _e('Store Admin Email', 'mp'); ?></th>
  5470. <td>
  5471. <?php $store_email = $this->get_setting('store_email') ? $this->get_setting('store_email') : get_option("admin_email"); ?>
  5472. <span class="description"><?php _e('The email address that new order notifications are sent to and received from.', 'mp') ?></span><br />
  5473. <input type="text" name="mp[store_email]" value="<?php echo esc_attr($store_email); ?>" maxlength="150" size="50" />
  5474. </td>
  5475. </tr>
  5476. <tr>
  5477. <th scope="row"><?php _e('New Order', 'mp'); ?></th>
  5478. <td>
  5479. <span class="description"><?php _e('The email text sent to your customer to confirm a new order. These codes will be replaced with order details: CUSTOMERNAME, ORDERID, ORDERINFO, SHIPPINGINFO, PAYMENTINFO, TOTAL, TRACKINGURL, ORDERNOTES. No HTML allowed.', 'mp') ?></span><br />
  5480. <label><?php _e('Subject:', 'mp'); ?><br />
  5481. <input type="text" class="mp_emails_sub" name="mp[email][new_order_subject]" value="<?php echo esc_attr($this->get_setting('email->new_order_subject')); ?>" maxlength="150" /></label><br />
  5482. <label><?php _e('Text:', 'mp'); ?><br />
  5483. <textarea class="mp_emails_txt" name="mp[email][new_order_txt]"><?php echo esc_textarea($this->get_setting('email->new_order_txt')); ?></textarea>
  5484. </label>
  5485. </td>
  5486. </tr>
  5487. <tr>
  5488. <th scope="row"><?php _e('Order Shipped', 'mp'); ?></th>
  5489. <td>
  5490. <span class="description"><?php _e('The email text sent to your customer when you mark an order as "Shipped". These codes will be replaced with order details: CUSTOMERNAME, ORDERID, ORDERINFO, SHIPPINGINFO, PAYMENTINFO, TOTAL, TRACKINGURL, ORDERNOTES. No HTML allowed.', 'mp') ?></span><br />
  5491. <label><?php _e('Subject:', 'mp'); ?><br />
  5492. <input type="text" class="mp_emails_sub" name="mp[email][shipped_order_subject]" value="<?php echo esc_attr($this->get_setting('email->shipped_order_subject')); ?>" maxlength="150" /></label><br />
  5493. <label><?php _e('Text:', 'mp'); ?><br />
  5494. <textarea class="mp_emails_txt" name="mp[email][shipped_order_txt]"><?php echo esc_textarea($this->get_setting('email->shipped_order_txt')); ?></textarea>
  5495. </label>
  5496. </td>
  5497. </tr>
  5498. </table>
  5499. </div>
  5500. </div>
  5501. <div class="postbox mp-pages-msgs">
  5502. <h3 class='hndle'><span><?php _e('Store Pages', 'mp') ?></span></h3>
  5503. <div class="inside">
  5504. <table class="form-table">
  5505. <tr>
  5506. <th scope="row"><?php _e('Store Page', 'mp'); ?></th>
  5507. <td>
  5508. <span class="description"><?php _e('The main store page is an actual page on your site. You can edit it here:', 'mp') ?></span>
  5509. <?php
  5510. $post_id = get_option('mp_store_page');
  5511. edit_post_link(__('Edit Page &raquo;', 'mp'), '', '', $post_id);
  5512. ?>
  5513. </td>
  5514. </tr>
  5515. <tr>
  5516. <th scope="row"><?php _e('Product Listing Pages', 'mp'); ?></th>
  5517. <td>
  5518. <span class="description"><?php _e('Displayed at the top of the product listing pages. Optional, HTML allowed.', 'mp') ?></span><br />
  5519. <?php wp_editor( $this->get_setting('msg->product_list'), 'product_list', array('textarea_name'=>'mp[msg][product_list]') ); ?>
  5520. </td>
  5521. </tr>
  5522. <tr>
  5523. <th scope="row"><?php _e('Order Status Page', 'mp'); ?></th>
  5524. <td>
  5525. <span class="description"><?php _e('Displayed at the top of the Order Status page. Optional, HTML allowed.', 'mp') ?></span><br />
  5526. <?php wp_editor( $this->get_setting('msg->order_status'), 'order_status', array('textarea_name'=>'mp[msg][order_status]') ); ?>
  5527. </td>
  5528. </tr>
  5529. </table>
  5530. </div>
  5531. </div>
  5532. <div class="postbox mp-pages-msgs">
  5533. <h3 class='hndle'><span><?php _e('Shopping Cart Pages', 'mp') ?></span></h3>
  5534. <div class="inside">
  5535. <table class="form-table">
  5536. <tr>
  5537. <th scope="row"><?php _e('Shopping Cart Page', 'mp'); ?></th>
  5538. <td>
  5539. <span class="description"><?php _e('Displayed at the top of the Shopping Cart page. Optional, HTML allowed.', 'mp') ?></span><br />
  5540. <?php wp_editor( $this->get_setting('msg->cart'), 'cart', array('textarea_name'=>'mp[msg][cart]') ); ?>
  5541. </td>
  5542. </tr>
  5543. <tr id="mp_msgs_shipping">
  5544. <th scope="row"><?php _e('Shipping Form Page', 'mp'); ?></th>
  5545. <td>
  5546. <span class="description"><?php _e('Displayed at the top of the Shipping Form page. Optional, HTML allowed.', 'mp') ?></span><br />
  5547. <?php wp_editor( $this->get_setting('msg->shipping'), 'shipping', array('textarea_name'=>'mp[msg][shipping]') ); ?>
  5548. </td>
  5549. </tr>
  5550. <tr>
  5551. <th scope="row"><?php _e('Payment Form Page', 'mp'); ?></th>
  5552. <td>
  5553. <span class="description"><?php _e('Displayed at the top of the Payment Form page. Optional, HTML allowed.', 'mp') ?></span><br />
  5554. <?php wp_editor( $this->get_setting('msg->checkout'), 'checkout', array('textarea_name'=>'mp[msg][checkout]') ); ?>
  5555. </td>
  5556. </tr>
  5557. <tr>
  5558. <th scope="row"><?php _e('Order Confirmation Page', 'mp'); ?></th>
  5559. <td>
  5560. <span class="description"><?php _e('Displayed at the top of the final Order Confirmation page. HTML allowed.', 'mp') ?></span><br />
  5561. <?php wp_editor( $this->get_setting('msg->confirm_checkout'), 'confirm_checkout', array('textarea_name'=>'mp[msg][confirm_checkout]') ); ?>
  5562. </td>
  5563. </tr>
  5564. <tr>
  5565. <th scope="row"><?php _e('Order Complete Page', 'mp'); ?></th>
  5566. <td>
  5567. <span class="description"><?php _e('Displayed at the top of the page notifying customers of a successful order. HTML allowed.', 'mp') ?></span><br />
  5568. <?php wp_editor( $this->get_setting('msg->success'), 'success', array('textarea_name'=>'mp[msg][success]') ); ?>
  5569. </td>
  5570. </tr>
  5571. </table>
  5572. </div>
  5573. </div>
  5574. <?php
  5575. //for adding additional messages
  5576. do_action('mp_messages_settings', $settings);
  5577. ?>
  5578. <p class="submit">
  5579. <input class="button-primary" type="submit" name="submit_settings" value="<?php _e('Save Changes', 'mp') ?>" />
  5580. </p>
  5581. </form>
  5582. </div>
  5583. <?php
  5584. break;
  5585. //---------------------------------------------------//
  5586. case "shipping":
  5587. global $mp_shipping_plugins;
  5588. //save settings from screen. Put here to be before plugin is loaded
  5589. if (isset($_POST['shipping_settings'])) {
  5590. $settings = get_option('mp_settings');
  5591. //allow plugins to verify settings before saving
  5592. $settings = array_merge($settings, apply_filters('mp_shipping_settings_filter', $_POST['mp']));
  5593. update_option('mp_settings', $settings);
  5594. echo '<div class="updated fade"><p>'.__('Settings saved.', 'mp').'</p></div>';
  5595. }
  5596. ?>
  5597. <script type="text/javascript">
  5598. jQuery(document).ready(function ($) {
  5599. $("#mp-select-all").click(function() {
  5600. $("#mp-target-countries input[type='checkbox']").attr('checked', true);
  5601. return false;
  5602. });
  5603. $("#mp-select-eu").click(function() {
  5604. $("#mp-target-countries input[type='checkbox'].eu").attr('checked', true);
  5605. return false;
  5606. });
  5607. $("#mp-select-none").click(function() {
  5608. $("#mp-target-countries input[type='checkbox']").attr('checked', false);
  5609. return false;
  5610. });
  5611. $(".mp-shipping-method").change(function() {
  5612. $("#mp-shipping-form").submit();
  5613. });
  5614. });
  5615. </script>
  5616. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/delivery.png'; ?>" /></div>
  5617. <h2><?php _e('Shipping Settings', 'mp'); ?></h2>
  5618. <div id="poststuff" class="metabox-holder mp-settings">
  5619. <form id="mp-shipping-form" method="post" action="edit.php?post_type=product&amp;page=marketpress&amp;tab=shipping">
  5620. <input type="hidden" name="shipping_settings" value="1" />
  5621. <div id="mp_flat_rate" class="postbox">
  5622. <h3 class='hndle'><span><?php _e('General Settings', 'mp') ?></span></h3>
  5623. <div class="inside">
  5624. <table class="form-table">
  5625. <tr>
  5626. <th scope="row"><?php _e('Choose Target Countries', 'mp') ?></th>
  5627. <td>
  5628. <div><?php _e('Select:', 'mp') ?> <a id="mp-select-all" href="#"><?php _e('All', 'mp') ?></a>&nbsp; <a id="mp-select-eu" href="#"><?php _e('EU', 'mp') ?></a>&nbsp; <a id="mp-select-none" href="#"><?php _e('None', 'mp') ?></a></div>
  5629. <div id="mp-target-countries">
  5630. <?php
  5631. foreach ($this->countries as $code => $name) {
  5632. ?><label><input type="checkbox"<?php echo (in_array($code, $this->eu_countries)) ? ' class="eu"' : ''; ?> name="mp[shipping][allowed_countries][]" value="<?php echo $code; ?>"<?php echo (in_array($code, $this->get_setting('shipping->allowed_countries', array()))) ? ' checked="checked"' : ''; ?> /> <?php echo esc_attr($name); ?></label><br /><?php
  5633. }
  5634. ?>
  5635. </div><br />
  5636. <span class="description"><?php _e('These are the countries you will sell and ship to.', 'mp') ?></span>
  5637. </td>
  5638. </tr>
  5639. <tr>
  5640. <th scope="row"><?php _e('Select Shipping Method', 'mp') ?></th>
  5641. <td>
  5642. <select name="mp[shipping][method]" class="mp-shipping-method">
  5643. <option value="none"<?php selected($this->get_setting('shipping->method'), 'none'); ?>><?php _e('No Shipping', 'mp'); ?></option>
  5644. <?php
  5645. $calculated_methods = 0;
  5646. foreach ((array)$mp_shipping_plugins as $code => $plugin) {
  5647. if ($plugin[2]) {
  5648. $calculated_methods++;
  5649. continue;
  5650. }
  5651. ?><option value="<?php echo $code; ?>"<?php selected($this->get_setting('shipping->method'), $code); ?>><?php echo esc_attr($plugin[1]); ?></option><?php
  5652. }
  5653. if ($calculated_methods) {
  5654. ?><option value="calculated"<?php selected($this->get_setting('shipping->method'), 'calculated'); ?>><?php _e('Calculated Options', 'mp'); ?></option><?php
  5655. }
  5656. ?>
  5657. </select>
  5658. </td>
  5659. </tr>
  5660. <?php
  5661. if ($calculated_methods && $this->get_setting('shipping->method') == 'calculated') {
  5662. ?>
  5663. <tr>
  5664. <th scope="row"><?php _e('Select Shipping Options', 'mp') ?></th>
  5665. <td>
  5666. <span class="description"><?php _e('Select which calculated shipping methods the customer will be able to choose from:', 'mp') ?></span><br />
  5667. <?php
  5668. foreach ((array)$mp_shipping_plugins as $code => $plugin) {
  5669. if (!$plugin[2]) continue; //skip non calculated
  5670. ?><label><input type="checkbox" class="mp-shipping-method" name="mp[shipping][calc_methods][<?php echo $code; ?>]" value="<?php echo $code; ?>"<?php echo $this->get_setting("shipping->calc_methods->$code") ? ' checked="checked"' : ''; ?> /> <?php echo esc_attr($plugin[1]); ?></label><br /><?php
  5671. }
  5672. ?>
  5673. </td>
  5674. </tr>
  5675. <?php
  5676. }
  5677. ?>
  5678. <tr>
  5679. <th scope="row"><?php _e('Measurement System', 'mp') ?></th>
  5680. <td>
  5681. <label><input value="english" name="mp[shipping][system]" type="radio"<?php checked($this->get_setting('shipping->system'), 'english') ?> /> <?php _e('Engish (Pounds)', 'mp') ?></label>
  5682. <label><input value="metric" name="mp[shipping][system]" type="radio"<?php checked($this->get_setting('shipping->system'), 'metric') ?> /> <?php _e('Metric (Kilograms)', 'mp') ?></label>
  5683. </td>
  5684. </tr>
  5685. </table>
  5686. </div>
  5687. </div>
  5688. <?php
  5689. //for adding additional settings for a shipping module
  5690. do_action('mp_shipping_settings', $settings);
  5691. ?>
  5692. <p class="submit">
  5693. <input class="button-primary" type="submit" name="submit_settings" value="<?php _e('Save Changes', 'mp') ?>" />
  5694. </p>
  5695. </form>
  5696. </div>
  5697. <?php
  5698. break;
  5699. //---------------------------------------------------//
  5700. case "gateways":
  5701. global $mp_gateway_plugins;
  5702. //save settings
  5703. if (isset($_POST['gateway_settings'])) {
  5704. if ( isset( $_POST['mp'] ) ) {
  5705. $filtered_settings = apply_filters('mp_gateway_settings_filter', $_POST['mp']);
  5706. //allow plugins to verify settings before saving
  5707. $settings = array_merge($settings, $filtered_settings);
  5708. update_option('mp_settings', $settings);
  5709. }
  5710. echo '<div class="updated fade"><p>'.__('Settings saved.', 'mp').'</p></div>';
  5711. }
  5712. ?>
  5713. <script type="text/javascript">
  5714. jQuery(document).ready(function ($) {
  5715. $("input.mp_allowed_gateways").change(function() {
  5716. $("#mp-gateways-form").submit();
  5717. });
  5718. });
  5719. </script>
  5720. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/credit-cards.png'; ?>" /></div>
  5721. <h2><?php _e('Payment Settings', 'mp'); ?></h2>
  5722. <div id="poststuff" class="metabox-holder mp-settings">
  5723. <form id="mp-gateways-form" method="post" action="edit.php?post_type=product&amp;page=marketpress&amp;tab=gateways">
  5724. <input type="hidden" name="gateway_settings" value="1" />
  5725. <?php if (!$this->global_cart) { ?>
  5726. <div id="mp_gateways" class="postbox">
  5727. <h3 class='hndle'><span><?php _e('General Settings', 'mp') ?></span></h3>
  5728. <div class="inside">
  5729. <table class="form-table">
  5730. <tr>
  5731. <th scope="row"><?php _e('Select Payment Gateway(s)', 'mp') ?></th>
  5732. <td>
  5733. <?php
  5734. //check network permissions
  5735. if (is_multisite() && !is_main_site()) {
  5736. $network_settings = get_site_option( 'mp_network_settings' );
  5737. foreach ((array)$mp_gateway_plugins as $code => $plugin) {
  5738. if ($network_settings['allowed_gateways'][$code] == 'full') {
  5739. $allowed_plugins[$code] = $plugin;
  5740. } else if ($network_settings['allowed_gateways'][$code] == 'supporter' && function_exists('is_pro_site') && is_pro_site(false, $network_settings['gateways_pro_level'][$code]) ) {
  5741. $allowed_plugins[$code] = $plugin;
  5742. }
  5743. }
  5744. $mp_gateway_plugins = $allowed_plugins;
  5745. }
  5746. foreach ((array)$mp_gateway_plugins as $code => $plugin) {
  5747. if ($plugin[3]) { //if demo
  5748. ?><label><input type="checkbox" class="mp_allowed_gateways" name="mp[gateways][allowed][]" value="<?php echo $code; ?>" disabled="disabled" /> <?php echo esc_attr($plugin[1]); ?></label> <a class="mp-pro-update" href="http://premium.wpmudev.org/project/e-commerce" title="<?php _e('Upgrade', 'mp'); ?> &raquo;"><?php _e('Pro Only &raquo;', 'mp'); ?></a><br /><?php
  5749. } else {
  5750. ?><label><input type="checkbox" class="mp_allowed_gateways" name="mp[gateways][allowed][]" value="<?php echo $code; ?>"<?php echo (in_array($code, $this->get_setting('gateways->allowed', array()))) ? ' checked="checked"' : ''; ?> /> <?php echo esc_attr($plugin[1]); ?></label><br /><?php
  5751. }
  5752. }
  5753. ?>
  5754. </td>
  5755. </tr>
  5756. </table>
  5757. </div>
  5758. </div>
  5759. <?php } ?>
  5760. <?php
  5761. //for adding additional settings for a payment gateway plugin
  5762. do_action('mp_gateway_settings', $settings);
  5763. ?>
  5764. <p class="submit">
  5765. <input class="button-primary" type="submit" name="submit_settings" value="<?php _e('Save Changes', 'mp') ?>" />
  5766. </p>
  5767. </form>
  5768. </div>
  5769. <?php
  5770. break;
  5771. //---------------------------------------------------//
  5772. case "shortcodes":
  5773. ?>
  5774. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/help.png'; ?>" /></div>
  5775. <h2><?php _e('MarketPress Shortcodes', 'mp'); ?></h2>
  5776. <div id="poststuff" class="metabox-holder mp-settings">
  5777. <!--
  5778. <div class="postbox">
  5779. <h3 class='hndle'><span><?php _e('General Information', 'mp') ?></span></h3>
  5780. <div class="inside">
  5781. <iframe src="http://premium.wpmudev.org/wdp-un.php?action=help&id=144" width="100%" height="400px"></iframe>
  5782. </div>
  5783. </div>
  5784. -->
  5785. <div class="postbox">
  5786. <h3 class='hndle'><span><?php _e('Shortcodes', 'mp') ?></span></h3>
  5787. <div class="inside">
  5788. <p><?php _e('Shortcodes allow you to include dynamic store content in posts and pages on your site. Simply type or paste them into your post or page content where you would like them to appear. Optional attributes can be added in a format like <em>[shortcode attr1="value" attr2="value"]</em>.', 'mp') ?></p>
  5789. <table class="form-table">
  5790. <tr>
  5791. <th scope="row"><?php _e('Product Tag Cloud', 'mp') ?></th>
  5792. <td>
  5793. <strong>[mp_tag_cloud]</strong> -
  5794. <span class="description"><?php _e('Displays a cloud or list of your product tags.', 'mp') ?></span>
  5795. <a href="http://codex.wordpress.org/Template_Tags/wp_tag_cloud"><?php _e('Optional Attributes &raquo;', 'mp') ?></a>
  5796. </td>
  5797. </tr>
  5798. <tr>
  5799. <th scope="row"><?php _e('Product Categories List', 'mp') ?></th>
  5800. <td>
  5801. <strong>[mp_list_categories]</strong> -
  5802. <span class="description"><?php _e('Displays an HTML list of your product categories.', 'mp') ?></span>
  5803. <a href="http://codex.wordpress.org/Template_Tags/wp_list_categories"><?php _e('Optional Attributes &raquo;', 'mp') ?></a>
  5804. </td>
  5805. </tr>
  5806. <tr>
  5807. <th scope="row"><?php _e('Product Categories Dropdown', 'mp') ?></th>
  5808. <td>
  5809. <strong>[mp_dropdown_categories]</strong> -
  5810. <span class="description"><?php _e('Displays an HTML dropdown of your product categories.', 'mp') ?></span>
  5811. <a href="http://codex.wordpress.org/Template_Tags/wp_dropdown_categories"><?php _e('Optional Attributes &raquo;', 'mp') ?></a>
  5812. </td>
  5813. </tr>
  5814. <tr>
  5815. <th scope="row"><?php _e('Popular Products List', 'mp') ?></th>
  5816. <td>
  5817. <strong>[mp_popular_products]</strong> -
  5818. <span class="description"><?php _e('Displays a list of popular products ordered by sales.', 'mp') ?></span>
  5819. <p>
  5820. <strong><?php _e('Optional Attributes:', 'mp') ?></strong>
  5821. <ul class="mp-shortcode-options">
  5822. <li><?php _e('"number" - max number of products to display. Defaults to 5.', 'mp') ?></li>
  5823. <li><?php _e('Example:', 'mp') ?> <em>[mp_popular_products number="5"]</em></li>
  5824. </ul></p>
  5825. </td>
  5826. </tr>
  5827. <tr>
  5828. <th scope="row"><?php _e('Products List', 'mp') ?></th>
  5829. <td>
  5830. <strong>[mp_list_products]</strong> -
  5831. <span class="description"><?php _e('Displays a list of products according to preference. Optional attributes default to the values in Presentation Settings -> Product List.', 'mp') ?></span>
  5832. <p>
  5833. <strong><?php _e('Optional Attributes:', 'mp') ?></strong>
  5834. <ul class="mp-shortcode-options">
  5835. <li><?php _e('"paginate" - Whether to paginate the product list. This is useful to only show a subset.', 'mp') ?></li>
  5836. <li><?php _e('"page" - The page number to display in the product list if "paginate" is set to true.', 'mp') ?></li>
  5837. <li><?php _e('"per_page" - How many products to display in the product list if "paginate" is set to true.', 'mp') ?></li>
  5838. <li><?php _e('"order_by" - What field to order products by. Can be: title, date, ID, author, price, sales, rand (random).', 'mp') ?></li>
  5839. <li><?php _e('"order" - Direction to order products by. Can be: DESC, ASC', 'mp') ?></li>
  5840. <li><?php _e('"category" - Limits list to a specific product category. Use the category Slug', 'mp') ?></li>
  5841. <li><?php _e('"tag" - Limits list to a specific product tag. Use the tag Slug', 'mp') ?></li>
  5842. <li><?php _e('Example:', 'mp') ?> <em>[mp_list_products paginate="true" page="1" per_page="10" order_by="price" order="DESC" category="downloads"]</em></li>
  5843. </ul></p>
  5844. </td>
  5845. </tr>
  5846. <tr>
  5847. <th scope="row"><?php _e('Single Product', 'mp') ?></th>
  5848. <td>
  5849. <strong>[mp_product]</strong> -
  5850. <span class="description"><?php _e('Displays a single product according to preference.', 'mp') ?></span>
  5851. <p>
  5852. <strong><?php _e('Optional Attributes:', 'mp') ?></strong>
  5853. <ul class="mp-shortcode-options">
  5854. <li><?php _e('"product_id" - The ID of the product to display. This is the Post ID, you can find it in the url of a product edit page.', 'mp') ?></li>
  5855. <li><?php _e('"title" - Whether to display the product title.', 'mp') ?></li>
  5856. <li><?php _e('"content" - Whether and what type of content to display. Options are false/0, "full", or "excerpt". Default "full"', 'mp') ?></li>
  5857. <li><?php _e('"image" - Whether and what context of image size to display. Options are false/0, "single", or "list". Default "single"', 'mp') ?></li>
  5858. <li><?php _e('"meta" - Whether to display the product meta (price, buy button).', 'mp') ?></li>
  5859. <li><?php _e('Example:', 'mp') ?> <em>[mp_product product_id="1" title="1" content="excerpt" image="single" meta="1"]</em></li>
  5860. </ul></p>
  5861. </td>
  5862. </tr>
  5863. <tr>
  5864. <th scope="row"><?php _e('Product Image', 'mp') ?></th>
  5865. <td>
  5866. <strong>[mp_product_image]</strong> -
  5867. <span class="description"><?php _e('Displays the featured image of a given product.', 'mp') ?></span>
  5868. <p>
  5869. <strong><?php _e('Optional Attributes:', 'mp') ?></strong>
  5870. <ul class="mp-shortcode-options">
  5871. <li><?php _e('"product_id" - The ID for the product. This is the Post ID, you can find it in the url of a product edit page. Optional if shortcode is in the loop.', 'mp') ?></li>
  5872. <li><?php _e('"context" - What context for preset size options. Options are list, single, or widget, default single.', 'mp') ?></li>
  5873. <li><?php _e('"size" - Set a custom pixel width/height. If omitted defaults to the size set by "context".', 'mp') ?></li>
  5874. <li><?php _e('Example:', 'mp') ?> <em>[mp_product_image product_id="1" size="150"]</em></li>
  5875. </ul></p>
  5876. </td>
  5877. </tr>
  5878. <tr>
  5879. <th scope="row"><?php _e('Product Buy Button', 'mp') ?></th>
  5880. <td>
  5881. <strong>[mp_buy_button]</strong> -
  5882. <span class="description"><?php _e('Displays the buy or add to cart button.', 'mp') ?></span>
  5883. <p>
  5884. <strong><?php _e('Optional Attributes:', 'mp') ?></strong>
  5885. <ul class="mp-shortcode-options">
  5886. <li><?php _e('"product_id" - The ID for the product. This is the Post ID, you can find it in the url of a product edit page. Optional if shortcode is in the loop.', 'mp') ?></li>
  5887. <li><?php _e('"context" - What context for display. Options are list or single, default single which shows all variations.', 'mp') ?></li>
  5888. <li><?php _e('Example:', 'mp') ?> <em>[mp_buy_button product_id="1" context="single"]</em></li>
  5889. </ul></p>
  5890. </td>
  5891. </tr>
  5892. <tr>
  5893. <th scope="row"><?php _e('Product Price', 'mp') ?></th>
  5894. <td>
  5895. <strong>[mp_product_price]</strong> -
  5896. <span class="description"><?php _e('Displays the product price (and sale price).', 'mp') ?></span>
  5897. <p>
  5898. <strong><?php _e('Optional Attributes:', 'mp') ?></strong>
  5899. <ul class="mp-shortcode-options">
  5900. <li><?php _e('"product_id" - The ID for the product. This is the Post ID, you can find it in the url of a product edit page. Optional if shortcode is in the loop.', 'mp') ?></li>
  5901. <li><?php _e('"label" - A label to prepend to the price. Defaults to "Price: ".', 'mp') ?></li>
  5902. <li><?php _e('Example:', 'mp') ?> <em>[mp_product_price product_id="1" label="Buy this thing now!"]</em></li>
  5903. </ul></p>
  5904. </td>
  5905. </tr>
  5906. <tr>
  5907. <th scope="row"><?php _e('Product Meta', 'mp') ?></th>
  5908. <td>
  5909. <strong>[mp_product_meta]</strong> -
  5910. <span class="description"><?php _e('Displays the full product meta box with price and buy now/add to cart button.', 'mp') ?></span>
  5911. <p>
  5912. <strong><?php _e('Optional Attributes:', 'mp') ?></strong>
  5913. <ul class="mp-shortcode-options">
  5914. <li><?php _e('"product_id" - The ID for the product. This is the Post ID, you can find it in the url of a product edit page. Optional if shortcode is in the loop.', 'mp') ?></li>
  5915. <li><?php _e('"label" - A label to prepend to the price. Defaults to "Price: ".', 'mp') ?></li>
  5916. <li><?php _e('"context" - What context for display. Options are list or single, default single which shows all variations.', 'mp') ?></li>
  5917. <li><?php _e('Example:', 'mp') ?> <em>[mp_product_meta product_id="1" label="Buy this thing now!"]</em></li>
  5918. </ul></p>
  5919. </td>
  5920. </tr>
  5921. <tr>
  5922. <th scope="row"><?php _e('Store Links', 'mp') ?></th>
  5923. <td>
  5924. <strong>[mp_cart_link]</strong> -
  5925. <span class="description"><?php _e('Displays a link or url to the current shopping cart page.', 'mp') ?></span><br />
  5926. <strong>[mp_store_link]</strong> -
  5927. <span class="description"><?php _e('Displays a link or url to the current store page.', 'mp') ?></span><br />
  5928. <strong>[mp_products_link]</strong> -
  5929. <span class="description"><?php _e('Displays a link or url to the current products list page.', 'mp') ?></span><br />
  5930. <strong>[mp_orderstatus_link]</strong> -
  5931. <span class="description"><?php _e('Displays a link or url to the order status page.', 'mp') ?></span><br />
  5932. <p>
  5933. <strong><?php _e('Optional Attributes:', 'mp') ?></strong>
  5934. <ul class="mp-shortcode-options">
  5935. <li><?php _e('"url" - Whether to return a clickable link or url. Can be: true, false. Defaults to showing link.', 'mp') ?></li>
  5936. <li><?php _e('"link_text" - The text to show in the link.', 'mp') ?></li>
  5937. <li><?php _e('Example:', 'mp') ?> <em>[mp_cart_link link_text="Go here!"]</em></li>
  5938. </ul></p>
  5939. </td>
  5940. </tr>
  5941. <tr>
  5942. <th scope="row"><?php _e('Store Navigation List', 'mp') ?></th>
  5943. <td>
  5944. <strong>[mp_store_navigation]</strong> -
  5945. <span class="description"><?php _e('Displays a list of links to your store pages.', 'mp') ?></span>
  5946. </td>
  5947. </tr>
  5948. </table>
  5949. </div>
  5950. </div>
  5951. <?php
  5952. //for adding additional help content boxes
  5953. do_action('mp_help_page', $settings);
  5954. ?>
  5955. </div>
  5956. <?php
  5957. break;
  5958. //---------------------------------------------------//
  5959. case "importers":
  5960. ?>
  5961. <div class="icon32"><img src="<?php echo $this->plugin_url . 'images/import.png'; ?>" /></div>
  5962. <form id="mp-import-form" method="post" action="" enctype="multipart/form-data">
  5963. <h2><?php _e('Import Products', 'mp'); ?></h2>
  5964. <div id="poststuff" class="metabox-holder mp-importer">
  5965. <?php do_action('marketpress_add_importer'); ?>
  5966. </div>
  5967. </form>
  5968. </div>
  5969. <?php
  5970. break;
  5971. } //end switch
  5972. //hook to create a new admin screen.
  5973. do_action('marketpress_add_screen', $tab);
  5974. echo '</div>';
  5975. }
  5976. } //end class
  5977. global $mp;
  5978. $mp = new MarketPress();
  5979. //Shopping cart widget
  5980. class MarketPress_Shopping_Cart extends WP_Widget {
  5981. function MarketPress_Shopping_Cart() {
  5982. $widget_ops = array('classname' => 'mp_cart_widget', 'description' => __('Shows dynamic shopping cart contents along with a checkout button for your MarketPress store.', 'mp') );
  5983. $this->WP_Widget('mp_cart_widget', __('Shopping Cart', 'mp'), $widget_ops);
  5984. }
  5985. function widget($args, $instance) {
  5986. global $mp;
  5987. if ( get_query_var('pagename') == 'cart' )
  5988. return;
  5989. if ($instance['only_store_pages'] && !mp_is_shop_page())
  5990. return;
  5991. extract( $args );
  5992. echo $before_widget;
  5993. $title = $instance['title'];
  5994. if ( !empty( $title ) ) { echo $before_title . apply_filters('widget_title', $title) . $after_title; };
  5995. if ( !empty($instance['custom_text']) )
  5996. echo '<div class="custom_text">' . $instance['custom_text'] . '</div>';
  5997. echo '<div class="mp_cart_widget_content">';
  5998. mp_show_cart('widget');
  5999. echo '</div>';
  6000. echo $after_widget;
  6001. }
  6002. function update( $new_instance, $old_instance ) {
  6003. $instance = $old_instance;
  6004. $instance['title'] = stripslashes( wp_filter_nohtml_kses( $new_instance['title']) );
  6005. $instance['custom_text'] = stripslashes( wp_filter_kses( $new_instance['custom_text']) );
  6006. $instance['only_store_pages'] = !empty($new_instance['only_store_pages']) ? 1 : 0;
  6007. /*
  6008. $instance['show_thumbnail'] = !empty($new_instance['show_thumbnail']) ? 1 : 0;
  6009. $instance['size'] = !empty($new_instance['size']) ? intval($new_instance['size']) : 25;
  6010. */
  6011. return $instance;
  6012. }
  6013. function form( $instance ) {
  6014. $instance = wp_parse_args( (array) $instance, array( 'title' => __('Shopping Cart', 'mp'), 'custom_text' => '', 'only_store_pages' => 0 ) );
  6015. $title = $instance['title'];
  6016. $custom_text = $instance['custom_text'];
  6017. $only_store_pages = isset( $instance['only_store_pages'] ) ? (bool) $instance['only_store_pages'] : false;
  6018. /*
  6019. $show_thumbnail = isset( $instance['show_thumbnail'] ) ? (bool) $instance['show_thumbnail'] : false;
  6020. $size = !empty($instance['size']) ? intval($instance['size']) : 25;
  6021. */
  6022. ?>
  6023. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'mp') ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
  6024. <p><label for="<?php echo $this->get_field_id('custom_text'); ?>"><?php _e('Custom Text:', 'mp') ?><br />
  6025. <textarea class="widefat" id="<?php echo $this->get_field_id('custom_text'); ?>" name="<?php echo $this->get_field_name('custom_text'); ?>"><?php echo esc_attr($custom_text); ?></textarea></label>
  6026. </p>
  6027. <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('only_store_pages'); ?>" name="<?php echo $this->get_field_name('only_store_pages'); ?>"<?php checked( $only_store_pages ); ?> />
  6028. <label for="<?php echo $this->get_field_id('only_store_pages'); ?>"><?php _e( 'Only show on store pages', 'mp' ); ?></label></p>
  6029. <?php
  6030. /* Disable untill we can mod the cart
  6031. <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_thumbnail'); ?>" name="<?php echo $this->get_field_name('show_thumbnail'); ?>"<?php checked( $show_thumbnail ); ?> />
  6032. <label for="<?php echo $this->get_field_id('show_thumbnail'); ?>"><?php _e( 'Show Thumbnail', 'mp' ); ?></label><br />
  6033. <label for="<?php echo $this->get_field_id('size'); ?>"><?php _e('Thumbnail Size:', 'mp') ?> <input id="<?php echo $this->get_field_id('size'); ?>" name="<?php echo $this->get_field_name('size'); ?>" type="text" size="3" value="<?php echo $size; ?>" /></label></p>
  6034. */
  6035. }
  6036. }
  6037. //Product listing widget
  6038. class MarketPress_Product_List extends WP_Widget {
  6039. function MarketPress_Product_List() {
  6040. $widget_ops = array('classname' => 'mp_product_list_widget', 'description' => __('Shows a customizable list of products from your MarketPress store.', 'mp') );
  6041. $this->WP_Widget('mp_product_list_widget', __('Product List', 'mp'), $widget_ops);
  6042. }
  6043. function widget($args, $instance) {
  6044. global $mp;
  6045. if ($instance['only_store_pages'] && !mp_is_shop_page())
  6046. return;
  6047. extract( $args );
  6048. echo $before_widget;
  6049. $title = $instance['title'];
  6050. if ( !empty( $title ) ) { echo $before_title . apply_filters('widget_title', $title) . $after_title; };
  6051. if ( !empty($instance['custom_text']) )
  6052. echo '<div id="custom_text">' . $instance['custom_text'] . '</div>';
  6053. /* setup our custom query */
  6054. //setup taxonomy if applicable
  6055. if ($instance['taxonomy_type'] == 'category') {
  6056. $taxonomy_query = '&product_category=' . $instance['taxonomy'];
  6057. } else if ($instance['taxonomy_type'] == 'tag') {
  6058. $taxonomy_query = '&product_tag=' . $instance['taxonomy'];
  6059. } else {
  6060. $taxonomy_query = '';
  6061. }
  6062. //figure out perpage
  6063. if (isset($instance['num_products']) && intval($instance['num_products']) > 0) {
  6064. $paginate_query = '&posts_per_page='.intval($instance['num_products']).'&paged=1';
  6065. } else {
  6066. $paginate_query = '&posts_per_page=10&paged=1';
  6067. }
  6068. //get order by
  6069. if ($instance['order_by']) {
  6070. if ($instance['order_by'] == 'price')
  6071. $order_by_query = '&meta_key=mp_price&orderby=meta_value_num';
  6072. else if ($instance['order_by'] == 'sales')
  6073. $order_by_query = '&meta_key=mp_sales_count&orderby=meta_value_num';
  6074. else
  6075. $order_by_query = '&orderby='.$instance['order_by'];
  6076. } else {
  6077. $order_by_query = '&orderby=title';
  6078. }
  6079. //get order direction
  6080. if ($instance['order']) {
  6081. $order_query = '&order='.$instance['order'];
  6082. } else {
  6083. $order_query = '&orderby=DESC';
  6084. }
  6085. //The Query
  6086. $custom_query = new WP_Query('post_type=product' . $taxonomy_query . $paginate_query . $order_by_query . $order_query);
  6087. //do we have products?
  6088. if (count($custom_query->posts)) {
  6089. echo '<ul id="mp_product_list">';
  6090. foreach ($custom_query->posts as $post) {
  6091. echo '<li '.mp_product_class(false, 'mp_product', $post->ID).'>';
  6092. echo '<h3 class="mp_product_name"><a href="' . get_permalink( $post->ID ) . '">' . esc_attr($post->post_title) . '</a></h3>';
  6093. if ($instance['show_thumbnail'])
  6094. mp_product_image( true, 'widget', $post->ID, $instance['size'] );
  6095. if ($instance['show_excerpt'])
  6096. echo '<div class="mp_product_content">' . $mp->product_excerpt($post->post_excerpt, $post->post_content, $post->ID) . '</div>';
  6097. if ($instance['show_price'] || $instance['show_button']) {
  6098. echo '<div class="mp_product_meta">';
  6099. if ($instance['show_price'])
  6100. echo mp_product_price(false, $post->ID, '');
  6101. if ($instance['show_button'])
  6102. echo mp_buy_button(false, 'list', $post->ID);
  6103. echo '</div>';
  6104. }
  6105. echo '</li>';
  6106. }
  6107. echo '</ul>';
  6108. } else {
  6109. ?>
  6110. <div class="widget-error">
  6111. <?php _e('No Products', 'mp') ?>
  6112. </div>
  6113. <?php
  6114. }
  6115. echo $after_widget;
  6116. }
  6117. function update( $new_instance, $old_instance ) {
  6118. $instance = $old_instance;
  6119. $instance['title'] = stripslashes( wp_filter_nohtml_kses( $new_instance['title'] ) );
  6120. $instance['custom_text'] = stripslashes( wp_filter_kses( $new_instance['custom_text'] ) );
  6121. $instance['num_products'] = intval($new_instance['num_products']);
  6122. $instance['order_by'] = $new_instance['order_by'];
  6123. $instance['order'] = $new_instance['order'];
  6124. $instance['taxonomy_type'] = $new_instance['taxonomy_type'];
  6125. $instance['taxonomy'] = ($new_instance['taxonomy_type']) ? sanitize_title($new_instance['taxonomy']) : '';
  6126. $instance['show_thumbnail'] = !empty($new_instance['show_thumbnail']) ? 1 : 0;
  6127. $instance['size'] = !empty($new_instance['size']) ? intval($new_instance['size']) : 50;
  6128. $instance['show_excerpt'] = !empty($new_instance['show_excerpt']) ? 1 : 0;
  6129. $instance['show_price'] = !empty($new_instance['show_price']) ? 1 : 0;
  6130. $instance['show_button'] = !empty($new_instance['show_button']) ? 1 : 0;
  6131. $instance['only_store_pages'] = !empty($new_instance['only_store_pages']) ? 1 : 0;
  6132. return $instance;
  6133. }
  6134. function form( $instance ) {
  6135. $instance = wp_parse_args( (array) $instance, array( 'title' => __('Our Products', 'mp'), 'custom_text' => '', 'num_products' => 10, 'order_by' => 'title', 'order' => 'DESC', 'show_thumbnail' => 1, 'size' => 50, 'only_store_pages' => 0 ) );
  6136. $title = $instance['title'];
  6137. $custom_text = $instance['custom_text'];
  6138. $num_products = intval($instance['num_products']);
  6139. $order_by = $instance['order_by'];
  6140. $order = $instance['order'];
  6141. $taxonomy_type = $instance['taxonomy_type'];
  6142. $taxonomy = $instance['taxonomy'];
  6143. $show_thumbnail = isset( $instance['show_thumbnail'] ) ? (bool) $instance['show_thumbnail'] : false;
  6144. $size = !empty($instance['size']) ? intval($instance['size']) : 50;
  6145. $show_excerpt = isset( $instance['show_excerpt'] ) ? (bool) $instance['show_excerpt'] : false;
  6146. $show_price = isset( $instance['show_price'] ) ? (bool) $instance['show_price'] : false;
  6147. $show_button = isset( $instance['show_button'] ) ? (bool) $instance['show_button'] : false;
  6148. $only_store_pages = isset( $instance['only_store_pages'] ) ? (bool) $instance['only_store_pages'] : false;
  6149. ?>
  6150. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'mp') ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
  6151. <p><label for="<?php echo $this->get_field_id('custom_text'); ?>"><?php _e('Custom Text:', 'mp') ?><br />
  6152. <textarea class="widefat" id="<?php echo $this->get_field_id('custom_text'); ?>" name="<?php echo $this->get_field_name('custom_text'); ?>"><?php echo esc_attr($custom_text); ?></textarea></label>
  6153. </p>
  6154. <h3><?php _e('List Settings', 'mp'); ?></h3>
  6155. <p>
  6156. <label for="<?php echo $this->get_field_id('num_products'); ?>"><?php _e('Number of Products:', 'mp') ?> <input id="<?php echo $this->get_field_id('num_products'); ?>" name="<?php echo $this->get_field_name('num_products'); ?>" type="text" size="3" value="<?php echo $num_products; ?>" /></label><br />
  6157. </p>
  6158. <p>
  6159. <label for="<?php echo $this->get_field_id('order_by'); ?>"><?php _e('Order Products By:', 'mp') ?></label><br />
  6160. <select id="<?php echo $this->get_field_id('order_by'); ?>" name="<?php echo $this->get_field_name('order_by'); ?>">
  6161. <option value="title"<?php selected($order_by, 'title') ?>><?php _e('Product Name', 'mp') ?></option>
  6162. <option value="date"<?php selected($order_by, 'date') ?>><?php _e('Publish Date', 'mp') ?></option>
  6163. <option value="ID"<?php selected($order_by, 'ID') ?>><?php _e('Product ID', 'mp') ?></option>
  6164. <option value="author"<?php selected($order_by, 'author') ?>><?php _e('Product Author', 'mp') ?></option>
  6165. <option value="sales"<?php selected($order_by, 'sales') ?>><?php _e('Number of Sales', 'mp') ?></option>
  6166. <option value="price"<?php selected($order_by, 'price') ?>><?php _e('Product Price', 'mp') ?></option>
  6167. <option value="rand"<?php selected($order_by, 'rand') ?>><?php _e('Random', 'mp') ?></option>
  6168. </select><br />
  6169. <label><input value="DESC" name="<?php echo $this->get_field_name('order'); ?>" type="radio"<?php checked($order, 'DESC') ?> /> <?php _e('Descending', 'mp') ?></label>
  6170. <label><input value="ASC" name="<?php echo $this->get_field_name('order'); ?>" type="radio"<?php checked($order, 'ASC') ?> /> <?php _e('Ascending', 'mp') ?></label>
  6171. </p>
  6172. <p>
  6173. <label><?php _e('Taxonomy Filter:', 'mp') ?></label><br />
  6174. <select id="<?php echo $this->get_field_id('taxonomy_type'); ?>" name="<?php echo $this->get_field_name('taxonomy_type'); ?>">
  6175. <option value=""<?php selected($taxonomy_type, '') ?>><?php _e('No Filter', 'mp') ?></option>
  6176. <option value="category"<?php selected($taxonomy_type, 'category') ?>><?php _e('Category', 'mp') ?></option>
  6177. <option value="tag"<?php selected($taxonomy_type, 'tag') ?>><?php _e('Tag', 'mp') ?></option>
  6178. </select>
  6179. <input id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>" type="text" size="17" value="<?php echo $taxonomy; ?>" title="<?php _e('Enter the Slug', 'mp'); ?>" />
  6180. </p>
  6181. <h3><?php _e('Display Settings', 'mp'); ?></h3>
  6182. <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_thumbnail'); ?>" name="<?php echo $this->get_field_name('show_thumbnail'); ?>"<?php checked( $show_thumbnail ); ?> />
  6183. <label for="<?php echo $this->get_field_id('show_thumbnail'); ?>"><?php _e( 'Show Thumbnail', 'mp' ); ?></label><br />
  6184. <label for="<?php echo $this->get_field_id('size'); ?>"><?php _e('Thumbnail Size:', 'mp') ?> <input id="<?php echo $this->get_field_id('size'); ?>" name="<?php echo $this->get_field_name('size'); ?>" type="text" size="3" value="<?php echo $size; ?>" /></label></p>
  6185. <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_excerpt'); ?>" name="<?php echo $this->get_field_name('show_excerpt'); ?>"<?php checked( $show_excerpt ); ?> />
  6186. <label for="<?php echo $this->get_field_id('show_excerpt'); ?>"><?php _e( 'Show Excerpt', 'mp' ); ?></label><br />
  6187. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_price'); ?>" name="<?php echo $this->get_field_name('show_price'); ?>"<?php checked( $show_price ); ?> />
  6188. <label for="<?php echo $this->get_field_id('show_price'); ?>"><?php _e( 'Show Price', 'mp' ); ?></label><br />
  6189. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('show_button'); ?>" name="<?php echo $this->get_field_name('show_button'); ?>"<?php checked( $show_button ); ?> />
  6190. <label for="<?php echo $this->get_field_id('show_button'); ?>"><?php _e( 'Show Buy Button', 'mp' ); ?></label></p>
  6191. <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('only_store_pages'); ?>" name="<?php echo $this->get_field_name('only_store_pages'); ?>"<?php checked( $only_store_pages ); ?> />
  6192. <label for="<?php echo $this->get_field_id('only_store_pages'); ?>"><?php _e( 'Only show on store pages', 'mp' ); ?></label></p>
  6193. <?php
  6194. }
  6195. }
  6196. //Product categories widget
  6197. class MarketPress_Categories_Widget extends WP_Widget {
  6198. function MarketPress_Categories_Widget() {
  6199. $widget_ops = array( 'classname' => 'mp_categories_widget', 'description' => __( "A list or dropdown of product categories from your MarketPress store.", 'mp' ) );
  6200. $this->WP_Widget('mp_categories_widget', __('Product Categories', 'mp'), $widget_ops);
  6201. }
  6202. function widget( $args, $instance ) {
  6203. if ($instance['only_store_pages'] && !mp_is_shop_page())
  6204. return;
  6205. extract( $args );
  6206. $title = apply_filters('widget_title', empty( $instance['title'] ) ? __('Product Categories', 'mp') : $instance['title'], $instance, $this->id_base);
  6207. $c = $instance['count'] ? '1' : '0';
  6208. $h = $instance['hierarchical'] ? '1' : '0';
  6209. $d = $instance['dropdown'] ? '1' : '0';
  6210. echo $before_widget;
  6211. if ( $title )
  6212. echo $before_title . $title . $after_title;
  6213. $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
  6214. if ( $d ) {
  6215. $cat_args['show_option_none'] = __('Select Category');
  6216. $cat_args['taxonomy'] = 'product_category';
  6217. $cat_args['id'] = 'mp_category_dropdown';
  6218. mp_dropdown_categories( true, $cat_args );
  6219. } else {
  6220. ?>
  6221. <ul id="mp_category_list">
  6222. <?php
  6223. $cat_args['title_li'] = '';
  6224. $cat_args['taxonomy'] = 'product_category';
  6225. wp_list_categories( $cat_args );
  6226. ?>
  6227. </ul>
  6228. <?php
  6229. }
  6230. echo $after_widget;
  6231. }
  6232. function update( $new_instance, $old_instance ) {
  6233. $instance = $old_instance;
  6234. $instance['title'] = strip_tags($new_instance['title']);
  6235. $instance['count'] = !empty($new_instance['count']) ? 1 : 0;
  6236. $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
  6237. $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
  6238. $instance['only_store_pages'] = !empty($new_instance['only_store_pages']) ? 1 : 0;
  6239. return $instance;
  6240. }
  6241. function form( $instance ) {
  6242. //Defaults
  6243. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'only_store_pages' => 0 ) );
  6244. $title = esc_attr( $instance['title'] );
  6245. $count = isset($instance['count']) ? (bool) $instance['count'] :false;
  6246. $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
  6247. $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
  6248. $only_store_pages = isset( $instance['only_store_pages'] ) ? (bool) $instance['only_store_pages'] : false;
  6249. ?>
  6250. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
  6251. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  6252. <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
  6253. <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Show as dropdown' ); ?></label><br />
  6254. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
  6255. <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show product counts', 'mp' ); ?></label><br />
  6256. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
  6257. <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
  6258. <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('only_store_pages'); ?>" name="<?php echo $this->get_field_name('only_store_pages'); ?>"<?php checked( $only_store_pages ); ?> />
  6259. <label for="<?php echo $this->get_field_id('only_store_pages'); ?>"><?php _e( 'Only show on store pages', 'mp' ); ?></label></p>
  6260. <?php
  6261. }
  6262. }
  6263. //Product tags cloud
  6264. class MarketPress_Tag_Cloud_Widget extends WP_Widget {
  6265. function MarketPress_Tag_Cloud_Widget() {
  6266. $widget_ops = array( 'classname' => 'mp_tag_cloud_widget', 'description' => __( "Your most used product tags in cloud format from your MarketPress store.") );
  6267. $this->WP_Widget('mp_tag_cloud_widget', __('Product Tag Cloud', 'mp'), $widget_ops);
  6268. }
  6269. function widget( $args, $instance ) {
  6270. if ($instance['only_store_pages'] && !mp_is_shop_page())
  6271. return;
  6272. extract($args);
  6273. $current_taxonomy = 'product_tag';
  6274. if ( !empty($instance['title']) ) {
  6275. $title = $instance['title'];
  6276. }
  6277. $title = apply_filters('widget_title', $title, $instance, $this->id_base);
  6278. echo $before_widget;
  6279. if ( $title )
  6280. echo $before_title . $title . $after_title;
  6281. echo '<div>';
  6282. wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('taxonomy' => $current_taxonomy) ) );
  6283. echo "</div>\n";
  6284. echo $after_widget;
  6285. }
  6286. function update( $new_instance, $old_instance ) {
  6287. $instance['title'] = strip_tags(stripslashes($new_instance['title']));
  6288. $instance['only_store_pages'] = !empty($new_instance['only_store_pages']) ? 1 : 0;
  6289. return $instance;
  6290. }
  6291. function form( $instance ) {
  6292. $instance = wp_parse_args( (array) $instance, array( 'title' => __('Product Tags', 'mp'), 'only_store_pages' => 0 ) );
  6293. $only_store_pages = isset( $instance['only_store_pages'] ) ? (bool) $instance['only_store_pages'] : false;
  6294. ?>
  6295. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
  6296. <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>
  6297. <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('only_store_pages'); ?>" name="<?php echo $this->get_field_name('only_store_pages'); ?>"<?php checked( $only_store_pages ); ?> />
  6298. <label for="<?php echo $this->get_field_id('only_store_pages'); ?>"><?php _e( 'Only show on store pages', 'mp' ); ?></label></p>
  6299. <?php
  6300. }
  6301. }
  6302. ?>