Skip to content

Commit

Permalink
Merge pull request #187 from JayWood/pull_1
Browse files Browse the repository at this point in the history
Fix JSON decode and empty post_date
  • Loading branch information
mAAdhaTTah committed Oct 8, 2017
2 parents fcca4d2 + 76b36ed commit 4fbee8e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
6 changes: 6 additions & 0 deletions lib/import.php
Expand Up @@ -187,7 +187,13 @@ protected function blob_to_post( WordPress_GitHub_Sync_Blob $blob ) {
}

if ( array_key_exists( 'post_date', $meta ) ) {

if ( empty( $meta['post_date'] ) ) {
$meta['post_date'] = current_time( 'mysql' );
}

$args['post_date'] = $meta['post_date'];

$args['post_date_gmt'] = get_gmt_from_date( $meta['post_date'] );
unset( $meta['post_date'] );
}
Expand Down
42 changes: 39 additions & 3 deletions lib/payload.php
Expand Up @@ -38,10 +38,10 @@ class WordPress_GitHub_Sync_Payload {
*/
public function __construct( WordPress_GitHub_Sync $app, $raw_data ) {
$this->app = $app;
$this->data = json_decode( $raw_data );
$this->data = $this->get_payload_from_raw_response( $raw_data );

if ($this->data === null) {
switch (json_last_error()) {
if ( null === $this->data ) {
switch ( json_last_error() ) {
case JSON_ERROR_DEPTH:
$this->error = __( 'Maximum stack depth exceeded', 'wp-github-sync' );
break;
Expand All @@ -64,6 +64,42 @@ public function __construct( WordPress_GitHub_Sync $app, $raw_data ) {
}
}


/**
* Attempts to get the JSON decoded string.
*
* @param string $raw_data A raw string from php://input
*
* @see WordPress_GitHub_Sync_Request::read_raw_data()
*
* @return Object|null An object from JSON Decode or false if failure.
*
* @author JayWood <jjwood2004@gmail.com>
*/
private function get_payload_from_raw_response( $raw_data ) {

/*
* Try this the old way first, despite this not working in some servers. Assuming there's a flag
* at the Nginx or Apache level that auto-parses encoded strings.
*/
$maybe_decoded = json_decode( $raw_data );
if ( null !== $maybe_decoded ) {
return $maybe_decoded;
}

/*
* GitHub returns a raw string with Action and Payload keys by default, we have to parse that string
* using parse_str() and then grab the payload.
*/
parse_str( $raw_data, $decoded_data );

if ( ! isset( $decoded_data['payload'] ) ) {
return null;
}

return json_decode( $decoded_data['payload'] );
}

/**
* Returns whether payload should be imported.
*
Expand Down

0 comments on commit 4fbee8e

Please sign in to comment.