-
Notifications
You must be signed in to change notification settings - Fork 16
/
neuralab-woocommerce-salesforce-integration.php
238 lines (203 loc) · 7.32 KB
/
neuralab-woocommerce-salesforce-integration.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
<?php
/**
* Plugin Name: Neuralab WooCommerce SalesForce Integration
* Plugin URI: https://github.com/Neuralab/WooCommerce-Salesforce-integration
* Description: Syncing engine for all sort of datasets and configurations.
* Version: 0.9.2
* Author: Neuralab
* Author URI: https://neuralab.net
* Developer: matej@neuralab
* Text Domain: woocommerce-integration-nwsi
*
* WC requires at least: 3.3
* WC tested up to: 3.3.4
*
* License: MIT
* Requires at least: 4.9
*/
if ( !defined( "ABSPATH" ) ) {
exit;
}
if ( !function_exists( "nwsi_is_woocommerce_active" ) ) {
/**
* Return true if Woocommerce plugin is active
* @return boolean
*/
function nwsi_is_woocommerce_active() {
if ( in_array( "woocommerce/woocommerce.php", apply_filters( "active_plugins", get_option( "active_plugins" ) ) ) ) {
return true;
}
return false;
}
}
if ( !function_exists( "nwsi_admin_notice_missing_woocommerce" ) ) {
/**
* Echo admin notice HTML for missing WooCommerce plugin
*/
function nwsi_admin_notice_missing_woocommerce() {
global $current_screen;
if( $current_screen->parent_base === "plugins" ) {
?>
<div class="notice notice-error">
<p><?php _e( "Please install and activate <a href='https://woocommerce.com/' target='_blank'>WooCommerce</a> before activating the Neuralab WooCommerce SalesForce Integration plugin!", "woocommerce-integration-nwsi" ); ?></p>
</div>
<?php
}
}
}
if ( nwsi_is_woocommerce_active() ) {
if ( !class_exists( "NWSI_Main" ) ) {
/**
* Main plugin's class. Holds installation and init methods.
*/
class NWSI_Main {
/**
* Current version of the plugin.
*
* @var string
*/
const VERSION = "0.9.2";
/**
* Singleton instance.
*
* @var NWSI_Main
*/
protected static $instance = null;
/**
* @var NWSI_Salesforce_Worker
*/
private $worker;
/**
* Class constructor, initialize essential classes and hooks.
*/
protected function __construct() {
$this->define_plugin_constants();
require_once( "includes/controllers/core/class-nwsi-salesforce-object-manager.php" );
require_once( "includes/controllers/core/class-nwsi-salesforce-worker.php" );
require_once( "includes/controllers/utilites/class-nwsi-utility.php" );
require_once( "includes/views/class-nwsi-settings.php" );
add_filter( "woocommerce_integrations", array( $this, "add_integration_section" ) );
if ( is_admin() ) {
require_once( "includes/views/class-nwsi-orders-view.php" );
$orders_view = new NWSI_Orders_View();
$orders_view->register_hooks();
//TODO: Find a better way of doing this
add_action('admin_enqueue_scripts', function() {
wp_enqueue_style( "nwsi-settings-style", plugins_url( "/includes/style/nwsi-settings.css", __FILE__ ) );
});
}
$this->worker = new NWSI_Salesforce_Worker();
// add_action( "woocommerce_checkout_order_processed", array( $this, "process_order" ), 10, 1 );
add_action( "woocommerce_thankyou", array( $this, "process_order" ), 90, 1 );
}
/**
* Define all the constants used in plugin.
*/
private static function define_plugin_constants() {
if ( !defined( "NWSI_DIR_NAME" ) ) {
define( "NWSI_DIR_NAME", basename( __DIR__ ) );
}
if ( !defined( "NWSI_DIR_PATH" ) ) {
define( "NWSI_DIR_PATH", plugin_dir_path( __FILE__ ) );
}
if ( !defined( "NWSI_DIR_URL" ) ) {
define( "NWSI_DIR_URL", plugins_url( "/", __FILE__ ) );
}
}
/**
* Process order.
* @param int $order_id
*/
public function process_order( $order_id ) {
if ( !empty( get_option( "woocommerce_nwsi_automatic_order_sync" ) ) ) {
$this->worker->process_order( $order_id );
}
}
/**
* Create plugins table and triggers in DB.
*/
public static function install() {
if ( !current_user_can( "activate_plugins" ) ) {
return;
}
NWSI_Main::define_plugin_constants();
update_option( "woocommerce_nwsi_login_url", "https://login.salesforce.com" );
require_once( "includes/controllers/core/class-nwsi-db.php" );
$db = new NWSI_DB();
$db->create_relationship_table();
if ( $db->is_relationship_table_empty() ) {
// insert default relationships
require_once( "includes/controllers/utilites/class-nwsi-utility.php" );
$utility = new NWSI_Utility();
$relationships = $utility->load_from_file( "default_relationships.json", "data" );
if ( !empty( $relationships ) ) {
foreach( $relationships as $relationship ) {
$db->save_new_relationship( $relationship->from_object, $relationship->from_object_label,
$relationship->to_object, $relationship->to_object_label, $relationship->relationships,
$relationship->required_sf_objects, $relationship->unique_sf_fields );
}
}
}
}
/**
* Delete plugin tables and related WP options.
*/
public static function uninstall() {
if ( !current_user_can( "activate_plugins" ) ) {
return;
}
delete_option( "woocommerce_nwsi_settings" );
delete_option( "woocommerce_nwsi_access_token" );
delete_option( "woocommerce_nwsi_refresh_token" );
delete_option( "woocommerce_nwsi_instance_url" );
delete_option( "woocommerce_nwsi_automatic_order_sync" );
delete_option( "woocommerce_nwsi_connection_hash" );
delete_option( "woocommerce_nwsi_login_url" );
require_once( "includes/controllers/core/class-nwsi-db.php" );
$db = new NWSI_DB();
$db->delete_relationship_table();
// clear any cached data that has been removed
wp_cache_flush();
}
/**
* Return integrations array with new section element. Needed for
* woocommerce_integrations filter hook.
*
* @param array $integrations
* @return array
*/
public function add_integration_section( $integrations ) {
$integrations[] = "NWSI_Settings";
return $integrations;
}
/**
* Return class instance.
*
* @return NWSI_Main
*/
public static function get_instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Cloning is forbidden.
*/
public function __clone() {
_doing_it_wrong( __FUNCTION__, __( "Cloning is forbidden!", "woocommerce-integration-nwsi" ), "4.0" );
}
/**
* Unserializing instances of this class is forbidden.
*/
public function __wakeup() {
_doing_it_wrong( __FUNCTION__, __( "Unserializing instances is forbidden!", "woocommerce-integration-nwsi" ), "4.0" );
}
}
}
register_activation_hook( __FILE__, array( "NWSI_Main", "install" ) );
register_uninstall_hook( __FILE__, array( "NWSI_Main", "uninstall" ) );
add_action( "plugins_loaded", array( "NWSI_Main", "get_instance" ), 0 );
} else {
add_action( "admin_notices", "nwsi_admin_notice_missing_woocommerce" );
}