Plugin Directory

Changeset 2908629


Ignore:
Timestamp:
05/05/2023 04:11:25 PM (3 years ago)
Author:
deryck
Message:

Added last_login as a filter parameter in user endpoint.

Location:
user-toolkit/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • user-toolkit/trunk/README.txt

    r2876129 r2908629  
    44Tags: user profile, last login, disable user, registration date
    55Requires at least: 5.9.5
    6 Tested up to: 6.1
     6Tested up to: 6.2
    77Requires PHP: 7.3
    8 Stable tag: 1.2
     8Stable tag: 1.2.1
    99License: GPLv2 or later
    1010License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3131Full support for administration over SSL (if applicable).
    3232
     33** REST API Support **
     34
     35The field last_login is included as a result in endpoint wp/v2/users/.
     36Filtering the endpoint wp/v2/users/ using parameter last_login is also supported.
     37
    3338## USAGE
    3439
     
    52572. Disable all or any column clicking "Screen Options" on the right top corner of the screen.
    5358
     59** Retrieve Last Login info using REST API **
     60
     611. Get last_login field with ISO 8601 form on endpoint wp/v2/users/
     622. Filter using parameter last_login using the following options wp/v2/users/?last_login=FROM,[TO:optional] using ISO 8601 or Y-m-d format.
    5463
    5564## PRIVACY STATEMENT
     
    8493
    8594== Changelog ==
     95
     96= 1.2.1 =
     97* Added last_login as a filter parameter in user endpoint.
    8698
    8799= 1.2 =
     
    122134== Upgrade Notice ==
    123135
     136= 1.2.1 =
     137* Added last_login as a filter parameter in user endpoint.
     138
    124139= 1.2 =
    125140* Last Login date is now filterable in User List
  • user-toolkit/trunk/src/RestEndpoints.php

    r2876117 r2908629  
    1212        add_action( 'rest_api_init', [ $this, 'registerCanLoginField' ] );
    1313        add_action( 'rest_api_init', [ $this, 'registerLastLoginField' ] );
     14        add_filter( 'rest_user_collection_params', [ $this, 'registerParams' ] );
     15        add_filter( 'rest_user_query', [ $this, 'filterByParams' ], 10, 2 );
    1416    }
    1517
     
    7678    }
    7779
     80    public function registerParams( $params ) {
     81        $params['last_login']   = [
     82            'description' => 'Last login range',
     83            'type'        => 'string',
     84        ];
     85
     86        return $params;
     87    }
     88
     89    public function filterByParams( $prepared_args, \WP_REST_Request $request ) {
     90        $last_login = $request->get_param( 'last_login' );
     91        if ( ! empty( $last_login ) ) {
     92
     93            $last_login_range = explode( ',', $last_login );
     94
     95            if ( count( $last_login_range ) === 1 ) {
     96                $last_login_range[1] = date( 'Y-m-d\TH:i:s', strtotime( 'now' ) );
     97            }
     98
     99            $last_login_args = array_map( function ( $date ) {
     100                return strtotime( $date );
     101            }, $last_login_range );
     102
     103            $prepared_args['meta_query'] = [
     104                [
     105                    'key'     => 'last_login',
     106                    'value'   => $last_login_args,
     107                    'compare' => 'BETWEEN',
     108                ],
     109            ];
     110        }
     111
     112        return $prepared_args;
     113    }
     114
    78115
    79116}
  • user-toolkit/trunk/user-toolkit.php

    r2876117 r2908629  
    99 * Plugin URI:        https://deryckoe.com/user-toolkit
    1010 * Description:       The missing user tools and activity data that you need and don't have by default.
    11  * Version:           1.2
     11 * Version:           1.2.1
    1212 * Author:            Deryck Oñate
    1313 * Author URI:        http://deryckoe.com
Note: See TracChangeset for help on using the changeset viewer.