PageRenderTime 152ms CodeModel.GetById 36ms RepoModel.GetById 3ms app.codeStats 0ms

/www/wp-content/plugins/ithemes-exchange/lib/functions/functions.php

https://github.com/ArzuA/gitwordpress
PHP | 1491 lines | 898 code | 190 blank | 403 comment | 156 complexity | 3ca9f848ef382193d88fd7e875c0c66a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

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

  1. <?php
  2. /**
  3. * Evaluates natural language strings to boolean equivalent
  4. *
  5. * Used primarily for handling boolean text provided in it_exchange() function options.
  6. * All values defined as true will return true, anything else is false.
  7. *
  8. * Boolean values will be passed through.
  9. *
  10. * @author Jonathan Davis from Shopp
  11. * @since 0.4.0
  12. *
  13. * @param string $string The natural language value
  14. * @param array $istrue A list strings that are true
  15. * @return boolean The boolean value of the provided text
  16. */
  17. function it_exchange_str_true ( $string, $istrue = array('yes', 'y', 'true','1','on','open') ) {
  18. if (is_array($string)) return false;
  19. if (is_bool($string)) return $string;
  20. return in_array(strtolower($string),$istrue);
  21. }
  22. /**
  23. * Parses tag option strings or arrays
  24. *
  25. * @author Jonathan Davis from Shopp
  26. * @since 0.4.0
  27. *
  28. * @param string|array $options URL-compatible query string or associative array of tag options
  29. * @return array API-ready options list
  30. */
  31. function it_exchange_parse_options( $options ) {
  32. // Set empty array
  33. $paramset = array();
  34. // If options is empty, return empty array
  35. if ( empty( $options ) )
  36. return $paramset;
  37. // If options is string, convert to array ($paramset) via parse_str
  38. if ( is_string( $options) )
  39. parse_str( $options, $paramset );
  40. else
  41. $paramset = $options;
  42. // Passed options are now an array ($paramset). Reset $options variable
  43. $options = array();
  44. // Clean keys and values
  45. foreach ( array_keys($paramset) as $key )
  46. $options[ strtolower($key) ] = $paramset[$key];
  47. // Strip slashes
  48. if ( get_magic_quotes_gpc() )
  49. $options = stripslashes_deep( $options );
  50. return $options;
  51. }
  52. /**
  53. * Formats a price based on settings
  54. *
  55. * @since 0.4.0
  56. * @todo possibly get this working with LC_MONETARY and money_format()
  57. * @return string
  58. */
  59. function it_exchange_format_price( $price, $show_symbol = true ) {
  60. if ( ! is_numeric( $price ) )
  61. $price = 0;
  62. $before = $after = '';
  63. $settings = it_exchange_get_option( 'settings_general' );
  64. $currency = it_exchange_get_currency_symbol( $settings['default-currency'] );
  65. if ( $show_symbol ) {
  66. if ( 'after' === $settings['currency-symbol-position'] )
  67. $after = $currency;
  68. else
  69. $before = $currency;
  70. }
  71. return $before . number_format( $price, 2, $settings['currency-decimals-separator'], $settings['currency-thousands-separator'] ) . $after;
  72. }
  73. /**
  74. * Loads the frontend CSS on all exchange pages
  75. *
  76. * @since 0.4.0
  77. *
  78. * @return void
  79. */
  80. function it_exchange_load_public_scripts( $current_view ) {
  81. $purchase_requirements = (array) it_exchange_get_purchase_requirements();
  82. $purchase_requirements = array_keys( $purchase_requirements );
  83. $settings = it_exchange_get_option( 'settings_general' );
  84. wp_register_style( 'it-exchange-icon-fonts', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/styles/exchange-fonts.css' ) );
  85. // Frontend Product JS
  86. if ( is_singular( 'it_exchange_prod' ) ) {
  87. $script_deps = array();
  88. if ( ( 1 == $settings['enable-gallery-zoom'] ) )
  89. array_push( $script_deps, 'jquery-zoom' );
  90. if ( ( 1 == $settings['enable-gallery-popup'] ) )
  91. array_push( $script_deps, 'jquery-colorbox' );
  92. wp_enqueue_script( 'it-exchange-product-public-js', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/exchange-product.js' ), $script_deps, false, true );
  93. wp_enqueue_style( 'it-exchange-icon-fonts' );
  94. }
  95. // ****** CHECKOUT SPECIFIC SCRIPTS *******
  96. if ( it_exchange_is_page( 'checkout' ) ) {
  97. // Enqueue purchase dialog JS on checkout screen
  98. $file = dirname( dirname( __FILE__ ) ) . '/purchase-dialog/js/exchange-purchase-dialog.js';
  99. wp_enqueue_script( 'exchange-purchase-dialog', ITUtility::get_url_from_file( $file ), array( 'jquery', 'detect-credit-card-type' ), false, true );
  100. // Register select to autocomplte
  101. wp_enqueue_style( 'it-exchange-autocomplete-style' );
  102. // General Checkout
  103. $script = ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/checkout-page.js' );
  104. wp_enqueue_script( 'it-exchange-checkout-page', $script, array( 'jquery' ), false, true );
  105. // Load Logged In purchase requirement JS if not logged in and on checkout page.
  106. if ( in_array( 'logged-in', $purchase_requirements ) && ! is_user_logged_in() ) {
  107. $script = ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/logged-in-purchase-requirement.js' );
  108. wp_enqueue_script( 'it-exchange-logged-in-purchase-requirement', $script, array( 'jquery' ), false, true );
  109. }
  110. // Load Billing Address purchase requirement JS if not logged in and on checkout page.
  111. if ( in_array( 'billing-address', $purchase_requirements ) ) {
  112. $script = ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/billing-address-purchase-requirement.js' );
  113. wp_enqueue_script( 'it-exchange-billing-address-purchase-requirement', $script, array( 'jquery', 'it-exchange-country-states-sync' ), false, true );
  114. }
  115. // Load country / state field sync if on checkout page
  116. wp_enqueue_script( 'it-exchange-country-states-sync', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/country-states-sync.js' ), array( 'jquery', 'jquery-ui-autocomplete', 'jquery-select-to-autocomplete' ), false, true );
  117. } // ****** END CHECKOUT SPECIFIC SCRIPTS *******
  118. // Frontend Style
  119. if ( ! apply_filters( 'it_exchange_disable_frontend_stylesheet', false ) )
  120. wp_enqueue_style( 'it-exchange-public-css', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/styles/exchange.css' ) );
  121. // Parent theme /exchange/style.css if it exists
  122. $parent_theme_css = get_template_directory() . '/exchange/style.css';
  123. if ( is_file( $parent_theme_css ) )
  124. wp_enqueue_style( 'it-exchange-parent-theme-css', ITUtility::get_url_from_file( $parent_theme_css ) );
  125. // Child theme /exchange/style.css if it exists
  126. $child_theme_css = get_stylesheet_directory() . '/exchange/style.css';
  127. if ( is_file( $child_theme_css ) && ( $parent_theme_css != $child_theme_css || ! is_file( $parent_theme_css ) ) )
  128. wp_enqueue_style( 'it-exchange-child-theme-css', ITUtility::get_url_from_file( $child_theme_css ) );
  129. }
  130. add_action( 'wp_enqueue_scripts', 'it_exchange_load_public_scripts' );
  131. /**
  132. * Registers generic scripts we might want to use in plugins/addons
  133. *
  134. * @since 1.7.0
  135. *
  136. * @return void
  137. */
  138. function it_exchange_register_scripts() {
  139. // jQuery Zoom
  140. wp_register_script( 'jquery-zoom', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/jquery.zoom.min.js' ), array( 'jquery' ), false, true );
  141. // jQuery Colorbox
  142. wp_register_script( 'jquery-colorbox', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/jquery.colorbox.min.js' ), array( 'jquery' ), false, true );
  143. // Detect CC Type
  144. wp_register_script( 'detect-credit-card-type', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/detect-credit-card-type.js' ), array( 'jquery' ), false, true );
  145. // Detect CC Type
  146. wp_register_script( 'it-exchange-event-manager', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/event-manager.js' ), array(), false, true );
  147. // Select to Autocomplete
  148. wp_register_script( 'jquery-select-to-autocomplete', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/js/jquery.select-to-autocomplete.min.js' ), array( 'jquery', 'jquery-ui-autocomplete' ) );
  149. wp_register_style( 'it-exchange-autocomplete-style', ITUtility::get_url_from_file( dirname( dirname( __FILE__ ) ) . '/assets/styles/autocomplete.css' ) );
  150. }
  151. add_action( 'wp_enqueue_scripts', 'it_exchange_register_scripts', 1 );
  152. add_action( 'admin_enqueue_scripts', 'it_exchange_register_scripts', 1 );
  153. /**
  154. * Loads functions.php in theme if it exists
  155. *
  156. * @since 1.2.0
  157. *
  158. * @return void
  159. */
  160. function it_exchange_load_theme_functions_for_exchange() {
  161. $parent_theme_functions = get_template_directory() . '/exchange/functions.php';
  162. $child_theme_functions = get_stylesheet_directory() . '/exchange/functions.php';
  163. // Parent theme
  164. if ( is_file( $parent_theme_functions ) )
  165. include_once( $parent_theme_functions );
  166. // Child theme or primary theme if not parent
  167. if ( is_file( $child_theme_functions ) )
  168. include_once( $child_theme_functions );
  169. }
  170. add_action( 'it_exchange_enabled_addons_loaded', 'it_exchange_load_theme_functions_for_exchange' );
  171. /**
  172. * Hook for processing webhooks from services like PayPal IPN, Stripe, etc.
  173. *
  174. * @since 0.4.0
  175. */
  176. function it_exchange_process_webhooks() {
  177. // Grab registered webhooks
  178. $webhooks = it_exchange_get_webhooks();
  179. // Loop through them and init callbacks
  180. foreach( $webhooks as $key => $param ) {
  181. if ( ! empty( $_REQUEST[$param] ) )
  182. do_action( 'it_exchange_webhook_' . $param, $_REQUEST );
  183. }
  184. do_action( 'it_exchange_webhooks_processed' );
  185. }
  186. add_action( 'wp', 'it_exchange_process_webhooks' );
  187. /**
  188. * Add reset exchange button to settings page if WP_Debug is on
  189. *
  190. * @since 0.4.2
  191. *
  192. * @param object $form the ITForm object for the settings form
  193. * @return void
  194. */
  195. function it_exchange_add_plugin_reset_checkbox_to_settings( $form ) {
  196. if ( it_exchange_has_messages( 'notice' ) ) {
  197. foreach ( it_exchange_get_messages( 'notice' ) as $notice ) {
  198. ITUtility::show_status_message( $notice );
  199. }
  200. }
  201. if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG || ! current_user_can( 'administrator' ) )
  202. return;
  203. // Never check this by default.
  204. $form->set_option( 'reset-exchange', 0 );
  205. ?>
  206. <tr valign="top">
  207. <th scope="row"><strong><?php _e( 'Dangerous Settings', 'it-l10n-ithemes-exchange' ); ?></strong></th>
  208. <td></td>
  209. </tr>
  210. <tr valign="top">
  211. <th scope="row"><label for="reset-exchange"><?php _e( 'Reset Exchange', 'it-l10n-ithemes-exchange' ) ?></label></th>
  212. <td>
  213. <?php $form->add_check_box( 'reset-exchange' ); ?>
  214. <label for="reset-exchange"><?php _e( 'Reset ALL data', 'it-l10n-ithemes-exchange' ) ?></label><br />
  215. <span class="description"><?php _e( 'Checking this box will rest ALL settings and DELETE ALL DATA.', 'it-l10n-ithemes-exchange' ); ?></span>
  216. </td>
  217. </tr>
  218. <?php
  219. }
  220. add_action( 'it_exchange_general_settings_table_bottom', 'it_exchange_add_plugin_reset_checkbox_to_settings' );
  221. /**
  222. * This function resets Exchange
  223. *
  224. * Deletes all Products
  225. * Deletes all transactions
  226. * Deletes all core settings
  227. * Fires a hook so that addons can do the same.
  228. *
  229. * @since 0.4.2
  230. * @return void
  231. */
  232. function it_exchange_reset_everything() {
  233. // Don't do anything if WP_DEBUG isn't true
  234. if ( ! defined( 'WP_DEBUG' ) || ! WP_DEBUG )
  235. return;
  236. // Don't do anything if we're not on the settings page
  237. if ( empty( $GLOBALS['pagenow'] ) || 'admin.php' != $GLOBALS['pagenow'] || empty( $_GET['page'] ) || 'it-exchange-settings' != $_GET['page'] )
  238. return;
  239. // Don't do anything if the nonce doesn't validate
  240. $nonce = empty( $_POST['_wpnonce'] ) ? false : $_POST['_wpnonce'];
  241. if ( ! wp_verify_nonce( $nonce, 'exchange-general-settings' ) )
  242. return;
  243. // Don't do anything if the checkbox wasnt' checked
  244. $data = ITForm::get_post_data();
  245. if ( empty( $data['reset-exchange'] ) )
  246. return;
  247. // Use Post stati rather than 'any' for post type to include trashed and other non-searchable stati
  248. $stati = array_keys( get_post_stati() );
  249. // Delete all Products
  250. if ( ! apply_filters( 'it_exchange_preserve_products_on_reset', false ) ) {
  251. while( $products = it_exchange_get_products( array( 'posts_per_page' => 20, 'post_status' => $stati ) ) ) {
  252. foreach ( $products as $product ) {
  253. wp_delete_post( $product->ID, true );
  254. }
  255. }
  256. }
  257. // Delete all Transactions
  258. if ( ! apply_filters( 'it_exchange_preserve_transactions_on_reset', false ) ) {
  259. while( $transactions = it_exchange_get_transactions( array( 'posts_per_page' => 20, 'post_status' => $stati ) ) ) {
  260. foreach ( $transactions as $transaction ) {
  261. wp_delete_post( $transaction->ID, true );
  262. }
  263. }
  264. }
  265. // Delete all Coupons
  266. if ( ! apply_filters( 'it_exchange_preserve_coupons_on_reset', false ) ) {
  267. while( $coupons = it_exchange_get_coupons( array( 'posts_per_page' => 20, 'post_status' => $stati ) ) ) {
  268. foreach ( $coupons as $coupon ) {
  269. wp_delete_post( $coupon->ID, true );
  270. }
  271. }
  272. }
  273. // Delete all Downloads (post types, not files uploaded to WP Media Library)
  274. if ( ! apply_filters( 'it_exchange_preserve_products_on_reset', false ) ) {
  275. while( $downloads = get_posts( array( 'post_type' => 'it_exchange_download', 'post_status' => $stati ) ) ) {
  276. foreach ( $downloads as $download ) {
  277. wp_delete_post( $download->ID, true );
  278. }
  279. }
  280. // Delete all session data for everyone. This is inside the check for product preserves on purpose
  281. it_exchange_db_delete_all_sessions();
  282. }
  283. // Delete all core settings
  284. $settings_keys = array(
  285. 'it-storage-exchange_addon_offline_payments',
  286. 'it-storage-exchange_addon_paypal_standard',
  287. 'it-storage-exchange_addon_stripe',
  288. 'it-storage-exchange_addon_zero_sum_checkout',
  289. 'it-storage-exchange_enabled_add_ons',
  290. 'it-storage-exchange_settings_email',
  291. 'it-storage-exchange_settings_general',
  292. 'it-storage-exchange_settings_pages',
  293. 'it-exchange-hide-wizard-nag',
  294. );
  295. $settings_keys = apply_filters( 'it_exchange_reset_all_settings_keys', $settings_keys );
  296. foreach( $settings_keys as $option ) {
  297. delete_option( $option );
  298. }
  299. do_action( 'it_exchange_reset_exchange' );
  300. // Log message and redirect
  301. it_exchange_add_message( 'notice', __( 'Exchange has been reset. All data has been deleted.', 'it-l10n-ithemes-exchange' ) );
  302. wp_safe_redirect( add_query_arg( 'page', 'it-exchange-settings', trailingslashit( get_admin_url() ) . 'admin.php' ) );
  303. die();
  304. }
  305. add_action( 'admin_init', 'it_exchange_reset_everything' );
  306. /**
  307. * Register core pages
  308. *
  309. * @since 0.4.4
  310. *
  311. * @return void
  312. */
  313. function it_exchange_register_core_pages() {
  314. // Product
  315. $options = array(
  316. 'slug' => 'product',
  317. 'name' => __( 'Product', 'it-l10n-ithemes-exchange' ),
  318. 'rewrite-rules' => false, //array( 10, 'it_exchange_get_core_page_rewrites' ),
  319. 'url' => 'it_exchange_get_core_page_urls',
  320. 'settings-name' => __( 'Product Base', 'it-l10n-ithemes-exchange' ),
  321. 'type' => 'exchange',
  322. 'menu' => false,
  323. 'optional' => false,
  324. );
  325. it_exchange_register_page( 'product', $options );
  326. // Store
  327. $options = array(
  328. 'slug' => 'store',
  329. 'name' => __( 'Store', 'it-l10n-ithemes-exchange' ),
  330. 'rewrite-rules' => array( 230, 'it_exchange_get_core_page_rewrites' ),
  331. 'url' => 'it_exchange_get_core_page_urls',
  332. 'settings-name' => __( 'Store Page', 'it-l10n-ithemes-exchange' ),
  333. 'tip' => __( 'Where all your products are shown in one place', 'it-l10n-ithemes-exchange' ),
  334. 'type' => 'exchange',
  335. 'menu' => true,
  336. 'optional' => true,
  337. );
  338. it_exchange_register_page( 'store', $options );
  339. // Transaction
  340. $options = array(
  341. 'slug' => 'transaction',
  342. 'name' => __( 'Transaction', 'it-l10n-ithemes-exchange' ),
  343. 'rewrite-rules' => array( 210, 'it_exchange_get_core_page_rewrites' ),
  344. 'url' => 'it_exchange_get_core_page_urls',
  345. 'settings-name' => __( 'Transaction', 'it-l10n-ithemes-exchange' ),
  346. 'type' => 'exchange',
  347. 'menu' => false,
  348. 'optional' => false,
  349. );
  350. it_exchange_register_page( 'transaction', $options );
  351. // Customer Registration
  352. $options = array(
  353. 'slug' => 'registration',
  354. 'name' => __( 'Registration', 'it-l10n-ithemes-exchange' ),
  355. 'rewrite-rules' => array( 105, 'it_exchange_get_core_page_rewrites' ),
  356. 'url' => 'it_exchange_get_core_page_urls',
  357. 'settings-name' => __( 'Customer Registration', 'it-l10n-ithemes-exchange' ),
  358. 'tip' => __( 'Where customers register to login, download, etc. You can turn off registration and allow guest checkouts in Exchange / Add-ons / Digital Downloads Settings.', 'it-l10n-ithemes-exchange' ),
  359. 'type' => 'exchange',
  360. 'menu' => true,
  361. 'optional' => true,
  362. );
  363. it_exchange_register_page( 'registration', $options );
  364. // Account
  365. $options = array(
  366. 'slug' => 'account',
  367. 'name' => __( 'Account', 'it-l10n-ithemes-exchange' ),
  368. 'rewrite-rules' => array( 135, 'it_exchange_get_core_page_rewrites' ),
  369. 'url' => 'it_exchange_get_core_page_urls',
  370. 'settings-name' => __( 'Account Page', 'it-l10n-ithemes-exchange' ),
  371. 'tip' => __( 'Customers get an account when they buy something, so they can login and download their purchases. This is the main landing page for customers after they log in.', 'it-l10n-ithemes-exchange' ),
  372. 'type' => 'exchange',
  373. 'menu' => true,
  374. 'optional' => false,
  375. );
  376. it_exchange_register_page( 'account', $options );
  377. // Profile
  378. $options = array(
  379. 'slug' => 'profile',
  380. 'name' => __( 'Profile', 'it-l10n-ithemes-exchange' ),
  381. 'rewrite-rules' => array( 130, 'it_exchange_get_core_page_rewrites' ),
  382. 'url' => 'it_exchange_get_core_page_urls',
  383. 'settings-name' => __( 'Profile Page', 'it-l10n-ithemes-exchange' ),
  384. 'tip' => __( 'Private details about your customers that they can change.', 'it-l10n-ithemes-exchange' ),
  385. 'type' => 'exchange',
  386. 'menu' => true,
  387. 'optional' => true,
  388. );
  389. it_exchange_register_page( 'profile', $options );
  390. // Downloads
  391. $options = array(
  392. 'slug' => 'downloads',
  393. 'name' => __( 'Downloads', 'it-l10n-ithemes-exchange' ),
  394. 'rewrite-rules' => array( 125, 'it_exchange_get_core_page_rewrites' ),
  395. 'url' => 'it_exchange_get_core_page_urls',
  396. 'settings-name' => __( 'Customer Downloads', 'it-l10n-ithemes-exchange' ),
  397. 'tip' => __( 'Page where the customer can find all of their available downloads.', 'it-l10n-ithemes-exchange' ),
  398. 'type' => 'exchange',
  399. 'menu' => true,
  400. 'optional' => true,
  401. );
  402. it_exchange_register_page( 'downloads', $options );
  403. // Purchases
  404. $options = array(
  405. 'slug' => 'purchases',
  406. 'name' => __( 'Purchases', 'it-l10n-ithemes-exchange' ),
  407. 'rewrite-rules' => array( 120, 'it_exchange_get_core_page_rewrites' ),
  408. 'url' => 'it_exchange_get_core_page_urls',
  409. 'settings-name' => __( 'Purchases', 'it-l10n-ithemes-exchange' ),
  410. 'type' => 'exchange',
  411. 'menu' => true,
  412. 'optional' => true,
  413. );
  414. it_exchange_register_page( 'purchases', $options );
  415. // Log In
  416. $options = array(
  417. 'slug' => 'log-in',
  418. 'name' => __( 'Log In', 'it-l10n-ithemes-exchange' ),
  419. 'rewrite-rules' => array( 110, 'it_exchange_get_core_page_rewrites' ),
  420. 'url' => 'it_exchange_get_core_page_urls',
  421. 'settings-name' => __( 'Customer Log In', 'it-l10n-ithemes-exchange' ),
  422. 'type' => 'exchange',
  423. 'menu' => true,
  424. 'optional' => true,
  425. );
  426. it_exchange_register_page( 'login', $options );
  427. // Log Out
  428. $options = array(
  429. 'slug' => 'log-out',
  430. 'name' => __( 'Log Out', 'it-l10n-ithemes-exchange' ),
  431. 'rewrite-rules' => array( 115, 'it_exchange_get_core_page_rewrites' ),
  432. 'url' => 'it_exchange_get_core_page_urls',
  433. 'settings-name' => __( 'Customer Log Out', 'it-l10n-ithemes-exchange' ),
  434. 'type' => 'exchange',
  435. 'menu' => true,
  436. 'optional' => true,
  437. );
  438. it_exchange_register_page( 'logout', $options );
  439. // Confirmation
  440. $options = array(
  441. 'slug' => 'confirmation',
  442. 'name' => __( 'Thank you', 'it-l10n-ithemes-exchange' ),
  443. 'rewrite-rules' => array( 205, 'it_exchange_get_core_page_rewrites' ),
  444. 'url' => 'it_exchange_get_core_page_urls',
  445. 'settings-name' => __( 'Purchase Confirmation', 'it-l10n-ithemes-exchange' ),
  446. 'type' => 'exchange',
  447. 'menu' => false,
  448. 'optional' => false,
  449. );
  450. it_exchange_register_page( 'confirmation', $options );
  451. }
  452. add_action( 'it_libraries_loaded', 'it_exchange_register_core_pages' );
  453. /**
  454. * Returns rewrites for core pages
  455. *
  456. * @since 0.4.4
  457. *
  458. * @param string page
  459. * @return array
  460. */
  461. function it_exchange_get_core_page_rewrites( $page ) {
  462. $slug = it_exchange_get_page_slug( $page );
  463. switch( $page ) {
  464. case 'store' :
  465. $rewrites = array(
  466. $slug . '$' => 'index.php?' . $slug . '=1',
  467. );
  468. return $rewrites;
  469. break;
  470. case 'account' :
  471. $profile_slug = it_exchange_get_page_slug( 'profile' );
  472. // If we're using WP as acount page type, add the WP slug to rewrites and return.
  473. if ( 'wordpress' == it_exchange_get_page_type( 'account' ) ) {
  474. $account = get_page( it_exchange_get_page_wpid( 'account' ) );
  475. $slug = $account->post_name;
  476. }
  477. $rewrites = array(
  478. $slug . '/([^/]+)/?$' => 'index.php?' . $slug . '=$matches[1]',//&' . $profile_slug . '=1',
  479. $slug . '$' => 'index.php?' . $slug . '=1',//&' . $profile_slug . '=1',
  480. );
  481. return $rewrites;
  482. break;
  483. case 'profile' :
  484. $account_slug = it_exchange_get_page_slug( 'account' );
  485. // If we're using WP as acount page type, add the WP slug to rewrites and return.
  486. if ( 'wordpress' == it_exchange_get_page_type( 'account' ) ) {
  487. $account = get_page( it_exchange_get_page_wpid( 'account' ) );
  488. $account_slug = $account->post_name;
  489. }
  490. $rewrites = array(
  491. $account_slug . '/([^/]+)/' . $slug => 'index.php?' . $account_slug . '=$matches[1]&' . $slug . '=1',
  492. $account_slug . '/' . $slug . '$' => 'index.php?' . $account_slug . '=1&' . $slug . '=1',
  493. );
  494. return $rewrites;
  495. break;
  496. case 'registration' :
  497. $account_slug = it_exchange_get_page_slug( 'account' );
  498. // If we're using WP as acount page type, add the WP slug to rewrites and return.
  499. if ( 'wordpress' == it_exchange_get_page_type( 'account' ) ) {
  500. $account = get_page( it_exchange_get_page_wpid( 'account' ) );
  501. $account_slug = $account->post_name;
  502. }
  503. $rewrites = array(
  504. $account_slug . '/' . $slug . '$' => 'index.php?' . $account_slug . '=1&' . $slug . '=1',
  505. );
  506. return $rewrites;
  507. break;
  508. case 'login' :
  509. $account_slug = it_exchange_get_page_slug( 'account' );
  510. // If we're using WP as acount page type, add the WP slug to rewrites and return.
  511. if ( 'wordpress' == it_exchange_get_page_type( 'account' ) ) {
  512. $account = get_page( it_exchange_get_page_wpid( 'account' ) );
  513. $account_slug = $account->post_name;
  514. }
  515. $rewrites = array(
  516. $account_slug . '/' . $slug . '$' => 'index.php?' . $account_slug . '=1&' . $slug . '=1',
  517. );
  518. return $rewrites;
  519. break;
  520. case 'logout' :
  521. $account_slug = it_exchange_get_page_slug( 'account' );
  522. // If we're using WP as acount page type, add the WP slug to rewrites and return.
  523. if ( 'wordpress' == it_exchange_get_page_type( 'account' ) ) {
  524. $account = get_page( it_exchange_get_page_wpid( 'account' ) );
  525. $account_slug = $account->post_name;
  526. }
  527. $rewrites = array(
  528. $account_slug . '/' . $slug . '$' => 'index.php?' . $account_slug . '=1&' . $slug . '=1',
  529. );
  530. return $rewrites;
  531. break;
  532. case 'purchases' :
  533. $account_slug = it_exchange_get_page_slug( 'account' );
  534. // If we're using WP as acount page type, add the WP slug to rewrites and return.
  535. if ( 'wordpress' == it_exchange_get_page_type( 'account' ) ) {
  536. $account = get_page( it_exchange_get_page_wpid( 'account' ) );
  537. $account_slug = $account->post_name;
  538. }
  539. $rewrites = array(
  540. $account_slug . '/([^/]+)/' . $slug . '$' => 'index.php?' . $account_slug . '=$matches[1]&' . $slug . '=1',
  541. $account_slug . '/' . $slug . '$' => 'index.php?' . $account_slug . '=1&' . $slug . '=1',
  542. );
  543. return $rewrites;
  544. break;
  545. case 'downloads' :
  546. $account_slug = it_exchange_get_page_slug( 'account' );
  547. // If we're using WP as acount page type, add the WP slug to rewrites and return.
  548. if ( 'wordpress' == it_exchange_get_page_type( 'account' ) ) {
  549. $account = get_page( it_exchange_get_page_wpid( 'account' ) );
  550. $account_slug = $account->post_name;
  551. }
  552. $rewrites = array(
  553. $account_slug . '/([^/]+)/' . $slug . '$' => 'index.php?' . $account_slug . '=$matches[1]&' . $slug . '=1',
  554. $account_slug . '/' . $slug . '$' => 'index.php?' . $account_slug . '=1&' . $slug . '=1',
  555. );
  556. return $rewrites;
  557. break;
  558. case 'confirmation' :
  559. $rewrites = array(
  560. $slug . '/([^/]+)/?$' => 'index.php?' . $slug . '=$matches[1]',
  561. );
  562. return $rewrites;
  563. break;
  564. case 'transaction' :
  565. $rewrites = array(
  566. $slug => 'index.php?' . $slug . '=1',
  567. );
  568. return $rewrites;
  569. break;
  570. }
  571. return false;
  572. }
  573. /**
  574. * Returns URL for core pages
  575. *
  576. * @since 0.4.4
  577. *
  578. * @param string page
  579. * @return array
  580. */
  581. function it_exchange_get_core_page_urls( $page ) {
  582. $slug = it_exchange_get_page_slug( $page );
  583. $permalinks = (boolean) get_option( 'permalink_structure' );
  584. $base = trailingslashit( get_home_url() );
  585. // Proccess superwidget links
  586. if ( it_exchange_in_superwidget() && $slug != 'transaction' && $page != 'confirmation' ) {
  587. // Get current URL without exchange query args
  588. $url = it_exchange_clean_query_args();
  589. return add_query_arg( 'ite-sw-state', $slug, $url );
  590. }
  591. switch ( $page ) {
  592. // Store
  593. case 'store' :
  594. if ( $permalinks )
  595. return trailingslashit( $base . $slug );
  596. else
  597. return add_query_arg( array( $slug => 1 ), $base );
  598. break;
  599. // Anything that is a subpage of store
  600. case 'confirmation' :
  601. case 'transaction' :
  602. if ( $permalinks )
  603. return trailingslashit( $base . $slug );
  604. else
  605. return add_query_arg( array( $slug => 1 ), $base );
  606. break;
  607. // Anything else
  608. default :
  609. // Account Slug
  610. if ( 'wordpress' == it_exchange_get_page_type( 'account' ) ) {
  611. $account_page = get_page( it_exchange_get_page_wpid( 'account' ) );
  612. $account_slug = $account_page->post_name;
  613. } else {
  614. $account_slug = it_exchange_get_page_slug( 'account' );
  615. }
  616. // Replace account value with name if user is logged in
  617. if ( $permalinks )
  618. $base = trailingslashit( $base . $account_slug );
  619. else
  620. $base = add_query_arg( array( $account_slug => 1 ), $base );
  621. $account_name = get_query_var( 'account' );
  622. if ( $account_name && '1' != $account_name && ( 'login' != $page && 'logout' != $page ) ) {
  623. if ( $permalinks ) {
  624. $base = trailingslashit( $base . $account_name );
  625. } else {
  626. $base = remove_query_arg( $account_slug, $base );
  627. $base = add_query_arg( array( $account_slug => $account_name ), $base );
  628. }
  629. }
  630. if ( 'account' == $page ) {
  631. return $base;
  632. } else {
  633. if ( $permalinks )
  634. return trailingslashit( $base . $slug );
  635. else
  636. return add_query_arg( array( $slug => 1 ), $base );
  637. }
  638. break;
  639. }
  640. }
  641. /**
  642. * Creates a shortcode that returns content template parts for pages
  643. *
  644. * @since 0.4.8
  645. *
  646. * @param array $atts attributes passed in via shortcode arguments
  647. * @return string the template part
  648. */
  649. function it_exchange_add_page_shortcode( $atts ) {
  650. $defaults = array(
  651. 'page' => false,
  652. );
  653. $atts = shortcode_atts( $defaults, $atts );
  654. // Don't return anything if page type is not WordPress
  655. if ( 'wordpress' != it_exchange_get_page_type( $atts['page'] ) )
  656. return '';
  657. if ( empty( $atts['page'] ) )
  658. return false;
  659. ob_start();
  660. it_exchange_get_template_part( 'content', $atts['page'] );
  661. return ob_get_clean();
  662. }
  663. add_shortcode( 'it-exchange-page', 'it_exchange_add_page_shortcode' );
  664. /**
  665. * Creates a shortcode that returns customer information
  666. *
  667. * @since 1.4.0
  668. *
  669. * @param array $atts attributes passed in via shortcode arguments
  670. * @return string the template part
  671. */
  672. function it_exchange_add_customer_shortcode( $atts ) {
  673. $defaults = array(
  674. 'show' => false,
  675. 'avatar_size' => 128,
  676. );
  677. $atts = shortcode_atts( $defaults, $atts );
  678. $whitelist = array(
  679. 'first-name', 'last-name', 'username', 'email', 'avatar', 'site-name',
  680. );
  681. $whitelist = apply_filters( 'it_exchange_customer_shortcode_tag_list', $whitelist );
  682. if ( empty( $atts['show'] ) || ! in_array( $atts['show'], (array) $whitelist ) )
  683. return '';
  684. $options = array(
  685. 'format' => 'field-value',
  686. );
  687. if ( 'avatar' == $atts['show'] )
  688. $options['size'] = $atts['avatar_size'];
  689. $output = it_exchange( 'customer', 'get-' . $atts['show'], $options );
  690. if ( empty( $output ) ) {
  691. //fallbacks if we have empty $output
  692. switch( $atts['show'] ) {
  693. case 'first-name':
  694. $output = it_exchange( 'customer', 'get-username', array( 'format' => 'field-value' ) );
  695. break;
  696. }
  697. }
  698. return $output;
  699. }
  700. add_shortcode( 'it_exchange_customer', 'it_exchange_add_customer_shortcode' );
  701. /**
  702. * Adds date retraints to query posts.
  703. *
  704. * This function isn't applied to any queries by default. Certain functions like those in the basic_reporting addon add it as a filter and remove it.
  705. *
  706. * @since 0.4.9
  707. *
  708. * @param string $where the where clause of the query
  709. * @return string
  710. */
  711. function it_exchange_filter_where_clause_for_all_queries( $where='' ) {
  712. // If this filter has been added, we expect one of the following two GLOBALS to have been set
  713. $start_date = empty( $GLOBALS['it_exchange']['where_start'] ) ? false : $GLOBALS['it_exchange']['where_start'];
  714. $end_date = empty( $GLOBALS['it_exchange']['where_end'] ) ? false : $GLOBALS['it_exchange']['where_end'];
  715. // Return without doing anything if neither start or end are set
  716. if ( ! $start_date && ! $end_date )
  717. return $where;
  718. if ( $start_date )
  719. $where .= $GLOBALS['wpdb']->prepare( ' AND post_date >= %s', $start_date );
  720. if ( $end_date )
  721. $where .= $GLOBALS['wpdb']->prepare( ' AND post_date <= %s', $end_date );
  722. return $where;
  723. }
  724. /**
  725. * Clear the sessions when multi-item carts are enabled
  726. *
  727. * @todo replace when we introduce on enable an on diable hooks
  728. *
  729. * @since 0.4.11
  730. *
  731. * @param string $addon name of addon being enabled.
  732. * @return void
  733. */
  734. function it_exchange_clear_sessions_when_multi_item_cart_is_enabled( $addon_slug ) {
  735. if ( 'multi-item-cart-option' == $addon_slug['slug'] )
  736. it_exchange_db_delete_all_sessions();
  737. }
  738. add_action( 'it_exchange_add_on_enabled', 'it_exchange_clear_sessions_when_multi_item_cart_is_enabled' );
  739. /**
  740. * Registers our default purchase requirements
  741. *
  742. * @since 1.2.0
  743. */
  744. function it_exchange_register_default_purchase_requirements() {
  745. // Link vars
  746. $login = __( 'Log in', 'it-l10n-ithemes-exchange' );
  747. $register = __( 'register', 'it-l10n-ithemes-exchange' );
  748. $cart = __( 'edit your cart', 'it-l10n-ithemes-exchange' );
  749. $login_link = '<a href="' . it_exchange_get_page_url( 'login' ) . '" class="it-exchange-login-requirement-login">';
  750. $reg_link = '<a href="' . it_exchange_get_page_url( 'registration' ) . '" class="it-exchange-login-requirement-registration">';
  751. $cart_link = '<a href="' . it_exchange_get_page_url( 'cart' ) . '">';
  752. $close_link = '</a>';
  753. // User must be logged-in to checkout
  754. $properties = array(
  755. 'priority' => 1,
  756. 'requirement-met' => 'is_user_logged_in',
  757. 'sw-template-part' => it_exchange_get_default_sw_checkout_mode(), //apply_filters( 'it_exchange_sw_template_part_for_logged_in_purchase_requirement', 'registration' ),
  758. 'checkout-template-part' => 'logged-in', //apply_filters( 'it_exchange_checkout_template_part_for_logged_in_purchase_requirement', 'logged-in' ),
  759. 'notification' => sprintf( __( 'You must be logged in to complete your purchase. %s' . $login . '%s, %s' . $register . '%s or %s' . $cart . '%s', 'it-l10n-ithemes-exchange' ), $login_link, $close_link, $reg_link, $close_link, $cart_link, $close_link ),
  760. );
  761. it_exchange_register_purchase_requirement( 'logged-in', $properties );
  762. // Billing Address Purchase Requirement
  763. $properties = array(
  764. 'priority' => 5.11,
  765. 'requirement-met' => 'it_exchange_get_customer_billing_address',
  766. 'sw-template-part' => apply_filters( 'it_exchange_sw_template_part_for_logged_in_purchase_requirement', 'billing-address' ),
  767. 'checkout-template-part' => 'billing-address',
  768. 'notification' => __( 'We need a billing address before you can checkout', 'it-l10n-ithemes-exchange' ),
  769. );
  770. // Only init the billing address if an add-on asks for it
  771. if ( apply_filters( 'it_exchange_billing_address_purchase_requirement_enabled', false ) )
  772. it_exchange_register_purchase_requirement( 'billing-address', $properties );
  773. }
  774. add_action( 'init', 'it_exchange_register_default_purchase_requirements' );
  775. /**
  776. * The default checkout mode for the superwidget
  777. *
  778. * @since 1.6.0
  779. *
  780. * @return string
  781. */
  782. function it_exchange_get_default_sw_checkout_mode() {
  783. $settings = it_exchange_get_option( 'settings_general' );
  784. $default_mode = empty( $settings['checkout-reg-form'] ) ? 'registration' : $settings['checkout-reg-form'];
  785. $default_mode = apply_filters( 'it_exchange_get_default_sw_checkout_mode', $default_mode );
  786. add_filter( 'it_exchange_is_sw_' . $default_mode . '_checkout_mode', '__return_true' );
  787. return $default_mode;
  788. }
  789. /**
  790. * The default checkout mode for the page
  791. *
  792. * @since 1.6.0
  793. *
  794. * @return string
  795. */
  796. function it_exchange_get_default_content_checkout_mode() {
  797. $settings = it_exchange_get_option( 'settings_general' );
  798. $default_mode = empty( $settings['checkout-reg-form'] ) ? 'registration' : $settings['checkout-reg-form'];
  799. $default_mode = apply_filters( 'it_exchange_get_default_content_checkout_mode', $default_mode );
  800. add_filter( 'it_exchange_is_content_' . $default_mode . '_checkout_mode', '__return_true' );
  801. return $default_mode;
  802. }
  803. add_action( 'template_redirect', 'it_exchange_get_default_content_checkout_mode' );
  804. /**
  805. * Registers any purchase requirements Super Widget template parts as valid
  806. *
  807. * @since 1.2.0
  808. *
  809. * @param array $existing The existing valid template parts
  810. * @reutrn array
  811. */
  812. function it_exchange_register_valid_sw_states_for_purchase_reqs( $existing ) {
  813. foreach( (array) it_exchange_get_purchase_requirements() as $slug => $properties ) {
  814. $sw_template = empty( $properties['sw-template-part'] ) ? false : $properties['sw-template-part'];
  815. if ( empty( $existing[$sw_template] ) )
  816. $existing[] = $sw_template;
  817. }
  818. return $existing;
  819. }
  820. add_filter( 'it_exchange_super_widget_valid_states', 'it_exchange_register_valid_sw_states_for_purchase_reqs' );
  821. /**
  822. * Add purchase requiremnt notification to chekcout page if needed.
  823. *
  824. * @since 1.2.0
  825. *
  826. * @return void
  827. */
  828. function it_exchange_add_purchase_requirement_notification() {
  829. if ( false === ( $notification = it_exchange_get_next_purchase_requirement_property( 'notification' ) ) )
  830. return;
  831. do_action( 'it_exchange_content_checkout_before_purchase_requirements_notification_element' );
  832. ?>
  833. <div class="it-exchange-checkout-purchase-requirements-notification">
  834. <?php _e( $notification ); ?>
  835. </div>
  836. <?php
  837. do_action( 'it_exchange_content_checkout_actions_after_purchase_requirements_notification_element' );
  838. }
  839. add_action( 'it_exchange_content_checkout_after_purchase_requirements', 'it_exchange_add_purchase_requirement_notification' );
  840. /**
  841. * Rmove purchase options if purchase requirements haven't been met
  842. *
  843. * @since 1.2.0
  844. *
  845. * @reutnr void
  846. */
  847. function it_exchange_disable_purchase_options_on_checkout_page( $elements ) {
  848. if ( false === ( $message = it_exchange_get_next_purchase_requirement_property( 'notification' ) ) )
  849. return $elements;
  850. // Locate the transaction-methods key in elements array (if it exists)
  851. $index = array_search( 'transaction-methods', $elements );
  852. if ( false === $index )
  853. return $elements;
  854. // Remove transaction-methods
  855. unset( $elements[$index] );
  856. return $elements;
  857. }
  858. add_filter( 'it_exchange_get_content_checkout_actions_elements', 'it_exchange_disable_purchase_options_on_checkout_page' );
  859. /**
  860. * Add Billing Address to the super-widget-checkout totals loop
  861. *
  862. * @since 1.3.0
  863. *
  864. * @param array $loops list of existing elements
  865. * @return array
  866. */
  867. function it_exchange_add_billing_address_to_sw_template_totals_loops( $loops ) {
  868. // Abandon if not doing billing
  869. if ( ! apply_filters( 'it_exchange_billing_address_purchase_requirement_enabled', false ) )
  870. return $loops;
  871. // Set index to end of array.
  872. $index = array_search( 'discounts', $loops );
  873. $index = ( false === $index ) ? array_search( 'totals-taxes-simple', $loops ) : $index;
  874. $index = ( false === $index ) ? count($loops) -1 : $index;
  875. array_splice( $loops, $index, 0, 'billing-address' );
  876. return $loops;
  877. }
  878. add_filter( 'it_exchange_get_super-widget-checkout_after-cart-items_loops', 'it_exchange_add_billing_address_to_sw_template_totals_loops' );
  879. /**
  880. * Clear Billing Address when the cart is emptied or a user logs out.
  881. *
  882. * @since 1.3.0
  883. *
  884. * @return void
  885. */
  886. function it_exchange_clear_billing_on_cart_empty() {
  887. it_exchange_remove_cart_data( 'billing-address' );
  888. }
  889. add_action( 'it_exchange_empty_shopping_cart', 'it_exchange_clear_billing_on_cart_empty' );
  890. add_action( 'wp_logout', 'it_exchange_clear_billing_on_cart_empty' );
  891. /**
  892. * AJAX callback for Country / State drop downs
  893. *
  894. * @since 1.3.0
  895. *
  896. * @return void
  897. */
  898. function print_country_states_ajax() {
  899. if ( empty( $_POST['ite_action_ajax'] ) || 'ite-country-states-update' != $_POST['ite_action_ajax'] )
  900. return;
  901. define( 'DOING_AJAX', true );
  902. $base_country = empty( $_POST['ite_base_country_ajax'] ) ? 'US' : $_POST['ite_base_country_ajax'];
  903. $base_state = empty( $_POST['ite_base_state_ajax'] ) ? '' : $_POST['ite_base_state_ajax'];
  904. $template_part = empty( $_POST['ite_template_part_ajax'] ) ? '' : $_POST['ite_template_part_ajax'];
  905. $admin_prefix = empty( $_POST['ite_admin_prefix_ajax'] ) ? false : $_POST['ite_admin_prefix_ajax'];
  906. if ( $admin_prefix && 'false' != $admin_prefix ) {
  907. do_action( 'it_exchange_admin_country_states_sync_for_' . $admin_prefix );
  908. die( __( 'Coding Error: Please hook into the following action, print your field based on $_POST vars and die():<br /> "it_exchange_admin_country_states_sync_for_' . $admin_prefix . '"' ) );
  909. } else {
  910. it_exchange_get_template_part( $template_part );
  911. }
  912. die();
  913. }
  914. add_action( 'init', 'print_country_states_ajax' );
  915. /**
  916. * Prints a homeURL var in JS
  917. *
  918. * @since 1.3.0
  919. */
  920. function it_exchange_print_home_url_in_js() {
  921. ?>
  922. <script type="text/javascript">
  923. var itExchangeAjaxCountryStatesAjaxURL = '<?php echo esc_js( trailingslashit( get_site_url() ) ); ?>';
  924. </script>
  925. <?php
  926. }
  927. add_action( 'wp_head', 'it_exchange_print_home_url_in_js' );
  928. /**
  929. * Force rewrite rule update on upgrade
  930. *
  931. * @since 1.4.0
  932. *
  933. * @param array $versions old and new versions. not used here
  934. * @return void
  935. */
  936. function it_exchange_force_rewrite_flush_on_upgrade() {
  937. add_option('_it-exchange-flush-rewrites', true );
  938. }
  939. add_action( 'it_exchange_version_updated', 'it_exchange_force_rewrite_flush_on_upgrade' );
  940. /**
  941. * Force rewrite rule update on upgrade
  942. *
  943. * @since 1.8.1
  944. *
  945. * @param array $versions old and new versions. not used here
  946. * @return void
  947. */
  948. function it_exchange_clean_duplicate_user_post_meta( $versions ) {
  949. if ( version_compare( '1.8.1', $versions['previous'], '>' ) ) {
  950. global $wpdb;
  951. $wpdb->query(
  952. "
  953. DELETE n1
  954. FROM $wpdb->postmeta n1, $wpdb->postmeta n2
  955. WHERE n1.post_id = n2.post_id
  956. AND n1.meta_key = '_it_exchange_transaction_id'
  957. AND n1.meta_value = n2.meta_value
  958. AND n1.meta_id > n2.meta_id
  959. "
  960. );
  961. $wpdb->query(
  962. "
  963. DELETE n1
  964. FROM $wpdb->usermeta n1, $wpdb->usermeta n2
  965. WHERE n1.user_id = n2.user_id
  966. AND n1.meta_key = '_it_exchange_transaction_id'
  967. AND n1.meta_value = n2.meta_value
  968. AND n1.umeta_id > n2.umeta_id
  969. "
  970. );
  971. }
  972. }
  973. add_action( 'it_exchange_version_updated', 'it_exchange_clean_duplicate_user_post_meta' );
  974. /**
  975. * Add custom image sizs to use in themes and admin.
  976. *
  977. * @since 1.6.0
  978. *
  979. * @return void
  980. */
  981. function it_exchange_add_image_sizes() {
  982. $image_sizes = array(
  983. 'large' => array(
  984. 'width' => 1000,
  985. 'height' => 1000,
  986. 'crop' => false
  987. ),
  988. 'thumb' => array(
  989. 'width' => 150,
  990. 'height' => 150,
  991. 'crop' => true
  992. ),
  993. );
  994. foreach ( $image_sizes as $name => $data ) {
  995. add_image_size( 'it-exchange-' . $name, $data['width'], $data['height'], $data['crop'] );
  996. }
  997. }
  998. /*
  999. NOTE Tableing this for now until we write a way to regenerate images for users.
  1000. add_action( 'init', 'it_exchange_add_image_sizes' );
  1001. */
  1002. /**
  1003. * Change the content_width global if we are viewing
  1004. * an Exchange product page.
  1005. *
  1006. * NOTE The function is temporary until we add the image
  1007. * sizes function above.
  1008. *
  1009. * @since 1.5
  1010. *
  1011. * @return void
  1012. * @var $content_width
  1013. */
  1014. function it_exchange_set_content_width_on_product_pages() {
  1015. if ( it_exchange_is_page( 'product' ) ) {
  1016. global $content_width;
  1017. $content_width = 1024;
  1018. }
  1019. }
  1020. add_action( 'template_redirect', 'it_exchange_set_content_width_on_product_pages', 100 );
  1021. /**
  1022. * Redirects to Exchange Login page if login fails
  1023. *
  1024. * Technically, we're hijacking a filter to use it for an action.
  1025. *
  1026. * @since 1.6.0
  1027. *
  1028. * @param object $error instance of WP_Error
  1029. * @return mixed
  1030. */
  1031. function it_exchange_redirect_to_correct_login_form_on_error( $error ) {
  1032. if ( empty( $error ) || ! is_wp_error( $error ) || empty( $error->errors ) )
  1033. return $error;
  1034. $wp_referer = wp_get_referer();
  1035. $exchange_pages[] = it_exchange_get_page_url( 'login' );
  1036. $exchange_pages[] = it_exchange_get_page_url( 'checkout' );
  1037. if ( in_array( $wp_referer, $exchange_pages ) ) {
  1038. it_exchange_add_message( 'error', $error->get_error_message() );
  1039. $url_target = ( $wp_referer == $exchange_pages[1] ) ? 'checkout' : 'login';
  1040. it_exchange_redirect( $wp_referer, 'login-failed-from-' . $url_target );
  1041. die();
  1042. }
  1043. return $error;
  1044. }
  1045. add_filter( 'wp_login_errors', 'it_exchange_redirect_to_correct_login_form_on_error', 99 );
  1046. /**
  1047. * Prints a tooltip in the admin
  1048. *
  1049. * @since 1.7.9
  1050. *
  1051. * @param string $text the HTML for the tooltip. Can be a plaintext string or HTML
  1052. * @param boolean $echo echo the tooltip? defaults to true
  1053. * @param string $indicator the character used to indicate a tooltip is avaialable. Defaults to 'i'
  1054. * @return string
  1055. */
  1056. function it_exchange_admin_tooltip( $text, $echo=true, $indicator='i' ) {
  1057. $tooltip = '<span class="it-exchange-tip" data-tip-content="' . esc_attr( $text ) . '">' . $indicator . '</span>';
  1058. $tooltip = apply_filters( 'it_exchange_admin_tooltip', $tooltip, $text, $indicator );
  1059. if ( true === $echo )
  1060. echo $tooltip;
  1061. return $tooltip;
  1062. }
  1063. /**
  1064. * Blocks access to Download iThemes Exchange attachments
  1065. *
  1066. * @since 1.7.18
  1067. * @return void
  1068. */
  1069. function it_exchange_block_attachments() {
  1070. if ( ! is_attachment() )
  1071. return;
  1072. $uri = wp_get_attachment_url( get_the_ID() );
  1073. $args = array(
  1074. 'post_type' => 'it_exchange_download',
  1075. 'meta_query' => array(
  1076. array(
  1077. 'key' => '_it-exchange-download-info',
  1078. 'value' => $uri,
  1079. 'compare' => 'LIKE',
  1080. )
  1081. ),
  1082. );
  1083. $results = get_posts( $args );
  1084. if ( empty( $results ) )
  1085. return;
  1086. wp_die( __( 'You do not have permission to view this file.', 'it-l10n-ithemes-exchange' ), __( 'Error', 'it-l10n-ithemes-exchange' ), array( 'response' => 403, 'back_link' => true ) );
  1087. }
  1088. add_action( 'template_redirect', 'it_exchange_block_attachments' );
  1089. /************************************
  1090. * THE FOLLOWING API METHODS AREN'T READY
  1091. * FOR PRIMETIME YET SO THEY LIVE HERE FOR NOW.
  1092. * USE WITH CAUTION
  1093. *************************************/
  1094. function it_exchange_add_product( $args=array() ) {
  1095. $defaults = array(
  1096. 'status' => 'publish',
  1097. );
  1098. $defaults = apply_filters( 'it_exchange_add_product_defaults', $defaults );
  1099. $args = ITUtility::merge_defaults( $args, $defaults );
  1100. // Convert $args to insert post args
  1101. $post_args = array();
  1102. $post_args['post_status'] = $args['status'];
  1103. $post_args['post_type'] = 'it_exchange_prod';
  1104. $post_args['post_title'] = empty( $args['title'] ) ? '' : $args['title'];
  1105. $post_args['post_content'] = ( it_exchange_product_type_supports_feature( $args['type'], 'extended-description' ) && ! empty( $args['extended-description'] ) ) ? $args['extended-description'] : '';
  1106. // Insert Post and get ID
  1107. if ( $product_id = wp_insert_post( $post_args ) ) {
  1108. update_post_meta( $product_id, '_it_exchange_product_type', $args['type'] );
  1109. update_post_meta( $product_id, '_it-exchange-visibility', empty( $args['show_in_store'] ) ? 'hidden' : 'visible' );
  1110. $type = $args['type'];
  1111. // Product Images from URLs
  1112. if ( ! empty( $args['images-from-urls'] ) && is_array( $args['images-from-urls'] ) ) {
  1113. foreach( $args['images-from-urls'] as $url => $description ) {
  1114. it_exchange_add_remote_image_to_product_images( $url, $product_id, $description );
  1115. }
  1116. unset( $args['images-from-url'] );
  1117. }
  1118. unset( $args['status'] );
  1119. unset( $args['extended-description'] );
  1120. unset( $args['type'] );
  1121. foreach( $args as $key => $value ) {
  1122. if ( it_exchange_product_type_supports_feature( $type, $key ) )
  1123. it_exchange_update_product_feature( $product_id, $key, $value );
  1124. }
  1125. return $product_id;
  1126. }
  1127. return false;
  1128. }
  1129. function it_exchange_add_remote_image_to_product_images( $url, $product_id, $desc='' ) {
  1130. $tmp = download_url( $url );
  1131. // Set variables for storage
  1132. // fix file filename for query strings
  1133. preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $url, $matches);
  1134. $file_array['name'] = basename($matches[0]);
  1135. $file_array['tmp_name'] = $tmp;
  1136. // If error storing temporarily, unlink
  1137. if ( is_wp_error( $tmp ) ) {
  1138. @unlink($file_array['tmp_name']);
  1139. $file_array['tmp_name'] = '';
  1140. }
  1141. // do the validation and storage stuff
  1142. $id = media_handle_sideload( $file_array, $product_id, $desc );
  1143. // If error storing permanently, unlink
  1144. if ( is_wp_error($id) ) {
  1145. @unlink($file_array['tmp_name']);
  1146. return $id;
  1147. }
  1148. $product_images = it_exchange_get_product_feature( $product_id, 'product-images' );
  1149. if ( empty( $product_images ) || ! is_array( $product_images ) )
  1150. $product_images = array( $id );
  1151. else
  1152. $product_images[] = $id;
  1153. it_exchange_update_product_feature( $product_id, 'product-images', $product_images );
  1154. @unlink( $file_array['temp_name'] );
  1155. return $id;
  1156. }
  1157. if ( !function_exists( 'it_exchange_dropdown_taxonomies' ) ) {
  1158. function it_exchange_dropdown_taxonomies( $args = '' ) {
  1159. $defaults = array(
  1160. 'show_option_all' => '', 'show_option_none' => '',
  1161. 'orderby' => 'id', 'order' => 'ASC',
  1162. 'show_count' => 0,
  1163. 'hide_empty' => 1, 'child_of' => 0,
  1164. 'exclude' => '', 'echo' => 1,
  1165. 'selected' => 0, 'hierarchical' => 0,
  1166. 'name' => 'tax', 'id' => '',
  1167. 'class' => 'postform', 'depth' => 0,
  1168. 'tab_index' => 0, 'taxonomy' => 'category',
  1169. 'hide_if_empty' => false
  1170. );
  1171. $defaults['selected'] = ( is_tax() ) ? get_query_var( 'term' ) : 0;
  1172. $r = wp_parse_args( $args, $defaults );
  1173. if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
  1174. $r['pad_counts'] = true;
  1175. }
  1176. extract( $r );
  1177. $tab_index_attribute = '';
  1178. if ( (int) $tab_index > 0 )
  1179. $tab_index_attribute = " tabindex=\"$tab_index\"";
  1180. $terms = get_terms( $taxonomy, $r );
  1181. $name = esc_attr( $name );
  1182. $class = esc_attr( $class );
  1183. $id = $id ? esc_attr( $id ) : $name;
  1184. if ( ! $r['hide_if_empty'] || ! empty($terms) )
  1185. $output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
  1186. else
  1187. $output = '';
  1188. if ( empty($terms) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
  1189. $show_option_none = apply_filters( 'list_cats', $show_option_none );
  1190. $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
  1191. }
  1192. if ( ! empty( $terms ) ) {
  1193. if ( $show_option_all ) {
  1194. $show_option_all = apply_filters( 'list_cats', $show_option_all );
  1195. $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
  1196. $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
  1197. }
  1198. if ( $show_option_none ) {
  1199. $show_option_none = apply_filters( 'list_cats', $show_option_none );
  1200. $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
  1201. $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
  1202. }
  1203. if ( $hierarchical )
  1204. $depth = $r['depth']; // Walk the full depth.
  1205. else
  1206. $depth = -1; // Flat.
  1207. $output .= it_exchange_walk_product_category_dropdown_tree( $terms, $depth, $r );
  1208. }
  1209. if ( ! $r['hide_if_empty'] || ! empty($terms) )
  1210. $output .= "</select>\n";
  1211. $output = apply_filters( 'wp_dropdown_cats', $output );
  1212. if ( $echo )
  1213. echo $output;
  1214. return $output;
  1215. }
  1216. }
  1217. /**
  1218. * Add At a Glance dashboard stats for products
  1219. *
  1220. * @since 1.7.27
  1221. */
  1222. function it_exchange_at_a_glance( $elements ) {
  1223. $product_counts = wp_count_posts( 'it_exchange_prod' );
  1224. if ( $product_counts && $product_counts->publish ) {
  1225. $text = _n( '%s Product', '%s Products', $product_counts->publish );
  1226. $text = sprintf( $text, number_format_i18n( $product_counts->publish ) );
  1227. $post_type_object = get_post_type_object( 'it_exchange_prod' );
  1228. if ( $post_type_object && current_user_can( $post_type_object->cap->edit_posts ) ) {
  1229. $elements[] = sprintf( '<a class="it-exchange-glance-products" href="edit.php?post_type=%1$s">%2$s</a>', 'it_exchange_prod', $text );
  1230. } else {
  1231. $elements = sprintf( '<span class="it-exchange-glance-products">%2$s</span>', 'it_exchange_prod', $text );
  1232. }
  1233. }
  1234. return $elements;
  1235. }
  1236. add_filter( 'dashboard_glance_items', 'it_exchange_at_a_glance' );
  1237. /**
  1238. * Retrieve HTML dropdown (select) content for category list.
  1239. *
  1240. * @uses Walker_CategoryDropdown to create HTML dropdown content.
  1241. * @since 1.7.9
  1242. * @see Walker_CategoryDropdown::walk() for parameters and return description.
  1243. */
  1244. function it_exchange_walk_product_category_dropdown_tree() {
  1245. $args = func_get_args();
  1246. // the user's options are the third parameter
  1247. if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') )
  1248. $walker = new Walker_ProductCategoryDropdown;
  1249. else
  1250. $walker = $args[2]['walker'];
  1251. return call_user_func_array(array( &$walker, 'walk' ), $args );
  1252. }
  1253. /**

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