Skip to content

Commit d1f477f

Browse files
Sync DB options table
Ignore transient options
1 parent 333f8ad commit d1f477f

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

includes/class-stl-db-comparer.php

+38-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class STL_DB_Comparer {
5252
* @var array
5353
*/
5454
private $excluded_tables = array(
55-
'options', // Often contains transients and cache data
55+
// 'options', // Often contains transients and cache data
5656
'usermeta', // User session data changes frequently
5757
'sessions', // Session data changes frequently
5858
'term_taxonomy', // Term taxonomy data changes frequently
@@ -290,6 +290,22 @@ public function get_changes( $force = false ) {
290290

291291
return $grouped_changes;
292292
}
293+
294+
private function is_excluded_name($name): bool {
295+
if (strpos($name, '_site_transient') === 0) {
296+
return true;
297+
}
298+
299+
if (strpos($name, '_transient') === 0) {
300+
return true;
301+
}
302+
303+
if ($name === 'wp_staging_user_roles') {
304+
return true;
305+
}
306+
307+
return false;
308+
}
293309

294310
/**
295311
* Compare a single table between production and staging
@@ -325,11 +341,15 @@ private function compare_table( $production_table, $staging_table, $table_name )
325341
// Find new entries (in staging but not in production)
326342
$new_ids = array_diff( $staging_ids, $production_ids );
327343
foreach ( $new_ids as $id ) {
344+
if ( $table_name == 'options' && ! empty($staging_row['option_name']) && $this->is_excluded_name($staging_row['option_name'])){
345+
continue;
346+
}
347+
328348
$staging_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$staging_table} WHERE {$primary_key} = %s", $id ), ARRAY_A );
329349

330350
$changes[] = array(
331351
'id' => $id,
332-
'type' => 'added',
352+
'type' => 'added' . (!empty($staging_row['option_name']) ? " > {$staging_row['option_name']}" : ''),
333353
'summary' => sprintf( __( 'New entry with ID %s', 'staging2live' ), $id ),
334354
'details' => $staging_row,
335355
);
@@ -338,11 +358,14 @@ private function compare_table( $production_table, $staging_table, $table_name )
338358
// Find deleted entries (in production but not in staging)
339359
$deleted_ids = array_diff( $production_ids, $staging_ids );
340360
foreach ( $deleted_ids as $id ) {
361+
if ( $table_name == 'options' && ! empty($staging_row['option_name']) && $this->is_excluded_name($staging_row['option_name']))
362+
continue;
363+
341364
$production_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$production_table} WHERE {$primary_key} = %s", $id ), ARRAY_A );
342365

343366
$changes[] = array(
344367
'id' => $id,
345-
'type' => 'deleted',
368+
'type' => 'deleted' . (!empty($staging_row['option_name']) ? " > {$staging_row['option_name']}" : ''),
346369
'summary' => sprintf( __( 'Entry with ID %s deleted', 'staging2live' ), $id ),
347370
'details' => $production_row,
348371
);
@@ -354,7 +377,13 @@ private function compare_table( $production_table, $staging_table, $table_name )
354377
foreach ( $common_ids as $id ) {
355378
$production_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$production_table} WHERE {$primary_key} = %s", $id ), ARRAY_A );
356379
$staging_row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$staging_table} WHERE {$primary_key} = %s", $id ), ARRAY_A );
357-
380+
381+
$option = '';
382+
if (!empty($production_row['option_name']))
383+
$option = $production_row['option_name'];
384+
if (!empty($staging_row['option_name']))
385+
$option = $staging_row['option_name'];
386+
358387
$row_changes = array();
359388

360389
foreach ( $columns as $column ) {
@@ -394,10 +423,13 @@ private function compare_table( $production_table, $staging_table, $table_name )
394423
if ( ! empty( $row_changes ) ) {
395424
// Get a human-readable name for this entry if possible
396425
$entry_name = $this->get_entry_name( $table_name, $production_row );
397-
426+
427+
if ( $table_name == 'options' && ! empty($option) && $this->is_excluded_name($option))
428+
continue;
429+
398430
$changes[] = array(
399431
'id' => $id,
400-
'type' => 'modified',
432+
'type' => 'modified' . ($option != '' ? " > $option" : ''),
401433
'summary' => $entry_name
402434
? sprintf( __( 'Changes to "%s"', 'staging2live' ), $entry_name )
403435
: sprintf( __( 'Entry with ID %s modified', 'staging2live' ), $id ),

0 commit comments

Comments
 (0)