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