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

REST issue when activating Gutenberg on IIS #9376

Closed
caseyctg opened this issue Aug 27, 2018 · 14 comments
Closed

REST issue when activating Gutenberg on IIS #9376

caseyctg opened this issue Aug 27, 2018 · 14 comments
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@caseyctg
Copy link

Warning: Attempt to modify property 'data' of non-object in C:\inetpub\SERVER_FOLDER\wp-content\plugins\gutenberg\lib\rest-api.php on line 202

Warning: Attempt to modify property 'data' of non-object in C:\inetpub\SERVER_FOLDER\wp-content\plugins\gutenberg\lib\rest-api.php on line 203

Fatal error: Uncaught Error: Call to a member function get_data() on array in C:\inetpub\SERVER_FOLDER\wp-content\plugins\gutenberg\lib\rest-api.php:223 Stack trace: #0 C:\inetpub\SERVER_FOLDER\wp-includes\class-wp-hook.php(286): gutenberg_add_block_format_to_post_content(Array, Object(WP_Post), Object(WP_REST_Request)) #1 C:\inetpub\SERVER_FOLDER\wp-includes\plugin.php(203): WP_Hook->apply_filters(Array, Array) #2 C:\inetpub\SERVER_FOLDER\wp-includes\rest-api\endpoints\class-wp-rest-posts-controller.php(1609): apply_filters('rest_prepare_po...', Object(WP_REST_Response), Object(WP_Post), Object(WP_REST_Request)) #3 C:\inetpub\SERVER_FOLDER\wp-includes\rest-api\endpoints\class-wp-rest-posts-controller.php(465): WP_REST_Posts_Controller->prepare_item_for_response(Object(WP_Post), Object(WP_REST_Request)) #4 C:\inetpub\SERVER_FOLDER\wp-includes\rest-api\class-wp-rest-server.php(936): WP_REST_Posts_Controller->get_item(Object(WP_REST_Request)) #5 C:\inetpub\SERVER_FOLDER\wp-includes\rest-api.php(390): WP_REST_Server->dispatch(Object(WP_REST_Request)) #6 C:\inetpub\SERVER_FOLDER\w in C:\inetpub\SERVER_FOLDER\wp-content\plugins\gutenberg\lib\rest-api.php on line 223

@caseyctg
Copy link
Author

This also happens after trying to post.

@caseyctg
Copy link
Author

Note, pages ARE working with gutenberg. I just can't add posts. I'm wondering if this is an issue with the custom post types in the theme.

@caseyctg
Copy link
Author

Default theme works, so it must be something within my current theme. closing.

@caseyctg
Copy link
Author

BTW, removing this filter allowed Gutenberg to function:
add_filter( 'rest_prepare_post', 'get_all_posts', 10, 3 );

@caseyctg caseyctg reopened this Aug 27, 2018
@caseyctg
Copy link
Author

Reopening because this should work still. The fact that my theme uses this filter should not cause Gutenberg to break.

@caseyctg
Copy link
Author

function get_all_posts( $data, $post, $context ) {

//Featured Image
$featured_image_id = $data->data['featured_media']; // get featured image id
$featured_image_url = wp_get_attachment_image_src( $featured_image_id, 'original' ); // get url of the original size
if( $featured_image_url ) {
	$data->data['featured_image_url'] = $featured_image_url[0];
}

//tags
$tags = wp_get_post_tags($post->ID);
$data->data['tags'] = [];
$keyCount = 0;
foreach ($tags as $tag) {
	$data->data['tags'][$keyCount]['id'] = $tag->term_id;
    $data->data['tags'][$keyCount]['tag'] .= $tag->name;
	$data->data['tags'][$keyCount]['slug'] .= $tag->slug;
	$keyCount++;
}

//author
$author_name = get_author_name($data->data['author']); 
$data->data['authorname'] = $author_name;

return [
	'id'		=> $data->data['id'],
	'date'		=> $data->data['date'],
	'link'     	=> $data->data['link'],
	'title'    	=> $data->data['title']['rendered'],
	'content'	=> $data->data['content']['rendered'],
	'excerpt'	=> $data->data['excerpt']['rendered'],
	'featuredimage' => $data->data['featured_image_url'],
	'authorName'	=> $data->data['authorname'],
	'tags'		=>  $data->data['tags'] 
];

}

@caseyctg
Copy link
Author

It seems the issue is the fact that the data is an array and so get_data and get_links do not accept an array.

@caseyctg
Copy link
Author

Ok, so it seems returning an array in rest_prepare_posts is an issue. This works:

$data->data['myarr'] = [
	'id'		=> $data->data['id'],
	'date'		=> $data->data['date'],
	'link'     	=> $data->data['link'],
	'title'    	=> $data->data['title']['rendered'],
	'content'	=> $data->data['content']['rendered'],
	'excerpt'	=> $data->data['excerpt']['rendered'],
	'featuredimage' => $data->data['featured_image_url'],
	'authorName'	=> $data->data['authorname'],
	'tags'		=>  $data->data['tags'] 
];

return $data;

@designsimply designsimply added [Type] Help Request Help with setup, implementation, or "How do I?" questions. Needs Technical Feedback Needs testing from a developer perspective. labels Aug 27, 2018
@caseyctg
Copy link
Author

This accomplished it and allowed Gutenberg to function:

`function get_all_posts( $data, $post, $context ) {

//Featured Image
$featured_image_id = $data->data['featured_media']; // get featured image id
$featured_image_url = wp_get_attachment_image_src( $featured_image_id, 'original' ); // get url of the original size
if( $featured_image_url ) {
	$data->data['featuredimage'] = $featured_image_url[0];
}else{
	$data->data['featuredimage'] = '';
}

//tags
$tags = wp_get_post_tags($post->ID);
$data->data['tags'] = [];
$keyCount = 0;
foreach ($tags as $tag) {
	$data->data['tags'][$keyCount]['id'] = $tag->term_id;
    $data->data['tags'][$keyCount]['tag'] .= $tag->name;
	$data->data['tags'][$keyCount]['slug'] .= $tag->slug;
	$keyCount++;
}

//author
$author_name = get_author_name($data->data['author']); 
$data->data['authorName'] = $author_name;

//flatten out nested objects
$data->data['title'] = $data->data['title']['rendered'];
$data->data['content'] = $data->data['content']['rendered'];
$data->data['excerpt'] = $data->data['excerpt']['rendered'];
	
//filter out unneeded stuff
unset($data->data['date_gmt']);
unset($data->data['guid']);
unset($data->data['modified']);
unset($data->data['modified_gmt']);
unset($data->data['slug']);
unset($data->data['status']);
unset($data->data['type']);
unset($data->data['categories']);
unset($data->data['_links']);
unset($data->data['author']);
unset($data->data['featured_media']);
unset($data->data['comment_status']);
unset($data->data['ping_status']);
unset($data->data['sticky']);
unset($data->data['template']);
unset($data->data['format']);
unset($data->data['meta']);

/*
this is the needed format:
[
	'id'		=> $data->data['id'],
	'date'		=> $data->data['date'],
	'link'     	=> $data->data['link'],
	'title'    	=> $data->data['title']['rendered'],
	'content'	=> $data->data['content']['rendered'],
	'excerpt'	=> $data->data['excerpt']['rendered'],
	'featuredimage' => $data->data['featured_image_url'],
	'authorName'	=> $data->data['authorname'],
	'tags'		=>  $data->data['tags'] 
];
*/
return $data;

}`

@TimothyBJacobs
Copy link
Member

TimothyBJacobs commented Aug 27, 2018

You should not be changing the response format of core endpoints so drastically. This is not supported.

@caseyctg
Copy link
Author

Note, unset doesn't work on _links. Had to remove them manually. There has to be a better way.

$data->remove_link( 'collection' );
$data->remove_link( 'self' );
$data->remove_link( 'about' );
$data->remove_link( 'author' );
$data->remove_link( 'replies' );
$data->remove_link( 'version-history' );
$data->remove_link( 'https://api.w.org/featuredmedia' );
$data->remove_link( 'https://api.w.org/attachment' );
$data->remove_link( 'https://api.w.org/term' );
$data->remove_link( 'curies' );
$data->remove_link('predecessor-version');

@caseyctg
Copy link
Author

caseyctg commented Aug 27, 2018

@TimothyBJacobs what's the proper way to do this? Based on what you're saying, I assume I should be creating a custom route?

@TimothyBJacobs
Copy link
Member

Yes. You should create a custom route in your own namespace.

@caseyctg
Copy link
Author

@TimothyBJacobs Ok, thanks. I'll try that route.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

3 participants