Ticket #28290: 28290.2.diff
| File 28290.2.diff, 18.8 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/option.php
972 972 } 973 973 974 974 /** 975 * Retrieve siteoption value based on name of option.975 * Retrieve a network option value based on name of option. 976 976 * 977 977 * @since 2.8.0 978 * @since 4.4.0 Modified into wrapper for get_network_option() 979 * 980 * @see get_network_option() 981 * 982 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. 983 * @param mixed $default Optional value to return if option doesn't exist. Default false. 984 * @param bool $deprecated Whether to use cache. Multisite only. Always set to true. 985 * @return mixed Value set for the option. 986 */ 987 function get_site_option( $option, $default = false, $deprecated = true ) { 988 return get_network_option( $option, $default ); 989 } 990 991 /** 992 * Add a new network option. 993 * 994 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case. 995 * 996 * @since 2.8.0 997 * @since 4.4.0 Modified into wrapper for add_network_option() 998 * 999 * @see add_network_option() 1000 * 1001 * @param string $option Name of option to add. Expected to not be SQL-escaped. 1002 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped. 1003 * @return bool False if the option was not added. True if the option was added. 1004 */ 1005 function add_site_option( $option, $value ) { 1006 return add_network_option( $option, $value ); 1007 } 1008 1009 /** 1010 * Removes a network option by name. 1011 * 1012 * @since 2.8.0 1013 * @since 4.4.0 Modified into wrapper for delete_network_option() 1014 * 1015 * @see delete_network_option() 1016 * 1017 * @param string $option Name of option to remove. Expected to not be SQL-escaped. 1018 * @return bool True, if succeed. False, if failure. 1019 */ 1020 function delete_site_option( $option ) { 1021 return delete_network_option( $option ); 1022 } 1023 1024 /** 1025 * Update the value of a network option that was already added. 1026 * 1027 * @since 2.8.0 1028 * @since 4.4.0 Modified into wrapper for update_network_option() 1029 * 1030 * @see update_network_option() 1031 * 1032 * @param string $option Name of option. Expected to not be SQL-escaped. 1033 * @param mixed $value Option value. Expected to not be SQL-escaped. 1034 * @return bool False if value was not updated. True if value was updated. 1035 */ 1036 function update_site_option( $option, $value ) { 1037 return update_network_option( $option, $value ); 1038 } 1039 1040 /** 1041 * Retrieve a network's option value based on name of option. 1042 * 1043 * @since 4.4.0 978 1044 * 979 1045 * @see get_option() 980 1046 * 981 1047 * @global wpdb $wpdb 982 1048 * 983 * @param string $optionName of option to retrieve. Expected to not be SQL-escaped.984 * @param mixed $default Optional value to return ifoption doesn't exist. Default false.985 * @param bool $use_cache Whether to use cache. Multisite only. Default true.1049 * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. 1050 * @param mixed $default Optional. Value to return if the option doesn't exist. Default false. 1051 * @param int|bool $network_id Optional. ID of the network. Defaults to current network ID. 986 1052 * @return mixed Value set for the option. 987 1053 */ 988 function get_site_option( $option, $default = false, $use_cache = true ) { 989 global $wpdb; 1054 function get_network_option( $option, $default = false, $network_id = false ) { 1055 global $wpdb, $current_site; 1056 1057 $network_id = (int) $network_id; 1058 1059 // Fallback to the current network if a network ID is not specified. 1060 if ( ! $network_id && is_multisite() ) { 1061 $network_id = $current_site->id; 1062 } 990 1063 991 1064 /** 992 * Filter an existing siteoption before it is retrieved.1065 * Filter an existing network option before it is retrieved. 993 1066 * 994 1067 * The dynamic portion of the hook name, `$option`, refers to the option name. 995 1068 * … … 1005 1078 */ 1006 1079 $pre = apply_filters( 'pre_site_option_' . $option, false, $option ); 1007 1080 1008 if ( false !== $pre ) 1009 return $pre; 1081 if ( false !== $pre ) { 1082 return $pre; 1083 } 1010 1084 1011 1085 // prevent non-existent options from triggering multiple queries 1012 $notoptions_key = " {$wpdb->siteid}:notoptions";1086 $notoptions_key = "$network_id:notoptions"; 1013 1087 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 1014 1088 1015 if ( isset( $notoptions[ $option] ) ) {1089 if ( isset( $notoptions[ $option ] ) ) { 1016 1090 1017 1091 /** 1018 * Filter a specific default siteoption.1092 * Filter a specific default network option. 1019 1093 * 1020 1094 * The dynamic portion of the hook name, `$option`, refers to the option name. 1021 1095 * … … 1030 1104 } 1031 1105 1032 1106 if ( ! is_multisite() ) { 1033 1034 1107 /** This filter is documented in wp-includes/option.php */ 1035 1108 $default = apply_filters( 'default_site_option_' . $option, $default, $option ); 1036 $value = get_option( $option, $default);1109 $value = get_option( $option, $default ); 1037 1110 } else { 1038 $cache_key = "{$wpdb->siteid}:$option"; 1039 if ( $use_cache ) 1040 $value = wp_cache_get($cache_key, 'site-options'); 1111 $cache_key = "$network_id:$option"; 1112 $value = wp_cache_get( $cache_key, 'site-options' ); 1041 1113 1042 if ( ! isset($value) || (false === $value)) {1043 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $wpdb->siteid ) );1114 if ( ! isset( $value ) || false === $value ) { 1115 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 1044 1116 1045 1117 // Has to be get_row instead of get_var because of funkiness with 0, false, null values 1046 1118 if ( is_object( $row ) ) { … … 1049 1121 wp_cache_set( $cache_key, $value, 'site-options' ); 1050 1122 } else { 1051 1123 if ( ! is_array( $notoptions ) ) { 1052 $notoptions = array();1124 $notoptions = array(); 1053 1125 } 1054 $notoptions[ $option] = true;1126 $notoptions[ $option ] = true; 1055 1127 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1056 1128 1057 1129 /** This filter is documented in wp-includes/option.php */ … … 1061 1133 } 1062 1134 1063 1135 /** 1064 * Filter the value of an existing siteoption.1136 * Filter the value of an existing network option. 1065 1137 * 1066 1138 * The dynamic portion of the hook name, `$option`, refers to the option name. 1067 1139 * … … 1069 1141 * @since 3.0.0 1070 1142 * @since 4.4.0 The `$option` parameter was added 1071 1143 * 1072 * @param mixed $value Value of siteoption.1144 * @param mixed $value Value of network option. 1073 1145 * @param string $option Option name. 1074 1146 */ 1075 1147 return apply_filters( 'site_option_' . $option, $value, $option ); 1076 1148 } 1077 1149 1078 1150 /** 1079 * Add a new siteoption.1151 * Add a new network option. 1080 1152 * 1081 * Existing options will not be updated. Note that prior to 3.3 this wasn't the case.1153 * Existing options will not be updated. 1082 1154 * 1083 * @since 2.8.01155 * @since 4.4.0 1084 1156 * 1085 1157 * @see add_option() 1086 1158 * 1087 1159 * @global wpdb $wpdb 1088 1160 * 1089 * @param string $option Name of option to add. Expected to not be SQL-escaped. 1090 * @param mixed $value Optional. Option value, can be anything. Expected to not be SQL-escaped. 1161 * @param string $option Name of option to add. Expected to not be SQL-escaped. 1162 * @param mixed $value Option value, can be anything. Expected to not be SQL-escaped. 1163 * @param int|bool $network_id Optional. ID of the network. Defaults to current network ID. 1091 1164 * @return bool False if option was not added and true if option was added. 1092 1165 */ 1093 function add_site_option( $option, $value ) { 1094 global $wpdb; 1166 function add_network_option( $option, $value, $network_id = false ) { 1167 global $wpdb, $current_site; 1168 1169 $network_id = (int) $network_id; 1170 1171 // Fallback to the current network if a network ID is not specified. 1172 if ( ! $network_id && is_multisite() ) { 1173 $network_id = $current_site->id; 1174 } 1095 1175 1096 1176 wp_protect_special_option( $option ); 1097 1177 1098 1178 /** 1099 * Filter the value of a specific siteoption before it is added.1179 * Filter the value of a specific network option before it is added. 1100 1180 * 1101 1181 * The dynamic portion of the hook name, `$option`, refers to the option name. 1102 1182 * … … 1104 1184 * @since 3.0.0 1105 1185 * @since 4.4.0 The `$option` parameter was added 1106 1186 * 1107 * @param mixed $value Value of siteoption.1187 * @param mixed $value Value of network option. 1108 1188 * @param string $option Option name. 1109 1189 */ 1110 1190 $value = apply_filters( 'pre_add_site_option_' . $option, $value, $option ); 1111 1191 1112 $notoptions_key = " {$wpdb->siteid}:notoptions";1192 $notoptions_key = "$network_id:notoptions"; 1113 1193 1114 if ( ! is_multisite() ) {1194 if ( ! is_multisite() ) { 1115 1195 $result = add_option( $option, $value ); 1116 1196 } else { 1117 $cache_key = " {$wpdb->siteid}:$option";1197 $cache_key = "$network_id:$option"; 1118 1198 1119 1199 // Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query 1120 1200 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 1121 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option] ) )1122 if ( false !== get_ site_option( $option ) )1201 if ( ! is_array( $notoptions ) || ! isset( $notoptions[ $option ] ) ) { 1202 if ( false !== get_network_option( $option, false, $network_id ) ) { 1123 1203 return false; 1204 } 1205 } 1124 1206 1125 1207 $value = sanitize_option( $option, $value ); 1126 1208 1127 1209 $serialized_value = maybe_serialize( $value ); 1128 $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => $wpdb->siteid, 'meta_key'=> $option, 'meta_value' => $serialized_value ) );1210 $result = $wpdb->insert( $wpdb->sitemeta, array( 'site_id' => $network_id, 'meta_key' => $option, 'meta_value' => $serialized_value ) ); 1129 1211 1130 if ( ! $result ) 1212 if ( ! $result ) { 1131 1213 return false; 1214 } 1132 1215 1133 1216 wp_cache_set( $cache_key, $value, 'site-options' ); 1134 1217 1135 1218 // This option exists now 1136 1219 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); // yes, again... we need it to be fresh 1137 if ( is_array( $notoptions ) && isset( $notoptions[ $option] ) ) {1138 unset( $notoptions[ $option] );1220 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 1221 unset( $notoptions[ $option ] ); 1139 1222 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1140 1223 } 1141 1224 } … … 1143 1226 if ( $result ) { 1144 1227 1145 1228 /** 1146 * Fires after a specific siteoption has been successfully added.1229 * Fires after a specific network option has been successfully added. 1147 1230 * 1148 1231 * The dynamic portion of the hook name, `$option`, refers to the option name. 1149 1232 * 1150 1233 * @since 2.9.0 As "add_site_option_{$key}" 1151 1234 * @since 3.0.0 1152 1235 * 1153 * @param string $option Name of siteoption.1154 * @param mixed $value Value of siteoption.1236 * @param string $option Name of the network option. 1237 * @param mixed $value Value of the network option. 1155 1238 */ 1156 do_action( "add_site_option_{$option}", $option, $value );1239 do_action( 'add_site_option_' . $option, $option, $value ); 1157 1240 1158 1241 /** 1159 * Fires after a siteoption has been successfully added.1242 * Fires after a network option has been successfully added. 1160 1243 * 1161 1244 * @since 3.0.0 1162 1245 * 1163 * @param string $option Name of siteoption.1164 * @param mixed $value Value of siteoption.1246 * @param string $option Name of the network option. 1247 * @param mixed $value Value of the network option. 1165 1248 */ 1166 do_action( "add_site_option", $option, $value );1249 do_action( 'add_site_option', $option, $value ); 1167 1250 1168 1251 return true; 1169 1252 } 1253 1170 1254 return false; 1171 1255 } 1172 1256 1173 1257 /** 1174 * Removes siteoption by name.1258 * Removes network option by name. 1175 1259 * 1176 * @since 2.8.01260 * @since 4.4.0 1177 1261 * 1178 1262 * @see delete_option() 1179 1263 * 1180 1264 * @global wpdb $wpdb 1181 1265 * 1182 * @param string $option Name of option to remove. Expected to not be SQL-escaped. 1266 * @param string $option Name of option to remove. Expected to not be SQL-escaped. 1267 * @param int|bool $network_id Optional. ID of the network. Defaults to current network ID. 1183 1268 * @return bool True, if succeed. False, if failure. 1184 1269 */ 1185 function delete_site_option( $option ) { 1186 global $wpdb; 1270 function delete_network_option( $option, $network_id = false ) { 1271 global $wpdb, $current_site; 1272 1273 $network_id = (int) $network_id; 1274 1275 // Fallback to the current network if a network ID is not specified. 1276 if ( ! $network_id && is_multisite() ) { 1277 $network_id = $current_site->id; 1278 } 1187 1279 1188 1280 /** 1189 * Fires immediately before a specific siteoption is deleted.1281 * Fires immediately before a specific network option is deleted. 1190 1282 * 1191 1283 * The dynamic portion of the hook name, `$option`, refers to the option name. 1192 1284 * … … 1197 1289 */ 1198 1290 do_action( 'pre_delete_site_option_' . $option, $option ); 1199 1291 1200 if ( ! is_multisite() ) {1292 if ( ! is_multisite() ) { 1201 1293 $result = delete_option( $option ); 1202 1294 } else { 1203 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $ wpdb->siteid ) );1204 if ( is_null( $row ) || ! $row->meta_id )1295 $row = $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->sitemeta} WHERE meta_key = %s AND site_id = %d", $option, $network_id ) ); 1296 if ( is_null( $row ) || ! $row->meta_id ) { 1205 1297 return false; 1206 $cache_key = "{$wpdb->siteid}:$option"; 1298 } 1299 $cache_key = "$network_id:$option"; 1207 1300 wp_cache_delete( $cache_key, 'site-options' ); 1208 1301 1209 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $ wpdb->siteid ) );1302 $result = $wpdb->delete( $wpdb->sitemeta, array( 'meta_key' => $option, 'site_id' => $network_id ) ); 1210 1303 } 1211 1304 1212 1305 if ( $result ) { 1213 1306 1214 1307 /** 1215 * Fires after a specific siteoption has been deleted.1308 * Fires after a specific network option has been deleted. 1216 1309 * 1217 1310 * The dynamic portion of the hook name, `$option`, refers to the option name. 1218 1311 * 1219 1312 * @since 2.9.0 As "delete_site_option_{$key}" 1220 1313 * @since 3.0.0 1221 1314 * 1222 * @param string $option Name of the siteoption.1315 * @param string $option Name of the network option. 1223 1316 */ 1224 do_action( "delete_site_option_{$option}", $option );1317 do_action( 'delete_site_option_' . $option, $option ); 1225 1318 1226 1319 /** 1227 * Fires after a siteoption has been deleted.1320 * Fires after a network option has been deleted. 1228 1321 * 1229 1322 * @since 3.0.0 1230 1323 * 1231 * @param string $option Name of the siteoption.1324 * @param string $option Name of the network option. 1232 1325 */ 1233 do_action( "delete_site_option", $option );1326 do_action( 'delete_site_option', $option ); 1234 1327 1235 1328 return true; 1236 1329 } 1330 1237 1331 return false; 1238 1332 } 1239 1333 1240 1334 /** 1241 * Update the value of a siteoption that was already added.1335 * Update the value of a network option that was already added. 1242 1336 * 1243 * @since 2.8.01337 * @since 4.4.0 1244 1338 * 1245 1339 * @see update_option() 1246 1340 * 1247 1341 * @global wpdb $wpdb 1248 1342 * 1249 * @param string $option Name of option. Expected to not be SQL-escaped. 1250 * @param mixed $value Option value. Expected to not be SQL-escaped. 1343 * @param string $option Name of option. Expected to not be SQL-escaped. 1344 * @param mixed $value Option value. Expected to not be SQL-escaped. 1345 * @param int|bool $network_id Optional. ID of the network. Defaults to current network ID. 1251 1346 * @return bool False if value was not updated and true if value was updated. 1252 1347 */ 1253 function update_site_option( $option, $value ) { 1254 global $wpdb; 1348 function update_network_option( $option, $value, $network_id = false ) { 1349 global $wpdb, $current_site; 1350 1351 $network_id = (int) $network_id; 1352 1353 // Fallback to the current network if a network ID is not specified. 1354 if ( ! $network_id && is_multisite() ) { 1355 $network_id = $current_site->id; 1356 } 1255 1357 1256 1358 wp_protect_special_option( $option ); 1257 1359 1258 $old_value = get_ site_option( $option);1360 $old_value = get_network_option( $option, false, $network_id ); 1259 1361 1260 1362 /** 1261 * Filter a specific siteoption before its value is updated.1363 * Filter a specific network option before its value is updated. 1262 1364 * 1263 1365 * The dynamic portion of the hook name, `$option`, refers to the option name. 1264 1366 * … … 1266 1368 * @since 3.0.0 1267 1369 * @since 4.4.0 The `$option` parameter was added 1268 1370 * 1269 * @param mixed $value New value of siteoption.1270 * @param mixed $old_value Old value of siteoption.1371 * @param mixed $value New value of the network option. 1372 * @param mixed $old_value Old value of the network option. 1271 1373 * @param string $option Option name. 1272 1374 */ 1273 1375 $value = apply_filters( 'pre_update_site_option_' . $option, $value, $old_value, $option ); 1274 1376 1275 if ( $value === $old_value ) 1377 if ( $value === $old_value ) { 1276 1378 return false; 1379 } 1277 1380 1278 if ( false === $old_value ) 1279 return add_site_option( $option, $value ); 1381 if ( false === $old_value ) { 1382 return add_network_option( $option, $value, $network_id ); 1383 } 1280 1384 1281 $notoptions_key = " {$wpdb->siteid}:notoptions";1385 $notoptions_key = "$network_id:notoptions"; 1282 1386 $notoptions = wp_cache_get( $notoptions_key, 'site-options' ); 1283 if ( is_array( $notoptions ) && isset( $notoptions[ $option] ) ) {1284 unset( $notoptions[ $option] );1387 if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) { 1388 unset( $notoptions[ $option ] ); 1285 1389 wp_cache_set( $notoptions_key, $notoptions, 'site-options' ); 1286 1390 } 1287 1391 1288 if ( ! is_multisite() ) {1392 if ( ! is_multisite() ) { 1289 1393 $result = update_option( $option, $value ); 1290 1394 } else { 1291 1395 $value = sanitize_option( $option, $value ); 1292 1396 1293 1397 $serialized_value = maybe_serialize( $value ); 1294 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $ wpdb->siteid, 'meta_key' => $option ) );1398 $result = $wpdb->update( $wpdb->sitemeta, array( 'meta_value' => $serialized_value ), array( 'site_id' => $network_id, 'meta_key' => $option ) ); 1295 1399 1296 1400 if ( $result ) { 1297 $cache_key = " {$wpdb->siteid}:$option";1401 $cache_key = "$network_id:$option"; 1298 1402 wp_cache_set( $cache_key, $value, 'site-options' ); 1299 1403 } 1300 1404 } … … 1302 1406 if ( $result ) { 1303 1407 1304 1408 /** 1305 * Fires after the value of a specific siteoption has been successfully updated.1409 * Fires after the value of a specific network option has been successfully updated. 1306 1410 * 1307 1411 * The dynamic portion of the hook name, `$option`, refers to the option name. 1308 1412 * 1309 1413 * @since 2.9.0 As "update_site_option_{$key}" 1310 1414 * @since 3.0.0 1311 1415 * 1312 * @param string $option Name of siteoption.1313 * @param mixed $value Current value of siteoption.1314 * @param mixed $old_value Old value of siteoption.1416 * @param string $option Name of the network option. 1417 * @param mixed $value Current value of the network option. 1418 * @param mixed $old_value Old value of the network option. 1315 1419 */ 1316 do_action( "update_site_option_{$option}", $option, $value, $old_value );1420 do_action( 'update_site_option_' . $option, $option, $value, $old_value ); 1317 1421 1318 1422 /** 1319 * Fires after the value of a siteoption has been successfully updated.1423 * Fires after the value of a network option has been successfully updated. 1320 1424 * 1321 1425 * @since 3.0.0 1322 1426 * 1323 * @param string $option Name of siteoption.1324 * @param mixed $value Current value of siteoption.1325 * @param mixed $old_value Old value of siteoption.1427 * @param string $option Name of the network option. 1428 * @param mixed $value Current value of the network option. 1429 * @param mixed $old_value Old value of the network option. 1326 1430 */ 1327 do_action( "update_site_option", $option, $value, $old_value );1431 do_action( 'update_site_option', $option, $value, $old_value ); 1328 1432 1329 1433 return true; 1330 1434 } 1435 1331 1436 return false; 1332 1437 } 1333 1438