Skip to content

Commit

Permalink
Add acess control setting filter
Browse files Browse the repository at this point in the history
  • Loading branch information
raftaar1191 committed Dec 5, 2023
1 parent b909bc3 commit 80a6bd5
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 187 deletions.
30 changes: 21 additions & 9 deletions admin/menu/class-view-analytics-settings-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,26 @@ public function avatar_settings() {
),
array(
'type' => 'toggle',
'description' => __( 'Show Avatar update Count in Public', 'view-analytics' ),
'id' => 'show_view_count',
'description' => __( 'Show Profile Avatar update Count in Public', 'view-analytics' ),
'id' => 'show_view_count_profile_avatar',
'default' => true,
),
array(
'type' => 'toggle',
'description' => __( 'Show Profile Cover Image update Count in Public', 'view-analytics' ),
'id' => 'show_view_count_profile_cover',
'default' => true,
),
array(
'type' => 'toggle',
'description' => __( 'Show Group Avatar update Count in Public', 'view-analytics' ),
'id' => 'show_view_count_group_avatar',
'default' => true,
),
array(
'type' => 'toggle',
'description' => __( 'Show Group Cover Image update Count in Public', 'view-analytics' ),
'id' => 'show_view_count_group_cover',
'default' => true,
),
)
Expand All @@ -206,13 +224,7 @@ public function forum_settings() {
'label' => __( 'Enable Forum/Topic/Reply View Count', 'view-analytics' ),
'id' => 'main',
'default' => true,
),
array(
'type' => 'toggle',
'description' => __( 'Show Forum view Count in Public', 'view-analytics' ),
'id' => 'show_view_count',
'default' => true,
),
)
)
);
}
Expand Down
4 changes: 2 additions & 2 deletions includes/class-view-analytics-activator.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,8 @@ public static function create_table() {
'_view_analytics_forum_table_count_enable',
);

foreach( $default_active_keys as $key ) {
if ( option_exists( $key ) ) {
foreach( $default_active_keys as $key ) {
if ( empty( get_option( $key, false ) ) ) {
update_option( $key, true );
}
}
Expand Down
80 changes: 23 additions & 57 deletions includes/class-view-analytics-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,52 +102,41 @@ public function view_count_enable() {

/**
* Return the View Analytics show count
*
* $author_id = 0. Here this is added for the Media to view by the Non login user as well
*/
public function view_count_show_view_count( $author_id = 0, $group_id = false ) {
public function access( $author_id = 0, $group_id = false, $key = false, $value = false ) {


if ( empty( $author_id ) ) {
return false;
}

$key = empty( $key ) ? 'show_view_count' : $key;

if( ! $this->view_count_enable() ) {
return false;
}

if ( ! $this->get_view_setting_active( 'show_view_count' ) ) {
return false;
}


/**
* if the user is the admin then return true
*/
if( $this->is_admin() ) {
if ( $this->is_admin( $author_id, $group_id ) ) {
return true;
}

/**
* If group id is there check for the Group Admin and Moderations
*/
if(
! empty( $group_id )
&& (
groups_is_user_admin( $author_id, $group_id )
|| groups_is_user_mod( $author_id, $group_id )
)
) {
return true;

if ( ! $this->get_view_setting_active( $key ) ) {
return false;
}

/**
* If group id is there check for the Group Admin and Moderations
*/
if ( ! empty( $group_id ) ) {
$value = groups_is_user_member( $author_id, $group_id );
} else {
$value = $this->is_author( $author_id );
if ( ! empty( $author_id ) ) {
/**
* If group id is there check for the Group Admin and Moderations
*/
if ( ! empty( $group_id ) ) {
$value = groups_is_user_member( $author_id, $group_id );
} else {
$value = $this->is_author( $author_id );
}
}

return apply_filters( $this->create_filter_key( 'show_view_count' ), $value );
return apply_filters( $this->create_filter_key( $key ), $value );
}

/**
Expand Down Expand Up @@ -182,26 +171,10 @@ public function is_buddyboss() {
return defined( 'BP_PLATFORM_VERSION' );
}


/**
* Check if the current user is allow to view the Media View List
*/
public function can_current_user_view_list( $group_id = false ) {

return $this->is_admin( $group_id );
}

/**
* Check if the current user is admin or if you want to check of the user is Group admin
*/
public function is_admin( $group_id = false ) {

$current_user_id = get_current_user_id();

if ( empty( $current_user_id ) ) {
return false;
}

public function is_admin( $author_id = false, $group_id = false ) {

/**
* If user is site admin
Expand All @@ -212,9 +185,9 @@ public function is_admin( $group_id = false ) {

if(
! empty( $group_id )
&& ! empty( $author_id )
&& (
groups_is_user_admin( $current_user_id, $group_id )
|| groups_is_user_mod( $current_user_id, $group_id )
groups_is_user_admin( $author_id, $group_id ) || groups_is_user_mod( $author_id, $group_id )
)
) {
return true;
Expand All @@ -230,13 +203,6 @@ public function is_author( $author_id ) {
return ( get_current_user_id() == $author_id ) ? true : false;
}

/**
* Check if the current user is allow to view the Media View List
*/
public function can_current_user_view_list_current_user( $author_id = 0 ) {
return ( get_current_user_id() == $author_id ) ? true : false;
}

/**
* Return all the media type
*/
Expand Down
3 changes: 2 additions & 1 deletion includes/class-view-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ public function __construct() {
* @return View_Analytics - Main instance.
*/
public static function instance() {

if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
Expand Down Expand Up @@ -468,7 +469,7 @@ private function define_admin_hooks() {
* Menu
*/
$admin_main_menu = new View_Analytics_Admin_Main_Menu( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'admin_menu', $admin_main_menu, 'menu', 100 );
$this->loader->add_action( 'admin_menu', $admin_main_menu, 'menu', 1000 );

$admin_setting_menu = new View_Analytics_Admin_Setting_Menu( $this->get_plugin_name(), $this->get_version() );
$this->loader->add_action( 'init', $admin_setting_menu, 'setting_menu', 100 );
Expand Down
7 changes: 0 additions & 7 deletions includes/forum/class-view-analytics-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,6 @@ public function view_count_key() {
return '_view_analytics_forum_table_count_enable';
}

/**
* Check if the current user is allow to view the Media View List
*/
public function can_current_user_view_list( $group_id = false ) {
return $this->can_current_user_view_list_current_user();
}

/**
* Show the message about when the user has view the Media
*/
Expand Down
2 changes: 1 addition & 1 deletion includes/group/class-view-analytics-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function view_count_key() {
public function get_view_body_message( $user_id, $view_count ) {
$displayname = bp_core_get_user_displayname( $user_id );
$view = _n( 'time', 'times', $view_count, 'view-analytics' );
return sprintf( __( '%s saw your profile %s %s.', 'view-analytics' ), $displayname, $view_count, $view );
return sprintf( __( '%s saw this group %s %s.', 'view-analytics' ), $displayname, $view_count, $view );

}

Expand Down
42 changes: 0 additions & 42 deletions includes/media/class-view-analytics-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,48 +72,6 @@ public function view_count_key() {
return '_view_analytics_media_table_count_enable';
}

/**
* Return the View Analytics show count
* Overwrite this for Media View Count
*/
public function view_count_show_view_count( $author_id = false, $group_id = false ) {

$view = false;

if ( ! $this->view_count_enable() ) {
return $view;
}

if ( $this->get_view_setting_active( 'show_view_count' ) ) {
$view = apply_filters( $this->create_filter_key( 'show_view_count' ), true );
}

return $view;
}

/**
* Return the View Analytics Media show the user view list
*/
public function view_count_show_user_list( $author_id ) {

if( ! $this->view_count_enable() ) {
return false;
}

if ( ! $this->get_view_setting_active( 'show_view_user_list' ) ) {
return false;
}

/**
* if the user is the admin then return true
*/
if( $this->is_admin() ) {
return true;
}

return apply_filters( $this->create_filter_key( 'show_view_user_list' ), $this->is_author( $author_id ) );
}

/**
* Return the View Analytics Media Count Key
*/
Expand Down
2 changes: 1 addition & 1 deletion includes/media/class-view-analytics-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function permissions_check( $request ) {
* Get the Author ID
*/
$author_id = empty( $details['author_id'] ) ? 0 : absint( $details['author_id'] );
if( $this->common->view_count_show_user_list( $author_id ) ) {
if( $this->common->access( $author_id, false, 'show_view_user_list' ) ) {
return true;
}

Expand Down
8 changes: 0 additions & 8 deletions includes/profile/class-view-analytics-common.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,6 @@ public static function instance() {
public function view_count_key() {
return '_view_analytics_profile_table_count_enable';
}


/**
* Check if the current user is allow to view the Profile View List
*/
public function can_current_user_view_list( $group_id = false ) {
return $this->can_current_user_view_list_current_user();
}

/**
* Show the message about when the user has view the Profile
Expand Down
53 changes: 33 additions & 20 deletions public/partials/avatar/view-analytics-public-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,37 @@ public function __construct( $plugin_name, $version ) {
* Show menu option on the profile Page
*/
public function profile_navigation() {

$displayed_user_id = bp_displayed_user_id();

/**
* Check if the curret user has access to view the Profile View Tab
*/
if ( $this->common->view_count_show_view_count() ) {
bp_core_new_nav_item(
array(
'name' => __( 'Profile Avatar Update', 'view-analytics' ),
'slug' => 'profile-avatar-update',
'screen_function' => array( $this, 'xprofile_avatar_view_manage' )
)
);

bp_core_new_nav_item(
array(
'name' => __( 'Profile Cover Update', 'view-analytics' ),
'slug' => 'profile-cover-update',
'screen_function' => array( $this, 'xprofile_cover_view_manage' )
)
);
if ( $displayed_user_id ) {

if ( $this->common->access( $displayed_user_id, false, 'show_view_count_profile_avatar' ) ) {

bp_core_new_nav_item(
array(
'name' => __( 'Profile Avatar Update', 'view-analytics' ),
'slug' => 'profile-avatar-update',
'screen_function' => array( $this, 'xprofile_avatar_view_manage' ),
'show_for_displayed_user' => false
)
);
}

if ( $this->common->access( $displayed_user_id, false, 'show_view_count_profile_cover' ) ) {
bp_core_new_nav_item(
array(
'name' => __( 'Profile Cover Update', 'view-analytics' ),
'slug' => 'profile-cover-update',
'screen_function' => array( $this, 'xprofile_cover_view_manage' ),
'show_for_displayed_user' => false
)
);
}

}
}

Expand All @@ -105,11 +116,13 @@ public function profile_navigation() {
public function group_navigation() {

$current_group = groups_get_current_group();

/**
* Check if the curret user has access to view the Profile View Tab
*/
if ( ! empty( $current_group ) && $this->common->view_count_show_view_count() ) {
if ( ! empty( $current_group->id ) ) {

$current_user_id = get_current_user_id();

$group_link = bp_get_group_permalink( $current_group );

Expand All @@ -120,7 +133,7 @@ public function group_navigation() {
'parent_url' => $group_link,
'position' => 100,
'screen_function' => array( $this, 'group_avatar_view_manage' ),
'user_has_access' => $this->common->can_current_user_view_list() // Only the logged in user can access this on his/her profile
'user_has_access' => $this->common->access( $current_user_id, $current_group->id, 'show_view_count_group_avatar' )
) );

bp_core_new_subnav_item( array(
Expand All @@ -130,7 +143,7 @@ public function group_navigation() {
'parent_url' => $group_link,
'position' => 100,
'screen_function' => array( $this, 'group_cover_view_manage' ),
'user_has_access' => $this->common->can_current_user_view_list() // Only the logged in user can access this on his/her profile
'user_has_access' => $this->common->access( $current_user_id, $current_group->id, 'show_view_count_group_cover' )
) );
}
}
Expand Down
2 changes: 1 addition & 1 deletion public/partials/group/view-analytics-public-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function navigation() {
'parent_url' => $group_link,
'position' => 100,
'screen_function' => array( $this, 'view_manage' ),
'user_has_access' => $this->common->view_count_show_view_count( $current_user_id, $current_group->id )
'user_has_access' => $this->common->access( $current_user_id, $current_group->id )
) );
}
}
Expand Down
Loading

0 comments on commit 80a6bd5

Please sign in to comment.