plug_woohook.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. if (!class_exists("plug_wookook")) {
  3. if (!function_exists("sanitize_username")) {
  4. function sanitize_username($username) {
  5. $parts = explode("@", $username);
  6. if (count($parts) == 2) {
  7. $username = $parts[0];
  8. }
  9. return $username;
  10. }
  11. }
  12. class plug_wookook {
  13. function __construct($ns) {
  14. $this->title = __("plug_wookook", "dagsopt");
  15. $this->pluginname = $ns->pluginname;
  16. $this->file = $ns->file;
  17. $this->ns = $ns;
  18. }
  19. function start() {
  20. add_action('init', array(&$this, 'xx__update_custom_roles'));
  21. add_action('wp_loaded', array(&$this, 'header'), 1);
  22. add_role("suspenderet-abonnent", "Suspenderet abonnent", array('read' => true, 'level_0' => false));
  23. add_role("betalende-abonnent", "Betalende abonnent", array('read' => true, 'level_0' => true));
  24. add_action('manage_users_custom_column', array(&$this, 'imel_custom_column_userfield'), 15, 3);
  25. add_filter('manage_users_columns', array(&$this, 'imel_column_userfield'), 15, 1);
  26. }
  27. function imel_column_userfield($defaults) {
  28. $defaults['abostatus'] = __('Abo Status', 'id-column');
  29. $defaults['aboproduct'] = __('Abo Product', 'id-column');
  30. return $defaults;
  31. }
  32. function imel_custom_column_userfield($value, $column_name, $id) {
  33. if ($column_name == 'aboproduct') {
  34. $burl = get_option($this->pluginname . "_woohook_baseurl_order");
  35. $pid = get_user_meta($id, 'abo_prod', true);
  36. $s = '<a href="' . $burl . '' . $pid . '">' . $pid . '</a>';
  37. return $s;
  38. }
  39. if ($column_name == 'abostatus') {
  40. $s = get_user_meta($id, 'abo_status', true);
  41. $s .= " " . get_user_meta($id, 'abo_statuscode', true);
  42. return $s;
  43. }
  44. }
  45. function xx__update_custom_roles() {
  46. if (get_option('custom_roles_version') < 3) {
  47. // add_role( "suspenderet-abonnent", "Suspenderet abonnent", array( 'read' => true, 'level_0' => false ) );
  48. // add_role( "betalende-abonnent", "Betalende abonnent", array( 'read' => true, 'level_0' => true ) );
  49. update_option('custom_roles_version', 3);
  50. }
  51. }
  52. function header() {
  53. if (isset($_REQUEST['woohook'])) {
  54. $hh = apache_request_headers();
  55. if (isset($hh["X-WC-Webhook-Source"])) {
  56. $shopurl = $hh["X-WC-Webhook-Source"];
  57. } else {
  58. $shopurl = null;
  59. }
  60. $thecode = get_option($this->pluginname . "_wookook_code", false);
  61. $exitrole = trim(get_option($this->pluginname . "_wookook_exitrole", false));
  62. if ($_REQUEST['woohook'] !== "$thecode") {
  63. die("code not right");
  64. }
  65. $rr = file_get_contents('php://input');
  66. //$itemnumbers = explode(",",);
  67. $itemroles = array();
  68. $lins = explode("\n", get_option($this->pluginname . "_wookook_item_roles", false));
  69. foreach ($lins as $nn => $line) {
  70. if (stristr($line, " > ")) {
  71. $a = explode(" > ", $line);
  72. $nums = explode(" ", $a[0]);
  73. $role = $a[1];
  74. foreach ($nums as $pp => $itemnum) {
  75. if (strlen($role) > 0) {
  76. $itemroles[trim($itemnum)] = trim($role);
  77. }
  78. }
  79. }
  80. }
  81. $found = false;
  82. try {
  83. $order = json_decode($rr, true);
  84. if (isset($order['line_items'])) {
  85. foreach ($order['line_items'] as $num => $line) {
  86. if (isset($itemroles["" . $line['product_id']])) {
  87. $found = true;
  88. $therole = $itemroles["" . $line['product_id']];
  89. $theproduct = $line['product_id'];
  90. }
  91. }
  92. }
  93. } catch (Exception $e) {
  94. echo 'Caught exception: ', $e->getMessage(), "\n";
  95. }
  96. $active = false;
  97. if (isset($order['status'])) {
  98. if ($order['status'] == "active") {
  99. $found = true;
  100. $active = true;
  101. } else if ($order['status'] == "processing") {
  102. if (isset($order['date_paid_gmt']) && strlen($order['date_paid_gmt']) > 0) {
  103. $found = true;
  104. $active = true;
  105. } else {
  106. $found = false;
  107. }
  108. } else if ($order['status'] == "on-hold") {
  109. $active = false;
  110. } else if ($order['status'] == "completed") {
  111. if (isset($order['date_paid_gmt']) && strlen($order['date_paid_gmt']) > 0) {
  112. $active = true;
  113. } else {
  114. $active = false;
  115. }
  116. }
  117. }
  118. // echo($order['status']."::".$found."::".$active." : ".$order['date_paid']." : ".$order['date_paid_gmt']);
  119. // die("\n\nDONE");
  120. $testmode = false;
  121. if ($found) {
  122. $resu = array();
  123. $user_email = $order['billing']['email'];
  124. $user_name = $user_email; //sanitize_username("O".$order['id']);
  125. $user_id = username_exists($user_name);
  126. if (!$user_id && false == email_exists($user_email)) {
  127. $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
  128. if ($testmode) {
  129. $resu[] = "$user_id mock created : $user_name $user_email ";
  130. } else {
  131. $user_id = wp_create_user($user_name, $random_password, $user_email);
  132. $xrole = $active ? $therole : $exitrole;
  133. wp_update_user(array('ID' => $user_id, 'role' => $xrole, 'first_name' => $order['billing']['first_name'], 'last_name' => $order['billing']['last_name']));
  134. update_user_meta($user_id, 'abo_status', "ACTIVE");
  135. update_user_meta($user_id, 'abo_statuscode', $order['id']);
  136. update_user_meta($user_id, 'abo_prod', $theproduct);
  137. wp_send_new_user_notifications($user_id, 'both');
  138. $resu[] = "CREATED: " . $order['status'] . " $active :: $theproduct :: $user_id or $user_email exists role > '$xrole'";
  139. }
  140. } else {
  141. $xrole = $active ? $therole : $exitrole;
  142. wp_update_user(array('ID' => $user_id, 'role' => $xrole, 'first_name' => $order['billing']['first_name'], 'last_name' => $order['billing']['last_name']));
  143. $u = new WP_User($user_id);
  144. $u->set_role($xrole);
  145. update_user_meta($user_id, 'abo_status', $active ? "ACTIVE" : "SUSPENDED");
  146. update_user_meta($user_id, 'abo_statuscode', $order['id']);
  147. update_user_meta($user_id, 'abo_prod', $theproduct);
  148. $resu[] = "UPDATED: " . $order['status'] . " $active :: $theproduct :: $user_id or $user_email exists role > '$xrole'";
  149. }
  150. $aemail = get_bloginfo('admin_email');
  151. $rr = wp_mail($aemail, 'CCR ' . count($resu) . " : " . getenv('HOSTNAME') . " : " . $_SERVER['REMOTE_ADDR'] . " : " . $_SERVER['SERVER_ADDR'] . " : " . $_SERVER['SERVER_NAME'] . " " . date("U"), "::" . print_r($resu, true));
  152. echo ("DONE live");
  153. print_r($resu);
  154. die("done $rr");
  155. } else {
  156. die("no action done");
  157. }
  158. }
  159. }
  160. function help() {
  161. ?>
  162. plug_wookook
  163. <?php
  164. }
  165. function Option($pre) {
  166. //update_option ( $pre.'_items', $_POST [ $pre.'_items' ] );
  167. update_option($pre . '_code', $_POST[$pre . '_code']);
  168. update_option($pre . '_exitrole', $_POST[$pre . '_exitrole']);
  169. update_option($pre . '_item_roles', $_POST[$pre . '_item_roles']);
  170. update_option($pre . '_baseurl_product', $_POST[$pre . '_baseurl_product']);
  171. update_option($pre . '_baseurl_order', $_POST[$pre . '_baseurl_order']);
  172. }
  173. function admin_line($pre) {
  174. ?>
  175. <?php echo (__("Shop baseurl products:", "dagsopt")) ?><br>
  176. <input name="<?php echo ($pre . '_baseurl_product'); ?>" value="<?php echo (stripcslashes(get_option($pre . "_baseurl_product"))); ?>">
  177. <?php echo (__("Shop baseurl orders:", "dagsopt")) ?><br>
  178. <input name="<?php echo ($pre . '_baseurl_order'); ?>" value="<?php echo (stripcslashes(get_option($pre . "_baseurl_order"))); ?>">
  179. <?php echo (__("Itemnumber1 ... ItemnumberN > Role:", "dagsopt")) ?><br>
  180. <textarea name="<?php echo ($pre . '_item_roles'); ?>" rows="7" class="large-text code"><?php echo (stripcslashes(get_option($pre . "_item_roles"))); ?></textarea>
  181. <?php echo (__("Code:", "dagsopt")) ?><br>
  182. <textarea name="<?php echo ($pre . '_code'); ?>" rows="2" class="large-text code"><?php echo (stripcslashes(get_option($pre . "_code"))); ?></textarea>
  183. <?php echo (__("Exitrole:", "dagsopt")) ?><br>
  184. <input name="<?php echo ($pre . '_exitrole'); ?>" value="<?php echo (stripcslashes(get_option($pre . "_exitrole"))); ?>">
  185. <?php
  186. }
  187. }
  188. global $plug_wookook;
  189. $plug_wookook = new plug_wookook($this);
  190. $this->dagsopt['plug_wookook'] = $plug_wookook;
  191. }