Skip to content

Commit

Permalink
Logged in user array error fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
kylephillips committed Mar 24, 2015
1 parent 4dc3ebd commit 4fd2b8f
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
16 changes: 13 additions & 3 deletions app/Entities/Favorite/SyncUserFavorite.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public function cookie()
public function updateUserMeta($favorites)
{
if ( !is_user_logged_in() ) return false;
$user_id = get_current_user_id();
update_user_meta( $user_id, 'simplefavorites', $favorites );
return update_user_meta( get_current_user_id(), 'simplefavorites', $this->array_flatten($favorites) );
}

/**
Expand All @@ -75,9 +74,20 @@ private function removeFavorite()
private function addFavorite()
{
$favorites = $this->user->getFavorites();
array_push($favorites, $this->post_id);
$favorites[] = $this->post_id;
$this->updateUserMeta($favorites);
return $favorites;
}

/**
* Flatten Array
*/
private function array_flatten(array $array)
{
$return = array();
array_walk_recursive($array, function($a) use (&$return) { $return[] = $a; });
return $return;
}


}
4 changes: 2 additions & 2 deletions app/Entities/User/UserFavorites.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function __construct($user_id)
*/
public function getFavoritesArray()
{
if ( $this->user_id ) return get_user_meta($this->user_id, 'simplefavorites', true);
if ( is_user_logged_in() ) return get_user_meta(get_current_user_id(), 'simplefavorites', true);
if ( $this->user_id ) return get_user_meta($this->user_id, 'simplefavorites', false);
if ( is_user_logged_in() ) return get_user_meta(get_current_user_id(), 'simplefavorites', false);
return $this->user_repo->getFavorites();
}

Expand Down
9 changes: 7 additions & 2 deletions app/Entities/User/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ public function getsButton()
public function getFavorites()
{
$saveType = $this->settings_repo->saveType();
if ( is_user_logged_in() ) return get_user_meta(get_current_user_id(), 'simplefavorites', true);

// if ( is_user_logged_in() ) {
// $favorites = get_user_meta(get_current_user_id(), 'simplefavorites');
// if ( empty($favorites) ) return array();
// return $favorites[0];
// }
$favorites = ( $saveType == 'cookie' ) ? $this->getCookieFavorites() : $this->getSessionFavorites();
return $favorites;
}
Expand Down Expand Up @@ -65,7 +70,7 @@ private function getCookieFavorites()
public function isFavorite($post_id)
{
$all_favorites = $this->getFavorites();
return ( in_array($post_id, $all_favorites) ) ? true : false;
return ( isset($all_favorites) && in_array($post_id, $all_favorites) ) ? true : false;
}


Expand Down
2 changes: 1 addition & 1 deletion assets/js/simple-favorites.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions assets/js/src/simple-favorites.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ function get_favorites()
action : 'simplefavorites_list'
},
success: function(data){
console.log(data);
var favorites = [];
$.each(data.favorites, function(i, v){
favorites[i] = v;
Expand Down Expand Up @@ -111,6 +112,9 @@ function submit_favorite(button)
nonce : simple_favorites_nonce,
postid : post_id,
status : status
},
success: function(data){
console.log(data);
}
});
}
Expand Down

0 comments on commit 4fd2b8f

Please sign in to comment.