plug_woohook.php 8.4 KB

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