Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Workaround for createFromFormat on PHP > 5.2

  • Loading branch information...
commit d7e732f26894c301e4e051bce6e3d9a406ebd1cc 1 parent 0fbe7f7
@osiux osiux authored
View
21 lib/class-wp-json-datetime.php
@@ -0,0 +1,21 @@
+<?php
+class WP_JSON_DateTime extends DateTime
+{
+ /**
+ * Workaround for DateTime::createFromFormat on PHP > 5.2
+ * Found on http://stackoverflow.com/a/17084893/717643
+ *
+ * @param string $format The format that the passed in string should be in.
+ * @param string $string String representing the time.
+ * @param DateTimeZone $timezone A DateTimeZone object representing the desired time zone.
+ * @return Datetime
+ */
+ public static function createFromFormat($format, $time, $timezone = null)
+ {
+ if ( method_exists('DateTime', 'createFromFormat') ) {
+ return parent::createFromFormat($format, $time, $timezone);
+ }
+
+ return new DateTime(date($format, strtotime($time)), $timezone);
+ }
+}
View
10 lib/class-wp-json-posts.php
@@ -299,7 +299,7 @@ function editPost( $id, $data, $_headers = array() ) {
// and C's asctime() format (and ignore invalid headers)
$formats = array( DateTime::RFC1123, DateTime::RFC1036, 'D M j H:i:s Y' );
foreach ( $formats as $format ) {
- $check = DateTime::createFromFormat( $format, $_headers['IF_UNMODIFIED_SINCE'] );
+ $check = WP_JSON_DateTime::createFromFormat( $format, $_headers['IF_UNMODIFIED_SINCE'] );
if ( $check !== false )
break;
@@ -533,12 +533,12 @@ protected function prepare_post( $post, $context = 'view' ) {
// Dates
$timezone = $this->server->get_timezone();
- $date = DateTime::createFromFormat( 'Y-m-d H:i:s', $post['post_date'], $timezone );
+ $date = WP_JSON_DateTime::createFromFormat( 'Y-m-d H:i:s', $post['post_date'], $timezone );
$post_fields['date'] = $date->format( 'c' );
$post_fields_extended['date_tz'] = $date->format( 'e' );
$post_fields_extended['date_gmt'] = date( 'c', strtotime( $post['post_date_gmt'] ) );
- $modified = DateTime::createFromFormat( 'Y-m-d H:i:s', $post['post_modified'], $timezone );
+ $modified = WP_JSON_DateTime::createFromFormat( 'Y-m-d H:i:s', $post['post_modified'], $timezone );
$post_fields['modified'] = $modified->format( 'c' );
$post_fields_extended['modified_tz'] = $modified->format( 'e' );
$post_fields_extended['modified_gmt'] = date( 'c', strtotime( $post['post_modified_gmt'] ) );
@@ -895,7 +895,7 @@ protected function parse_date( $date, $force_utc = false ) {
if ( strpos( $date, '.' ) !== false ) {
$date = preg_replace( '/\.\d+/', '', $date );
}
- $datetime = DateTime::createFromFormat( DateTime::RFC3339, $date );
+ $datetime = WP_JSON_DateTime::createFromFormat( DateTime::RFC3339, $date );
return $datetime;
}
@@ -997,7 +997,7 @@ protected function prepare_comment( $comment, $requested_fields = array( 'commen
// Date
$timezone = $this->server->get_timezone();
- $date = DateTime::createFromFormat( 'Y-m-d H:i:s', $comment->comment_date, $timezone );
+ $date = WP_JSON_DateTime::createFromFormat( 'Y-m-d H:i:s', $comment->comment_date, $timezone );
$fields['date'] = $date->format( 'c' );
$fields['date_tz'] = $date->format( 'e' );
$fields['date_gmt'] = date( 'c', strtotime( $comment->comment_date_gmt ) );
View
2  lib/class-wp-json-server.php
@@ -700,7 +700,7 @@ public function parse_date( $date, $force_utc = false ) {
if ( strpos( $date, '.' ) !== false ) {
$date = preg_replace( '/\.\d+/', '', $date );
}
- $datetime = DateTime::createFromFormat( DateTime::RFC3339, $date );
+ $datetime = WP_JSON_DateTime::createFromFormat( DateTime::RFC3339, $date );
return $datetime;
}
View
1  lib/wp-json.php
@@ -26,6 +26,7 @@
include_once(ABSPATH . 'wp-admin/includes/admin.php');
include_once(ABSPATH . WPINC . '/class-IXR.php');
include_once(ABSPATH . WPINC . '/class-wp-xmlrpc-server.php');
+include_once(ABSPATH . WPINC . '/class-wp-json-datetime.php');
include_once(ABSPATH . WPINC . '/class-wp-json-server.php');
// Allow for a plugin to insert a different class to handle requests.
View
2  plugin.php
@@ -9,6 +9,8 @@
*/
include_once( dirname( __FILE__ ) . '/lib/class-jsonserializable.php' );
+include_once( dirname( __FILE__ ) . '/lib/class-wp-json-datetime.php' );
+
include_once( dirname( __FILE__ ) . '/lib/class-wp-json-responsehandler.php' );
include_once( dirname( __FILE__ ) . '/lib/class-wp-json-responseinterface.php' );
include_once( dirname( __FILE__ ) . '/lib/class-wp-json-response.php' );
Please sign in to comment.
Something went wrong with that request. Please try again.