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 |
||
8 | class Jetpack_Sync_Options { |
||
9 | |||
10 | static function delete_option( $name ) { |
||
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 |
||
|
|||
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 read_option( $name, $default = null ) { |
|
61 | |||
62 | } |