diff --git a/wpsc-components/merchant-core-v2/helpers/checkout.php b/wpsc-components/merchant-core-v2/helpers/checkout.php index 8956ca7c6b..953c2c9d45 100644 --- a/wpsc-components/merchant-core-v2/helpers/checkout.php +++ b/wpsc-components/merchant-core-v2/helpers/checkout.php @@ -56,7 +56,7 @@ function _wpsc_filter_merchant_v2_payment_method_form_fields( $fields ) { if ( empty( $selected_value ) ) { $current_purchase_log_id = wpsc_get_customer_meta( 'current_purchase_log_id' ); - $purchase_log = new WPSC_Purchase_Log( $current_purchase_log_id ); + $purchase_log = wpsc_get_order( $current_purchase_log_id ); $selected_value = $purchase_log->get( 'gateway' ); } diff --git a/wpsc-components/merchant-core-v3/gateways/amazon-payments.php b/wpsc-components/merchant-core-v3/gateways/amazon-payments.php index e30d593de5..5d2fc2b47a 100644 --- a/wpsc-components/merchant-core-v3/gateways/amazon-payments.php +++ b/wpsc-components/merchant-core-v3/gateways/amazon-payments.php @@ -980,7 +980,7 @@ public static function get_instance( $gateway ) { } public function set_purchase_log( $id ) { - $this->log = new WPSC_Purchase_Log( $id ); + $this->log = wpsc_get_order( $id ); } /** diff --git a/wpsc-components/merchant-core-v3/gateways/paypal-express-checkout.php b/wpsc-components/merchant-core-v3/gateways/paypal-express-checkout.php index 32400c51bb..5925ab06e6 100644 --- a/wpsc-components/merchant-core-v3/gateways/paypal-express-checkout.php +++ b/wpsc-components/merchant-core-v3/gateways/paypal-express-checkout.php @@ -342,7 +342,7 @@ protected function set_purchase_log_for_callbacks( $sessionid = false ) { } // Create a new Purchase Log entry - $purchase_log = new WPSC_Purchase_Log( $sessionid, 'sessionid' ); + $purchase_log = wpsc_get_order( $sessionid, 'sessionid' ); if ( ! $purchase_log->exists() ) { return null; diff --git a/wpsc-components/merchant-core-v3/gateways/paypal-pro.php b/wpsc-components/merchant-core-v3/gateways/paypal-pro.php index 161f91802b..101a2aaece 100644 --- a/wpsc-components/merchant-core-v3/gateways/paypal-pro.php +++ b/wpsc-components/merchant-core-v3/gateways/paypal-pro.php @@ -149,9 +149,9 @@ protected function set_purchase_log_for_callbacks( $id = false, $sessionid = fal // Create a new Purchase Log entry if ( $sessionid === 'sessionid' ) { - $purchase_log = new WPSC_Purchase_Log( $id, 'sessionid' ); + $purchase_log = wpsc_get_order( $id, 'sessionid' ); } else { - $purchase_log = new WPSC_Purchase_Log( $id, 'id' ); + $purchase_log = wpsc_get_order( $id, 'id' ); } if ( ! $purchase_log->exists() ) { diff --git a/wpsc-components/merchant-core-v3/gateways/worldpay.php b/wpsc-components/merchant-core-v3/gateways/worldpay.php index 5016d4e5ad..95c88df323 100644 --- a/wpsc-components/merchant-core-v3/gateways/worldpay.php +++ b/wpsc-components/merchant-core-v3/gateways/worldpay.php @@ -413,7 +413,7 @@ public function execute( $endpoint, $params = array(), $type = 'POST' ) { public function type_of_goods( $log_id ) { $digital = 0; - $log = new WPSC_Purchase_Log( $log_id ); + $log = wpsc_get_order( $log_id ); $cart = $log->get_items(); foreach ( $cart as $cartitem ) { @@ -460,7 +460,7 @@ public static function get_instance( $gateway ) { } public function set_purchase_log( $id ) { - $this->log = new WPSC_Purchase_Log( $id ); + $this->log = wpsc_get_order( $id ); } /** diff --git a/wpsc-components/merchant-core-v3/helpers/checkout.php b/wpsc-components/merchant-core-v3/helpers/checkout.php index 7d60e837de..dfa34c149d 100644 --- a/wpsc-components/merchant-core-v3/helpers/checkout.php +++ b/wpsc-components/merchant-core-v3/helpers/checkout.php @@ -111,7 +111,7 @@ function _wpsc_filter_merchant_v3_payment_method_form_fields( $fields ) { if ( empty( $selected_value ) ) { $current_purchase_log_id = wpsc_get_customer_meta( 'current_purchase_log_id' ); - $purchase_log = new WPSC_Purchase_Log( $current_purchase_log_id ); + $purchase_log = wpsc_get_order( $current_purchase_log_id ); $selected_value = $purchase_log->get( 'gateway' ); } diff --git a/wpsc-components/theme-engine-v1/helpers/ajax.php b/wpsc-components/theme-engine-v1/helpers/ajax.php index f3fdd1ff5d..b4de31bd54 100644 --- a/wpsc-components/theme-engine-v1/helpers/ajax.php +++ b/wpsc-components/theme-engine-v1/helpers/ajax.php @@ -735,7 +735,7 @@ function wpsc_submit_checkout( $collected_data = true ) { 'wpec_taxes_rate' => $tax_percentage, ); - $purchase_log = new WPSC_Purchase_Log( $args ); + $purchase_log = wpsc_get_order( $args ); $purchase_log->save(); $purchase_log_id = $purchase_log->get( 'id' ); diff --git a/wpsc-components/theme-engine-v1/templates/functions/wpsc-transaction_results_functions.php b/wpsc-components/theme-engine-v1/templates/functions/wpsc-transaction_results_functions.php index dff19ae268..6b06eecd42 100644 --- a/wpsc-components/theme-engine-v1/templates/functions/wpsc-transaction_results_functions.php +++ b/wpsc-components/theme-engine-v1/templates/functions/wpsc-transaction_results_functions.php @@ -15,7 +15,7 @@ function transaction_results( $sessionid, $display_to_screen = true, $transactio // pre-3.8.9 variable $echo_to_screen = $display_to_screen; - $purchase_log_object = new WPSC_Purchase_Log( $sessionid, 'sessionid' ); + $purchase_log_object = wpsc_get_order( $sessionid, 'sessionid' ); // compatibility with pre-3.8.9 templates where they use a global // $purchase_log object which is simply just a database row diff --git a/wpsc-components/theme-engine-v2/classes/cart-item-table-order.php b/wpsc-components/theme-engine-v2/classes/cart-item-table-order.php index a996e2cbc9..3cfc2a43da 100644 --- a/wpsc-components/theme-engine-v2/classes/cart-item-table-order.php +++ b/wpsc-components/theme-engine-v2/classes/cart-item-table-order.php @@ -13,7 +13,7 @@ public function __construct( $id ) { $this->show_thumbnails = true; $this->show_shipping = true; - $this->log = new WPSC_Purchase_Log( $id ); + $this->log = wpsc_get_order( $id ); $this->items = $this->get_items(); } diff --git a/wpsc-components/theme-engine-v2/classes/shipping-calculator.php b/wpsc-components/theme-engine-v2/classes/shipping-calculator.php index 1561b29cf0..26cd9a0a0c 100644 --- a/wpsc-components/theme-engine-v2/classes/shipping-calculator.php +++ b/wpsc-components/theme-engine-v2/classes/shipping-calculator.php @@ -14,7 +14,7 @@ public static function get_instance( $purchase_log = false ) { } if ( is_int( $purchase_log ) ) { - $purchase_log = new WPSC_Purchase_Log( $purchase_log ); + $purchase_log = wpsc_get_order( $purchase_log ); } $id = $purchase_log->get( 'id' ); @@ -76,7 +76,7 @@ public function __construct( $purchase_log = false ) { // in case of integer argument, initialize the purchase log object if ( ! is_object( $purchase_log ) ) { - $purchase_log = new WPSC_Purchase_Log( absint( $purchase_log ) ); + $purchase_log = wpsc_get_order( absint( $purchase_log ) ); } $this->purchase_log = $purchase_log; @@ -171,7 +171,7 @@ private function get_active_shipping() { $current_purchase_log_id = wpsc_get_customer_meta( 'current_purchase_log_id' ); - $purchase_log = new WPSC_Purchase_Log( $current_purchase_log_id ); + $purchase_log = wpsc_get_order( $current_purchase_log_id ); $module = $purchase_log->get( 'shipping_method' ); $option = $purchase_log->get( 'shipping_option' ); @@ -190,4 +190,4 @@ private function get_active_shipping() { private function encode_shipping_option_id( $module, $option ) { return md5( $module . ':' . $option ); } -} \ No newline at end of file +} diff --git a/wpsc-components/theme-engine-v2/helpers/checkout-results.php b/wpsc-components/theme-engine-v2/helpers/checkout-results.php index e076e62cbd..d0108fc60b 100644 --- a/wpsc-components/theme-engine-v2/helpers/checkout-results.php +++ b/wpsc-components/theme-engine-v2/helpers/checkout-results.php @@ -15,7 +15,7 @@ function transaction_results( $sessionid, $display_to_screen = true, $transactio // pre-3.8.9 variable $echo_to_screen = $display_to_screen; - $purchase_log_object = new WPSC_Purchase_Log( $sessionid, 'sessionid' ); + $purchase_log_object = wpsc_get_order( $sessionid, 'sessionid' ); // compatibility with pre-3.8.9 templates where they use a global // $purchase_log object which is simply just a database row diff --git a/wpsc-components/theme-engine-v2/mvc/controllers/checkout.php b/wpsc-components/theme-engine-v2/mvc/controllers/checkout.php index 68a05e5b57..f3e91ebff6 100644 --- a/wpsc-components/theme-engine-v2/mvc/controllers/checkout.php +++ b/wpsc-components/theme-engine-v2/mvc/controllers/checkout.php @@ -161,7 +161,7 @@ private function submit_review_order() { // Update the Purchase Log $purchase_log_id = wpsc_get_customer_meta( 'current_purchase_log_id' ); - $purchase_log = new WPSC_Purchase_Log( $purchase_log_id ); + $purchase_log = wpsc_get_order( $purchase_log_id ); $purchase_log->set( 'base_shipping', $wpsc_cart->calculate_base_shipping() ); $purchase_log->set( 'totalprice', $wpsc_cart->calculate_total_price() ); $purchase_log->save(); @@ -241,14 +241,14 @@ public function get_purchase_log() { $create = true; if ( $purchase_log_id ) { - $purchase_log = new WPSC_Purchase_Log( $purchase_log_id ); + $purchase_log = wpsc_get_order( $purchase_log_id ); $create = ! $purchase_log->exists(); } if ( $create ) { wpsc_delete_customer_meta( 'current_purchase_log_id' ); - $purchase_log = new WPSC_Purchase_Log(); + $purchase_log = wpsc_get_order(); $purchase_log->set( array( 'user_ID' => get_current_user_id(), @@ -452,7 +452,7 @@ private function submit_payment_method() { } $purchase_log_id = wpsc_get_customer_meta( 'current_purchase_log_id' ); - $purchase_log = new WPSC_Purchase_Log( $purchase_log_id ); + $purchase_log = wpsc_get_order( $purchase_log_id ); $submitted_gateway = $_POST['wpsc_payment_method']; $purchase_log->set( array( diff --git a/wpsc-components/theme-engine-v2/mvc/controllers/customer-account.php b/wpsc-components/theme-engine-v2/mvc/controllers/customer-account.php index 4d4906d167..3048faac7a 100644 --- a/wpsc-components/theme-engine-v2/mvc/controllers/customer-account.php +++ b/wpsc-components/theme-engine-v2/mvc/controllers/customer-account.php @@ -98,7 +98,7 @@ private function order( $id ) { $this->view = 'customer-account-order'; $form_data_obj = new WPSC_Checkout_Form_Data( $id ); $this->form = WPSC_Checkout_Form::get(); - $this->log = new WPSC_Purchase_Log( $id ); + $this->log = wpsc_get_order( $id ); $this->title = sprintf( __( 'View Order #%d', 'wp-e-commerce' ), $id diff --git a/wpsc-includes/checkout-form-data.class.php b/wpsc-includes/checkout-form-data.class.php index 417f5bfeff..179f7464e6 100644 --- a/wpsc-includes/checkout-form-data.class.php +++ b/wpsc-includes/checkout-form-data.class.php @@ -289,7 +289,7 @@ public function set( $key, $value = '' ) { */ public function save() { - $log = new WPSC_Purchase_Log( $this->log_id ); + $log = wpsc_get_order( $this->log_id ); $form = WPSC_Checkout_Form::get(); $fields = $form->get_fields(); diff --git a/wpsc-includes/google-analytics.class.php b/wpsc-includes/google-analytics.class.php index b8d54206b9..88bc8b3620 100644 --- a/wpsc-includes/google-analytics.class.php +++ b/wpsc-includes/google-analytics.class.php @@ -151,7 +151,7 @@ public function remove_currency_and_html( $args ) { } public function add_pushes( $session_id ) { - $purchase = new WPSC_Purchase_Log( $session_id, 'sessionid' ); + $purchase = wpsc_get_order( $session_id, 'sessionid' ); $purchase_id = $purchase->get( 'id' ); $data = new WPSC_Checkout_Form_Data( $purchase_id ); diff --git a/wpsc-includes/merchant.class.php b/wpsc-includes/merchant.class.php index 8fda50bd48..d8299f5914 100755 --- a/wpsc-includes/merchant.class.php +++ b/wpsc-includes/merchant.class.php @@ -278,7 +278,7 @@ function return_to_checkout() { * go to transaction results, if this changes and you extend this, your merchant module may go to the wrong place */ function go_to_transaction_results( $session_id ) { - $purchase_log = new WPSC_Purchase_Log( $this->purchase_id ); + $purchase_log = wpsc_get_order( $this->purchase_id ); //Now to do actions once the payment has been attempted switch ( $purchase_log->get( 'processed' ) ) { @@ -334,7 +334,7 @@ function set_transaction_details( $transaction_id, $status = 1 ) { */ function set_authcode( $authcode, $append = false ){ - $log = new WPSC_Purchase_Log( $this->purchase_id ); + $log = wpsc_get_order( $this->purchase_id ); $current_authcode = $log->get( 'authcode' ); if ( $append && ! empty( $current_authcode ) ) { diff --git a/wpsc-includes/purchase-log.class.php b/wpsc-includes/purchase-log.class.php index 03d79e5a26..4d26fc395c 100644 --- a/wpsc-includes/purchase-log.class.php +++ b/wpsc-includes/purchase-log.class.php @@ -6,7 +6,7 @@ define( 'WPSC_PURCHASE_LOG_STATS_CACHE_EXPIRE', DAY_IN_SECONDS * 2 ); } -class WPSC_Purchase_Log extends WPSC_Query_Base { +class WPSC_Purchase_Log extends WPSC_Query_Registry { const INCOMPLETE_SALE = 1; const ORDER_RECEIVED = 2; const ACCEPTED_PAYMENT = 3; @@ -139,6 +139,8 @@ class WPSC_Purchase_Log extends WPSC_Query_Base { protected $shipping_country = null; protected $payment_method = null; protected $shipping_method = null; + protected static $flag = false; + protected static $alt_ids = false; /** * Get the SQL query format for a column @@ -426,7 +428,7 @@ public function update_caches() { */ public static function delete_cache( $value, $col = 'id' ) { // this will pull from the old cache, so no worries there - $log = new WPSC_Purchase_Log( $value, $col ); + $log = self::get_instance( $value, $col ); $log->delete_caches( $value, $col ); } @@ -529,13 +531,15 @@ public function delete( $log_id = false ) { * create a new empty object. Otherwise, this will get the purchase log from the * DB either by using purchase log id or sessionid (specified by the 2nd argument). * + * It is preferred to use WPSC_Purchase_Log::get_instance() or wpsc_get_order(). + * * Eg: * * // get purchase log with ID number 23 - * $log = new WPSC_Purchase_Log( 23 ); + * $log = WPSC_Purchase_Log::get_instance( 23 ); * * // get purchase log with sessionid "asdf" - * $log = new WPSC_Purchase_Log( 'asdf', 'sessionid' ) + * $log = WPSC_Purchase_Log::get_instance( 'asdf', 'sessionid' ) * * @access public * @since 3.8.9 @@ -544,6 +548,12 @@ public function delete( $log_id = false ) { * @param string $col Optional. Defaults to 'id'. */ public function __construct( $value = false, $col = 'id' ) { + if ( ! self::$flag ) { + _wpsc_doing_it_wrong( 'wpsc_purchlog_notes_class_error', __( 'Please use `WPSC_Purchase_Log::get_instance( $log_id )` instead of `new WPSC_Purchase_Log( $log_id ).', 'wp-e-commerce' ), '3.12.0' ); + } + + parent::add_instance( $this ); + if ( false === $value ) { return; } @@ -586,6 +596,72 @@ public function __construct( $value = false, $col = 'id' ) { } } + /** + * Retrieve a WPSC_Purchase_Log instance. + * Get the log either by using purchase log id or sessionid (specified by the 2nd argument). + * + * Eg: + * + * // get purchase log with ID number 23 + * $log = WPSC_Purchase_Log::get_instance( 23 ); + * + * // get purchase log with sessionid "asdf" + * $log = WPSC_Purchase_Log::get_instance( 'asdf', 'sessionid' ) + * + * @since 3.12.0 + * + * @param string $value Optional. Defaults to false. + * @param string $col Optional. Defaults to 'id'. + * + * @return WPSC_Purchase_Log object instance. + */ + public static function get_instance( $value = false, $col = 'id' ) { + $instance = false; + + if ( $value && isset( self::$alt_ids[ $col ][ $value ] ) ) { + $instance = parent::_get_instance( __CLASS__, self::$alt_ids[ $col ][ $value ] ); + } + + if ( ! $instance ) { + self::$flag = true; + $instance = new self( $value, $col ); + self::$flag = false; + } + + $id = $instance->get( 'id' ); + $instance_id = $instance->instance_id(); + + // If fetched object matches one we already have (based on the ID), then use original instance. + if ( $id && 'id' !== $col && isset( self::$alt_ids['id'][ $id ] ) ) { + $existing = parent::_get_instance( __CLASS__, self::$alt_ids['id'][ $id ] ); + if ( $existing ) { + $instance = $existing; + $instance_id = $instance->instance_id(); + } + } + + if ( $value && 'id' !== $col ) { + self::$alt_ids[ $col ][ $value ] = $instance_id; + } + + if ( $id ) { + self::$alt_ids['id'][ $id ] = $instance_id; + } + + return $instance; + } + + /** + * Retrieves the unique identifier for a WPSC_Query_Base instance. + * + * @since 3.12.0 + * + * @return mixed + */ + public function instance_id() { + return spl_object_hash( $this ); + } + private function set_total_shipping() { $base_shipping = $this->get( 'base_shipping' ); @@ -1044,6 +1120,16 @@ public function save() { $this->save_meta(); } + $instance_id = $this->instance_id(); + + if ( $sessionid = $this->get( 'sessionid' ) ) { + self::$alt_ids['sessionid'][ $sessionid ] = $instance_id; + } + + if ( $id = $this->get( 'id' ) ) { + self::$alt_ids['id'][ $id ] = $instance_id; + } + do_action( 'wpsc_purchase_log_save', $this ); return $result; @@ -1138,7 +1224,7 @@ public static function get_log_by_meta( $key = '', $value = '' ) { $id = $wpdb->get_var( $sql ); if ( $id ) { - return new WPSC_Purchase_Log( $id ); + return self::get_instance( $id ); } else { return false; } diff --git a/wpsc-includes/purchase-log.helpers.php b/wpsc-includes/purchase-log.helpers.php index 028faabf05..464ffbc5eb 100644 --- a/wpsc-includes/purchase-log.helpers.php +++ b/wpsc-includes/purchase-log.helpers.php @@ -11,7 +11,7 @@ */ function wpsc_get_order( $order_id, $by = 'id' ) { $order = wpsc_is_order( $order_id ); - return $order ? $order : new WPSC_Purchase_Log( $order_id, $by ); + return $order ? $order : WPSC_Purchase_Log::get_instance( $order_id, $by ); } /** diff --git a/wpsc-includes/purchaselogs-items.class.php b/wpsc-includes/purchaselogs-items.class.php index ac8b76ebd1..c876984c54 100755 --- a/wpsc-includes/purchaselogs-items.class.php +++ b/wpsc-includes/purchaselogs-items.class.php @@ -23,7 +23,7 @@ public function __construct( $id, $purchase_log = null ) { $this->log = $purchase_log instanceof WPSC_Purchase_Log ? $purchase_log - : new WPSC_Purchase_Log( $this->purchlogid ); + : wpsc_get_order( $this->purchlogid ); $this->get_purchlog_details(); } diff --git a/wpsc-includes/purchaselogs.functions.php b/wpsc-includes/purchaselogs.functions.php index 5c08d3b4b3..c771e128c2 100755 --- a/wpsc-includes/purchaselogs.functions.php +++ b/wpsc-includes/purchaselogs.functions.php @@ -557,7 +557,7 @@ function wpsc_purchlog_edit_status( $purchlog_id = '', $purchlog_status = '' ) { $purchlog_status = absint( $_POST['new_status'] ); } - $purchase_log = new WPSC_Purchase_Log( $purchlog_id ); + $purchase_log = wpsc_get_order( $purchlog_id ); // In the future when everyone is using the 2.0 merchant api, // we should use the merchant class to update the staus, diff --git a/wpsc-merchants/paypal-standard.merchant.php b/wpsc-merchants/paypal-standard.merchant.php index 137aa2ee1c..4ce0085f85 100755 --- a/wpsc-merchants/paypal-standard.merchant.php +++ b/wpsc-merchants/paypal-standard.merchant.php @@ -137,7 +137,7 @@ function _construct_value_array( $aggregate = false ) { 'notify_url' => $notify_url, ); } - + // Customer details $paypal_vars += array( 'email' => $this->cart_data['email_address'], @@ -422,7 +422,7 @@ function parse_gateway_notification() { private function import_ipn_data() { global $wpdb; - $purchase_log = new WPSC_Purchase_Log( $this->cart_data['session_id'], 'sessionid' ); + $purchase_log = wpsc_get_order( $this->cart_data['session_id'], 'sessionid' ); if ( ! $purchase_log->exists() ) { return; @@ -582,7 +582,7 @@ public function is_valid_ipn_response() { $valid = false; } - $purchase_log = new WPSC_Purchase_Log( $this->cart_data['session_id'], 'sessionid' ); + $purchase_log = wpsc_get_order( $this->cart_data['session_id'], 'sessionid' ); if ( ! $purchase_log->exists() ) { $valid = false; @@ -932,7 +932,7 @@ function _wpsc_buy_now_transaction_results() { if ( ! isset( $_REQUEST['sessionid'] ) ) return; - $purchase_log = new WPSC_Purchase_Log( $_REQUEST['sessionid'], 'sessionid' ); + $purchase_log = wpsc_get_order( $_REQUEST['sessionid'], 'sessionid' ); if ( ! $purchase_log->exists() || $purchase_log->is_transaction_completed() ) return; diff --git a/wpsc-shipping/library/shipwire_functions.php b/wpsc-shipping/library/shipwire_functions.php index 842e07e33a..69725cd0ee 100755 --- a/wpsc-shipping/library/shipwire_functions.php +++ b/wpsc-shipping/library/shipwire_functions.php @@ -338,7 +338,7 @@ public static function send_order_request( $xml ) { */ public function shipwire_on_checkout( $log_id ) { - $log = new WPSC_Purchase_Log( $log_id ); + $log = wpsc_get_order( $log_id ); if ( ! $log->is_transaction_completed() ) { return false; diff --git a/wpsc-theme/functions/wpsc-transaction_results_functions.php b/wpsc-theme/functions/wpsc-transaction_results_functions.php index dff19ae268..6b06eecd42 100644 --- a/wpsc-theme/functions/wpsc-transaction_results_functions.php +++ b/wpsc-theme/functions/wpsc-transaction_results_functions.php @@ -15,7 +15,7 @@ function transaction_results( $sessionid, $display_to_screen = true, $transactio // pre-3.8.9 variable $echo_to_screen = $display_to_screen; - $purchase_log_object = new WPSC_Purchase_Log( $sessionid, 'sessionid' ); + $purchase_log_object = wpsc_get_order( $sessionid, 'sessionid' ); // compatibility with pre-3.8.9 templates where they use a global // $purchase_log object which is simply just a database row