PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/ms-blogs.php

https://gitlab.com/endomorphosis/reservationtelco
PHP | 577 lines | 408 code | 91 blank | 78 comment | 130 complexity | f3231b4c57c9df38c868b5d18e6c2c91 MD5 | raw file
  1. <?php
  2. /**
  3. * Site/blog functions that work with the blogs table and related data.
  4. *
  5. * @package WordPress
  6. * @subpackage Multisite
  7. * @since 3.0.0
  8. */
  9. // @todo use update_blog_details
  10. function wpmu_update_blogs_date() {
  11. global $wpdb;
  12. $wpdb->update( $wpdb->blogs, array('last_updated' => current_time('mysql', true)), array('blog_id' => $wpdb->blogid) );
  13. refresh_blog_details( $wpdb->blogid );
  14. do_action( 'wpmu_blog_updated', $wpdb->blogid );
  15. }
  16. function get_blogaddress_by_id( $blog_id ) {
  17. $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
  18. return esc_url( 'http://' . $bloginfo->domain . $bloginfo->path );
  19. }
  20. function get_blogaddress_by_name( $blogname ) {
  21. global $current_site;
  22. if ( is_subdomain_install() ) {
  23. if ( $blogname == 'main' )
  24. $blogname = 'www';
  25. $url = rtrim( network_home_url(), '/' );
  26. if ( !empty( $blogname ) )
  27. $url = preg_replace( '|^([^\.]+://)|', '$1' . $blogname . '.', $url );
  28. } else {
  29. $url = network_home_url( $blogname );
  30. }
  31. return esc_url( $url . '/' );
  32. }
  33. function get_blogaddress_by_domain( $domain, $path ){
  34. if ( is_subdomain_install() ) {
  35. $url = "http://".$domain.$path;
  36. } else {
  37. if ( $domain != $_SERVER['HTTP_HOST'] ) {
  38. $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
  39. $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
  40. // we're not installing the main blog
  41. if ( $blogname != 'www.' )
  42. $url .= $blogname . '/';
  43. } else { // main blog
  44. $url = 'http://' . $domain . $path;
  45. }
  46. }
  47. return esc_url( $url );
  48. }
  49. function get_id_from_blogname( $name ) {
  50. global $wpdb, $current_site;
  51. $blog_id = wp_cache_get( "get_id_from_blogname_" . $name, 'blog-details' );
  52. if ( $blog_id )
  53. return $blog_id;
  54. if ( is_subdomain_install() ) {
  55. $domain = $name . '.' . $current_site->domain;
  56. $path = $current_site->path;
  57. } else {
  58. $domain = $current_site->domain;
  59. $path = $current_site->path . $name . '/';
  60. }
  61. $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
  62. wp_cache_set( 'get_id_from_blogname_' . $name, $blog_id, 'blog-details' );
  63. return $blog_id;
  64. }
  65. /**
  66. * Retrieve the details for a blog from the blogs table and blog options.
  67. *
  68. * @since 3.0.0
  69. * @param int|string|array $fields A blog ID, a blog name, or an array of fields to query against.
  70. * @param bool $get_all Whether to retrieve all details or only the details in the blogs table. Default is true.
  71. * @return object Blog details.
  72. */
  73. function get_blog_details( $fields, $get_all = true ) {
  74. global $wpdb;
  75. if ( is_array($fields ) ) {
  76. if ( isset($fields['blog_id']) ) {
  77. $blog_id = $fields['blog_id'];
  78. } elseif ( isset($fields['domain']) && isset($fields['path']) ) {
  79. $key = md5( $fields['domain'] . $fields['path'] );
  80. $blog = wp_cache_get($key, 'blog-lookup');
  81. if ( false !== $blog )
  82. return $blog;
  83. if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
  84. $nowww = substr( $fields['domain'], 4 );
  85. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) AND path = %s ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'], $fields['path'] ) );
  86. } else {
  87. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s AND path = %s", $fields['domain'], $fields['path'] ) );
  88. }
  89. if ( $blog ) {
  90. wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
  91. $blog_id = $blog->blog_id;
  92. } else {
  93. return false;
  94. }
  95. } elseif ( isset($fields['domain']) && is_subdomain_install() ) {
  96. $key = md5( $fields['domain'] );
  97. $blog = wp_cache_get($key, 'blog-lookup');
  98. if ( false !== $blog )
  99. return $blog;
  100. if ( substr( $fields['domain'], 0, 4 ) == 'www.' ) {
  101. $nowww = substr( $fields['domain'], 4 );
  102. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain IN (%s,%s) ORDER BY CHAR_LENGTH(domain) DESC", $nowww, $fields['domain'] ) );
  103. } else {
  104. $blog = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE domain = %s", $fields['domain'] ) );
  105. }
  106. if ( $blog ) {
  107. wp_cache_set($blog->blog_id . 'short', $blog, 'blog-details');
  108. $blog_id = $blog->blog_id;
  109. } else {
  110. return false;
  111. }
  112. } else {
  113. return false;
  114. }
  115. } else {
  116. if ( !is_numeric( $fields ) )
  117. $blog_id = get_id_from_blogname( $fields );
  118. else
  119. $blog_id = $fields;
  120. }
  121. $blog_id = (int) $blog_id;
  122. $all = $get_all == true ? '' : 'short';
  123. $details = wp_cache_get( $blog_id . $all, 'blog-details' );
  124. if ( $details ) {
  125. if ( ! is_object( $details ) ) {
  126. if ( $details == -1 ) {
  127. return false;
  128. } else {
  129. // Clear old pre-serialized objects. Cache clients do better with that.
  130. wp_cache_delete( $blog_id . $all, 'blog-details' );
  131. unset($details);
  132. }
  133. } else {
  134. return $details;
  135. }
  136. }
  137. // Try the other cache.
  138. if ( $get_all ) {
  139. $details = wp_cache_get( $blog_id . 'short', 'blog-details' );
  140. } else {
  141. $details = wp_cache_get( $blog_id, 'blog-details' );
  142. // If short was requested and full cache is set, we can return.
  143. if ( $details ) {
  144. if ( ! is_object( $details ) ) {
  145. if ( $details == -1 ) {
  146. return false;
  147. } else {
  148. // Clear old pre-serialized objects. Cache clients do better with that.
  149. wp_cache_delete( $blog_id, 'blog-details' );
  150. unset($details);
  151. }
  152. } else {
  153. return $details;
  154. }
  155. }
  156. }
  157. if ( empty($details) ) {
  158. $details = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $blog_id ) );
  159. if ( ! $details ) {
  160. // Set the full cache.
  161. wp_cache_set( $blog_id, -1, 'blog-details' );
  162. return false;
  163. }
  164. }
  165. if ( ! $get_all ) {
  166. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  167. return $details;
  168. }
  169. $details->blogname = get_blog_option( $blog_id, 'blogname' );
  170. $details->siteurl = get_blog_option( $blog_id, 'siteurl' );
  171. $details->post_count = get_blog_option( $blog_id, 'post_count' );
  172. $details = apply_filters( 'blog_details', $details );
  173. wp_cache_set( $blog_id . $all, $details, 'blog-details' );
  174. $key = md5( $details->domain . $details->path );
  175. wp_cache_set( $key, $details, 'blog-lookup' );
  176. return $details;
  177. }
  178. /**
  179. * Clear the blog details cache.
  180. *
  181. * @since 3.0.0
  182. *
  183. * @param int $blog_id Blog ID
  184. */
  185. function refresh_blog_details( $blog_id ) {
  186. $blog_id = (int) $blog_id;
  187. $details = get_blog_details( $blog_id, false );
  188. wp_cache_delete( $blog_id , 'blog-details' );
  189. wp_cache_delete( $blog_id . 'short' , 'blog-details' );
  190. wp_cache_delete( md5( $details->domain . $details->path ) , 'blog-lookup' );
  191. wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
  192. wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
  193. }
  194. /**
  195. * Update the details for a blog. Updates the blogs table for a given blog id.
  196. *
  197. * @since 3.0.0
  198. *
  199. * @param int $blog_id Blog ID
  200. * @param array $details Array of details keyed by blogs table field names.
  201. * @return bool True if update succeeds, false otherwise.
  202. */
  203. function update_blog_details( $blog_id, $details = array() ) {
  204. global $wpdb;
  205. if ( empty($details) )
  206. return false;
  207. if ( is_object($details) )
  208. $details = get_object_vars($details);
  209. $current_details = get_blog_details($blog_id, false);
  210. if ( empty($current_details) )
  211. return false;
  212. $current_details = get_object_vars($current_details);
  213. $details = array_merge($current_details, $details);
  214. $details['last_updated'] = current_time('mysql', true);
  215. $update_details = array();
  216. $fields = array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id');
  217. foreach ( array_intersect( array_keys( $details ), $fields ) as $field )
  218. $update_details[$field] = $details[$field];
  219. $wpdb->update( $wpdb->blogs, $update_details, array('blog_id' => $blog_id) );
  220. // If spam status changed, issue actions.
  221. if ( $details[ 'spam' ] != $current_details[ 'spam' ] ) {
  222. if ( $details[ 'spam' ] == 1 )
  223. do_action( "make_spam_blog", $blog_id );
  224. else
  225. do_action( "make_ham_blog", $blog_id );
  226. }
  227. if ( isset($details[ 'public' ]) )
  228. update_blog_option( $blog_id, 'blog_public', $details[ 'public' ], false );
  229. refresh_blog_details($blog_id);
  230. return true;
  231. }
  232. /**
  233. * Retrieve option value based on setting name and blog_id.
  234. *
  235. * If the option does not exist or does not have a value, then the return value
  236. * will be false. This is useful to check whether you need to install an option
  237. * and is commonly used during installation of plugin options and to test
  238. * whether upgrading is required.
  239. *
  240. * There is a filter called 'blog_option_$option' with the $option being
  241. * replaced with the option name. The filter takes two parameters. $value and
  242. * $blog_id. It returns $value.
  243. * The 'option_$option' filter in get_option() is not called.
  244. *
  245. * @since NA
  246. * @package WordPress MU
  247. * @subpackage Option
  248. * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
  249. *
  250. * @param int $blog_id is the id of the blog.
  251. * @param string $setting Name of option to retrieve. Should already be SQL-escaped
  252. * @param string $default (optional) Default value returned if option not found.
  253. * @return mixed Value set for the option.
  254. */
  255. function get_blog_option( $blog_id, $setting, $default = false ) {
  256. global $wpdb;
  257. $key = $blog_id."-".$setting."-blog_option";
  258. $value = wp_cache_get( $key, "site-options" );
  259. if ( $value == null ) {
  260. if ( $blog_id == $wpdb->blogid ) {
  261. $value = get_option( $setting, $default );
  262. $notoptions = wp_cache_get( 'notoptions', 'options' );
  263. if ( isset( $notoptions[$setting] ) ) {
  264. wp_cache_set( $key, 'noop', 'site-options' );
  265. $value = $default;
  266. } elseif ( $value == false ) {
  267. wp_cache_set( $key, 'falsevalue', 'site-options' );
  268. } else {
  269. wp_cache_set( $key, $value, 'site-options' );
  270. }
  271. return apply_filters( 'blog_option_' . $setting, $value, $blog_id );
  272. } else {
  273. $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
  274. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
  275. if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
  276. $value = $row->option_value;
  277. if ( $value == false )
  278. wp_cache_set( $key, 'falsevalue', 'site-options' );
  279. else
  280. wp_cache_set( $key, $value, 'site-options' );
  281. } else { // option does not exist, so we must cache its non-existence
  282. wp_cache_set( $key, 'noop', 'site-options' );
  283. $value = $default;
  284. }
  285. }
  286. } elseif ( $value == 'noop' ) {
  287. $value = $default;
  288. } elseif ( $value == 'falsevalue' ) {
  289. $value = false;
  290. }
  291. // If home is not set use siteurl.
  292. if ( 'home' == $setting && '' == $value )
  293. return get_blog_option( $blog_id, 'siteurl' );
  294. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
  295. $value = untrailingslashit( $value );
  296. if (! @unserialize( $value ) )
  297. $value = stripslashes( $value );
  298. return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
  299. }
  300. function add_blog_option( $id, $key, $value ) {
  301. $id = (int) $id;
  302. switch_to_blog($id);
  303. add_option( $key, $value );
  304. restore_current_blog();
  305. wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options' );
  306. }
  307. function delete_blog_option( $id, $key ) {
  308. $id = (int) $id;
  309. switch_to_blog($id);
  310. delete_option( $key );
  311. restore_current_blog();
  312. wp_cache_set( $id."-".$key."-blog_option", '', 'site-options' );
  313. }
  314. function update_blog_option( $id, $key, $value, $refresh = true ) {
  315. $id = (int) $id;
  316. switch_to_blog($id);
  317. update_option( $key, $value );
  318. restore_current_blog();
  319. if ( $refresh == true )
  320. refresh_blog_details( $id );
  321. wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options');
  322. }
  323. function switch_to_blog( $new_blog, $validate = false ) {
  324. global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
  325. if ( empty($new_blog) )
  326. $new_blog = $blog_id;
  327. if ( $validate && ! get_blog_details( $new_blog ) )
  328. return false;
  329. if ( empty($switched_stack) )
  330. $switched_stack = array();
  331. $switched_stack[] = $blog_id;
  332. /* If we're switching to the same blog id that we're on,
  333. * set the right vars, do the associated actions, but skip
  334. * the extra unnecessary work */
  335. if ( $blog_id == $new_blog ) {
  336. do_action( 'switch_blog', $blog_id, $blog_id );
  337. $switched = true;
  338. return true;
  339. }
  340. $wpdb->set_blog_id($new_blog);
  341. $table_prefix = $wpdb->prefix;
  342. $prev_blog_id = $blog_id;
  343. $blog_id = $new_blog;
  344. if ( is_object( $wp_roles ) ) {
  345. $wpdb->suppress_errors();
  346. if ( method_exists( $wp_roles ,'_init' ) )
  347. $wp_roles->_init();
  348. elseif ( method_exists( $wp_roles, '__construct' ) )
  349. $wp_roles->__construct();
  350. $wpdb->suppress_errors( false );
  351. }
  352. if ( is_object( $current_user ) )
  353. $current_user->for_blog( $blog_id );
  354. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
  355. $global_groups = $wp_object_cache->global_groups;
  356. else
  357. $global_groups = false;
  358. wp_cache_init();
  359. if ( function_exists('wp_cache_add_global_groups') ) {
  360. if ( is_array( $global_groups ) )
  361. wp_cache_add_global_groups( $global_groups );
  362. else
  363. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient', 'global-posts' ) );
  364. wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
  365. }
  366. do_action('switch_blog', $blog_id, $prev_blog_id);
  367. $switched = true;
  368. return true;
  369. }
  370. function restore_current_blog() {
  371. global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
  372. if ( !$switched )
  373. return false;
  374. if ( !is_array( $switched_stack ) )
  375. return false;
  376. $blog = array_pop( $switched_stack );
  377. if ( $blog_id == $blog ) {
  378. do_action( 'switch_blog', $blog, $blog );
  379. /* If we still have items in the switched stack, consider ourselves still 'switched' */
  380. $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
  381. return true;
  382. }
  383. $wpdb->set_blog_id($blog);
  384. $prev_blog_id = $blog_id;
  385. $blog_id = $blog;
  386. $table_prefix = $wpdb->prefix;
  387. if ( is_object( $wp_roles ) ) {
  388. $wpdb->suppress_errors();
  389. if ( method_exists( $wp_roles ,'_init' ) )
  390. $wp_roles->_init();
  391. elseif ( method_exists( $wp_roles, '__construct' ) )
  392. $wp_roles->__construct();
  393. $wpdb->suppress_errors( false );
  394. }
  395. if ( is_object( $current_user ) )
  396. $current_user->for_blog( $blog_id );
  397. if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) )
  398. $global_groups = $wp_object_cache->global_groups;
  399. else
  400. $global_groups = false;
  401. wp_cache_init();
  402. if ( function_exists('wp_cache_add_global_groups') ) {
  403. if ( is_array( $global_groups ) )
  404. wp_cache_add_global_groups( $global_groups );
  405. else
  406. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient' ) );
  407. wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
  408. }
  409. do_action('switch_blog', $blog_id, $prev_blog_id);
  410. /* If we still have items in the switched stack, consider ourselves still 'switched' */
  411. $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
  412. return true;
  413. }
  414. function is_archived( $id ) {
  415. return get_blog_status($id, 'archived');
  416. }
  417. function update_archived( $id, $archived ) {
  418. update_blog_status($id, 'archived', $archived);
  419. return $archived;
  420. }
  421. /**
  422. * Update a blog details field.
  423. *
  424. * @since 3.0.0
  425. *
  426. * @param int $blog_id BLog ID
  427. * @param string $pref A field name
  428. * @param string $value Value for $pref
  429. * @param bool $refresh Whether to refresh the blog details cache. Default is true.
  430. */
  431. function update_blog_status( $blog_id, $pref, $value, $refresh = true ) {
  432. global $wpdb;
  433. if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
  434. return $value;
  435. $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $blog_id) );
  436. if ( $refresh )
  437. refresh_blog_details($blog_id);
  438. if ( $pref == 'spam' ) {
  439. if ( $value == 1 )
  440. do_action( "make_spam_blog", $blog_id );
  441. else
  442. do_action( "make_ham_blog", $blog_id );
  443. }
  444. return $value;
  445. }
  446. function get_blog_status( $id, $pref ) {
  447. global $wpdb;
  448. $details = get_blog_details( $id, false );
  449. if ( $details )
  450. return $details->$pref;
  451. return $wpdb->get_var( $wpdb->prepare("SELECT %s FROM {$wpdb->blogs} WHERE blog_id = %d", $pref, $id) );
  452. }
  453. function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
  454. global $wpdb;
  455. return $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' AND last_updated != '0000-00-00 00:00:00' ORDER BY last_updated DESC limit %d, %d", $wpdb->siteid, $start, $quantity ) , ARRAY_A );
  456. }
  457. function get_blog_list( $start = 0, $num = 10, $deprecated = '' ) {
  458. global $wpdb;
  459. $blogs = get_site_option( "blog_list" );
  460. $update = false;
  461. if ( is_array( $blogs ) ) {
  462. if ( ( $blogs['time'] + 60 ) < time() ) { // cache for 60 seconds.
  463. $update = true;
  464. }
  465. } else {
  466. $update = true;
  467. }
  468. if ( $update == true ) {
  469. unset( $blogs );
  470. $blogs = $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $wpdb->siteid), ARRAY_A );
  471. foreach ( (array) $blogs as $details ) {
  472. $blog_list[ $details['blog_id'] ] = $details;
  473. $blog_list[ $details['blog_id'] ]['postcount'] = $wpdb->get_var( "SELECT COUNT(ID) FROM " . $wpdb->base_prefix . $details['blog_id'] . "_posts WHERE post_status='publish' AND post_type='post'" );
  474. }
  475. unset( $blogs );
  476. $blogs = $blog_list;
  477. update_site_option( "blog_list", $blogs );
  478. }
  479. if ( false == is_array( $blogs ) )
  480. return array();
  481. if ( $num == 'all' )
  482. return array_slice( $blogs, $start, count( $blogs ) );
  483. else
  484. return array_slice( $blogs, $start, $num );
  485. }
  486. ?>