Skip to content

Commit 214a93f

Browse files
committed
Introduce the singleton pattern; deprecate plugin global
1 parent 7a7e58c commit 214a93f

File tree

2 files changed

+79
-8
lines changed

2 files changed

+79
-8
lines changed

readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ Some other notes to help developers:
8787
If you want to access the order based on the sequential order number, you can do so with a helper method:
8888

8989
`
90-
$order_id = $GLOBALS['wc_seq_order_number']->find_order_by_order_number( $order_number );
90+
$order_id = wc_sequential_order_numbers()->find_order_by_order_number( $order_number );
9191
`
9292

9393
This will give you the order's ID (post ID), and you can get the order object from this.

woocommerce-sequential-order-numbers.php

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,15 @@
2828
return;
2929
}
3030

31-
/**
32-
* The WC_Seq_Order_Number global object
33-
* @name $wc_seq_order_number
34-
* @global WC_Seq_Order_Number $GLOBALS['wc_seq_order_number']
35-
*/
36-
$GLOBALS['wc_seq_order_number'] = new WC_Seq_Order_Number();
37-
3831
class WC_Seq_Order_Number {
3932

4033

4134
/** version number */
4235
const VERSION = '1.7.0';
4336

37+
/** @var \WC_Seq_Order_Number single instance of this plugin */
38+
protected static $instance;
39+
4440
/** version option name */
4541
const VERSION_OPTION_NAME = 'woocommerce_seq_order_number_db_version';
4642

@@ -60,6 +56,30 @@ public function __construct() {
6056
}
6157

6258

59+
/**
60+
* Cloning instances is forbidden due to singleton pattern.
61+
*
62+
* @since 1.7.0
63+
*/
64+
public function __clone() {
65+
66+
/* translators: Placeholders: %s - plugin name */
67+
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot clone instances of %s.', 'woocommerce-sequential-order-numbers' ), $this->get_plugin_name() ), '1.7.0' );
68+
}
69+
70+
71+
/**
72+
* Unserializing instances is forbidden due to singleton pattern.
73+
*
74+
* @since 1.7.0
75+
*/
76+
public function __wakeup() {
77+
78+
/* translators: Placeholders: %s - plugin name */
79+
_doing_it_wrong( __FUNCTION__, sprintf( esc_html__( 'You cannot unserialize instances of %s.', 'woocommerce-sequential-order-numbers' ), $this->get_plugin_name() ), '1.7.0' );
80+
}
81+
82+
6383
/**
6484
* Initialize the plugin, bailing if any required conditions are not met,
6585
* including minimum WooCommerce version
@@ -335,6 +355,21 @@ public function subscriptions_remove_renewal_order_meta( $order_meta_query ) {
335355
/** Helper Methods ******************************************************/
336356

337357

358+
/**
359+
* Main Sequential Order Numbers Instance, ensures only one instance is/can be loaded
360+
*
361+
* @since 1.7.0
362+
* @see wc_sequential_order_numbers()
363+
* @return \WC_Seq_Order_Number
364+
*/
365+
public static function instance() {
366+
if ( is_null( self::$instance ) ) {
367+
self::$instance = new self();
368+
}
369+
return self::$instance;
370+
}
371+
372+
338373
/**
339374
* Checks if WooCommerce is active
340375
*
@@ -496,3 +531,39 @@ private function upgrade( $installed_version ) {
496531

497532

498533
}
534+
535+
536+
/**
537+
* Returns the One True Instance of Sequential Order Numbers
538+
*
539+
* @since 1.7.0
540+
* @return \WC_Seq_Order_Number
541+
*/
542+
function wc_sequential_order_numbers() {
543+
return WC_Seq_Order_Number::instance();
544+
}
545+
546+
547+
/**
548+
* Returns the One True Instance of Sequential Order Numbers after warning that
549+
* the global has been deprecated
550+
*
551+
* @since 1.7.0
552+
* @return \WC_Seq_Order_Number
553+
*/
554+
function wc_sequential_order_numbers_deprecated_global() {
555+
556+
/* @deprecated since 1.7.0 */
557+
_deprecated_function( "\$GLOBALS['wc_seq_order_number']", '1.7.0', 'wc_sequential_order_numbers()' );
558+
559+
return wc_sequential_order_numbers();
560+
}
561+
562+
563+
/**
564+
* The WC_Seq_Order_Number global object
565+
* @deprecated 1.7.0
566+
* @name $wc_seq_order_number
567+
* @global WC_Seq_Order_Number $GLOBALS['wc_seq_order_number']
568+
*/
569+
$GLOBALS['wc_seq_order_number'] = wc_sequential_order_numbers_deprecated_global();

0 commit comments

Comments
 (0)