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
elasticpress / includes / classes / ElasticPressIoTemplateManager.php

ElasticPressIoTemplateManager.php in ElasticPress 5.3.5, at includes/classes/ElasticPressIoTemplateManager.php

147 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ElasticPress.io Template Manager trait for search API interactions
4 *
5 * @package elasticpress
6 * @since 5.3.0
7 */
8
9 namespace ElasticPress;
10
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit; // Exit if accessed directly.
13 }
14
15 /**
16 * ElasticPress.io Template Manager trait.
17 *
18 * This trait provides common functionality for features that interact with
19 * ElasticPress.io search APIs, including search template management and API requests.
20 *
21 * @since 5.3.0
22 */
23 trait ElasticPressIoTemplateManager {
24
25 /**
26 * Get the endpoint for the search template.
27 *
28 * @return string Search template endpoint.
29 */
30 abstract public function get_template_endpoint(): string;
31
32 /**
33 * Generate and return a search template.
34 *
35 * @return string The search template as JSON.
36 */
37 abstract public function get_search_template(): string;
38
39 /**
40 * Get the feature slug.
41 *
42 * @return string Feature slug.
43 */
44 abstract public function get_feature_slug(): string;
45
46 /**
47 * Get the hook prefix
48 *
49 * @return string Hook prefix.
50 */
51 public function get_hook_prefix(): string {
52 return 'ep_' . str_replace( '-', '_', $this->get_feature_slug() );
53 }
54
55 /**
56 * Save the search template to ElasticPress.io.
57 *
58 * @return void
59 */
60 public function epio_save_search_template(): void {
61 $endpoint = $this->get_template_endpoint();
62 $template = $this->get_search_template();
63
64 Elasticsearch::factory()->remote_request(
65 $endpoint,
66 [
67 'blocking' => false,
68 'body' => $template,
69 'method' => 'PUT',
70 ]
71 );
72
73 /**
74 * Fires after the request is sent the search template API endpoint.
75 *
76 * @since 5.3.0
77 * @hook {hook_prefix}_template_saved
78 * @param {string} $template The search template (JSON).
79 */
80 do_action( $this->get_hook_prefix() . '_template_saved', $template );
81 }
82
83 /**
84 * Delete the search template from ElasticPress.io.
85 *
86 * @return void
87 */
88 public function epio_delete_search_template(): void {
89 $endpoint = $this->get_template_endpoint();
90
91 Elasticsearch::factory()->remote_request(
92 $endpoint,
93 [
94 'blocking' => false,
95 'method' => 'DELETE',
96 ]
97 );
98
99 /**
100 * Fires after the request is sent the search template API endpoint.
101 *
102 * @since 5.3.0
103 * @hook {hook_prefix}_template_deleted
104 */
105 do_action( $this->get_hook_prefix() . '_template_deleted' );
106 }
107
108 /**
109 * Get the saved search template from ElasticPress.io.
110 *
111 * @return string|\WP_Error Search template if found, WP_Error on error.
112 */
113 public function epio_get_search_template() {
114 $endpoint = $this->get_template_endpoint();
115 $request = Elasticsearch::factory()->remote_request( $endpoint );
116
117 if ( is_wp_error( $request ) ) {
118 return $request;
119 }
120
121 $response = wp_remote_retrieve_body( $request );
122
123 return $response;
124 }
125
126 /**
127 * Handle feature activation/deactivation to save or delete templates.
128 *
129 * @param string $feature Feature slug
130 * @param array $settings Feature settings
131 * @param array $data Feature activation data
132 *
133 * @return void
134 */
135 public function after_update_feature( string $feature, array $settings, array $data ): void {
136 if ( $feature !== $this->get_feature_slug() ) {
137 return;
138 }
139
140 if ( true === $data['active'] ) {
141 $this->epio_save_search_template();
142 } else {
143 $this->epio_delete_search_template();
144 }
145 }
146 }
147