/
restful-hello-dolly.php
85 lines (74 loc) · 1.93 KB
/
restful-hello-dolly.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Plugin Name: Restful Hello Dolly
* Version: 0.5
* Description: A more RESTful Hello Dolly
* Author: Jake Spurlock
* Author URI: http://jakespurlock.com
* Plugin URI: http://jakespurlock.com
* Text Domain: restful-hello-dolly
* Domain Path: /languages
* @package Restful Hello Dolly
*/
/**
* RESTful Hello Dolly Output Class.
* Wrap all of the default output functions into one extended class.
*/
class RESTful_Hello_Dolly {
/**
* The one instance of RESTful Hello Dolly Routes.
*
* @var RESTful Hello Dolly Output
*/
private static $instance;
/**
* Instantiate or return the one RESTful Hello Dolly Routes instance.
*
* @return RESTful Hello Dolly Output
*/
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Construct the object.
*
* @return RESTful Hello Dolly Routes
*/
public function __construct() {
if ( function_exists( 'hello_dolly_get_lyric' ) ) {
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
}
/**
* Register the API routes
*
* @param array $routes The current JSON API routes.
* @return array The modified routes.
*/
public function register_routes() {
// Return a random Hello Dolly lyric
register_rest_route( 'restful-hello-dolly/v1', '/lyric', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'rest_get_lyric' ),
) );
}
/**
* Use the search endpoint to get a list of recent articles that were published
* @return array Published articles
*/
public function rest_get_lyric( $request ) {
return hello_dolly_get_lyric();
}
}
/**
* Wrapper function to return the one RESTful Hello Dolly Output instance.
* @return RESTful Hello Dolly Output
*/
function RESTful_Hello_Dolly() {
return RESTful_Hello_Dolly::instance();
}
// Kick off the class.
add_action( 'init', 'RESTful_Hello_Dolly' );