Skip to content

Commit

Permalink
Add functionality to reset page order via REST.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscoe committed Jan 29, 2023
1 parent 683f312 commit ad88c90
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
11 changes: 11 additions & 0 deletions assets/js/src/simple-page-ordering.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,14 @@ sortable_post_table.sortable({
}
}
});

jQuery( function() {
// set up click handler for order reset link
jQuery( '#simple-page-ordering-reset' ).on( 'click', function(e) {
e.preventDefault();
var post_type = jQuery( this ).data( 'posttype' );
if ( window.confirm( 'Are you sure you want to reset the ' + post_type + ' order?' ) ) {
jQuery.post( '/wp-json/simple-page-ordering/v1/reset_page_ordering/', { post_type: post_type }, function() { window.location.reload(); } );
}
} );
});
48 changes: 46 additions & 2 deletions simple-page-ordering.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,12 +164,13 @@ function () {
* Add page ordering help to the help tab
*/
public static function admin_head() {
$screen = get_current_screen();
$reset_order = sprintf( '<a href="#" id="simple-page-ordering-reset" data-posttype="%s">%s</a>', get_query_var('post_type'), __( 'Reset post order', 'simple-page-ordering' ) );
$screen = get_current_screen();
$screen->add_help_tab(
array(
'id' => 'simple_page_ordering_help_tab',
'title' => 'Simple Page Ordering',
'content' => '<p>' . __( 'To reposition an item, simply drag and drop the row by "clicking and holding" it anywhere (outside of the links and form controls) and moving it to its new position.', 'simple-page-ordering' ) . '</p>',
'content' => '<p>' . __( 'To reposition an item, simply drag and drop the row by "clicking and holding" it anywhere (outside of the links and form controls) and moving it to its new position.', 'simple-page-ordering' ) . '</p></p>' . $reset_order . '</p>',
)
);
}
Expand Down Expand Up @@ -492,6 +493,23 @@ public static function rest_api_init() {
],
]
);

register_rest_route(
'simple-page-ordering/v1',
'reset_page_ordering',
[
'methods' => 'POST',
'callback' => array( __CLASS__, 'rest_reset_page_ordering' ),
'permission_callback' => '__return_true',
'args' => [
'post_type' => [
'description' => __( 'Post type.', 'simple-page-ordering' ),
'required' => true,
'type' => 'string',
],
],
]
);
}

/**
Expand Down Expand Up @@ -525,6 +543,32 @@ public static function rest_page_ordering( WP_REST_Request $request ) {
)
);
}

/**
* Handle REST reset page sorting
*
* @param WP_REST_Request $request The REST request object.
*/
public static function rest_reset_page_ordering( WP_REST_Request $request ) {
global $wpdb;

$post_type = empty( $request->get_param( 'post_type' ) ) ? false : $request->get_param( 'post_type' );

// check we have a post type
if ( false === $post_type ) {
return new WP_Error( __( 'Missing mandatory parameters.', 'simple-page-ordering' ) );
}

// reset the order of all posts of given post type
$wpdb->update( 'wp_posts', array( 'menu_order' => 0 ), array( 'post_type' => $post_type ) );

return new WP_REST_Response(
array(
'status' => 200,
'response' => 'success',
)
);
}
}

Simple_Page_Ordering::get_instance();
Expand Down

0 comments on commit ad88c90

Please sign in to comment.