plug_woohook.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. }
  84. }
  85. // echo($order['status']."::".$found."::".$active." : ".$order['date_paid']." : ".$order['date_paid_gmt']);
  86. // die("\n\nDONE");
  87. $testmode = false;
  88. if($found){
  89. $resu = array();
  90. $user_email = $order['billing']['email'];
  91. $user_name = $user_email;//sanitize_username("O".$order['id']);
  92. $user_id = username_exists( $user_name );
  93. if ( ! $user_id && false == email_exists( $user_email ) ) {
  94. $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false );
  95. if($testmode){
  96. $resu[] = "$user_id mock created : $user_name $user_email ";
  97. }else{
  98. $user_id = wp_create_user( $user_name, $random_password, $user_email );
  99. $xrole = $active ? $therole : $exitrole;
  100. wp_update_user( array( 'ID' => $user_id, 'role' => $xrole, 'first_name' => $order['billing']['first_name'],'last_name'=> $order['billing']['last_name']) );
  101. wp_send_new_user_notifications($user_id,'both');
  102. $resu[] = "CREATED: ".$order['status']." $active $user_id or $user_email exists role > '$xrole'";
  103. }
  104. } else {
  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. $u = new WP_User( $user_id );
  108. $u->set_role( $xrole );
  109. $resu[] = "UPDATED: ".$order['status']." $active $user_id or $user_email exists role > '$xrole'";
  110. }
  111. $aemail = get_bloginfo('admin_email');
  112. $rr = wp_mail($aemail, 'CCR '. count($resu)." : ". getenv('HOSTNAME')." : ".$_SERVER['REMOTE_ADDR']." : " . $_SERVER['SERVER_ADDR'] . " : ". $_SERVER['SERVER_NAME']." ".date("U"), "::".print_r($resu,true));
  113. echo("DONE live");
  114. print_r($resu);
  115. die("done $rr");
  116. }else{
  117. die("no action done");
  118. }
  119. }
  120. }
  121. function help(){
  122. ?>
  123. plug_wookook
  124. <?php
  125. }
  126. function Option($pre){
  127. //update_option ( $pre.'_items', $_POST [ $pre.'_items' ] );
  128. update_option ( $pre.'_code', $_POST [ $pre.'_code' ] );
  129. update_option ( $pre.'_exitrole', $_POST [ $pre.'_exitrole' ] );
  130. update_option ( $pre.'_item_roles', $_POST [ $pre.'_item_roles' ] );
  131. }
  132. function admin_line($pre){
  133. ?>
  134. <?php echo(__("Itemnumber1 ... ItemnumberN > Role:","dagsopt")) ?><br>
  135. <textarea name="<?php echo( $pre.'_item_roles' ); ?>" rows="7" class="large-text code"><?php echo(stripcslashes(get_option($pre."_item_roles"))); ?></textarea>
  136. <?php echo(__("Code:","dagsopt")) ?><br>
  137. <textarea name="<?php echo( $pre.'_code' ); ?>" rows="2" class="large-text code"><?php echo(stripcslashes(get_option($pre."_code"))); ?></textarea>
  138. <?php echo(__("Exitrole:","dagsopt")) ?><br>
  139. <input name="<?php echo( $pre.'_exitrole' ); ?>" value="<?php echo(stripcslashes(get_option($pre."_exitrole"))); ?>">
  140. <?php
  141. }
  142. }
  143. global $plug_wookook;
  144. $plug_wookook = new plug_wookook($this);
  145. $this->dagsopt['plug_wookook'] = $plug_wookook;
  146. }