PageRenderTime 22ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/php/functions-checkout.php

https://gitlab.com/Blueprint-Marketing/Yoast-theme-public
PHP | 353 lines | 205 code | 57 blank | 91 comment | 29 complexity | 603c51b6ebf8a5f1f6e6b5a25b526580 MD5 | raw file
  1. <?php
  2. /**
  3. * @package Yoast\YoastCom
  4. */
  5. namespace Yoast\YoastCom\Theme;
  6. /**
  7. * Echoes the title for a checkout item
  8. *
  9. * @param int $post_id Post ID to echo the checkout title for.
  10. */
  11. function the_checkout_item_title( $post_id = null ) {
  12. if ( null === $post_id ) {
  13. $post_id = get_the_ID();
  14. }
  15. $item_title = esc_html( get_the_title( $post_id ) );
  16. if ( stripos( $item_title, 'for ' ) ) {
  17. $item_title = str_replace( ' for ', '</strong> for ', $item_title );
  18. }
  19. else {
  20. $item_title .= '</strong>';
  21. }
  22. echo '<strong>' . $item_title . '</strong>';
  23. }
  24. /**
  25. * Outputs one or more images for a certain gateway
  26. *
  27. * @param array $gateway The gateway to output the image for.
  28. */
  29. function the_gateway_image( $gateway ) {
  30. $image = '';
  31. switch ( $gateway['admin_label'] ) {
  32. case 'PayPal Express':
  33. case 'PayPal Standard':
  34. $image = <<<'HTML'
  35. <img class="edd_payment__method" alt="PayPal" src="%1$s/Paypal_90x24_x2.png" width="90" height="24"/>
  36. HTML;
  37. break;
  38. case 'Stripe':
  39. $image = <<<'HTML'
  40. <img class="edd_payment__method" alt="MasterCard" src="%1$s/MasterCard_66x40_x2.png" width="66" height="40"/>
  41. <img class="edd_payment__method" alt="Visa" src="%1$s/Visa_59x20_x2.png" width="59" height="20"/>
  42. <img class="edd_payment__method" alt="American Express" src="%1$s/AMEX_50x44_x2.png" width="50" height="44"/>
  43. HTML;
  44. break;
  45. }
  46. $image = sprintf( $image, get_template_directory_uri() . '/images/shop' );
  47. echo $image;
  48. }
  49. /**
  50. * Returns the EDD step we are on at this moment
  51. *
  52. * @return int
  53. */
  54. function get_checkout_step() {
  55. global $edd_options;
  56. $step = 2;
  57. if ( filter_input( INPUT_GET, 'payment-mode' ) ) {
  58. $step = 3;
  59. }
  60. else if ( is_page( $edd_options['success_page'] ) ) {
  61. $step = 4;
  62. }
  63. return $step;
  64. }
  65. /**
  66. * Returns the discounts for a certain download, returns an empty array if no discounts apply to this product.
  67. *
  68. * @param int $download_id The ID for the EDD download.
  69. *
  70. * @return array A list of discounts for the download.
  71. */
  72. function download_get_discounts( $download_id ) {
  73. $discounts = edd_get_cart_discounts();
  74. if ( empty( $discounts ) ) {
  75. $discounts = array();
  76. }
  77. $discounts = array_map( function( $discount_code ) {
  78. $discount_id = edd_get_discount_id_by_code( $discount_code );
  79. return array(
  80. 'code' => $discount_code,
  81. 'id' => $discount_id,
  82. 'name' => get_post_meta( $discount_id, '_edd_discount_name', true ),
  83. );
  84. }, $discounts );
  85. return array_filter( $discounts, function( $discount ) use ( $download_id ) {
  86. $requirements = edd_get_discount_product_reqs( $discount['id'] );
  87. $excluded = edd_get_discount_excluded_products( $discount['id'] );
  88. return ( in_array( $download_id, $requirements ) && ! in_array( $download_id, $excluded ) );
  89. });
  90. }
  91. /**
  92. * Returns whether or not a download has a discount
  93. *
  94. * @param array $item A cart item array.
  95. *
  96. * @return bool
  97. */
  98. function download_has_discount( $item ) {
  99. $discount_amount = edd_get_cart_item_discount_amount( $item );
  100. return 0 !== $discount_amount;
  101. }
  102. /**
  103. * Returns the discounts that are global to the cart
  104. *
  105. * @return array A list of discounts that are global to the cart.
  106. */
  107. function get_global_discounts() {
  108. $discounts = edd_get_cart_discounts();
  109. if ( empty( $discounts ) ) {
  110. $discounts = array();
  111. }
  112. $discounts = array_map( function( $discount_code ) {
  113. $discount_id = edd_get_discount_id_by_code( $discount_code );
  114. return array(
  115. 'code' => $discount_code,
  116. 'id' => $discount_id,
  117. 'name' => get_post_meta( $discount_id, '_edd_discount_name', true ),
  118. );
  119. }, $discounts );
  120. return array_filter( $discounts, function( $discount ) {
  121. return ! edd_is_discount_not_global( $discount['id'] );
  122. } );
  123. }
  124. /**
  125. * Returns whether or not the given download is a renewal
  126. *
  127. * @return bool
  128. */
  129. function cart_has_renewal_discount() {
  130. if ( ! edd_sl_renewals_allowed() ) {
  131. return false;
  132. }
  133. if ( ! EDD()->session->get( 'edd_is_renewal' ) ) {
  134. return false;
  135. }
  136. $renewal_discount = cart_get_renewal_discount();
  137. return ! empty( $renewal_discount );
  138. }
  139. /**
  140. * Returns the renewal object
  141. *
  142. * @return float ID of the renewal discount
  143. */
  144. function cart_get_renewal_discount() {
  145. $renewal_discount = (float) edd_get_option( 'edd_sl_renewal_discount', false );
  146. if ( $renewal_discount < 1 ) {
  147. $renewal_discount *= 100;
  148. }
  149. return $renewal_discount;
  150. }
  151. /**
  152. * Returns whether or not the cart actually has taxes (ea. more then zero)
  153. *
  154. * @return bool
  155. */
  156. function cart_has_tax() {
  157. return ( 0.0 !== (float) edd_get_cart_tax() );
  158. }
  159. /**
  160. * Returns whether or not the current cart is a renewal
  161. *
  162. * @return bool
  163. */
  164. function cart_is_renewal() {
  165. return ! ! EDD()->session->get( 'edd_is_renewal' );
  166. }
  167. /**
  168. * Checks whether an error is set for $field.
  169. *
  170. * @param string $field The field to check.
  171. *
  172. * @return bool
  173. */
  174. function has_edd_error( $field ) {
  175. $errors = edd_get_errors();
  176. if ( isset( $errors[ $field ] ) ) {
  177. return trim( $errors[ $field ] );
  178. }
  179. return false;
  180. }
  181. /**
  182. * Returns an array filled with customer data
  183. *
  184. * @return array {
  185. * @type string $first_name The customer's first name.
  186. * @type string $last_name The customer's last name.
  187. * @type string $email The customer's email address.
  188. * }
  189. */
  190. function get_customer() {
  191. $customer = EDD()->session->get( 'customer' );
  192. $customer = wp_parse_args( $customer, array( 'first_name' => '', 'last_name' => '', 'email' => '' ) );
  193. if ( is_user_logged_in() ) {
  194. $user_data = get_userdata( get_current_user_id() );
  195. foreach ( $customer as $key => $field ) {
  196. if ( 'email' === $key && empty( $field ) ) {
  197. $customer[ $key ] = $user_data->user_email;
  198. }
  199. elseif ( empty( $field ) ) {
  200. $customer[ $key ] = $user_data->$key;
  201. }
  202. }
  203. }
  204. $customer = array_map( 'sanitize_text_field', $customer );
  205. $convert = array(
  206. 'edd_first' => 'first_name',
  207. 'edd_last' => 'last_name',
  208. 'edd_email' => 'email',
  209. );
  210. // For some reason when a Stripe error occurs the `customer` session is empty, but `edd_purchase is filled`.
  211. $edd_purchase = EDD()->session->get( 'edd_purchase' );
  212. $edd_purchase = $edd_purchase['post_data'];
  213. if ( ! empty( $edd_purchase ) ) {
  214. foreach ( $convert as $from => $to ) {
  215. if ( array_key_exists( $from, $edd_purchase ) ) {
  216. $customer[ $to ] = $edd_purchase[ $from ];
  217. }
  218. }
  219. }
  220. // Also sometimes both customer and edd_purchase are empty but $_POST is filled with data.
  221. if ( ! empty( $_POST ) ) {
  222. foreach ( $convert as $from => $to ) {
  223. if ( array_key_exists( $from, $_POST ) ) {
  224. $customer[ $to ] = $_POST[ $from ];
  225. }
  226. }
  227. }
  228. return $customer;
  229. }
  230. /**
  231. * Returns an array filled with the customer address details
  232. *
  233. * @return array {
  234. * @type array $address {
  235. * @type string $line1 Address line 1.
  236. * @type string $line2 Address line 2.
  237. * @type string $city The customer's city.
  238. * @type string $zip The customer's zipcode.
  239. * @type string $state The customer's state.
  240. * @type string $country The customer's country.
  241. * }
  242. * }
  243. */
  244. function get_customer_address() {
  245. $logged_in = is_user_logged_in();
  246. $customer = EDD()->session->get( 'customer' );
  247. $customer = wp_parse_args( $customer, array(
  248. 'address' => array(
  249. 'line1' => '',
  250. 'line2' => '',
  251. 'city' => '',
  252. 'zip' => '',
  253. 'state' => '',
  254. 'country' => '',
  255. ),
  256. ) );
  257. $customer['address'] = array_map( 'sanitize_text_field', $customer['address'] );
  258. if ( $logged_in ) {
  259. $user_address = get_user_meta( get_current_user_id(), '_edd_user_address', true );
  260. foreach ( $customer['address'] as $key => $field ) {
  261. if ( empty( $field ) && ! empty( $user_address[ $key ] ) ) {
  262. $customer['address'][ $key ] = $user_address[ $key ];
  263. }
  264. else {
  265. $customer['address'][ $key ] = '';
  266. }
  267. }
  268. }
  269. $convert = array(
  270. 'card_address' => 'line1',
  271. 'card_address_2' => 'line2',
  272. 'card_city' => 'city',
  273. 'card_zip' => 'zip',
  274. 'card_state' => 'state',
  275. 'billing_country' => 'country',
  276. );
  277. // For some reason when a Stripe error occurs the `customer` session is empty, but `edd_purchase is filled`.
  278. $edd_purchase = EDD()->session->get( 'edd_purchase' );
  279. $edd_purchase = $edd_purchase['post_data'];
  280. if ( ! empty( $edd_purchase ) ) {
  281. foreach ( $convert as $from => $to ) {
  282. if ( array_key_exists( $from, $edd_purchase ) ) {
  283. $customer['address'][ $to ] = $edd_purchase[ $from ];
  284. }
  285. }
  286. }
  287. // Also sometimes both customer and edd_purchase are empty but $_POST is filled with data.
  288. if ( ! empty( $_POST ) ) {
  289. foreach ( $convert as $from => $to ) {
  290. if ( array_key_exists( $from, $_POST ) ) {
  291. $customer['address'][ $to ] = $_POST[ $from ];
  292. }
  293. }
  294. }
  295. return $customer;
  296. }