| 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 STATUS_TRANSIENT_NAME = 'ep_elasticpress_io_status'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Return singleton instance of class |
| 28 |
* |
| 29 |
* @return object |
| 30 |
*/ |
| 31 |
public static function factory() { |
| 32 |
_doing_it_wrong( |
| 33 |
__METHOD__, |
| 34 |
esc_html__( 'ElasticPressIo::factory() is deprecated. Use \ElasticPress\get_container()->get( \ElasticPress\ElasticPressIo::class ) instead.', 'elasticpress' ), |
| 35 |
'ElasticPress 5.3.0' |
| 36 |
); |
| 37 |
|
| 38 |
return \ElasticPress\get_container()->get( __CLASS__ ); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get status from ElasticPress.io. |
| 43 |
* |
| 44 |
* @since 5.3.0 |
| 45 |
* @param bool $skip_cache Whether to fetch the API or use the cached messages. Defaults to false, i.e., use cache. |
| 46 |
* @return array ElasticPress.io endpoint status. |
| 47 |
*/ |
| 48 |
public function get_endpoint_status( $skip_cache = false ): array { |
| 49 |
static $status = null; |
| 50 |
|
| 51 |
// Avoid sending multiple requests to the API in the same WP request. |
| 52 |
if ( null !== $status ) { |
| 53 |
return (array) $status; |
| 54 |
} |
| 55 |
|
| 56 |
if ( ! Utils\is_epio() ) { |
| 57 |
return []; |
| 58 |
} |
| 59 |
|
| 60 |
$status = get_transient( self::STATUS_TRANSIENT_NAME ); |
| 61 |
if ( ! $skip_cache && false !== $status ) { |
| 62 |
return $status; |
| 63 |
} |
| 64 |
|
| 65 |
$response = \ElasticPress\Elasticsearch::factory()->remote_request( 'endpoint-status' ); |
| 66 |
|
| 67 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 68 |
if ( is_wp_error( $response ) || 200 !== $response_code ) { |
| 69 |
return []; |
| 70 |
} |
| 71 |
|
| 72 |
$status = (array) json_decode( wp_remote_retrieve_body( $response ), true ); |
| 73 |
|
| 74 |
set_transient( self::STATUS_TRANSIENT_NAME, $status, HOUR_IN_SECONDS ); |
| 75 |
|
| 76 |
return $status; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Get messages from ElasticPress.io. |
| 81 |
* |
| 82 |
* @param bool $skip_cache Whether to use cached messages or not. Defaults to false, i.e., use cache. |
| 83 |
* @return array ElasticPress.io messages. |
| 84 |
*/ |
| 85 |
public function get_endpoint_messages( $skip_cache = false ): array { |
| 86 |
if ( ! Utils\is_epio() ) { |
| 87 |
return []; |
| 88 |
} |
| 89 |
|
| 90 |
$endpoint_status = $this->get_endpoint_status( $skip_cache ); |
| 91 |
|
| 92 |
return $endpoint_status['messages'] ?? []; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Get available services from ElasticPress.io. |
| 97 |
* |
| 98 |
* @since 5.3.0 |
| 99 |
* @param bool $skip_cache Whether to use cached available services or not. Defaults to false, i.e., use cache. |
| 100 |
* @return array ElasticPress.io available services. |
| 101 |
*/ |
| 102 |
public function get_endpoint_available_services( $skip_cache = false ): array { |
| 103 |
if ( ! Utils\is_epio() ) { |
| 104 |
return []; |
| 105 |
} |
| 106 |
|
| 107 |
$endpoint_status = $this->get_endpoint_status( $skip_cache ); |
| 108 |
|
| 109 |
return $endpoint_status['avaiableServices'] ?? []; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Returns true if the service is available. |
| 114 |
* |
| 115 |
* @param string $service_name The name of the service to check. |
| 116 |
* @param bool $skip_cache Whether to use cached available services or not. Defaults to false, i.e., use cache. |
| 117 |
* @return bool True if the service is available, false otherwise. |
| 118 |
*/ |
| 119 |
public function is_service_available( $service_name, $skip_cache = false ): bool { |
| 120 |
$available_services = $this->get_endpoint_available_services( $skip_cache ); |
| 121 |
return isset( $available_services[ $service_name ] ) && ! empty( $available_services[ $service_name ] ); |
| 122 |
} |
| 123 |
} |
| 124 |
|