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

Hooks. Actions : before_add_favorite, before_remove_favorite and Filter : favorites_before_update_meta #188

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions app/Entities/Favorite/SyncSingleFavorite.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php
namespace Favorites\Entities\Favorite;

use Favorites\Entities\User\UserRepository;
Expand All @@ -7,7 +7,7 @@
/**
* Sync a single favorite to a given save type
*/
class SyncSingleFavorite
class SyncSingleFavorite
{
/**
* The Post ID
Expand Down Expand Up @@ -52,11 +52,13 @@ public function session()
public function cookie()
{
if ( $this->user->isFavorite($this->post_id, $this->site_id) ){
do_action('before_remove_favorite',$this->post_id);
$favorites = $this->removeFavorite();
setcookie( 'simplefavorites', json_encode( $favorites ), time() + apply_filters( 'simplefavorites_cookie_expiration_interval', 31556926 ), '/' );
return;
}
$favorites = $this->addFavorite();
do_action('before_add_favorite',$this->post_id);
setcookie( 'simplefavorites', json_encode( $favorites ), time() + apply_filters( 'simplefavorites_cookie_expiration_interval', 31556926 ), '/' );
return;
}
Expand All @@ -67,6 +69,7 @@ public function cookie()
public function updateUserMeta($favorites)
{
if ( !is_user_logged_in() ) return;
$favorites = apply_filters('favorites_before_update_meta', $favorites);
update_user_meta( intval(get_current_user_id()), 'simplefavorites', $favorites );
}

Expand Down Expand Up @@ -123,11 +126,11 @@ private function addFavorite()
];
}
foreach( $favorites[$key]['groups'] as $group_key => $group){
if ( $group['group_id'] == $this->group_id )
if ( $group['group_id'] == $this->group_id )
$favorites[$key]['groups'][$group_key]['posts'][] = $this->post_id;
}
}
$this->updateUserMeta($favorites);
return $favorites;
}
}
}