| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Imagify\Dependencies\WPMedia\Mixpanel; |
| 5 |
|
| 6 |
use Imagify_WPMedia_ConsumerStrategies_AbstractConsumer as WPMedia_ConsumerStrategies_AbstractConsumer; |
| 7 |
|
| 8 |
/** |
| 9 |
* Consumes messages and sends them to a host/endpoint using WordPress's HTTP API |
| 10 |
*/ |
| 11 |
class WPConsumer extends WPMedia_ConsumerStrategies_AbstractConsumer { |
| 12 |
|
| 13 |
/** |
| 14 |
* Number of seconds to allow the request to execute. |
| 15 |
* |
| 16 |
* Not configurable: analytics must never delay a response. Also the lowest value |
| 17 |
* WordPress honours, as it clamps the cURL timeout to a minimum of 1 second. |
| 18 |
* |
| 19 |
* @var int |
| 20 |
*/ |
| 21 |
const REQUEST_TIMEOUT = 1; |
| 22 |
|
| 23 |
/** |
| 24 |
* The host to connect to (e.g. api.mixpanel.com) |
| 25 |
* |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
protected $host; |
| 29 |
|
| 30 |
/** |
| 31 |
* The host-relative endpoint to write to (e.g. /engage) |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
protected $endpoint; |
| 36 |
|
| 37 |
/** |
| 38 |
* The protocol to use for the cURL connection |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
protected $protocol; |
| 43 |
|
| 44 |
/** |
| 45 |
* Creates a new WPConsumer and assigns properties from the $options array |
| 46 |
* |
| 47 |
* The request timeout and blocking behaviour are fixed, and cannot be set here. |
| 48 |
* |
| 49 |
* @param array{host: string, endpoint: string, use_ssl?: bool} $options Options for the consumer. |
| 50 |
*/ |
| 51 |
public function __construct( $options ) { |
| 52 |
parent::__construct( $options ); |
| 53 |
|
| 54 |
$this->host = $options['host']; |
| 55 |
$this->endpoint = $options['endpoint']; |
| 56 |
$this->protocol = isset( $options['use_ssl'] ) && ( true === $options['use_ssl'] ) ? 'https' : 'http'; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Send post request to the given host/endpoint using WordPress's HTTP API |
| 61 |
* |
| 62 |
* @param mixed[] $batch Batch of data to send to mixpanel. |
| 63 |
* |
| 64 |
* @return bool |
| 65 |
*/ |
| 66 |
public function persist( $batch ) { |
| 67 |
if ( count( $batch ) <= 0 ) { |
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
$url = $this->protocol . '://' . $this->host . $this->endpoint; |
| 72 |
$data = 'data=' . $this->_encode( $batch ); |
| 73 |
|
| 74 |
// Non-blocking still waits up to the timeout, it only discards the response: |
| 75 |
// the timeout is what bounds the cost of a degraded endpoint. |
| 76 |
$response = wp_remote_post( |
| 77 |
$url, |
| 78 |
[ |
| 79 |
'timeout' => self::REQUEST_TIMEOUT, |
| 80 |
'blocking' => false, |
| 81 |
'body' => $data, |
| 82 |
] |
| 83 |
); |
| 84 |
|
| 85 |
if ( is_wp_error( $response ) ) { |
| 86 |
$this->_handleError( $response->get_error_code(), $response->get_error_message() ); |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
* Report the batch as consumed even on failure. Returning false would make the |
| 91 |
* producer re-queue it and retry the flush up to 10 times at shutdown, once per |
| 92 |
* producer, multiplying the cost of an unreachable endpoint. |
| 93 |
* |
| 94 |
* @see \Imagify_WPMedia_Producers_MixpanelBaseProducer::__destruct() |
| 95 |
*/ |
| 96 |
return true; |
| 97 |
} |
| 98 |
} |
| 99 |
|