plug_woohook.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_role( "betalende-abonnent", "Betalende abonnent", array( 'read' => true, 'level_0' => true ) );
  21. add_action('wp_loaded',array(&$this,'header'),1);
  22. }
  23. function header(){
  24. if(isset($_REQUEST['woohook'])){
  25. $thecode = get_option($this->pluginname."_wookook_code",false);
  26. if($_REQUEST['woohook']!=="$thecode"){
  27. die("code not right");
  28. }
  29. $rr = file_get_contents('php://input');
  30. //$itemnumbers = explode(",",);
  31. $itemroles = array( );
  32. $lins = explode("\n",get_option($this->pluginname."_wookook_item_roles",false));
  33. foreach ($lins as $nn => $line) {
  34. if(stristr($line, " > ")){
  35. $a = explode(" > ",$line);
  36. $nums = explode(" ",$a[0]);
  37. $role = $a[1];
  38. foreach ($nums as $pp => $itemnum) {
  39. if(strlen($role)>0){
  40. $itemroles[$itemnum] = $role;
  41. }
  42. }
  43. }
  44. }
  45. $found = false;
  46. try {
  47. $order = json_decode($rr,true);
  48. if(isset($order['date_paid_gmt'])){
  49. foreach ($order['line_items'] as $num => $line) {
  50. if(isset($itemroles["".$line['product_id']])){
  51. $found = true;
  52. $therole = $itemroles["".$line['product_id']];
  53. }
  54. }
  55. }
  56. } catch (Exception $e) {
  57. echo 'Caught exception: ', $e->getMessage(), "\n";
  58. }
  59. $testmode = false;
  60. if($found){
  61. $resu = array();
  62. $user_email = $order['billing']['email'];
  63. $user_name = sanitize_username("O".$order['id']);
  64. $user_id = username_exists( $user_name );
  65. if ( ! $user_id && false == email_exists( $user_email ) ) {
  66. $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false );
  67. if($testmode){
  68. $resu[] = "$user_id mock created : $user_name $user_email ";
  69. }else{
  70. $user_id = wp_create_user( $user_name, $random_password, $user_email );
  71. wp_update_user( array( 'ID' => $user_id, 'role' => $therole) );
  72. wp_send_new_user_notifications($user_id,'both');
  73. $resu[] = "$user_id created : $user_name $user_email ";
  74. }
  75. } else {
  76. $resu[] = "$user_id or $user_email exists";
  77. $random_password = __( 'User already exists. Password inherited.', 'textdomain' );
  78. }
  79. $aemail = get_bloginfo('admin_email');
  80. $rr = wp_mail($aemail, 'CCR '. count($resu)." : ". getenv('HOSTNAME')." : ".$_SERVER['REMOTE_ADDR']." : " . $_SERVER['SERVER_ADDR'] . " : ". $_SERVER['SERVER_NAME']." ".date("U"), "::".print_r($resu,true));
  81. echo("DONE live");
  82. print_r($resu);
  83. die("done $rr");
  84. }else{
  85. die("no action done");
  86. }
  87. }
  88. }
  89. function help(){
  90. ?>
  91. plug_wookook
  92. <?php
  93. }
  94. function Option($pre){
  95. //update_option ( $pre.'_items', $_POST [ $pre.'_items' ] );
  96. update_option ( $pre.'_code', $_POST [ $pre.'_code' ] );
  97. update_option ( $pre.'_item_roles', $_POST [ $pre.'_item_roles' ] );
  98. }
  99. function admin_line($pre){
  100. ?>
  101. <?php echo(__("Itemnumber1 ... ItemnumberN > Role:","dagsopt")) ?><br>
  102. <textarea name="<?php echo( $pre.'_item_roles' ); ?>" rows="7" class="large-text code"><?php echo(stripcslashes(get_option($pre."_item_roles"))); ?></textarea>
  103. <?php echo(__("Code:","dagsopt")) ?><br>
  104. <textarea name="<?php echo( $pre.'_code' ); ?>" rows="2" class="large-text code"><?php echo(stripcslashes(get_option($pre."_code"))); ?></textarea>
  105. <?php
  106. }
  107. }
  108. global $plug_wookook;
  109. $plug_wookook = new plug_wookook($this);
  110. $this->dagsopt['plug_wookook'] = $plug_wookook;
  111. }