forked from getdokan/wepos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wepos.php
470 lines (400 loc) · 13 KB
/
wepos.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
/*
Plugin Name: wePOS - Point Of Sale (POS) for WooCommerce
Plugin URI: https://wedevs.com/wepos
Description: A beautiful and fast Point of Sale (POS) system for WooCommerce
Version: 1.1.1
Author: weDevs
Author URI: https://wedevs.com/
Text Domain: wepos
Domain Path: /languages
WC requires at least: 3.0
WC tested up to: 3.8.1
License: GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
/**
* Copyright (c) YEAR weDevs (email: [email protected]). All rights reserved.
*
* Released under the GPL license
* http://www.opensource.org/licenses/gpl-license.php
*
* This is an add-on for WordPress
* http://wordpress.org/
*
* **********************************************************************
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* **********************************************************************
*/
// don't call the file directly
if ( !defined( 'ABSPATH' ) ) exit;
/**
* We_POS class
*
* @class We_POS The class that holds the entire We_POS plugin
*/
final class We_POS {
/**
* Plugin version
*
* @var string
*/
public $version = '1.1.1';
/**
* Holds various class instances
*
* @var array
*/
private $container = [];
/**
* Constructor for the We_POS class
*
* Sets up all the appropriate hooks and actions
* within our plugin.
*/
public function __construct() {
$this->define_constants();
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
add_action( 'init', [ $this, 'add_rewrite_rules' ] );
add_filter( 'query_vars', [ $this, 'register_query_var' ] );
add_action( 'plugins_loaded', [ $this, 'init_gateways' ], 11, 1 );
add_action( 'woocommerce_loaded', array( $this, 'init_plugin' ) );
add_action( 'woocommerce_init', array( $this, 'on_wc_init' ) );
// Handle appseror tracker
$this->appsero_init_tracker_wepos();
}
/**
* Is WC active
*
* @since 1.0.8
*
* @return void
*/
public function is_wc_active() {
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'wepos_active_plugins', get_option( 'active_plugins' ) ) ) ) {
return true;
}
return false;
}
/**
* Add the required rewrite rules
*
* @return void
*/
public function add_rewrite_rules() {
add_rewrite_rule( '^wepos/?$', 'index.php?wepos=true', 'top' );
if ( get_transient( 'wepos-flush-rewrites' ) ) {
flush_rewrite_rules( true );
delete_transient( 'wepos-flush-rewrites' );
}
}
/**
* Register our query vars
*
* @param array $vars
*
* @return array
*/
public function register_query_var( $vars ) {
$vars[] = 'wepos';
return $vars;
}
/**
* Available Gateway
*
* @since 1.0.0
*
* @return void
*/
public function available_gateway() {
return apply_filters( 'wepos_register_gateway', [
'WePOS\gateways\Cash' => WEPOS_INCLUDES . '/gateways/class-cash-gateway.php'
] );
}
/**
* Init all gatewas
*
* @since 1.0.0
*
* @return void
*/
public function init_gateways() {
if ( ! $this->is_wc_active() ) {
return;
}
$gateways = $this->available_gateway();
foreach ( $gateways as $class => $path ) {
require_once $path;
}
}
/**
* Initializes the We_POS() class
*
* Checks for an existing We_POS() instance
* and if it doesn't find one, creates it.
*/
public static function init() {
static $instance = false;
if ( ! $instance ) {
$instance = new We_POS();
}
return $instance;
}
/**
* Magic getter to bypass referencing plugin.
*
* @param $prop
*
* @return mixed
*/
public function __get( $prop ) {
if ( array_key_exists( $prop, $this->container ) ) {
return $this->container[ $prop ];
}
return $this->{$prop};
}
/**
* Magic isset to bypass referencing plugin.
*
* @param $prop
*
* @return mixed
*/
public function __isset( $prop ) {
return isset( $this->{$prop} ) || isset( $this->container[ $prop ] );
}
/**
* Define the constants
*
* @return void
*/
public function define_constants() {
define( 'WEPOS_VERSION', $this->version );
define( 'WEPOS_FILE', __FILE__ );
define( 'WEPOS_PATH', dirname( WEPOS_FILE ) );
define( 'WEPOS_INCLUDES', WEPOS_PATH . '/includes' );
define( 'WEPOS_URL', plugins_url( '', WEPOS_FILE ) );
define( 'WEPOS_ASSETS', WEPOS_URL . '/assets' );
}
/**
* Load the plugin after all plugis are loaded
*
* @return void
*/
public function init_plugin() {
$this->includes();
$this->init_hooks();
do_action( 'wepos_loaded' );
}
/**
* Placeholder for activation function
*
* Nothing being called here yet.
*/
public function activate() {
if ( ! function_exists( 'WC' ) ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( '<div class="error"><p>' . sprintf( wp_kses_post( '<b>WePOS</b> requires <a href="%s">WooCommerce</a> to be installed & activated! Go back your <a href="%s">Plugin page</a>', 'wepos' ), 'https://wordpress.org/plugins/woocommerce/', esc_url( admin_url( 'plugins.php' ) ) ) . '</p></div>' );
}
$installed = get_option( 'we_pos_installed' );
if ( ! $installed ) {
update_option( 'we_pos_installed', time() );
}
if ( function_exists( 'dokan' ) ) {
$users_query = new WP_User_Query( array(
'role__in' => [ 'seller', 'vendor_staff' ]
) );
$users = $users_query->get_results();
if ( count( $users ) > 0 ) {
foreach ( $users as $user ) {
$user->add_cap( 'publish_shop_orders' );
$user->add_cap( 'list_users' );
}
}
}
update_option( 'we_pos_version', WEPOS_VERSION );
set_transient( 'wepos-flush-rewrites', 1 );
}
/**
* Placeholder for deactivation function
*
* Nothing being called here yet.
*/
public function deactivate() {
$users_query = new WP_User_Query( array(
'role__in' => [ 'seller', 'vendor_staff' ]
) );
$users = $users_query->get_results();
if ( count( $users ) > 0 ) {
foreach ( $users as $user ) {
$user->remove_cap( 'publish_shop_orders' );
$user->remove_cap( 'list_users' );
}
}
}
/**
* Include the required files
*
* @return void
*/
public function includes() {
require_once WEPOS_INCLUDES . '/functions.php';
require_once WEPOS_INCLUDES . '/class-assets.php';
if ( $this->is_request( 'admin' ) ) {
require_once WEPOS_INCLUDES . '/admin/class-admin.php';
require_once WEPOS_INCLUDES . '/admin/class-settings.php';
require_once WEPOS_INCLUDES . '/admin/class-products.php';
require_once WEPOS_INCLUDES . '/admin/class-updates.php';
}
if ( $this->is_request( 'frontend' ) ) {
require_once WEPOS_INCLUDES . '/class-frontend.php';
}
if ( class_exists( 'WeDevs_Dokan' ) ) {
require_once WEPOS_INCLUDES . '/class-dokan.php';
}
require_once WEPOS_INCLUDES . '/class-rest-api.php';
}
/**
* Initialize the hooks
*
* @return void
*/
public function init_hooks() {
add_action( 'init', array( $this, 'init_classes' ) );
add_action( 'init', array( $this, 'localization_setup' ) );
}
/**
* Instantiate the required classes
*
* @return void
*/
public function init_classes() {
if ( $this->is_request( 'admin' ) ) {
$this->container['admin'] = new WePOS\Admin\Admin();
$this->container['settings'] = new WePOS\Admin\Settings();
new WePOS\Admin\Products();
new WePOS\Admin\Updates();
}
if ( $this->is_request( 'frontend' ) ) {
$this->container['frontend'] = new WePOS\Frontend();
}
if ( class_exists( 'WeDevs_Dokan' ) ) {
$this->container['dokan'] = new WePOS\Dokan();
}
$this->container['rest'] = new WePOS\REST_API();
$this->container['assets'] = new WePOS\Assets();
}
/**
* Initialize plugin for localization
*
* @uses load_plugin_textdomain()
*/
public function localization_setup() {
load_plugin_textdomain( 'wepos', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* What type of request is this?
*
* @param string $type admin, ajax, cron or frontend.
*
* @return bool
*/
private function is_request( $type ) {
switch ( $type ) {
case 'admin' :
return is_admin();
case 'ajax' :
return defined( 'DOING_AJAX' );
case 'cron' :
return defined( 'DOING_CRON' );
case 'frontend' :
return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
}
}
/**
* On WC init, include cart required files in REST request
*
* @since 1.0.5
*
* @return void
*/
public function on_wc_init() {
if ( wc()->is_rest_api_request() ) {
$namespace = '/wepos/v1/';
$rest_bases = [
'products',
];
foreach ( $rest_bases as $rest_base ) {
$endpoint = $namespace . $rest_base;
if ( strpos( $_SERVER['REQUEST_URI'], $endpoint ) ) {
$this->include_wc_files();
break;
}
}
}
}
/**
* Initialize the plugin tracker
*
* @return void
*/
function appsero_init_tracker_wepos() {
if ( ! class_exists( 'Appsero\Client' ) ) {
require_once WEPOS_PATH . '/lib/appsero/src/Client.php';
}
$client = new Appsero\Client( '48fa1273-3e91-4cd6-9c07-d18ad6bc2f54', 'wePos', __FILE__ );
// Active insights
$client->insights()
->add_extra( function() {
$products = wc_get_products( [ 'fields' => 'ids', 'paginate' => true ] );
$orders = wc_get_orders( [ 'fields' => 'ids', 'paginate' => true ] );
return [
'products' => $products->total,
'orders' => $orders->total
];
} )
->init();
}
/**
* Include cart required files in REST request
*
* @since 1.0.5
*
* @return void
*/
public function include_wc_files() {
if ( ! wc()->cart ) {
include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
include_once WC_ABSPATH . 'includes/class-wc-cart.php';
include_once WC_ABSPATH . 'includes/class-wc-tax.php';
include_once WC_ABSPATH . 'includes/class-wc-shipping-zones.php';
include_once WC_ABSPATH . 'includes/class-wc-customer.php';
include_once WC_ABSPATH . 'includes/class-wc-session-handler.php';
// Session class, handles session data for users - can be overwritten if custom handler is needed.
$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
wc()->session = new $session_class();
wc()->session->init();
wc()->customer = new WC_Customer( get_current_user_id(), true );
// Cart needs the customer info.
wc()->cart = new WC_Cart();
// Customer should be saved during shutdown.
add_action( 'shutdown', array( wc()->customer, 'save' ), 10 );
}
}
} // We_POS
$wepos = We_POS::init();