-
Notifications
You must be signed in to change notification settings - Fork 0
/
wppdev-woocommerce-order-status.php
291 lines (244 loc) · 6.69 KB
/
wppdev-woocommerce-order-status.php
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* Plugin Name: WPPluginsDev WooCommerce Order Status
* Plugin URI: https://wpplugins.dev
* Description: Manage Woo Order Status and Workflow
* Version: 1.1.1
* Author: WPPluginsDev
* Author URI: https://wpplugins.dev
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Domain Path: /app/languages
* Text Domain: wppdev-woocommerce-order-status
*/
/**
* Copyright notice
*
* @copyright WPPluginDev (https://wpplugins.dev/)
*
* Authors: Fabio Jun, Leandro Yukiu
*
* @license http://opensource.org/licenses/GPL-2.0 GNU General Public License, version 2 (GPL-2.0)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2, as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see https://www.gnu.org/licenses/gpl-2.0.html.
*/
/**
* Plugin name dir constant.
*
*/
define( 'WPPDEV_WO_PLUGIN_NAME', dirname( plugin_basename( __FILE__ ) ) );
/**
* Plugin version
*
*/
define( 'WPPDEV_WO_PLUGIN_VERSION', '1.1.1' );
/**
* Plugin base dir
*/
define( 'WPPDEV_WO_PLUGIN_BASE_DIR', dirname( __FILE__ ) );
include WPPDEV_WO_PLUGIN_BASE_DIR . '/vendor/autoload.php';
use WPPluginsDev\Traits\Trait_Hook;
use WPPluginsDev\Helpers\Helper_Debug;
use WPPluginsDev\Helpers\Helper_Util;
use WPPluginsDev\WooOrderWorkflow\Controllers\Controller_Plugin;
use WPPluginsDev\WooOrderWorkflow\Controllers\Rest\Controller_Rest_Status;
use WPPluginsDev\WooOrderWorkflow\Models\Model_Status;
use WPPluginsDev\WooOrderWorkflow\Models\Model_Settings;
/**
* Primary plugin class.
*
* Initialize required plugin hooks.
* Init CPT and Rest points registration.
*
* Control of plugin is passed to the MVC implementation found
* inside the /app/ folder.
*
* @return object Plugin instance.
*/
class WooOrderWorkflowPlugin {
/**
* WP hooking wrapper.
*/
use Trait_Hook;
/**
* Singleton instance of the plugin.
*/
private static $instance = null;
/**
* The plugin name.
*/
private $name;
/**
* The plugin version.
*/
private $version;
/**
* The plugin file.
*/
private $file;
/**
* The plugin path.
*/
private $dir;
/**
* The plugin URL.
*/
private $url;
/**
* The main controller of the plugin.
*/
private $controller;
/**
* Plugin constructor.
*
* Set properties, registers hooks and loads the plugin.
*/
public function __construct() {
/** Setup plugin properties */
$this->name = WPPDEV_WO_PLUGIN_NAME;
$this->version = WPPDEV_WO_PLUGIN_VERSION;
$this->file = __FILE__;
$this->dir = plugin_dir_path( __FILE__ );
$this->url = plugin_dir_url( __FILE__ );
$this->basename = plugin_basename( __FILE__ );
$this->add_filter( 'plugin_action_links_' . $this->basename, 'add_settings_link' );
$this->add_action( 'init', 'register_custom_post_types', 1 );
/**
* Hooks init to create the primary plugin controller.
*/
$this->add_action( 'init', 'plugin_constructing' );
$this->add_action( 'rest_api_init', 'rest_api_init' );
$this->add_action( 'plugins_loaded', 'plugin_localization' );
register_activation_hook( __FILE__, [ $this, 'install' ] );
// $this->uninstall();
/** Grab instance of self. */
self::$instance = $this;
}
/**
* Add Settings Link in admin plugins list.
*/
public function add_settings_link( $links ) {
$links[] = sprintf(
'<a href="%s">%s</a>',
esc_url( Controller_Plugin::get_admin_settings_url() ),
__( 'Settings', 'wppdev-woocommerce-order-status' ),
);
return $links;
}
/**
* Loads primary plugin controllers.
*/
public function plugin_constructing() {
$this->controller = new Controller_Plugin( $this );
}
/**
* Register REST API Routes.
* Create Controllers to handle REST calls.
*/
public function rest_api_init() {
$controllers = array(
'Controller_Rest_Status' => new Controller_Rest_Status(),
);
foreach( $controllers as $controller ) {
$controller->register_routes();
}
}
/**
* Register plugin custom post types.
*
* @return void
*/
public function register_custom_post_types() {
$cpts = array(
Model_Status::$POST_TYPE => Model_Status::get_register_post_type_args(),
);
foreach ( $cpts as $cpt => $args ) {
Helper_Util::register_post_type( $cpt, $args );
}
}
/**
* Flush rewrite rules.
* Flush and save flushed status.
*/
public function flush_rewrite_rules( $force = false ) {
$flushed = Model_Settings::get_custom_setting( 'general', 'flushed_rewrite_rules' );
if( $force || empty( $flushed ) ) {
flush_rewrite_rules( true );
Model_Settings::set_custom_setting( 'general', 'flushed_rewrite_rules', true );
}
}
/**
* Load plugin localization files.
* Place files in plugin-dir/languages folder.
* ex: woocommerce-order-workflow-pt_BR.mo
*/
public function plugin_localization() {
load_plugin_textdomain(
'wppdev-woocommerce-order-status',
false,
WPPDEV_WO_PLUGIN_NAME . '/languages'
);
}
/**
* Load core order statuses.
*
* Register uninstall hook.
*/
public function install() {
register_uninstall_hook( __FILE__, [ $this, 'uninstall' ] );
$core = Model_Status::get_core_statuses();
if( empty( $core ) ) {
Model_Status::load_core_statuses();
}
}
/**
* Clear all order status custom post type.
*/
public function uninstall() {
error_log( "UNINSTALL " . __FILE__ );
$statuses = Model_Status::get_statuses();
foreach( $statuses as $status ) {
$status->delete();
error_log( "Deleted order status id: $status->id, slug: $status->slug" );
}
}
/**
* Get singleton instance of the plugin.
*
* @return plugin instance
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Returns property associated with the plugin.
*
* @access public
* @param string $property The name of a property.
* @return mixed Returns mixed value of a property or NULL if a property doesn't exist.
*/
public function __get( $property ) {
if ( property_exists( $this, $property ) ) {
return $this->$property;
}
}
}
/**
* Create an instance of the plugin object.
*
* This is the primary entry point for the plugin.
*/
WooOrderWorkflowPlugin::instance();