Completed
Push — add/sync-rest-2 ( a434af...d8d9e1 )
by
unknown
77:18 queued 68:04
created

Jetpack_Sync_Dashboard::ajax_begin_full_sync()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
class Jetpack_Sync_Dashboard {
4
	static function init() {
5
		error_log("initialized sync actions");
6
		add_action( 'wp_ajax_jetpack-sync-queue-status', array( __CLASS__, 'ajax_queue_status' ) );
7
		add_action( 'wp_ajax_jetpack-sync-begin-full-sync', array( __CLASS__, 'ajax_begin_full_sync' ) );
8
		add_action( 'wp_ajax_jetpack-sync-full-sync-status', array( __CLASS__, 'ajax_full_sync_status' ) );
9
	}
10
11
	// returns size of queue and age of oldest item (aka lag)
12
	static function ajax_queue_status() {
13
		$response = json_encode( self::queue_status() );
14
		echo $response;
15
		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...
16
	}
17
18
	static function ajax_begin_full_sync() {
19
		Jetpack_Sync::schedule_full_sync();
20
	}
21
22
	static function ajax_full_sync_status() {
23
		$client = Jetpack_Sync_Client::getInstance();
24
		$response = json_encode( $client->get_full_sync_client()->get_status() );
25
		echo $response;
26
		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...
27
	}
28
29
	static function queue_status() {
30
		$client = Jetpack_Sync_Client::getInstance();
31
		$queue = $client->get_sync_queue();
32
33
		return array(
34
			'size' => $queue->size(),
35
			'lag' => $queue->lag()
36
		);
37
	}
38
39
	static function jetpack_sync_admin_head() {
40
		$initial_state = json_encode( self::queue_status() );
41
		?>
42
		<script type="text/javascript">
43
			jQuery( document ).ready( function($) {
44
				JetpackSyncStatus.init( '#sync_status', <?php echo $initial_state ?> );
45
			} );
46
		</script>
47
		<?php 
48
	}
49
50
	static function dashboard_ui() {			
51
		if ( ! current_user_can( 'manage_options' ) )
52
			wp_die( esc_html__('You do not have sufficient permissions to access this page.', 'jetpack' ) );
53
54
		$strings = json_encode( array(
55
			'WAITING'     => array(
56
				'action' => __( 'Refresh Status', 'jetpack' ),
57
				'status' => __( 'Indexing request queued and waiting&hellip;', 'jetpack' ),
58
			),
59
			'INDEXING'    => array(
60
				'action' => __( 'Refresh Status', 'jetpack' ),
61
				'status' => __( 'Indexing posts', 'jetpack' ),
62
			),
63
			'DONE'        => array(
64
				'action' => __( 'Reindex Posts', 'jetpack' ),
65
				'status' => __( 'Posts indexed.', 'jetpack' ),
66
			),
67
			'ERROR'       => array(
68
				'action' => __( 'Refresh Status', 'jetpack' ),
69
				'status' => __( 'Status unknown.', 'jetpack' ),
70
			),
71
			'ERROR:LARGE' => array(
72
				'action' => __( 'Refresh Status', 'jetpack' ),
73
				'status' => __( 'This site is too large, please contact Jetpack support to sync.', 'jetpack' ),
74
			),
75
		) );
76
77
		wp_enqueue_script(
78
			'jetpack_sync_reindex_control',
79
			plugins_url( '_inc/jetpack-sync.js', JETPACK__PLUGIN_FILE ),
80
			array( 'jquery' ),
81
			JETPACK__VERSION
82
		);
83
84
// 		$template = <<<EOT
85
// 			<p class="jetpack_sync_reindex_control" id="jetpack_sync_reindex_control" data-strings="%s">
86
// 				<input type="submit" class="jetpack_sync_reindex_control_action button" value="%s" disabled />
87
// 				<span class="jetpack_sync_reindex_control_status">&hellip;</span>
88
// 			</p>
89
// EOT;
90
91
		
92
93
		$template = <<<EOT
94
			<div id="sync_status">
95
				Sync status:
96
			</div>
97
			<p class="jetpack_sync_reindex_control" id="jetpack_sync_reindex_control" data-strings="%s">
98
				This is a test 
99
				<input type="submit" class="jetpack_sync_reindex_control_action button" value="%s" disabled />
100
				<span class="jetpack_sync_reindex_control_status">&hellip;</span>
101
			</p>
102
EOT;
103
104
		echo sprintf(
105
			$template,
106
			esc_attr( $strings ),
107
			esc_attr__( 'Refresh Status', 'jetpack' )
108
		);
109
	}
110
111
}
112