PageRenderTime 32ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/blog/wp-includes/wpmu-functions.php

https://bitbucket.org/malathisuji/nrcfoss-au-tamil-computing-activities
PHP | 2407 lines | 1871 code | 418 blank | 118 comment | 517 complexity | 00b864d7e0aa2473ec11c92de45ec13b MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. Helper functions for WPMU
  4. */
  5. function load_muplugin_textdomain($domain, $path = false) {
  6. $locale = get_locale();
  7. if ( empty($locale) )
  8. $locale = 'en_US';
  9. if ( false === $path )
  10. $path = WPMU_PLUGIN_DIR;
  11. $mofile = WPMU_PLUGIN_DIR . "/$domain-$locale.mo";
  12. load_textdomain($domain, $mofile);
  13. }
  14. function wpmu_update_blogs_date() {
  15. global $wpdb;
  16. $wpdb->update( $wpdb->blogs, array('last_updated' => current_time('mysql', true)), array('blog_id' => $wpdb->blogid) );
  17. refresh_blog_details( $wpdb->blogid );
  18. do_action( 'wpmu_blog_updated', $wpdb->blogid );
  19. }
  20. function get_blogaddress_by_id( $blog_id ) {
  21. $bloginfo = get_blog_details( (int) $blog_id, false ); // only get bare details!
  22. return clean_url("http://" . $bloginfo->domain . $bloginfo->path);
  23. }
  24. function get_blogaddress_by_name( $blogname ) {
  25. global $current_site;
  26. if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' ) {
  27. if( $blogname == 'main' )
  28. $blogname = 'www';
  29. return clean_url( "http://" . $blogname . "." . $current_site->domain . $current_site->path );
  30. } else {
  31. return clean_url( "http://" . $current_site->domain . $current_site->path . $blogname . '/' );
  32. }
  33. }
  34. function get_blogaddress_by_domain( $domain, $path ){
  35. if( defined( "VHOST" ) && constant( "VHOST" ) == 'yes' ) {
  36. $url = "http://".$domain.$path;
  37. } else {
  38. if( $domain != $_SERVER['HTTP_HOST'] ) {
  39. $blogname = substr( $domain, 0, strpos( $domain, '.' ) );
  40. if( $blogname != 'www.' ) {
  41. $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path . $blogname . '/';
  42. } else { // we're installing the main blog
  43. $url = 'http://' . substr( $domain, strpos( $domain, '.' ) + 1 ) . $path;
  44. }
  45. } else { // main blog
  46. $url = 'http://' . $domain . $path;
  47. }
  48. }
  49. return clean_url($url);
  50. }
  51. function get_sitestats() {
  52. global $wpdb;
  53. $stats['blogs'] = get_blog_count();
  54. $count_ts = get_site_option( "get_user_count_ts" );
  55. if( time() - $count_ts > 3600 ) {
  56. $count = $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->users}" );
  57. update_site_option( "user_count", $count );
  58. update_site_option( "user_count_ts", time() );
  59. } else {
  60. $count = get_site_option( "user_count" );
  61. }
  62. $stats['users'] = $count;
  63. return $stats;
  64. }
  65. function get_admin_users_for_domain( $sitedomain = '', $path = '' ) {
  66. global $wpdb;
  67. if( $sitedomain == '' ) {
  68. $site_id = $wpdb->siteid;
  69. } else {
  70. $site_id = $wpdb->get_var( $wpdb->prepare("SELECT id FROM $wpdb->site WHERE domain = %s AND path = %s", $sitedomain, $path) );
  71. }
  72. if( $site_id != false ) {
  73. return $wpdb->get_results( $wpdb->prepare("SELECT u.ID, u.user_login, u.user_pass FROM $wpdb->users AS u, $wpdb->sitemeta AS sm WHERE sm.meta_key = 'admin_user_id' AND u.ID = sm.meta_value AND sm.site_id = %d", $site_id), ARRAY_A );
  74. }
  75. return false;
  76. }
  77. function get_user_details( $username ) {
  78. global $wpdb;
  79. return $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_login = %s", $username) );
  80. }
  81. function is_main_blog() {
  82. global $current_blog, $current_site;
  83. if( $current_blog->domain == $current_site->domain && $current_blog->path == $current_site->path )
  84. return true;
  85. return false;
  86. }
  87. function get_id_from_blogname( $name ) {
  88. global $wpdb, $current_site;
  89. $blog_id = wp_cache_get( "get_id_from_blogname_" . $name, 'blog-details' );
  90. if( $blog_id )
  91. return $blog_id;
  92. if( constant( 'VHOST' ) == 'yes' ) {
  93. $domain = $name . '.' . $current_site->domain;
  94. $path = $current_site->path;
  95. } else {
  96. $domain = $current_site->domain;
  97. $path = $current_site->path . $name . '/';
  98. }
  99. $blog_id = $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM {$wpdb->blogs} WHERE domain = %s AND path = %s", $domain, $path) );
  100. wp_cache_set( 'get_id_from_blogname_' . $name, $blog_id, 'blog-details' );
  101. return $blog_id;
  102. }
  103. function get_blog_details( $id, $getall = true ) {
  104. global $wpdb;
  105. if( !is_numeric( $id ) ) {
  106. $id = get_id_from_blogname( $id );
  107. }
  108. $all = $getall == true ? '' : 'short';
  109. $details = wp_cache_get( $id . $all, 'blog-details' );
  110. if ( $details ) {
  111. if ( $details == -1 )
  112. return false;
  113. elseif ( !is_object($details) ) // Clear old pre-serialized objects. Cache clients do better with that.
  114. wp_cache_delete( $id . $all, 'blog-details' );
  115. else
  116. return $details;
  117. }
  118. $details = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->blogs WHERE blog_id = %d /* get_blog_details */", $id) );
  119. if ( !$details ) {
  120. wp_cache_set( $id . $all, -1, 'blog-details' );
  121. return false;
  122. }
  123. if ( !$getall ) {
  124. wp_cache_set( $id . $all, $details, 'blog-details' );
  125. return $details;
  126. }
  127. $wpdb->suppress_errors();
  128. switch_to_blog( $id );
  129. $details->blogname = get_option( 'blogname' );
  130. $details->siteurl = get_option( 'siteurl' );
  131. $details->post_count = get_option( 'post_count' );
  132. restore_current_blog();
  133. $wpdb->suppress_errors( false );
  134. $details = apply_filters('blog_details', $details);
  135. wp_cache_set( $id . $all, $details, 'blog-details' );
  136. $key = md5( $details->domain . $details->path );
  137. wp_cache_set( $key, $details, 'blog-lookup' );
  138. return $details;
  139. }
  140. function refresh_blog_details( $id ) {
  141. $id = (int) $id;
  142. $details = get_blog_details( $id, false );
  143. wp_cache_delete( $id , 'blog-details' );
  144. wp_cache_delete( $id . 'short' , 'blog-details' );
  145. wp_cache_delete( md5( $details->domain . $details->path ) , 'blog-lookup' );
  146. wp_cache_delete( 'current_blog_' . $details->domain, 'site-options' );
  147. wp_cache_delete( 'current_blog_' . $details->domain . $details->path, 'site-options' );
  148. }
  149. function get_current_user_id() {
  150. global $current_user;
  151. return $current_user->ID;
  152. }
  153. function is_site_admin( $user_login = false ) {
  154. global $current_user;
  155. if ( !$current_user && !$user_login )
  156. return false;
  157. if ( $user_login ) {
  158. $user_login = sanitize_user( $user_login );
  159. } elseif( isset( $current_user->user_login ) ) {
  160. $user_login = $current_user->user_login;
  161. } else {
  162. return false;
  163. }
  164. $site_admins = get_site_option( 'site_admins', array('admin') );
  165. if( is_array( $site_admins ) && in_array( $user_login, $site_admins ) )
  166. return true;
  167. return false;
  168. }
  169. /**
  170. * Retrieve option value based on setting name and blog_id.
  171. *
  172. * If the option does not exist or does not have a value, then the return value
  173. * will be false. This is useful to check whether you need to install an option
  174. * and is commonly used during installation of plugin options and to test
  175. * whether upgrading is required.
  176. *
  177. * There is a filter called 'blog_option_$option' with the $option being
  178. * replaced with the option name. The filter takes two parameters. $value and
  179. * $blog_id. It returns $value.
  180. * The 'option_$option' filter in get_option() is not called.
  181. *
  182. * @since NA
  183. * @package WordPress MU
  184. * @subpackage Option
  185. * @uses apply_filters() Calls 'blog_option_$optionname' with the option name value.
  186. *
  187. * @param int $blog_id is the id of the blog.
  188. * @param string $setting Name of option to retrieve. Should already be SQL-escaped
  189. * @param string $default (optional) Default value returned if option not found.
  190. * @return mixed Value set for the option.
  191. */
  192. function get_blog_option( $blog_id, $setting, $default = false ) {
  193. global $wpdb;
  194. $key = $blog_id."-".$setting."-blog_option";
  195. $value = wp_cache_get( $key, "site-options" );
  196. if ( $value == null ) {
  197. $blog_prefix = $wpdb->get_blog_prefix( $blog_id );
  198. $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$blog_prefix}options WHERE option_name = %s", $setting ) );
  199. if ( is_object( $row ) ) { // Has to be get_row instead of get_var because of funkiness with 0, false, null values
  200. $value = $row->option_value;
  201. if ( $value == false ) {
  202. wp_cache_set( $key, 'falsevalue', 'site-options' );
  203. } else {
  204. wp_cache_set( $key, $value, 'site-options' );
  205. }
  206. } else { // option does not exist, so we must cache its non-existence
  207. wp_cache_set( $key, 'noop', 'site-options' );
  208. $value = $default;
  209. }
  210. } elseif( $value == 'noop' ) {
  211. $value = $default;
  212. } elseif( $value == 'falsevalue' ) {
  213. $value = false;
  214. }
  215. // If home is not set use siteurl.
  216. if ( 'home' == $setting && '' == $value )
  217. return get_blog_option( $blog_id, 'siteurl' );
  218. if ( 'siteurl' == $setting || 'home' == $setting || 'category_base' == $setting )
  219. $value = preg_replace( '|/+$|', '', $value );
  220. if (! @unserialize( $value ) )
  221. $value = stripslashes( $value );
  222. return apply_filters( 'blog_option_' . $setting, maybe_unserialize( $value ), $blog_id );
  223. }
  224. function add_blog_option( $id, $key, $value ) {
  225. $id = (int) $id;
  226. switch_to_blog($id);
  227. add_option( $key, $value );
  228. restore_current_blog();
  229. wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options' );
  230. }
  231. function delete_blog_option( $id, $key ) {
  232. $id = (int) $id;
  233. switch_to_blog($id);
  234. delete_option( $key );
  235. restore_current_blog();
  236. wp_cache_set( $id."-".$key."-blog_option", '', 'site-options' );
  237. }
  238. function update_blog_option( $id, $key, $value, $refresh = true ) {
  239. $id = (int) $id;
  240. switch_to_blog($id);
  241. update_option( $key, $value );
  242. restore_current_blog();
  243. if( $refresh == true )
  244. refresh_blog_details( $id );
  245. wp_cache_set( $id."-".$key."-blog_option", $value, 'site-options');
  246. }
  247. function switch_to_blog( $new_blog ) {
  248. global $wpdb, $table_prefix, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
  249. if ( empty($new_blog) )
  250. $new_blog = $blog_id;
  251. if ( empty($switched_stack) )
  252. $switched_stack = array();
  253. $switched_stack[] = $blog_id;
  254. /* If we're switching to the same blog id that we're on,
  255. * set the right vars, do the associated actions, but skip
  256. * the extra unnecessary work */
  257. if ( $blog_id == $new_blog ) {
  258. do_action( 'switch_blog', $blog_id, $blog_id );
  259. $switched = true;
  260. return true;
  261. }
  262. $wpdb->set_blog_id($new_blog);
  263. $table_prefix = $wpdb->prefix;
  264. $prev_blog_id = $blog_id;
  265. $blog_id = $new_blog;
  266. if( is_object( $wp_roles ) ) {
  267. $wpdb->suppress_errors();
  268. if ( method_exists( $wp_roles ,'_init' ) ) {
  269. $wp_roles->_init();
  270. } elseif( method_exists( $wp_roles, '__construct' ) ) {
  271. $wp_roles->__construct();
  272. }
  273. $wpdb->suppress_errors( false );
  274. }
  275. if ( is_object( $current_user ) )
  276. $current_user->_init_caps();
  277. if ( is_object( $wp_object_cache ) ) {
  278. $global_groups = $wp_object_cache->global_groups;
  279. } else {
  280. $global_groups = false;
  281. }
  282. wp_cache_init();
  283. if ( function_exists('wp_cache_add_global_groups') ) {
  284. if ( is_array( $global_groups ) ) {
  285. wp_cache_add_global_groups( $global_groups );
  286. } else {
  287. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient', 'global-posts' ) );
  288. }
  289. wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
  290. }
  291. do_action('switch_blog', $blog_id, $prev_blog_id);
  292. $switched = true;
  293. return true;
  294. }
  295. function restore_current_blog() {
  296. global $table_prefix, $wpdb, $blog_id, $switched, $switched_stack, $wp_roles, $current_user, $wp_object_cache;
  297. if ( !$switched )
  298. return false;
  299. if ( !is_array( $switched_stack ) )
  300. return false;
  301. $blog = array_pop( $switched_stack );
  302. if ( $blog_id == $blog ) {
  303. do_action( 'switch_blog', $blog, $blog );
  304. /* If we still have items in the switched stack, consider ourselves still 'switched' */
  305. $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
  306. return true;
  307. }
  308. $wpdb->set_blog_id($blog);
  309. $prev_blog_id = $blog_id;
  310. $blog_id = $blog;
  311. $table_prefix = $wpdb->prefix;
  312. if( is_object( $wp_roles ) ) {
  313. $wpdb->suppress_errors();
  314. if ( method_exists( $wp_roles ,'_init' ) ) {
  315. $wp_roles->_init();
  316. } elseif( method_exists( $wp_roles, '__construct' ) ) {
  317. $wp_roles->__construct();
  318. }
  319. $wpdb->suppress_errors( false );
  320. }
  321. if ( is_object( $current_user ) )
  322. $current_user->_init_caps();
  323. if ( is_object( $wp_object_cache ) ) {
  324. $global_groups = $wp_object_cache->global_groups;
  325. } else {
  326. $global_groups = false;
  327. }
  328. wp_cache_init();
  329. if ( function_exists('wp_cache_add_global_groups') ) {
  330. if ( is_array( $global_groups ) ) {
  331. wp_cache_add_global_groups( $global_groups );
  332. } else {
  333. wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'site-transient' ) );
  334. }
  335. wp_cache_add_non_persistent_groups(array( 'comment', 'counts', 'plugins' ));
  336. }
  337. do_action('switch_blog', $blog_id, $prev_blog_id);
  338. /* If we still have items in the switched stack, consider ourselves still 'switched' */
  339. $switched = ( is_array( $switched_stack ) && count( $switched_stack ) > 0 );
  340. return true;
  341. }
  342. function get_blogs_of_user( $id, $all = false ) {
  343. global $wpdb;
  344. $cache_suffix = $all ? '_all' : '_short';
  345. $return = wp_cache_get( 'blogs_of_user_' . $id . $cache_suffix, 'users' );
  346. if ( $return ) {
  347. return apply_filters( 'get_blogs_of_user', $return, $id, $all );
  348. }
  349. $user = get_userdata( (int) $id );
  350. if ( !$user )
  351. return false;
  352. $blogs = $match = array();
  353. foreach ( (array) $user as $key => $value ) {
  354. if ( false !== strpos( $key, '_capabilities') && 0 === strpos( $key, $wpdb->base_prefix ) && preg_match( '/' . $wpdb->base_prefix . '(\d+)_capabilities/', $key, $match ) ) {
  355. $blog = get_blog_details( $match[1] );
  356. if ( $blog && isset( $blog->domain ) && ( $all == true || $all == false && ( $blog->archived == 0 && $blog->spam == 0 && $blog->deleted == 0 ) ) ) {
  357. $blogs[$match[1]]->userblog_id = $match[1];
  358. $blogs[$match[1]]->blogname = $blog->blogname;
  359. $blogs[$match[1]]->domain = $blog->domain;
  360. $blogs[$match[1]]->path = $blog->path;
  361. $blogs[$match[1]]->site_id = $blog->site_id;
  362. $blogs[$match[1]]->siteurl = $blog->siteurl;
  363. }
  364. }
  365. }
  366. wp_cache_add( 'blogs_of_user_' . $id . $cache_suffix, $blogs, 'users', 5 );
  367. return apply_filters( 'get_blogs_of_user', $blogs, $id, $all );
  368. }
  369. function get_active_blog_for_user( $user_id ) { // get an active blog for user - either primary blog or from blogs list
  370. global $wpdb;
  371. $blogs = get_blogs_of_user( $user_id );
  372. if ( empty( $blogs ) ) {
  373. $details = get_dashboard_blog();
  374. add_user_to_blog( $details->blog_id, $user_id, 'subscriber' );
  375. update_usermeta( $user_id, 'primary_blog', $details->blog_id );
  376. wp_cache_delete( $user_id, 'users' );
  377. return $details;
  378. }
  379. $primary_blog = get_usermeta( $user_id, "primary_blog" );
  380. $details = get_dashboard_blog();
  381. if ( $primary_blog ) {
  382. $blogs = get_blogs_of_user( $user_id );
  383. if ( isset( $blogs[ $primary_blog ] ) == false ) {
  384. add_user_to_blog( $details->blog_id, $user_id, 'subscriber' );
  385. update_usermeta( $user_id, 'primary_blog', $details->blog_id );
  386. wp_cache_delete( $user_id, 'users' );
  387. } else {
  388. $details = get_blog_details( $primary_blog );
  389. }
  390. } else {
  391. add_user_to_blog( $details->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
  392. update_usermeta( $user_id, 'primary_blog', $details->blog_id );
  393. }
  394. if ( ( is_object( $details ) == false ) || ( is_object( $details ) && $details->archived == 1 || $details->spam == 1 || $details->deleted == 1 ) ) {
  395. $blogs = get_blogs_of_user( $user_id, true ); // if a user's primary blog is shut down, check their other blogs.
  396. $ret = false;
  397. if ( is_array( $blogs ) && count( $blogs ) > 0 ) {
  398. foreach( (array) $blogs as $blog_id => $blog ) {
  399. if ( $blog->site_id != $wpdb->siteid )
  400. continue;
  401. $details = get_blog_details( $blog_id );
  402. if ( is_object( $details ) && $details->archived == 0 && $details->spam == 0 && $details->deleted == 0 ) {
  403. $ret = $blog;
  404. $changed = false;
  405. if ( get_usermeta( $user_id , 'primary_blog' ) != $blog_id ) {
  406. update_usermeta( $user_id, 'primary_blog', $blog_id );
  407. $changed = true;
  408. }
  409. if ( !get_usermeta($user_id , 'source_domain') ) {
  410. update_usermeta( $user_id, 'source_domain', $blog->domain );
  411. $changed = true;
  412. }
  413. if ( $changed )
  414. wp_cache_delete( $user_id, 'users' );
  415. break;
  416. }
  417. }
  418. } else {
  419. // Should never get here
  420. $dashboard_blog = get_dashboard_blog();
  421. add_user_to_blog( $dashboard_blog->blog_id, $user_id, 'subscriber' ); // Add subscriber permission for dashboard blog
  422. update_usermeta( $user_id, 'primary_blog', $dashboard_blog->blog_id );
  423. return $dashboard_blog;
  424. }
  425. return $ret;
  426. } else {
  427. return $details;
  428. }
  429. }
  430. function is_user_member_of_blog( $user_id, $blog_id = 0 ) {
  431. $user_id = (int) $user_id;
  432. $blog_id = (int) $blog_id;
  433. if( $blog_id == 0 ) {
  434. global $wpdb;
  435. $blog_id = $wpdb->blogid;
  436. }
  437. $blogs = get_blogs_of_user( $user_id );
  438. if( is_array( $blogs ) ) {
  439. return array_key_exists( $blog_id, $blogs );
  440. } else {
  441. return false;
  442. }
  443. }
  444. function is_archived( $id ) {
  445. return get_blog_status($id, 'archived');
  446. }
  447. function update_archived( $id, $archived ) {
  448. update_blog_status($id, 'archived', $archived);
  449. return $archived;
  450. }
  451. function update_blog_status( $id, $pref, $value, $refresh = 1 ) {
  452. global $wpdb;
  453. if ( !in_array( $pref, array( 'site_id', 'domain', 'path', 'registered', 'last_updated', 'public', 'archived', 'mature', 'spam', 'deleted', 'lang_id') ) )
  454. return $value;
  455. $wpdb->update( $wpdb->blogs, array($pref => $value, 'last_updated' => current_time('mysql', true)), array('blog_id' => $id) );
  456. if( $refresh == 1 )
  457. refresh_blog_details($id);
  458. if( $pref == 'spam' ) {
  459. if( $value == 1 ) {
  460. do_action( "make_spam_blog", $id );
  461. } else {
  462. do_action( "make_ham_blog", $id );
  463. }
  464. }
  465. return $value;
  466. }
  467. function get_blog_status( $id, $pref ) {
  468. global $wpdb;
  469. $details = get_blog_details( $id, false );
  470. if( $details ) {
  471. return $details->$pref;
  472. }
  473. return $wpdb->get_var( $wpdb->prepare("SELECT $pref FROM {$wpdb->blogs} WHERE blog_id = %d", $id) );
  474. }
  475. function get_last_updated( $deprecated = '', $start = 0, $quantity = 40 ) {
  476. global $wpdb;
  477. 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 );
  478. }
  479. function get_most_active_blogs( $num = 10, $display = true ) {
  480. $most_active = get_site_option( "most_active" );
  481. $update = false;
  482. if( is_array( $most_active ) ) {
  483. if( ( $most_active['time'] + 60 ) < time() ) { // cache for 60 seconds.
  484. $update = true;
  485. }
  486. } else {
  487. $update = true;
  488. }
  489. if( $update == true ) {
  490. unset( $most_active );
  491. $blogs = get_blog_list( 0, 'all', false ); // $blog_id -> $details
  492. if( is_array( $blogs ) ) {
  493. reset( $blogs );
  494. foreach ( (array) $blogs as $key => $details ) {
  495. $most_active[ $details['blog_id'] ] = $details['postcount'];
  496. $blog_list[ $details['blog_id'] ] = $details; // array_slice() removes keys!!
  497. }
  498. arsort( $most_active );
  499. reset( $most_active );
  500. foreach ( (array) $most_active as $key => $details ) {
  501. $t[ $key ] = $blog_list[ $key ];
  502. }
  503. unset( $most_active );
  504. $most_active = $t;
  505. }
  506. update_site_option( "most_active", $most_active );
  507. }
  508. if( $display == true ) {
  509. if( is_array( $most_active ) ) {
  510. reset( $most_active );
  511. foreach ( (array) $most_active as $key => $details ) {
  512. $url = clean_url("http://" . $details['domain'] . $details['path']);
  513. echo "<li>" . $details['postcount'] . " <a href='$url'>$url</a></li>";
  514. }
  515. }
  516. }
  517. return array_slice( $most_active, 0, $num );
  518. }
  519. function get_blog_list( $start = 0, $num = 10, $deprecated = '' ) {
  520. global $wpdb;
  521. $blogs = get_site_option( "blog_list" );
  522. $update = false;
  523. if( is_array( $blogs ) ) {
  524. if( ( $blogs['time'] + 60 ) < time() ) { // cache for 60 seconds.
  525. $update = true;
  526. }
  527. } else {
  528. $update = true;
  529. }
  530. if( $update == true ) {
  531. unset( $blogs );
  532. $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 );
  533. foreach ( (array) $blogs as $details ) {
  534. $blog_list[ $details['blog_id'] ] = $details;
  535. $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'" );
  536. }
  537. unset( $blogs );
  538. $blogs = $blog_list;
  539. update_site_option( "blog_list", $blogs );
  540. }
  541. if( false == is_array( $blogs ) )
  542. return array();
  543. if( $num == 'all' ) {
  544. return array_slice( $blogs, $start, count( $blogs ) );
  545. } else {
  546. return array_slice( $blogs, $start, $num );
  547. }
  548. }
  549. function get_user_count() {
  550. global $wpdb;
  551. $count_ts = get_site_option( "user_count_ts" );
  552. if( time() - $count_ts > 3600 ) {
  553. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(ID) as c FROM $wpdb->users WHERE spam = '0' AND deleted = '0'") );
  554. update_site_option( "user_count", $count );
  555. update_site_option( "user_count_ts", time() );
  556. }
  557. $count = get_site_option( "user_count" );
  558. return $count;
  559. }
  560. function get_blog_count( $id = 0 ) {
  561. global $wpdb;
  562. if( $id == 0 )
  563. $id = $wpdb->siteid;
  564. $count_ts = get_site_option( "blog_count_ts" );
  565. if( time() - $count_ts > 3600 ) {
  566. $count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(blog_id) as c FROM $wpdb->blogs WHERE site_id = %d AND spam = '0' AND deleted = '0' and archived = '0'", $id) );
  567. update_site_option( "blog_count", $count );
  568. update_site_option( "blog_count_ts", time() );
  569. }
  570. $count = get_site_option( "blog_count" );
  571. return $count;
  572. }
  573. function get_blog_post( $blog_id, $post_id ) {
  574. global $wpdb;
  575. $key = $blog_id . "-" . $post_id;
  576. $post = wp_cache_get( $key, "global-posts" );
  577. if( $post == false ) {
  578. $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM " . $wpdb->get_blog_prefix( $blog_id ) . "posts WHERE ID = %d", $post_id ) );
  579. wp_cache_add( $key, $post, "global-posts" );
  580. }
  581. return $post;
  582. }
  583. function clear_global_post_cache( $post_id ) {
  584. global $wpdb;
  585. wp_cache_delete( $wpdb->blogid . '-' . $post_id, 'global-posts' );
  586. }
  587. add_action( 'publish_post', 'clear_global_post_cache' );
  588. add_action( 'delete_post', 'clear_global_post_cache' );
  589. function add_user_to_blog( $blog_id, $user_id, $role ) {
  590. switch_to_blog($blog_id);
  591. $user = new WP_User($user_id);
  592. if ( empty($user) )
  593. return new WP_Error('user_does_not_exist', __('That user does not exist.'));
  594. if ( !get_usermeta($user_id, 'primary_blog') ) {
  595. update_usermeta($user_id, 'primary_blog', $blog_id);
  596. $details = get_blog_details($blog_id);
  597. update_usermeta($user_id, 'source_domain', $details->domain);
  598. }
  599. $user->set_role($role);
  600. do_action('add_user_to_blog', $user_id, $role, $blog_id);
  601. wp_cache_delete( $user_id, 'users' );
  602. restore_current_blog();
  603. return true;
  604. }
  605. function remove_user_from_blog($user_id, $blog_id = '', $reassign = '') {
  606. global $wpdb;
  607. switch_to_blog($blog_id);
  608. $user_id = (int) $user_id;
  609. do_action('remove_user_from_blog', $user_id, $blog_id);
  610. // If being removed from the primary blog, set a new primary if the user is assigned
  611. // to multiple blogs.
  612. $primary_blog = get_usermeta($user_id, 'primary_blog');
  613. if ( $primary_blog == $blog_id ) {
  614. $new_id = '';
  615. $new_domain = '';
  616. $blogs = get_blogs_of_user($user_id);
  617. foreach ( (array) $blogs as $blog ) {
  618. if ( $blog->userblog_id == $blog_id )
  619. continue;
  620. $new_id = $blog->userblog_id;
  621. $new_domain = $blog->domain;
  622. break;
  623. }
  624. update_usermeta($user_id, 'primary_blog', $new_id);
  625. update_usermeta($user_id, 'source_domain', $new_domain);
  626. }
  627. // wp_revoke_user($user_id);
  628. $user = new WP_User($user_id);
  629. $user->remove_all_caps();
  630. $blogs = get_blogs_of_user($user_id);
  631. if ( count($blogs) == 0 ) {
  632. update_usermeta($user_id, 'primary_blog', '');
  633. update_usermeta($user_id, 'source_domain', '');
  634. }
  635. if( $reassign != '' ) {
  636. $reassign = (int) $reassign;
  637. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->posts SET post_author = %d WHERE post_author = %d", $reassign, $user_id) );
  638. $wpdb->query( $wpdb->prepare("UPDATE $wpdb->links SET link_owner = %d WHERE link_owner = %d", $reassign, $user_id) );
  639. }
  640. restore_current_blog();
  641. }
  642. function create_empty_blog( $domain, $path, $weblog_title, $site_id = 1 ) {
  643. $domain = addslashes( $domain );
  644. $weblog_title = addslashes( $weblog_title );
  645. if( empty($path) )
  646. $path = '/';
  647. // Check if the domain has been used already. We should return an error message.
  648. if ( domain_exists($domain, $path, $site_id) )
  649. return __('error: Blog URL already taken.');
  650. // Need to backup wpdb table names, and create a new wp_blogs entry for new blog.
  651. // Need to get blog_id from wp_blogs, and create new table names.
  652. // Must restore table names at the end of function.
  653. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  654. return __('error: problem creating blog entry');
  655. switch_to_blog($blog_id);
  656. install_blog($blog_id);
  657. restore_current_blog();
  658. return $blog_id;
  659. }
  660. function get_blog_permalink( $_blog_id, $post_id ) {
  661. $key = "{$_blog_id}-{$post_id}-blog_permalink";
  662. $link = wp_cache_get( $key, 'site-options' );
  663. if( $link == false ) {
  664. switch_to_blog( $_blog_id );
  665. $link = get_permalink( $post_id );
  666. restore_current_blog();
  667. wp_cache_add( $key, $link, 'site-options', 360 );
  668. }
  669. return $link;
  670. }
  671. function get_blog_id_from_url( $domain, $path = '/' ) {
  672. global $wpdb;
  673. $domain = strtolower( $wpdb->escape( $domain ) );
  674. $path = strtolower( $wpdb->escape( $path ) );
  675. $id = wp_cache_get( md5( $domain . $path ), 'blog-id-cache' );
  676. if( $id == -1 ) { // blog does not exist
  677. return 0;
  678. } elseif( $id ) {
  679. return (int)$id;
  680. }
  681. $id = $wpdb->get_var( "SELECT blog_id FROM $wpdb->blogs WHERE domain = '$domain' and path = '$path' /* get_blog_id_from_url */" );
  682. if ( !$id ) {
  683. wp_cache_set( md5( $domain . $path ), -1, 'blog-id-cache' );
  684. return false;
  685. }
  686. wp_cache_set( md5( $domain . $path ), $id, 'blog-id-cache' );
  687. return $id;
  688. }
  689. // wpmu admin functions
  690. function wpmu_admin_do_redirect( $url = '' ) {
  691. $ref = '';
  692. if ( isset( $_GET['ref'] ) )
  693. $ref = $_GET['ref'];
  694. if ( isset( $_POST['ref'] ) )
  695. $ref = $_POST['ref'];
  696. if( $ref ) {
  697. $ref = wpmu_admin_redirect_add_updated_param( $ref );
  698. wp_redirect( $ref );
  699. exit();
  700. }
  701. if( empty( $_SERVER['HTTP_REFERER'] ) == false ) {
  702. wp_redirect( $_SERVER['HTTP_REFERER'] );
  703. exit();
  704. }
  705. $url = wpmu_admin_redirect_add_updated_param( $url );
  706. if( isset( $_GET['redirect'] ) ) {
  707. if( substr( $_GET['redirect'], 0, 2 ) == 's_' ) {
  708. $url .= "&action=blogs&s=". wp_specialchars( substr( $_GET['redirect'], 2 ) );
  709. }
  710. } elseif( isset( $_POST['redirect'] ) ) {
  711. $url = wpmu_admin_redirect_add_updated_param( $_POST['redirect'] );
  712. }
  713. wp_redirect( $url );
  714. exit();
  715. }
  716. function wpmu_admin_redirect_add_updated_param( $url = '' ) {
  717. if( strpos( $url, 'updated=true' ) === false ) {
  718. if( strpos( $url, '?' ) === false ) {
  719. return $url . '?updated=true';
  720. } else {
  721. return $url . '&updated=true';
  722. }
  723. }
  724. return $url;
  725. }
  726. function is_blog_user( $blog_id = 0 ) {
  727. global $current_user, $wpdb;
  728. if ( !$blog_id )
  729. $blog_id = $wpdb->blogid;
  730. $cap_key = $wpdb->base_prefix . $blog_id . '_capabilities';
  731. if ( is_array($current_user->$cap_key) && in_array(1, $current_user->$cap_key) )
  732. return true;
  733. return false;
  734. }
  735. function validate_email( $email, $check_domain = true) {
  736. if (ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.
  737. '[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.
  738. '[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $email))
  739. {
  740. if ($check_domain && function_exists('checkdnsrr')) {
  741. list (, $domain) = explode('@', $email);
  742. if (checkdnsrr($domain.'.', 'MX') || checkdnsrr($domain.'.', 'A')) {
  743. return true;
  744. }
  745. return false;
  746. }
  747. return true;
  748. }
  749. return false;
  750. }
  751. function is_email_address_unsafe( $user_email ) {
  752. $banned_names = get_site_option( "banned_email_domains" );
  753. if ($banned_names && !is_array( $banned_names )) {
  754. $banned_names = explode( "\n", $banned_names);
  755. }
  756. if ( is_array( $banned_names ) && empty( $banned_names ) == false ) {
  757. $email_domain = strtolower( substr( $user_email, 1 + strpos( $user_email, '@' ) ) );
  758. foreach( (array) $banned_names as $banned_domain ) {
  759. if( $banned_domain == '' )
  760. continue;
  761. if (
  762. strstr( $email_domain, $banned_domain ) ||
  763. (
  764. strstr( $banned_domain, '/' ) &&
  765. preg_match( $banned_domain, $email_domain )
  766. )
  767. )
  768. return true;
  769. }
  770. }
  771. return false;
  772. }
  773. function wpmu_validate_user_signup($user_name, $user_email) {
  774. global $wpdb;
  775. $errors = new WP_Error();
  776. $user_name = preg_replace( "/\s+/", '', sanitize_user( $user_name, true ) );
  777. $user_email = sanitize_email( $user_email );
  778. if ( empty( $user_name ) )
  779. $errors->add('user_name', __("Please enter a username"));
  780. $maybe = array();
  781. preg_match( "/[a-z0-9]+/", $user_name, $maybe );
  782. if( $user_name != $maybe[0] ) {
  783. $errors->add('user_name', __("Only lowercase letters and numbers allowed"));
  784. }
  785. $illegal_names = get_site_option( "illegal_names" );
  786. if( is_array( $illegal_names ) == false ) {
  787. $illegal_names = array( "www", "web", "root", "admin", "main", "invite", "administrator" );
  788. add_site_option( "illegal_names", $illegal_names );
  789. }
  790. if( in_array( $user_name, $illegal_names ) == true ) {
  791. $errors->add('user_name', __("That username is not allowed"));
  792. }
  793. if( is_email_address_unsafe( $user_email ) )
  794. $errors->add('user_email', __("You cannot use that email address to signup. We are having problems with them blocking some of our email. Please use another email provider."));
  795. if( strlen( $user_name ) < 4 ) {
  796. $errors->add('user_name', __("Username must be at least 4 characters"));
  797. }
  798. if ( strpos( " " . $user_name, "_" ) != false )
  799. $errors->add('user_name', __("Sorry, usernames may not contain the character '_'!"));
  800. // all numeric?
  801. $match = array();
  802. preg_match( '/[0-9]*/', $user_name, $match );
  803. if ( $match[0] == $user_name )
  804. $errors->add('user_name', __("Sorry, usernames must have letters too!"));
  805. if ( !is_email( $user_email ) )
  806. $errors->add('user_email', __("Please enter a correct email address"));
  807. if ( !validate_email( $user_email ) )
  808. $errors->add('user_email', __("Please check your email address."));
  809. $limited_email_domains = get_site_option( 'limited_email_domains' );
  810. if ( is_array( $limited_email_domains ) && empty( $limited_email_domains ) == false ) {
  811. $emaildomain = substr( $user_email, 1 + strpos( $user_email, '@' ) );
  812. if( in_array( $emaildomain, $limited_email_domains ) == false ) {
  813. $errors->add('user_email', __("Sorry, that email address is not allowed!"));
  814. }
  815. }
  816. // Check if the username has been used already.
  817. if ( username_exists($user_name) )
  818. $errors->add('user_name', __("Sorry, that username already exists!"));
  819. // Check if the email address has been used already.
  820. if ( email_exists($user_email) )
  821. $errors->add('user_email', __("Sorry, that email address is already used!"));
  822. // Has someone already signed up for this username?
  823. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  824. if ( $signup != null ) {
  825. $registered_at = mysql2date('U', $signup->registered);
  826. $now = current_time( 'timestamp', true );
  827. $diff = $now - $registered_at;
  828. // If registered more than two days ago, cancel registration and let this signup go through.
  829. if ( $diff > 172800 ) {
  830. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_login = %s", $user_name) );
  831. } else {
  832. $errors->add('user_name', __("That username is currently reserved but may be available in a couple of days."));
  833. }
  834. if( $signup->active == 0 && $signup->user_email == $user_email )
  835. $errors->add('user_email_used', __("username and email used"));
  836. }
  837. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  838. if ( $signup != null ) {
  839. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  840. // If registered more than two days ago, cancel registration and let this signup go through.
  841. if ( $diff > 172800 ) {
  842. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE user_email = %s", $user_email) );
  843. } else {
  844. $errors->add('user_email', __("That email address has already been used. Please check your inbox for an activation email. It will become available in a couple of days if you do nothing."));
  845. }
  846. }
  847. $result = array('user_name' => $user_name, 'user_email' => $user_email, 'errors' => $errors);
  848. return apply_filters('wpmu_validate_user_signup', $result);
  849. }
  850. function wpmu_validate_blog_signup($blogname, $blog_title, $user = '') {
  851. global $wpdb, $domain, $base, $current_site;
  852. $blogname = preg_replace( "/\s+/", '', sanitize_user( $blogname, true ) );
  853. $blog_title = strip_tags( $blog_title );
  854. $blog_title = substr( $blog_title, 0, 50 );
  855. $errors = new WP_Error();
  856. $illegal_names = get_site_option( "illegal_names" );
  857. if( $illegal_names == false ) {
  858. $illegal_names = array( "www", "web", "root", "admin", "main", "invite", "administrator" );
  859. add_site_option( "illegal_names", $illegal_names );
  860. }
  861. if ( empty( $blogname ) )
  862. $errors->add('blogname', __("Please enter a blog name"));
  863. $maybe = array();
  864. preg_match( "/[a-z0-9]+/", $blogname, $maybe );
  865. if( $blogname != $maybe[0] ) {
  866. $errors->add('blogname', __("Only lowercase letters and numbers allowed"));
  867. }
  868. if( in_array( $blogname, $illegal_names ) == true ) {
  869. $errors->add('blogname', __("That name is not allowed"));
  870. }
  871. if( strlen( $blogname ) < 4 && !is_site_admin() ) {
  872. $errors->add('blogname', __("Blog name must be at least 4 characters"));
  873. }
  874. if ( strpos( " " . $blogname, "_" ) != false )
  875. $errors->add('blogname', __("Sorry, blog names may not contain the character '_'!"));
  876. // do not allow users to create a blog that conflicts with a page on the main blog.
  877. if ( constant( "VHOST" ) == 'no' && $wpdb->get_var( $wpdb->prepare( "SELECT post_name FROM " . $wpdb->get_blog_prefix( $current_site->blog_id ) . "posts WHERE post_type = 'page' AND post_name = %s", $blogname ) ) ) {
  878. $errors->add( 'blogname', __( "Sorry, you may not use that blog name" ) );
  879. }
  880. // all numeric?
  881. $match = array();
  882. preg_match( '/[0-9]*/', $blogname, $match );
  883. if ( $match[0] == $blogname )
  884. $errors->add('blogname', __("Sorry, blog names must have letters too!"));
  885. $blogname = apply_filters( "newblogname", $blogname );
  886. $blog_title = stripslashes( $blog_title );
  887. if ( empty( $blog_title ) )
  888. $errors->add('blog_title', __("Please enter a blog title"));
  889. // Check if the domain/path has been used already.
  890. if( constant( "VHOST" ) == 'yes' ) {
  891. $mydomain = "$blogname.$domain";
  892. $path = $base;
  893. } else {
  894. $mydomain = "$domain";
  895. $path = $base.$blogname.'/';
  896. }
  897. if ( domain_exists($mydomain, $path) )
  898. $errors->add('blogname', __("Sorry, that blog already exists!"));
  899. if ( username_exists( $blogname ) ) {
  900. if ( is_object( $user ) == false || ( is_object($user) && ( $user->user_login != $blogname ) ) )
  901. $errors->add( 'blogname', __( "Sorry, that blog is reserved!" ) );
  902. }
  903. // Has someone already signed up for this domain?
  904. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) ); // TODO: Check email too?
  905. if ( ! empty($signup) ) {
  906. $diff = current_time( 'timestamp', true ) - mysql2date('U', $signup->registered);
  907. // If registered more than two days ago, cancel registration and let this signup go through.
  908. if ( $diff > 172800 ) {
  909. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->signups WHERE domain = %s AND path = %s", $mydomain, $path) );
  910. } else {
  911. $errors->add('blogname', __("That blog is currently reserved but may be available in a couple days."));
  912. }
  913. }
  914. $result = array('domain' => $mydomain, 'path' => $path, 'blogname' => $blogname, 'blog_title' => $blog_title, 'errors' => $errors);
  915. return apply_filters('wpmu_validate_blog_signup', $result);
  916. }
  917. // Record signup information for future activation. wpmu_validate_signup() should be run
  918. // on the inputs before calling wpmu_signup().
  919. function wpmu_signup_blog($domain, $path, $title, $user, $user_email, $meta = '') {
  920. global $wpdb;
  921. $key = substr( md5( time() . rand() . $domain ), 0, 16 );
  922. $meta = serialize($meta);
  923. $domain = $wpdb->escape($domain);
  924. $path = $wpdb->escape($path);
  925. $title = $wpdb->escape($title);
  926. $wpdb->insert( $wpdb->signups, array(
  927. 'domain' => $domain,
  928. 'path' => $path,
  929. 'title' => $title,
  930. 'user_login' => $user,
  931. 'user_email' => $user_email,
  932. 'registered' => current_time('mysql', true),
  933. 'activation_key' => $key,
  934. 'meta' => $meta
  935. ) );
  936. wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta);
  937. }
  938. function wpmu_signup_user($user, $user_email, $meta = '') {
  939. global $wpdb;
  940. // Format data
  941. $user = preg_replace( "/\s+/", '', sanitize_user( $user, true ) );
  942. $user_email = sanitize_email( $user_email );
  943. $key = substr( md5( time() . rand() . $user_email ), 0, 16 );
  944. $meta = serialize($meta);
  945. $wpdb->insert( $wpdb->signups, array(
  946. 'domain' => '',
  947. 'path' => '',
  948. 'title' => '',
  949. 'user_login' => $user,
  950. 'user_email' => $user_email,
  951. 'registered' => current_time('mysql', true),
  952. 'activation_key' => $key,
  953. 'meta' => $meta
  954. ) );
  955. wpmu_signup_user_notification($user, $user_email, $key, $meta);
  956. }
  957. // Notify user of signup success.
  958. function wpmu_signup_blog_notification($domain, $path, $title, $user, $user_email, $key, $meta = '') {
  959. global $current_site;
  960. if( !apply_filters('wpmu_signup_blog_notification', $domain, $path, $title, $user, $user_email, $key, $meta) )
  961. return false;
  962. // Send email with activation link.
  963. if( constant( "VHOST" ) == 'no' || $current_site->id != 1 ) {
  964. $activate_url = "http://" . $current_site->domain . $current_site->path . "wp-activate.php?key=$key";
  965. } else {
  966. $activate_url = "http://{$domain}{$path}wp-activate.php?key=$key";
  967. }
  968. $activate_url = clean_url($activate_url);
  969. $admin_email = get_site_option( "admin_email" );
  970. if( $admin_email == '' )
  971. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  972. $from_name = get_site_option( "site_name" ) == '' ? 'WordPress' : wp_specialchars( get_site_option( "site_name" ) );
  973. $message_headers = "MIME-Version: 1.0\n" . "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  974. $message = sprintf( apply_filters( 'wpmu_signup_blog_notification_email', __( "To activate your blog, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\nAfter you activate, you can visit your blog here:\n\n%s" ) ), $activate_url, clean_url( "http://{$domain}{$path}" ), $key );
  975. // TODO: Don't hard code activation link.
  976. $subject = sprintf( apply_filters( 'wpmu_signup_blog_notification_subject', __( '[%1s] Activate %2s' ) ), $from_name, clean_url( 'http://' . $domain . $path ) );
  977. wp_mail($user_email, $subject, $message, $message_headers);
  978. return true;
  979. }
  980. function wpmu_signup_user_notification($user, $user_email, $key, $meta = '') {
  981. global $current_site;
  982. if( !apply_filters('wpmu_signup_user_notification', $user, $user_email, $key, $meta) )
  983. return false;
  984. // Send email with activation link.
  985. $admin_email = get_site_option( "admin_email" );
  986. if( $admin_email == '' )
  987. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  988. $from_name = get_site_option( "site_name" ) == '' ? 'WordPress' : wp_specialchars( get_site_option( "site_name" ) );
  989. $message_headers = "MIME-Version: 1.0\n" . "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  990. $message = sprintf( apply_filters( 'wpmu_signup_user_notification_email', __( "To activate your user, please click the following link:\n\n%s\n\nAfter you activate, you will receive *another email* with your login.\n\n" ) ), site_url( "wp-activate.php?key=$key" ), $key );
  991. // TODO: Don't hard code activation link.
  992. $subject = sprintf( __( apply_filters( 'wpmu_signup_user_notification_subject', '[%1s] Activate %2s' ) ), $from_name, $user);
  993. wp_mail($user_email, $subject, $message, $message_headers);
  994. return true;
  995. }
  996. function wpmu_activate_signup($key) {
  997. global $wpdb, $current_site;
  998. $signup = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->signups WHERE activation_key = %s", $key) );
  999. if ( empty($signup) )
  1000. return new WP_Error('invalid_key', __('Invalid activation key.'));
  1001. if ( $signup->active )
  1002. return new WP_Error('already_active', __('The blog is already active.'), $signup);
  1003. $meta = unserialize($signup->meta);
  1004. $user_login = $wpdb->escape($signup->user_login);
  1005. $user_email = $wpdb->escape($signup->user_email);
  1006. wpmu_validate_user_signup($user_login, $user_email);
  1007. $password = generate_random_password();
  1008. $user_id = username_exists($user_login);
  1009. if ( ! $user_id )
  1010. $user_id = wpmu_create_user($user_login, $password, $user_email);
  1011. else
  1012. $user_already_exists = true;
  1013. if ( ! $user_id )
  1014. return new WP_Error('create_user', __('Could not create user'), $signup);
  1015. $now = current_time('mysql', true);
  1016. if ( empty($signup->domain) ) {
  1017. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  1018. if ( isset( $user_already_exists ) )
  1019. return new WP_Error( 'user_already_exists', __( 'That username is already activated.' ), $signup);
  1020. wpmu_welcome_user_notification($user_id, $password, $meta);
  1021. $user_site = get_site_option( 'dashboard_blog', $current_site->blog_id );
  1022. if ( $user_site == false ) {
  1023. add_user_to_blog( '1', $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
  1024. } else {
  1025. add_user_to_blog( $user_site, $user_id, get_site_option( 'default_user_role', 'subscriber' ) );
  1026. }
  1027. add_new_user_to_blog( $user_id, $user_email, $meta );
  1028. do_action('wpmu_activate_user', $user_id, $password, $meta);
  1029. return array('user_id' => $user_id, 'password' => $password, 'meta' => $meta);
  1030. }
  1031. wpmu_validate_blog_signup($signup->domain, $signup->title);
  1032. $blog_id = wpmu_create_blog( $signup->domain, $signup->path, $signup->title, $user_id, $meta, $wpdb->siteid );
  1033. // TODO: What to do if we create a user but cannot create a blog?
  1034. if ( is_wp_error($blog_id) ) {
  1035. // If blog is taken, that means a previous attempt to activate this blog failed in between creating the blog and
  1036. // setting the activation flag. Let's just set the active flag and instruct the user to reset their password.
  1037. if ( 'blog_taken' == $blog_id->get_error_code() ) {
  1038. $blog_id->add_data( $signup );
  1039. $wpdb->update( $wpdb->signups, array( 'active' => 1, 'activated' => $now ), array( 'activation_key' => $key ) );
  1040. }
  1041. return $blog_id;
  1042. }
  1043. $wpdb->update( $wpdb->signups, array('active' => 1, 'activated' => $now), array('activation_key' => $key) );
  1044. wpmu_welcome_notification($blog_id, $user_id, $password, $signup->title, $meta);
  1045. do_action('wpmu_activate_blog', $blog_id, $user_id, $password, $signup->title, $meta);
  1046. return array('blog_id' => $blog_id, 'user_id' => $user_id, 'password' => $password, 'title' => $signup->title, 'meta' => $meta);
  1047. }
  1048. function generate_random_password( $len = 8 ) {
  1049. $random_password = substr(md5(uniqid(microtime())), 0, intval( $len ) );
  1050. $random_password = apply_filters('random_password', $random_password);
  1051. return $random_password;
  1052. }
  1053. function wpmu_create_user( $user_name, $password, $email) {
  1054. $user_name = preg_replace( "/\s+/", '', sanitize_user( $user_name, true ) );
  1055. if ( username_exists($user_name) )
  1056. return false;
  1057. // Check if the email address has been used already.
  1058. if ( email_exists($email) )
  1059. return false;
  1060. $user_id = wp_create_user( $user_name, $password, $email );
  1061. $user = new WP_User($user_id);
  1062. // Newly created users have no roles or caps until they are added to a blog.
  1063. update_user_option($user_id, 'capabilities', '');
  1064. update_user_option($user_id, 'user_level', '');
  1065. do_action( 'wpmu_new_user', $user_id );
  1066. return $user_id;
  1067. }
  1068. function wpmu_create_blog($domain, $path, $title, $user_id, $meta = '', $site_id = 1) {
  1069. $domain = preg_replace( "/\s+/", '', sanitize_user( $domain, true ) );
  1070. if( constant( 'VHOST' ) == 'yes' )
  1071. $domain = str_replace( '@', '', $domain );
  1072. $title = strip_tags( $title );
  1073. $user_id = (int) $user_id;
  1074. if( empty($path) )
  1075. $path = '/';
  1076. // Check if the domain has been used already. We should return an error message.
  1077. if ( domain_exists($domain, $path, $site_id) )
  1078. return new WP_Error('blog_taken', __('Blog already exists.'));
  1079. if ( !defined("WP_INSTALLING") )
  1080. define( "WP_INSTALLING", true );
  1081. if ( ! $blog_id = insert_blog($domain, $path, $site_id) )
  1082. return new WP_Error('insert_blog', __('Could not create blog.'));
  1083. switch_to_blog($blog_id);
  1084. install_blog($blog_id, $title);
  1085. install_blog_defaults($blog_id, $user_id);
  1086. add_user_to_blog($blog_id, $user_id, 'administrator');
  1087. if ( is_array($meta) ) foreach ($meta as $key => $value) {
  1088. if( $key == 'public' || $key == 'archived' || $key == 'mature' || $key == 'spam' || $key == 'deleted' || $key == 'lang_id' ) {
  1089. update_blog_status( $blog_id, $key, $value );
  1090. } else {
  1091. update_option( $key, $value );
  1092. }
  1093. }
  1094. add_option( 'WPLANG', get_site_option( 'WPLANG' ) );
  1095. update_option( 'blog_public', $meta['public'] );
  1096. if ( !is_site_admin() && get_usermeta( $user_id, 'primary_blog' ) == get_site_option( 'dashboard_blog', 1 ) )
  1097. update_usermeta( $user_id, 'primary_blog', $blog_id );
  1098. restore_current_blog();
  1099. do_action( 'wpmu_new_blog', $blog_id, $user_id );
  1100. return $blog_id;
  1101. }
  1102. function newblog_notify_siteadmin( $blog_id, $deprecated = '' ) {
  1103. global $current_site;
  1104. if( get_site_option( 'registrationnotification' ) != 'yes' )
  1105. return false;
  1106. $email = get_site_option( 'admin_email' );
  1107. if( is_email($email) == false )
  1108. return false;
  1109. $options_site_url = clean_url("http://{$current_site->domain}{$current_site->path}wp-admin/wpmu-options.php");
  1110. switch_to_blog( $blog_id );
  1111. $blogname = get_option( 'blogname' );
  1112. $siteurl = get_option( 'siteurl' );
  1113. restore_current_blog();
  1114. $msg = sprintf( __( "New Blog: %1s
  1115. URL: %2s
  1116. Remote IP: %3s
  1117. Disable these notifications: %4s"), $blogname, $siteurl, $_SERVER['REMOTE_ADDR'], $options_site_url);
  1118. $msg = apply_filters( 'newblog_notify_siteadmin', $msg );
  1119. wp_mail( $email, sprintf( __( "New Blog Registration: %s" ), $siteurl ), $msg );
  1120. return true;
  1121. }
  1122. function newuser_notify_siteadmin( $user_id ) {
  1123. global $current_site;
  1124. if( get_site_option( 'registrationnotification' ) != 'yes' )
  1125. return false;
  1126. $email = get_site_option( 'admin_email' );
  1127. if( is_email($email) == false )
  1128. return false;
  1129. $user = new WP_User($user_id);
  1130. $options_site_url = clean_url("http://{$current_site->domain}{$current_site->path}wp-admin/wpmu-options.php");
  1131. $msg = sprintf(__("New User: %1s
  1132. Remote IP: %2s
  1133. Disable these notifications: %3s"), $user->user_login, $_SERVER['REMOTE_ADDR'], $options_site_url);
  1134. $msg = apply_filters( 'newuser_notify_siteadmin', $msg );
  1135. wp_mail( $email, sprintf(__("New User Registration: %s"), $user->user_login), $msg );
  1136. return true;
  1137. }
  1138. function domain_exists($domain, $path, $site_id = 1) {
  1139. global $wpdb;
  1140. return $wpdb->get_var( $wpdb->prepare("SELECT blog_id FROM $wpdb->blogs WHERE domain = %s AND path = %s AND site_id = %d", $domain, $path, $site_id) );
  1141. }
  1142. function insert_blog($domain, $path, $site_id) {
  1143. global $wpdb;
  1144. $path = trailingslashit($path);
  1145. $site_id = (int) $site_id;
  1146. $result = $wpdb->insert( $wpdb->blogs, array('site_id' => $site_id, 'domain' => $domain, 'path' => $path, 'registered' => current_time('mysql')) );
  1147. if ( ! $result )
  1148. return false;
  1149. refresh_blog_details($wpdb->insert_id);
  1150. return $wpdb->insert_id;
  1151. }
  1152. // Install an empty blog. wpdb should already be switched.
  1153. function install_blog($blog_id, $blog_title = '') {
  1154. global $wpdb, $table_prefix, $wp_roles;
  1155. $wpdb->suppress_errors();
  1156. // Cast for security
  1157. $blog_id = (int) $blog_id;
  1158. require_once( ABSPATH . 'wp-admin/includes/upgrade.php');
  1159. if ( $wpdb->get_results("SELECT ID FROM $wpdb->posts") )
  1160. die(__('<h1>Already Installed</h1><p>You appear to have already installed WordPress. To reinstall please clear your old database tables first.</p>') . '</body></html>');
  1161. $wpdb->suppress_errors( false);
  1162. $url = get_blogaddress_by_id($blog_id);
  1163. // Set everything up
  1164. make_db_current_silent();
  1165. populate_options();
  1166. populate_roles();
  1167. $wp_roles->_init();
  1168. // fix url.
  1169. update_option('siteurl', $url);
  1170. update_option('home', $url);
  1171. update_option('fileupload_url', $url . "files" );
  1172. update_option('upload_path', "wp-content/blogs.dir/" . $blog_id . "/files");
  1173. update_option('blogname', stripslashes( $blog_title ) );
  1174. update_option('admin_email', '');
  1175. $wpdb->update( $wpdb->options, array('option_value' => ''), array('option_name' => 'admin_email') );
  1176. // Default category
  1177. $wpdb->insert( $wpdb->terms, array('term_id' => 1, 'name' => __('Uncategorized'), 'slug' => sanitize_title(__('Uncategorized')), 'term_group' => 0) );
  1178. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => 1, 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1) );
  1179. // Default link category
  1180. $cat_name = __('Blogroll');
  1181. $cat_slug = sanitize_title($cat_name);
  1182. $blogroll_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM {$wpdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
  1183. if( $blogroll_id == null ) {
  1184. $wpdb->insert( $wpdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
  1185. $blogroll_id = $wpdb->insert_id;
  1186. }
  1187. $wpdb->insert( $wpdb->terms, array('term_id' => $blogroll_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) );
  1188. $wpdb->insert( $wpdb->term_taxonomy, array('term_id' => $blogroll_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 2) );
  1189. update_option('default_link_category', $blogroll_id);
  1190. // remove all perms
  1191. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'user_level') );
  1192. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE meta_key = %s", $table_prefix.'capabilities') );
  1193. $wpdb->suppress_errors( false );
  1194. }
  1195. // should be switched already as $blog_id is ignored.
  1196. function install_blog_defaults($blog_id, $user_id) {
  1197. global $wpdb, $wp_rewrite, $current_site, $table_prefix;
  1198. $wpdb->suppress_errors();
  1199. // Cast for security
  1200. $user_id = (int) $user_id;
  1201. $blog_id = (int) $blog_id;
  1202. // Default links
  1203. $wpdb->insert( $wpdb->links, array( 'link_url' => 'http://wordpress.com/', 'link_name' => 'WordPress.com', 'link_owner' => $user_id, 'link_rss' => 'http://en.blog.wordpress.com/feed/', 'link_notes' => '' ) );
  1204. $wpdb->insert( $wpdb->links, array( 'link_url' => 'http://wordpress.org/', 'link_name' => 'WordPress.org', 'link_owner' => $user_id, 'link_rss' => 'http://wordpress.org/development/feed/', 'link_notes' => '' ) );
  1205. $wpdb->insert( $wpdb->term_relationships, array('object_id' => 1, 'term_taxonomy_id' => 2));
  1206. $wpdb->insert( $wpdb->term_relationships, array('object_id' => 2, 'term_taxonomy_id' => 2));
  1207. // First post
  1208. $now = date('Y-m-d H:i:s');
  1209. $now_gmt = gmdate('Y-m-d H:i:s');
  1210. $first_post = get_site_option( 'first_post' );
  1211. if( $first_post == false ) {
  1212. $first_post = stripslashes( __( 'Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' ) );
  1213. }
  1214. $first_post = str_replace( "SITE_URL", clean_url("http://" . $current_site->domain . $current_site->path), $first_post );
  1215. $first_post = str_replace( "SITE_NAME", $current_site->site_name, $first_post );
  1216. $wpdb->insert( $wpdb->posts, array(
  1217. 'post_author' => $user_id,
  1218. 'post_date' => $now,
  1219. 'post_date_gmt' => $now_gmt,
  1220. 'post_content' => stripslashes( $first_post ),
  1221. 'post_excerpt' => '',
  1222. 'post_title' => __('Hello world!'),
  1223. 'post_name' => __('hello-world'),
  1224. 'post_modified' => $now,
  1225. 'post_modified_gmt' => $now_gmt,
  1226. 'comment_count' => 1,
  1227. 'to_ping' => '',
  1228. 'pinged' => '',
  1229. 'post_content_filtered' => ''
  1230. ) );
  1231. $wpdb->insert( $wpdb->term_relationships, array('object_id' => 1, 'term_taxonomy_id' => 1));
  1232. update_option( "post_count", 1 );
  1233. // First page
  1234. $wpdb->insert( $wpdb->posts, array(
  1235. 'post_author' => $user_id,
  1236. 'post_date' => $now,
  1237. 'post_date_gmt' => $now_gmt,
  1238. 'post_content' => get_site_option( 'first_page' ),
  1239. 'post_excerpt' => '',
  1240. 'post_title' => __('About'),
  1241. 'post_name' => __('about'),
  1242. 'post_modified' => $now,
  1243. 'post_modified_gmt' => $now_gmt,
  1244. 'post_status' => 'publish',
  1245. 'post_type' => 'page',
  1246. 'to_ping' => '',
  1247. 'pinged' => '',
  1248. 'post_content_filtered' => ''
  1249. ) );
  1250. // Flush rules to pick up the new page.
  1251. $wp_rewrite->init();
  1252. $wp_rewrite->flush_rules();
  1253. // Default comment
  1254. $wpdb->insert( $wpdb->comments, array(
  1255. 'comment_post_ID' => '1',
  1256. 'comment_author' => __( get_site_option( 'first_comment_author' ) ),
  1257. 'comment_author_email' => '',
  1258. 'comment_author_url' => get_site_option( 'first_comment_url' ),
  1259. 'comment_author_IP' => '127.0.0.1',
  1260. 'comment_date' => $now,
  1261. 'comment_date_gmt' => $now_gmt,
  1262. 'comment_content' => __( get_site_option( 'first_comment' ) )
  1263. ) );
  1264. $user = new WP_User($user_id);
  1265. $wpdb->update( $wpdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') );
  1266. // Remove all perms except for the login user.
  1267. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'user_level') );
  1268. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'capabilities') );
  1269. // Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id.
  1270. if ( !is_site_admin( $user->user_login ) && $user_id != 1 )
  1271. $wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $wpdb->base_prefix.'1_capabilities') );
  1272. $wpdb->suppress_errors( false );
  1273. }
  1274. function wpmu_welcome_notification($blog_id, $user_id, $password, $title, $meta = '') {
  1275. global $current_site;
  1276. if( !apply_filters('wpmu_welcome_notification', $blog_id, $user_id, $password, $title, $meta) )
  1277. return false;
  1278. $welcome_email = stripslashes( get_site_option( 'welcome_email' ) );
  1279. if( $welcome_email == false )
  1280. $welcome_email = stripslashes( __( "Dear User,
  1281. Your new SITE_NAME blog has been successfully set up at:
  1282. BLOG_URL
  1283. You can log in to the administrator account with the following information:
  1284. Username: USERNAME
  1285. Password: PASSWORD
  1286. Login Here: BLOG_URLwp-login.php
  1287. We hope you enjoy your new weblog.
  1288. Thanks!
  1289. --The WordPress Team
  1290. SITE_NAME" ) );
  1291. $url = get_blogaddress_by_id($blog_id);
  1292. $user = new WP_User($user_id);
  1293. $welcome_email = str_replace( "SITE_NAME", $current_site->site_name, $welcome_email );
  1294. $welcome_email = str_replace( "BLOG_TITLE", $title, $welcome_email );
  1295. $welcome_email = str_replace( "BLOG_URL", $url, $welcome_email );
  1296. $welcome_email = str_replace( "USERNAME", $user->user_login, $welcome_email );
  1297. $welcome_email = str_replace( "PASSWORD", $password, $welcome_email );
  1298. $welcome_email = apply_filters( "update_welcome_email", $welcome_email, $blog_id, $user_id, $password, $title, $meta);
  1299. $admin_email = get_site_option( "admin_email" );
  1300. if( $admin_email == '' )
  1301. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1302. $from_name = get_site_option( "site_name" ) == '' ? 'WordPress' : wp_specialchars( get_site_option( "site_name" ) );
  1303. $message_headers = "MIME-Version: 1.0\n" . "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1304. $message = $welcome_email;
  1305. if( empty( $current_site->site_name ) )
  1306. $current_site->site_name = "WordPress MU";
  1307. $subject = apply_filters( 'update_welcome_subject', sprintf(__('New %1$s Blog: %2$s'), $current_site->site_name, stripslashes( $title ) ) );
  1308. wp_mail($user->user_email, $subject, $message, $message_headers);
  1309. return true;
  1310. }
  1311. function wpmu_welcome_user_notification($user_id, $password, $meta = '') {
  1312. global $current_site;
  1313. if( !apply_filters('wpmu_welcome_user_notification', $user_id, $password, $meta) )
  1314. return false;
  1315. $welcome_email = get_site_option( 'welcome_user_email' );
  1316. $user = new WP_User($user_id);
  1317. $welcome_email = apply_filters( "update_welcome_user_email", $welcome_email, $user_id, $password, $meta);
  1318. $welcome_email = str_replace( "SITE_NAME", $current_site->site_name, $welcome_email );
  1319. $welcome_email = str_replace( "USERNAME", $user->user_login, $welcome_email );
  1320. $welcome_email = str_replace( "PASSWORD", $password, $welcome_email );
  1321. $welcome_email = str_replace( "LOGINLINK", wp_login_url(), $welcome_email );
  1322. $admin_email = get_site_option( "admin_email" );
  1323. if( $admin_email == '' )
  1324. $admin_email = 'support@' . $_SERVER['SERVER_NAME'];
  1325. $from_name = get_site_option( "site_name" ) == '' ? 'WordPress' : wp_specialchars( get_site_option( "site_name" ) );
  1326. $message_headers = "MIME-Version: 1.0\n" . "From: \"{$from_name}\" <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_option('blog_charset') . "\"\n";
  1327. $message = $welcome_email;
  1328. if( empty( $current_site->site_name ) )
  1329. $current_site->site_name = "WordPress MU";
  1330. $subject = apply_filters( 'update_welcome_user_subject', sprintf(__('New %1$s User: %2$s'), $current_site->site_name, $user->user_login) );
  1331. wp_mail($user->user_email, $subject, $message, $message_headers);
  1332. return true;
  1333. }
  1334. function get_current_site() {
  1335. global $current_site;
  1336. return $current_site;
  1337. }
  1338. function get_user_id_from_string( $string ) {
  1339. global $wpdb;
  1340. $user_id = 0;
  1341. if ( is_email( $string ) ) {
  1342. $user = get_user_by_email($string);
  1343. if ( $user )
  1344. $user_id = $user->ID;
  1345. } elseif ( is_numeric( $string ) ) {
  1346. $user_id = $string;
  1347. } else {
  1348. $user = get_userdatabylogin($string);
  1349. if ( $user )
  1350. $user_id = $user->ID;
  1351. }
  1352. return $user_id;
  1353. }
  1354. function get_most_recent_post_of_user( $user_id ) {
  1355. global $wpdb;
  1356. $user_blogs = get_blogs_of_user( (int) $user_id );
  1357. $most_recent_post = array();
  1358. // Walk through each blog and get the most recent post
  1359. // published by $user_id
  1360. foreach ( (array) $user_blogs as $blog ) {
  1361. $recent_post = $wpdb->get_row( $wpdb->prepare("SELECT ID, post_date_gmt FROM {$wpdb->base_prefix}{$blog->userblog_id}_posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A);
  1362. // Make sure we found a post
  1363. if ( isset($recent_post['ID']) ) {
  1364. $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
  1365. // If this is the first post checked or if this post is
  1366. // newer than the current recent post, make it the new
  1367. // most recent post.
  1368. if (
  1369. !isset($most_recent_post['post_gmt_ts'])
  1370. || ($post_gmt_ts > $most_recent_post['post_gmt_ts'])
  1371. ) {
  1372. $most_recent_post = array(
  1373. 'blog_id' => $blog->userblog_id,
  1374. 'post_id' => $recent_post['ID'],
  1375. 'post_date_gmt' => $recent_post['post_date_gmt'],
  1376. 'post_gmt_ts' => $post_gmt_ts
  1377. );
  1378. }
  1379. }
  1380. }
  1381. return $most_recent_post;
  1382. }
  1383. /* Misc functions */
  1384. function fix_upload_details( $uploads ) {
  1385. $uploads['url'] = str_replace( UPLOADS, "files", $uploads['url'] );
  1386. $uploads['baseurl'] = str_replace( UPLOADS, "files", $uploads['baseurl'] );
  1387. return $uploads;
  1388. }
  1389. function get_dirsize( $directory ) {
  1390. $dirsize = get_transient( 'dirsize_cache' );
  1391. if ( is_array( $dirsize ) && isset( $dirsize[ $directory ][ 'size' ] ) ) {
  1392. return $dirsize[ $directory ][ 'size' ];
  1393. }
  1394. if ( false == is_array( $dirsize ) ) {
  1395. $dirsize = array();
  1396. }
  1397. $dirsize[ $directory ][ 'size' ] = recurse_dirsize( $directory );
  1398. set_transient( 'dirsize_cache', $dirsize, 3600 );
  1399. return $dirsize[ $directory ][ 'size' ];
  1400. }
  1401. function clear_dirsize_cache( $file = true ) {
  1402. delete_transient( 'dirsize_cache' );
  1403. return $file;
  1404. }
  1405. add_filter( 'wp_handle_upload', 'clear_dirsize_cache' );
  1406. add_action( 'delete_attachment', 'clear_dirsize_cache' );
  1407. function recurse_dirsize( $directory ) {
  1408. $size = 0;
  1409. if(substr($directory,-1) == '/') $directory = substr($directory,0,-1);
  1410. if(!file_exists($directory) || !is_dir($directory) || !is_readable($directory)) return false;
  1411. if($handle = opendir($directory)) {
  1412. while(($file = readdir($handle)) !== false) {
  1413. $path = $directory.'/'.$file;
  1414. if($file != '.' && $file != '..') {
  1415. if(is_file($path)) {
  1416. $size += filesize($path);
  1417. } elseif(is_dir($path)) {
  1418. $handlesize = recurse_dirsize($path);
  1419. if($handlesize >= 0) {
  1420. $size += $handlesize;
  1421. } else {
  1422. return false;
  1423. }
  1424. }
  1425. }
  1426. }
  1427. closedir($handle);
  1428. }
  1429. return $size;
  1430. }
  1431. function upload_is_user_over_quota( $echo = true ) {
  1432. if ( get_site_option( 'upload_space_check_disabled' ) ) {
  1433. return true;
  1434. }
  1435. $spaceAllowed = get_space_allowed();
  1436. if(empty($spaceAllowed) || !is_numeric($spaceAllowed))
  1437. $spaceAllowed = 10; // Default space allowed is 10 MB
  1438. $dirName = BLOGUPLOADDIR;
  1439. $size = get_dirsize($dirName) / 1024 / 1024;
  1440. if( ($spaceAllowed-$size) < 0 ) {
  1441. if( $echo )
  1442. _e( "Sorry, you have used your space allocation. Please delete some files to upload more files." ); //No space left
  1443. return true;
  1444. } else {
  1445. return false;
  1446. }
  1447. }
  1448. function check_upload_mimes($mimes) {
  1449. $site_exts = explode( " ", get_site_option( "upload_filetypes" ) );
  1450. foreach ( $site_exts as $ext ) {
  1451. foreach ( $mimes as $ext_pattern => $mime ) {
  1452. if( $ext != '' && strpos( $ext_pattern, $ext ) !== false ) {
  1453. $site_mimes[$ext_pattern] = $mime;
  1454. }
  1455. }
  1456. }
  1457. return $site_mimes;
  1458. }
  1459. function update_posts_count( $deprecated = '' ) {
  1460. global $wpdb;
  1461. update_option( "post_count", (int) $wpdb->get_var( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_status = 'publish' and post_type = 'post'" ) );
  1462. }
  1463. function wpmu_log_new_registrations( $blog_id, $user_id ) {
  1464. global $wpdb;
  1465. $user = new WP_User( (int) $user_id );
  1466. $wpdb->insert( $wpdb->registration_log, array('email' => $user->user_email, 'IP' => preg_replace( '/[^0-9., ]/', '',$_SERVER['REMOTE_ADDR'] ), 'blog_id' => $blog_id, 'date_registered' => current_time('mysql')) );
  1467. }
  1468. function fix_import_form_size( $size ) {
  1469. if( upload_is_user_over_quota( false ) == true ) {
  1470. return 0;
  1471. }
  1472. $spaceAllowed = 1024 * 1024 * get_space_allowed();
  1473. $dirName = BLOGUPLOADDIR;
  1474. $dirsize = get_dirsize($dirName) ;
  1475. if( $size > $spaceAllowed - $dirsize ) {
  1476. return $spaceAllowed - $dirsize; // remaining space
  1477. } else {
  1478. return $size; // default
  1479. }
  1480. }
  1481. if ( !function_exists('graceful_fail') ) :
  1482. function graceful_fail( $message ) {
  1483. $message = apply_filters('graceful_fail', $message);
  1484. $message_template = apply_filters( 'graceful_fail_template',
  1485. '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  1486. <html xmlns="http://www.w3.org/1999/xhtml"><head profile="http://gmpg.org/xfn/11">
  1487. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  1488. <title>Error!</title>
  1489. <style type="text/css">
  1490. img {
  1491. border: 0;
  1492. }
  1493. body {
  1494. line-height: 1.6em; font-family: Georgia, serif; width: 390px; margin: auto;
  1495. text-align: center;
  1496. }
  1497. .message {
  1498. font-size: 22px;
  1499. width: 350px;
  1500. margin: auto;
  1501. }
  1502. </style>
  1503. </head>
  1504. <body>
  1505. <p class="message">%s</p>
  1506. </body>
  1507. </html>' );
  1508. die( sprintf( $message_template, $message ) );
  1509. }
  1510. endif;
  1511. /* Delete blog */
  1512. class delete_blog {
  1513. function delete_blog() {
  1514. $this->reallydeleteblog = false;
  1515. add_action('admin_menu', array(&$this, 'admin_menu'));
  1516. add_action('admin_footer', array(&$this, 'admin_footer'));
  1517. }
  1518. function admin_footer() {
  1519. global $wpdb, $current_blog, $current_site;
  1520. if( $current_blog->domain . $current_blog->path == $current_site->domain . $current_site->path )
  1521. return false;
  1522. if( $this->reallydeleteblog == true ) {
  1523. wpmu_delete_blog( $wpdb->blogid );
  1524. }
  1525. }
  1526. function admin_menu() {
  1527. global $current_blog, $current_site;
  1528. if( $current_blog->domain . $current_blog->path != $current_site->domain . $current_site->path )
  1529. add_submenu_page('options-general.php', __('Delete Blog'), __('Delete Blog'), 'manage_options', 'delete-blog', array(&$this, 'plugin_content'));
  1530. }
  1531. function plugin_content() {
  1532. global $current_blog, $current_site;
  1533. $this->delete_blog_hash = get_settings('delete_blog_hash');
  1534. echo '<div class="wrap"><h2>' . __('Delete Blog') . '</h2>';
  1535. if( $_POST['action'] == "deleteblog" && $_POST['confirmdelete'] == '1' ) {
  1536. $hash = substr( md5( $_SERVER['REQUEST_URI'] . time() ), 0, 6 );
  1537. update_option( "delete_blog_hash", $hash );
  1538. $url_delete = get_option( "siteurl" ) . "/wp-admin/options-general.php?page=delete-blog&h=" . $hash;
  1539. $msg = __("Dear User,
  1540. You recently clicked the 'Delete Blog' link on your blog and filled in a
  1541. form on that page.
  1542. If you really want to delete your blog, click the link below. You will not
  1543. be asked to confirm again so only click this link if you are 100% certain:
  1544. URL_DELETE
  1545. If you delete your blog, please consider opening a new blog here
  1546. some time in the future! (But remember your current blog and username
  1547. are gone forever.)
  1548. Thanks for using the site,
  1549. Webmaster
  1550. SITE_NAME
  1551. ");
  1552. $msg = str_replace( "URL_DELETE", $url_delete, $msg );
  1553. $msg = str_replace( "SITE_NAME", $current_site->site_name, $msg );
  1554. wp_mail( get_option( "admin_email" ), "[ " . get_option( "blogname" ) . " ] ".__("Delete My Blog"), $msg );
  1555. ?>
  1556. <p><?php _e('Thank you. Please check your email for a link to confirm your action. Your blog will not be deleted until this link is clicked.') ?></p>
  1557. <?php
  1558. } elseif( isset( $_GET['h'] ) && $_GET['h'] != '' && get_option('delete_blog_hash') != false ) {
  1559. if( get_option('delete_blog_hash') == $_GET['h'] ) {
  1560. $this->reallydeleteblog = true;
  1561. echo "<p>" . sprintf(__('Thank you for using %s, your blog has been deleted. Happy trails to you until we meet again.'), $current_site->site_name) . "</p>";
  1562. } else {
  1563. $this->reallydeleteblog = false;
  1564. echo "<p>" . __("I'm sorry, the link you clicked is stale. Please select another option.") . "</p>";
  1565. }
  1566. } else {
  1567. ?>
  1568. <p><?php printf(__('If you do not want to use your %s blog any more, you can delete it using the form below. When you click <strong>Delete My Blog</strong> you will be sent an email with a link in it. Click on this link to delete your blog.'), $current_site->site_name); ?></p>
  1569. <p><?php _e('Remember, once deleted your blog cannot be restored.') ?></p>
  1570. <form method='post' name='deletedirect'>
  1571. <input type="hidden" name="page" value="<?php echo $_GET['page'] ?>" />
  1572. <input type='hidden' name='action' value='deleteblog' />
  1573. <p><input id='confirmdelete' type='checkbox' name='confirmdelete' value='1' /> <label for='confirmdelete'><strong><?php printf( __("I'm sure I want to permanently disable my blog, and I am aware I can never get it back or use %s again."), $current_blog->domain); ?></strong></label></p>
  1574. <p class="submit"><input type='submit' value='<?php _e('Delete My Blog Permanently &raquo;') ?>' /></p>
  1575. </form>
  1576. <?php
  1577. }
  1578. echo "</div>";
  1579. }
  1580. }
  1581. $delete_blog_obj = new delete_blog();
  1582. /* Global Categories */
  1583. function global_terms( $term_id, $deprecated = '' ) {
  1584. global $wpdb;
  1585. $term_id = intval( $term_id );
  1586. $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->terms WHERE term_id = %d", $term_id ) );
  1587. $global_id = $wpdb->get_var( $wpdb->prepare( "SELECT cat_ID FROM $wpdb->sitecategories WHERE category_nicename = %s", $c->slug ) );
  1588. if ( $global_id == null ) {
  1589. $wpdb->insert( $wpdb->sitecategories, array('cat_name' => $c->name, 'category_nicename' => $c->slug) );
  1590. $global_id = $wpdb->insert_id;
  1591. }
  1592. if ( $global_id == $term_id )
  1593. return $global_id;
  1594. if( get_option( 'default_category' ) == $term_id )
  1595. update_option( 'default_category', $global_id );
  1596. $wpdb->update( $wpdb->terms, array('term_id' => $global_id), array('term_id' => $term_id) );
  1597. $wpdb->update( $wpdb->term_taxonomy, array('term_id' => $global_id), array('term_id' => $term_id) );
  1598. $wpdb->update( $wpdb->term_taxonomy, array('parent' => $global_id), array('parent' => $term_id) );
  1599. clean_term_cache($term_id);
  1600. return $global_id;
  1601. }
  1602. function redirect_this_site( $deprecated = '' ) {
  1603. global $current_site;
  1604. return array( $current_site->domain );
  1605. }
  1606. function upload_is_file_too_big( $upload ) {
  1607. if( is_array( $upload ) == false || defined( 'WP_IMPORTING' ) )
  1608. return $upload;
  1609. if( strlen( $upload[ 'bits' ] ) > ( 1024 * get_site_option( 'fileupload_maxk', 1500 ) ) ) {
  1610. return sprintf(__( "This file is too big. Files must be less than %dKb in size.<br />" ), get_site_option( 'fileupload_maxk', 1500 ));
  1611. }
  1612. return $upload;
  1613. }
  1614. function wordpressmu_authenticate_siteadmin( $user, $password = '' ) {
  1615. if( is_site_admin( $user->user_login ) == false && ( $primary_blog = get_usermeta( $user->user_id, "primary_blog" ) ) ) {
  1616. $details = get_blog_details( $primary_blog );
  1617. if( is_object( $details ) && $details->spam == 1 ) {
  1618. return new WP_Error('blog_suspended', __('Blog Suspended.'));
  1619. }
  1620. }
  1621. return $user;
  1622. }
  1623. function wordpressmu_wp_mail_from( $email ) {
  1624. if( strpos( $email, 'wordpress@' ) !== false )
  1625. $email = get_option( 'admin_email' );
  1626. return $email;
  1627. }
  1628. /*
  1629. XMLRPC getUsersBlogs() for a multiblog environment
  1630. http://trac.mu.wordpress.org/attachment/ticket/551/xmlrpc-mu.php
  1631. */
  1632. function wpmu_blogger_getUsersBlogs($args) {
  1633. global $current_blog;
  1634. $domain = $current_blog->domain;
  1635. $path = $current_blog->path . 'xmlrpc.php';
  1636. $rpc = new IXR_Client("http://{$domain}{$path}");
  1637. $rpc->query('wp.getUsersBlogs', $args[1], $args[2]);
  1638. $blogs = $rpc->getResponse();
  1639. if ( isset($blogs['faultCode']) ) {
  1640. return new IXR_Error($blogs['faultCode'], $blogs['faultString']);
  1641. }
  1642. if ( $_SERVER['HTTP_HOST'] == $domain && $_SERVER['REQUEST_URI'] == $path ) {
  1643. return $blogs;
  1644. } else {
  1645. foreach ( (array) $blogs as $blog ) {
  1646. if ( strpos($blog['url'], $_SERVER['HTTP_HOST']) )
  1647. return array($blog);
  1648. }
  1649. return array();
  1650. }
  1651. }
  1652. function attach_wpmu_xmlrpc($methods) {
  1653. $methods['blogger.getUsersBlogs'] = 'wpmu_blogger_getUsersBlogs';
  1654. return $methods;
  1655. }
  1656. function mu_locale( $locale ) {
  1657. if( defined('WP_INSTALLING') == false ) {
  1658. $mu_locale = get_option('WPLANG');
  1659. if( $mu_locale === false )
  1660. $mu_locale = get_site_option('WPLANG');
  1661. if( $mu_locale !== false )
  1662. return $mu_locale;
  1663. }
  1664. return $locale;
  1665. }
  1666. function signup_nonce_fields() {
  1667. $id = mt_rand();
  1668. echo "<input type='hidden' name='signup_form_id' value='{$id}' />";
  1669. wp_nonce_field('signup_form_' . $id, '_signup_form', false);
  1670. }
  1671. function signup_nonce_check( $result ) {
  1672. if( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
  1673. return $result;
  1674. if ( wp_create_nonce('signup_form_' . $_POST[ 'signup_form_id' ]) != $_POST['_signup_form'] )
  1675. wp_die( __('Please try again!') );
  1676. return $result;
  1677. }
  1678. function maybe_redirect_404() {
  1679. global $current_site;
  1680. if( is_main_blog() && is_404() && defined( 'NOBLOGREDIRECT' ) && constant( 'NOBLOGREDIRECT' ) != '' ) {
  1681. $destination = constant( 'NOBLOGREDIRECT' );
  1682. if ( $destination == '%siteurl%' )
  1683. $destination = $current_site->domain . $current_site->path;
  1684. wp_redirect( $destination );
  1685. exit();
  1686. }
  1687. }
  1688. function remove_tinymce_media_button( $buttons ) {
  1689. unset( $buttons[ array_search( 'media', $buttons ) ] );
  1690. return $buttons;
  1691. }
  1692. function maybe_add_existing_user_to_blog() {
  1693. if ( false === strpos( $_SERVER[ 'REQUEST_URI' ], '/newbloguser/' ) ) {
  1694. return false;
  1695. }
  1696. $parts = explode( '/', $_SERVER[ 'REQUEST_URI' ] );
  1697. $key = array_pop( $parts );
  1698. if ( $key == '' )
  1699. $key = array_pop( $parts );
  1700. $details = get_option( "new_user_" . $key );
  1701. add_existing_user_to_blog( $details );
  1702. delete_option( 'new_user_' . $key );
  1703. wp_die( sprintf(__('You have been added to this blog. Please visit the <a href="%s">homepage</a> or <a href="%s">login</a> using your username and password.'), site_url(), admin_url() ) );
  1704. }
  1705. function add_existing_user_to_blog( $details = false ) {
  1706. if ( is_array( $details ) ) {
  1707. add_user_to_blog( '', $details[ 'user_id' ], $details[ 'role' ] );
  1708. do_action( "added_existing_user", $details[ 'user_id' ] );
  1709. }
  1710. }
  1711. function add_new_user_to_blog( $user_id, $email, $meta ) {
  1712. global $current_site;
  1713. if( $meta[ 'add_to_blog' ] ) {
  1714. $blog_id = $meta[ 'add_to_blog' ];
  1715. $role = $meta[ 'new_role' ];
  1716. remove_user_from_blog($user_id, $current_site->blogid); // remove user from main blog.
  1717. add_user_to_blog( $blog_id, $user_id, $role );
  1718. update_usermeta( $user_id, 'primary_blog', $blog_id );
  1719. }
  1720. }
  1721. function fix_phpmailer_messageid( $phpmailer ) {
  1722. global $current_site;
  1723. $phpmailer->Hostname = $current_site->domain;
  1724. }
  1725. function is_user_spammy( $username = 0 ) {
  1726. if( $username == 0 ) {
  1727. global $current_user;
  1728. $user_id = $current_user->ID;
  1729. } else {
  1730. $user_id = get_user_id_from_string( $username );
  1731. }
  1732. $u = new WP_User( $user_id );
  1733. if( $u->spam == 1 )
  1734. return true;
  1735. return false;
  1736. }
  1737. function login_spam_check( $user, $password ) {
  1738. if( is_user_spammy( $user->ID ) )
  1739. return new WP_Error('invalid_username', __('<strong>ERROR</strong>: your account has been marked as a spammer.'));
  1740. return $user;
  1741. }
  1742. add_action( 'wp_authenticate_user', 'login_spam_check', 10, 2 );
  1743. function update_blog_public( $old_value, $value ) {
  1744. global $wpdb;
  1745. do_action('update_blog_public');
  1746. update_blog_status( $wpdb->blogid, 'public', (int) $value );
  1747. }
  1748. add_action('update_option_blog_public', 'update_blog_public', 10, 2);
  1749. function strtolower_usernames( $username, $raw, $strict ) {
  1750. return strtolower( $username );
  1751. }
  1752. /* Short circuit the update checks. Make sure update informtion is
  1753. stored in wp_sitemeta rather than the options table of individual blogs */
  1754. // update_plugins (transient)
  1755. function site_delete_update_plugins() {
  1756. return update_site_option( 'update_plugins', false );
  1757. }
  1758. add_action( 'delete_transient_update_plugins', 'site_delete_update_plugins' );
  1759. function site_pre_update_plugins() {
  1760. return get_site_option( 'update_plugins' );
  1761. }
  1762. add_filter( 'pre_transient_update_plugins', 'site_pre_update_plugins' );
  1763. function site_pre_set_transient_update_plugins( $value ) {
  1764. update_site_option( 'update_plugins', $value );
  1765. return $value;
  1766. }
  1767. add_filter( 'pre_set_transient_update_plugins', 'site_pre_set_transient_update_plugins' );
  1768. add_action( 'add_option__transient_update_plugins', 'site_add_option__transient_update');
  1769. // update_themes (transient)
  1770. function site_delete_update_themes() {
  1771. return update_site_option( 'update_themes', false );
  1772. }
  1773. add_action( 'delete_transient_update_themes', 'site_delete_update_themes' );
  1774. function site_pre_update_themes() {
  1775. return get_site_option( 'update_themes' );
  1776. }
  1777. add_filter( 'pre_transient_update_themes', 'site_pre_update_themes' );
  1778. function site_pre_set_transient_update_themes( $value ) {
  1779. update_site_option( 'update_themes', $value );
  1780. return $value;
  1781. }
  1782. add_filter( 'pre_set_transient_update_themes', 'site_pre_set_transient_update_themes' );
  1783. add_action( 'add_option__transient_update_themes', 'site_add_option__transient_update');
  1784. // update_core (transient)
  1785. function site_delete_update_core() {
  1786. return update_site_option( 'update_core', false );
  1787. }
  1788. add_action( 'delete_transient_update_core', 'site_delete_update_core' );
  1789. function site_pre_update_core() {
  1790. return get_site_option( 'update_core' );
  1791. }
  1792. add_filter( 'pre_transient_update_core', 'site_pre_update_core' );
  1793. function site_pre_set_transient_update_core( $value ) {
  1794. update_site_option( 'update_core', $value );
  1795. return $value;
  1796. }
  1797. add_filter( 'pre_set_transient_update_core', 'site_pre_set_transient_update_core' );
  1798. add_action( 'add_option__transient_update_core', 'site_add_option__transient_update');
  1799. // dismissed_update_core (option, not a transient)
  1800. function site_pre_dismissed_update_core() {
  1801. return get_site_option( 'dismissed_update_core' );
  1802. }
  1803. add_filter( 'pre_option_dismissed_update_core', 'site_pre_dismissed_update_core' );
  1804. function site_pre_update_option_dismissed_update_core( $newvalue, $oldvalue ) {
  1805. update_site_option( 'dismissed_update_core', $newvalue );
  1806. delete_option('dismissed_update_core');
  1807. // Return the old value so the update_option() call is aborted after this filter is run. It's in sitemeta now.
  1808. return $oldvalue;
  1809. }
  1810. add_filter( 'pre_update_option_dismissed_update_core', 'site_pre_update_option_dismissed_update_core', 10, 2 );
  1811. function site_add_option__transient_update($name) {
  1812. delete_option($name);
  1813. }
  1814. /* Redirect all hits to "dashboard" blog to wp-admin/ Dashboard. */
  1815. function redirect_mu_dashboard() {
  1816. global $current_site, $current_blog;
  1817. $dashboard_blog = get_dashboard_blog();
  1818. if ( $current_blog->blog_id == $dashboard_blog->blog_id && $dashboard_blog->blog_id != $current_site->blog_id ) {
  1819. $protocol = ( is_ssl() ? 'https://' : 'http://' );
  1820. wp_redirect( $protocol . $dashboard_blog->domain . trailingslashit( $dashboard_blog->path ) . 'wp-admin/' );
  1821. die();
  1822. }
  1823. }
  1824. add_action( 'template_redirect', 'redirect_mu_dashboard' );
  1825. function get_dashboard_blog() {
  1826. global $current_site;
  1827. if ( get_site_option( 'dashboard_blog' ) == false ) {
  1828. return get_blog_details( $current_site->blog_id );
  1829. } else {
  1830. return get_blog_details( get_site_option( 'dashboard_blog' ) );
  1831. }
  1832. }
  1833. function is_user_option_local( $key, $user_id = 0, $blog_id = 0 ) {
  1834. global $current_user, $wpdb;
  1835. if( $user_id == 0 )
  1836. $user_id = $current_user->ID;
  1837. if( $blog_id == 0 )
  1838. $blog_id = $wpdb->blogid;
  1839. $local_key = $wpdb->base_prefix . $blog_id . "_" . $key;
  1840. if( isset( $current_user->$local_key ) )
  1841. return true;
  1842. return false;
  1843. }
  1844. function fix_active_plugins( $value ) {
  1845. if( false == is_array( $value ) )
  1846. $value = array();
  1847. return $value;
  1848. }
  1849. add_filter( "option_active_plugins", "fix_active_plugins" );
  1850. if ( !function_exists('rss_gc') ) :
  1851. function rss_gc() {
  1852. global $wpdb;
  1853. // Garbage Collection
  1854. $rows = $wpdb->get_results( "SELECT meta_key FROM {$wpdb->sitemeta} WHERE meta_key LIKE 'rss\_%\_ts' AND meta_value < unix_timestamp( date_sub( NOW(), interval 7200 second ) )" );
  1855. if( is_array( $rows ) ) {
  1856. foreach( $rows as $row ) {
  1857. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s", $row->meta_key ) );
  1858. $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE meta_key = %s", str_replace( '_ts', '', $row->meta_key ) ) );
  1859. }
  1860. }
  1861. }
  1862. endif;
  1863. add_action( 'wp_rss_gc', 'rss_gc' );
  1864. function retrieve_password_sitename( $title ) {
  1865. global $current_site;
  1866. return sprintf( __( '[%s] Password Reset' ), $current_site->site_name );
  1867. }
  1868. add_filter( 'retrieve_password_title', 'retrieve_password_sitename' );
  1869. function reset_password_sitename( $title ) {
  1870. global $current_site;
  1871. return sprintf( __( '[%s] Your new password' ), $current_site->site_name );
  1872. }
  1873. add_filter( 'password_reset_title', 'reset_password_sitename' );
  1874. function lowercase_username( $username, $raw_username, $strict ) {
  1875. return strtolower( $username );
  1876. }
  1877. add_filter( 'sanitize_user', 'lowercase_username', 10, 3 );
  1878. function mu_upload_dir( $uploads ) {
  1879. $dir = $uploads[ 'basedir' ];
  1880. if( defined( 'BLOGUPLOADDIR' ) )
  1881. $dir = constant( 'BLOGUPLOADDIR' );
  1882. $dir = untrailingslashit( $dir ) . $uploads[ 'subdir' ];
  1883. $uploads[ 'path' ] = $dir;
  1884. return $uploads;
  1885. }
  1886. add_filter( 'upload_dir', 'mu_upload_dir' );
  1887. function users_can_register_signup_filter() {
  1888. $registration = get_site_option('registration');
  1889. if ( $registration == 'all' || $registration == 'user' ) {
  1890. return true;
  1891. } else {
  1892. return false;
  1893. }
  1894. }
  1895. add_filter('option_users_can_register', 'users_can_register_signup_filter');
  1896. function welcome_user_msg_filter( $text ) {
  1897. if ( !$text ) {
  1898. return __( "Dear User,
  1899. Your new account is set up.
  1900. You can log in with the following information:
  1901. Username: USERNAME
  1902. Password: PASSWORD
  1903. LOGINLINK
  1904. Thanks!
  1905. --The Team @ SITE_NAME" );
  1906. }
  1907. return $text;
  1908. }
  1909. add_filter( 'site_option_welcome_user_email', 'welcome_user_msg_filter' );
  1910. function first_page_filter( $text ) {
  1911. if ( !$text ) {
  1912. return __( "This is an example of a WordPress page, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many pages like this one or sub-pages as you like and manage all of your content inside of WordPress." );
  1913. }
  1914. return $text;
  1915. }
  1916. add_filter( 'site_option_first_page', 'first_page_filter' );
  1917. function first_comment_filter( $text ) {
  1918. if ( !$text ) {
  1919. return __( "This is an example of a WordPress comment, you could edit this to put information about yourself or your site so readers know where you are coming from. You can create as many comments like this one or sub-comments as you like and manage all of your content inside of WordPress." );
  1920. }
  1921. return $text;
  1922. }
  1923. add_filter( 'site_option_first_comment', 'first_comment_filter' );
  1924. function first_comment_author_filter( $text ) {
  1925. if ( !$text ) {
  1926. return __( "Mr WordPress" );
  1927. }
  1928. return $text;
  1929. }
  1930. add_filter( 'site_option_first_comment_author', 'first_comment_author_filter' );
  1931. function first_comment_url_filter( $text ) {
  1932. global $current_site;
  1933. if ( !$text ) {
  1934. return 'http://' . $current_site->domain . $current_site->path;
  1935. }
  1936. return $text;
  1937. }
  1938. add_filter( 'site_option_first_comment_url', 'first_comment_url_filter' );
  1939. function mu_filter_plugins_list( $active_plugins ) {
  1940. $active_sitewide_plugins = get_site_option( 'active_sitewide_plugins' );
  1941. if ( !$active_sitewide_plugins )
  1942. return $active_plugins;
  1943. $plugins = array_merge( (array) $active_plugins, array_keys( (array) $active_sitewide_plugins ) );
  1944. sort( $plugins );
  1945. return $plugins;
  1946. }
  1947. add_filter( 'active_plugins', 'mu_filter_plugins_list' );
  1948. /**
  1949. * Whether to force SSL on content.
  1950. *
  1951. * @since 2.8.5
  1952. *
  1953. * @param string|bool $force
  1954. * @return bool True if forced, false if not forced.
  1955. */
  1956. function force_ssl_content( $force = '' ) {
  1957. static $forced_content;
  1958. if ( '' != $force ) {
  1959. $old_forced = $forced_content;
  1960. $forced_content = $force;
  1961. return $old_forced;
  1962. }
  1963. return $forced_content;
  1964. }
  1965. /**
  1966. * Formats an String URL to use HTTPS if HTTP is found.
  1967. * Useful as a filter.
  1968. *
  1969. * @since 2.8.5
  1970. **/
  1971. function filter_SSL( $url) {
  1972. if ( !is_string( $url ) ) {
  1973. return get_bloginfo( 'url' ); //return home blog url with proper scheme
  1974. }
  1975. $arrURL = parse_url( $url );
  1976. if ( force_ssl_content() && is_ssl() ) {
  1977. if ( 'http' === $arrURL['scheme'] && 'https' !== $arrURL['scheme'] ) {
  1978. $url = str_replace( $arrURL['scheme'], 'https', $url );
  1979. }
  1980. }
  1981. return $url;
  1982. }
  1983. function maybe_cancel_post_by_email() {
  1984. if ( false == defined( 'POST_BY_EMAIL' ) ) {
  1985. die( __( 'This action has been disabled by the administrator' ) );
  1986. }
  1987. }
  1988. add_action( 'wp-mail.php', 'maybe_cancel_post_by_email' );
  1989. ?>