Skip to content

Commit

Permalink
Refactor settings save for better removal
Browse files Browse the repository at this point in the history
  • Loading branch information
jrfoell committed Sep 24, 2021
1 parent 6ae49da commit 5eb05bc
Showing 1 changed file with 50 additions and 8 deletions.
58 changes: 50 additions & 8 deletions src/WPStrava/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ public function sanitize_nickname( $nicknames ) {
}
}

// Process $nicknames so indexes start with zero.
$nicknames = array_merge( $nicknames, array() );
// Rebase array keys after unset.
$nicknames = array_values( $nicknames );
}

foreach ( $nicknames as $index => $nickname ) {
Expand All @@ -351,13 +351,54 @@ public function sanitize_nickname( $nicknames ) {
*
* Renamed from sanitize_token().
*
* @param string $id Client ID.
* @return string
* @param array $ids Client IDs.
* @return array
* @author Justin Foell <[email protected]>
* @since 2.0
*/
public function sanitize_id( $id ) {
return $id;
public function sanitize_id( $ids ) {
$this->get_ids();

if ( ! is_array( $ids ) ) {
$ids = array( $ids );
}

// Filter empty IDs.
$ids = array_filter( $ids );

// Rebase array keys after unset.
$ids = array_values( $ids );

$this->maybe_clean_info( $ids );

$this->ids = $ids;

return $ids;
}

/**
* Remove IDs from strava_info that are being deleted.
*
* @param array $ids IDs that we're keeping.
* @author Justin Foell <[email protected]>
* @since 2.10.1
*/
private function maybe_clean_info( $ids ) {
$update = false;

$infos = $this->info;

foreach ( $infos as $id => $info ) {
if ( ! in_array( $id, $ids ) ) {
$update = true;
unset( $infos[ $id ] );
}
}

if ( $update ) {
update_option( 'strava_info', $infos );
}

}

/**
Expand Down Expand Up @@ -584,10 +625,11 @@ public function get_ids() {
foreach ( $ids as $index => $id ) {
if ( empty( $id ) ) {
unset( $ids[ $index ] );
$ids = array_values( $ids ); // Rebase array keys after unset @see https://stackoverflow.com/a/5943165/2146022
}
}
$this->ids = $ids;

// Rebase array keys after unset @see https://stackoverflow.com/a/5943165/2146022
$this->ids = array_values( $ids );
return $this->ids;
}

Expand Down

0 comments on commit 5eb05bc

Please sign in to comment.