Tribe__PUE__Plugin_Info::from_json( string $json )
Create a new instance of Tribe__PUE__Plugin_Info from JSON-encoded plugin info returned by an external update API.
Contents
Parameters
- $json
-
(string) (Required) Valid JSON string representing plugin info.
Return
(Tribe__PUE__Plugin_Info) New instance of Tribe<strong>PUE</strong>Plugin_Info, or NULL on error.
Source
File: src/Tribe/PUE/Plugin_Info.php
public static function from_json( $json ) {
$apiResponse = json_decode( $json );
// Get first item of the response array
if ( $apiResponse && ! empty( $apiResponse->results ) ) {
$apiResponse = current( $apiResponse->results );
}
if ( empty( $apiResponse ) || ! is_object( $apiResponse ) ) {
return null;
}
//Very, very basic validation.
$valid = ( isset( $apiResponse->name ) && ! empty( $apiResponse->name ) && isset( $apiResponse->version ) && ! empty( $apiResponse->version ) ) || ( isset( $apiResponse->api_invalid ) || isset( $apiResponse->no_api ) );
if ( ! $valid ) {
return null;
}
$info = new Tribe__PUE__Plugin_Info();
foreach ( get_object_vars( $apiResponse ) as $key => $value ) {
$key = str_replace( 'plugin_', '', $key ); // let's strip out the "plugin_" prefix we've added in plugin-updater-classes.
$info->$key = $value;
}
return $info;
}