| 1 |
<?php |
| 2 |
/** |
| 3 |
* ElasticPress.io report class |
| 4 |
* |
| 5 |
* @since 4.4.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\StatusReport; |
| 10 |
|
| 11 |
use ElasticPress\Indexables; |
| 12 |
use ElasticPress\Feature\InstantResults; |
| 13 |
use ElasticPress\Utils; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
/** |
| 18 |
* ElasticPressIo report class |
| 19 |
* |
| 20 |
* @package ElasticPress |
| 21 |
*/ |
| 22 |
class ElasticPressIo extends Report { |
| 23 |
|
| 24 |
/** |
| 25 |
* Return the report title |
| 26 |
* |
| 27 |
* @return string |
| 28 |
*/ |
| 29 |
public function get_title() : string { |
| 30 |
return __( 'ElasticPress.io', 'elasticpress' ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Return the report fields |
| 35 |
* |
| 36 |
* @return array |
| 37 |
*/ |
| 38 |
public function get_groups() : array { |
| 39 |
$groups = [ |
| 40 |
$this->get_autosuggest_group(), |
| 41 |
$this->get_instant_results_group(), |
| 42 |
$this->get_orders_search_group(), |
| 43 |
]; |
| 44 |
|
| 45 |
return array_values( array_filter( $groups ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Process the ElasticPress.io Autosuggest allowed parameters. |
| 50 |
* |
| 51 |
* @return array |
| 52 |
*/ |
| 53 |
protected function get_autosuggest_group() : array { |
| 54 |
$autosuggest_feature = \ElasticPress\Features::factory()->get_registered_feature( 'autosuggest' ); |
| 55 |
|
| 56 |
if ( ! $autosuggest_feature->is_active() ) { |
| 57 |
return []; |
| 58 |
} |
| 59 |
|
| 60 |
$title = __( 'Allowed Autosuggest Parameters', 'elasticpress' ); |
| 61 |
$allowed_params = $autosuggest_feature->epio_autosuggest_set_and_get(); |
| 62 |
|
| 63 |
if ( empty( $allowed_params ) ) { |
| 64 |
$fields['not_available'] = [ |
| 65 |
'label' => __( 'Allowed Autosuggest Parameters', 'elasticpress' ), |
| 66 |
'value' => __( 'Allowed autosuggest parameters info not available.', 'elasticpress' ), |
| 67 |
]; |
| 68 |
|
| 69 |
return [ |
| 70 |
'title' => $title, |
| 71 |
'fields' => $fields, |
| 72 |
]; |
| 73 |
} |
| 74 |
|
| 75 |
$allowed_params = wp_parse_args( |
| 76 |
$allowed_params, |
| 77 |
[ |
| 78 |
'postTypes' => [], |
| 79 |
'postStatus' => [], |
| 80 |
'searchFields' => [], |
| 81 |
'returnFields' => '', |
| 82 |
] |
| 83 |
); |
| 84 |
|
| 85 |
$fields = [ |
| 86 |
'Post Types' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['postTypes'] ), |
| 87 |
'Post Status' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['postStatus'] ), |
| 88 |
'Search Fields' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['searchFields'] ), |
| 89 |
'Returned Fields' => wp_sprintf( esc_html( var_export( $allowed_params['returnFields'], true ) ) ), // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
| 90 |
]; |
| 91 |
|
| 92 |
$formatted_fields = []; |
| 93 |
|
| 94 |
foreach ( $fields as $label => $value ) { |
| 95 |
$formatted_fields[ sanitize_title( $label ) ] = [ |
| 96 |
'label' => $label, |
| 97 |
'value' => $value, |
| 98 |
]; |
| 99 |
} |
| 100 |
|
| 101 |
return [ |
| 102 |
'title' => $title, |
| 103 |
'fields' => $formatted_fields, |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Process the ElasticPress.io Instant Results templates. |
| 109 |
* |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
protected function get_instant_results_group() : array { |
| 113 |
$instant_results_feature = \ElasticPress\Features::factory()->get_registered_feature( 'instant-results' ); |
| 114 |
|
| 115 |
if ( ! $instant_results_feature->is_active() ) { |
| 116 |
return []; |
| 117 |
} |
| 118 |
|
| 119 |
$title = __( 'Instant Results Template', 'elasticpress' ); |
| 120 |
$fields = []; |
| 121 |
|
| 122 |
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { |
| 123 |
$sites = Utils\get_sites( 0, true ); |
| 124 |
|
| 125 |
foreach ( $sites as $site ) { |
| 126 |
switch_to_blog( $site['blog_id'] ); |
| 127 |
|
| 128 |
$field = $this->get_instant_results_field(); |
| 129 |
$fields = array_merge( $fields, $field ); |
| 130 |
|
| 131 |
restore_current_blog(); |
| 132 |
} |
| 133 |
} else { |
| 134 |
$fields = $this->get_instant_results_field(); |
| 135 |
} |
| 136 |
|
| 137 |
return [ |
| 138 |
'title' => $title, |
| 139 |
'fields' => $fields, |
| 140 |
]; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Process the ElasticPress.io Orders Search templates. |
| 145 |
* |
| 146 |
* @since 4.5.0 |
| 147 |
* @return array |
| 148 |
*/ |
| 149 |
protected function get_orders_search_group() : array { |
| 150 |
$woocommerce_feature = \ElasticPress\Features::factory()->get_registered_feature( 'woocommerce' ); |
| 151 |
|
| 152 |
if ( ! $woocommerce_feature->is_active() ) { |
| 153 |
return []; |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! $woocommerce_feature->orders ) { |
| 157 |
return []; |
| 158 |
} |
| 159 |
|
| 160 |
$title = __( 'Orders Search Template', 'elasticpress' ); |
| 161 |
$fields = []; |
| 162 |
|
| 163 |
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { |
| 164 |
$sites = Utils\get_sites( 0, true ); |
| 165 |
|
| 166 |
foreach ( $sites as $site ) { |
| 167 |
switch_to_blog( $site['blog_id'] ); |
| 168 |
|
| 169 |
$field = $this->get_orders_search_field(); |
| 170 |
$fields = array_merge( $fields, $field ); |
| 171 |
|
| 172 |
restore_current_blog(); |
| 173 |
} |
| 174 |
} else { |
| 175 |
$fields = $this->get_orders_search_field(); |
| 176 |
} |
| 177 |
|
| 178 |
return [ |
| 179 |
'title' => $title, |
| 180 |
'fields' => $fields, |
| 181 |
]; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Process the ElasticPress.io Instant Results template. |
| 186 |
* |
| 187 |
* @return array |
| 188 |
*/ |
| 189 |
protected function get_instant_results_field() : array { |
| 190 |
$index = Indexables::factory()->get( 'post' )->get_index_name(); |
| 191 |
|
| 192 |
if ( ! $index ) { |
| 193 |
return []; |
| 194 |
} |
| 195 |
|
| 196 |
$feature = new InstantResults\InstantResults(); |
| 197 |
$template = $feature->epio_get_search_template(); |
| 198 |
|
| 199 |
if ( is_wp_error( $template ) ) { |
| 200 |
return [ |
| 201 |
$index => [ |
| 202 |
'label' => $index, |
| 203 |
'value' => $template->get_error_message(), |
| 204 |
], |
| 205 |
]; |
| 206 |
} |
| 207 |
|
| 208 |
return [ |
| 209 |
$index => [ |
| 210 |
'label' => $index, |
| 211 |
'value' => $template, |
| 212 |
], |
| 213 |
]; |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Return the report messages. |
| 218 |
* |
| 219 |
* @return array |
| 220 |
* @since 4.5.0 |
| 221 |
*/ |
| 222 |
public function get_messages() : array { |
| 223 |
$messages = \ElasticPress\ElasticPressIo::factory()->get_endpoint_messages( true ); |
| 224 |
$messages = array_values( $messages ); |
| 225 |
|
| 226 |
return $messages; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Process the ElasticPress.io Orders Search template. |
| 231 |
* |
| 232 |
* @since 4.5.0 |
| 233 |
* @return array |
| 234 |
*/ |
| 235 |
protected function get_orders_search_field() : array { |
| 236 |
$index = Indexables::factory()->get( 'post' )->get_index_name(); |
| 237 |
|
| 238 |
if ( ! $index ) { |
| 239 |
return []; |
| 240 |
} |
| 241 |
|
| 242 |
$woocommerce_feature = \ElasticPress\Features::factory()->get_registered_feature( 'woocommerce' ); |
| 243 |
$template = $woocommerce_feature->orders_autosuggest->get_search_template(); |
| 244 |
|
| 245 |
if ( is_wp_error( $template ) ) { |
| 246 |
return [ |
| 247 |
$index => [ |
| 248 |
'label' => $index, |
| 249 |
'value' => $template->get_error_message(), |
| 250 |
], |
| 251 |
]; |
| 252 |
} |
| 253 |
|
| 254 |
return [ |
| 255 |
$index => [ |
| 256 |
'label' => $index, |
| 257 |
'value' => $template, |
| 258 |
], |
| 259 |
]; |
| 260 |
} |
| 261 |
} |
| 262 |
|