forked from claudiosanches/woocommerce-correios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-correios.php
183 lines (158 loc) · 4.69 KB
/
woocommerce-correios.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
<?php
/**
* Plugin Name: WooCommerce Correios
* Plugin URI: https://github.com/claudiosmweb/woocommerce-correios
* Description: Correios para WooCommerce
* Author: claudiosanches, rodrigoprior
* Author URI: http://claudiosmweb.com/
* Version: 2.2.1
* License: GPLv2 or later
* Text Domain: woocommerce-correios
* Domain Path: /languages/
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if ( ! class_exists( 'WC_Correios' ) ) :
/**
* WooCommerce Correios main class.
*/
class WC_Correios {
/**
* Plugin version.
*
* @var string
*/
const VERSION = '2.2.1';
/**
* Integration id.
*
* @var string
*/
protected static $method_id = 'correios';
/**
* Instance of this class.
*
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin public actions.
*/
private function __construct() {
// Load plugin text domain
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
if ( class_exists( 'SimpleXmlElement' ) ) {
// Checks with WooCommerce is installed.
if ( class_exists( 'WC_Shipping_Method' ) ) {
$this->includes();
if ( is_admin() ) {
$this->admin_includes();
}
add_filter( 'woocommerce_shipping_methods', array( $this, 'add_method' ) );
add_action( 'wp_ajax_wc_correios_simulator', array( 'WC_Correios_Product_Shipping_Simulator', 'ajax_simulator' ) );
add_action( 'wp_ajax_nopriv_wc_correios_simulator', array( 'WC_Correios_Product_Shipping_Simulator', 'ajax_simulator' ) );
} else {
add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
}
} else {
add_action( 'admin_notices', array( $this, 'simplexmlelement_missing_notice' ) );
}
}
/**
* Return an instance of this class.
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Return the method id/slug.
*
* @return string Gateway id/slug variable.
*/
public static function get_method_id() {
return self::$method_id;
}
/**
* Get templates path.
*
* @return string
*/
public static function get_templates_path() {
return plugin_dir_path( __FILE__ ) . 'templates/';
}
/**
* Load the plugin text domain for translation.
*/
public function load_plugin_textdomain() {
$locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce-correios' );
load_textdomain( 'woocommerce-correios', trailingslashit( WP_LANG_DIR ) . 'woocommerce-correios/woocommerce-correios-' . $locale . '.mo' );
load_plugin_textdomain( 'woocommerce-correios', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Includes.
*/
private function includes() {
include_once 'includes/class-wc-correios-error.php';
include_once 'includes/class-wc-correios-package.php';
include_once 'includes/class-wc-correios-connect.php';
include_once 'includes/class-wc-correios-shipping.php';
include_once 'includes/class-wc-correios-product-shipping-simulator.php';
include_once 'includes/class-wc-correios-emails.php';
include_once 'includes/class-wc-correios-tracking-history.php';
}
/**
* Admin includes.
*/
private function admin_includes() {
include_once 'includes/admin/class-wc-correios-admin-orders.php';
}
/**
* Add the shipping method to WooCommerce.
*
* @param array $methods WooCommerce payment methods.
*
* @return array Payment methods with Correios.
*/
public function add_method( $methods ) {
$methods[] = 'WC_Correios_Shipping';
return $methods;
}
/**
* WooCommerce fallback notice.
*
* @return string
*/
public function woocommerce_missing_notice() {
echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Correios depends on the last version of %s to work!', 'woocommerce-correios' ), '<a href="http://wordpress.org/extend/plugins/woocommerce/">' . __( 'WooCommerce', 'woocommerce-correios' ) . '</a>' ) . '</p></div>';
}
/**
* SimpleXMLElement fallback notice.
*
* @return string
*/
public function simplexmlelement_missing_notice() {
echo '<div class="error"><p>' . sprintf( __( 'WooCommerce Correios depends to %s to work!', 'woocommerce-correios' ), '<a href="http://php.net/manual/en/book.simplexml.php">' . __( 'SimpleXML', 'woocommerce-correios' ) . '</a>' ) . '</p></div>';
}
/**
* Plugin logger
*
* @return WC_Logger
*/
public static function logger() {
if ( class_exists( 'WC_Logger' ) ) {
return new WC_Logger();
} else {
global $woocommerce;
return $woocommerce->logger();
}
}
}
add_action( 'plugins_loaded', array( 'WC_Correios', 'get_instance' ), 0 );
endif;