Last active
August 29, 2015 14:05
-
-
Save birgire/d3c2d94a6d0e67d2c991 to your computer and use it in GitHub Desktop.
WordPress: Merge array edits made by a given user.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* function merge_user_edits() | |
* | |
* Merge the user edits to the existing meta data: | |
* | |
* @author birgire | |
* @version 0.0.1 | |
* @param integer $user_id | |
* @param array $data | |
* @param array $edits | |
* @param string $key | |
* @return array $new | |
* | |
*/ | |
function merge_user_edits( $user_id = 0, $data = array(), $edits = array(), $key = '' ) | |
{ | |
$count_data = count( $data ); | |
$count_edits = count( $edits ); | |
$tmp = array(); | |
$new = array(); | |
if( $user_id > 0 && $count_edits > 0 && $count_data > 0 ) | |
{ | |
for ( $i = 0; $i < $count_data; $i++ ) | |
{ | |
if( isset( $data[$i][$key] ) && $user_id == $data[$i][$key] ) | |
{ | |
$tmp[] = $data[$i]; | |
} | |
} | |
$new = $edits + $tmp; | |
$new = array_slice( $new, 0, count( $edits ) ); | |
} | |
return $new; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Usage examples for the merge_user_edits() function | |
* | |
* @author birgire | |
* @version 0.0.1 | |
*/ | |
//---------------------------------------- | |
// All meta data: | |
//---------------------------------------- | |
$data = array( | |
array( | |
'figuresugsubjectfrom' => '1', | |
'figuresugsubjectposition' => 'CEO', | |
'figuresugsubjectworkplace' => 'Office 1', | |
'figuresugsubjectlocation' => 'London', | |
'figuresugsubjectstatus' => 'on', | |
), | |
array( | |
'figuresugsubjectfrom' => '2', | |
'figuresugsubjectposition' => 'CTO', | |
'figuresugsubjectworkplace' => 'Office 2', | |
'figuresugsubjectlocation' => 'Paris', | |
'figuresugsubjectstatus' => 'on', | |
), | |
array( | |
'figuresugsubjectfrom' => '1', | |
'figuresugsubjectposition' => 'Manager', | |
'figuresugsubjectworkplace' => 'Office 3', | |
'figuresugsubjectlocation' => 'Reykjavik', | |
'figuresugsubjectstatus' => 'on', | |
), | |
); | |
//---------------------------------------- | |
// Edits #1 from user with user_id = 1. | |
// Two rows with edit + One new row: | |
//---------------------------------------- | |
$edits_1 = array( | |
array( // Edit row: | |
'figuresugsubjectfrom' => '1', | |
'figuresugsubjectposition' => 'CEO', | |
'figuresugsubjectworkplace' => 'Office 1b', | |
'figuresugsubjectlocation' => 'London', | |
'figuresugsubjectstatus' => 'on', | |
), | |
array( // Edit row: | |
'figuresugsubjectfrom' => '1', | |
'figuresugsubjectposition' => 'Manager', | |
'figuresugsubjectworkplace' => 'Office 3a', | |
'figuresugsubjectlocation' => 'Reykjavik', | |
'figuresugsubjectstatus' => 'on', | |
), | |
array( // New row: | |
'figuresugsubjectfrom' => '1', | |
'figuresugsubjectposition' => 'Sales', | |
'figuresugsubjectworkplace' => 'Office 10', | |
'figuresugsubjectlocation' => 'Sydney', | |
'figuresugsubjectstatus' => 'off', | |
), | |
); | |
//---------------------------------------- | |
// Edits #2 from user with user_id = 1. | |
// One row with edit + One deleted row: | |
//---------------------------------------- | |
$edits_2 = array( | |
array( // Edit row: | |
'figuresugsubjectfrom' => '1', | |
'figuresugsubjectposition' => 'CEO', | |
'figuresugsubjectworkplace' => 'Office 1c', | |
'figuresugsubjectlocation' => 'London', | |
'figuresugsubjectstatus' => 'on', | |
), | |
); | |
//---------------------------------------- | |
// Merged results: | |
//---------------------------------------- | |
// Example #1 | |
$merge = merge_user_edits( $user_id = 1, $data, $edits_1, 'figuresugsubjectfrom' ); | |
print_r( $merge ); | |
// Example #2 | |
$merge = merge_user_edits( $user_id = 1, $data, $edits_2, 'figuresugsubjectfrom' ); | |
print_r( $merge ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment