-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.php
51 lines (44 loc) · 2.36 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/*
Plugin Name: YOURLS API Action List Extended
Plugin URI: http://github.com/Codeinwp/yourls-api-list-extended
Description: YOURLS API List action with sorting, pagination, total count, and field selection
Version: 1.0.0
Author: Hardeep Asrani
Author URI: https://themeisle.com
*/
// No direct call.
if ( ! defined( 'YOURLS_ABSPATH' ) ) {
die();
}
// Define custom action "list".
yourls_add_filter( 'api_action_list', 'ti_list_function' );
// List Function with Sorting, Pagination, Total Count, and Field Selection.
function ti_list_function() {
$table = YOURLS_DB_TABLE_URL;
// Set default values for sorting, offset, and per page limit
$sort_by = isset( $_REQUEST['sortby'] ) && in_array( $_REQUEST['sortby'], [ 'keyword', 'url', 'title', 'ip', 'timestamp', 'click' ] ) ? $_REQUEST['sortby'] : 'timestamp';
$sort_order = isset( $_REQUEST['sortorder'] ) && in_array( strtoupper( $_REQUEST['sortorder'] ), [ 'ASC', 'DESC' ] ) ? strtoupper( $_REQUEST['sortorder'] ) : 'DESC';
$offset = isset( $_REQUEST['offset'] ) && is_numeric( $_REQUEST['offset'] ) ? (int)$_REQUEST['offset'] : 0;
$perpage = isset( $_REQUEST['perpage'] ) && is_numeric( $_REQUEST['perpage'] ) ? (int)$_REQUEST['perpage'] : 50;
$fields = isset( $_REQUEST['fields'] ) && is_array( $_REQUEST['fields'] ) ? $_REQUEST['fields'] : [ '*' ];
$query = isset( $_REQUEST['query'] ) ? $_REQUEST['query'] : '';
// Validate fields to ensure the query won't break if an invalid field is requested.
$valid_fields = [ 'keyword', 'url', 'title', 'timestamp', 'ip', 'clicks' ];
$selected_fields = array_intersect( $fields, $valid_fields );
$selected_fields = ! empty( $selected_fields ) ? implode( ',', $selected_fields ) : '*';
// Get total number of links.
$total = yourls_get_db()->fetchValue( "SELECT COUNT(*) FROM `$table`" );
$where = $query ? "WHERE `keyword` LIKE '%$query%'" : ''; // If you have millions of links, this query can be slow. You can replace LIKE with = if you want exact match.
$query = "SELECT $selected_fields FROM `$table` $where ORDER BY `$sort_by` $sort_order LIMIT $offset, $perpage";
$results = yourls_get_db()->fetchObjects( $query );
// Return the response with results and total count.
return array(
'statusCode' => 200,
'message' => 'success',
'result' => $results,
'total' => $total,
'offset' => $offset,
'perpage' => $perpage,
);
}