PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/www/wp-content/plugins/ithemes-exchange/core-addons/admin/guest-checkout/lib/filters.php

https://github.com/ArzuA/gitwordpress
PHP | 555 lines | 230 code | 79 blank | 246 comment | 52 complexity | 3a74382b388bee3f1412d75b31edd8a6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Enqueues Guest Checkout SW JS
  4. *
  5. * @since 1.6.0
  6. *
  7. * @return void
  8. */
  9. function it_exchange_guest_checkout_enqueue_sw_js() {
  10. $file = ITUtility::get_url_from_file( dirname( __FILE__ ) . '/assets/js/super-widget.js' );
  11. wp_enqueue_script( 'it-exchange-guest-checkout-sw', $file, array( 'it-exchange-super-widget' ), false, true );
  12. }
  13. add_action( 'it_exchange_enqueue_super_widget_scripts', 'it_exchange_guest_checkout_enqueue_sw_js' );
  14. /**
  15. * Enqueues the checkout page scripts
  16. *
  17. * @since 1.6.0
  18. *
  19. * @return void
  20. */
  21. function it_exchange_guest_checkout_enqueue_checkout_scripts() {
  22. if ( ! it_exchange_is_page( 'checkout' ) )
  23. return;
  24. $file = ITUtility::get_url_from_file( dirname( __FILE__ ) . '/assets/js/checkout.js' );
  25. wp_enqueue_script( 'it-exchange-guest-checkout-checkout-page', $file, array( 'jquery' ), false, true );
  26. }
  27. add_action( 'wp_enqueue_scripts', 'it_exchange_guest_checkout_enqueue_checkout_scripts' );
  28. /**
  29. * Init Guest Checkout Registration/Login via email
  30. *
  31. * @since 1.6.0
  32. *
  33. * @return void
  34. */
  35. function it_exchange_guest_checkout_init_login() {
  36. if ( empty( $_POST['it-exchange-init-guest-checkout'] ) )
  37. return;
  38. // Vaidate email address
  39. if ( ! is_email( $_POST['email'] ) ) {
  40. it_exchange_add_message( 'error', __( 'Please use a properly formatted email address.', 'it-l10n-ithemes-exchange' ) );
  41. return;
  42. }
  43. $customer_email = $_POST['email'];
  44. it_exchange_init_guest_checkout_session( $customer_email );
  45. }
  46. add_action( 'template_redirect', 'it_exchange_guest_checkout_init_login' );
  47. /**
  48. * Return true on has_transaction (for confirmation screen) if conditionals match
  49. *
  50. * Conditionals:
  51. * - We're doing a guest checkout
  52. * - Transaction was a guest checkout transaction
  53. * - Current guest has same email as one used in the transaction
  54. *
  55. * @since 1.6.0
  56. *
  57. * @param boolean $has_transaction the value coming in from the WP filter
  58. * @param integer $transaction_id the transaction ID
  59. * @param mixed $user_id normally the WP user ID but could be something different if changed by an add-on
  60. */
  61. function it_exchange_guest_checkout_guest_has_transaction( $has_transaction, $transaction_id, $user_id ) {
  62. if ( ! it_exchange_doing_guest_checkout() )
  63. return $has_transaction;
  64. $transaction = it_exchange_get_transaction( $transaction_id );
  65. if ( empty( $transaction->cart_details->is_guest_checkout ) )
  66. return $has_transaction;
  67. if ( empty( $transaction->customer_id ) || $transaction->customer_id != $user_id )
  68. return $has_transaction;
  69. return true;
  70. }
  71. add_filter( 'it_exchange_customer_has_transaction', 'it_exchange_guest_checkout_guest_has_transaction', 10, 3 );
  72. /**
  73. * Continues the guest checkout session or ends it based on timeout
  74. *
  75. * @since 1.6.0
  76. *
  77. * @return void
  78. */
  79. function it_exchange_handle_guest_checkout_session() {
  80. // Abandon if also initing. We have another function hooked to template_redirect for that.
  81. if ( ! empty( $_POST['it-exchange-init-guest-checkout'] ) )
  82. return;
  83. $guest_session = it_exchange_get_cart_data( 'guest-checkout' );
  84. $guest_session = empty( $guest_session ) ? false : reset( $guest_session );
  85. // IF we don't have a guest session, return
  86. if ( ! $guest_session )
  87. return;
  88. // Grab guest session timeout value from settings
  89. $settings = it_exchange_get_option( 'addon-guest-checkout' );
  90. $timeout = empty( $settings['cart-expiration'] ) ? 15 : $settings['cart-expiration'];
  91. // Do some math.
  92. $expires = $guest_session + ( $timeout * 60 );
  93. /**
  94. * DISABLING SESSION TIMEOUTS FOR NOW
  95. if ( ( $expires ) <= time() ) {
  96. it_exchange_kill_guest_checkout_session();
  97. it_exchange_add_message( 'notice', __( 'Session has expired.', 'it-l10n-ithemes-exchange' ) );
  98. } else {
  99. it_exchange_guest_checkout_bump_session();
  100. }
  101. */
  102. it_exchange_guest_checkout_bump_session();
  103. }
  104. add_action( 'template_redirect', 'it_exchange_handle_guest_checkout_session', 9 );
  105. add_action( 'it_exchange_super_widget_ajax_top', 'it_exchange_handle_guest_checkout_session', 9 );
  106. /**
  107. * Modify customer billing address
  108. *
  109. * @since 1.6.0
  110. *
  111. * @param array $billing_address the billing address returned from customer meta
  112. * @return mixed
  113. */
  114. function it_exchange_guest_checkout_handle_billing_address( $address ) {
  115. if ( ! it_exchange_doing_guest_checkout() )
  116. return $address;
  117. if ( ! $guest_billing = it_exchange_get_cart_data( 'guest-billing-address' ) )
  118. $guest_billing = false;
  119. return $guest_billing;
  120. }
  121. add_filter( 'it_exchange_get_customer_billing_address', 'it_exchange_guest_checkout_handle_billing_address' );
  122. /**
  123. * Modify cart billing address
  124. *
  125. * @since 1.6.0
  126. *
  127. * @return array
  128. */
  129. function it_exchange_guest_checkout_handle_cart_billing_address( $cart_billing ) {
  130. if ( ! it_exchange_doing_guest_checkout() )
  131. return $cart_billing;
  132. if ( ! $guest_billing = it_exchange_get_cart_data( 'guest-billing-address' ) ) {
  133. foreach( $cart_billing as $key => $value ) {
  134. $guest_billing[$key] = '';
  135. }
  136. } else {
  137. $guest_billing = $guest_billing;
  138. }
  139. return $guest_billing;
  140. }
  141. add_filter( 'it_exchange_get_cart_billing_address', 'it_exchange_guest_checkout_handle_cart_billing_address' );
  142. /**
  143. * Do not update the Customer's Billing address if doing guest checkout. Add it to the session instead
  144. *
  145. * @since 1.6.0
  146. *
  147. * @param array $address the address array that is supposed to be added to the customer
  148. * @return array
  149. */
  150. function it_exchange_guest_checkout_handle_update_billing_address( $address ) {
  151. if ( ! it_exchange_doing_guest_checkout() )
  152. return $address;
  153. // Add the address to our cart
  154. it_exchange_update_cart_data( 'guest-billing-address', $address );
  155. // Return false so that the customer address doesn't get updated
  156. return false;
  157. }
  158. add_action( 'it_exchange_save_customer_billing_address', 'it_exchange_guest_checkout_handle_update_billing_address' );
  159. /**
  160. * Modify customer shipping address
  161. *
  162. * @since 1.6.0
  163. *
  164. * @param array $shipping_address the shipping address returned from customer meta
  165. * @return mixed
  166. */
  167. function it_exchange_guest_checkout_handle_shipping_address( $address ) {
  168. if ( ! it_exchange_doing_guest_checkout() )
  169. return $address;
  170. if ( ! $guest_shipping = it_exchange_get_cart_data( 'guest-shipping-address' ) )
  171. $guest_shipping = false;
  172. return $guest_shipping;
  173. }
  174. add_filter( 'it_exchange_get_customer_shipping_address', 'it_exchange_guest_checkout_handle_shipping_address' );
  175. /**
  176. * Returns the customer email for a guest transaction
  177. *
  178. * @since 1.6.0
  179. *
  180. * @param string $email the email passed through from the WP filter
  181. * @param mixed $transaction the id or the object
  182. */
  183. function it_exchange_get_guest_checkout_transaction_email( $email, $transaction ) {
  184. $transaction = it_exchange_get_transaction( $transaction );
  185. if ( empty( $transaction->cart_details->is_guest_checkout ) )
  186. return $email;
  187. return ! empty( $transaction->customer_id ) && is_email( $transaction->customer_id ) ? $transaction->customer_id : $email;
  188. }
  189. add_filter( 'it_exchange_get_transaction_customer_email', 'it_exchange_get_guest_checkout_transaction_email', 10, 2 );
  190. /**
  191. * Returns the customer id for a guest transaction
  192. *
  193. * @since 1.6.0
  194. *
  195. * @param string $id the id passed through from the WP filter
  196. * @param mixed $transaction the id or the object
  197. */
  198. function it_exchange_get_guest_checkout_transaction_id( $id, $transaction ) {
  199. $transaction = it_exchange_get_transaction( $transaction );
  200. if ( empty( $transaction->cart_details->is_guest_checkout ) )
  201. return $id;
  202. return ! empty( $transaction->customer_id ) && is_email( $transaction->customer_id ) ? $transaction->customer_id : $id;
  203. }
  204. add_filter( 'it_exchange_get_transaction_customer_id', 'it_exchange_get_guest_checkout_transaction_id', 10, 2 );
  205. /**
  206. * Do not print link to customer details on payment transactions admin page
  207. *
  208. * @since 1.6.0
  209. *
  210. * @param boolean $display_link yes or no
  211. * @param object $wp_post the wp post_type for the transaction
  212. * @return boolean
  213. */
  214. function it_exchange_hide_admin_customer_details_link_on_transaction_details_page( $display_link, $wp_post ) {
  215. if ( ! $transaction = it_exchange_get_transaction( $wp_post->ID ) )
  216. return $display_link;
  217. if ( ! empty( $transaction->cart_details->is_guest_checkout ) )
  218. return false;
  219. return $display_link;
  220. }
  221. add_filter( 'it_exchange_transaction_detail_has_customer_profile', 'it_exchange_hide_admin_customer_details_link_on_transaction_details_page', 10, 2 );
  222. /**
  223. * Modify cart shipping address
  224. *
  225. * @since 1.6.0
  226. *
  227. * @return array
  228. */
  229. function it_exchange_guest_checkout_handle_cart_shipping_address( $cart_shipping ) {
  230. if ( ! it_exchange_doing_guest_checkout() )
  231. return $cart_shipping;
  232. if ( ! $guest_shipping = it_exchange_get_cart_data( 'guest-shipping-address' ) ) {
  233. foreach( $cart_shipping as $key => $value ) {
  234. $guest_shipping[$key] = '';
  235. }
  236. } else {
  237. $guest_shipping = $guest_shipping;
  238. }
  239. return $guest_shipping;
  240. }
  241. add_filter( 'it_exchange_get_cart_shipping_address', 'it_exchange_guest_checkout_handle_cart_shipping_address' );
  242. /**
  243. * Do not update the Customer's shipping address if doing guest checkout. Add it to the session instead
  244. *
  245. * @since 1.6.0
  246. *
  247. * @param array $address the address array that is supposed to be added to the customer
  248. * @return array
  249. */
  250. function it_exchange_guest_checkout_handle_update_shipping_address( $address ) {
  251. if ( ! it_exchange_doing_guest_checkout() )
  252. return $address;
  253. // Add the address to our cart
  254. it_exchange_update_cart_data( 'guest-shipping-address', $address );
  255. // Return false so that the customer address doesn't get updated
  256. return false;
  257. }
  258. add_action( 'it_exchange_save_customer_shipping_address', 'it_exchange_guest_checkout_handle_update_shipping_address' );
  259. /**
  260. * Flags the user as someone who registered as a guest
  261. *
  262. * @since 1.6.0
  263. *
  264. * @param object $data custoemr data
  265. * @param int $customer_id the wp customer_id
  266. * @return object
  267. */
  268. function it_exchange_guest_checkout_set_customer_data( $data, $customer_id ) {
  269. // Set initial guest status on saved usermeta
  270. $data->registered_as_guest = (boolean) get_user_meta( $customer_id, 'it-exchange-registered-as-guest', true );
  271. return $data;
  272. }
  273. add_filter( 'it_exchange_set_customer_data', 'it_exchange_guest_checkout_set_customer_data', 10, 2 );
  274. /**
  275. * Flag transaction object as guest checkout
  276. *
  277. * @since 1.6.0
  278. *
  279. * @param object $transaction_object the transaction object right before being added to database
  280. * @return object
  281. */
  282. function it_exchange_flag_transaction_as_guest_checkout( $transaction_object ) {
  283. if ( ! it_exchange_doing_guest_checkout() )
  284. return $transaction_object;
  285. $transaction_object->is_guest_checkout = true;
  286. return $transaction_object;
  287. }
  288. add_filter( 'it_exchange_generate_transaction_object', 'it_exchange_flag_transaction_as_guest_checkout' );
  289. /**
  290. * Adds post meta to flag as guest checkout after its inserted into the DB
  291. *
  292. * So that we can filter it out of queries
  293. *
  294. * @since 1.6.0
  295. *
  296. * @param integer $transaction_id
  297. * @return void
  298. */
  299. function it_exchange_flag_transaction_post_as_guest_checkout( $transaction_id ) {
  300. $transaction = it_exchange_get_transaction( $transaction_id );
  301. if ( ! empty( $transaction->cart_details->is_guest_checkout ) )
  302. update_post_meta( $transaction_id, '_it-exchange-is-guest-checkout', true );
  303. return $transaction;
  304. }
  305. add_action( 'it_exchange_add_transaction_success', 'it_exchange_flag_transaction_post_as_guest_checkout' );
  306. /**
  307. * Removes guest checkout transactions from User Purchases
  308. *
  309. * If a registerd user checkouts as a guest rather than logging in, the transaction
  310. * is still attached to them but we don't want to show it to them in their front end profile.
  311. *
  312. * @since 1.6.0
  313. *
  314. * @param array $args wp post args used for the post query
  315. * @return array
  316. */
  317. function it_exchange_guest_checkout_filter_frontend_purchases( $args ) {
  318. if ( is_admin() || it_exchange_is_page( 'confirmation' ) )
  319. return $args;
  320. if ( empty( $args['meta_query'] ) )
  321. $args['meta_query'] = array();
  322. $args['meta_query'][] = array(
  323. 'key' => '_it-exchange-is-guest-checkout',
  324. 'compare' => 'NOT EXISTS',
  325. );
  326. return $args;
  327. }
  328. add_filter( 'it_exchange_get_transactions_get_posts_args', 'it_exchange_guest_checkout_filter_frontend_purchases' );
  329. /**
  330. * Modifies the Transaction Customer data when dealing with a guest checkout
  331. *
  332. * @since 1.6.0
  333. *
  334. * @param object $customer the customer object
  335. * @return object
  336. */
  337. function it_exchange_guest_checkout_modify_transaction_customer( $customer, $transaction ) {
  338. if ( empty( $transaction->cart_details->is_guest_checkout ) )
  339. return $customer;
  340. $customer = ( ! empty( $transaction->customer_id ) && is_email( $transaction->customer_id ) ) ? it_exchange_guest_checkout_generate_guest_user_object( $transaction->customer_id ) : false;
  341. if ( ! empty( $customer ) ) {
  342. $customer->wp_user = new stdClass();
  343. $customer->wp_user->display_name = __( 'Guest Customer', 'it-l10n-ithemes-exchange' );
  344. }
  345. return $customer;
  346. }
  347. add_filter( 'it_exchange_get_transaction_customer', 'it_exchange_guest_checkout_modify_transaction_customer', 10, 2 );
  348. /**
  349. * Modifies the Customer data when dealing with a guest checkout
  350. *
  351. * This modifies the feedback on the Checkout Page in the Logged-In purchse requirement
  352. *
  353. * @since 1.6.0
  354. *
  355. * @param object $customer the customer object
  356. * @return object
  357. */
  358. function it_exchange_guest_checkout_modify_customer( $customer ) {
  359. if ( ! it_exchange_doing_guest_checkout() || is_admin() )
  360. return $customer;
  361. $email = it_exchange_get_cart_data( 'guest-checkout-user' );
  362. $email = is_array( $email ) ? reset( $email ) : $email;
  363. $customer = it_exchange_guest_checkout_generate_guest_user_object( $email, true );
  364. return $customer;
  365. }
  366. add_filter( 'it_exchange_get_customer', 'it_exchange_guest_checkout_modify_customer' );
  367. /**
  368. * This modifies the loginout link generated by WP when we're doing Guest Checkout
  369. *
  370. * @since 1.6.0
  371. *
  372. * @param string $url the html for the loginout link
  373. * @param string $redirect the URL we're redirecting to after logged out.
  374. * @return string
  375. */
  376. function it_exchange_guest_checkout_modify_loginout_link( $url, $redirect ) {
  377. if ( ! it_exchange_doing_guest_checkout() )
  378. return $url;
  379. $url = add_query_arg( array( 'it-exchange-guest-logout' => 1 ), $redirect );
  380. return $url;
  381. }
  382. add_filter( 'logout_url', 'it_exchange_guest_checkout_modify_loginout_link', 10, 2 );
  383. /**
  384. * Logs out a guest checkout session
  385. *
  386. * @since 1.6.0
  387. *
  388. * @return void
  389. */
  390. function it_exchange_logout_guest_checkout_session() {
  391. if ( ( it_exchange_is_page( 'logout' ) && it_exchange_doing_guest_checkout() ) || ! empty( $_REQUEST['it-exchange-guest-logout'] ) ) {
  392. it_exchange_kill_guest_checkout_session();
  393. wp_redirect( remove_query_arg( 'it-exchange-guest-logout' ) );
  394. }
  395. }
  396. add_action( 'template_redirect', 'it_exchange_logout_guest_checkout_session', 1 );
  397. /**
  398. * Allow downloads to be served regardless of the requirement to be logged in if user checkout out as a guest
  399. *
  400. * @since 1.6.0
  401. *
  402. * @param boolean $setting the default setting
  403. * @param array $hash_data the download has data
  404. * @return boolean
  405. */
  406. function it_exchange_allow_file_downloads_for_guest_checkout( $setting, $hash_data ) {
  407. if ( ! $transaction = it_exchange_get_transaction( $hash_data['transaction_id'] ) )
  408. return $setting;
  409. return empty( $transaction->cart_details->is_guest_checkout ) ? $setting : false;
  410. }
  411. add_filter( 'it_exchange_require_user_login_for_download', 'it_exchange_allow_file_downloads_for_guest_checkout', 10, 2 );
  412. /**
  413. * Clear guest session when an authentication attemp happens.
  414. *
  415. * @since 1.6.0
  416. *
  417. * @param mixed $incoming Whatever is coming from WP hook API. We don't use it.
  418. * @return void
  419. */
  420. function it_exchange_end_guest_checkout_on_login_attempt( $incoming ) {
  421. if ( it_exchange_doing_guest_checkout() )
  422. it_exchange_kill_guest_checkout_session();
  423. return $incoming;
  424. }
  425. add_filter( 'authenticate', 'it_exchange_end_guest_checkout_on_login_attempt' );
  426. /**
  427. * Proccesses Guest login via superwidget
  428. *
  429. * @since 1.6.0
  430. *
  431. */
  432. function it_exchange_guest_checkout_process_ajax_login() {
  433. if ( empty( $_REQUEST['sw-action'] ) || 'guest-checkout' != $_REQUEST['sw-action'] || empty( $_POST['email'] ) ) {
  434. it_exchange_add_message( 'error', __( 'Please use a properly formatted email address.', 'it-l10n-ithemes-exchange' ) );
  435. die('0');
  436. }
  437. // Vaidate email address
  438. if ( ! is_email( $_POST['email'] ) ) {
  439. it_exchange_add_message( 'error', __( 'Please use a properly formatted email address.', 'it-l10n-ithemes-exchange' ) );
  440. die('0');
  441. }
  442. $customer_email = $_POST['email'];
  443. it_exchange_init_guest_checkout_session( $customer_email );
  444. die('1');
  445. }
  446. add_action( 'it_exchange_processing_super_widget_ajax_guest-checkout', 'it_exchange_guest_checkout_process_ajax_login' );
  447. /**
  448. * Remove the download page link in the email if this was a guest checkout transaction
  449. *
  450. * @since 1.6.0
  451. *
  452. * @param boolean $boolean incoming from WP Filter
  453. * @param int $id the transaction ID
  454. * @return boolean
  455. */
  456. function it_exchange_guest_checkout_maybe_remove_download_page_link_from_email( $boolean, $id ) {
  457. if ( ! $transaction = it_exchange_get_transaction( $id ) )
  458. return $boolean;
  459. return empty( $transaction->cart_details->is_guest_checkout );
  460. }
  461. add_filter( 'it_exchange_print_downlods_page_link_in_email', 'it_exchange_guest_checkout_maybe_remove_download_page_link_from_email', 10, 2 );
  462. /**
  463. * Filter email for sending if its false and we're transaction was a guest checkout
  464. *
  465. * @since 1.7.12
  466. *
  467. * @param string $to_email the email address we're sending it to
  468. * @param object $transaction the transaction object
  469. * @return string
  470. */
  471. function it_exchange_guest_checkout_modify_confirmation_email_address( $to_email, $transaction ) {
  472. if ( ! empty( $to_email ) || empty( $transaction->cart_details->is_guest_checkout ) )
  473. return $to_email;
  474. return is_email( $transaction->customer_id ) ? $transaction->customer_id : '';
  475. }
  476. add_filter( 'it_exchange_send_purchase_emails_to', 'it_exchange_guest_checkout_modify_confirmation_email_address', 10, 2 );