Ticket #21270: 21270.3.diff

File 21270.3.diff, 7.1 KB (added by ryan, 10 months ago)
Line 
1Index: wp-includes/ms-blogs.php
2===================================================================
3--- wp-includes/ms-blogs.php    (revision 21353)
4+++ wp-includes/ms-blogs.php    (working copy)
5@@ -307,114 +307,86 @@
6 }
7 
8 /**
9- * Retrieve option value based on setting name and blog_id.
10+ * Retrieve option value for a given blog id based on name of option.
11  *
12  * If the option does not exist or does not have a value, then the return value
13  * will be false. This is useful to check whether you need to install an option
14  * and is commonly used during installation of plugin options and to test
15  * whether upgrading is required.
16  *
17- * There is a filter called 'blog_option_$option' with the $option being
18- * replaced with the option name. The filter takes two parameters. $value and
19- * $blog_id. It returns $value.
20- * The 'option_$option' filter in get_option() is not called.
21+ * If the option was serialized then it will be unserialized when it is returned.
22  *
23  * @since MU
24- * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
25  *
26- * @param int $blog_id Optional. Blog ID, can be null to refer to the current blog.
27- * @param string $setting Name of option to retrieve. Should already be SQL-escaped.
28- * @param string $default (optional) Default value returned if option not found.
29+ * @param int $id A blog ID. Can be null to refer to the current blog.
30+ * @param string $option Name of option to retrieve. Expected to not be SQL-escaped.
31+ * @param mixed $default Optional. Default value to return if the option does not exist.
32  * @return mixed Value set for the option.
33  */
34-function get_blog_option( $blog_id, $setting, $default = false ) {
35-       global $wpdb;
36+function get_blog_option( $id, $option, $default = false ) {
37+       $id = (int) $id;
38 
39-       if ( null === $blog_id )
40-               $blog_id = $wpdb->blogid;
41+       if ( $id == get_current_blog_id() )
42+               return get_option( $option, $default );
43 
44-       $key = $blog_id . '-' . $setting . '-blog_option';
45-       $value = wp_cache_get( $key, 'site-options' );
46-       if ( $value == null ) {
47-               if ( $blog_id == $wpdb->blogid ) {
48-                       $value = get_option( $setting, $default );
49-                       $notoptions = wp_cache_get( 'notoptions', 'options' );
50-                       if ( isset( $notoptions[$setting] ) ) {
51-                               wp_cache_set( $key, 'noop', 'site-options' );
52-                               $value = $default;
53-                       } elseif ( $value == false ) {
54-                               wp_cache_set( $key, 'falsevalue', 'site-options' );
55-                       } else {
56-                               wp_cache_set( $key, $value, 'site-options' );
57-                       }
58-                       return apply_filters( 'blog_option_' . $setting, $value, $blog_id );
59-               } else {
60-                       $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
61-                       $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
62-                       if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
63-                               $value = $row->option_value;
64-                               if ( $value == false )
65-                                       wp_cache_set( $key, 'falsevalue', 'site-options' );
66-                               else
67-                                       wp_cache_set( $key, $value, 'site-options' );
68-                       } else { // option does not exist, so we must cache its non-existence
69-                               wp_cache_set( $key, 'noop', 'site-options' );
70-                               $value = $default;
71-                       }
72-               }
73-       } elseif ( $value == 'noop' ) {
74-               $value = $default;
75-       } elseif ( $value == 'falsevalue' ) {
76-               $value = false;
77-       }
78-       // If home is not set use siteurl.
79-       if ( 'home' == $setting && '' == $value )
80-               return get_blog_option( $blog_id, 'siteurl' );
81+       switch_to_blog( $id );
82+       $option = get_option( $option, $default );
83+       restore_current_blog();
84 
85-       if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
86-               $value = untrailingslashit( $value );
87-
88-       return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
89+       return $option;
90 }
91 
92 /**
93- * Add an option for a particular blog.
94+ * Add a new option for a given blog id.
95  *
96+ * You do not need to serialize values. If the value needs to be serialized, then
97+ * it will be serialized before it is inserted into the database. Remember,
98+ * resources can not be serialized or added as an option.
99+ *
100+ * You can create options without values and then update the values later.
101+ * Existing options will not be updated and checks are performed to ensure that you
102+ * aren't adding a protected WordPress option. Care should be taken to not name
103+ * options the same as the ones which are protected.
104+ *
105  * @since MU
106  *
107- * @param int $id The blog id
108- * @param string $key The option key
109- * @param mixed $value The option value
110- * @return bool True on success, false on failure.
111+ * @param int $id A blog ID. Can be null to refer to the current blog.
112+ * @param string $option Name of option to add. Expected to not be SQL-escaped.
113+ * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped.
114+ * @return bool False if option was not added and true if option was added.
115  */
116-function add_blog_option( $id, $key, $value ) {
117+function add_blog_option( $id, $option, $value ) {
118        $id = (int) $id;
119 
120-       switch_to_blog($id);
121+       if ( $id == get_current_blog_id() )
122+               return add_option( $option, $value );
123+
124+       switch_to_blog( $id );
125        $return = add_option( $key, $value );
126        restore_current_blog();
127-       if ( $return )
128-               wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options' );
129+
130        return $return;
131 }
132 
133 /**
134- * Delete an option for a particular blog.
135+ * Removes option by name for a given blog id. Prevents removal of protected WordPress options.
136  *
137  * @since MU
138  *
139- * @param int $id The blog id
140- * @param string $key The option key
141- * @return bool True on success, false on failure.
142+ * @param int $id A blog ID. Can be null to refer to the current blog.
143+ * @param string $option Name of option to remove. Expected to not be SQL-escaped.
144+ * @return bool True, if option is successfully deleted. False on failure.
145  */
146-function delete_blog_option( $id, $key ) {
147+function delete_blog_option( $id, $option ) {
148        $id = (int) $id;
149 
150-       switch_to_blog($id);
151-       $return = delete_option( $key );
152+       if ( $id == get_current_blog_id() )
153+               return delete_option( $option );
154+
155+       switch_to_blog( $id );
156+       $return = delete_option( $option );
157        restore_current_blog();
158-       if ( $return )
159-               wp_cache_set( $id . '-' . $key . '-blog_option', '', 'site-options' );
160+
161        return $return;
162 }
163 
164@@ -424,24 +396,25 @@
165  * @since MU
166  *
167  * @param int $id The blog id
168- * @param string $key The option key
169+ * @param string $option The option key
170  * @param mixed $value The option value
171  * @return bool True on success, false on failrue.
172  */
173-function update_blog_option( $id, $key, $value, $deprecated = null ) {
174+function update_blog_option( $id, $option, $value, $deprecated = null ) {
175        $id = (int) $id;
176 
177        if ( null !== $deprecated  )
178                _deprecated_argument( __FUNCTION__, '3.1' );
179 
180-       switch_to_blog($id);
181-       $return = update_option( $key, $value );
182+       if ( $id == get_current_blog_id() )
183+               return update_option( $option, $value );
184+
185+       switch_to_blog( $id );
186+       $return = update_option( $option, $value );
187        restore_current_blog();
188 
189        refresh_blog_details( $id );
190 
191-       if ( $return )
192-               wp_cache_set( $id . '-' . $key . '-blog_option', $value, 'site-options');
193        return $return;
194 }
195