Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.12% covered (warning)
78.12%
50 / 64
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Debug_Data
78.12% covered (warning)
78.12%
50 / 64
50.00% covered (danger)
50.00%
1 / 2
11.05
0.00% covered (danger)
0.00%
0 / 1
 add_fields
100.00% covered (success)
100.00%
47 / 47
100.00% covered (success)
100.00%
1 / 1
5
 seconds_to_time
17.65% covered (danger)
17.65%
3 / 17
0.00% covered (danger)
0.00%
0 / 1
18.96
1<?php
2/**
3 * Sync fields for the Jetpack Connection section of the Site Health Info tab.
4 *
5 * @package automattic/jetpack-sync
6 */
7
8namespace Automattic\Jetpack\Sync;
9
10/**
11 * Adds the sync status to the section the connection package builds.
12 *
13 * @since 5.5.0
14 */
15class Debug_Data {
16
17    /**
18     * Add the sync fields.
19     *
20     * @since 5.5.0
21     *
22     * @param array $fields Fields keyed by ID, each with a `label`, a `value` and a `private` flag.
23     *
24     * @return array The fields with the sync status added.
25     */
26    public static function add_fields( $fields ) {
27        if ( ! Actions::sync_allowed() ) {
28            $fields['sync_allowed'] = array(
29                'label'   => 'Sync Allowed',
30                'value'   => 'No',
31                'private' => false,
32            );
33
34            return $fields;
35        }
36
37        $fields['sync_allowed'] = array(
38            'label'   => 'Sync Allowed',
39            'value'   => 'Yes',
40            'private' => false,
41        );
42
43        $sync_module = Modules::get_module( 'full-sync' );
44        '@phan-var \Automattic\Jetpack\Sync\Modules\Full_Sync_Immediately|\Automattic\Jetpack\Sync\Modules\Full_Sync $sync_module';
45        if ( $sync_module ) {
46            $human_readable_sync_status = array();
47            foreach ( $sync_module->get_status() as $sync_status => $sync_status_value ) {
48                $human_readable_sync_status[ $sync_status ] =
49                    in_array( $sync_status, array( 'started', 'queue_finished', 'send_started', 'finished' ), true )
50                        ? gmdate( 'r', $sync_status_value ) : $sync_status_value;
51            }
52            $fields['full_sync'] = array(
53                'label'   => 'Full Sync Status',
54                'value'   => wp_json_encode( $human_readable_sync_status, JSON_UNESCAPED_SLASHES ),
55                'private' => false,
56            );
57        }
58
59        $queue = Sender::get_instance()->get_sync_queue();
60
61        $fields['sync_size'] = array(
62            'label'   => 'Sync Queue Size',
63            'value'   => $queue->size(),
64            'private' => false,
65        );
66        $fields['sync_lag']  = array(
67            'label'   => 'Sync Queue Lag',
68            'value'   => self::seconds_to_time( $queue->lag() ),
69            'private' => false,
70        );
71
72        $full_sync_queue = Sender::get_instance()->get_full_sync_queue();
73
74        $fields['full_sync_size'] = array(
75            'label'   => 'Full Sync Queue Size',
76            'value'   => $full_sync_queue->size(),
77            'private' => false,
78        );
79        $fields['full_sync_lag']  = array(
80            'label'   => 'Full Sync Queue Lag',
81            'value'   => self::seconds_to_time( $full_sync_queue->lag() ),
82            'private' => false,
83        );
84
85        return $fields;
86    }
87
88    /**
89     * Convert seconds to "1 hour, 2 minutes, 3 seconds".
90     *
91     * Core's human_time_diff() rounds to one unit, which hides a lag of seconds.
92     *
93     * @param int|float $seconds Number of seconds.
94     *
95     * @return string Human readable time.
96     */
97    private static function seconds_to_time( $seconds ) {
98        $seconds = (int) $seconds;
99        if ( 0 === $seconds ) {
100            return '0 seconds';
101        }
102
103        $units = array(
104            'week'   => WEEK_IN_SECONDS,
105            'day'    => DAY_IN_SECONDS,
106            'hour'   => HOUR_IN_SECONDS,
107            'minute' => MINUTE_IN_SECONDS,
108            'second' => 1,
109        );
110
111        $parts = array();
112        foreach ( $units as $name => $divisor ) {
113            $quot = (int) ( $seconds / $divisor );
114            if ( $quot ) {
115                $parts[]  = $quot . ' ' . $name . ( abs( $quot ) > 1 ? 's' : '' );
116                $seconds -= $quot * $divisor;
117            }
118        }
119
120        return implode( ', ', $parts );
121    }
122}