voceconnect/thermal-api

Issue Returning Posts with Galleries Embedded

ph0bos opened this issue · 2 comments

When attempting to perform a GET request on the following endpoints a "Slim Application Error" is thrown.

  • /wp_api/v1/posts
  • /wp_api/v1/posts/{postID}

This error is only thrown when a gallery embedded in one of the posts (the gallery contains a single image). I'm running a clean version of WordPress 3.8.1 with no plugins other than "Thermal API".

Slim Application Error
The application could not run because of the following error:

Details

Type: ErrorException
Code: 8
Message: Trying to get property of non-object
File: /vagrant/wordpress/wp-content/plugins/thermal-api/api/v1/controllers/Posts.php
Line: 497
Trace

#0 /vagrant/wordpress/wp-content/plugins/thermal-api/api/v1/controllers/Posts.php(497): Slim\Slim::handleErrors(8, 'Trying to get p...', '/vagrant/wordpr...', 497, Array)
#1 /vagrant/wordpress/wp-content/plugins/thermal-api/api/v1/controllers/Posts.php(456): Voce\Thermal\v1\Controllers\Posts::_get_gallery_attachments(17, 'ASC', 'post__in', Array, Array)
#2 /vagrant/wordpress/wp-content/plugins/thermal-api/api/v1/controllers/Posts.php(292): Voce\Thermal\v1\Controllers\Posts::_get_gallery_meta(Object(WP_Post), Array)
#3 [internal function]: Voce\Thermal\v1\Controllers\Posts::format(Object(WP_Post), 0, 'read')
#4 /vagrant/wordpress/wp-content/plugins/thermal-api/api/v1/controllers/Posts.php(35): array_walk(Array, Array, 'read')
#5 [internal function]: Voce\Thermal\v1\Controllers\Posts::find(Object(Slim\Slim))
#6 /vagrant/wordpress/wp-content/plugins/thermal-api/api/API_Base.php(87): call_user_func_array(Array, Array)
#7 [internal function]: Voce\Thermal\{closure}()
#8 /vagrant/wordpress/wp-content/plugins/thermal-api/vendor/slim/slim/Slim/Router.php(172): call_user_func_array(Object(Closure), Array)
#9 /vagrant/wordpress/wp-content/plugins/thermal-api/vendor/slim/slim/Slim/Slim.php(1222): Slim\Router->dispatch(Object(Slim\Route))
#10 /vagrant/wordpress/wp-content/plugins/thermal-api/vendor/slim/slim/Slim/Middleware/Flash.php(86): Slim\Slim->call()
#11 /vagrant/wordpress/wp-content/plugins/thermal-api/vendor/slim/slim/Slim/Middleware/MethodOverride.php(94): Slim\Middleware\Flash->call()
#12 /vagrant/wordpress/wp-content/plugins/thermal-api/vendor/slim/slim/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
#13 /vagrant/wordpress/wp-content/plugins/thermal-api/vendor/slim/slim/Slim/Slim.php(1174): Slim\Middleware\PrettyExceptions->call()
#14 /vagrant/wordpress/wp-content/plugins/thermal-api/dispatcher.php(57): Slim\Slim->run()
#15 [internal function]: Voce\Thermal\API_Dispatcher->dispatch_api('')
#16 /vagrant/wordpress/wp-includes/plugin.php(429): call_user_func_array(Array, Array)
#17 /vagrant/wordpress/wp-settings.php(368): do_action('wp_loaded')
#18 /vagrant/wordpress/wp-config.php(90): require_once('/vagrant/wordpr...')
#19 /vagrant/wordpress/wp-load.php(29): require_once('/vagrant/wordpr...')
#20 /vagrant/wordpress/wp-blog-header.php(12): require_once('/vagrant/wordpr...')
#21 /vagrant/wordpress/index.php(17): require('/vagrant/wordpr...')
#22 {main}

https://github.com/voceconnect/thermal-api/blob/master/api/v1/controllers/Posts.php#L497
For the lazy...

foreach ( $_attachments as $key => $val ) {
                $attachments[$val->ID] = $_attachments[$key];
            }

I'm not sure why an attachment wouldn't have an ID property, but I suppose wrapping it in a property_exists condition would suppress the error at least.

Indeed, i've just performed some debugging...

It would appear that the error is thrown because the the ID of the attachment is actually set on $val rather than $val->ID.

As such the below modification now prevents the error and results in the expected response;

            foreach ( $_attachments as $key => $val ) {
                $attachments[$val] = $_attachments[$key];
            }