123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- if (!class_exists("plug_wookook")) {
- if(!function_exists("sanitize_username")){
- function sanitize_username($username) {
- $parts = explode("@", $username);
- if (count($parts) == 2) {
- $username = $parts[0];
- }
- return $username;
- }
- }
- class plug_wookook {
- function __construct($ns) {
- $this->title = __("plug_wookook","dagsopt");
- $this->pluginname = $ns->pluginname;
- $this->file = $ns->file;
- $this->ns = $ns;
- }
- function start() {
- add_action( 'init', array(&$this,'xx__update_custom_roles') );
- add_action('wp_loaded',array(&$this,'header'),1);
- add_role( "suspenderet-abonnent", "Suspenderet abonnent", array( 'read' => true, 'level_0' => false ) );
- add_role( "betalende-abonnent", "Betalende abonnent", array( 'read' => true, 'level_0' => true ) );
- }
-
- function xx__update_custom_roles() {
- if ( get_option( 'custom_roles_version' ) < 3 ) {
- // add_role( "suspenderet-abonnent", "Suspenderet abonnent", array( 'read' => true, 'level_0' => false ) );
- // add_role( "betalende-abonnent", "Betalende abonnent", array( 'read' => true, 'level_0' => true ) );
- update_option( 'custom_roles_version', 3 );
- }
- }
- function header(){
- if(isset($_REQUEST['woohook'])){
- $thecode = get_option($this->pluginname."_wookook_code",false);
- $exitrole = trim(get_option($this->pluginname."_wookook_exitrole",false));
- if($_REQUEST['woohook']!=="$thecode"){
- die("code not right");
- }
- $rr = file_get_contents('php://input');
- //$itemnumbers = explode(",",);
- $itemroles = array( );
- $lins = explode("\n",get_option($this->pluginname."_wookook_item_roles",false));
- foreach ($lins as $nn => $line) {
- if(stristr($line, " > ")){
- $a = explode(" > ",$line);
- $nums = explode(" ",$a[0]);
- $role = $a[1];
- foreach ($nums as $pp => $itemnum) {
- if(strlen($role)>0){
- $itemroles[trim($itemnum)] = trim($role);
- }
- }
- }
- }
-
- $found = false;
- try {
- $order = json_decode($rr,true);
- if(isset($order['line_items'])){
- foreach ($order['line_items'] as $num => $line) {
- if(isset($itemroles["".$line['product_id']])){
- $found = true;
- $therole = $itemroles["".$line['product_id']];
- }
- }
- }
- } catch (Exception $e) {
- echo 'Caught exception: ', $e->getMessage(), "\n";
- }
- $active = false;
- if(isset($order['status'])){
-
- if($order['status']=="active"){
- $found = true;
- $active = true;
- }else if($order['status']=="processing"){
- if(isset($order['date_paid_gmt']) && strlen($order['date_paid_gmt'])>0){
- $found = true;
- $active = true;
- }else{
- $found = false;
- }
- }else if($order['status']=="on-hold"){
- $active = false;
- }
- }
-
- // echo($order['status']."::".$found."::".$active." : ".$order['date_paid']." : ".$order['date_paid_gmt']);
- // die("\n\nDONE");
- $testmode = false;
- if($found){
- $resu = array();
- $user_email = $order['billing']['email'];
- $user_name = $user_email;//sanitize_username("O".$order['id']);
- $user_id = username_exists( $user_name );
- if ( ! $user_id && false == email_exists( $user_email ) ) {
- $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false );
- if($testmode){
- $resu[] = "$user_id mock created : $user_name $user_email ";
- }else{
- $user_id = wp_create_user( $user_name, $random_password, $user_email );
- $xrole = $active ? $therole : $exitrole;
- wp_update_user( array( 'ID' => $user_id, 'role' => $xrole, 'first_name' => $order['billing']['first_name'],'last_name'=> $order['billing']['last_name']) );
- wp_send_new_user_notifications($user_id,'both');
- $resu[] = "CREATED: ".$order['status']." $active $user_id or $user_email exists role > '$xrole'";
- }
- } else {
- $xrole = $active ? $therole : $exitrole;
-
- wp_update_user( array( 'ID' => $user_id, 'role' => $xrole, 'first_name' => $order['billing']['first_name'],'last_name'=> $order['billing']['last_name']) );
- $u = new WP_User( $user_id );
- $u->set_role( $xrole );
-
- $resu[] = "UPDATED: ".$order['status']." $active $user_id or $user_email exists role > '$xrole'";
- }
- $aemail = get_bloginfo('admin_email');
- $rr = wp_mail($aemail, 'CCR '. count($resu)." : ". getenv('HOSTNAME')." : ".$_SERVER['REMOTE_ADDR']." : " . $_SERVER['SERVER_ADDR'] . " : ". $_SERVER['SERVER_NAME']." ".date("U"), "::".print_r($resu,true));
- echo("DONE live");
- print_r($resu);
- die("done $rr");
- }else{
- die("no action done");
- }
- }
- }
- function help(){
- ?>
- plug_wookook
- <?php
- }
-
- function Option($pre){
-
- //update_option ( $pre.'_items', $_POST [ $pre.'_items' ] );
-
- update_option ( $pre.'_code', $_POST [ $pre.'_code' ] );
- update_option ( $pre.'_exitrole', $_POST [ $pre.'_exitrole' ] );
-
- update_option ( $pre.'_item_roles', $_POST [ $pre.'_item_roles' ] );
-
-
- }
-
-
- function admin_line($pre){
-
- ?>
- <?php echo(__("Itemnumber1 ... ItemnumberN > Role:","dagsopt")) ?><br>
- <textarea name="<?php echo( $pre.'_item_roles' ); ?>" rows="7" class="large-text code"><?php echo(stripcslashes(get_option($pre."_item_roles"))); ?></textarea>
- <?php echo(__("Code:","dagsopt")) ?><br>
- <textarea name="<?php echo( $pre.'_code' ); ?>" rows="2" class="large-text code"><?php echo(stripcslashes(get_option($pre."_code"))); ?></textarea>
- <?php echo(__("Exitrole:","dagsopt")) ?><br>
- <input name="<?php echo( $pre.'_exitrole' ); ?>" value="<?php echo(stripcslashes(get_option($pre."_exitrole"))); ?>">
- <?php
- }
- }
- global $plug_wookook;
- $plug_wookook = new plug_wookook($this);
- $this->dagsopt['plug_wookook'] = $plug_wookook;
-
- }
|