int, 'body' => array|null, 'raw' => string ). */ public static function post_json( $url, $headers, $body, $timeout = 120 ) { $attempt = 0; while ( true ) { $response = wp_remote_post( $url, array( 'timeout' => $timeout, 'redirection' => 0, 'user-agent' => self::user_agent(), 'headers' => array_merge( array( 'Content-Type' => 'application/json', 'Accept' => 'application/json', ), $headers ), 'body' => wp_json_encode( $body ), 'data_format' => 'body', ) ); if ( is_wp_error( $response ) ) { $timed_out = false !== stripos( $response->get_error_message(), 'timed out' ); if ( ! $timed_out && $attempt < 1 ) { ++$attempt; usleep( 700000 ); continue; } return $response; } $code = (int) wp_remote_retrieve_response_code( $response ); if ( ( 429 === $code || $code >= 500 ) && $attempt < 1 ) { ++$attempt; usleep( 1500000 ); continue; } $raw = (string) wp_remote_retrieve_body( $response ); $decoded = json_decode( $raw, true ); return array( 'code' => $code, 'body' => is_array( $decoded ) ? $decoded : null, 'raw' => $raw, ); } } /** * A short, plain error text from a provider's answer. * * @since 1.0.0 * @param array $result What post_json() returned. * @return string */ public static function error_text( $result ) { $message = ''; $body = isset( $result['body'] ) ? $result['body'] : null; if ( is_array( $body ) ) { if ( isset( $body['error']['message'] ) && is_string( $body['error']['message'] ) ) { $message = $body['error']['message']; } elseif ( isset( $body['error'] ) && is_string( $body['error'] ) ) { $message = $body['error']; } elseif ( isset( $body['message'] ) && is_string( $body['message'] ) ) { $message = $body['message']; } elseif ( isset( $body[0]['error']['message'] ) && is_string( $body[0]['error']['message'] ) ) { $message = $body[0]['error']['message']; } } if ( '' === $message && isset( $result['raw'] ) ) { $message = wp_strip_all_tags( (string) $result['raw'] ); } return sanitize_text_field( mb_substr( $message, 0, 300 ) ); } /** * Remember the last failure so the settings screen can show it. * * @since 1.0.0 * @param string $message What went wrong. */ public static function remember_error( $message ) { set_transient( 'svabg_last_error', array( 'time' => time(), 'message' => sanitize_text_field( mb_substr( (string) $message, 0, 400 ) ), ), WEEK_IN_SECONDS ); } /** * Pull the JSON object out of a model's answer (handles code fences and chatter). * * @since 1.0.0 * @param string $text Answer. * @return array|null */ public static function parse_json( $text ) { $text = trim( (string) $text ); if ( '' === $text ) { return null; } $decoded = json_decode( $text, true ); if ( is_array( $decoded ) ) { return $decoded; } if ( preg_match( '/```(?:json)?\s*(\{.*\})\s*```/s', $text, $m ) ) { $decoded = json_decode( $m[1], true ); if ( is_array( $decoded ) ) { return $decoded; } } $start = strpos( $text, '{' ); $end = strrpos( $text, '}' ); if ( false !== $start && false !== $end && $end > $start ) { $decoded = json_decode( substr( $text, $start, $end - $start + 1 ), true ); if ( is_array( $decoded ) ) { return $decoded; } } return null; } }