'; } add_action( 'restrict_manage_users', 'fourlogin_filter_links' ); /** * Get count of total users for current page. * @param string $made_by_fourlogin * @return number */ function fourlogin_count_users( $made_by_fourlogin = '' ){ if ( $made_by_fourlogin ){ $args = array( 'meta_key' => 'made_by_fourlogin', 'meta_value' => $made_by_fourlogin, 'meta_compare' => '=', 'count_total' => true, 'fields' => 'ID', ); // When this user do not logged in by 4login, 'made_by_fourlogin' is not defined in DB. } elseif ( 0 === $made_by_fourlogin ) { $args = array( 'meta_key' => 'made_by_fourlogin', 'meta_compare' => 'NOT EXISTS', 'count_total' => true, 'fields' => 'ID', ); } else { $args = array( 'count_total' => true, 'fields' => 'ID', ); } // Remove fourlogin_filter_users because this filter hook runs when you make the instance of WP_Users_Query remove_filter('pre_get_users', 'fourlogin_filter_users'); $fourlogin_count = new WP_User_Query( $args ); add_filter('pre_get_users', 'fourlogin_filter_users'); return $fourlogin_count -> total_users; } /** * Filter users by 4login information. * @param WP_User_Query $query * @return WP_User_Query $query */ function fourlogin_filter_users($query) { if (isset($_GET['fourlogininfo']) && isset($_GET['_wpnonce'])) { $nonce = sanitize_key(wp_unslash($_GET['_wpnonce'])); if (wp_verify_nonce($nonce, 'nonce_action')) { if ($_GET['fourlogininfo'] !== '0') { $query->set('meta_key', 'made_by_fourlogin'); $query->set('meta_value', sanitize_text_field(wp_unslash($_GET['fourlogininfo']))); $query->set('meta_compare', '='); } else if ($_GET['fourlogininfo'] === '0') { $query->set('meta_key', 'made_by_fourlogin'); $query->set('meta_compare', 'NOT EXISTS'); } } } return $query; } add_filter('pre_get_users', 'fourlogin_filter_users'); /** * Make the columns of "FourLogin info" and "Sent FourLogin Code" * * @param string $columns * @return string $columns */ function fourlogin_add_columns( $columns ) { $columns['made_by_fourlogin'] = __('4Login Information', '4login-for-secure-and-smart-access'); $columns['fourlogin_sent'] = __('Sent 4Login Code', '4login-for-secure-and-smart-access'); return $columns; } add_filter( 'manage_users_columns', 'fourlogin_add_columns' ); /** * Make the content of "FourLogin info" and "Sent FourLogin Code" * * @param string $output * @param string $column * @param int $user_id * @return Ambigous */ function fourlogin_user_info_column( $output, $column, $user_id ) { if ( 'made_by_fourlogin' == $column ){ $output = get_user_meta( $user_id, 'made_by_fourlogin', true); $output = fourlogin_get_text_fourlogininfo( $output ); } elseif ('fourlogin_sent'){ $output = get_user_meta( $user_id, 'fourlogin_sent_fourlogincode', true); if( $output ){ $output = esc_attr( $output ); } else { $output = ''; } } return $output; } add_filter( 'manage_users_custom_column', 'fourlogin_user_info_column', 10, 3 ); /** * Translate 'made_by_fourlogin' to display message. * * @param int||blank $made_by_fourlogin 1, 2, 3 or '' * @return string message for user_meta 'made_by_fourlogin' */ function fourlogin_get_text_fourlogininfo( $made_by_fourlogin ){ switch ( $made_by_fourlogin ){ case 1: case 2: return __( 'Made by 4Login', '4login-for-secure-and-smart-access' ); break; case 3: return __( 'Already logged in with 4Login(Existing User)', '4login-for-secure-and-smart-access' ); break; default: return __( 'Not yet logged in with 4Login', '4login-for-secure-and-smart-access' ); break; } } /** * Add bulk actions to send 4Login Code * @param array $bulk_actions * @return array $bulk_actions */ function fourlogin_mail_bulk_action( $bulk_actions ){ $bulk_actions['send_fourlogin_code'] = __('Send 4Login Code', '4login-for-secure-and-smart-access'); return $bulk_actions; } add_filter('bulk_actions-users', 'fourlogin_mail_bulk_action'); /** * Add features when you submit "Send 4Login Code" button. * @param string $redirect_to * @param string $doactions * @param array $user_ids * @return string $redirect_to */ function fourlogin_send_mail( $redirect_to, $doaction, $user_ids ) { if ($doaction === 'send_fourlogin_code') { $email_results = fourlogin_send_fourlogin_code($user_ids); if ($email_results) { $redirect_to = wp_nonce_url(add_query_arg('bulk_send_fourlogin_code', count($user_ids), $redirect_to), 'nonce_bulk_action'); } else { $redirect_to = wp_nonce_url(add_query_arg('bulk_send_fourlogin_code', 'error', $redirect_to), 'nonce_bulk_action'); } } return $redirect_to; } add_filter('handle_bulk_actions-users', 'fourlogin_send_mail', 10, 3); /** * * Send 4login Code for selected users. * @param array $user_ids */ function fourlogin_send_fourlogin_code( $user_ids ){ if ( empty( $user_ids ) ){ return; } $_users = get_users( array( 'include' => $user_ids, 'fields' => array( 'ID', 'user_email'), ) ); $fourlogin_mails = fourlogin_options_get( FOURLOGIN_OPTION_MAILS ); $subject = fourlogin_make_mail( $fourlogin_mails['subject'] ); $message = fourlogin_make_mail( $fourlogin_mails['message'] ); $result = array(); foreach( $_users as $_user ){ $result[$_user -> ID] = wp_mail( $_user -> user_email, $subject, $message ); if( $result[$_user -> ID] ){ update_user_meta( $_user -> ID, 'fourlogin_sent_fourlogincode', date_i18n( get_option('date_format').' '.get_option('time_format'), false, false ) ); } } return $result; } /** * display if you sent email or not * */ function fourlogin_bulk_action_result() { if(isset($_GET['_wpnonce'])) { $nonce = sanitize_key(wp_unslash($_GET['_wpnonce'])); if (wp_verify_nonce($nonce, 'nonce_bulk_action') && isset($_GET['bulk_send_fourlogin_code']) && $_GET['bulk_send_fourlogin_code'] === 'error') { ?>