PluginProbe
WP Last Login / 2
WP Last Login v2
trunk 1.0 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.3.0 1.4.0 2 3 4 5 6 7
wp-last-login / wp-last-login.php

wp-last-login.php in WP Last Login 2, at wp-last-login.php

209 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: WP Last Login
4 * Plugin URI: http://en.wp.obenland.it/wp-last-login/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-last-login
5 * Description: Displays the date of the last login in user lists.
6 * Version: 2
7 * Author: Konstantin Obenland
8 * Author URI: http://en.wp.obenland.it/#utm_source=wordpress&utm_medium=plugin&utm_campaign=wp-last-login
9 * Text Domain: wp-last-login
10 * Domain Path: /lang
11 * License: GPLv2
12 *
13 * @package WP Last Login
14 */
15
16 if ( ! class_exists( 'Obenland_Wp_Plugins_V4' ) ) {
17 require_once 'obenland-wp-plugins.php';
18 }
19
20 /**
21 * Class Obenland_Wp_Last_Login.
22 */
23 class Obenland_Wp_Last_Login extends Obenland_Wp_Plugins_V4 {
24
25 /**
26 * Constructor.
27 *
28 * @author Konstantin Obenland
29 * @since 1.0 - 23.01.2012
30 * @access public
31 */
32 public function __construct() {
33 parent::__construct( array(
34 'textdomain' => 'wp-last-login',
35 'plugin_path' => __FILE__,
36 'donate_link_id' => 'K32M878XHREQC',
37 ) );
38
39 load_plugin_textdomain( 'wp-last-login', false, 'wp-last-login/lang' );
40
41 $this->hook( 'wp_login' );
42
43 /**
44 * Programmers:
45 * To limit this information to certain user roles, add a filter to
46 * 'wpll_current_user_can' and check for user permissions, returning
47 * true or false!
48 *
49 * Example:
50 *
51 * function prefix_wpll_visibility( $bool ) {
52 * return current_user_can( 'manage_options' ); // Only for Admins
53 * }
54 * add_filter( 'wpll_current_user_can', 'prefix_wpll_visibility' );
55 */
56 if ( is_admin() && apply_filters( 'wpll_current_user_can', true ) ) {
57
58 $this->hook( 'manage_site-users-network_columns', 'add_column', 1 );
59 $this->hook( 'manage_users_columns', 'add_column', 1 );
60 $this->hook( 'wpmu_users_columns', 'add_column', 1 );
61 $this->hook( 'admin_print_styles-users.php', 'column_style' );
62 $this->hook( 'admin_print_styles-site-users.php', 'column_style' );
63 $this->hook( 'manage_users_custom_column' );
64 $this->hook( 'manage_users_sortable_columns', 'add_sortable' );
65 $this->hook( 'manage_users-network_sortable_columns', 'add_sortable' );
66 $this->hook( 'pre_get_users' );
67 }
68 }
69
70 /**
71 * Update the login timestamp.
72 *
73 * @author Konstantin Obenland
74 * @since 1.0 - 23.01.2012
75 * @access public
76 *
77 * @param string $user_login The user's login name.
78 *
79 * @return void
80 */
81 public function wp_login( $user_login ) {
82 $user = get_user_by( 'login', $user_login );
83 update_user_meta( $user->ID, $this->textdomain, time() );
84 }
85
86 /**
87 * Adds the last login column to the network admin user list.
88 *
89 * @author Konstantin Obenland
90 * @since 1.0 - 23.01.2012
91 * @access public
92 *
93 * @param array $cols The default columns.
94 *
95 * @return array
96 */
97 public function add_column( $cols ) {
98 $cols[ $this->textdomain ] = __( 'Last Login', 'wp-last-login' );
99 return $cols;
100 }
101
102 /**
103 * Adds the last login column to the network admin user list.
104 *
105 * @author Konstantin Obenland
106 * @since 1.0 - 23.01.2012
107 * @access public
108 *
109 * @param string $value Value of the custom column.
110 * @param string $column_name The name of the column.
111 * @param int $user_id The user's id.
112 *
113 * @return string
114 */
115 public function manage_users_custom_column( $value, $column_name, $user_id ) {
116 if ( $this->textdomain === $column_name ) {
117 $value = __( 'Never.', 'wp-last-login' );
118 $last_login = (int) get_user_meta( $user_id, $this->textdomain, true );
119
120 if ( $last_login ) {
121 $format = apply_filters( 'wpll_date_format', get_option( 'date_format' ) );
122 $value = date_i18n( $format, $last_login );
123 }
124 }
125
126 return $value;
127 }
128
129 /**
130 * Register the column as sortable.
131 *
132 * @author Konstantin Obenland
133 * @since 1.2.0 - 11.12.2012
134 * @access public
135 *
136 * @param array $columns User table columns.
137 *
138 * @return array
139 */
140 public function add_sortable( $columns ) {
141 $columns[ $this->textdomain ] = $this->textdomain;
142
143 return $columns;
144 }
145
146 /**
147 * Handle ordering by last login.
148 *
149 * @since 1.2.0 - 11.12.2012
150 * @access public
151 *
152 * @param WP_User_Query $user_query Request arguments.
153 *
154 * @return WP_User_Query
155 */
156 public function pre_get_users( $user_query ) {
157 if ( isset( $user_query->query_vars['orderby'] ) && $this->textdomain === $user_query->query_vars['orderby'] ) {
158 $user_query->query_vars = array_merge( $user_query->query_vars, array(
159 // phpcs:ignore WordPress.VIP.SlowDBQuery.slow_db_query_meta_key
160 'meta_key' => $this->textdomain,
161 'orderby' => 'meta_value_num',
162 ) );
163 }
164
165 return $user_query;
166 }
167
168 /**
169 * Defines the width of the column
170 *
171 * @author Konstantin Obenland
172 * @since 1.0 - 23.01.2012
173 * @access public
174 *
175 * @return void
176 */
177 public function column_style() {
178 ?>
179 <style type="text/css">
180 .column-wp-last-login { width: 12%; }
181 </style>
182 <?php
183 }
184
185 } // End of class Obenland_Wp_Last_Login.
186
187
188 new Obenland_Wp_Last_Login();
189
190 /**
191 * Sets a default meta value for all users.
192 *
193 * Allows sorting users by last login to work, even though some might not have
194 * recorded login time.
195 *
196 * @see https://wordpress.org/support/topic/wp-40-sorting-by-date-doesnt-work
197 */
198 function wpll_activate() {
199 $user_ids = get_users( array(
200 'blog_id' => '',
201 'fields' => 'ID',
202 ) );
203
204 foreach ( $user_ids as $user_id ) {
205 update_user_meta( $user_id, 'wp-last-login', 0 );
206 }
207 }
208 register_activation_hook( __FILE__, 'wpll_activate' );
209