-
Notifications
You must be signed in to change notification settings - Fork 0
/
bm-woocommerce-ordersync.php
368 lines (298 loc) · 11.4 KB
/
bm-woocommerce-ordersync.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
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
<?php
/**
* Plugin Name: WooCommerce Order Synchronizer
* Version: 0.0.3
* Plugin URI:
* Author: Trevor Bice
* Author URI: http://burningman.org/
* Text Domain: bm_woocommerce_ordersync
* License: GPLv2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* Description: This tool is designed to help keep WooCommerce Orders Synchronized between Producion and Staging on the WP Engine Hosting.
****/
if ( ! defined( 'ABSPATH' ) ) exit;
class bm_woocommerce_ordersync {
private static $instance = false;
private $wpengine;
private $page;
private $overwrite_posts;
public static function instance() {
if ( ! self::$instance ) {
self::$instance = new self;
self::$instance->init();
}
return self::$instance;
}
function init() {
add_action( 'admin_menu', array( self::instance(), 'create_menu' ) );
add_action('wp_ajax_remove_post_conflicts', array( self::instance(), 'remove_post_conflicts' ) );
add_action('wp_ajax_import_woocommerce_orders', array( self::instance(), 'import_woocommerce_orders' ) );
add_action('wp_ajax_checkOrderSynchronization', array( self::instance(), 'checkOrderSynchronization' ) );
}
function checkOrderSynchronization() {
global $wpdb;
$this->get_wpengine_installation();
$query = "SELECT * FROM wp_".$this->wpengine.".wp_posts ";
$query .= "WHERE post_type = 'shop_order' ";
$wpdb->query( $query );
$results = $wpdb->get_results( $query );
$production_string = json_encode($results);
$production_string = md5($production_string);
$query = "SELECT * FROM snapshot_".$this->wpengine.".wp_posts ";
$query .= "WHERE post_type = 'shop_order' ";
$wpdb->query( $query );
$results = $wpdb->get_results( $query );
$staging_string = json_encode($results);
$staging_string = md5($staging_string);
if($production_string==$staging_string) {
return true;
}
return false;
}
function import_woocommerce_orders() {
global $wpdb;
$this->get_wpengine_installation();
$post_columns = $this->get_table_fieldnames('wp_posts');
$query = "REPLACE INTO snapshot_".$this->wpengine.".wp_posts ";
$query .= $post_columns;
$query .= " SELECT * FROM wp_".$this->wpengine.".wp_posts ";
$query .= "WHERE post_type = 'shop_order' ";
$wpdb->query( $query );
echo $query."<br/>";
$query = "REPLACE INTO snapshot_".$this->wpengine.".wp_postmeta ";
$query .= "(meta_id,post_id,meta_key,meta_value) ";
$query .= " (SELECT DISTINCT pm.* FROM wp_".$this->wpengine.".wp_postmeta AS pm ";
$query .= " LEFT JOIN wp_".$this->wpengine.".wp_posts AS p ON p.ID = pm.post_id ";
$query .= "WHERE p.post_type = 'shop_order' ) ";
$wpdb->query( $query );
echo $query."<br/>";
$query = "SELECT pm.* FROM wp_".$this->wpengine.".wp_postmeta AS pm ";
$query .= "LEFT JOIN wp_".$this->wpengine.".wp_posts AS p ON p.ID = pm.post_id ";
$query .= "WHERE p.post_type = 'shop_order' ";
echo $query."<br/>";
$postmeta = $wpdb->get_results( $query );
foreach($postmeta as $index=>$val) {
$query = "SELECT * FROM ".$dbname['staging'].".wp_postmeta ";
$query .= "WHERE post_id = '".$val->post_id."' ";
$query .= "AND meta_key = '".$val->meta_key."' ";
echo $query."<br/>";
$ifexist = $wpdb->get_results( $query );
// If we don't find an entry we can copy this record over
if( count($ifexist)<=0) {
$query = "REPLACE INTO ".$dbname['staging'].".wp_postmeta ";
$query .= $get_postmeta_columns." ";
$query .= "(SELECT post_id,meta_key,meta_value FROM ".$dbname['production'].".wp_postmeta ";
$query .= "WHERE post_id = '".$val->post_id."' ";
$query .= "AND meta_key = '".$val->meta_key."' )";
echo $query."<br/>";
$wpdb->query( $query );
}
}
$query = "REPLACE INTO snapshot_".$this->wpengine.".wp_woocommerce_order_items ";
$query .= $this->get_table_fieldnames('wp_woocommerce_order_items')." ";
$query .= "SELECT * FROM wp_".$this->wpengine.".wp_woocommerce_order_items ";
$wpdb->query( $query );
echo $query."<br/>";
$query = "REPLACE INTO snapshot_".$this->wpengine."wp_woocommerce_order_itemmeta ";
$query .= $this->get_table_fieldnames('wp_woocommerce_order_itemmeta')." ";
$query .= "SELECT * FROM wp_".$this->wpengine.".wp_woocommerce_order_itemmeta ";
$wpdb->query( $query );
echo $query."<br/>";
wp_die();
}
function remove_post_conflicts(){
global $wpdb;
$this->get_wpengine_installation();
$chunk['batch'] = $_REQUEST["batch"];
$chunk['size'] = $_REQUEST['size'];
$chunk['type'] = $_REQUEST['type'];
$item_index = $chunk['size'] * ($chunk['batch']-1);
if($chunk['type'] == "posts") {
$this->get_overwrite_posts();
$chunk_array = array_chunk($this->overwrite_posts, $chunk['size']);
// Batch 1 needs to get index 0
$this_chunk = $chunk_array[0];
foreach($this_chunk as $key=>$val) {
//print_r($val);
//echo "<br/><br/>";
$auto_increment = $this->get_autoincrement('wp_posts');
echo "Auto Increment is ".$auto_increment."<br/>";
$posts_result = $wpdb->update(
"wp_posts"
, array( 'ID' => $auto_increment )
, array( 'ID' => $val->ID )
, array( '%d' )
);
echo $wpdb->last_query."<br/>";
if($posts_result) {
echo "Re-setting the Staging wp_posts <b>post_id</b> from ".$val->ID." to ".$auto_increment."<br/>" ;
echo $val->ID."\n\r";
$fixit = scandir( plugin_dir_path(__FILE__)."post_conflicts");
echo "<ul>";
foreach($fixit as $index=>$file) {
if( preg_match("#[.]php$#", $file)){
require( "post_conflicts/".$file);
}
}
echo "</ul>";
$query = "ALTER TABLE wp_".$this->wpengine.".wp_posts AUTO_INCREMENT = ".($auto_increment+1)." ";
//echo $query;
$wpdb->query( $query );
echo "Autoincrement amount increased from ".$auto_increment." to ".($auto_increment+1)."<br/>";
//echo "<hr/>";
}
echo "<hr/>";
}
}
else if($chunk['type'] == "postmeta") {
$this->get_overwrite_postmeta();
$chunk_array = array_chunk($this->overwrite_postmeta, $chunk['size']);
// Turns out we don't actually need the chunk index since each time this loads the overwrite_postmeta is reset
// It just works out in the end, like magic
$this_chunk = $chunk_array[0];
// print_r($chunk_array);
// die();
foreach($this_chunk as $key=>$val) {
//if($i>10) {break;}
$auto_increment = $this->get_autoincrement('wp_postmeta');
$fixit = scandir( plugin_dir_path(__FILE__)."postmeta_conflicts");
echo "<ul>";
foreach($fixit as $index=>$file) {
if( preg_match("#[.]php$#", $file)){
require( "postmeta_conflicts/".$file);
}
}
echo "</ul>";
$query = "ALTER TABLE wp_".$this->wpengine.".wp_postmeta AUTO_INCREMENT = ".($auto_increment+1)." ";
echo "Autoincrement amount increased from ".$auto_increment." to ".($auto_increment+1)."<br/>";
//echo "<hr/>";
$wpdb->query( $query );
}
}
wp_die();
}
function create_menu() {
// Create Tools sub-menu.
$this->page = add_submenu_page( 'tools.php', __( 'WooCommerce Order Sync', 'bm-woocommerce-ordersync' ), __( 'WooCommerce Order Sync', 'bm-woocommerce-ordersync' ), 'manage_options', 'bm-woocommerce-ordersync', array( $this, 'settings_page' ) );
}
function get_wpengine_installation() {
global $wpdb;
if(preg_match("#^snapshot_(.*)$#",$wpdb->dbname, $tmp)) {
// This must be getting loaded from the 'Staging' context
$wpengine = $tmp[1];
// Check that both the 'Production' and 'Staging' databases exist
$query = "SHOW DATABASES LIKE 'wp_".$wpengine."' ";
if( count($wpdb->get_results( $query ))>0) {
$this->wpengine = $wpengine;
return $wpengine;
}
}
else if(preg_match("#^wp_(.*)^#",$wpdb->dbname, $tmp)) {
// This must be getting loaded from the 'Production' context
$wpengine = $tmp[1];
// Check that both the 'Production' and 'Staging' databases exist
$query = "SHOW DATABASES LIKE 'snapshot_".$wpengine."' ";
if( count($wpdb->get_results( $query ))>0) {
$this->wpengine = $wpengine;
return $wpengine;
}
}
return false;
}
function get_table_fieldnames($tablename) {
global $wpdb;
$query = "
SELECT DISTINCT column_name
FROM information_schema.columns
WHERE table_name='".$tablename."';
";
//echo $query."<hr/>";
$columns = $wpdb->get_results( $query );
foreach($columns as $index=>$val) {
$field_names.= ",".$val->column_name;
}
$field_names = "(".substr($field_names,1).")";
return $field_names;
}
function get_overwrite_posts() {
global $wpdb;
// This batch needs new IDs
// This SQL statement might be a tad confusing, so here is a quick explanation
// We want to get a list of all the posts in the 'Production' site whose Post IDs
// have maches in the 'Staging' site.
// Then get a list of all posts
$query = "";
$query .= "SELECT * FROM wp_".$this->wpengine.".wp_posts ";
$query .= "WHERE ID IN ( ";
$query .= "SELECT ID FROM snapshot_".$this->wpengine.".wp_posts ";
$query .= "WHERE ID IN( ";
$query .= "SELECT ID FROM wp_".$this->wpengine.".wp_posts ";
$query .= "WHERE post_type = 'shop_order' ";
$query .= ") ";
$query .= "AND post_type != 'shop_order' ";
$query .= "ORDER BY ID ASC ";
$query .= ") ";
$query .= "ORDER BY ID ASC ";
//$query .= "AND wp_".$this->wpengine.".post_type != 'shop_order' ";
$overwrite_posts = $wpdb->get_results( $query );
//echo $query."<br/>";
//die();
$this->overwrite_posts = $overwrite_posts;
//echo $this->wpengine."<br/>";
//die();
return $overwrite_posts;
}
function get_overwrite_postmeta() {
global $wpdb;
// This batch needs new IDs
$query = "
SELECT * FROM wp_".$this->wpengine.".wp_postmeta
WHERE meta_id IN (
SELECT pm.meta_id FROM snapshot_".$this->wpengine.".wp_postmeta AS pm
LEFT JOIN snapshot_".$this->wpengine.".wp_posts AS p ON p.ID = pm.post_id
";
$query .= "WHERE meta_id IN( ";
$query .= "SELECT pm.meta_id FROM wp_".$this->wpengine.".wp_posts AS p ";
$query .= "LEFT JOIN wp_".$this->wpengine.".wp_postmeta AS pm ON p.ID = pm.post_id ";
$query .= "WHERE p.post_type = 'shop_order' ";
$query .= ") ";
$query .= "AND p.post_type != 'shop_order' ";
$query .= "
)
";
// echo $query."<hr/>";
//die();
//die();
$overwrite_postmeta = $wpdb->get_results( $query );
$this->overwrite_postmeta = $overwrite_postmeta;
return $overwrite_postmeta;
}
function get_autoincrement($tablename, $environment='production') {
global $wpdb;
if($environment == 'production'){
$table_prefix = "wp_";
}
else if($environment == 'staging'){
$table_prefix = "snapshot_";
}
$query = "SELECT `AUTO_INCREMENT` FROM information_schema.TABLES ";
$query .= "WHERE TABLE_SCHEMA = '".$table_prefix.$this->wpengine."' ";
$query .= "AND TABLE_NAME = '".$tablename."' ";
// echo $query."<br/>";
$auto_increment = $wpdb->get_results( $query );
return $auto_increment[0]->AUTO_INCREMENT;
}
function set_wpengine_databases() {
}
function settings_page() {
global $wpdb;
ob_start();
include("tmpl/admin.php");
$output = ob_get_contents(); // Put ob content in a variable
$ob_end_clean();
echo $output;
}
}
$bm_woocommerce_ordersync = new bm_woocommerce_ordersync;
$bm_woocommerce_ordersync->init();
?>