Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Track changes to options more deeply #573

Merged
merged 3 commits into from Jul 18, 2014
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 37 additions & 4 deletions includes/connector.php
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--;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shadyvb Can you teach me what this means?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fjarrett If you're talking about $deep, i used it so i can control how deep should i check for changes in the array. 0 stands for the first level only ( do not check children at all ), 1 the first level of children, etc .. for options that are stored hierarchically <input name='plugin_name[section][field]' /> .. and since the function is calling itself, i need to decrement the value on each call.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shadyvb Oh I get it now, decrementing the value. So $deep-- is the opposite of $deep++, makes sense. I've just never seen it before so it almost seemed like a typo! Thanks for explaining.

$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;
}

}