PluginProbe
ElasticPress / 4.4.0
ElasticPress v4.4.0
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 / StatusReport / LastSync.php

LastSync.php in ElasticPress 4.4.0, at includes/classes/StatusReport/LastSync.php

129 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $title = __( 'Last Sync', 'elasticpress' );
36 $fields = [];
37
38 $sync_info = \ElasticPress\IndexHelper::factory()->get_last_index();
39
40 if ( empty( $sync_info ) ) {
41 $fields['not_available'] = [
42 'label' => esc_html__( 'Last Sync', 'elasticpress' ),
43 'value' => esc_html__( 'Last sync info not available.', 'elasticpress' ),
44 ];
45
46 return [
47 [
48 'title' => $title,
49 'fields' => $fields,
50 ],
51 ];
52 }
53
54 if ( ! empty( $sync_info['end_time_gmt'] ) ) {
55 unset( $sync_info['end_time_gmt'] );
56 }
57
58 if ( ! empty( $sync_info['total_time'] ) ) {
59 $sync_info['total_time'] = human_readable_duration( gmdate( 'H:i:s', ceil( $sync_info['total_time'] ) ) );
60 }
61
62 if ( ! empty( $sync_info['end_date_time'] ) ) {
63 $sync_info['end_date_time'] = wp_date(
64 'Y/m/d g:i:s a',
65 strtotime( $sync_info['end_date_time'] )
66 );
67 }
68
69 if ( ! empty( $sync_info['start_date_time'] ) ) {
70 $sync_info['start_date_time'] = wp_date(
71 'Y/m/d g:i:s a',
72 strtotime( $sync_info['start_date_time'] )
73 );
74 }
75
76 if ( ! empty( $sync_info['method'] ) ) {
77 $methods = [
78 'web' => esc_html__( 'WP Dashboard', 'elasticpress' ),
79 'cli' => esc_html__( 'WP-CLI', 'elasticpress' ),
80 ];
81
82 $sync_info['method'] = $methods[ $sync_info['method'] ] ?? $sync_info['method'];
83 }
84
85 $labels = [
86 'total' => esc_html__( 'Total', 'elasticpress' ),
87 'synced' => esc_html__( 'Synced', 'elasticpress' ),
88 'skipped' => esc_html__( 'Skipped', 'elasticpress' ),
89 'failed' => esc_html__( 'Failed', 'elasticpress' ),
90 'errors' => esc_html__( 'Errors', 'elasticpress' ),
91 'method' => esc_html__( 'Method', 'elasticpress' ),
92 'end_date_time' => esc_html__( 'End Date Time', 'elasticpress' ),
93 'start_date_time' => esc_html__( 'Start Date Time', 'elasticpress' ),
94 'total_time' => esc_html__( 'Total Time', 'elasticpress' ),
95 ];
96
97 /**
98 * Apply a custom order to the table rows.
99 *
100 * As some rows could be unavailable (if the last sync was done using an older version of the plugin, for example),
101 * the usual `array_replace(array_flip())` strategy to reorder an array adds a wrong numeric value to the
102 * non-existent row.
103 */
104 $preferred_order = [ 'method', 'start_date_time', 'end_date_time', 'total_time', 'total', 'synced', 'skipped', 'failed', 'errors' ];
105 $ordered_sync_info = [];
106 foreach ( $preferred_order as $field ) {
107 if ( array_key_exists( $field, $sync_info ) ) {
108 $ordered_sync_info[ $field ] = $sync_info[ $field ] ?? esc_html_x( 'N/A', 'Sync info not available', 'elasticpress' );
109 unset( $sync_info[ $field ] );
110 }
111 }
112 $sync_info = $ordered_sync_info + $sync_info;
113
114 foreach ( $sync_info as $label => $value ) {
115 $fields[ sanitize_title( $label ) ] = [
116 'label' => $labels[ $label ] ?? $label,
117 'value' => $value,
118 ];
119 }
120
121 return [
122 [
123 'title' => $title,
124 'fields' => $fields,
125 ],
126 ];
127 }
128 }
129