call_api(); if ( ! $api_response ) { // Can't connect. Not healthy. delete_transient( 'adminlabs_api_credentials_health' ); return false; } elseif ( $api_response['id'] !== self::$account_id ) { // Account ID in response differences from ours in databse. Not healthy. delete_transient( 'adminlabs_api_credentials_health' ); return false; } // API connection is healthy. Cache result five minutes. AdminLabs_Tools::set_transient( 'adminlabs_api_credentials_health', 'ok', 300 ); return true; } // end check_credentials_health public function call_api_delete( $endpoint = null ) { return self::$instance->call_api( $endpoint, null, false, 0, 'DELETE' ); } // end call_api_delete public function call_api_post( $endpoint = null, $data = null ) { return self::$instance->call_api( $endpoint, $data, false, 0, 'POST' ); } // end call_api_delete /** * Make call to API * * @since 1.0.0 * @param string $endpoint Which endpoint to call * @param mixed $data data that we send to API as a body, null if no data and make get request * @param boolean $cache true if response should be cached, defaults to false * @param integer $cache_lifetime Set cache lifetime, defaults to 15 minutes * @return mixed Payload from API response if call succesful, false if some error happened */ public function call_api( $endpoint = 'account', $data = null, $cache = false, $cache_lifetime = 900, $method = 'GET' ) { if ( 'POST' === $method || 'DELETE' === $method ) { $cache = false; } // Check that there's no response cached for this endpoint. if ( $cache ) { $data_from_cache = self::$instance->get_api_response_cache( $endpoint ); if ( false !== $data_from_cache ) { return $data_from_cache; } } // Arguments for API call. $args = array( 'headers' => array( 'Content-Type' => 'application/json', 'User-Agent' => 'adminlabs/wordpress;php', 'account-id' => self::$account_id, 'api-key' => self::$api_key, ), //'sslverify' => false, ); if ( 'POST' === $method ) { $args['body'] = json_encode( $data ); $response = wp_safe_remote_post( trailingslashit( self::$api_base_url ) . $endpoint, $args ); } else if ( 'DELETE' === $method ) { $args['method'] = 'DELETE'; $response = wp_safe_remote_request( trailingslashit( self::$api_base_url ) . $endpoint, $args ); } else { $response = wp_safe_remote_get( trailingslashit( self::$api_base_url ) . $endpoint, $args ); } // WP couldn't make the call for some reason, return false as a error. if ( is_wp_error( $response ) ) { return false; } // Test that API response code is 200-299 which proves succesfull request. $response_code = (int) $response['response']['code']; if ( ( $response_code <= 200 && $response_code > 300 ) || $response_code > 300 ) { return false; } // Get request response body and endcode the JSON data. $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); // Response was not valid JSON, return false as a error. if ( json_last_error() !== JSON_ERROR_NONE ) { return false; } // If response should be cached, cache it. if ( $cache ) { self::$instance->set_api_response_cache( $endpoint, $data, $cache_lifetime ); } if ( 'account' === $endpoint ) { add_option( 'adminlabs_timezone', $data['timeZone'] ); } return $data; } // end call_api /** * Get response for endpoint from cache * * @since 1.0.0 * @param string $endpoint For what endpoint we want response * @return mixed String if there's cached response, false if not */ private function get_api_response_cache( $endpoint ) { return get_transient( 'adminlabs_api_response_' . md5( $endpoint ) ); } // end get_api_response_cache /** * Save response for endpoint to cache * * @since 0.0.1-alpha * @param string $endpoint For what endpoint to cache response * @param string $data Response data to cache * @param integer $lifetime How long the response should be cached, defaults to 15 minutes */ private function set_api_response_cache( $endpoint = null, $data = null, $lifetime = 900 ) { if ( ! $endpoint || ! $data ) { return; } AdminLabs_Tools::set_transient( 'adminlabs_api_response_' . md5( $endpoint ), $data, $lifetime ); } // end set_api_response_cache } endif;