Skip to content
This repository was archived by the owner on Aug 15, 2022. It is now read-only.

Commit 666d2b7

Browse files
committed
Remove sticky, add status
Since "sticky" doesn't work right, I'm switching the example code to use Status instead.
1 parent f2bae81 commit 666d2b7

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

assets/js/rest-example-jquery.js

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
jQuery( function( $ ) {
33

4+
var POST_STATUS = [ 'publish', 'future', 'draft', 'pending', 'private' ];
45
var g_users = {};
56
var g_posts = [];
67
var g_searchTimeout = null;
@@ -42,7 +43,7 @@ jQuery( function( $ ) {
4243
var headerRow = $( '<tr/>' ).appendTo( table );
4344
$( '<th/>', { text: i18n.post } ).appendTo( headerRow );
4445
$( '<th/>', { text: i18n.author } ).appendTo( headerRow );
45-
$( '<th/>', { text: i18n.sticky } ).appendTo( headerRow );
46+
$( '<th/>', { text: i18n.status } ).appendTo( headerRow );
4647

4748
// For each post, create a row.
4849
posts.filter( function( post ) {
@@ -55,6 +56,7 @@ jQuery( function( $ ) {
5556

5657
// Creates a new TR element for a post.
5758
var generatePostRow = function( post, users ) {
59+
var i18n = screen_data.i18n;
5860
var row = $( '<tr/>', { 'key': post.id } );
5961

6062
// Post Title
@@ -72,19 +74,23 @@ jQuery( function( $ ) {
7274
html: author.name
7375
} ).appendTo( authorCell );
7476

75-
// Sticky
76-
var stickyCell = $( '<td/>' ).appendTo( row );
77-
var stickyInput = $( '<input/>', {
78-
'type': 'checkbox',
79-
} ).appendTo( stickyCell );
80-
stickyInput.prop( 'checked', post.sticky );
81-
82-
stickyInput.on( 'change', function( evt ) {
83-
stickyInput.prop( 'disabled', true );
84-
var props = {
85-
sticky: (! post.sticky)
86-
};
87-
updatePost( post, props );
77+
// Status
78+
var statusCell = $( '<td/>' ).appendTo( row );
79+
var statusSelect = $( '<select/>' ).appendTo( statusCell );
80+
POST_STATUS.filter( function( value ) {
81+
var opt = $( '<option/>', {
82+
'value': value,
83+
text: i18n[ value ],
84+
'selected': ( post.status == value )
85+
} ).appendTo( statusSelect );
86+
} );
87+
statusSelect.on( 'change', function( evt ) {
88+
var selected = $( 'option:selected', this );
89+
var newStatus = ( selected.length ? selected[0].value : null );
90+
if ( newStatus ) {
91+
statusSelect.prop( 'disabled', true ); // This will be regenerated
92+
updatePost( post, { status: newStatus } );
93+
}
8894
} );
8995

9096
return row;
@@ -157,7 +163,7 @@ jQuery( function( $ ) {
157163
regeneratePost( data );
158164
},
159165
error: function( req ) {
160-
console.error( 'error on sticky update' );
166+
console.error( 'error on post update' );
161167
console.error( req );
162168
// Reset the post to its previous state.
163169
regeneratePost( post );
@@ -179,16 +185,22 @@ jQuery( function( $ ) {
179185

180186
var text = $( '#search-box' ).val();
181187
var apiString = 'wp/v2/posts?' +
188+
'context=edit&per_page=100&' +
189+
'status=' + POST_STATUS.toString() + '&' +
182190
'search=' + encodeURIComponent( text );
183191

184192
$.ajax( {
185193
url: screen_data.api_root + apiString,
194+
beforeSend: function( req ) {
195+
req.setRequestHeader( 'X-WP-Nonce', screen_data.api_nonce );
196+
},
186197
success: function( data ) {
187198
g_posts = data;
188199
regenerateTable();
200+
// TODO: Fetch users here instead of in regenerateTable()
189201
},
190202
error: function( req ) {
191-
console.error( 'error on request' );
203+
console.error( 'error on posts request' );
192204
console.error( req );
193205
},
194206
complete: function() {
@@ -206,7 +218,7 @@ jQuery( function( $ ) {
206218
g_searchTimeout = window.setTimeout( function() {
207219
searchPosts();
208220
g_searchTimeout = null;
209-
}, 500 );
221+
}, 250 );
210222
} );
211223

212224
// Immediately search when the form is submitted (i.e. <ENTER> key )

wp-rest-api-plugin-jquery-example.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ public function output_page() {
6464
'i18n' => array(
6565
'post' => __( 'Post', $text_domain ),
6666
'author' => __( 'Author', $text_domain ),
67-
'sticky' => __( 'Sticky', $text_domain ),
67+
'status' => __( 'Status', $text_domain ),
68+
'publish' => __( 'Published', $text_domain ),
69+
'future' => __( 'Scheduled', $text_domain ),
70+
'draft' => __( 'Draft', $text_domain ),
71+
'pending' => __( 'Pending Review', $text_domain ),
72+
'private' => __( 'Private', $text_domain ),
6873
'loading' => __( '(loading)', $text_domain ),
6974
)
7075
) );

0 commit comments

Comments
 (0)