false, 'error' => __( 'The plugin is not connected to the WinnyPlus platform.', 'winnyplus' ) ); } $connection = WinnyPlus_Connect_Connection::get(); $token = WinnyPlus_Connect_Connection::get_token(); if ( empty( $token ) ) { return array( 'success' => false, 'error' => __( 'Invalid connection token.', 'winnyplus' ) ); } $url = trailingslashit( $connection['site_url'] ) . 'v1/integrations/installations/' . rawurlencode( $connection['installation_id'] ) . '/events'; $body = array( 'type' => $type, 'external_event_id' => $external_event_id, 'occurred_at' => gmdate( 'Y-m-d\TH:i:s\Z' ), 'data' => $data, ); $response = wp_remote_post( $url, array( 'timeout' => 20, 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $token, ), 'body' => wp_json_encode( $body ), ) ); if ( is_wp_error( $response ) ) { return array( 'success' => false, 'error' => $response->get_error_message() ); } $code = wp_remote_retrieve_response_code( $response ); $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); $success = ( $code >= 200 && $code < 300 ); // The home shows when something last reached WinnyPlus (one small option, written at most once a minute) if ( $success && time() - (int) get_option( 'winnyplus_connect_last_sent_at', 0 ) >= MINUTE_IN_SECONDS ) { update_option( 'winnyplus_connect_last_sent_at', time(), false ); } return array( 'success' => $success, 'error' => $success ? '' : ( ! empty( $response_body['message'] ) ? $response_body['message'] : __( 'Unknown failure from the WinnyPlus platform', 'winnyplus' ) ), ); } }