Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/rest api reordering post #74

Merged
merged 11 commits into from
Oct 26, 2021
Merged

Conversation

ciprianimike
Copy link
Contributor

Description of the Change

The function ajax_simple_page_ordering() is the callback function of the AJAX call, it was also responsible for saving the position of the posts.

A refactor has been done, moving the sorting and posts updating part into a new page_ordering() function.

The REST endpoint has been added: wp-json/simplepageordering/v1/page_ordering/

The rest_page_ordering() function has been added as callback for the new REST endpoint and it calls the page_ordering() function.

page_ordering() is therefore the part of the code where the sorting of post takes place and this is now called via AJAX or REST.

Verification Process

after taking some of the code out of the ajax_simple_page_ordering () function, I made sure that the operation remained intact, verifying the parameters sent in the request before the refactor and replicating them after the refactor.

I then sent the same parameters through the new REST endpoint and verified that the reordering is consistent, the same parameters via AJAX or REST must lead to the usual sorting results.

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Applicable Issues

#67

Sorry, something went wrong.

jeffpaul and others added 3 commits October 11, 2021 15:31

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Bump WP tested-up-to version 5.8
@ciprianimike ciprianimike requested a review from jeffpaul October 12, 2021 05:21
@jeffpaul jeffpaul linked an issue Oct 12, 2021 that may be closed by this pull request
@jeffpaul jeffpaul requested review from a team and cadic and removed request for jeffpaul and a team October 12, 2021 17:05
@jeffpaul jeffpaul added this to the 2.4.0 milestone Oct 12, 2021

// check and make sure we have what we need
if ( empty( $post_id ) || ( ! isset( $previd ) && ! isset( $nextid ) ) ) {
wp_send_json_error();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REST Callback expected to return the result of the action instead of error processing or output inside the callback function. If an error happens, we need to return new WP_Error( ... )

Importantly, a REST API route’s callback should always return data; it shouldn’t attempt to send the response body itself.


self::page_ordering( $post_id, $previd, $nextid, $start, $excluded );

wp_send_json_error();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After successful processing, REST endpoint callback expected to return WP_REST_Response

$previd = empty( $previd ) ? false : (int) $previd;
$nextid = empty( $nextid ) ? false : (int) $nextid;
$start = empty( $start ) ? 1 : (int) $start;
$excluded = empty( $excluded ) ? array( $post_id ) : array_filter( (array) json_decode( $excluded ), 'intval' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
Copy link
Contributor

@cadic cadic Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this second validation really necessary after validating $_POST arguments in ajax_simple_page_ordering()?

$previd = empty( $_GET['previd'] ) ? false : (int) $_GET['previd'];
$nextid = empty( $_GET['nextid'] ) ? false : (int) $_GET['nextid'];
$start = empty( $_GET['start'] ) ? 1 : (int) $_GET['start'];
$excluded = empty( $_GET['excluded'] ) ? array( $_GET['id'] ) : array_filter( (array) json_decode( $_GET['excluded'] ), 'intval' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

REST arguments are passed to the callback as a single argument rest_page_ordering( WP_REST_Request $request ) and should be accessed as incoming array values, not from $_GET or $_POST.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, many PHPCS rulesets advise using filter_input() instead of accessing request globals directly.

- Added PHPCS via composer
- Added 10UP PHPCS ruleset
- Clean up
- page_ordering returns object or WP_Error
- added REST responses
@jeffpaul jeffpaul requested a review from cadic October 20, 2021 13:59
Copy link
Contributor

@cadic cadic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ciprianimike thank you for the update. There are couple minor changes left and maybe a merge conflict (version bumb and readme files)

readme.txt Outdated
@@ -3,7 +3,7 @@ Contributors: 10up, jakemgold, welcher, helen, thinkoomph
Donate link: http://10up.com/plugins/simple-page-ordering-wordpress/
Tags: order, re-order, ordering, pages, page, manage, menu_order, hierarchical, ajax, drag-and-drop, admin
Requires at least: 3.8
Tested up to: 5.8
Tested up to: 5.7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really downshift this from 5.8? Also includes README.md, CREDITS.md and CHANGELOG.md

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merged develop into the feature branch, resolved conflicts and added documentation

* @param int $previd The previous post ID.
* @param int $nextid The next post ID.
* @param int $start The start index.
* @param string $excluded Array of post IDs.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc type for $excluded seems to be array

@ciprianimike ciprianimike requested a review from cadic October 22, 2021 09:54
@ciprianimike
Copy link
Contributor Author

@cadic got it, added parameters required and default

@ciprianimike ciprianimike requested a review from cadic October 22, 2021 16:18
Copy link
Contributor

@cadic cadic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ciprianimike, it's a great new feature!

@jeffpaul jeffpaul merged commit 3213ca3 into develop Oct 26, 2021
@jeffpaul jeffpaul deleted the feature/REST-API-reordering-post branch October 26, 2021 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add REST API for reordering posts
4 participants