| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | function add_site_meta( $site_id, $meta_key, $meta_value, $unique = false ) { |
|---|
| 4 | // Bail if site meta table is not installed. |
|---|
| 5 | if ( get_option( 'db_version' ) < 34370 ) { |
|---|
| 6 | return false; |
|---|
| 7 | } |
|---|
| 8 | |
|---|
| 9 | $added = add_metadata( 'blog', $site_id, $meta_key, $meta_value, $unique ); |
|---|
| 10 | |
|---|
| 11 | // Bust site query cache. |
|---|
| 12 | if ( $added ) { |
|---|
| 13 | wp_cache_set( 'last_changed', microtime(), 'sites' ); |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | return $added; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | function delete_site_meta( $site_id, $meta_key, $meta_value = '' ) { |
|---|
| 21 | // Bail if site meta table is not installed. |
|---|
| 22 | if ( get_option( 'db_version' ) < 34370 ) { |
|---|
| 23 | return false; |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | $deleted = delete_metadata( 'blog', $site_id, $meta_key, $meta_value ); |
|---|
| 27 | |
|---|
| 28 | // Bust site query cache. |
|---|
| 29 | if ( $deleted ) { |
|---|
| 30 | wp_cache_set( 'last_changed', microtime(), 'sites' ); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | return $deleted; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | function get_site_meta( $site_id, $key = '', $single = false ) { |
|---|
| 37 | // Bail if site meta table is not installed. |
|---|
| 38 | if ( get_option( 'db_version' ) < 34370 ) { |
|---|
| 39 | return false; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | // backwards compat |
|---|
| 43 | $pre = apply_filters( "pre_option_{$key}", false, $key ); |
|---|
| 44 | if ( false !== $pre ){ |
|---|
| 45 | return $pre; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | $value = get_metadata( 'blog', $site_id, $key, $single ); |
|---|
| 49 | //backwards compat |
|---|
| 50 | return apply_filters( "option_{$key}", maybe_unserialize( $value ), $key ); |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | function update_site_meta( $site_id, $meta_key, $meta_value, $prev_value = '' ) { |
|---|
| 54 | // Bail if site meta table is not installed. |
|---|
| 55 | if ( get_option( 'db_version' ) < 34370 ) { |
|---|
| 56 | return false; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | $updated = update_metadata( 'blog', $site_id, $meta_key, $meta_value, $prev_value ); |
|---|
| 60 | |
|---|
| 61 | // Bust site query cache. |
|---|
| 62 | if ( $updated ) { |
|---|
| 63 | wp_cache_set( 'last_changed', microtime(), 'sites' ); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | return $updated; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | function list_of_site_meta_keys(){ |
|---|
| 70 | $list = array( 'blogname','siteurl','post_count','home','allowedthemes','blog_public','WPLANG','blogdescription'); |
|---|
| 71 | return apply_filters('list_of_site_meta_keys', $list); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | function sync_update_option( $option, $old_value, $value ){ |
|---|
| 75 | $list = list_of_site_meta_keys(); |
|---|
| 76 | if(!in_array( $option, $list) ){ |
|---|
| 77 | return; |
|---|
| 78 | } |
|---|
| 79 | $site_id = get_current_blog_id(); |
|---|
| 80 | update_site_meta( $site_id, $option, $value, $old_value ); |
|---|
| 81 | return; |
|---|
| 82 | } |
|---|
| 83 | add_action( 'updated_option', 'sync_update_option', 99, 3 ); |
|---|
| 84 | |
|---|
| 85 | function sync_add_option( $option, $value ){ |
|---|
| 86 | $list = list_of_site_meta_keys(); |
|---|
| 87 | if(!in_array( $option, $list) ){ |
|---|
| 88 | return; |
|---|
| 89 | } |
|---|
| 90 | $site_id = get_current_blog_id(); |
|---|
| 91 | add_site_meta( $site_id, $option, $value ); |
|---|
| 92 | return; |
|---|
| 93 | } |
|---|
| 94 | add_action( 'added_option', 'sync_add_option', 99, 2 ); |
|---|
| 95 | |
|---|
| 96 | function sync_delete_option( $option ){ |
|---|
| 97 | $list = list_of_site_meta_keys(); |
|---|
| 98 | if(!in_array( $option, $list) ){ |
|---|
| 99 | return; |
|---|
| 100 | } |
|---|
| 101 | $site_id = get_current_blog_id(); |
|---|
| 102 | delete_site_meta( $site_id, $option ); |
|---|
| 103 | return; |
|---|
| 104 | } |
|---|
| 105 | add_action( 'deleted_option', 'sync_delete_option', 99, 1 ); |
|---|