class.jetpack-sync-options.php
| 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 |
| 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 | 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 | } |