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

Jetpack_Sync_Reindex   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 162
Duplicated Lines 9.88 %

Coupling/Cohesion

Components 1
Dependencies 3
Metric Value
dl 16
loc 162
rs 10
wmc 20
lcom 1
cbo 3

10 Methods

Rating   Name   Duplication   Size   Complexity  
A reindex_if_needed() 0 5 2
A reindex_needed() 0 3 1
A reindex_trigger() 0 20 2
B reindex_status() 0 24 4
B reindex_ui() 0 44 1
A _get_post_count_local() 0 9 1
B _get_post_count_cloud() 0 25 2
A sync_reindex_trigger() 8 8 3
A sync_reindex_status() 8 8 3
A init() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
class Jetpack_Sync_Reindex {
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() ) {
12
			self::reindex_trigger();
13
		}
14
	}
15
16
	static function reindex_needed() {
17
		return ( self::_get_post_count_local() != self::_get_post_count_cloud() );
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
	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() );
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() );
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
}