{ return false; } return true; } /** * Get the remote notification object. * * @since 1.3.0 * * @param array $notification The notification data array. * * @return object|false */ protected function get_remote_notification( $notification ) { $content = get_transient( 'rn_last_notification_' . $notification['notice_id'] ); if ( false === $content ) { add_option( 'rdn_fetch_' . $notification['notice_id'], 'fetch' ); } return $content; } /** * Maybe get a notification from the remote server. * * @since 1.2.0 * * @param array $notification The notification data array. * * @return string|WP_Error */ protected function remote_get_notification( $notification ) { /* Query the server. */ $response = wp_remote_get( $this->build_query_url( $notification['server_url'], $this->get_payload( $notification ) ), array( 'timeout' => apply_filters( 'rn_http_request_timeout', 5 ) ) ); /* If we have a WP_Error object we abort. */ if ( is_wp_error( $response ) ) { return $response; } if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { $response_code = wp_remote_retrieve_response_code( $response ); // translators: Invalid code. $message = sprintf( esc_html__( 'The server response was invalid (code %s)', 'loginpress' ), $response_code ?: 'unknown' ); return new WP_Error( 'invalid_response', $message ); } $body = wp_remote_retrieve_body( $response ); if ( empty( $body ) ) { return new WP_Error( 'empty_response', esc_html__( 'The server response is empty', 'loginpress' ) ); } $body = json_decode( $body ); if ( is_null( $body ) ) { return new WP_Error( 'json_decode_error', esc_html__( 'Cannot decode the response content', 'loginpress' ) ); } set_transient( 'rn_last_notification_' . $notification['notice_id'], $body, $notification['cache_lifetime'] * 60 * 60 ); delete_option( 'rdn_fetch_' . $notification['notice_id'] ); if ( $this->is_notification_error( $body ) ) { /** @phpstan-ignore-next-line */ return new WP_Error( 'notification_error', $this->get_notification_error_message( $body ) ); } return $body; } /** * Check if the notification returned by the server is an error. * * @since 1.2.0 * * @param object $notification Notification returned. * * @return bool */ protected function is_notification_error( $notification ) { if ( false === $this->get_notification_error_message( $notification ) ) { return false; } return true; } /** * Get the error message returned by the remote server. * * @since 1.2.0 * * @param object $notification Notification returned. * * @return bool|string */ protected function get_notification_error_message( $notification ) { /** @phpstan-ignore-next-line */ if ( ! is_object( $notification ) ) { return false; } if ( ! isset( $notification->error ) ) { return false; } return sanitize_text_field( $notification->error ); } } }