This repository has been archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
wp-rest-api-plugin-jquery-example.php
127 lines (107 loc) · 3.95 KB
/
wp-rest-api-plugin-jquery-example.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php /*
********************************************************************************
Plugin Name: REST API Plugin - JQuery Example
Plugin URI: https://github.com/coderkevin/wp-rest-api-plugin-jquery-example
Description: Example code for using the WordPress REST API in a plugin
Version: 0.1.0
Author: Kevin Killingsworth
Author URI: https://github.com/coderkevin
License: GPL-3.0+
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Requires at least: 4.4
Text Domain: rest-api-plugin-jquery-example
Domain Path: /languages/
*******************************************************************************/
defined( 'ABSPATH' ) or die( 'No direct access.' );
/**
* Main class.
*/
class REST_API_Plugin_JQuery_Example {
const VERSION = '0.1.0';
const TEXT_DOMAIN = 'rest-api-plugin-jquery-example';
public function __construct() {
add_action( 'plugins_loaded', array( $this, 'init' ), 0 );
}
/**
* Initialize localization and admin menu.
*/
public function init() {
$this->load_plugin_textdomain();
add_action( 'admin_menu', array( $this, 'attach_menu' ) );
}
/**
* Load Localization files.
*
* Note: first loaded translation overrides following.
*
* This look for:
* WP_LANG_DIR/rest-api-plugin-jquery-example/rest-api-plugin-jquery-example-LOCALE.mo
* WP_LANG_DIR/plugins/rest-api-plugin-jquery-example-LOCALE.mo
*/
public function load_plugin_textdomain() {
$domain = REST_API_Plugin_JQuery_Example::TEXT_DOMAIN;
$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
$mo_file = $domain . '-' . get_locale() . '.mo';
load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $mo_file );
load_plugin_textdomain( $domain, false, plugin_basename( dirname( __FILE__ ) ) . '/languages/' );
}
/**
* Attaches the admin menu we'll use for our plugin.
*/
public function attach_menu() {
add_menu_page(
__( 'REST API Plugin Example Using jQuery', 'rest-api-plugin-jquery-example' ),
__( 'REST API Example', 'rest-api-plugin-jquery-example' ),
'read',
'rest-api-plugin-jquery-example',
array( $this, 'output_page' ),
null,
null
);
}
/**
* Outputs the scripts and html for our page.
*/
public function output_page() {
wp_enqueue_script(
'rest-api-plugin-jquery-example-js',
plugins_url( 'assets/js/rest-example-jquery.js', __FILE__ ),
array( 'jquery' ),
REST_API_Plugin_JQuery_Example::VERSION,
true
);
wp_localize_script( 'rest-api-plugin-jquery-example-js', 'screen_data', array(
'api_root' => esc_url_raw( rest_url() ),
'api_nonce' => wp_create_nonce( 'wp_rest' ),
'i18n' => array(
'post' => __( 'Post', 'rest-api-plugin-jquery-example' ),
'author' => __( 'Author', 'rest-api-plugin-jquery-example' ),
'status' => __( 'Status', 'rest-api-plugin-jquery-example' ),
'publish' => __( 'Published', 'rest-api-plugin-jquery-example' ),
'future' => __( 'Scheduled', 'rest-api-plugin-jquery-example' ),
'draft' => __( 'Draft', 'rest-api-plugin-jquery-example' ),
'pending' => __( 'Pending Review', 'rest-api-plugin-jquery-example' ),
'private' => __( 'Private', 'rest-api-plugin-jquery-example' ),
'loading' => __( '(loading)', 'rest-api-plugin-jquery-example' ),
)
) );
?>
<div class="rest-api-example">
<h1><?php _e( 'REST API Plugin Example Using jQuery', 'rest-api-plugin-jquery-example' ) ?></h1>
<p><?php _e( 'This is the example page for using the REST API from a plugin using jQuery!', 'rest-api-plugin-jquery-example' ) ?></p>
<div id="post-search">
<form id="search-form" class="search">
<label>
<?php _e( 'Search Posts:', 'rest-api-plugin-jquery-example' ) ?>
<input id="search-box" type="search"></input>
</label>
<span id="search-message"></span>
</form>
<table id="post-table">
</table>
</div>
</div>
<?php
}
}
return new REST_API_Plugin_JQuery_Example();