diff --git a/wpsc-core/wpsc-includes.php b/wpsc-core/wpsc-includes.php index b3aa28378f..db6dfee947 100644 --- a/wpsc-core/wpsc-includes.php +++ b/wpsc-core/wpsc-includes.php @@ -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' ); diff --git a/wpsc-includes/purchase-log-notes.class.php b/wpsc-includes/purchase-log-notes.class.php index 2807b1a092..7eca8ff517 100644 --- a/wpsc-includes/purchase-log-notes.class.php +++ b/wpsc-includes/purchase-log-notes.class.php @@ -1,6 +1,6 @@ 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 ) ) { @@ -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 * diff --git a/wpsc-includes/purchase-log.class.php b/wpsc-includes/purchase-log.class.php index 2ff1eb5cdc..f8003d71e3 100644 --- a/wpsc-includes/purchase-log.class.php +++ b/wpsc-includes/purchase-log.class.php @@ -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; diff --git a/wpsc-includes/purchase-log.helpers.php b/wpsc-includes/purchase-log.helpers.php index aa4cccc77a..028faabf05 100644 --- a/wpsc-includes/purchase-log.helpers.php +++ b/wpsc-includes/purchase-log.helpers.php @@ -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 ) { diff --git a/wpsc-includes/query-registry.class.php b/wpsc-includes/query-registry.class.php new file mode 100644 index 0000000000..3ca6840439 --- /dev/null +++ b/wpsc-includes/query-registry.class.php @@ -0,0 +1,100 @@ +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(); + +}