Skip to content

Commit

Permalink
Merge pull request #573 from x-team/deep-options-tracking
Browse files Browse the repository at this point in the history
Track changes to options more deeply
  • Loading branch information
frankiejarrett committed Jul 18, 2014
2 parents bf12bba + 8e87e26 commit bed8bd1
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions includes/connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,14 @@ public static function delayed_log_commit() {

/**
* Compare two values and return changed keys if they are arrays
* @param mixed $old_value Value before change
* @param mixed $new_value Value after change
*
* @param mixed $old_value Value before change
* @param mixed $new_value Value after change
* @param bool|int $deep Get array children changes keys as well, not just parents
*
* @return array
*/
public static function get_changed_keys( $old_value, $new_value ) {
public static function get_changed_keys( $old_value, $new_value, $deep = false ) {
if ( ! is_array( $old_value ) && ! is_array( $new_value ) ) {
return array();
}
Expand Down Expand Up @@ -191,7 +194,37 @@ function( $value ) {
}
);

return array_values( array_unique( $result ) );
$result = array_values( array_unique( $result ) );

if ( false === $deep ) {
return $result; // Return an numerical based array with changed TOP PARENT keys only
}

$result = array_fill_keys( $result, null );

foreach ( $result as $key => $val ) {
if ( in_array( $key, $unique_keys_old ) ) {
$result[ $key ] = false; // Removed
}
elseif ( in_array( $key, $unique_keys_new ) ) {
$result[ $key ] = true; // Added
}
elseif ( $deep ) { // Changed, find what changed, only if we're allowed to explore a new level
if ( is_array( $old_value[ $key ] ) && is_array( $new_value[ $key ] ) ) {
$inner = array();
$parent = $key;
$deep--;
$changed = self::get_changed_keys( $old_value[ $key ], $new_value[ $key ], $deep );
foreach ( $changed as $child => $change ) {
$inner[ $parent . '::' . $child ] = $change;
}
$result[ $key ] = 0; // Changed parent which has a changed children
$result = array_merge( $result, $inner );
}
}
}

return $result;
}

}

0 comments on commit bed8bd1

Please sign in to comment.