-
Notifications
You must be signed in to change notification settings - Fork 2
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
Experimental auditing features #79
Changes from all commits
729b186
7ebad5d
e455d5e
e820005
8ee2d70
76f376a
18d0b35
deba42e
ad21919
67569b5
b4b4f90
121cc68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
/** | ||
* Newspack Users Admin page | ||
* | ||
* @package Newspack | ||
*/ | ||
|
||
namespace Newspack_Network; | ||
|
||
/** | ||
* Class to handle the Users admin page | ||
*/ | ||
class Users { | ||
|
||
/** | ||
* Runs the initialization. | ||
*/ | ||
public static function init() { | ||
add_filter( 'manage_users_columns', [ __CLASS__, 'manage_users_columns' ] ); | ||
add_filter( 'manage_users_custom_column', [ __CLASS__, 'manage_users_custom_column' ], 10, 3 ); | ||
add_filter( 'users_list_table_query_args', [ __CLASS__, 'users_list_table_query_args' ] ); | ||
} | ||
|
||
/** | ||
* Add a custom column to the Users table | ||
* | ||
* @param array $columns The current columns. | ||
* @return array | ||
*/ | ||
public static function manage_users_columns( $columns ) { | ||
if ( Site_Role::is_hub() ) { | ||
$columns['newspack_network_activity'] = __( 'Newspack Network Activity', 'newspack-network' ); | ||
} | ||
if ( \Newspack_Network\Admin::use_experimental_auditing_features() ) { | ||
$columns['newspack_network_user'] = __( 'Network Original User', 'newspack-network' ); | ||
} | ||
return $columns; | ||
} | ||
|
||
/** | ||
* Add content to the custom column | ||
* | ||
* @param string $value The current column value. | ||
* @param string $column_name The current column name. | ||
* @param int $user_id The current user ID. | ||
* @return string | ||
*/ | ||
public static function manage_users_custom_column( $value, $column_name, $user_id ) { | ||
if ( 'newspack_network_user' === $column_name ) { | ||
$remote_site = get_user_meta( $user_id, \Newspack_Network\Utils\Users::USER_META_REMOTE_SITE, true ); | ||
$remote_id = (int) get_user_meta( $user_id, \Newspack_Network\Utils\Users::USER_META_REMOTE_ID, true ); | ||
if ( $remote_site ) { | ||
return sprintf( | ||
'<a href="%swp-admin/user-edit.php?user_id=%d">%s</a>', | ||
trailingslashit( esc_url( $remote_site ) ), | ||
$remote_id, | ||
sprintf( '%s (#%d)', $remote_site, $remote_id ) | ||
); | ||
} | ||
} | ||
if ( 'newspack_network_activity' === $column_name && Site_Role::is_hub() ) { | ||
$user = get_user_by( 'id', $user_id ); | ||
if ( ! $user ) { | ||
return $value; | ||
} | ||
|
||
$last_activity = \Newspack_Network\Hub\Stores\Event_Log::get( [ 'email' => $user->user_email ], 1 ); | ||
|
||
if ( empty( $last_activity ) ) { | ||
return '-'; | ||
} | ||
|
||
$last_activity = $last_activity[0]; | ||
|
||
$summary = $last_activity->get_summary(); | ||
$event_log_url = add_query_arg( | ||
[ | ||
'page' => \Newspack_Network\Hub\Admin\Event_Log::PAGE_SLUG, | ||
'email' => $user->user_email, | ||
], | ||
admin_url( 'admin.php' ) | ||
); | ||
return sprintf( | ||
'%s: <code>%s</code><br><a href="%s">%s</a>', | ||
__( 'Last Activity', 'newspack-network' ), | ||
$summary, | ||
$event_log_url, | ||
__( 'View all', 'newspack-network' ) | ||
); | ||
|
||
} | ||
return $value; | ||
} | ||
|
||
/** | ||
* Handle the filtering of users by multiple roles. | ||
* Unfortunatelly, `get_views` and `get_views_links` are not filterable, so "All" will | ||
* be displayed as the active filter. | ||
* | ||
* @param array $args The current query args. | ||
*/ | ||
public static function users_list_table_query_args( $args ) { | ||
if ( isset( $_REQUEST['role__in'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
$args['role__in'] = explode( ',', sanitize_text_field( $_REQUEST['role__in'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended | ||
unset( $args['role'] ); | ||
} | ||
return $args; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,23 +15,6 @@ | |
* The Membership_Plans Table | ||
*/ | ||
class Membership_Plans_Table extends \WP_List_Table { | ||
/** | ||
* Whether to show local or network plans. | ||
* | ||
* @var bool | ||
*/ | ||
private $is_local = false; | ||
|
||
/** | ||
* Constructs the controller. | ||
* | ||
* @param bool $is_local Whether to show local or network plans. | ||
*/ | ||
public function __construct( $is_local = false ) { | ||
$this->is_local = $is_local; | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Get the table columns | ||
* | ||
|
@@ -42,12 +25,12 @@ public function get_columns() { | |
'id' => __( 'ID', 'newspack-network' ), | ||
'name' => __( 'Name', 'newspack-network' ), | ||
]; | ||
if ( $this->is_local ) { | ||
$columns['node_url'] = __( '-', 'newspack-network' ); | ||
} else { | ||
$columns['node_url'] = __( 'Node URL', 'newspack-network' ); | ||
} | ||
$columns['site_url'] = __( 'Site URL', 'newspack-network' ); | ||
$columns['network_pass_id'] = __( 'Network ID', 'newspack-network' ); | ||
if ( \Newspack_Network\Admin::use_experimental_auditing_features() ) { | ||
$columns['active_members_count'] = __( 'Active Members', 'newspack-network' ); | ||
$columns['network_pass_discrepancies'] = __( 'Discrepancies', 'newspack-network' ); | ||
} | ||
$columns['links'] = __( 'Links', 'newspack-network' ); | ||
return $columns; | ||
} | ||
|
@@ -57,11 +40,7 @@ public function get_columns() { | |
*/ | ||
public function prepare_items() { | ||
$this->_column_headers = [ $this->get_columns(), [], [], 'id' ]; | ||
if ( $this->is_local ) { | ||
$this->items = Membership_Plans::get_local_membership_plans(); | ||
} else { | ||
$this->items = Membership_Plans::get_membershp_plans_from_nodes(); | ||
} | ||
$this->items = Membership_Plans::get_membershp_plans_from_network(); | ||
} | ||
|
||
/** | ||
|
@@ -72,16 +51,42 @@ public function prepare_items() { | |
* @return string | ||
*/ | ||
public function column_default( $item, $column_name ) { | ||
$memberships_list_url = sprintf( '%s/wp-admin/edit.php?s&post_status=wcm-active&post_type=wc_user_membership&post_parent=%d', $item['site_url'], $item['id'] ); | ||
|
||
if ( $column_name === 'network_pass_id' && $item[ $column_name ] ) { | ||
return sprintf( '<code>%s</code>', $item[ $column_name ] ); | ||
} | ||
if ( $column_name === 'links' ) { | ||
$edit_url = get_edit_post_link( $item['id'] ); | ||
if ( isset( $item['node_url'] ) ) { | ||
$edit_url = sprintf( '%s/wp-admin/post.php?post=%d&action=edit', $item['node_url'], $item['id'] ); | ||
if ( $column_name === 'network_pass_discrepancies' && isset( $item['network_pass_discrepancies'] ) && $item['network_pass_id'] ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we be checking There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for that, |
||
$discrepancies = $item['network_pass_discrepancies']; | ||
$count = count( $discrepancies ); | ||
if ( $count === 0 ) { | ||
return esc_html__( 'None', 'newspack-network' ); | ||
} | ||
|
||
$memberships_list_url_with_emails_url = add_query_arg( | ||
\Newspack_Network\Woocommerce_Memberships\Admin::MEMBERSHIPS_TABLE_EMAILS_QUERY_PARAM, | ||
implode( ',', $discrepancies ), | ||
$memberships_list_url | ||
); | ||
$message = sprintf( | ||
/* translators: %d is the number of members */ | ||
_n( | ||
'%d member doesn\'t match the shared member pool', | ||
'%d members don\'t match the shared member pool', | ||
$count, | ||
'newspack-plugin' | ||
), | ||
$count | ||
); | ||
return sprintf( '<a href="%s">%s</a>', esc_url( $memberships_list_url_with_emails_url ), esc_html( $message ) ); | ||
} | ||
if ( $column_name === 'links' ) { | ||
$edit_url = sprintf( '%s/wp-admin/post.php?post=%d&action=edit', $item['site_url'], $item['id'] ); | ||
return sprintf( '<a href="%s">%s</a>', esc_url( $edit_url ), esc_html__( 'Edit', 'newspack-network' ) ); | ||
} | ||
if ( $column_name === 'active_members_count' && $item[ $column_name ] ) { | ||
return sprintf( '<a href="%s">%s</a>', esc_url( $memberships_list_url ), $item[ $column_name ] ); | ||
} | ||
return isset( $item[ $column_name ] ) ? $item[ $column_name ] : ''; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: A return tag is missing and would be helpful here and in a handful of other methods in this PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 121cc68