Completed
Push — add/sso-analytics ( 683a91...e67d7d )
by
unknown
162:06 queued 152:34
created

Jetpack_Sync_Dashboard   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
dl 0
loc 159
rs 10
wmc 15
lcom 1
cbo 2

15 Methods

Rating   Name   Duplication   Size   Complexity  
A add_page_actions() 0 4 1
A get_page_hook() 0 6 1
B page_admin_scripts() 0 40 1
A page_render() 0 3 1
A init() 0 9 1
A ajax_queue_status() 0 5 1
A ajax_reset_queue() 0 5 1
A ajax_unlock_queue() 0 5 1
A ajax_begin_full_sync() 0 4 1
A ajax_cancel_full_sync() 0 3 1
A ajax_full_sync_status() 0 5 1
A queue_status() 0 9 1
A full_sync_status() 0 5 1
A dashboard_ui() 0 20 1
A js_progress_template() 0 17 1
1
<?php
2
3
require_once( jetpack_require_lib_dir() . '/admin-pages/class.jetpack-admin-page.php' );
4
5
class Jetpack_Sync_Dashboard extends Jetpack_Admin_Page {
6
	protected $dont_show_if_not_active = false; // TODO: Update to true
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
7
8
	function add_page_actions( $hook ) {
9
10
		add_action( "admin_footer-$hook", array( $this, 'js_progress_template' ) );
11
	}
12
13
	function get_page_hook() {
14
		return add_submenu_page( null, __( 'Jetpack Sync Status', 'jetpack' ), '', 'manage_options', 'jetpack-sync', array(
15
			$this,
16
			'render'
17
		) );
18
	}
19
20
	function page_admin_scripts() {
21
		wp_register_script(
22
			'jetpack_sync_reindex_control',
23
			plugins_url( '_inc/jetpack-sync.js', JETPACK__PLUGIN_FILE ),
24
			array( 'jquery', 'wp-util' ),
25
			JETPACK__VERSION,
26
			true // load it at the bottom of the page
27
		);
28
29
		$strings                  = array(
30
			'WAITING'     => array(
31
				'action' => __( 'Refresh Status', 'jetpack' ),
32
				'status' => __( 'Indexing request queued and waiting&hellip;', 'jetpack' ),
33
			),
34
			'INDEXING'    => array(
35
				'action' => __( 'Refresh Status', 'jetpack' ),
36
				'status' => __( 'Indexing posts', 'jetpack' ),
37
			),
38
			'DONE'        => array(
39
				'action' => __( 'Reindex Posts', 'jetpack' ),
40
				'status' => __( 'Posts indexed.', 'jetpack' ),
41
			),
42
			'ERROR'       => array(
43
				'action' => __( 'Refresh Status', 'jetpack' ),
44
				'status' => __( 'Status unknown.', 'jetpack' ),
45
			),
46
			'ERROR:LARGE' => array(
47
				'action' => __( 'Refresh Status', 'jetpack' ),
48
				'status' => __( 'This site is too large, please contact Jetpack support to sync.', 'jetpack' ),
49
			),
50
		);
51
		$initial_queue_status     = json_encode( $this->queue_status() );
52
		$initial_full_sync_status = json_encode( $this->full_sync_status() );
53
54
		wp_localize_script( 'jetpack_sync_reindex_control', 'sync_dashboard', array(
55
			'possible_status'  => $strings,
56
			'queue_status'     => $initial_queue_status,
57
			'full_sync_status' => $initial_full_sync_status
58
		) );
59
	}
60
61
	function page_render() {
62
		$this->dashboard_ui();
63
	}
64
65
	function init() {
66
		add_action( 'wp_ajax_jetpack-sync-queue-status', array( $this, 'ajax_queue_status' ) );
67
		add_action( 'wp_ajax_jetpack-sync-reset-queue', array( $this, 'ajax_reset_queue' ) );
68
		add_action( 'wp_ajax_jetpack-sync-unlock-queue', array( $this, 'ajax_unlock_queue' ) );
69
		add_action( 'wp_ajax_jetpack-sync-begin-full-sync', array( $this, 'ajax_begin_full_sync' ) );
70
		add_action( 'wp_ajax_jetpack-sync-cancel-full-sync', array( $this, 'ajax_cancel_full_sync' ) );
71
		add_action( 'wp_ajax_jetpack-sync-full-sync-status', array( $this, 'ajax_full_sync_status' ) );
72
73
	}
74
75
	// returns size of queue and age of oldest item (aka lag)
76
	function ajax_queue_status() {
77
		$response = json_encode( $this->queue_status() );
78
		echo $response;
79
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax_queue_status() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
80
	}
81
82
	function ajax_reset_queue() {
83
		Jetpack_Sync_Client::getInstance()->reset_sync_queue();
84
		echo json_encode( array( 'success' => true ) );
85
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax_reset_queue() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
86
	}
87
88
	function ajax_unlock_queue() {
89
		Jetpack_Sync_Client::getInstance()->get_sync_queue()->force_checkin();
90
		echo json_encode( array( 'success' => true ) );
91
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax_unlock_queue() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
92
	}
93
94
	function ajax_begin_full_sync() {
95
		Jetpack_Sync_Client::getInstance()->get_full_sync_client()->start();
96
		$this->ajax_full_sync_status();
97
	}
98
99
	function ajax_cancel_full_sync() {
100
		// TODO	
0 ignored issues
show
Coding Style introduced by
Comment refers to a TODO task

This check looks TODO comments that have been left in the code.

``TODO``s show that something is left unfinished and should be attended to.

Loading history...
101
	}
102
103
	function ajax_full_sync_status() {
104
		$response = json_encode( $this->full_sync_status() );
105
		echo $response;
106
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax_full_sync_status() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
107
	}
108
109
	function queue_status() {
110
		$client = Jetpack_Sync_Client::getInstance();
111
		$queue  = $client->get_sync_queue();
112
113
		return array(
114
			'size' => $queue->size(),
115
			'lag'  => $queue->lag()
116
		);
117
	}
118
119
	function full_sync_status() {
120
		$client = Jetpack_Sync_Client::getInstance();
121
122
		return $client->get_full_sync_client()->get_complete_status();
123
	}
124
125
	function dashboard_ui() {
126
		wp_enqueue_script( 'jetpack_sync_reindex_control' );
127
		?>
128
		<div class="wrapper">
129
			<div class="page-content">
130
				<div id="sync_status">
131
					Sync status:
132
				</div>
133
				<p><strong>Warning: Clicking either of these buttons can get you out of sync!</strong></p>
134
				<button class="button" id="reset_queue_button">Reset Queue</button>
135
				<button class="button" id="unlock_queue_button">Unlock Queue</button>
136
				<hr/>
137
				<h2>Full Sync</h2>
138
				<button class="button" id="full_sync_button">Do full sync</button>
139
				<div id="full_sync_status"></div>
140
				<div id="display-sync-status"></div>
141
			</div>
142
		</div>
143
		<?php
144
	}
145
146
	function js_progress_template() { ?>
147
		<script type="text/html" id="tmpl-sync-progress">
148
			<div>
149
				Sync Status: {{ data.phase }}
150
			</div>
151
			<div>
152
				<p>Posts: {{ data.posts && data.posts.progress }} %</p>
153
				<p>Comments: {{ data.comments && data.comments.progress }} %</p>
154
				<p>Terms: {{ data.terms && data.terms.progress }} %</p>
155
				<p>Users: {{ data.users && data.users.progress }} %</p>
156
				<p>Functions: {{ data.functions && data.functions.progress }} %</p>
157
				<p>Constants: {{ data.constants && data.constants.progress }} %</p>
158
				<p>Options: {{ data.options && data.options.progress }} %</p>
159
			</div>
160
		</script>
161
		<?php
162
	}
163
}
164