| 1 |
<?php |
| 2 |
/** |
| 3 |
* Health info screen. |
| 4 |
* |
| 5 |
* @package ElasticPress |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ElasticPress\Screen; |
| 9 |
|
| 10 |
use ElasticPress\Features as Features; |
| 11 |
use ElasticPress\IndexHelper; |
| 12 |
use ElasticPress\Utils as Utils; |
| 13 |
|
| 14 |
/** |
| 15 |
* Health info screen Class. |
| 16 |
*/ |
| 17 |
class HealthInfo { |
| 18 |
|
| 19 |
/** |
| 20 |
* Initialize class. |
| 21 |
*/ |
| 22 |
public function setup() { |
| 23 |
add_action( 'debug_information', [ $this, 'last_sync_health_info' ] ); |
| 24 |
add_filter( 'debug_information', [ $this, 'epio_autosuggest_health_check_info' ] ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Display sync info in site health screen. |
| 29 |
* |
| 30 |
* @param array $debug_info The debug info for site health screen. |
| 31 |
* @return array The debug info for site health screen. |
| 32 |
*/ |
| 33 |
public function last_sync_health_info( $debug_info ) { |
| 34 |
$debug_info['ep_last_sync'] = array( |
| 35 |
'label' => esc_html__( 'ElasticPress - Last Sync', 'elasticpress' ), |
| 36 |
'fields' => [], |
| 37 |
); |
| 38 |
|
| 39 |
$sync_info = IndexHelper::factory()->get_last_index(); |
| 40 |
|
| 41 |
if ( empty( $sync_info ) ) { |
| 42 |
$debug_info['ep_last_sync']['fields']['not_available'] = [ |
| 43 |
'label' => esc_html__( 'Last Sync', 'elasticpress' ), |
| 44 |
'value' => esc_html__( 'Last sync info not available.', 'elasticpress' ), |
| 45 |
'private' => true, |
| 46 |
]; |
| 47 |
return $debug_info; |
| 48 |
} |
| 49 |
|
| 50 |
if ( ! empty( $sync_info['end_time_gmt'] ) ) { |
| 51 |
unset( $sync_info['end_time_gmt'] ); |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! empty( $sync_info['total_time'] ) ) { |
| 55 |
$sync_info['total_time'] = human_readable_duration( gmdate( 'H:i:s', ceil( $sync_info['total_time'] ) ) ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! empty( $sync_info['end_date_time'] ) ) { |
| 59 |
$sync_info['end_date_time'] = wp_date( |
| 60 |
'Y/m/d g:i:s a', |
| 61 |
strtotime( $sync_info['end_date_time'] ) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! empty( $sync_info['start_date_time'] ) ) { |
| 66 |
$sync_info['start_date_time'] = wp_date( |
| 67 |
'Y/m/d g:i:s a', |
| 68 |
strtotime( $sync_info['start_date_time'] ) |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
if ( ! empty( $sync_info['method'] ) ) { |
| 73 |
$methods = [ |
| 74 |
'web' => esc_html__( 'WP Dashboard', 'elasticpress' ), |
| 75 |
'cli' => esc_html__( 'WP-CLI', 'elasticpress' ), |
| 76 |
]; |
| 77 |
|
| 78 |
$sync_info['method'] = $methods[ $sync_info['method'] ] ?? $sync_info['method']; |
| 79 |
} |
| 80 |
|
| 81 |
$labels = [ |
| 82 |
'total' => esc_html__( 'Total', 'elasticpress' ), |
| 83 |
'synced' => esc_html__( 'Synced', 'elasticpress' ), |
| 84 |
'skipped' => esc_html__( 'Skipped', 'elasticpress' ), |
| 85 |
'failed' => esc_html__( 'Failed', 'elasticpress' ), |
| 86 |
'errors' => esc_html__( 'Errors', 'elasticpress' ), |
| 87 |
'method' => esc_html__( 'Method', 'elasticpress' ), |
| 88 |
'end_date_time' => esc_html__( 'End Date Time', 'elasticpress' ), |
| 89 |
'start_date_time' => esc_html__( 'Start Date Time', 'elasticpress' ), |
| 90 |
'total_time' => esc_html__( 'Total Time', 'elasticpress' ), |
| 91 |
]; |
| 92 |
|
| 93 |
/** |
| 94 |
* Apply a custom order to the table rows. |
| 95 |
* |
| 96 |
* As some rows could be unavailable (if the last sync was done using an older version of the plugin, for example), |
| 97 |
* the usual `array_replace(array_flip())` strategy to reorder an array adds a wrong numeric value to the |
| 98 |
* non-existent row. |
| 99 |
*/ |
| 100 |
$preferred_order = [ 'method', 'start_date_time', 'end_date_time', 'total_time', 'total', 'synced', 'skipped', 'failed', 'errors' ]; |
| 101 |
$ordered_sync_info = []; |
| 102 |
foreach ( $preferred_order as $field ) { |
| 103 |
if ( array_key_exists( $field, $sync_info ) ) { |
| 104 |
$ordered_sync_info[ $field ] = $sync_info[ $field ] ?? esc_html_x( 'N/A', 'Sync info not available', 'elasticpress' ); |
| 105 |
unset( $sync_info[ $field ] ); |
| 106 |
} |
| 107 |
} |
| 108 |
$sync_info = $ordered_sync_info + $sync_info; |
| 109 |
|
| 110 |
foreach ( $sync_info as $label => $value ) { |
| 111 |
$debug_info['ep_last_sync']['fields'][ sanitize_title( $label ) ] = [ |
| 112 |
'label' => $labels[ $label ] ?? $label, |
| 113 |
'value' => $value, |
| 114 |
'private' => true, |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
return $debug_info; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Add Autosuggest info for EP.io Users in Health Check Info Screen. |
| 123 |
* |
| 124 |
* @since 3.5.x |
| 125 |
* @param array $debug_info Debug Info set so far. |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
public function epio_autosuggest_health_check_info( $debug_info ) { |
| 129 |
if ( ! Utils\is_epio() ) { |
| 130 |
return $debug_info; |
| 131 |
} |
| 132 |
|
| 133 |
$debug_info['epio_autosuggest'] = array( |
| 134 |
'label' => esc_html__( 'ElasticPress.io - Autosuggest', 'elasticpress' ), |
| 135 |
'fields' => [], |
| 136 |
); |
| 137 |
|
| 138 |
$autosuggest_feature = Features::factory()->get_registered_feature( 'autosuggest' ); |
| 139 |
$allowed_params = $autosuggest_feature->epio_autosuggest_set_and_get(); |
| 140 |
|
| 141 |
if ( empty( $allowed_params ) ) { |
| 142 |
return $debug_info; |
| 143 |
} |
| 144 |
|
| 145 |
$allowed_params = wp_parse_args( |
| 146 |
$allowed_params, |
| 147 |
[ |
| 148 |
'postTypes' => [], |
| 149 |
'postStatus' => [], |
| 150 |
'searchFields' => [], |
| 151 |
'returnFields' => '', |
| 152 |
] |
| 153 |
); |
| 154 |
|
| 155 |
$fields = [ |
| 156 |
'Post Types' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['postTypes'] ), |
| 157 |
'Post Status' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['postStatus'] ), |
| 158 |
'Search Fields' => wp_sprintf( esc_html__( '%l', 'elasticpress' ), $allowed_params['searchFields'] ), |
| 159 |
'Returned Fields' => wp_sprintf( esc_html( var_export( $allowed_params['returnFields'], true ) ) ), // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_export |
| 160 |
]; |
| 161 |
|
| 162 |
foreach ( $fields as $label => $value ) { |
| 163 |
$debug_info['epio_autosuggest']['fields'][ sanitize_title( $label ) ] = [ |
| 164 |
'label' => $label, |
| 165 |
'value' => $value, |
| 166 |
'private' => true, |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
return $debug_info; |
| 171 |
} |
| 172 |
} |
| 173 |
|