PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
← All changes | includes/classes/ElasticPressIo.php +70 -18 4.5.25.3.5 View file →
@@ -20,9 +20,9 @@
20 20 class ElasticPressIo {
21 21 /**
22 22 * Name of the transient that stores EP.io messages
23 23 */
24 - const MESSAGES_TRANSIENT_NAME = 'ep_elasticpress_io_messages';
24 + const STATUS_TRANSIENT_NAME = 'ep_elasticpress_io_status';
25 25
26 26 /**
27 27 * Return singleton instance of class
28 28 *
@@ -28,44 +28,96 @@
28 28 *
29 29 * @return object
30 30 */
31 31 public static function factory() {
32 - static $instance = false;
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 + );
33 37
34 - if ( ! $instance ) {
35 - $instance = new self();
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;
36 54 }
37 55
38 - return $instance;
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;
39 77 }
40 78
41 79 /**
42 80 * Get messages from ElasticPress.io.
43 81 *
44 - * @param bool $skip_cache Whether to fetch the API or use the cached messages. Defaults to false, i.e., use cache.
82 + * @param bool $skip_cache Whether to use cached messages or not. Defaults to false, i.e., use cache.
45 83 * @return array ElasticPress.io messages.
46 84 */
47 - public function get_endpoint_messages( $skip_cache = false ) : array {
85 + public function get_endpoint_messages( $skip_cache = false ): array {
48 86 if ( ! Utils\is_epio() ) {
49 87 return [];
50 88 }
51 89
52 - $transient = 'ep_elasticpress_io_messages';
53 - $messages = get_transient( $transient );
54 - if ( ! $skip_cache && false !== $messages ) {
55 - return $messages;
56 - }
90 + $endpoint_status = $this->get_endpoint_status( $skip_cache );
57 91
58 - $response = \ElasticPress\Elasticsearch::factory()->remote_request( 'endpoint-messages' );
92 + return $endpoint_status['messages'] ?? [];
93 + }
59 94
60 - $response_code = wp_remote_retrieve_response_code( $response );
61 - if ( is_wp_error( $response ) || 200 !== $response_code ) {
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() ) {
62 104 return [];
63 105 }
64 106
65 - $messages = (array) json_decode( wp_remote_retrieve_body( $response ), true );
107 + $endpoint_status = $this->get_endpoint_status( $skip_cache );
66 108
67 - set_transient( $transient, $messages, HOUR_IN_SECONDS );
109 + return $endpoint_status['avaiableServices'] ?? [];
110 + }
68 111
69 - return $messages;
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 ] );
70 122 }
71 123 }