123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- <?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));
- add_filter('manage_users_custom_column', array(&$this, 'imel_custom_column_userfieldx'), 13, 3);
- add_filter('manage_users_columns', array(&$this, 'imel_column_userfieldx'), 13, 1);
- }
- function imel_column_userfieldx($defaults) {
- $defaults['abostatus'] = __('Abo Status', 'id-column');
- $defaults['aboproduct'] = __('Abo Product', 'id-column');
- $defaults['aboorder'] = __('Abo Order', 'id-column');
- return $defaults;
- }
- function imel_custom_column_userfieldx($value, $column_name, $id) {
- if ($column_name == 'aboproduct') {
- $burl = get_option($this->pluginname . "_wookook_baseurl_product");
- $pid = get_user_meta($id, 'abo_prod', true);
- $s = '<a href="' . $burl . '' . $pid . '">' . $pid . '</a>';
- return $s;
- }
- if ($column_name == 'aboorder') {
- $burl = get_option($this->pluginname . "_wookook_baseurl_order");
- $pid = get_user_meta($id, 'abo_order', true);
- if ($pid) {
- $s = '<a href="' . $burl . '' . $pid . '">' . $pid . '</a>';
- } else {
- if (get_user_meta($id, 'abo_status', true) == "ACTIVE") {
- } else {
- $pid = get_user_meta($id, 'abo_statuscode', true);
- $s = '<a href="' . $burl . '' . $pid . '">' . $pid . '</a>';
- }
- }
- return $s;
- }
- if ($column_name == 'abostatus') {
- $s = get_user_meta($id, 'abo_status', true);
- return $s;
- }
- return $value;
- }
- 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'])) {
- $hh = apache_request_headers();
- if (isset($hh["X-WC-Webhook-Source"])) {
- $shopurl = $hh["X-WC-Webhook-Source"];
- } else {
- $shopurl = null;
- }
- $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;
- $theproduct = 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']];
- $theproduct = $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;
- } else if ($order['status'] == "completed") {
- if (isset($order['date_paid_gmt']) && strlen($order['date_paid_gmt']) > 0) {
- $active = true;
- } else {
- $active = false;
- }
- }
- }
- // echo($order['status']."::".$found."::".$active." : ".$order['date_paid']." : ".$order['date_paid_gmt']);
- // die("\n\nDONE");
- $testmode = false;
- if ($found && $theproduct) {
- $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']));
- update_user_meta($user_id, 'abo_status', $order['status'] . " " . ($active ? " " : " SUSPENDED"));
- update_user_meta($user_id, 'abo_order', $order['id']);
- update_user_meta($user_id, 'abo_prod', $theproduct);
- wp_send_new_user_notifications($user_id, 'both');
- $resu[] = "CREATED: " . $order['status'] . " $active :: $theproduct :: $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);
- update_user_meta($user_id, 'abo_status', $order['status'] . " " . ($active ? " " : " SUSPENDED"));
- update_user_meta($user_id, 'abo_order', $order['id']);
- update_user_meta($user_id, 'abo_prod', $theproduct);
- $resu[] = "UPDATED: " . $order['status'] . " $active :: $theproduct :: $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']);
- update_option($pre . '_baseurl_product', $_POST[$pre . '_baseurl_product']);
- update_option($pre . '_baseurl_order', $_POST[$pre . '_baseurl_order']);
- }
- function admin_line($pre) {
- ?>
- <hr>
- <?php echo (__("Shop baseurl products:", "dagsopt")) ?><br>
- <input name="<?php echo ($pre . '_baseurl_product'); ?>" class="large-text " value="<?php echo (stripcslashes(get_option($pre . "_baseurl_product"))); ?>">
- <hr>
- <?php echo (__("Shop baseurl orders:", "dagsopt")) ?><br>
- <input name="<?php echo ($pre . '_baseurl_order'); ?>" class="large-text " value="<?php echo (stripcslashes(get_option($pre . "_baseurl_order"))); ?>">
- <hr>
- <?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;
- }
|