Completed
Push — fix/full-sync-without-queue ( 8aaba8 )
by
unknown
20:00 queued 10:21
created

Jetpack_Full_Sync::write_option()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 15
nc 2
nop 2
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Full_Sync {
4
5
	// writes out a config item for each enabled module containing total items & input config
6
	static function start( $modules = null ) {
7
		// todo detect if config already set and/or sync already in progress
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...
8
9
		$config = array();
10
11
		if ( ! is_array( $modules ) ) {
12
			$modules = array();
13
		}
14
15 View Code Duplication
		if ( isset( $modules['users'] ) && 'initial' === $modules['users'] ) {
16
			$user_module = Jetpack_Sync_Modules::get_module( 'users' );
17
			$modules['users'] = $user_module->get_initial_sync_user_config();
18
		}
19
20
		// by default, all modules are fully enabled
21
		if ( count( $modules ) === 0 ) {
22
			$default_module_config = true;
23
		} else {
24
			$default_module_config = false;
25
		}
26
27
		// set default configuration, calculate totals, and save configuration if totals > 0
28
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
29
			$module_name = $module->name();
30
			if ( ! isset( $modules[ $module_name ] ) ) {
31
				$modules[ $module_name ] = $default_module_config;
32
			}
33
34
			// check if this module is enabled
35
			if ( ! ( $module_config = $modules[ $module_name ] ) ) {
36
				continue;
37
			}
38
39
			$total_items = $module->estimate_full_sync_actions( $module_config );
40
41
			if ( ! is_null( $total_items ) && $total_items > 0 ) {
42
				$config[ $module_name ] = array(
43
					'total' => $total_items,
44
					'config' => $module_config,
45
				);
46
			}
47
		}
48
49
		self::set_config( $config );
50
51
		// now let's set an initial status
52
		$status = array();
53
		foreach( $config as $module => $params ) {
54
			$status[ $module ] = array();
55
		}
56
57
		self::set_status( $status );
58
	}
59
60
	static function do_sync() {
61
		$config = self::get_config();
62
		$status = self::get_status();
63
64
		error_log("Got config: ".print_r($config,1));
65
		
66
		foreach ( Jetpack_Sync_Modules::get_modules() as $module ) {
67
			if ( isset( $status[ $module->name() ][ 'finished' ] ) ) {
68
				continue;
69
			}
70
71
			// do work
72
			Jetpack_Sync_Settings::set_is_sending( true );
73
			$processed_item_ids = apply_filters( 'jetpack_sync_send_data', $items_to_send, $this->codec->name(), microtime( true ), $queue->id );
0 ignored issues
show
Bug introduced by
The property codec does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The variable $items_to_send does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $this does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $queue does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Unused Code introduced by
$processed_item_ids is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
74
			Jetpack_Sync_Settings::set_is_sending( false );
75
		}
76
		// get state
77
		
78
		// continue sync
79
		// save state
80
		// return result
81
	}
82
83
	private static function set_config( $config ) {
84
		self::write_option( 'jetpack_sync_full_config', $config );
85
	}
86
87
	private static function get_config() {
88
		return self::read_option( 'jetpack_sync_full_config' );
89
	}
90
91
	private static function set_status( $status ) {
92
		self::write_option( 'jetpack_sync_full_status', $status );
93
	}
94
95
	private static function get_status() {
96
		return self::read_option( 'jetpack_sync_full_status' );
97
	}
98
99
	private static function write_option( $name, $value ) {
100
		// we write our own option updating code to bypass filters/caching/etc on set_option/get_option
101
		global $wpdb;
102
103
		$serialized_value = maybe_serialize( $value );
104
105
		// try updating, if no update then insert
106
		$updated_num = $wpdb->query(
107
			$wpdb->prepare(
108
				"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", 
109
				$serialized_value,
110
				$name
111
			)
112
		);
113
114
		if ( ! $updated_num ) {
115
			$updated_num = $wpdb->query(
116
				$wpdb->prepare(
117
					"INSERT INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )", 
118
					$name,
119
					$serialized_value
120
				)
121
			);
122
		}
123
124
		return $updated_num;
125
	}
126
127
	private static function read_option( $name ) {
128
		global $wpdb;
129
130
		$value = $wpdb->get_var( 
131
			$wpdb->prepare(
132
				"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", 
133
				$name
134
			)
135
		);
136
137
		return maybe_unserialize( $value );
138
	}
139
140
	// private function set_checkout_id( $checkout_id ) {
141
	// 	global $wpdb;
142
143
	// 	$expires = time() + Jetpack_Sync_Defaults::$default_sync_queue_lock_timeout;
144
	// 	$updated_num = $wpdb->query(
145
	// 		$wpdb->prepare(
146
	// 			"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", 
147
	// 			"$checkout_id:$expires",
148
	// 			self::get_lock_option_name()
149
	// 		)
150
	// 	);
151
152
	// 	if ( ! $updated_num ) {
153
	// 		$updated_num = $wpdb->query(
154
	// 			$wpdb->prepare(
155
	// 				"INSERT INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, 'no' )", 
156
	// 				self::get_lock_option_name(),
157
	// 				"$checkout_id:$expires"
158
	// 			)
159
	// 		);
160
	// 	}
161
162
	// 	return $updated_num;
163
	// }
164
165
	// private function delete_checkout_id() {
166
	// 	global $wpdb;
167
	// 	// rather than delete, which causes fragmentation, we update in place
168
	// 	return $wpdb->query(
169
	// 		$wpdb->prepare( 
170
	// 			"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", 
171
	// 			"0:0",
172
	// 			self::get_lock_option_name() 
173
	// 		) 
174
	// 	);
175
	// }
176
}