woocommerce-zapier.php
13.5 KB
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
<?php
/*
Plugin Name: WooCommerce Zapier Integration
Plugin URI: https://woocommerce.com/products/woocommerce-zapier/
Description: Integrates WooCommerce with the <a href="https://www.zapier.com" target="_blank">Zapier</a> web automation service.
Version: 1.6.3
Author: OM4
Author URI: http://wczap.com/
Text Domain: wc_zapier
*/
/*
Copyright 2013-2016 OM4 (email: info@om4.com.au web: https://om4.com.au/)
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
*/
/**
* Required functions
*/
if ( ! function_exists( 'woothemes_queue_update' ) )
require_once('woo-includes/woo-functions.php');
/**
* Plugin updates
*/
woothemes_queue_update( plugin_basename( __FILE__ ), '0782bdbe932c00f4978850268c6cfe40', 243589 );
/**
* Check WooCommerce exists and is active
*/
if ( is_woocommerce_active() ) {
if ( ! class_exists( 'WC_Zapier' ) ) {
/**
* This class is a singleton, and should be accessed via the WC_Zapier() function.
*
* Class WC_Zapier
*/
class WC_Zapier {
/**
* Database version (used for install/upgrade tasks if required)
*/
const db_version = 2;
/**
* Name of the wp_option record that stores the installed version number.
*/
const db_version_option_name = 'wc_zapier_version';
/**
* The minimum WooCommerce version that this plugin supports.
*/
const MINIMUM_SUPPORTED_WOOCOMMERCE_VERSION = '2.2.0';
/**
* URL to the documentation for this plugin.
*/
const documentation_url = 'https://docs.woocommerce.com/document/woocommerce-zapier/';
/**
* Stores the one and only instance of this class
* @var WC_Zapier
*/
private static $instance;
/**
* Stores the WC_Zapier_Admin class
* @var WC_Zapier_Admin
*/
private $admin;
/**
* Full path to this plugin's directory (including trailing slash)
* @var string
*/
public static $plugin_path;
/**
* Full URL to this plugin's directory (including trailing slash)
* @var string
*/
public static $plugin_url;
/**
* The list of WooCommerce order statuses.
*
* Generated by WC_Zapier::get_order_statuses()
*
* @var
*/
private static $order_statuses;
/**
* The main WC_Zapier instance.
*
* @return WC_Zapier
*/
public static function instance() {
if ( ! isset(self::$instance) ) {
self::$instance = new WC_Zapier();
self::$instance->initialise();
}
return self::$instance;
}
/**
* Initialise the plugin
*/
private function initialise() {
self::$plugin_path = dirname( __FILE__ ) . '/';
self::$plugin_url = plugin_dir_url( __FILE__ );
// Set up class autoloading
if ( function_exists( "__autoload" ) ) {
spl_autoload_register( "__autoload" );
}
spl_autoload_register( array( $this, 'autoload' ) );
load_plugin_textdomain( 'wc_zapier', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
}
/**
* Autoload the WC_Zapier_* classes when required.
* Helps keep code simple and memory consumption down.
*
* @param string $class_name
*/
public function autoload( $class_name ) {
// Only act on classes in this plugin
if ( false === strpos( $class_name, 'WC_Zapier' ) )
return;
$filename = "{$class_name}.php";
$directory = self::$plugin_path . 'includes/';
if ( 'WC_Zapier_Trigger_Factory' !== $class_name && false !== strpos( $class_name, 'Zapier_Trigger_' ) )
$directory .= 'triggers/';
$file = $directory . $filename;
/**
* Override the file path where a WC_Zapier_* class is going to be autoloaded from.
*
* @since 1.6.0
*
* @param string $file The full file path to class's PHP file.
* @param string $class_name The name of the PHP class to autoload.
*/
$file = apply_filters( 'wc_zapier_autoload_file', $file, $class_name );
if ( file_exists( $file ) )
require_once( $file );
}
/**
* Executed during the 'plugins_loaded' WordPress hook.
*
* - Checks that we're running the correct WooCommerce Version
* - Sets up various hooks
* - Load Supported Zapier Triggers
* - Loads the admin/dashboard interface if required
*/
public function plugins_loaded() {
// WooCommerce version check
if ( version_compare( WOOCOMMERCE_VERSION, self::MINIMUM_SUPPORTED_WOOCOMMERCE_VERSION, '<' ) ) {
add_action( 'admin_notices', array( $this, 'admin_notice' ) );
WC_Zapier()->log( "WooCommerce plugin version(" . WOOCOMMERCE_VERSION . ") is less than " . self::MINIMUM_SUPPORTED_WOOCOMMERCE_VERSION );
return;
}
// User has a supported version of WooCommerce, so let's proceed.
// WooCommerce init is priority 0, cancel_order is priority 10
// Initialize in between those two so that cancelled orders are accounted for
add_action( 'init', array( $this, 'init' ), 5 );
add_action( 'admin_init', array( $this, 'maybe_activate_or_update' ) );
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'action_links' ) );
if ( class_exists( 'WC_Subscriptions' ) ) {
// The WooCommerce Subscriptions plugin is active
new WC_Zapier_Subscriptions();
}
if ( function_exists( 'woocommerce_init_checkout_field_editor' ) ) {
// The WooCommerce Checkout Field Editor plugin is active
new WC_Zapier_Checkout_Field_Editor();
}
add_action( 'wc_zapier_resend_sample_data', array( 'WC_Zapier', 'resend_sample_data' ) );
}
/**
* Obtain the list of WooCommerce order statuses.
*
* @return string[] Array of order status slugs (excluding the wc- prefix)
*/
public static function get_order_statuses() {
if ( is_null( self::$order_statuses ) ) {
self::$order_statuses = array();
if ( function_exists( 'wc_get_order_statuses' ) ) {
// WC 2.2+
$statuses = wc_get_order_statuses();
foreach ( $statuses as $status => $status_label ) {
// Use the order status without wc- internal prefix
$status = 'wc-' === substr( $status, 0, 3 ) ? substr( $status, 3 ) : $status;
self::$order_statuses[] = $status;
}
} else {
// WC 2.1 and earlier
$statuses = get_terms( 'shop_order_status', array( 'hide_empty' => 0, 'orderby' => 'id' ) );
if ( ! is_wp_error( $statuses ) ) {
foreach ( $statuses as $status ) {
self::$order_statuses[] = $status->slug;
}
}
}
}
return self::$order_statuses;
}
/**
* Displays a message if the user isn't using a supported version of WooCommerce.
*/
public function admin_notice() {
?>
<div id="message" class="error">
<p><?php printf( __( 'The WooCommerce Zapier Integration plugin is only compatible with WooCommerce version %s or later. Please update WooCommerce.', 'wc_zapier' ), self::MINIMUM_SUPPORTED_WOOCOMMERCE_VERSION ); ?></p>
</div>
<?php
}
/**
* Registers the custom post type for storing Zapier feeds.
*
* Executed during the 'init' WordPress hook.
*/
public function init() {
/*
* The Custom Post Type that stores the zapier feeds
*/
register_post_type(
'wc_zapier_feed',
array(
'public' => false
, 'show_ui' => true
, 'show_in_menu' => 'woocommerce'
, 'supports' => false
, 'labels' => array(
'name' => __( 'Zapier Feeds', 'wc_zapier' )
, 'singular_name' => __( 'Zapier Feed', 'wc_zapier' )
, 'add_new_item' => __( 'Add New Zapier Feed', 'wc_zapier' )
, 'edit_item' => __( 'Edit Zapier Feed', 'wc_zapier' )
, 'new_item' => __( 'New Zapier Feed', 'wc_zapier' )
, 'view_item' => __( 'View Zapier Feed', 'wc_zapier' )
, 'search_items' => __( 'Search Zapier Feeds', 'wc_zapier' )
, 'not_found' => __( 'No Zapier Feeds found', 'wc_zapier' )
, 'not_found_in_trash' => __( 'No Zapier Feeds found in Trash', 'wc_zapier' )
)
)
);
WC_Zapier_Trigger_Factory::load_triggers();
if ( is_admin() ) {
$this->admin = new WC_Zapier_Admin();
}
}
/**
* Install the plugin if required.
*
* As per http://core.trac.wordpress.org/ticket/14170 it's far better to use an upgrade routine fired on admin_init
*
* Executed on every admin/dashboard page load (via the admin_init hook)
*/
public function maybe_activate_or_update() {
if ( self::db_version !== $installed_version = intval( get_option( self::db_version_option_name ) ) ) {
if ( 0 === $installed_version ) {
// Plugin activation/installation tasks
} else {
// Any database update/upgrade routines will go here
if ( 1 == $installed_version ) {
// Send sample data to all active feeds to ensure they have the latest field definitions
self::resend_sample_data_async();
}
}
update_option( self::db_version_option_name, self::db_version );
}
}
/**
* Return a formatted price (excluding currency symbols).
*
* @param int|float|double $price The unformatted price.
*
* @return string the formatted price (2 decimal places)
*/
public static function format_price( $price ) {
return wc_format_decimal ( $price, 2 );
}
/**
* Format a WordPress date into a W3C formatted date (eg 2005-08-15T15:52:01+00:00)
*
* @param string $date WordPress date
*
* @return string date formatted like 2005-08-15T15:52:01+00:00
*/
public static function format_date( $date ) {
// See http://www.php.net/manual/en/class.datetime.php#datetime.constants.types
return date_i18n( DATE_W3C, strtotime( $date ) );
}
/**
* Prepare (decode) a string, in preparation for sending to Zapier.
*
* Some order fields in WooCommerce are HTML entity encoded, and they need to be decoded before sending to Zapier.
*
* For example, "&" should be sent as "&".
*
* Ref: https://github.com/woothemes/woocommerce/commit/07237d9a09849e107707943453b1fb245b8b4a2d
* Ref: https://github.com/woothemes/woocommerce/issues/10149
*
* @param $string|array The string/array that needs to be decoded.
*
* @return string|array The decoded string/array.
*/
public static function decode( $value ) {
if ( is_array( $value ) ) {
return array_map( array( 'WC_Zapier', 'decode' ), $value );
} else {
return html_entity_decode( $value, ENT_QUOTES, get_bloginfo( 'charset' ) );
}
}
public function action_links( $links ) {
$plugin_links = array(
'<a href="' . admin_url( 'edit.php?post_type=wc_zapier_feed' ) . '">' . __( 'Settings', 'wc_zapier' ) . '</a>',
'<a href="' . self::documentation_url . '">' . __( 'Documentation', 'wc_zapier' ) . '</a>'
);
return array_merge( $plugin_links, $links );
}
/**
* Asynchronously send sample data to all Feeds that use the specified trigger(s).
*
* This ensures that all existing Zaps include the latest data specification
*
* @param array $trigger_keys List of trigger keys (strings)
*/
public static function resend_sample_data_async( $trigger_keys = array() ) {
wp_schedule_single_event( time(), 'wc_zapier_resend_sample_data', array( 'trigger_keys' => $trigger_keys ) );
}
/**
* Immediately re-send sample data to all currently active Zapier Feeds that use these triggers.
*
* This ensures that Zapier has the latest checkout field definitions.
*
* @param array $trigger_keys Array of trigger keys (strings)
*/
public static function resend_sample_data( $trigger_keys = array() ) {
if ( empty($trigger_keys ) ) {
$trigger_keys = WC_Zapier_Trigger_Factory::get_trigger_keys();
}
foreach ( $trigger_keys as $trigger_key ) {
try {
$trigger = WC_Zapier_Trigger_Factory::get_trigger_with_key( $trigger_key );
$trigger->send_sample_data_to_active_feeds_using_this_trigger();
} catch ( Exception $ex ) {
// Invalid trigger key - silently ignore and continue
}
}
}
/**
* Log a message if the WC Zapier Debugger plugin is active.
*
* If the plugin isn't active, then no logging occurs
*
* @param string $message
* @param int|null $object_id Optional order ID or customer ID
* @param string|null Object Name. Order or Customer
*/
public function log( $message, $object_id = null, $object_name = 'Order' ) {
if ( function_exists( "WC_Zapier_Debugger" ) ) {
if ( !is_null( $object_id ) ) {
$message = "$object_name #$object_id: $message";
}
WC_Zapier_Debugger()->log_message( $message );
} else {
// Debugger plugin not active. Do nothing.
}
}
}
/**
* This function should be used to access the WC_Zapier singleton class.
*
* It's simpler to use this function instead of a global variable.
*
* @return WC_Zapier
*/
function WC_Zapier() {
return WC_Zapier::instance();
}
// Let's get this thing started!
WC_Zapier();
}
}