Automattic / Automattic/jetpack

API v1.1/sites/$_blog_id/posts/new throws notices

Open
#12,183 4 comments 0 reactions 0 assignees View on GitHub
[Feature] WPCOM API [Pri] Normal [Status] Stale Bug
Dominant language
PHP
Stars
1.8k
Forks
898
Avg merge
1d 18h
Merged PRs (30d)
774

Description

Using https://github.com/Automattic/jetpack/blob/master/json-endpoints/class.wpcom-json-api-post-endpoint.php leaves following notices in Jetpack sites debug.log:

```
[27-Apr-2019 10:18:05 UTC] PHP Notice: Undefined index: type in /srv/users/user3043a48e/apps/user3043a48e/public/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php on line 242
[27-Apr-2019 10:18:05 UTC] PHP Notice: Undefined property: stdClass::$meta_key in /srv/users/user3043a48e/apps/user3043a48e/public/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php on line 815
[27-Apr-2019 10:18:05 UTC] PHP Notice: Undefined property: stdClass::$meta_key in /srv/users/user3043a48e/apps/user3043a48e/public/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php on line 816
[27-Apr-2019 10:18:05 UTC] PHP Notice: Undefined variable: post in /srv/users/user3043a48e/apps/user3043a48e/public/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php on line 881
[27-Apr-2019 10:18:05 UTC] PHP Notice: Trying to get property 'post_status' of non-object in /srv/users/user3043a48e/apps/user3043a48e/public/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php on line 881
```

This was discovered by @jeherve in the https://github.com/Automattic/jetpack/pull/9802

#### Steps to reproduce the issue

1. Create a fresh WP site with Jurassic Ninja
2. Connect to WPCOM
3. SSH into JN site, turn on WP_DEBUG_LOG
4. Go to your sandbox on WPCOM, use the following script to use the API endpoint:
```
'test',
'content' => 'test description',
'metadata' => array(
array(
'key' => 'test',
'value' => 'test 2'
)
),
);

$token = Jetpack_Data::get_access_token_by_blog_id_user_id( $_blog_id, wpcom_get_blog_owner( $_blog_id ) );
$args = array(
'url' => $url,
'blog_id' => $_blog_id,
'jetpack_token' => $token,
'method' => 'GET',
);
$args['method'] = 'POST';
$args['body'] = json_encode( $payload );

print_r( Jetpack_Client::json_api_request( $args ) );
```
5. Execute the script by `php ./script.php`
6. SSH/FTP into your JN site, see debug.log

See the attached notices

#### What I expected
No notices

#### What happened instead

A bunch of notices.

Contributor guide

Open the contributing guide

Research direction

Start with json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php at lines 242, 815-816, and 881, then reproduce the POST request using the PHP script and metadata payload in the issue. Done means the /sites/{site_id}/posts/new request completes without the reported PHP notices in WP_DEBUG_LOG.

Written by the indexing model from the issue text.

Assessment

Tech stack
php, wordpress
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.