-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraft-order-control.php
More file actions
264 lines (231 loc) · 6.37 KB
/
Copy pathdraft-order-control.php
File metadata and controls
264 lines (231 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
/**
* Plugin Name: Draft Order Control
* Plugin URI: https://github.com/davidrukahu/draft-order-control
* Description: Control when and how WooCommerce creates draft orders. Manage all 4 draft order creation conditions with individual enable/disable toggles.
* Version: 1.0.0
* Author: DavidR
* Author URI: https://github.com/davidrukahu
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: draft-order-control
* Requires at least: 6.0
* Requires PHP: 7.4
* WC requires at least: 8.0
* WC tested up to: 10.3
*
* @package DraftOrderControl
*/
defined( 'ABSPATH' ) || exit;
// Define plugin constants.
if ( ! defined( 'WCDOC_VERSION' ) ) {
define( 'WCDOC_VERSION', '1.0.0' );
}
if ( ! defined( 'WCDOC_PLUGIN_FILE' ) ) {
define( 'WCDOC_PLUGIN_FILE', __FILE__ );
}
if ( ! defined( 'WCDOC_PLUGIN_DIR' ) ) {
define( 'WCDOC_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'WCDOC_PLUGIN_URL' ) ) {
define( 'WCDOC_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'WCDOC_PLUGIN_BASENAME' ) ) {
define( 'WCDOC_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
}
/**
* Main plugin class.
*
* @class WCDOC_Plugin
*/
final class WCDOC_Plugin {
/**
* Plugin version.
*
* @var string
*/
public $version = WCDOC_VERSION;
/**
* The single instance of the class.
*
* @var WCDOC_Plugin
*/
protected static $_instance = null;
/**
* Main WCDOC_Plugin Instance.
*
* Ensures only one instance of WCDOC_Plugin is loaded or can be loaded.
*
* @return WCDOC_Plugin - Main instance.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Cloning is forbidden.
*/
public function __clone() {
wc_doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', 'draft-order-control' ), '1.0.0' );
}
/**
* Unserializing instances of this class is forbidden.
*/
public function __wakeup() {
wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'draft-order-control' ), '1.0.0' );
}
/**
* Constructor.
*/
public function __construct() {
$this->init_hooks();
}
/**
* Hook into actions and filters.
*/
private function init_hooks() {
add_action( 'before_woocommerce_init', array( $this, 'declare_feature_compatibility' ) );
add_action( 'plugins_loaded', array( $this, 'init' ), 0 );
register_activation_hook( WCDOC_PLUGIN_FILE, array( $this, 'activate' ) );
register_deactivation_hook( WCDOC_PLUGIN_FILE, array( $this, 'deactivate' ) );
}
/**
* Declare compatibility with WooCommerce features.
*/
public function declare_feature_compatibility() {
if ( ! class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
return;
}
// Declare compatibility with High-Performance Order Storage (HPOS).
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', WCDOC_PLUGIN_FILE, true );
// Declare compatibility with Cart and Checkout Blocks.
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'cart_checkout_blocks', WCDOC_PLUGIN_FILE, true );
}
/**
* Init plugin when WordPress Initialises.
*/
public function init() {
// Check if WooCommerce is active.
if ( ! $this->is_woocommerce_active() ) {
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
return;
}
// Load text domain.
$this->load_plugin_textdomain();
// Include required files.
$this->includes();
// Initialize plugin components.
$this->init_components();
}
/**
* Check if WooCommerce is active.
*
* @return bool
*/
private function is_woocommerce_active() {
return class_exists( 'WooCommerce' );
}
/**
* Display notice if WooCommerce is not active.
*/
public function woocommerce_missing_notice() {
?>
<div class="error">
<p>
<?php
echo esc_html__( 'WooCommerce Draft Order Control requires WooCommerce to be installed and active.', 'draft-order-control' );
?>
</p>
</div>
<?php
}
/**
* Load plugin text domain.
* Note: For WordPress.org hosted plugins, translations are loaded automatically.
*/
private function load_plugin_textdomain() {
// WordPress.org automatically loads translations for plugins.
// Only load manually if needed for non-WordPress.org installations.
if ( ! apply_filters( 'wcdoc_load_textdomain', false ) ) {
return;
}
// phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound -- Conditionally executed only for non-WordPress.org installations via filter.
load_plugin_textdomain(
'draft-order-control',
false,
dirname( WCDOC_PLUGIN_BASENAME ) . '/languages'
);
}
/**
* Include required core files.
*/
private function includes() {
require_once WCDOC_PLUGIN_DIR . 'includes/class-wcdoc-plugin.php';
require_once WCDOC_PLUGIN_DIR . 'includes/class-wcdoc-settings.php';
require_once WCDOC_PLUGIN_DIR . 'includes/class-wcdoc-controller.php';
if ( is_admin() ) {
require_once WCDOC_PLUGIN_DIR . 'admin/class-wcdoc-admin.php';
}
}
/**
* Initialize plugin components.
*/
private function init_components() {
WCDOC_Settings::instance();
WCDOC_Controller::instance();
if ( is_admin() ) {
WCDOC_Admin::instance();
}
}
/**
* Plugin activation.
*/
public function activate() {
// Set default options if they don't exist.
if ( ! get_option( 'wcdoc_settings' ) ) {
$default_settings = array(
'master_toggle' => true,
'conditions' => array(
'store_api_checkout' => array(
'enabled' => true,
'status' => 'checkout-draft',
),
'rest_api_v4_error' => array(
'enabled' => true,
'status' => 'checkout-draft',
),
'rest_api_v3_error' => array(
'enabled' => true,
'status' => 'checkout-draft',
),
'admin_manual' => array(
'enabled' => true,
'status' => 'auto-draft',
),
),
);
add_option( 'wcdoc_settings', $default_settings );
}
}
/**
* Plugin deactivation.
*/
public function deactivate() {
// Clean up any scheduled events if needed.
// Note: We keep settings intact on deactivation.
}
}
/**
* Main instance of WCDOC_Plugin.
*
* Returns the main instance of WCDOC to prevent the need to use globals.
*
* @return WCDOC_Plugin
*/
function WCDOC() {
return WCDOC_Plugin::instance();
}
// Initialize the plugin.
WCDOC();