| 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 |
return [ |
| 40 |
$this->get_autosuggest_group(), |
| 41 |
$this->get_instant_results_group(), |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Process the ElasticPress.io Autosuggest allowed parameters. |
| 47 |
* |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
protected function get_autosuggest_group() : array { |
| 51 |
$title = __( 'Allowed Autosuggest Parameters', 'elasticpress' ); |
| 52 |
|
| 53 |
$autosuggest_feature = \ElasticPress\Features::factory()->get_registered_feature( 'autosuggest' ); |
| 54 |
$allowed_params = $autosuggest_feature->epio_autosuggest_set_and_get(); |
| 55 |
|
| 56 |
if ( empty( $allowed_params ) ) { |
| 57 |
$fields['not_available'] = [ |
| 58 |
'label' => __( 'Allowed Autosuggest Parameters', 'elasticpress' ), |
| 59 |
'value' => __( 'Allowed autosuggest parameters info not available.', 'elasticpress' ), |
| 60 |
]; |
| 61 |
|
| 62 |
return [ |
| 63 |
[ |
| 64 |
'title' => $title, |
| 65 |
'fields' => $fields, |
| 66 |
], |
| 67 |
]; |
| 68 |
} |
| 69 |
|
| 70 |
$allowed_params = wp_parse_args( |
| 71 |
$allowed_params, |
| 72 |
[ |
| 73 |
'postTypes' => [], |
| 74 |
'postStatus' => [], |
| 75 |
'searchFields' => [], |
| 76 |
'returnFields' => '', |
| 77 |
] |
| 78 |
); |
| 79 |
|
| 80 |
$fields = [ |
| 81 |
'Post Types' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['postTypes'] ), |
| 82 |
'Post Status' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['postStatus'] ), |
| 83 |
'Search Fields' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['searchFields'] ), |
| 84 |
'Returned Fields' => wp_sprintf( esc_html( var_export( $allowed_params['returnFields'], true ) ) ), // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
| 85 |
]; |
| 86 |
|
| 87 |
$formatted_fields = []; |
| 88 |
|
| 89 |
foreach ( $fields as $label => $value ) { |
| 90 |
$formatted_fields[ sanitize_title( $label ) ] = [ |
| 91 |
'label' => $label, |
| 92 |
'value' => $value, |
| 93 |
]; |
| 94 |
} |
| 95 |
|
| 96 |
return [ |
| 97 |
'title' => $title, |
| 98 |
'fields' => $formatted_fields, |
| 99 |
]; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Process the ElasticPress.io Instant Results templates. |
| 104 |
* |
| 105 |
* @return array |
| 106 |
*/ |
| 107 |
protected function get_instant_results_group() : array { |
| 108 |
$title = __( 'Instant Results Template', 'elasticpress' ); |
| 109 |
$fields = []; |
| 110 |
|
| 111 |
if ( defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ) { |
| 112 |
$sites = Utils\get_sites(); |
| 113 |
|
| 114 |
foreach ( $sites as $site ) { |
| 115 |
if ( ! Utils\is_site_indexable( $site['blog_id'] ) ) { |
| 116 |
continue; |
| 117 |
} |
| 118 |
|
| 119 |
switch_to_blog( $site['blog_id'] ); |
| 120 |
|
| 121 |
$field = $this->get_instant_results_field(); |
| 122 |
$fields = array_merge( $fields, $field ); |
| 123 |
|
| 124 |
restore_current_blog(); |
| 125 |
} |
| 126 |
} else { |
| 127 |
$fields = $this->get_instant_results_field(); |
| 128 |
} |
| 129 |
|
| 130 |
return [ |
| 131 |
'title' => $title, |
| 132 |
'fields' => $fields, |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Process the ElasticPress.io Instant Results template. |
| 138 |
* |
| 139 |
* @return array|null |
| 140 |
*/ |
| 141 |
protected function get_instant_results_field() : array { |
| 142 |
$index = Indexables::factory()->get( 'post' )->get_index_name(); |
| 143 |
|
| 144 |
if ( ! $index ) { |
| 145 |
return []; |
| 146 |
} |
| 147 |
|
| 148 |
$feature = new InstantResults\InstantResults(); |
| 149 |
$template = $feature->epio_get_search_template(); |
| 150 |
|
| 151 |
if ( is_wp_error( $template ) ) { |
| 152 |
return [ |
| 153 |
$index => [ |
| 154 |
'label' => $index, |
| 155 |
'value' => $template->get_error_message(), |
| 156 |
], |
| 157 |
]; |
| 158 |
} |
| 159 |
|
| 160 |
return [ |
| 161 |
$index => [ |
| 162 |
'label' => $index, |
| 163 |
'value' => $template, |
| 164 |
], |
| 165 |
]; |
| 166 |
} |
| 167 |
} |
| 168 |
|