Looks like the line giving you the error is rather this one
$data = json_decode( $this->get_json(), true);
Like this (untested) it should work:
private function get_data() {
/**
* Ingests the data from get_json() then converts / decodes into an array.
*/
$json = $this->get_json();
if (is_wp_error($json)) {
return new WP_Error( 'invalid-data', esc_html__( 'Something is wrong. Did you enter the right username?', '_s' ) );
}
$data = json_decode( $json, true);
if ( is_wp_error( $data ) ) {
return new WP_Error( 'invalid-data', esc_html__( 'Something is wrong. Did you enter the right username?', '_s' ) );
} else {
return $data;
}
}
I’m actually not too familiar with WP_Error
, but aren’t errors supposed to be thrown instead of returned?
Why does it work like this? Well, the PHP error message tells you what goes wrong
json_decode() expects parameter 1 to be string, object given
json_decode( $this->get_json(),
^ should be a string but is an object
From your code we know, that it is an object of WP_Error
, because that is what you return from get_json()
.
I guess the important thing to take away: check for an error inside a variable/return before you use it.