This repository has been archived by the owner on May 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
doliwoo.php
260 lines (223 loc) · 7.58 KB
/
doliwoo.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
<?php
/*
Plugin Name: DoliWoo
Plugin URI: http://gpcsolutions.github.io/doliwoo
Description: Dolibarr WooCommerce integration
Version: 1.0.2
Author: GPC.solutions
Author URI: https://gpcsolutions.fr
License: GPL-3.0+
Text Domain: doliwoo
Domain Path: /languages
*/
/* Copyright (C) 2013-2014 Cédric Salvador <[email protected]>
* Copyright (C) 2015 Maxime Lafourcade <[email protected]>
* Copyright (C) 2015 Raphaël Doursenaud <[email protected]>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
/**
* DoliWoo plugin.
*
* Dolibarr WooCommerce integration.
*
* @package DoliWoo
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
load_plugin_textdomain(
'doliwoo',
false,
dirname( plugin_basename( __FILE__ ) ) . '/languages/'
);
// Check required extensions
if ( false === extension_loaded( 'soap' ) && false === extension_loaded( 'openssl' ) ) {
esc_html_e( __( 'This plugin needs SOAP and OpenSSL PHP extensions.', 'doliwoo' ) );
exit;
}
if ( false === extension_loaded( 'soap' ) ) {
esc_html_e( __( 'This plugin needs SOAP PHP extension.', 'doliwoo' ) );
exit;
}
if ( false === extension_loaded( 'openssl' ) ) {
esc_html_e( __( 'This plugin needs OpenSSL PHP extension.', 'doliwoo' ) );
exit;
}
// Make sure the settings class is available
if ( ! class_exists( 'Doliwoo_WC_Integration' ) ) :
// If WooCommerce is active
if ( in_array(
'woocommerce/woocommerce.php',
apply_filters( 'active_plugins', get_option( 'active_plugins' ) ),
true
) ) {
if ( ! class_exists( 'Doliwoo' ) ) {
/**
* Dolibarr Integration for WooCommerce
*/
class Doliwoo {
/** @var Doliwoo_WC_Integration Doliwoo Settings */
public $settings;
/** @var array SOAP authentication parameters */
public $ws_auth = array();
/** @var Doliwoo_WC_Params custom parameters */
public $woocommerce_parameters;
/** @var Doliwoo_Dolibarr external requests */
public $dolibarr;
/**
* DoliWoo plugin
*/
public function __construct() {
require_once 'includes/class-wc-params.php';
require_once 'includes/class-dolibarr.php';
$this->woocommerce_parameters = new Doliwoo_WC_Params();
$this->dolibarr = new Doliwoo_Dolibarr();
// Initialize plugin settings
add_action( 'plugins_loaded', array( $this, 'init' ) );
// Add a link to settings
add_filter(
'plugin_action_links_' . plugin_basename( __FILE__ ), array( $this, 'plugin_action_links' )
);
// Setup dolibarr environment
add_action( 'woocommerce_loaded', array( &$this->dolibarr, 'set_woocommerce' ) );
add_action( 'woocommerce_init', array( $this, 'set_settings' ) );
// Create custom tax classes and VAT rates on plugin settings saved
add_action( 'woocommerce_settings_saved', array( &$this->dolibarr->taxes, 'create_custom_tax_classes' ) );
// Import Dolibarr products on plugin settings saved
add_action( 'woocommerce_settings_saved', array( &$this->dolibarr, 'dolibarr_import_products' ) );
// Reschedule products imporrt
add_action( 'woocommerce_settings_saved', array( $this, 'reschedule_import_products' ) );
// Create a Dolibarr order on each WooCommerce order
add_action(
'woocommerce_checkout_order_processed',
array( &$this->dolibarr, 'dolibarr_create_order' )
);
// Add Dolibarr import products hook
add_action( 'import_products', array( &$this->dolibarr, 'dolibarr_import_products' ) );
// Dolibarr ID User admin custom meta
add_filter( 'manage_users_columns', array( &$this->woocommerce_parameters, 'user_columns' ) );
add_action( 'show_user_profile', array( &$this->woocommerce_parameters, 'customer_meta_fields' ) );
add_action( 'edit_user_profile', array( &$this->woocommerce_parameters, 'customer_meta_fields' ) );
add_action(
'personal_options_update',
array( &$this->woocommerce_parameters, 'save_customer_meta_fields' )
);
add_action(
'edit_user_profile_update',
array( &$this->woocommerce_parameters, 'save_customer_meta_fields' )
);
add_action(
'manage_users_custom_column',
array( &$this->woocommerce_parameters, 'user_column_values' ),
10, // Prio
3 // Args count
);
// Schedule the import of product data from Dolibarr
register_activation_hook( __FILE__, 'activation' );
register_deactivation_hook( __FILE__, 'deactivation' );
}
/**
* Initialize the plugin
*
* @return void
*/
public function init() {
require_once 'includes/class-wc-tax.php';
// Checks if WooCommerce is installed.
if ( class_exists( 'WC_Integration' ) ) {
// Include our integration class.
require_once 'includes/class-wc-integration.php';
// Register the integration.
add_filter( 'woocommerce_integrations', array( $this, 'add_integration' ) );
}
$this->dolibarr->taxes = new Doliwoo_WC_Tax();
}
/**
* On plugin activation
*
* @return void
*/
public function activation() {
// Schedule product import with a sensible default
wp_schedule_event( time(), 'daily', 'import_products' );
}
/**
* On plugin deactivation
*
* @return void
*/
public function deactivation() {
// Unschedule product import
wp_clear_scheduled_hook( 'import_products' );
}
/**
* Add a new integration to WooCommerce
*
* @param array $integrations Existing integrations
*
* @return string[] WooCommerce integrations
*/
public function add_integration( $integrations ) {
$integrations[] = 'Doliwoo_WC_Integration';
return $integrations;
}
/**
* Reschedules the automatic import of Dolibarr products
*
* @return void
*/
public function reschedule_import_products() {
$delay = $this->settings->delay_update;
wp_clear_scheduled_hook( 'import_products' );
wp_schedule_event( time(), $delay, 'import_products' );
}
/**
* Show action links on the plugin screen.
*
* @param mixed $links Plugin Action links
* @return array
*/
public static function plugin_action_links( $links ) {
$action_links = array(
'settings' => '<a href="' . admin_url(
'admin.php?page=wc-settings&tab=integration§ion=doliwoo'
) . '" title="' . esc_attr(
__( 'View DoliWoo Settings', 'doliwoo' )
) . '">' . esc_attr(
__( 'Settings', 'doliwoo' )
) . '</a>',
);
return array_merge( $action_links, $links );
}
/**
* Set Dolibarr settings from WooCommerce integration settings
*
* @return void
*/
public function set_settings() {
// Load settings
$integrations = WC()->integrations->get_integrations();
$this->dolibarr->settings = $integrations['doliwoo'];
$this->dolibarr->update_settings();
}
}
}
} else {
// WooCommerce is not available
esc_html_e( __( 'This extension needs WooCommerce', 'doliwoo' ) );
exit;
}
$doliwoo = new Doliwoo();
endif;