Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions wpsc-core/wpsc-includes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_once( WPSC_FILE_PATH . '/wpsc-includes/wpsc-meta-util.php' );
require_once( WPSC_FILE_PATH . '/wpsc-includes/wpsc-deprecated-meta.php' );
require_once( WPSC_FILE_PATH . '/wpsc-includes/query-base.class.php' );
require_once( WPSC_FILE_PATH . '/wpsc-includes/query-registry.class.php' );
require_once( WPSC_FILE_PATH . '/wpsc-includes/customer.php' );
require_once( WPSC_FILE_PATH . '/wpsc-includes/wpsc-meta-customer.php' );
require_once( WPSC_FILE_PATH . '/wpsc-includes/wpsc-meta-visitor.php' );
Expand Down
47 changes: 42 additions & 5 deletions wpsc-includes/purchase-log-notes.class.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

class WPSC_Purchase_Log_Notes extends WPSC_Query_Base implements Iterator {
class WPSC_Purchase_Log_Notes extends WPSC_Query_Registry implements Iterator {
const TYPE_DEFAULT = 0;
const TYPE_ERROR = 1;
const STATUS_PUBLIC = 0;
Expand Down Expand Up @@ -29,12 +29,15 @@ class WPSC_Purchase_Log_Notes extends WPSC_Query_Base implements Iterator {

protected static $map_text = array();
protected $log = null;
protected static $flag = false;

public function __construct( $log ) {
if ( $log instanceof WPSC_Purchase_Log ) {
$this->log = $log;
} else {
$this->log = new WPSC_Purchase_Log( $log );
$this->log = wpsc_get_order( $log );

parent::add_instance( $this );

if ( ! self::$flag ) {
_wpsc_doing_it_wrong( 'wpsc_purchlog_notes_class_error', __( 'Please use `WPSC_Purchase_Log_Notes::get_instance( $log_id )` instead of `new WPSC_Purchase_Log_Notes( $log_id ).', 'wp-e-commerce' ), '3.12.0' );
}

if ( empty( self::$map_text ) ) {
Expand All @@ -51,6 +54,40 @@ public function __construct( $log ) {
}
}

/**
* Retrieve a WPSC_Purchase_Log_Notes instance by instance id.
*
* @since 3.12.0
*
* @param string $instance_id A WPSC_Purchase_Log_Notes instance id.
*
* @return WPSC_Purchase_Log_Notes object instance.
*/
public static function get_instance( $log_id ) {
$log = wpsc_get_order( $log_id );

$instance = parent::_get_instance( __CLASS__, $log->get( 'id' ) );

if ( ! $instance ) {
self::$flag = true;
$instance = new self( $log );
self::$flag = false;
}

return $instance;
}

/**
* Retrieves the unique identifier for a WPSC_Query_Base instance.
*
* @since 3.12.0
*
* @return mixed
*/
public function instance_id() {
return $this->log->get( 'id' );
}

/**
* Fetches the actual record from the database
*
Expand Down
7 changes: 1 addition & 6 deletions wpsc-includes/purchase-log.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1586,12 +1586,7 @@ public function get_remaining_refund() {
* @return WPSC_Purchase_Log The current object (for method chaining)
*/
public function add_note( $note_text ) {
static $notes = null;

if ( ! ( $notes instanceof WPSC_Purchase_Log_Notes ) ) {
$notes = wpsc_get_order_notes( $this );
}

$notes = wpsc_get_order_notes( $this );
$notes->add( $note_text )->save();

return $this;
Expand Down
2 changes: 1 addition & 1 deletion wpsc-includes/purchase-log.helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ function wpsc_is_order( $order ) {
* @return WPSC_Purchase_Log
*/
function wpsc_get_order_notes( $order_id ) {
return new WPSC_Purchase_Log_Notes( $order_id );
return WPSC_Purchase_Log_Notes::get_instance( $order_id );
}

function wpsc_get_plaintext_table( $headings, $rows ) {
Expand Down
100 changes: 100 additions & 0 deletions wpsc-includes/query-registry.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* The WP eCommerce Base Query Registry Class
*
* @package wp-e-commerce
* @since 3.12.0
*/

abstract class WPSC_Query_Registry extends WPSC_Query_Base {

/**
* Array of all instances.
*
* @since 3.12.0
* @var array
*/
protected static $instances = array();

/**
* Add a WPSC_Query_Base instance object to the registry.
*
* @since 3.12.0
*
* @param WPSC_Query_Base $instance WPSC_Query_Base instance.
*/
protected static function add_instance( WPSC_Query_Base $instance ) {
self::$instances[ get_class( $instance ) ][ $instance->instance_id() ] = $instance;

return $instance;
}

/**
* Remove a WPSC_Query_Base instance object from the registry.
*
* @since 3.12.0
*
* @param string $instance_id A WPSC_Query_Base instance id.
* @param string $class_name The name of the instance class. Optional if $instance_id is an object.
*/
protected static function remove_instance( $instance_id, $class_name = '' ) {
$class_name = is_object( $instance_id ) ? get_class( $instance_id ) : $class_name;
if ( empty( $class_name ) ) {
throw new Exception( sprintf( __( '%s requires a class name be provided', 'wp-e-commerce' ), __METHOD__ ), __LINE__ );
}

if ( isset( self::$instances[ $class_name ][ $instance_id ] ) ) {
unset( self::$instances[ $class_name ][ $instance_id ] );
}
}

/**
* Retrieve a WPSC_Query_Base instance by instance id.
* a `get_instance` method is required in extended class.
* Extended method should call:
* `parent::_get_instance( __CLASS__, $id )`,
* and if not found, call:
* `$instance = parent::add_instance( new self( $log ) );`
*
* @since 3.12.0
*
* @param string $class_name The name of the instance class.
* @param string $instance_id A WPSC_Query_Base instance id.
*
* @return WPSC_Query_Base|bool False or WPSC_Query_Base object instance.
*/
protected static function _get_instance( $class_name, $instance_id ) {
if ( empty( self::$instances[ $class_name ][ $instance_id ] ) ) {
return false;
}

return self::$instances[ $class_name ][ $instance_id ];
}

/**
* Retrieve all WPSC_Query_Base instances registered.
*
* @since 3.12.0
*
* @param string $class_name The name of the class for which to fetch all instances.
*
* @return WPSC_Query_Base[] Array of all registered instance instances.
*/
public static function get_all( $class_name = '' ) {
if ( $class_name ) {
return isset( self::$instances[ $class_name ] ) ? self::$instances[ $class_name ] : array();
}

return self::$instances;
}

/**
* Retrieves the unique identifier for a WPSC_Query_Base instance.
*
* @since 3.12.0
*
* @return mixed
*/
abstract public function instance_id();

}