Completed
Push — add/sync-action ( 8b3b6b...1094d7 )
by
unknown
13:23
created

Jetpack_Sync_Dashboard::js_progress_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 17
rs 9.4285
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( $this, 'render' ) );
15
	}
16
17
	function page_admin_scripts() {
18
		wp_register_script(
19
			'jetpack_sync_reindex_control',
20
			plugins_url( '_inc/jetpack-sync.js', JETPACK__PLUGIN_FILE ),
21
			array( 'jquery', 'wp-util' ),
22
			JETPACK__VERSION,
23
			true // load it at the bottom of the page
24
		);
25
		
26
		$strings = array(
27
			'WAITING'     => array(
28
				'action' => __( 'Refresh Status', 'jetpack' ),
29
				'status' => __( 'Indexing request queued and waiting&hellip;', 'jetpack' ),
30
			),
31
			'INDEXING'    => array(
32
				'action' => __( 'Refresh Status', 'jetpack' ),
33
				'status' => __( 'Indexing posts', 'jetpack' ),
34
			),
35
			'DONE'        => array(
36
				'action' => __( 'Reindex Posts', 'jetpack' ),
37
				'status' => __( 'Posts indexed.', 'jetpack' ),
38
			),
39
			'ERROR'       => array(
40
				'action' => __( 'Refresh Status', 'jetpack' ),
41
				'status' => __( 'Status unknown.', 'jetpack' ),
42
			),
43
			'ERROR:LARGE' => array(
44
				'action' => __( 'Refresh Status', 'jetpack' ),
45
				'status' => __( 'This site is too large, please contact Jetpack support to sync.', 'jetpack' ),
46
			),
47
		);
48
		$initial_queue_status = json_encode( $this->queue_status() );
49
		$initial_full_sync_status = json_encode( $this->full_sync_status() );
50
		
51
		wp_localize_script( 'jetpack_sync_reindex_control', 'sync_dashboard', array(
52
			'possible_status' => $strings,
53
			'queue_status' => $initial_queue_status,
54
			'full_sync_status' => $initial_full_sync_status
55
		) );
56
	}
57
	function page_render() {
58
		$this->dashboard_ui();
59
	}
60
61
	function init() {
62
		add_action( 'wp_ajax_jetpack-sync-queue-status', array( $this, 'ajax_queue_status' ) );
63
		add_action( 'wp_ajax_jetpack-sync-reset-queue', array( $this, 'ajax_reset_queue' ) );
64
		add_action( 'wp_ajax_jetpack-sync-unlock-queue', array( $this, 'ajax_unlock_queue' ) );
65
		add_action( 'wp_ajax_jetpack-sync-begin-full-sync', array( $this, 'ajax_begin_full_sync' ) );
66
		add_action( 'wp_ajax_jetpack-sync-cancel-full-sync', array( $this, 'ajax_cancel_full_sync' ) );
67
		add_action( 'wp_ajax_jetpack-sync-full-sync-status', array( $this, 'ajax_full_sync_status' ) );
68
69
	}
70
71
	// returns size of queue and age of oldest item (aka lag)
72
	function ajax_queue_status() {
73
		$response = json_encode( $this->queue_status() );
74
		echo $response;
75
		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...
76
	}
77
78
	function ajax_reset_queue() {
79
		Jetpack_Sync_Client::getInstance()->reset_sync_queue();
80
		echo json_encode( array( 'success' => true ) );
81
		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...
82
	}
83
84
	function ajax_unlock_queue() {
85
		Jetpack_Sync_Client::getInstance()->get_sync_queue()->force_checkin();
86
		echo json_encode( array( 'success' => true ) );
87
		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...
88
	}
89
90
	function ajax_begin_full_sync() {
91
		Jetpack_Sync_Client::getInstance()->get_full_sync_client()->start();
92
		$this->ajax_full_sync_status();
93
	}
94
95
	function ajax_cancel_full_sync() {
96
		// 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...
97
	}
98
99
	function ajax_full_sync_status() {
100
		$response = json_encode( $this->full_sync_status() );
101
		echo $response;
102
		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...
103
	}
104
105
	function queue_status() {
106
		$client = Jetpack_Sync_Client::getInstance();
107
		$queue = $client->get_sync_queue();
108
109
		return array(
110
			'size' => $queue->size(),
111
			'lag' => $queue->lag()
112
		);
113
	}
114
115
	function full_sync_status() {
116
		$client = Jetpack_Sync_Client::getInstance();
117
		return $client->get_full_sync_client()->get_complete_status();
118
	}
119
120
	function dashboard_ui() {
121
		wp_enqueue_script( 'jetpack_sync_reindex_control' );
122
		?>
123
		<div class="wrapper">
124
			<div class="page-content">
125
				<div id="sync_status">
126
					Sync status:
127
				</div>
128
				<p><strong>Warning: Clicking either of these buttons can get you out of sync!</strong></p>
129
				<button class="button" id="reset_queue_button">Reset Queue</button>
130
				<button class="button" id="unlock_queue_button">Unlock Queue</button>
131
				<hr />
132
				<h2>Full Sync</h2>
133
				<button class="button" id="full_sync_button">Do full sync</button>
134
				<div id="full_sync_status"></div>
135
				<div id="display-sync-status"></div>
136
			</div>
137
		</div>
138
		<?php
139
	}
140
141
	function js_progress_template() { ?>
142
		<script type="text/html" id="tmpl-sync-progress">
143
			<div >
144
				Sync Status: {{ data.phase }}
145
			</div>
146
			<div >
147
				<p>Posts: {{ data.posts && data.posts.progress }} %</p>
148
				<p>Comments: {{ data.comments && data.comments.progress }} %</p>
149
				<p>Terms: {{ data.terms && data.terms.progress }} %</p>
150
				<p>Users: {{ data.users && data.users.progress }} %</p>
151
				<p>Functions: {{ data.functions && data.functions.progress }} %</p>
152
				<p>Constants: {{ data.constants && data.constants.progress }} %</p>
153
				<p>Options: {{ data.options && data.options.progress }} %</p>
154
			</p>
155
		</script>
156
		<?php
157
	}
158
}
159