plug_woohook.php 8.6 KB

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