-
Notifications
You must be signed in to change notification settings - Fork 2
/
backbone-post-listing.php
91 lines (80 loc) · 2.97 KB
/
backbone-post-listing.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
<?php
/*
Plugin Name: Backbone Post Listing
Description: A plugin that fetches posts and allows the title and publish status to be changed, using the WP REST API v2
Version: 1.0
Author: Brian Hogg
Author URI: http://brianhogg.com
License: GPL2
*/
class BackbonePostListing {
const VERSION = '1.0';
function __construct() {
add_action( 'admin_init', array( $this, 'init' ) );
add_action( 'admin_menu', array( $this, 'menu' ) );
}
function init() {
wp_register_script( 'backbone-example',
plugins_url( 'js/backbone-example.js', __FILE__ ),
array( 'wp-backbone', 'jquery-effects-highlight' ),
self::VERSION
);
}
function menu() {
$page_hook_suffix = add_submenu_page( 'edit.php', // The parent page of this submenu
__( 'Backbone Example', 'backbone-example' ), // The submenu title
__( 'Backbone Example', 'backbone-example' ), // The screen title
'manage_options', // The capability required for access to this submenu
'backbone-example-options', // The slug to use in the URL of the screen
array( $this, 'admin_page' ) // The function to call to display the screen
);
/*
* Use the retrieved $page_hook_suffix to hook the function that links our script.
* This hook invokes the function only on our plugin administration screen,
* see: http://codex.wordpress.org/Administration_Menus#Page_Hook_Suffix
*/
add_action( 'admin_print_scripts-' . $page_hook_suffix, array( $this, 'load_scripts' ) );
}
function get_json_api_url() {
return '/wp-json/wp/v2';
}
function load_scripts() {
// WP REST API feeds data in a certain format, build our seed data array to match
$posts = get_posts( array(
'posts_per_page' => 1000,
'post_status' => 'draft,publish',
'suppress_filters' => false, // run content/title through any filters
'orderby' => 'ID',
) );
$post_data = array();
foreach ( $posts as $post ) {
$post_data[] = array(
'id' => $post->ID,
'title' => array(
'rendered' => $post->post_title,
),
'status' => $post->post_status,
);
}
wp_localize_script(
'backbone-example',
'bbdata',
array(
'posts' => $post_data,
'api_url' => $this->get_json_api_url(),
'nonce' => wp_create_nonce( 'wp_rest' ),
)
);
wp_enqueue_script( 'backbone-example' );
wp_enqueue_style(
'backbone-demo',
plugins_url( 'css/style.css', __FILE__ ),
'',
self::VERSION
);
}
function admin_page() {
include( dirname( __FILE__ ) . '/include/admin.php' );
}
}
$GLOBALS['backbone_post_listing'] = new BackbonePostListing();