s2_multisite

The Subscribe2 s2 multisite class.

Defined (1)

The class is defined in the following location(s).

/classes/class-s2-multisite.php  
  1. class s2_multisite { 
  2. /** === WP Multisite specific functions === */ 
  3. /** 
  4. Handles subscriptions and unsubscriptions for different blogs on WPMU installs 
  5. */ 
  6. function wpmu_subscribe() { 
  7. global $mysubscribe2; 
  8. // subscribe to new blog 
  9. if ( !empty($_GET['s2mu_subscribe']) ) { 
  10. $sub_id = intval($_GET['s2mu_subscribe']); 
  11. if ( $sub_id >= 0 ) { 
  12. switch_to_blog($sub_id); 
  13.  
  14. $user_ID = get_current_user_id(); 
  15.  
  16. // if user is not a user of the current blog 
  17. if ( !is_user_member_of_blog($user_ID, $sub_id) ) { 
  18. // add user to current blog as subscriber 
  19. add_user_to_blog($sub_id, $user_ID, 'subscriber'); 
  20. // add an action hook for external manipulation of blog and user data 
  21. do_action_ref_array('subscribe2_wpmu_subscribe', array($user_ID, $sub_id)); 
  22.  
  23. // get categories, remove excluded ones if override is off 
  24. if ( 0 == $mysubscribe2->subscribe2_options['reg_override'] ) { 
  25. $all_cats = $mysubscribe2->all_cats(true, 'ID'); 
  26. } else { 
  27. $all_cats = $mysubscribe2->all_cats(false, 'ID'); 
  28.  
  29. $cats_string = ''; 
  30. foreach ( $all_cats as $cat ) { 
  31. ('' == $cats_string) ? $cats_string = "$cat->term_id" : $cats_string .= ", $cat->term_id"; 
  32. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_cat') . $cat->term_id, $cat->term_id); 
  33. if ( empty($cats_string) ) { 
  34. delete_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed')); 
  35. } else { 
  36. update_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), $cats_string); 
  37. } elseif ( !empty($_GET['s2mu_unsubscribe']) ) { 
  38. // unsubscribe from a blog 
  39. $unsub_id = intval($_GET['s2mu_unsubscribe']); 
  40. if ( $unsub_id >= 0 ) { 
  41. switch_to_blog($unsub_id); 
  42.  
  43. $user_ID = get_current_user_id(); 
  44.  
  45. // delete subscription to all categories on that blog 
  46. $cats = get_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), true); 
  47. $cats = explode(', ', $cats); 
  48. if ( !is_array($cats) ) { 
  49. $cats = array($cats); 
  50.  
  51. foreach ( $cats as $id ) { 
  52. delete_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_cat') . $id); 
  53. delete_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed')); 
  54.  
  55. // add an action hook for external manipulation of blog and user data 
  56. do_action_ref_array('subscribe2_wpmu_unsubscribe', array($user_ID, $unsub_id)); 
  57.  
  58. restore_current_blog(); 
  59.  
  60. if ( !is_user_member_of_blog($user_ID) ) { 
  61. $user_blogs = get_active_blog_for_user($user_ID); 
  62. if ( is_array($user_blogs) ) { 
  63. switch_to_blog(key($user_blogs)); 
  64. } else { 
  65. // no longer a member of a blog 
  66. wp_redirect(get_option('siteurl')); // redirect to front page 
  67. exit(0); 
  68.  
  69. // redirect to profile page 
  70. $url = get_option('siteurl') . '/wp-admin/admin.php?page=s2'; 
  71. wp_redirect($url); 
  72. exit(0); 
  73. } // end wpmu_subscribe() 
  74.  
  75. /** 
  76. Obtain a list of current WordPress multiuser blogs 
  77. Note this may affect performance but there is no alternative 
  78. */ 
  79. function get_mu_blog_list() { 
  80. global $wpdb; 
  81. $blogs = $wpdb->get_results( $wpdb->prepare("SELECT blog_id, domain, path FROM $wpdb->blogs WHERE site_id = %d AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' ORDER BY registered DESC", $wpdb->siteid), ARRAY_A ); 
  82.  
  83. foreach ( $blogs as $details ) { 
  84. //reindex the array so the key is the same as the blog_id 
  85. $blog_list[$details['blog_id']] = $details; 
  86.  
  87. if ( !is_array($blog_list) ) { 
  88. return array(); 
  89.  
  90. return apply_filters('s2_mu_blog_list', $blog_list); 
  91. } // end get_mu_blog_list() 
  92.  
  93. /** 
  94. Register user details when new user is added to a multisite blog 
  95. */ 
  96. function wpmu_add_user($user_ID = 0) { 
  97. global $mysubscribe2; 
  98. if ( 0 == $user_ID ) { return; } 
  99. if ( 'yes' == $mysubscribe2->subscribe2_options['autosub'] ) { 
  100. $mysubscribe2->register($user_ID, true); 
  101. } else { 
  102. $mysubscribe2->register($user_ID, false); 
  103. } // end wpmu_add_user() 
  104.  
  105. /** 
  106. Delete user details when a user is removed from a multisite blog 
  107. */ 
  108. function wpmu_remove_user($user_ID) { 
  109. global $mysubscribe2; 
  110. if ( 0 == $user_ID ) { return; } 
  111. delete_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_format')); 
  112. delete_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_autosub')); 
  113. $cats = get_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed'), true); 
  114. if ( !empty($cats) ) { 
  115. $cats = explode(', ', $cats); 
  116. foreach ( $cats as $cat ) { 
  117. delete_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_cat') . $cat); 
  118. delete_user_meta($user_ID, $mysubscribe2->get_usermeta_keyname('s2_subscribed')); 
  119. } // end wpmu_remove_user() 
  120.  
  121. /** 
  122. Rename WPMU widgets on upgrade without requiring user to re-enable 
  123. */ 
  124. function namechange_subscribe2_widget() { 
  125. global $wpdb; 
  126. $blogs = $wpdb->get_col("SELECT blog_id FROM {$wpdb->blogs}"); 
  127.  
  128. foreach ( $blogs as $blog ) { 
  129. switch_to_blog($blog); 
  130.  
  131. $sidebars = get_option('sidebars_widgets'); 
  132. if ( empty($sidebars) || !is_array($sidebars) ) { return; } 
  133. $changed = false; 
  134. foreach ( $sidebars as $s =>$sidebar ) { 
  135. if ( empty($sidebar) || !is_array($sidebar) ) { break; } 
  136. foreach ( $sidebar as $w => $widget ) { 
  137. if ( $widget == 'subscribe2widget' ) { 
  138. $sidebars[$s][$w] = 'subscribe2'; 
  139. $changed = true; 
  140. if ( $changed ) { 
  141. update_option('sidebar_widgets', $sidebars); 
  142. restore_current_blog(); 
  143. } // end namechange_subscribe2_widget()