plug_woohook.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. try {
  98. $order = json_decode($rr, true);
  99. if (isset($order['line_items'])) {
  100. foreach ($order['line_items'] as $num => $line) {
  101. if (isset($itemroles["" . $line['product_id']])) {
  102. $found = true;
  103. $therole = $itemroles["" . $line['product_id']];
  104. $theproduct = $line['product_id'];
  105. }
  106. }
  107. }
  108. } catch (Exception $e) {
  109. echo 'Caught exception: ', $e->getMessage(), "\n";
  110. }
  111. $active = false;
  112. if (isset($order['status'])) {
  113. if ($order['status'] == "active") {
  114. $found = true;
  115. $active = true;
  116. } else if ($order['status'] == "processing") {
  117. if (isset($order['date_paid_gmt']) && strlen($order['date_paid_gmt']) > 0) {
  118. $found = true;
  119. $active = true;
  120. } else {
  121. $found = false;
  122. }
  123. } else if ($order['status'] == "on-hold") {
  124. $active = false;
  125. } else if ($order['status'] == "completed") {
  126. if (isset($order['date_paid_gmt']) && strlen($order['date_paid_gmt']) > 0) {
  127. $active = true;
  128. } else {
  129. $active = false;
  130. }
  131. }
  132. }
  133. // echo($order['status']."::".$found."::".$active." : ".$order['date_paid']." : ".$order['date_paid_gmt']);
  134. // die("\n\nDONE");
  135. $testmode = false;
  136. if ($found) {
  137. $resu = array();
  138. $user_email = $order['billing']['email'];
  139. $user_name = $user_email; //sanitize_username("O".$order['id']);
  140. $user_id = username_exists($user_name);
  141. if (!$user_id && false == email_exists($user_email)) {
  142. $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
  143. if ($testmode) {
  144. $resu[] = "$user_id mock created : $user_name $user_email ";
  145. } else {
  146. $user_id = wp_create_user($user_name, $random_password, $user_email);
  147. $xrole = $active ? $therole : $exitrole;
  148. wp_update_user(array('ID' => $user_id, 'role' => $xrole, 'first_name' => $order['billing']['first_name'], 'last_name' => $order['billing']['last_name']));
  149. update_user_meta($user_id, 'abo_status', $order['status'] . " " . ($active ? " " : " SUSPENDED"));
  150. update_user_meta($user_id, 'abo_order', $order['id']);
  151. update_user_meta($user_id, 'abo_prod', $theproduct);
  152. wp_send_new_user_notifications($user_id, 'both');
  153. $resu[] = "CREATED: " . $order['status'] . " $active :: $theproduct :: $user_id or $user_email exists role > '$xrole'";
  154. }
  155. } else {
  156. $xrole = $active ? $therole : $exitrole;
  157. wp_update_user(array('ID' => $user_id, 'role' => $xrole, 'first_name' => $order['billing']['first_name'], 'last_name' => $order['billing']['last_name']));
  158. $u = new WP_User($user_id);
  159. $u->set_role($xrole);
  160. update_user_meta($user_id, 'abo_status', $order['status'] . " " . ($active ? " " : " SUSPENDED"));
  161. update_user_meta($user_id, 'abo_order', $order['id']);
  162. update_user_meta($user_id, 'abo_prod', $theproduct);
  163. $resu[] = "UPDATED: " . $order['status'] . " $active :: $theproduct :: $user_id or $user_email exists role > '$xrole'";
  164. }
  165. $aemail = get_bloginfo('admin_email');
  166. $rr = wp_mail($aemail, 'CCR ' . count($resu) . " : " . getenv('HOSTNAME') . " : " . $_SERVER['REMOTE_ADDR'] . " : " . $_SERVER['SERVER_ADDR'] . " : " . $_SERVER['SERVER_NAME'] . " " . date("U"), "::" . print_r($resu, true));
  167. echo ("DONE live");
  168. print_r($resu);
  169. die("done $rr");
  170. } else {
  171. die("no action done");
  172. }
  173. }
  174. }
  175. function help() {
  176. ?>
  177. plug_wookook
  178. <?php
  179. }
  180. function Option($pre) {
  181. //update_option ( $pre.'_items', $_POST [ $pre.'_items' ] );
  182. update_option($pre . '_code', $_POST[$pre . '_code']);
  183. update_option($pre . '_exitrole', $_POST[$pre . '_exitrole']);
  184. update_option($pre . '_item_roles', $_POST[$pre . '_item_roles']);
  185. update_option($pre . '_baseurl_product', $_POST[$pre . '_baseurl_product']);
  186. update_option($pre . '_baseurl_order', $_POST[$pre . '_baseurl_order']);
  187. }
  188. function admin_line($pre) {
  189. ?>
  190. <hr>
  191. <?php echo (__("Shop baseurl products:", "dagsopt")) ?><br>
  192. <input name="<?php echo ($pre . '_baseurl_product'); ?>" class="large-text " value="<?php echo (stripcslashes(get_option($pre . "_baseurl_product"))); ?>">
  193. <hr>
  194. <?php echo (__("Shop baseurl orders:", "dagsopt")) ?><br>
  195. <input name="<?php echo ($pre . '_baseurl_order'); ?>" class="large-text " value="<?php echo (stripcslashes(get_option($pre . "_baseurl_order"))); ?>">
  196. <hr>
  197. <?php echo (__("Itemnumber1 ... ItemnumberN > Role:", "dagsopt")) ?><br>
  198. <textarea name="<?php echo ($pre . '_item_roles'); ?>" rows="7" class="large-text code"><?php echo (stripcslashes(get_option($pre . "_item_roles"))); ?></textarea>
  199. <?php echo (__("Code:", "dagsopt")) ?><br>
  200. <textarea name="<?php echo ($pre . '_code'); ?>" rows="2" class="large-text code"><?php echo (stripcslashes(get_option($pre . "_code"))); ?></textarea>
  201. <?php echo (__("Exitrole:", "dagsopt")) ?><br>
  202. <input name="<?php echo ($pre . '_exitrole'); ?>" value="<?php echo (stripcslashes(get_option($pre . "_exitrole"))); ?>">
  203. <?php
  204. }
  205. }
  206. global $plug_wookook;
  207. $plug_wookook = new plug_wookook($this);
  208. $this->dagsopt['plug_wookook'] = $plug_wookook;
  209. }