1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jetpack sync debug helper class |
4
|
|
|
* |
5
|
|
|
* @package jetpack |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
defined( 'JETPACK__API_BASE' ) || define( 'JETPACK__API_BASE', get_option( Jetpack_Sync_Debug_Helper::API_BASE, 'https://jetpack.wordpress.com/jetpack.' ) ); |
9
|
|
|
|
10
|
|
|
Jetpack_Sync_Debug_Helper::init(); |
11
|
|
|
|
12
|
|
|
register_deactivation_hook( JETPACK_DEBUG_HELPER_BASE_PLUGIN_FILE, array( 'Jetpack_Sync_Debug_Helper', 'deactivate' ) ); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Helps debug sync errors |
16
|
|
|
*/ |
17
|
|
|
class Jetpack_Sync_Debug_Helper { |
18
|
|
|
const API_BASE = 'jetpack_debugger_api_base'; |
19
|
|
|
const LAST_SYNC_ERROR = 'jetpack_debugger_last_sync_error'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Saved error |
23
|
|
|
* |
24
|
|
|
* @var boolean |
25
|
|
|
*/ |
26
|
|
|
public static $saved_error = false; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Init hooks |
30
|
|
|
* |
31
|
|
|
* @return void |
32
|
|
|
*/ |
33
|
|
|
public static function init() { |
34
|
|
|
// allows us to set the value via the api. |
35
|
|
|
add_filter( 'jetpack_options_whitelist', array( 'Jetpack_Sync_Debug_Helper', 'whitelist_options' ) ); |
36
|
|
|
add_filter( 'jetpack_sync_send_data', array( 'Jetpack_Sync_Debug_Helper', 'pre_send' ), 8, 4 ); |
37
|
|
|
add_filter( 'jetpack_sync_send_data', array( 'Jetpack_Sync_Debug_Helper', 'store_sync_error' ), 11, 4 ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Deactivation hook |
42
|
|
|
*/ |
43
|
|
|
public static function deactivate() { |
44
|
|
|
delete_option( self::API_BASE ); |
45
|
|
|
delete_option( self::LAST_SYNC_ERROR ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Pre send |
50
|
|
|
* |
51
|
|
|
* @param array $data The action buffer. |
52
|
|
|
* @param string $codec_name The codec name used to encode the data. |
53
|
|
|
* @param double $sent_timestamp The current time. |
54
|
|
|
* @param string $queue_id The queue used to send ('sync' or 'full_sync'). |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public static function pre_send( $data, $codec_name, $sent_timestamp, $queue_id ) { |
58
|
|
|
if ( empty( $data ) ) { |
59
|
|
|
update_option( |
60
|
|
|
self::LAST_SYNC_ERROR, |
61
|
|
|
array( |
62
|
|
|
'error_code' => 'pre_empty', |
63
|
|
|
'queue' => $queue_id, |
64
|
|
|
'timestamp' => $sent_timestamp, |
65
|
|
|
'codec' => $codec_name, |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
self::$saved_error = true; |
70
|
|
|
} |
71
|
|
|
return $data; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Pre send |
76
|
|
|
* |
77
|
|
|
* @param array $data The action buffer. |
78
|
|
|
* @param string $codec_name The codec name used to encode the data. |
79
|
|
|
* @param double $sent_timestamp The current time. |
80
|
|
|
* @param string $queue_id The queue used to send ('sync' or 'full_sync'). |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public static function store_sync_error( $data, $codec_name, $sent_timestamp, $queue_id ) { |
84
|
|
View Code Duplication |
if ( isset( $data['error_code'] ) && ! self::$saved_error ) { |
85
|
|
|
update_option( |
86
|
|
|
self::LAST_SYNC_ERROR, |
87
|
|
|
array( |
88
|
|
|
'error_code' => $data['error_code'], |
89
|
|
|
'queue' => $queue_id, |
90
|
|
|
'timestamp' => $sent_timestamp, |
91
|
|
|
'codec' => $codec_name, |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
View Code Duplication |
if ( empty( $data ) && ! self::$saved_error ) { |
96
|
|
|
update_option( |
97
|
|
|
self::LAST_SYNC_ERROR, |
98
|
|
|
array( |
99
|
|
|
'error_code' => 'empty', |
100
|
|
|
'queue' => $queue_id, |
101
|
|
|
'timestamp' => $sent_timestamp, |
102
|
|
|
'codec' => $codec_name, |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
return $data; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Whitelist synced options |
111
|
|
|
* |
112
|
|
|
* @param array $options options. |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
public static function whitelist_options( $options ) { |
116
|
|
|
return array_merge( |
117
|
|
|
$options, |
118
|
|
|
array( |
119
|
|
|
self::API_BASE, |
120
|
|
|
self::LAST_SYNC_ERROR, |
121
|
|
|
) |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|