Completed
Push — branch-4.4 ( ced99a...8c5fae )
by
unknown
363:21 queued 355:47
created

Jetpack_Sync_Options::delete_option()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Simple class to read/write to the options table, bypassing 
5
 * problematic caching with get_option/set_option
6
 **/
7
8
class Jetpack_Sync_Options {
9
10
	static function delete_option( $name ) {
11
		global $wpdb;
12
		$wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $name ) );
13
	}
14
15
	static function update_option( $name, $value, $autoload = false ) {
16
17
		$autoload_value = $autoload ? 'yes' : 'no';
18
19
		// we write our own option updating code to bypass filters/caching/etc on set_option/get_option
20
		global $wpdb;
21
		$serialized_value = maybe_serialize( $value );
22
		// try updating, if no update then insert
23
		// TODO: try to deal with the fact that unchanged values can return updated_num = 0
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...
24
		// below we used "insert ignore" to at least suppress the resulting error
25
		$updated_num = $wpdb->query(
26
			$wpdb->prepare(
27
				"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", 
28
				$serialized_value,
29
				$name
30
			)
31
		);
32
33
		if ( ! $updated_num ) {
34
			$updated_num = $wpdb->query(
35
				$wpdb->prepare(
36
					"INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, '$autoload_value' )", 
37
					$name,
38
					$serialized_value
39
				)
40
			);
41
		}
42
		return $updated_num;
43
	}
44
45 View Code Duplication
	static function get_option( $name, $default = null ) {
46
		global $wpdb;
47
		$value = $wpdb->get_var( 
48
			$wpdb->prepare(
49
				"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", 
50
				$name
51
			)
52
		);
53
		$value = maybe_unserialize( $value );
54
55
		if ( $value === null && $default !== null ) {
56
			return $default;
57
		}
58
59
		return $value;
60
	}
61
	
62
}