-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwoocommerce-gateway-mondido-checkout.php
299 lines (250 loc) · 7.36 KB
/
woocommerce-gateway-mondido-checkout.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
<?php
/*
* Plugin Name: WooCommerce Mondido Checkout Gateway
* Plugin URI: https://www.mondido.com/
* Description: Provides a Payment Gateway through Mondido for WooCommerce.
* Author: Mondido
* Author URI: https://www.mondido.com/
* Version: 1.0.0
* Text Domain: woocommerce-gateway-mondido-checkout
* Domain Path: /languages
* WC requires at least: 3.0.0
* WC tested up to: 3.4.2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
class WC_Mondido_Checkout {
/**
* Constructor
*/
public function __construct() {
add_action( 'admin_notices', array( $this, 'display_admin_notices' ) );
// Activation
register_activation_hook( __FILE__, array( $this, 'activate' ) );
// Actions
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array(
$this,
'plugin_action_links'
) );
add_action( 'plugins_loaded', array( $this, 'init' ), 0 );
add_action( 'woocommerce_loaded', array(
$this,
'woocommerce_loaded'
), 40 );
}
/**
* Activate Plugin
*/
public function activate() {
// Required plugin: WooCommerce Mondido Payments Gateway
if ( class_exists( 'WC_Mondido_Payments', FALSE ) ) {
return TRUE;
}
// Download and Install WC_Mondido_Payments package
include_once ABSPATH . '/wp-includes/pluggable.php';
include_once ABSPATH . '/wp-admin/includes/plugin.php';
include_once ABSPATH . '/wp-admin/includes/file.php';
try {
if ( ! $plugin = self::get_core_plugin() ) {
// Install plugin
self::install_core_plugin();
// Plugin path
$plugin = self::get_core_plugin();
}
// Check is active
if ( ! is_plugin_active( $plugin ) ) {
// Activate plugin
self::activate_core_plugin();
WC_Admin_Notices::add_custom_notice(
'wc-mondido-checkout-notice',
__( 'Required WooCommerce Mondido Payments Gateway plugin was automatically installed.', 'woocommerce-gateway-mondido-checkout' )
);
}
} catch ( \Exception $e ) {
self::add_admin_notice( $e->getMessage(), 'error' );
return FALSE;
}
// Set Version
if ( ! get_option( 'woocommerce_mondido_checkout_version' ) ) {
add_option( 'woocommerce_mondido_checkout_version', '1.0.0' );
}
return TRUE;
}
/**
* Add relevant links to plugins page
*
* @param array $links
*
* @return array
*/
public function plugin_action_links( $links ) {
$plugin_links = array(
'<a href="' . admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=mondido_checkout' ) . '">' . __( 'Settings', 'woocommerce-gateway-mondido-checkout' ) . '</a>'
);
return array_merge( $plugin_links, $links );
}
/**
* Init localisations and files
* @return void
*/
public function init() {
// Localization
load_plugin_textdomain( 'woocommerce-gateway-mondido-checkout', FALSE, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* WooCommerce Loaded: load classes
* @return void
*/
public function woocommerce_loaded() {
if ( ! class_exists( 'WC_Mondido_Payments', FALSE ) ) {
return;
}
include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-mondido-checkout.php' );
}
/**
* Display admin notices
* @return void
*/
public function display_admin_notices() {
$notices = self::get_admin_notices();
if ( count( $notices ) === 0 ) {
return;
}
foreach ( $notices as $type => $messages ):
?>
<div class="<?php echo esc_html( $type ); ?> notice">
<?php foreach ( $messages as $message ): ?>
<p>
<?php echo esc_html( $message ); ?>
</p>
<?php endforeach; ?>
</div>
<?php
endforeach;
// Remove notices
delete_transient( 'wc-mondido-checkout-notice' );
// Deactivate plugin
deactivate_plugins( array( __FILE__ ), TRUE );
}
/**
* Add admin notice
*
* @param string $message
* @param string $type
* @return void
*/
public static function add_admin_notice( $message, $type = 'error' ) {
wp_cache_delete( 'wc-mondido-checkout-notice', 'transient' );
if ( ! ( $notices = get_transient( 'wc-mondido-checkout-notice' ) ) ) {
$notices = array();
}
if ( ! isset( $notices[ $type ] ) ) {
$notices[ $type ] = array();
}
$notices[ $type ][] = $message;
set_transient( 'wc-mondido-checkout-notice', $notices );
}
/**
* Get admin notices
* @return array
*/
public static function get_admin_notices() {
if ( ! ( $notices = get_transient( 'wc-mondido-checkout-notice' ) ) ) {
$notices = array();
}
return $notices;
}
/**
* Get Core Plugin path
* @return bool|string
*/
protected static function get_core_plugin() {
wp_cache_delete( 'plugins', 'plugins' );
$plugins = get_plugins();
foreach ( $plugins as $file => $plugin ) {
if ( strpos( $file, 'woocommerce-gateway-mondido.php' ) !== FALSE ) {
return $file;
}
}
return FALSE;
}
/**
* Activate Core Plugin
* @return bool
* @throws \Exception
*/
public static function activate_core_plugin() {
if ( $plugin = self::get_core_plugin() ) {
$result = activate_plugin( $plugin );
if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message() );
}
return TRUE;
}
throw new Exception( 'Failed to activate plugin' );
}
/**
* Install Core Plugin
* @throws \Exception
* @return void
*/
protected static function install_core_plugin() {
WP_Filesystem();
/** @var WP_Filesystem_Base $wp_filesystem */
global $wp_filesystem;
// Install plugin
// Get latest release from Github
$response = wp_remote_get( 'https://api.github.com/repos/Mondido/MondidoWooCommerce/releases/latest', array(
'headers' => array( 'Accept' => 'application/vnd.github.v3+json' ),
) );
if ( is_wp_error( $response ) ) {
throw new Exception( $response->get_error_message() );
}
$release = json_decode( $response['body'], TRUE );
if ( ! isset( $release['zipball_url'] ) ) {
throw new Exception( 'Failed to get latest release of WooCommerce Mondido Payments Gateway plugin' );
}
// Download package
$tmpfile = download_url( $release['zipball_url'] );
if ( is_wp_error( $tmpfile ) ) {
throw new Exception( $tmpfile->get_error_message() );
}
// Extract package
$tmpdir = rtrim( get_temp_dir(), '/' ) . '/' . uniqid( 'mondido_' );
if ( ! $wp_filesystem->exists( $tmpdir ) ) {
$wp_filesystem->mkdir( $tmpdir, FS_CHMOD_DIR );
}
$result = unzip_file( $tmpfile, $tmpdir );
if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message() );
}
// Remove temp file
$wp_filesystem->delete( $tmpfile );
// Move plugin to plugins directory
$files = $wp_filesystem->dirlist( $tmpdir );
foreach ( $files as $name => $details ) {
if ( strpos( $name, 'MondidoWooCommerce' ) !== FALSE ) {
$destination = WP_PLUGIN_DIR . '/MondidoWooCommerce';
// Remove destination directory if exists
if ( $wp_filesystem->exists( $destination ) ) {
$wp_filesystem->rmdir( $destination );
}
// Make destination directory
$wp_filesystem->mkdir( $destination, FS_CHMOD_DIR );
// Copy unpacked directory to destination directory
$result = copy_dir( $tmpdir . '/' . $name, $destination );
if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message() );
}
// Remove temp directory
$wp_filesystem->rmdir( $tmpdir );
return;
}
}
// Remove temp directory
$wp_filesystem->rmdir( $tmpdir );
throw new Exception( 'Failed to install WooCommerce Mondido Payments Gateway plugin' );
}
}
new WC_Mondido_Checkout();