Completed
Push — add/sync-rest-2 ( 4e2aeb...44e329 )
by
unknown
371:13 queued 361:54
created

Jetpack_Sync_Reindex::sync_reindex_status()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 8
loc 8
rs 9.4285
cc 3
eloc 6
nc 2
nop 0
1
<?php
2
3
class Jetpack_Sync_Reindex {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Jetpack_Sync_Reindex has been defined more than once; this definition is ignored, only the first definition in sync/class.jetpack-sync-dashboard.php (L3-72) is considered.

This check looks for classes that have been defined more than once.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
4
5
	static function init() {
6
		add_action( 'wp_ajax_jetpack-sync-reindex-trigger', array( __CLASS__, 'sync_reindex_trigger' ) );
7
		add_action( 'wp_ajax_jetpack-sync-reindex-status', array( __CLASS__, 'sync_reindex_status' ) );
8
	}
9
10
	static function reindex_if_needed() {
11
		if ( self::reindex_needed() ) {
0 ignored issues
show
Bug introduced by
The method reindex_needed() does not seem to exist on object<Jetpack_Sync_Reindex>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
12
			self::reindex_trigger();
0 ignored issues
show
Bug introduced by
The method reindex_trigger() does not seem to exist on object<Jetpack_Sync_Reindex>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
13
		}
14
	}
15
16
	static function reindex_needed() {
17
		return ( self::_get_post_count_local() != self::_get_post_count_cloud() );
0 ignored issues
show
Bug introduced by
The method _get_post_count_local() does not seem to exist on object<Jetpack_Sync_Reindex>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method _get_post_count_cloud() does not seem to exist on object<Jetpack_Sync_Reindex>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
18
	}
19
20
	static function reindex_trigger() {
21
		$response = array( 'status' => 'ERROR' );
22
23
		// Force a privacy check
24
		Jetpack::check_privacy( JETPACK__PLUGIN_FILE );
25
26
		Jetpack::load_xml_rpc_client();
27
		$client = new Jetpack_IXR_Client( array(
28
			'user_id' => JETPACK_MASTER_USER,
29
		) );
30
31
		$client->query( 'jetpack.reindexTrigger' );
32
33
		if ( ! $client->isError() ) {
34
			$response = $client->getResponse();
35
			Jetpack_Options::update_option( 'sync_bulk_reindexing', true );
36
		}
37
38
		return $response;
39
	}
40
41
	public function reindex_status() {
42
		$response = array( 'status' => 'ERROR' );
43
44
		// Assume reindexing is done if it was not triggered in the first place
45
		if ( false === Jetpack_Options::get_option( 'sync_bulk_reindexing' ) ) {
46
			return array( 'status' => 'DONE' );
47
		}
48
49
		Jetpack::load_xml_rpc_client();
50
		$client = new Jetpack_IXR_Client( array(
51
			'user_id' => JETPACK_MASTER_USER,
52
		) );
53
54
		$client->query( 'jetpack.reindexStatus' );
55
56
		if ( ! $client->isError() ) {
57
			$response = $client->getResponse();
58
			if ( 'DONE' == $response['status'] ) {
59
				Jetpack_Options::delete_option( 'sync_bulk_reindexing' );
60
			}
61
		}
62
63
		return $response;
64
	}
65
66 View Code Duplication
	static function reindex_ui() {
67
		$strings = json_encode( array(
68
			'WAITING'     => array(
69
				'action' => __( 'Refresh Status', 'jetpack' ),
70
				'status' => __( 'Indexing request queued and waiting&hellip;', 'jetpack' ),
71
			),
72
			'INDEXING'    => array(
73
				'action' => __( 'Refresh Status', 'jetpack' ),
74
				'status' => __( 'Indexing posts', 'jetpack' ),
75
			),
76
			'DONE'        => array(
77
				'action' => __( 'Reindex Posts', 'jetpack' ),
78
				'status' => __( 'Posts indexed.', 'jetpack' ),
79
			),
80
			'ERROR'       => array(
81
				'action' => __( 'Refresh Status', 'jetpack' ),
82
				'status' => __( 'Status unknown.', 'jetpack' ),
83
			),
84
			'ERROR:LARGE' => array(
85
				'action' => __( 'Refresh Status', 'jetpack' ),
86
				'status' => __( 'This site is too large, please contact Jetpack support to sync.', 'jetpack' ),
87
			),
88
		) );
89
90
		wp_enqueue_script(
91
			'jetpack_sync_reindex_control',
92
			plugins_url( '_inc/jquery.jetpack-sync.js', JETPACK__PLUGIN_FILE ),
93
			array( 'jquery' ),
94
			JETPACK__VERSION
95
		);
96
97
		$template = <<<EOT
98
			<p class="jetpack_sync_reindex_control" id="jetpack_sync_reindex_control" data-strings="%s">
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
		return sprintf(
105
			$template,
106
			esc_attr( $strings ),
107
			esc_attr__( 'Refresh Status', 'jetpack' )
108
		);
109
	}
110
111
	private function _get_post_count_local() {
112
		global $wpdb;
113
114
		return (int) $wpdb->get_var(
115
			"SELECT count(*)
116
				FROM {$wpdb->posts}
117
				WHERE post_status = 'publish' AND post_password = ''"
118
		);
119
	}
120
121
	private function _get_post_count_cloud() {
122
		$blog_id = Jetpack::init()->get_option( 'id' );
123
124
		$body = array(
125
			'size' => 1,
126
		);
127
128
		$response = wp_remote_post(
129
			"https://public-api.wordpress.com/rest/v1/sites/$blog_id/search",
130
			array(
131
				'timeout'    => 10,
132
				'user-agent' => 'jetpack_related_posts',
133
				'sslverify'  => true,
134
				'body'       => $body,
135
			)
136
		);
137
138
		if ( is_wp_error( $response ) ) {
139
			return 0;
140
		}
141
142
		$results = json_decode( wp_remote_retrieve_body( $response ), true );
143
144
		return (int) $results['results']['total'];
145
	}
146
147 View Code Duplication
	static function sync_reindex_trigger() {
148
		if ( Jetpack::current_user_is_connection_owner() && current_user_can( 'manage_options' ) ) {
149
			echo json_encode( self::reindex_trigger() );
0 ignored issues
show
Bug introduced by
The method reindex_trigger() does not seem to exist on object<Jetpack_Sync_Reindex>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
		} else {
151
			echo '{"status":"ERROR"}';
152
		}
153
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method sync_reindex_trigger() 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...
154
	}
155
156 View Code Duplication
	static function sync_reindex_status(){
157
		if ( Jetpack::current_user_is_connection_owner() && current_user_can( 'manage_options' ) ) {
158
			echo json_encode( self::reindex_status() );
0 ignored issues
show
Bug introduced by
The method reindex_status() does not seem to exist on object<Jetpack_Sync_Reindex>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
159
		} else {
160
			echo '{"status":"ERROR"}';
161
		}
162
		exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method sync_reindex_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...
163
	}
164
}