| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class for interacting with ElasticPress.io |
| 4 |
* |
| 5 |
* @since 4.5.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress; |
| 10 |
|
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; // Exit if accessed directly. |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* ElasticPressIo class |
| 17 |
* |
| 18 |
* @package ElasticPress |
| 19 |
*/ |
| 20 |
class ElasticPressIo { |
| 21 |
/** |
| 22 |
* Name of the transient that stores EP.io messages |
| 23 |
*/ |
| 24 |
const MESSAGES_TRANSIENT_NAME = 'ep_elasticpress_io_messages'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Return singleton instance of class |
| 28 |
* |
| 29 |
* @return object |
| 30 |
*/ |
| 31 |
public static function factory() { |
| 32 |
static $instance = false; |
| 33 |
|
| 34 |
if ( ! $instance ) { |
| 35 |
$instance = new self(); |
| 36 |
} |
| 37 |
|
| 38 |
return $instance; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get messages from ElasticPress.io. |
| 43 |
* |
| 44 |
* @param bool $skip_cache Whether to fetch the API or use the cached messages. Defaults to false, i.e., use cache. |
| 45 |
* @return array ElasticPress.io messages. |
| 46 |
*/ |
| 47 |
public function get_endpoint_messages( $skip_cache = false ) : array { |
| 48 |
if ( ! Utils\is_epio() ) { |
| 49 |
return []; |
| 50 |
} |
| 51 |
|
| 52 |
$transient = 'ep_elasticpress_io_messages'; |
| 53 |
$messages = get_transient( $transient ); |
| 54 |
if ( ! $skip_cache && false !== $messages ) { |
| 55 |
return $messages; |
| 56 |
} |
| 57 |
|
| 58 |
$response = \ElasticPress\Elasticsearch::factory()->remote_request( 'endpoint-messages' ); |
| 59 |
|
| 60 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 61 |
if ( is_wp_error( $response ) || 200 !== $response_code ) { |
| 62 |
return []; |
| 63 |
} |
| 64 |
|
| 65 |
$messages = (array) json_decode( wp_remote_retrieve_body( $response ), true ); |
| 66 |
|
| 67 |
set_transient( $transient, $messages, HOUR_IN_SECONDS ); |
| 68 |
|
| 69 |
return $messages; |
| 70 |
} |
| 71 |
} |
| 72 |
|