| 1 |
<?php |
| 2 |
/** |
| 3 |
* Last sync report class |
| 4 |
* |
| 5 |
* @since 4.4.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\StatusReport; |
| 10 |
|
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
/** |
| 14 |
* Last sync report class |
| 15 |
* |
| 16 |
* @package ElasticPress |
| 17 |
*/ |
| 18 |
class LastSync extends Report { |
| 19 |
|
| 20 |
/** |
| 21 |
* Return the report title |
| 22 |
* |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public function get_title(): string { |
| 26 |
return __( 'Last Sync', 'elasticpress' ); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Return the report fields |
| 31 |
* |
| 32 |
* @return array |
| 33 |
*/ |
| 34 |
public function get_groups(): array { |
| 35 |
$fields = []; |
| 36 |
|
| 37 |
$sync_info = \ElasticPress\IndexHelper::factory()->get_last_sync(); |
| 38 |
|
| 39 |
if ( empty( $sync_info ) ) { |
| 40 |
return []; |
| 41 |
} |
| 42 |
|
| 43 |
if ( ! empty( $sync_info['end_time_gmt'] ) ) { |
| 44 |
unset( $sync_info['end_time_gmt'] ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( ! empty( $sync_info['total_time'] ) ) { |
| 48 |
$sync_info['total_time'] = human_readable_duration( gmdate( 'H:i:s', ceil( $sync_info['total_time'] ) ) ); |
| 49 |
} |
| 50 |
|
| 51 |
if ( ! empty( $sync_info['end_date_time'] ) ) { |
| 52 |
$sync_info['end_date_time'] = wp_date( |
| 53 |
'Y/m/d g:i:s a', |
| 54 |
strtotime( $sync_info['end_date_time'] ) |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
if ( ! empty( $sync_info['start_date_time'] ) ) { |
| 59 |
$sync_info['start_date_time'] = wp_date( |
| 60 |
'Y/m/d g:i:s a', |
| 61 |
strtotime( $sync_info['start_date_time'] ) |
| 62 |
); |
| 63 |
} |
| 64 |
|
| 65 |
if ( ! empty( $sync_info['method'] ) ) { |
| 66 |
$methods = [ |
| 67 |
'web' => esc_html__( 'WP Dashboard', 'elasticpress' ), |
| 68 |
'cli' => esc_html__( 'WP-CLI', 'elasticpress' ), |
| 69 |
]; |
| 70 |
|
| 71 |
$sync_info['method'] = $methods[ $sync_info['method'] ] ?? $sync_info['method']; |
| 72 |
} |
| 73 |
|
| 74 |
if ( isset( $sync_info['is_full_sync'] ) ) { |
| 75 |
$sync_info['is_full_sync'] = $sync_info['is_full_sync'] ? esc_html__( 'Yes', 'elasticpress' ) : esc_html__( 'No', 'elasticpress' ); |
| 76 |
} |
| 77 |
|
| 78 |
$labels = [ |
| 79 |
'total' => esc_html__( 'Total', 'elasticpress' ), |
| 80 |
'synced' => esc_html__( 'Synced', 'elasticpress' ), |
| 81 |
'skipped' => esc_html__( 'Skipped', 'elasticpress' ), |
| 82 |
'failed' => esc_html__( 'Failed', 'elasticpress' ), |
| 83 |
'errors' => esc_html__( 'Errors', 'elasticpress' ), |
| 84 |
'method' => esc_html__( 'Method', 'elasticpress' ), |
| 85 |
'is_full_sync' => esc_html__( 'Full Sync', 'elasticpress' ), |
| 86 |
'end_date_time' => esc_html__( 'End Date Time', 'elasticpress' ), |
| 87 |
'start_date_time' => esc_html__( 'Start Date Time', 'elasticpress' ), |
| 88 |
'total_time' => esc_html__( 'Total Time', 'elasticpress' ), |
| 89 |
]; |
| 90 |
|
| 91 |
/** |
| 92 |
* Apply a custom order to the table rows. |
| 93 |
* |
| 94 |
* As some rows could be unavailable (if the last sync was done using an older version of the plugin, for example), |
| 95 |
* the usual `array_replace(array_flip())` strategy to reorder an array adds a wrong numeric value to the |
| 96 |
* non-existent row. |
| 97 |
*/ |
| 98 |
$preferred_order = [ 'method', 'is_full_sync', 'start_date_time', 'end_date_time', 'total_time', 'total', 'synced', 'skipped', 'failed', 'errors' ]; |
| 99 |
$ordered_sync_info = []; |
| 100 |
foreach ( $preferred_order as $field ) { |
| 101 |
if ( array_key_exists( $field, $sync_info ) ) { |
| 102 |
$ordered_sync_info[ $field ] = $sync_info[ $field ] ?? esc_html_x( 'N/A', 'Sync info not available', 'elasticpress' ); |
| 103 |
unset( $sync_info[ $field ] ); |
| 104 |
} |
| 105 |
} |
| 106 |
$sync_info = $ordered_sync_info + $sync_info; |
| 107 |
|
| 108 |
foreach ( $sync_info as $label => $value ) { |
| 109 |
$fields[ sanitize_title( $label ) ] = [ |
| 110 |
'label' => $labels[ $label ] ?? $label, |
| 111 |
'value' => $value, |
| 112 |
]; |
| 113 |
} |
| 114 |
$title = $sync_info['start_date_time'] ?? ''; |
| 115 |
if ( false !== \ElasticPress\Utils\get_indexing_status() ) { |
| 116 |
/* translators: last sync title */ |
| 117 |
$title = sprintf( __( '%s (In Progress)', 'elasticpress' ), $title ); |
| 118 |
} |
| 119 |
if ( 'status-report' === \ElasticPress\Screen::factory()->get_current_screen() ) { |
| 120 |
unset( $fields['start_date_time'] ); |
| 121 |
} |
| 122 |
|
| 123 |
return [ |
| 124 |
[ |
| 125 |
'title' => $title, |
| 126 |
'fields' => $fields, |
| 127 |
], |
| 128 |
]; |
| 129 |
} |
| 130 |
} |
| 131 |
|