PluginProbe
ElasticPress / 4.7.2
ElasticPress v4.7.2
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
elasticpress / includes / classes / ElasticPressIo.php

ElasticPressIo.php in ElasticPress 4.7.2, at includes/classes/ElasticPressIo.php

72 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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