-
Notifications
You must be signed in to change notification settings - Fork 3.7k
XML-RPC: Convert client-supplied dates before calling IXR_Date methods. #13517
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
Open
josephscott
wants to merge
1
commit into
WordPress:trunk
Choose a base branch
from
josephscott:xmlrpc/client-date-normalization
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -934,6 +934,30 @@ protected function _convert_date_gmt( $date_gmt, $date ) { | |
| return $this->_convert_date( $date_gmt ); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a client-supplied date value to an IXR_Date object. | ||
| * | ||
| * XML-RPC clients may send a date either as a dateTime.iso8601 value, which | ||
| * arrives as an IXR_Date object, or as a plain string. Any other type cannot | ||
| * be a date and results in an error. | ||
| * | ||
| * @since 7.2.0 | ||
| * | ||
| * @param mixed $date Client-supplied date value. | ||
| * @return IXR_Date|IXR_Error IXR_Date object on success, IXR_Error if the value is not a date. | ||
| */ | ||
| protected function _convert_client_date( $date ) { | ||
| if ( $date instanceof IXR_Date ) { | ||
| return $date; | ||
| } | ||
|
|
||
| if ( is_string( $date ) ) { | ||
| return $this->_convert_date( $date ); | ||
| } | ||
|
|
||
| return new IXR_Error( 400, __( 'Dates must be a dateTime.iso8601 value or a string.' ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Prepares post data for return in an XML-RPC object. | ||
| * | ||
|
|
@@ -1365,15 +1389,15 @@ public function wp_newPost( $args ) { | |
| } | ||
|
|
||
| // Convert the date field back to IXR form. | ||
| if ( isset( $content_struct['post_date'] ) && ! ( $content_struct['post_date'] instanceof IXR_Date ) ) { | ||
| if ( isset( $content_struct['post_date'] ) && is_string( $content_struct['post_date'] ) ) { | ||
| $content_struct['post_date'] = $this->_convert_date( $content_struct['post_date'] ); | ||
| } | ||
|
|
||
| /* | ||
| * Ignore the existing GMT date if it is empty or a non-GMT date was supplied in $content_struct, | ||
| * since _insert_post() will ignore the non-GMT date if the GMT date is set. | ||
| */ | ||
| if ( isset( $content_struct['post_date_gmt'] ) && ! ( $content_struct['post_date_gmt'] instanceof IXR_Date ) ) { | ||
| if ( isset( $content_struct['post_date_gmt'] ) && is_string( $content_struct['post_date_gmt'] ) ) { | ||
| if ( '0000-00-00 00:00:00' === $content_struct['post_date_gmt'] || isset( $content_struct['post_date'] ) ) { | ||
| unset( $content_struct['post_date_gmt'] ); | ||
| } else { | ||
|
|
@@ -1550,10 +1574,20 @@ protected function _insert_post( $user, $content_struct ) { | |
|
|
||
| // Do some timestamp voodoo. | ||
| if ( ! empty( $post_data['post_date_gmt'] ) ) { | ||
| $post_date_gmt = $this->_convert_client_date( $post_data['post_date_gmt'] ); | ||
| if ( $post_date_gmt instanceof IXR_Error ) { | ||
| return $post_date_gmt; | ||
| } | ||
|
|
||
| // We know this is supposed to be GMT, so we're going to slap that Z on there by force. | ||
| $date_created = rtrim( $post_data['post_date_gmt']->getIso(), 'Z' ) . 'Z'; | ||
| $date_created = rtrim( $post_date_gmt->getIso(), 'Z' ) . 'Z'; | ||
| } elseif ( ! empty( $post_data['post_date'] ) ) { | ||
| $date_created = $post_data['post_date']->getIso(); | ||
| $post_date = $this->_convert_client_date( $post_data['post_date'] ); | ||
| if ( $post_date instanceof IXR_Error ) { | ||
| return $post_date; | ||
| } | ||
|
|
||
| $date_created = $post_date->getIso(); | ||
| } | ||
|
|
||
| // Default to not flagging the post date to be edited unless it's intentional. | ||
|
|
@@ -1792,8 +1826,13 @@ public function wp_editPost( $args ) { | |
| } | ||
|
|
||
| if ( isset( $content_struct['if_not_modified_since'] ) ) { | ||
| $if_not_modified_since = $this->_convert_client_date( $content_struct['if_not_modified_since'] ); | ||
| if ( $if_not_modified_since instanceof IXR_Error ) { | ||
| return $if_not_modified_since; | ||
| } | ||
|
|
||
| // If the post has been modified since the date provided, return an error. | ||
| if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $content_struct['if_not_modified_since']->getTimestamp() ) { | ||
| if ( mysql2date( 'U', $post['post_modified_gmt'] ) > $if_not_modified_since->getTimestamp() ) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, what if |
||
| return new IXR_Error( 409, __( 'There is a revision of this post that is more recent.' ) ); | ||
| } | ||
| } | ||
|
|
@@ -3913,8 +3952,13 @@ public function wp_editComment( $args ) { | |
|
|
||
| // Do some timestamp voodoo. | ||
| if ( ! empty( $content_struct['date_created_gmt'] ) ) { | ||
| $date_created_gmt = $this->_convert_client_date( $content_struct['date_created_gmt'] ); | ||
| if ( $date_created_gmt instanceof IXR_Error ) { | ||
| return $date_created_gmt; | ||
| } | ||
|
|
||
| // We know this is supposed to be GMT, so we're going to slap that Z on there by force. | ||
| $date_created = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z'; | ||
| $date_created = rtrim( $date_created_gmt->getIso(), 'Z' ) . 'Z'; | ||
|
|
||
| $comment['comment_date'] = get_date_from_gmt( $date_created ); | ||
| $comment['comment_date_gmt'] = iso8601_to_datetime( $date_created, 'gmt' ); | ||
|
|
@@ -5671,10 +5715,20 @@ public function mw_newPost( $args ) { | |
|
|
||
| // Do some timestamp voodoo. | ||
| if ( ! empty( $content_struct['date_created_gmt'] ) ) { | ||
| $date_created_gmt = $this->_convert_client_date( $content_struct['date_created_gmt'] ); | ||
| if ( $date_created_gmt instanceof IXR_Error ) { | ||
| return $date_created_gmt; | ||
| } | ||
|
|
||
| // We know this is supposed to be GMT, so we're going to slap that Z on there by force. | ||
| $date_created = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z'; | ||
| $date_created = rtrim( $date_created_gmt->getIso(), 'Z' ) . 'Z'; | ||
| } elseif ( ! empty( $content_struct['dateCreated'] ) ) { | ||
| $date_created = $content_struct['dateCreated']->getIso(); | ||
| $date_created_object = $this->_convert_client_date( $content_struct['dateCreated'] ); | ||
| if ( $date_created_object instanceof IXR_Error ) { | ||
| return $date_created_object; | ||
| } | ||
|
|
||
| $date_created = $date_created_object->getIso(); | ||
| } | ||
|
|
||
| $post_date = ''; | ||
|
|
@@ -6077,10 +6131,20 @@ public function mw_editPost( $args ) { | |
|
|
||
| // Do some timestamp voodoo. | ||
| if ( ! empty( $content_struct['date_created_gmt'] ) ) { | ||
| $date_created_gmt = $this->_convert_client_date( $content_struct['date_created_gmt'] ); | ||
| if ( $date_created_gmt instanceof IXR_Error ) { | ||
| return $date_created_gmt; | ||
| } | ||
|
|
||
| // We know this is supposed to be GMT, so we're going to slap that Z on there by force. | ||
| $date_created = rtrim( $content_struct['date_created_gmt']->getIso(), 'Z' ) . 'Z'; | ||
| $date_created = rtrim( $date_created_gmt->getIso(), 'Z' ) . 'Z'; | ||
| } elseif ( ! empty( $content_struct['dateCreated'] ) ) { | ||
| $date_created = $content_struct['dateCreated']->getIso(); | ||
| $date_created_object = $this->_convert_client_date( $content_struct['dateCreated'] ); | ||
| if ( $date_created_object instanceof IXR_Error ) { | ||
| return $date_created_object; | ||
| } | ||
|
|
||
| $date_created = $date_created_object->getIso(); | ||
| } | ||
|
|
||
| // Default to not flagging the post date to be edited unless it's intentional. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this warrants adding a type check for the
$content_struct. Something like this:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same goes for
wp_editPostbelow.