plug_woohook.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. $rr = file_get_contents('php://input');
  26. $itemnumbers = explode(",",get_option($this->pluginname."_wookook_items",false));
  27. $found = false;
  28. try {
  29. $order = json_decode($rr,true);
  30. if(isset($order['date_paid_gmt'])){
  31. foreach ($order['line_items'] as $num => $line) {
  32. if(in_array($line['product_id'],$itemnumbers)){
  33. $found = true;
  34. }
  35. }
  36. }
  37. } catch (Exception $e) {
  38. echo 'Caught exception: ', $e->getMessage(), "\n";
  39. }
  40. $testmode = false;
  41. if($found){
  42. $resu = array();
  43. $user_email = $order['billing']['email'];
  44. $user_name = sanitize_username("O".$order['id']);
  45. $user_id = username_exists( $user_name );
  46. if ( ! $user_id && false == email_exists( $user_email ) ) {
  47. $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false );
  48. if($testmode){
  49. $resu[] = "$user_id mock created : $user_name $user_email ";
  50. }else{
  51. $user_id = wp_create_user( $user_name, $random_password, $user_email );
  52. wp_update_user( array( 'ID' => $user_id, 'role' => 'betalende-abonnent' ) );
  53. wp_send_new_user_notifications($user_id,'both');
  54. $resu[] = "$user_id created : $user_name $user_email ";
  55. }
  56. } else {
  57. $resu[] = "$user_id or $user_email exists";
  58. $random_password = __( 'User already exists. Password inherited.', 'textdomain' );
  59. }
  60. $aemail = get_bloginfo('admin_email');
  61. $rr = wp_mail($aemail, 'CCR '. count($resu)." : ". getenv('HOSTNAME')." : ".$_SERVER['REMOTE_ADDR']." : " . $_SERVER['SERVER_ADDR'] . " : ". $_SERVER['SERVER_NAME']." ".date("U"), "::".print_r($resu,true));
  62. echo("DONE live");
  63. print_r($resu);
  64. die("done $rr");
  65. }else{
  66. die("no action done");
  67. }
  68. }
  69. }
  70. function help(){
  71. ?>
  72. plug_wookook
  73. <?php
  74. }
  75. function Option($pre){
  76. update_option ( $pre.'_items', $_POST [ $pre.'_items' ] );
  77. }
  78. function admin_line($pre){
  79. ?>
  80. <table>
  81. <tr><td>
  82. </td><td>
  83. </td></tr>
  84. <tr><td>
  85. </td><td>
  86. </td></tr>
  87. <tr><td> <?php echo(__("Item numbers:","dagsopt")) ?><br><textarea name="<?php echo( $pre.'_items' ); ?>"><?php echo(stripcslashes(get_option($pre."_items"))); ?></textarea> <br></td></tr>
  88. </table>
  89. <?php
  90. }
  91. }
  92. global $plug_wookook;
  93. $plug_wookook = new plug_wookook($this);
  94. $this->dagsopt['plug_wookook'] = $plug_wookook;
  95. }