Skip to content

Commit

Permalink
Add hooks for medias
Browse files Browse the repository at this point in the history
  • Loading branch information
raftaar1191 committed Dec 7, 2023
1 parent a0b0e0e commit e742554
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions includes/media/view-analytics-counts.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ public function update_view_count( $key_id, $hash_id = '0', $media_id = 0, $atta
* Check if empty
*/
if ( empty( $views ) ) {
$this->common->table->user_add(
$id = $this->common->table->user_add(
$current_user_id,
$key_id,
$hash_id,
Expand All @@ -271,32 +271,41 @@ public function update_view_count( $key_id, $hash_id = '0', $media_id = 0, $atta
$media_type,
$components
);

/**
* Fire a hook when someone view media for the first time
*/
do_action( $this->common->create_hooks_key( '_view_media' ), $id, $key_id, 0, 1, $current_user_id );

} else {
$id = $views['id'];

/**
* Ref count
*/
$ref_count = empty( $views['ref_count'] ) ? 1 : absint( $views['ref_count'] ) + 1;
$old_ref_count = empty( $views['ref_count'] ) ? 1 : absint( $views['ref_count'] );
$ref_count = $old_ref_count + 1;
do_action( $this->common->create_hooks_key( '_view_media' ), $id, $key_id, $old_ref_count, $ref_count, $current_user_id );

/**
* Users list
*/
$users_list = empty( $views['users_list'] ) ? array() : maybe_unserialize( $views['users_list'] );
$old_user_count = count( $users_list );

if ( ! in_array( $current_user_id, $users_list ) ) {
array_unshift( $users_list, $current_user_id );
}

/**
* Users view count
*/
$user_count = count( $users_list );
do_action( $this->common->create_hooks_key( '_users_view_media' ), $id, $key_id, $old_user_count, $user_count, $current_user_id );

/**
* update session count
*/
$session_count = $this->common->table->user_get( $current_user_id, $key_id, true );
$session_count = empty( $session_count ) ? absint( $views['session_count'] ) + 1 : absint( $views['session_count'] );
$session_count = $this->common->table->user_get( $current_user_id, $key_id, true );
$old_session_count = absint( $views['session_count'] );
$session_count = empty( $session_count ) ? $old_session_count + 1 : $old_session_count;
do_action( $this->common->create_hooks_key( '_sessions_view_media' ), $id, $key_id, $old_session_count, $session_count, $current_user_id );

$this->common->table->user_update(
$id,
Expand Down

0 comments on commit e742554

Please sign in to comment.