-
Notifications
You must be signed in to change notification settings - Fork 2
/
trackmage.php
188 lines (159 loc) · 5.88 KB
/
trackmage.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
<?php
/**
* Plugin Name: TrackMage - Woo Shipment Tracking
* Plugin URI: https://trackmage.com/
* Description: TrackMage integrates shipments tracking into your WooCommerce store.
* Version: 2.0.2
* Author: TrackMage
* Author URI: https://trackmage.com
* Text Domain: trackmage
* License: GPL-3.0-or-later
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* GitHub Plugin URI: https://github.com/trackmage/trackmage-woo-shipment-tracking
* Requires PHP: 7.4
* Requires at least: 5.3
* Tested up to: 6.4.1
* WC requires at least: 4.5.0
* WC tested up to: 8.2.2
*
* Copyright (c) 2019-2023 TrackMage
*
* License: GNU General Public License v3.0
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
use TrackMage\WordPress\Plugin;
use TrackMage\WordPress\Helper;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
require_once ABSPATH . '/wp-admin/includes/plugin.php';
if (PHP_VERSION_ID < 50600 || (!is_plugin_active('woocommerce/woocommerce.php') && !is_plugin_active_for_network('woocommerce/woocommerce.php'))) {
add_action( 'plugins_loaded', 'trackmage_init_deactivation' );
/**
* Initialise deactivation functions.
*/
function trackmage_init_deactivation() {
if ( current_user_can( 'activate_plugins' ) ) {
add_action( 'admin_init', 'trackmage_deactivate' );
add_action( 'admin_notices', 'trackmage_deactivation_notice' );
}
}
/**
* Deactivate the plugin.
*/
function trackmage_deactivate() {
deactivate_plugins( plugin_basename( __FILE__ ) );
}
/**
* Show deactivation admin notice.
*/
function trackmage_deactivation_notice() {
if(PHP_VERSION_ID < 50600) {
$notice = sprintf(
// Translators: 1: Required PHP version, 2: Current PHP version.
__( '<strong>TrackMage for WordPress</strong> requires PHP %1$s to run. This site uses %2$s, so the plugin has been <strong>deactivated</strong>.', 'trackmage' ),
'5.6',
PHP_VERSION
);
}else{
$notice = __('To use TrackMage for WooCommerce it is required that WooCommerce is installed and activated', 'trackmage');
}
?>
<div class="error"><p><?php echo wp_kses_post( $notice ); ?></p></div>
<?php
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
}
return false;
}
if ( ! defined( 'TRACKMAGE_VERSION' ) ) {
// phpcs:ignore NeutronStandard.Constants.DisallowDefine.Define
define( 'TRACKMAGE_VERSION', '2.0.0' );
}
if ( ! defined( 'TRACKMAGE_DIR' ) ) {
// phpcs:ignore NeutronStandard.Constants.DisallowDefine.Define
define( 'TRACKMAGE_DIR', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'TRACKMAGE_VIEWS_DIR' ) ) {
// phpcs:ignore NeutronStandard.Constants.DisallowDefine.Define
define( 'TRACKMAGE_VIEWS_DIR', plugin_dir_path( __FILE__ ) . 'views/' );
}
if ( ! defined( 'TRACKMAGE_URL' ) ) {
// phpcs:ignore NeutronStandard.Constants.DisallowDefine.Define
define( 'TRACKMAGE_URL', plugin_dir_url( __FILE__ ) );
}
$content = file_get_contents(__DIR__ . '/vendor/composer/autoload_real.php');
$composerAutoloaderInitClassName = preg_match('/class\s+(ComposerAutoloaderInit[a-zA-Z0-9_]+)/', $content, $matches) ? $matches[1] ?? null : null;
// Load Composer autoloader.
if ( file_exists( __DIR__ . '/vendor/autoload.php' ) && (null === $composerAutoloaderInitClassName || !class_exists($composerAutoloaderInitClassName, false))) {
require_once __DIR__ . '/vendor/autoload.php';
}
define('TRACKMAGE_PLUGIN_FILE', __FILE__);
define('TRACKMAGE_PLUGIN_BASENAME', plugin_basename( TRACKMAGE_PLUGIN_FILE ));
if (!defined('TRACKMAGE_API_DOMAIN')) {
define('TRACKMAGE_API_DOMAIN', 'https://api.trackmage.com');
}
if (!defined('TRACKMAGE_APP_DOMAIN')) {
define('TRACKMAGE_APP_DOMAIN', 'https://app.trackmage.com');
}
add_action('plugins_loaded', 'trackMageInit');
register_activation_hook(__FILE__, 'trackMageActivate');
register_deactivation_hook(__FILE__, 'trackMageDeactivate');
register_uninstall_hook( __FILE__, 'trackMageUninstall');
load_plugin_textdomain( 'trackmage', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
/**
* trackMageActivate
*
* Plugin activate event
*/
function trackMageActivate() {
$plugin = Plugin::instance();
foreach($plugin->getRepos() as $repository) {
try {
$repository->init();
$plugin->getLogger()->info("Successfully created table {$repository->getTable()}");
} catch(Exception $e) {
$plugin->getLogger()->critical("Unable to create table {$repository->getTable()}: {$e->getMessage()}");
}
}
$plugin->dropOldTables();
$plugin->init();
if(!get_transient('trackmage-wizard-notice'))
set_transient( 'trackmage-wizard-notice', true );
}
/**
* Plugin deactivate event
*/
function trackMageDeactivate() {
Helper::clearTransients();
}
/**
* Initialize trackMage plugin components
*/
function trackMageInit() {
if(!is_plugin_active('woocommerce/woocommerce.php') && !is_plugin_active_for_network('woocommerce/woocommerce.php')) {
add_action('admin_notices', 'trackMageWooCommerceError');
return;
}
Plugin::instance()->init();
}
/**
* Display error message: WooCommerce not active
*/
function trackMageWooCommerceError() {
printf('<div class="error"><p>%s</p></div>', __('To use TrackMage for WooCommerce it is required that WooCommerce is installed and activated'));
}
function trackMageUninstall(){
foreach(Plugin::instance()->getRepos() as $repository) {
try {
$repository->drop();
} catch(Exception $e) {
Plugin::instance()->getLogger()->critical("Unable to drop table {$repository->getTable()}: {$e->getMessage()}");
}
}
Plugin::instance()->dropOldTables();
Helper::clearTransients();
}