-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPFL_last_page_changes.php
55 lines (52 loc) · 2.06 KB
/
EPFL_last_page_changes.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
52
53
54
55
<?php
/*
Plugin Name: EPFL last page changes
Description: Gives the name of the last user who modified a given page.
Version: 0.0.3
Author: Jérôme Cosandey
Author URI: https://github.com/saphirevert
*/
function getLastChange( $data ){
global $wpdb;
$url = $data->get_param( 'url' );
$postId = url_to_postid($url);
if ($postId === 0 && trailingslashit(get_site_url()) !== trailingslashit($url)) {
status_header(404, "This page does not exist");
return http_response_code();
} else {
$sql = $wpdb->prepare( "SELECT wp_users.user_login AS username, post_modified AS last_modified FROM `wp_posts`
LEFT JOIN wp_users ON wp_users.ID = wp_posts.post_author
WHERE post_parent=%d AND post_status!='publish' ORDER BY wp_posts.post_modified DESC LIMIT 1;",
array(
$postId,
));
$results = $wpdb->get_results( $sql );
return $results;
}
}
function getLastRevisions( $data ){
global $wpdb;
$name = $data->get_param( 'name' );
$name = $name ? $name : '';
$limit = $data->get_param( 'limit' );
$limit = $limit ? $limit : 5;
$sql = $wpdb->prepare( "SELECT wp_posts.post_title, wp_users.user_login AS username, post_modified AS last_modified FROM `wp_posts`
LEFT JOIN wp_users ON wp_users.ID = wp_posts.post_author
WHERE wp_posts.post_title LIKE %s ORDER BY wp_posts.post_modified DESC LIMIT %d;",
array(
'%' . $wpdb->esc_like($name) . '%',
$limit,
));
$results = $wpdb->get_results( $sql );
return $results;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'epfl/v1', '/lastchange', array(
'methods' => 'GET',
'callback' => 'getLastChange',
));
register_rest_route( 'epfl/v1', '/lastrevisions', array(
'methods' => 'GET',
'callback' => 'getLastRevisions',
));
});