-
Notifications
You must be signed in to change notification settings - Fork 8
/
woo-nfe.php
368 lines (315 loc) · 8.78 KB
/
woo-nfe.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
<?php
/**
* NFe for Woocommerce plugin.
*
* @author NFe.io
*
* @see https://github.com/nfe/woo-nfe
* @since 1.0.8
* @package Woo_Nfe
*
* @wordpress-plugin
* Plugin Name: NFe for Woocommerce
* Plugin URI: https://github.com/nfe/woo-nfe
* Description: Extension for connecting to NFe.io API
* Version: 1.3.1
* Author: NFe.io
* Author URI: https://nfe.io
* Developer: Project contributors
* Developer URI: https://github.com/nfe/woo-nfe/graphs/contributors
* Text Domain: woo-nfe
* Domain Path: /languages
* Network: false
*
* WC requires at least: 4.7.0
* WC tested up to: 6.1.0
*
* Copyright: © 2022 NFe.io
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WooCommerce_NFe' ) ) {
/**
* WooCommerce NFe.io Main Class.
*
* @since 1.0.0
*/
final class WooCommerce_NFe {
/**
* Flag to indicate whether this extension is running already.
*
* @var bool
*/
private $is_running = false;
/**
* A dummy constructor to prevent WooCommerce_NFe from being loaded more than once.
*
* @since 1.0.0
* @see WooCommerce_NFe::instance()
*/
public function __construct() {
// Do nothing here.
}
/**
* Main instance.
*
* @return instance
* @since 1.0.0
*
*/
public static function instance() {
// Store the instance locally to avoid private static replication.
static $instance = null;
// Only run these methods if they haven't been run previously.
if ( null === $instance ) {
$instance = new WooCommerce_NFe();
$instance->setup_globals();
$instance->dependencies();
$instance->includes();
$instance->setup_hooks();
}
// Always return the instance.
return $instance;
}
//
// **
// * Constructor.
// *
// * @param string $file The full path and filename of the file of plugin's
// * main file.
// * @param string $version The full path and filename of the file of plugin's
// * main file.
// */
// public function __construct( $file, $version ) {
// $this->file = $file;
// $this->version = $version;
// }
/**
* Run the extension.
*
* @return bool Returns true when it's running
*/
public function run() {
if ( $this->is_running ) {
return false;
}
$this->is_running = true;
return $this->is_running;
}
/**
* Load Localisation files.
*
* Note: the first-loaded translation file overrides any following ones if the same translation is present.
*
* Locales found in:
* - WP_LANG_DIR/woo-nfe/woo-nfe-LOCALE.mo
* - WP_LANG_DIR/plugins/woo-nfe-LOCALE.mo
*/
public function load_plugin_textdomain() {
$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
$locale = apply_filters( 'plugin_locale', $locale, 'woo-nfe' );
unload_textdomain( 'woo-nfe' );
load_textdomain( 'woo-nfe', WP_LANG_DIR . '/woo-nfe/woo-nfe-' . $locale . '.mo' );
load_plugin_textdomain( 'woo-nfe', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Adds our custom WC_NFe_Integration integration to WooCommerce.
*
* @param array $integrations wooCommerce Integrations.
*
* @return array
* @since 1.0.0
*
*/
public function nfe_integration( $integrations ) {
$integrations[] = 'WC_NFe_Integration';
return $integrations;
}
/**
* SOAPClient missing notice.
*
* @since 1.0.0
*/
public function soap_missing_notice() {
include $this->includes_dir . 'admin/views/html-notice-missing-soap-client.php';
}
/**
* WooCommerce missing notice.
*
* @since 1.0.0
*/
public function woocommerce_missing_notice() {
include $this->includes_dir . 'admin/views/html-notice-missing-woocommerce.php';
}
/**
* Action links.
*
* @param array $links links.
*
* @return array
* @since 1.0.0
*
*/
public function plugin_action_links( $links ) {
return array_merge(
array(
'<a href="' . esc_url( WOOCOMMERCE_NFE_SETTINGS_URL ) . '">' . __( 'Settings', 'woo-nfe' ) . '</a>',
),
$links
);
}
/**
* Sets some globals for the plugin.
*
* @since 1.0.0
*/
private function setup_globals() {
$this->domain = 'woo-nfe';
$this->name = 'WooCommerce NFe';
$this->file = __FILE__;
$this->basename = plugin_basename( $this->file );
$this->plugin_dir = plugin_dir_path( $this->file );
$this->plugin_url = plugin_dir_url( $this->file );
$this->includes_dir = trailingslashit( $this->plugin_dir . 'includes' );
// WooCommerce Webhook Callback.
if ( ! defined( 'WC_API_CALLBACK' ) ) {
define( 'WC_API_CALLBACK', 'nfe_webhook' );
}
}
/**
* Include needed files.
*
* @since 1.0.0
*/
private function includes() {
// NFe Client-PHP API.
require $this->plugin_dir . 'li/client-php/lib/init.php';
// Admin.
require $this->includes_dir . 'nfe-functions.php';
require $this->includes_dir . 'admin/class-settings.php';
require $this->includes_dir . 'admin/class-ajax.php';
require $this->includes_dir . 'admin/class-admin.php';
require $this->includes_dir . 'admin/class-api.php';
require $this->includes_dir . 'admin/class-emails.php';
require $this->includes_dir . 'admin/class-webhook.php';
// Front-end.
require $this->includes_dir . 'frontend/class-frontend.php';
}
/**
* Class dependencies.
*/
private function dependencies() {
// Check for SOAP.
if ( ! class_exists( 'SoapClient' ) ) {
add_action( 'admin_notices', array( $this, 'soap_missing_notice' ) );
return;
}
// Checks if WooCommerce is installed and with the proper version.
if ( ! $this->version_check() ) {
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
return;
}
}
/**
* Set hooks.
*
* @since 1.0.0
*/
private function setup_hooks() {
// Set up localisation.
$this->load_plugin_textdomain();
$settings_url = admin_url( 'admin.php?page=woocommerce_settings&tab=integration§ion=woo-nfe' );
if ( $this->version_check( '2.1' ) ) {
$settings_url = admin_url( 'admin.php?page=wc-settings&tab=integration§ion=woo-nfe' );
}
if ( ! defined( 'WOOCOMMERCE_NFE_SETTINGS_URL' ) ) {
define( 'WOOCOMMERCE_NFE_SETTINGS_URL', $settings_url );
}
if ( ! defined( 'WOOCOMMERCE_NFE_PATH' ) ) {
define( 'WOOCOMMERCE_NFE_PATH', plugin_dir_path( $this->file ) );
}
// Filters.
add_filter( 'woocommerce_integrations', array( $this, 'nfe_integration' ) );
add_filter( 'plugin_action_links_' . $this->basename, array( $this, 'plugin_action_links' ) );
}
/**
* Version check.
*
* @param string $version version to check against.
*
* @return bool
*/
private function version_check( $version = '3.5.1' ) {
if ( class_exists( 'WooCommerce' ) ) {
global $woocommerce;
if ( version_compare( $woocommerce->version, $version, '>=' ) ) {
return true;
}
}
return false;
}
/**
* Version check 1.
*
* @param string $version version to check against.
*
* @return bool
*/
private function version_check1( $version ) {
if ( class_exists( 'WooCommerce' ) ) {
global $woocommerce;
if ( version_compare( $version, $woocommerce->version, '>=' ) ) {
return true;
}
}
return false;
}
}
/**
* The main function responsible for returning the one true WooCommerce_NFe Instance.
*
* @return WooCommerce_NFe
* @since 1.0.0
*
*/
function woo_nfe() {
return WooCommerce_NFe::instance();
}
add_action( 'plugins_loaded', 'woo_nfe' );
/**
* Plugin Name: WooCommerce example extension
* Plugin URI: https://github.com/Automattic/wc-extensions-code-test-guide
* Description: WooCommerce example extension as a guide to write tests.
* Version: 1.0.0
* Author: Akeda Bagus <[email protected]>
* Author URI: http://gedex.web.id.
*/
/**
* Run the plugin during `woocommerce_init`.
*
* @return bool
*/
function wc_ee_run() {
return wc_ee_instance()->run();
}
add_action( 'woocommerce_init', 'wc_ee_run' );
/**
* Get instance of WC_Example_Extension.
*
* @return WC_Example_Extension Instance of WC_Example_Extension
*/
function wc_ee_instance() {
static $extension;
if ( ! isset( $extension ) ) {
$extension = WooCommerce_NFe::instance();
}
return $extension;
}
add_action( 'before_woocommerce_init', function () {
if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, false );
}
} );
}