PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

https://gitlab.com/Blueprint-Marketing/helpscout-edd
PHP | 191 lines | 115 code | 33 blank | 43 comment | 19 complexity | ae1cc296cde54736d970c69dddb2f913 MD5 | raw file
  1. <?php
  2. /**
  3. * HelpScout EDD integration.
  4. *
  5. * This code is based in large part on an example provided by HelpScout and then modified for Easy Digital Downloads and WP.
  6. */
  7. // We use core, so we include it.
  8. require '../wp-load.php';
  9. // Require the settings file for the secret key
  10. require './settings.php';
  11. class PluginHandler {
  12. private $input = false;
  13. /**
  14. * Returns the requested HTTP header.
  15. *
  16. * @param string $header
  17. * @return bool|string
  18. */
  19. private function getHeader( $header ) {
  20. if ( isset( $_SERVER[$header] ) ) {
  21. return $_SERVER[$header];
  22. }
  23. return false;
  24. }
  25. /**
  26. * Retrieve the JSON input
  27. *
  28. * @return bool|string
  29. */
  30. private function getJsonString() {
  31. if ( $this->input === false ) {
  32. $this->input = @file_get_contents( 'php://input' );
  33. }
  34. return $this->input;
  35. }
  36. /**
  37. * Generate the signature based on the secret key, to compare in isSignatureValid
  38. *
  39. * @return bool|string
  40. */
  41. private function generateSignature() {
  42. $str = $this->getJsonString();
  43. if ( $str ) {
  44. return base64_encode( hash_hmac( 'sha1', $str, HELPSCOUT_SECRET_KEY, true ) );
  45. }
  46. return false;
  47. }
  48. /**
  49. * Returns true if the current request is a valid webhook issued from Help Scout, false otherwise.
  50. *
  51. * @return boolean
  52. */
  53. private function isSignatureValid() {
  54. $signature = $this->generateSignature();
  55. return $signature == $this->getHeader( 'HTTP_X_HELPSCOUT_SIGNATURE' );
  56. }
  57. /**
  58. * Create a response.
  59. *
  60. * @return array
  61. */
  62. public function getResponse() {
  63. $ret = array( 'html' => '' );
  64. if ( !$this->isSignatureValid() ) {
  65. return array( 'html' => 'Invalid signature' );
  66. }
  67. $data = json_decode( $this->input, true );
  68. // do some stuff
  69. $ret['html'] = $this->fetchHtml( $data );
  70. // Used for debugging
  71. // $ret['html'] = '<pre>'.print_r($data,1).'</pre>' . $ret['html'];
  72. return $ret;
  73. }
  74. /**
  75. * Generate output for the response.
  76. *
  77. * @param $data
  78. * @return string
  79. */
  80. private function fetchHtml( $data ) {
  81. global $wpdb;
  82. if ( isset( $data['customer']['emails'] ) && is_array( $data['customer']['emails'] ) ) {
  83. $email_query = "IN (";
  84. foreach ( $data['customer']['emails'] as $email ) {
  85. $email_query .= "'" . $email . "',";
  86. }
  87. $email_query = rtrim( $email_query, ',' );
  88. $email_query .= ')';
  89. } else {
  90. $email_query = "= '" . $data['customer']['email'] . "'";
  91. }
  92. $query = "SELECT pm2.post_id, pm2.meta_value, p.post_status FROM $wpdb->postmeta pm, $wpdb->postmeta pm2, $wpdb->posts p WHERE pm.meta_key = '_edd_payment_user_email' AND pm.meta_value $email_query AND pm.post_id = pm2.post_id AND pm2.meta_key = '_edd_payment_meta' AND pm.post_id = p.ID AND p.post_status NOT IN ('failed','revoked') ORDER BY pm.post_id DESC";
  93. $results = $wpdb->get_results( $query );
  94. if ( !$results ) {
  95. $query = "SELECT pm.post_id, pm.meta_value, p.post_status FROM $wpdb->postmeta pm, $wpdb->posts p WHERE pm.meta_key = '_edd_payment_meta' AND pm.meta_value LIKE '%%" . $data['customer']['fname'] . "%%' AND pm.meta_value LIKE '%%" . $data['customer']['lname'] . "%%' AND pm.post_id = p.ID AND p.post_status NOT IN ('failed','revoked') ORDER BY pm.post_id DESC";
  96. $results = $wpdb->get_results( $query );
  97. }
  98. if ( !$results ) {
  99. return 'No license data found.';
  100. }
  101. $orders = array();
  102. foreach ( $results as $result ) {
  103. $order = array();
  104. $order['link'] = '<a target="_blank" href="' . get_admin_url( null, 'edit.php?post_type=download&page=edd-payment-history&view=edit-payment&purchase_id=' . $result->post_id ) . '">#' . $result->post_id . '</a>';
  105. $post = get_post( $result->post_id );
  106. $purchase = maybe_unserialize( $result->meta_value );
  107. $order['date'] = $post->post_date;
  108. unset( $post );
  109. $order['id'] = $result->post_id;
  110. $order['status'] = $result->post_status;
  111. $order['amount'] = edd_get_payment_amount( $result->post_id );
  112. $order['payment_method'] = edd_get_payment_gateway( $result->post_id );
  113. if ( 'paypal' == $order['payment_method'] ) {
  114. // Grab the PayPal transaction ID and link the transaction to PayPal
  115. $notes = edd_get_payment_notes( $result->post_id );
  116. foreach ( $notes as $note ) {
  117. if ( preg_match( '/^PayPal Transaction ID: ([^\s]+)/', $note->comment_content, $match ) )
  118. $order['paypal_transaction_id'] = $match[1];
  119. }
  120. $order['payment_method'] = '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=' . $order['paypal_transaction_id'] . '" target="_blank">PayPal</a>';
  121. }
  122. $downloads = maybe_unserialize( $purchase['downloads'] );
  123. if ( $downloads ) {
  124. $license_keys = '';
  125. foreach ( maybe_unserialize( $purchase['downloads'] ) as $download ) {
  126. $id = isset( $purchase['cart_details'] ) ? $download['id'] : $download;
  127. $licensing = new EDD_Software_Licensing();
  128. if ( get_post_meta( $id, '_edd_sl_enabled', true ) ) {
  129. $license = $licensing->get_license_by_purchase( $order['id'], $id );
  130. $license_keys .= '<strong>' . str_replace( " for WordPress", "", get_the_title( $id ) ) . "</strong><br/>"
  131. . edd_get_price_option_name( $id, $download['options']['price_id'] ) . '<br/>'
  132. . get_post_meta( $license->ID, '_edd_sl_key', true ) . '<br/><br/>';
  133. }
  134. }
  135. }
  136. if ( isset( $license_keys ) )
  137. $order['downloads'][] = $license_keys;
  138. $orders[] = $order;
  139. }
  140. $output = '';
  141. foreach ( $orders as $order ) {
  142. $output .= '<strong><i class="icon-cart"></i> ' . $order['link'] . '</strong>';
  143. if ( $order['status'] != 'publish' )
  144. $output .= ' - <span style="color:orange;font-weight:bold;">' . $order['status'] . '</span>';
  145. $output .= '<p><span class="muted">' . $order['date'] . '</span><br/>';
  146. $output .= '$' . $order['amount'] . ' - ' . $order['payment_method'] . '</p>';
  147. $output .= '<p><i class="icon-pointer"></i><a target="_blank" href="' . add_query_arg( array( 'edd-action' => 'email_links', 'purchase_id' => $order['id'] ), admin_url( 'edit.php?post_type=download&page=edd-payment-history' ) ) . '">' . __( 'Resend Purchase Receipt', 'edd' ) . '</a></p>';
  148. $output .= '<ul>';
  149. foreach ( $order['downloads'] as $download ) {
  150. $output .= '<li>' . $download . '</li>';
  151. }
  152. $output .= '</ul>';
  153. }
  154. return $output;
  155. }
  156. }
  157. $plugin = new PluginHandler();
  158. echo json_encode( $plugin->getResponse() );