plug_woohook.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. }
  25. function xx__update_custom_roles() {
  26. if (get_option('custom_roles_version') < 3) {
  27. // add_role( "suspenderet-abonnent", "Suspenderet abonnent", array( 'read' => true, 'level_0' => false ) );
  28. // add_role( "betalende-abonnent", "Betalende abonnent", array( 'read' => true, 'level_0' => true ) );
  29. update_option('custom_roles_version', 3);
  30. }
  31. }
  32. function header() {
  33. if (isset($_REQUEST['woohook'])) {
  34. $thecode = get_option($this->pluginname . "_wookook_code", false);
  35. $exitrole = trim(get_option($this->pluginname . "_wookook_exitrole", false));
  36. if ($_REQUEST['woohook'] !== "$thecode") {
  37. die("code not right");
  38. }
  39. $rr = file_get_contents('php://input');
  40. //$itemnumbers = explode(",",);
  41. $itemroles = array();
  42. $lins = explode("\n", get_option($this->pluginname . "_wookook_item_roles", false));
  43. foreach ($lins as $nn => $line) {
  44. if (stristr($line, " > ")) {
  45. $a = explode(" > ", $line);
  46. $nums = explode(" ", $a[0]);
  47. $role = $a[1];
  48. foreach ($nums as $pp => $itemnum) {
  49. if (strlen($role) > 0) {
  50. $itemroles[trim($itemnum)] = trim($role);
  51. }
  52. }
  53. }
  54. }
  55. $found = false;
  56. try {
  57. $order = json_decode($rr, true);
  58. if (isset($order['line_items'])) {
  59. foreach ($order['line_items'] as $num => $line) {
  60. if (isset($itemroles["" . $line['product_id']])) {
  61. $found = true;
  62. $therole = $itemroles["" . $line['product_id']];
  63. }
  64. }
  65. }
  66. } catch (Exception $e) {
  67. echo 'Caught exception: ', $e->getMessage(), "\n";
  68. }
  69. $active = false;
  70. if (isset($order['status'])) {
  71. if ($order['status'] == "active") {
  72. $found = true;
  73. $active = true;
  74. } else if ($order['status'] == "processing") {
  75. if (isset($order['date_paid_gmt']) && strlen($order['date_paid_gmt']) > 0) {
  76. $found = true;
  77. $active = true;
  78. } else {
  79. $found = false;
  80. }
  81. } else if ($order['status'] == "on-hold") {
  82. $active = false;
  83. } else if ($order['status'] == "completed") {
  84. if (isset($order['date_paid_gmt']) && strlen($order['date_paid_gmt']) > 0) {
  85. $active = true;
  86. } else {
  87. $active = false;
  88. }
  89. }
  90. }
  91. // echo($order['status']."::".$found."::".$active." : ".$order['date_paid']." : ".$order['date_paid_gmt']);
  92. // die("\n\nDONE");
  93. $testmode = false;
  94. if ($found) {
  95. $resu = array();
  96. $user_email = $order['billing']['email'];
  97. $user_name = $user_email; //sanitize_username("O".$order['id']);
  98. $user_id = username_exists($user_name);
  99. if (!$user_id && false == email_exists($user_email)) {
  100. $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
  101. if ($testmode) {
  102. $resu[] = "$user_id mock created : $user_name $user_email ";
  103. } else {
  104. $user_id = wp_create_user($user_name, $random_password, $user_email);
  105. $xrole = $active ? $therole : $exitrole;
  106. wp_update_user(array('ID' => $user_id, 'role' => $xrole, 'first_name' => $order['billing']['first_name'], 'last_name' => $order['billing']['last_name']));
  107. update_user_meta($user_id, 'abo_status', "ACTIVE");
  108. update_user_meta($user_id, 'abo_statuscode', $order['id']);
  109. wp_send_new_user_notifications($user_id, 'both');
  110. $resu[] = "CREATED: " . $order['status'] . " $active $user_id or $user_email exists role > '$xrole'";
  111. }
  112. } else {
  113. $xrole = $active ? $therole : $exitrole;
  114. wp_update_user(array('ID' => $user_id, 'role' => $xrole, 'first_name' => $order['billing']['first_name'], 'last_name' => $order['billing']['last_name']));
  115. $u = new WP_User($user_id);
  116. $u->set_role($xrole);
  117. update_user_meta($user_id, 'abo_status', $active ? "ACTIVE" : "SUSPENDED");
  118. update_user_meta($user_id, 'abo_statuscode', $order['id']);
  119. $resu[] = "UPDATED: " . $order['status'] . " $active $user_id or $user_email exists role > '$xrole'";
  120. }
  121. $aemail = get_bloginfo('admin_email');
  122. $rr = wp_mail($aemail, 'CCR ' . count($resu) . " : " . getenv('HOSTNAME') . " : " . $_SERVER['REMOTE_ADDR'] . " : " . $_SERVER['SERVER_ADDR'] . " : " . $_SERVER['SERVER_NAME'] . " " . date("U"), "::" . print_r($resu, true));
  123. echo ("DONE live");
  124. print_r($resu);
  125. die("done $rr");
  126. } else {
  127. die("no action done");
  128. }
  129. }
  130. }
  131. function help() {
  132. ?>
  133. plug_wookook
  134. <?php
  135. }
  136. function Option($pre) {
  137. //update_option ( $pre.'_items', $_POST [ $pre.'_items' ] );
  138. update_option($pre . '_code', $_POST[$pre . '_code']);
  139. update_option($pre . '_exitrole', $_POST[$pre . '_exitrole']);
  140. update_option($pre . '_item_roles', $_POST[$pre . '_item_roles']);
  141. }
  142. function admin_line($pre) {
  143. ?>
  144. <?php echo (__("Itemnumber1 ... ItemnumberN > Role:", "dagsopt")) ?><br>
  145. <textarea name="<?php echo ($pre . '_item_roles'); ?>" rows="7" class="large-text code"><?php echo (stripcslashes(get_option($pre . "_item_roles"))); ?></textarea>
  146. <?php echo (__("Code:", "dagsopt")) ?><br>
  147. <textarea name="<?php echo ($pre . '_code'); ?>" rows="2" class="large-text code"><?php echo (stripcslashes(get_option($pre . "_code"))); ?></textarea>
  148. <?php echo (__("Exitrole:", "dagsopt")) ?><br>
  149. <input name="<?php echo ($pre . '_exitrole'); ?>" value="<?php echo (stripcslashes(get_option($pre . "_exitrole"))); ?>">
  150. <?php
  151. }
  152. }
  153. global $plug_wookook;
  154. $plug_wookook = new plug_wookook($this);
  155. $this->dagsopt['plug_wookook'] = $plug_wookook;
  156. }