'key', 'plural' => 'keys', 'ajax' => false, ] ); } /** * No items found text. */ public function no_items() { esc_html_e( 'No keys found.', 'opalestate-pro' ); } /** * Get list columns. * * @return array */ public function get_columns() { return [ 'cb' => '', 'title' => __( 'Description', 'opalestate-pro' ), 'truncated_key' => __( 'Consumer key ending in', 'opalestate-pro' ), 'user' => __( 'User', 'opalestate-pro' ), 'permissions' => __( 'Permissions', 'opalestate-pro' ), 'last_access' => __( 'Last access', 'opalestate-pro' ), ]; } /** * Column cb. * * @param array $key Key data. * @return string */ public function column_cb( $key ) { return sprintf( '', $key['key_id'] ); } /** * Return title column. * * @param array $key Key data. * @return string */ public function column_title( $key ) { $url = admin_url( 'edit.php?post_type=opalestate_property&page=opalestate-settings&tab=api_keys&edit-key=' . $key['key_id'] ); $user_id = absint( $key['user_id'] ); // Check if current user can edit other users or if it's the same user. $can_edit = current_user_can( 'edit_user', $user_id ) || get_current_user_id() === $user_id; $output = ''; if ( $can_edit ) { $output .= ''; } if ( empty( $key['description'] ) ) { $output .= esc_html__( 'API key', 'opalestate-pro' ); } else { $output .= esc_html( $key['description'] ); } if ( $can_edit ) { $output .= ''; } $output .= ''; // Get actions. $actions = [ /* translators: %s: API key ID. */ 'id' => sprintf( __( 'ID: %d', 'opalestate-pro' ), $key['key_id'] ), ]; if ( $can_edit ) { $actions['edit'] = '' . __( 'View/Edit', 'opalestate-pro' ) . ''; $actions['trash'] = '' . esc_html__( 'Revoke', 'opalestate-pro' ) . ''; } $row_actions = []; foreach ( $actions as $action => $link ) { $row_actions[] = '' . $link . ''; } $output .= '
…' . esc_html( $key['truncated_key'] ) . '
';
}
/**
* Return user column.
*
* @param array $key Key data.
* @return string
*/
public function column_user( $key ) {
$user = get_user_by( 'id', $key['user_id'] );
if ( ! $user ) {
return '';
}
if ( current_user_can( 'edit_user', $user->ID ) ) {
return '' . esc_html( $user->display_name ) . '';
}
return esc_html( $user->display_name );
}
/**
* Return permissions column.
*
* @param array $key Key data.
* @return string
*/
public function column_permissions( $key ) {
$permission_key = $key['permissions'];
$permissions = [
'read' => __( 'Read', 'opalestate-pro' ),
'write' => __( 'Write', 'opalestate-pro' ),
'read_write' => __( 'Read/Write', 'opalestate-pro' ),
];
if ( isset( $permissions[ $permission_key ] ) ) {
return esc_html( $permissions[ $permission_key ] );
} else {
return '';
}
}
/**
* Return last access column.
*
* @param array $key Key data.
* @return string
*/
public function column_last_access( $key ) {
if ( ! empty( $key['last_access'] ) ) {
/* translators: 1: last access date 2: last access time */
$date = sprintf( __( '%1$s at %2$s', 'opalestate-pro' ), date_i18n( get_option( 'date_format' ), strtotime( $key['last_access'] ) ),
date_i18n( get_option( 'time_format' ), strtotime( $key['last_access'] ) ) );
return apply_filters( 'opalestate_api_key_last_access_datetime', $date, $key['last_access'] );
}
return __( 'Unknown', 'opalestate-pro' );
}
/**
* Get bulk actions.
*
* @return array
*/
protected function get_bulk_actions() {
if ( ! current_user_can( 'remove_users' ) ) {
return [];
}
return [
'revoke' => __( 'Revoke', 'opalestate-pro' ),
];
}
/**
* Search box.
*
* @param string $text Button text.
* @param string $input_id Input ID.
*/
public function search_box( $text, $input_id ) {
if ( empty( $_REQUEST['s'] ) && ! $this->has_items() ) { // WPCS: input var okay, CSRF ok.
return;
}
$input_id = $input_id . '-search-input';
$search_query = isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : ''; // WPCS: input var okay, CSRF ok.
echo ''; echo ''; echo ''; submit_button( $text, '', '', false, [ 'id' => 'search-submit', ] ); echo '
'; } /** * Prepare table list items. */ public function prepare_items() { global $wpdb; $per_page = $this->get_items_per_page( 'opalestate_keys_per_page' ); $current_page = $this->get_pagenum(); if ( 1 < $current_page ) { $offset = $per_page * ( $current_page - 1 ); } else { $offset = 0; } $search = ''; if ( ! empty( $_REQUEST['s'] ) ) { // WPCS: input var okay, CSRF ok. $search = "AND description LIKE '%" . esc_sql( $wpdb->esc_like( opalestate_clean( wp_unslash( $_REQUEST['s'] ) ) ) ) . "%' "; // WPCS: input var okay, CSRF ok. } // Get the API keys. $keys = $wpdb->get_results( "SELECT key_id, user_id, description, permissions, truncated_key, last_access FROM {$wpdb->prefix}opalestate_api_keys WHERE 1 = 1 {$search}" . $wpdb->prepare( 'ORDER BY key_id DESC LIMIT %d OFFSET %d;', $per_page, $offset ), ARRAY_A ); // WPCS: unprepared SQL ok. $count = $wpdb->get_var( "SELECT COUNT(key_id) FROM {$wpdb->prefix}opalestate_api_keys WHERE 1 = 1 {$search};" ); // WPCS: unprepared SQL ok. $this->items = $keys; // Set the pagination. $this->set_pagination_args( [ 'total_items' => $count, 'per_page' => $per_page, 'total_pages' => ceil( $count / $per_page ), ] ); } }