-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc2sh-api_endpoints.php
executable file
·65 lines (55 loc) · 1.75 KB
/
c2sh-api_endpoints.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
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Click 2 Share Plugin Api Endpoints
*
* Custom API endpoints to transfer data from Wordpress PHP to React JS
* - Shortlink from wp_get_shortlink() as custom field in Post API
* - Plugin settings from Wordpress DB
*
* @package Click2Share
*/
// Prevent direct access to the file
if (!defined('ABSPATH')) {
exit;
}
// Register custom shortlink and parmalink field for the post API.
add_action( 'rest_api_init', function () {
register_rest_field( 'post', 'c2sh_shortlink', array(
'get_callback' => 'c2sh_get_post_shortlink',
'update_callback' => null,
'schema' => array(
'description' => 'Shortlink of the post',
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
) );
register_rest_field( 'post', 'c2sh_permalink', array(
'get_callback' => 'c2sh_get_post_permalink',
'update_callback' => null,
'schema' => array(
'description' => 'Shortlink of the post',
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
) );
// Register a new REST API route for the plugin settings
register_rest_route('c2sh', '/settings', array(
'methods' => 'GET',
'callback' => 'c2sh_get_settings',
));
} );
// Return Shortlink
function c2sh_get_post_shortlink( $object, $field_name, $request ) {
$post_id = $object['id'];
return wp_get_shortlink( $post_id );
}
// Return Permalink
function c2sh_get_post_permalink( $object, $field_name, $request ) {
$post_id = $object['id'];
return get_permalink( $post_id );
}
// Return Plugin Settings
function c2sh_get_settings($request) {
$options = get_option('c2sh_settings');
return rest_ensure_response($options);
}