<commit_before><?php 
   /*
   Description: The Parcel4Me Woo Shop is the interface beteen parcel4me and WooCommerce
   Version: 0.0.1
   Author: ParcelForMe
   Author URI: http://parcelfor.me/
   License: MIT
   */

if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}


require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/vendor/parcel4me/parcel4me/src/parcel4me/p4m-shop.php';
// i'm not sure why I can't load it like this : require_once __DIR__.'/vendor/autoload.php';
// contributers who know composer autoloader better than me are very welcome to help with that bit. 

class Parcel4me_Woo_Cart_Adapter extends P4M\P4M_Shop {


    function userIsLoggedIn() 
    {
        return is_user_logged_in();
    }


    function createNewUser( $p4m_consumer ) 
    {
        $username = $p4m_consumer->Email;
        $password = wp_generate_password();
        $user_data = array(
                        'user_login'   => $username,
                        'user_pass'    => $password,
                        'user_email'   => $p4m_consumer->Email,
                        'first_name'   => $p4m_consumer->GivenName,
                        'nickname'     => $p4m_consumer->GivenName,
                        'display_name' => $p4m_consumer->GivenName,
                        'last_name'    => $p4m_consumer->FamilyName
        );
        $wp_userid = wp_insert_user( $user_data );

        if ( is_wp_error( $wp_userid ) ) {
            error_log('P4M: Unable to create new user; '.$username);
            return false;
        } else {
            $wp_user   = get_userdata( $wp_userid );
            $wp_user->id = $wp_userid; // p4m is expecting to see an ->id field (WP will also have the same as ->ID)
            return $wp_user;
        }
    }


    function isValidUserId( $localUserId ) 
    {
        return !!get_userdata( $user_id );
    }


    function fetchLocalUserByEmail( $localUserEmailAddress ) {
        return get_user_by( 'email', $localUserEmailAddress );
    }


    function loginUser( $localUserId ) 
    {
        wp_set_auth_cookie( $userid );
        return true;
    }


    function logoutCurrentUser() 
    {
        wp_logout();
        return true;
    }


    function setCurrentUserDetails( $p4m_consumer ) 
    {

        // update name and email of wordpress user
        $wp_user = wp_get_current_user();
        $wp_user->user_email        = $p4m_consumer->Email;
        $wp_user->first_name        = $p4m_consumer->GivenName;
        $wp_user->last_name         = $p4m_consumer->FamilyName;
        $wp_user->first_name        = $p4m_consumer->GivenName;
        $wp_user->nickname          = $p4m_consumer->GivenName;
        $wp_user->display_name      = $p4m_consumer->GivenName;
        $wp_user->last_name         = $p4m_consumer->FamilyName;
        
        $update_result = wp_update_user( $wp_user );

        // TODO: maybe update addresses and payment methods

        if ( is_wp_error( $update_result ) ) {
            error_log('P4M: Unable to update current user');
            return false;
        } else {
            return true;
        }
    }
    

    function getCurrentUserDetails() 
    {

        $wp_user = wp_get_current_user();

        if ( 0 == $current_user->ID ) {
            // No logged in user 
            return false;
        }

        // TODO : maybe get addresses from woo commerce if possible

        $consumer = new P4M\Model\Consumer();
        $consumer->GivenName  = $wp_user->first_name;
        $consumer->FamilyName = $wp_user->last_name;
        $consumer->Email      = $wp_user->user_email;

        if ( !property_exists( $consumer, 'Extras' ) ) {
            $consumer->Extras  = new stdClass();
        }  
        if ( !property_exists( $consumer->Extras, 'LocalId' ) ) {
            $consumer->Extras->LocalId = $current_user->ID;
        }

        $consumer->removeNullProperties();

        return $consumer;
    }



    function getCartOfCurrentUser() {
        /*
            some logic goes here to fetch my cart from 
            my shopping cart DB and put the details into 
            this $cart object 
        */

        // Convert the shopping cart from the shopping cart DB into a 
        // P4M Cart

        $cartItem = new P4M\Model\CartItem();
        $cartItem->Desc         = "A great thing I am buying";
        $cartItem->Qty          = 1;
        $cartItem->Price        = 100;
        $cartItem->LinkToImage  = "http://cdn2.wpbeginner.com/wp-content/uploads/2015/12/pixabay.jpg";
        $cartItem->removeNullProperties();

        $cart = new P4M\Model\Cart();
        $cart->SessionId    = $this->getCurrentSessionId();
        $cart->PaymentType  = "DB";
        $cart->Items        = [ $cartItem ];
        $cart->Currency     = "USD";
        $cart->Reference    = "12345"; //.rand(); // This is REQUIRED (and needs to change for subsequent Paypal payments)
        $cart->removeNullProperties();

        return $cart;
    }


    function setCartOfCurrentUser( $p4m_cart ) {
        /* 
            some logic goes here to set local shopping cart DB
            based on the passed in p4m shopping cart object 
        */

        return true;
    }
    

    function setAddressOfCurrentUser( $which_address, $p4m_address ) {
        /*
            logic here to find the address in the local DB
            and update it, or add if not exists
        */

        return true;
    }


    function updateShipping( $shippingServiceName, $amount, $dueDate ) {
        /*
            some logic goes here to set these shipping amounts and 
            possibly recalculate the tax on the current shopping cart 
        */

        return true;
    }


    function getCartTotals() {
        /*
            some logic goes here to fetch these values from
            the current shopping cart 
        */

        $r = new stdClass();
        $r->Tax      = 10.00;
        $r->Shipping = 20.00;
        $r->Discount = 5.00;
        $r->Total    = 112.22;

        return $r;
    }


    function updateWithDiscountCode( $discountCode ) {
        /* 
            some logic goes here to check if this discount code is valid,
            if not throw an error, if so then apply it to the cart and return the discount details 
        */

        $dis = new P4M\Model\Discount();

        if ($discountCode != 'valid_code') // special discount code "valid_code" works, else fails
        {
            throw new Exception('Unknown discount code.'); 
        }

        $dis->Code          = $discountCode;
        $dis->Description   = 'A demo valid coupon code!';
        $dis->Amount        = 0.01;

        return $dis;
    }


    function updateRemoveDiscountCode( $discountCode ) {
        /* 
            some logic goes here to remove this discount code from the cart 
            throw error if it is not on there
        */

        $dis = new P4M\Model\Discount();

        if ($discountCode != 'valid_code') // special discount code "valid_code" works, else fails
        {
            throw new Exception('Unknown discount code.'); 
        }

        $dis->Code          = $discountCode;
        $dis->Description   = 'A demo valid coupon code!';
        $dis->Amount        = 0.01;

        return $dis;
    }
    

        public function updateCartItemQuantities( $itemsUpdateArray ) {
            /*
            some logic to update the quantities on the cart lines
        */

        $dis = new P4M\Model\Discount();
        $dis->Code           = 'valid_code';
        $dis->Description    = 'A demo valid coupon code!';
        $dis->Amount         = 0.01;

        $disArray = [ $dis ];

        return $disArray;
        }


        public function completePurchase ( $p4m_cart, $transactionId, $transationTypeCode, $authCode ) {
            /*
            some logic to update the db to show that the purchase has happened and clear the cart 
        */

            return true;
        }
        

    function handleError($message) {
        echo '<div class="error p4m-error">'.$message.'</div>';
    }


}
?><commit_msg>get localLogin working (most changes to parcel4me-php)<commit_after><?php 
   /*
   Description: The Parcel4Me Woo Shop is the interface beteen parcel4me and WooCommerce
   Version: 0.0.1
   Author: ParcelForMe
   Author URI: http://parcelfor.me/
   License: MIT
   */

if ( ! defined( 'ABSPATH' ) ) { 
    exit; // Exit if accessed directly
}


require_once __DIR__.'/vendor/autoload.php';
require_once __DIR__.'/vendor/parcel4me/parcel4me/src/parcel4me/p4m-shop.php';
// i'm not sure why I can't load it like this : require_once __DIR__.'/vendor/autoload.php';
// contributers who know composer autoloader better than me are very welcome to help with that bit. 

class Parcel4me_Woo_Cart_Adapter extends P4M\P4M_Shop {


    function userIsLoggedIn() 
    {
        return is_user_logged_in();
    }


    function createNewUser( $p4m_consumer ) 
    {
        $username = $p4m_consumer->Email;
        $password = wp_generate_password();
        $user_data = array(
                        'user_login'   => $username,
                        'user_pass'    => $password,
                        'user_email'   => $p4m_consumer->Email,
                        'first_name'   => $p4m_consumer->GivenName,
                        'nickname'     => $p4m_consumer->GivenName,
                        'display_name' => $p4m_consumer->GivenName,
                        'last_name'    => $p4m_consumer->FamilyName
        );
        $wp_userid = wp_insert_user( $user_data );

        if ( is_wp_error( $wp_userid ) ) {
            error_log('P4M: Unable to create new user; '.$username);
            return false;
        } else {
            $wp_user   = get_userdata( $wp_userid );
            $wp_user->id = $wp_userid; // p4m is expecting to see an ->id field (WP will also have the same as ->ID)
            return $wp_user;
        }
    }


    function isValidUserId( $localUserId ) 
    {
        return !!get_userdata( $user_id );
    }


    function fetchLocalUserByEmail( $localUserEmailAddress ) {
        return get_user_by( 'email', $localUserEmailAddress );
    }


    function loginUser( $localUserId ) 
    {
        wp_set_auth_cookie( $localUserId );
        return true;
    }


    function logoutCurrentUser() 
    {
        wp_logout();
        return true;
    }


    function setCurrentUserDetails( $p4m_consumer ) 
    {

        // update name and email of wordpress user
        $wp_user = wp_get_current_user();
        $wp_user->user_email        = $p4m_consumer->Email;
        $wp_user->first_name        = $p4m_consumer->GivenName;
        $wp_user->last_name         = $p4m_consumer->FamilyName;
        $wp_user->first_name        = $p4m_consumer->GivenName;
        $wp_user->nickname          = $p4m_consumer->GivenName;
        $wp_user->display_name      = $p4m_consumer->GivenName;
        $wp_user->last_name         = $p4m_consumer->FamilyName;
        
        $update_result = wp_update_user( $wp_user );

        // TODO: maybe update addresses and payment methods

        if ( is_wp_error( $update_result ) ) {
            error_log('P4M: Unable to update current user');
            return false;
        } else {
            return true;
        }
    }
    

    function getCurrentUserDetails() 
    {

        $wp_user = wp_get_current_user();

        if ( 0 == $current_user->ID ) {
            // No logged in user 
            return false;
        }

        // TODO : maybe get addresses from woo commerce if possible

        $consumer = new P4M\Model\Consumer();
        $consumer->GivenName  = $wp_user->first_name;
        $consumer->FamilyName = $wp_user->last_name;
        $consumer->Email      = $wp_user->user_email;

        if ( !property_exists( $consumer, 'Extras' ) ) {
            $consumer->Extras  = new stdClass();
        }  
        if ( !property_exists( $consumer->Extras, 'LocalId' ) ) {
            $consumer->Extras->LocalId = $current_user->ID;
        }

        $consumer->removeNullProperties();

        return $consumer;
    }

// WIP ...

    function getCartOfCurrentUser() {
        /*
            some logic goes here to fetch my cart from 
            my shopping cart DB and put the details into 
            this $cart object 
        */

        // Convert the shopping cart from the shopping cart DB into a 
        // P4M Cart

        $cartItem = new P4M\Model\CartItem();
        $cartItem->Desc         = "A great thing I am buying";
        $cartItem->Qty          = 1;
        $cartItem->Price        = 100;
        $cartItem->LinkToImage  = "http://cdn2.wpbeginner.com/wp-content/uploads/2015/12/pixabay.jpg";
        $cartItem->removeNullProperties();

        $cart = new P4M\Model\Cart();
        $cart->SessionId    = $this->getCurrentSessionId();
        $cart->PaymentType  = "DB";
        $cart->Items        = [ $cartItem ];
        $cart->Currency     = "USD";
        $cart->Reference    = "12345"; //.rand(); // This is REQUIRED (and needs to change for subsequent Paypal payments)
        $cart->removeNullProperties();

        return $cart;
    }


    function setCartOfCurrentUser( $p4m_cart ) {
        /* 
            some logic goes here to set local shopping cart DB
            based on the passed in p4m shopping cart object 
        */

        return true;
    }
    

    function setAddressOfCurrentUser( $which_address, $p4m_address ) {
        /*
            logic here to find the address in the local DB
            and update it, or add if not exists
        */

        return true;
    }


    function updateShipping( $shippingServiceName, $amount, $dueDate ) {
        /*
            some logic goes here to set these shipping amounts and 
            possibly recalculate the tax on the current shopping cart 
        */

        return true;
    }


    function getCartTotals() {
        /*
            some logic goes here to fetch these values from
            the current shopping cart 
        */

        $r = new stdClass();
        $r->Tax      = 10.00;
        $r->Shipping = 20.00;
        $r->Discount = 5.00;
        $r->Total    = 112.22;

        return $r;
    }


    function updateWithDiscountCode( $discountCode ) {
        /* 
            some logic goes here to check if this discount code is valid,
            if not throw an error, if so then apply it to the cart and return the discount details 
        */

        $dis = new P4M\Model\Discount();

        if ($discountCode != 'valid_code') // special discount code "valid_code" works, else fails
        {
            throw new Exception('Unknown discount code.'); 
        }

        $dis->Code          = $discountCode;
        $dis->Description   = 'A demo valid coupon code!';
        $dis->Amount        = 0.01;

        return $dis;
    }


    function updateRemoveDiscountCode( $discountCode ) {
        /* 
            some logic goes here to remove this discount code from the cart 
            throw error if it is not on there
        */

        $dis = new P4M\Model\Discount();

        if ($discountCode != 'valid_code') // special discount code "valid_code" works, else fails
        {
            throw new Exception('Unknown discount code.'); 
        }

        $dis->Code          = $discountCode;
        $dis->Description   = 'A demo valid coupon code!';
        $dis->Amount        = 0.01;

        return $dis;
    }
    

        public function updateCartItemQuantities( $itemsUpdateArray ) {
            /*
            some logic to update the quantities on the cart lines
        */

        $dis = new P4M\Model\Discount();
        $dis->Code           = 'valid_code';
        $dis->Description    = 'A demo valid coupon code!';
        $dis->Amount         = 0.01;

        $disArray = [ $dis ];

        return $disArray;
        }


        public function completePurchase ( $p4m_cart, $transactionId, $transationTypeCode, $authCode ) {
            /*
            some logic to update the db to show that the purchase has happened and clear the cart 
        */

            return true;
        }
        

    function handleError($message) {
        echo '<div class="error p4m-error">'.$message.'</div>';
    }


}
?>
{'fraction_non_alphanumeric': 0.10358370152184586, 'fraction_numerical': 0.010370643102601865, 'mean_word_length': 1.7589300829524293, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 145, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5208633', 'n_tokens_mistral': 4987, 'n_tokens_neox': 4645, 'n_words': 1239}