function altlab_motherblog_func($atts)
 {
     $a = shortcode_atts(array('category' => 'uncategorized', 'sub_categories' => '', 'get_comments' => 'false'), $atts);
     /**
      * List categories for output
      *
      * @since   1.1.0
      * @var     string  $category           $a{category} from shortcode args
      * @var     string  $sub_categories     $a{sub_categories} from shortcode args
      * 
      * @return  string  HTML list of sub categories from the shortcode args
      */
     function list_created_categories($category, $sub_categories)
     {
         $category_list = "<ul><li>" . $category;
         if ($sub_categories) {
             $sub_categories = str_replace(' ', '', $sub_categories);
             $array = explode(',', $sub_categories);
             $category_list .= "<ul>";
             foreach ($array as $item) {
                 $category_list .= "<li>" . $item . "</li>";
             }
             $category_list .= "</ul>";
         }
         $category_list .= "</li></ul>";
         return $category_list;
     }
     function create_category($string)
     {
         $string = str_replace(' ', '-', $string);
         wp_insert_term($string, 'category');
         $category = get_term_by('name', $string, 'category');
         return $category;
     }
     function create_remote_category($string)
     {
         if ($_POST['have-network'] === 'Yes') {
             // Get blog id we will switch to for remote category creation
             // $entry, 1 comes from form input where user selects blog.
             $switch_to = $_POST['blog-select'];
             // https://codex.wordpress.org/WPMU_Functions/switch_to_blog
             switch_to_blog($switch_to);
             create_category($string);
             restore_current_blog();
         }
     }
     function create_sub_categories($string, $category)
     {
         if ($string) {
             $string = str_replace(' ', '', $string);
             $array = explode(',', $string);
             foreach ($array as $item) {
                 $the_sub_category = get_term_by('name', $item, 'category');
                 if (!$the_sub_category) {
                     $args = array('parent' => $category->term_id);
                     wp_insert_term($item, 'category', $args);
                 }
             }
         }
     }
     function create_remote_sub_categories($string, $category)
     {
         if ($_POST['have-network'] === 'Yes') {
             // Get blog id we will switch to for remote category creation
             // $entry, 1 comes from form input where user selects blog.
             $switch_to = $_POST['blog-select'];
             // https://codex.wordpress.org/WPMU_Functions/switch_to_blog
             switch_to_blog($switch_to);
             create_sub_categories($string, $category);
             restore_current_blog();
         }
     }
     function get_current_user_blogs()
     {
         $user_id = get_current_user_id();
         $user_blogs = get_blogs_of_user($user_id);
         $choices = '';
         foreach ($user_blogs as $user_blog) {
             $choices .= "<option value='" . $user_blog->userblog_id . "'>" . $user_blog->blogname . "</option>";
         }
         return $choices;
     }
     function get_blogs_of_current_user_by_role()
     {
         $user_id = get_current_user_id();
         $role = 'administrator';
         $blogs = get_blogs_of_user($user_id);
         foreach ($blogs as $blog_id => $blog) {
             // Get the user object for the user for this blog.
             $user = new WP_User($user_id, '', $blog_id);
             // Remove this blog from the list if the user doesn't have the role for it.
             if (!in_array($role, $user->roles)) {
                 unset($blogs[$blog_id]);
             }
         }
         return $blogs;
     }
     function create_blogs_dropdown($blogs)
     {
         $choices = '';
         foreach ($blogs as $blog) {
             $choices .= "<option value='" . $blog->userblog_id . "'>" . $blog->blogname . "</option>";
         }
         return $choices;
     }
     function add_current_user_to_mother_blog()
     {
         if ($_POST['have-network'] === 'Yes') {
             $blog_id = get_current_blog_id();
             $user_id = get_current_user_id();
             $role = 'subscriber';
             if (!is_user_member_of_blog($user_id, $blog_id)) {
                 add_user_to_blog($blog_id, $user_id, $role);
             }
         }
     }
     function create_fwp_link($mother_category)
     {
         if ($_POST['have-network'] === 'Yes') {
             // Switch to remote blog as selected by current user
             $switch_to = $_POST['blog-select'];
             switch_to_blog($switch_to);
             $remote_blog_url = get_site_url();
             $remote_blog_name = get_bloginfo('name');
             // switch back to motherblog
             restore_current_blog();
             $fwp_link_category = FeedWordPress::link_category_id();
             $linkdata = array("link_url" => $remote_blog_url, "link_name" => $remote_blog_name, "link_rss" => $remote_blog_url . '/category/' . $mother_category->slug . '/feed', "link_category" => $fwp_link_category);
             if (!function_exists('wp_insert_link')) {
                 include_once ABSPATH . '/wp-admin/includes/bookmark.php';
             }
             wp_insert_link($linkdata, true);
         }
     }
     function create_fwp_comments_link($mother_category)
     {
         if ($_POST['have-network'] === 'Yes') {
             // Switch to remote blog as selected by current user
             $switch_to = $_POST['blog-select'];
             switch_to_blog($switch_to);
             $remote_category_id = get_cat_ID($mother_category->slug);
             $remote_blog_url = get_site_url();
             $remote_blog_name = get_bloginfo('name');
             // switch back to motherblog
             restore_current_blog();
             $fwp_link_category = get_terms('link_category', $args = 'name__like=Contributors');
             $linkdata = array("link_url" => $remote_blog_url, "link_name" => $remote_blog_name . ' comments', "link_rss" => $remote_blog_url . '/comments/feed/?cat=' . $remote_category_id, "link_category" => $fwp_link_category[0]->term_id);
             if (!function_exists('wp_insert_link')) {
                 include_once ABSPATH . '/wp-admin/includes/bookmark.php';
             }
             wp_insert_link($linkdata, true);
         }
     }
     function create_fwp_link_off_network()
     {
         if ($_POST['have-network'] === 'No' && $_POST['blog-feed']) {
             $fwp_link_category = get_terms('link_category', $args = 'name__like=Contributors');
             $linkdata = array("link_url" => $_POST['blog-feed'], "link_name" => $_POST['blog-feed'], "link_rss" => $_POST['blog-feed'], "link_category" => $fwp_link_category[0]->term_id);
             if (!function_exists('wp_insert_link')) {
                 include_once ABSPATH . '/wp-admin/includes/bookmark.php';
             }
             wp_insert_link($linkdata, true);
         }
     }
     /**
      * Get details of remote blog
      *
      * @since 1.0.0
      * @var     $blogID     id of blog to switch to
      * @var     $category   term_object ( should be string? cause we don't yet even have an object to give it yet I don't think... )
      *
      * @return  array()  
      */
     function get_remote_blog_info($blogID, $category)
     {
         $remote_blog = new stdClass();
         switch_to_blog($blogID);
         $remote_blog->url = get_site_url();
         $remote_blog->name = get_bloginfo('name');
         $remote_blog->term_id = get_cat_ID($category->slug);
         $remote_blog->feed_url = get_site_url() . '/category/' . $category->slug . '/feed';
         $remote_blog->comments_feed_url = get_site_url() . '/comments/feed/?cat=' . get_cat_ID($category->slug);
         // switch back to motherblog
         restore_current_blog();
         return $remote_blog;
     }
     function get_network_name()
     {
         $network_id = get_blog_details()->site_id;
         $network_name = get_blog_details($network_id)->blogname;
         return $network_name;
     }
     function get_network_signup_url()
     {
         $network_signup_url = get_option('altlab_motherblog_options');
         if (!$network_signup_url && $_SERVER['HTTP_HOST'] === 'rampages.us') {
             $network_signup_url['network-signup-url'] = "http://rampages.us/vcu-wp-signup.php";
         }
         return $network_signup_url['network-signup-url'];
     }
     /**
      * Check if blog is already signed up
      *
      * @since 1.1.0
      * @var     $remote_blog    object containing details about the remote blog we are trying to signup
      *
      * @return  array()  
      */
     function check_for_dupes($remote_blog)
     {
         $args = array('limit' => -1, 'category_name' => 'Contributors');
         $links = get_bookmarks($args);
         foreach ($links as $link) {
             if ($link->link_rss === $remote_blog->feed_url) {
                 die('<p>This blog is already connected.</p>');
             }
         }
     }
     // set $mother_category var
     if (get_term_by('name', $a['category'], 'category')) {
         $mother_category = get_term_by('name', $a['category'], 'category');
     } else {
         $mother_category = create_category($a['category']);
     }
     // set $sub_categories var
     $sub_categories = $a['sub_categories'];
     // create sub cats if they don't exist already
     create_sub_categories($sub_categories, $mother_category);
     if (is_user_logged_in()) {
         $blog_select = "\n\t\t\t\t<p>\n\t\t\t\t\t<label>Which of your blogs would you like to connect?</label><br/>\n\t\t\t\t\t<select id='blog-select' name='blog-select'>\n\t\t\t\t    \t<option value=''>Select your blog</option>" . create_blogs_dropdown(get_blogs_of_current_user_by_role()) . "</select>\n\t\t\t\t</p>";
         $blog_select_login_prompt = "";
     } else {
         $blog_select = "";
         $blog_select_login_prompt = "<p>Awesome! But you'll have to <a href='" . wp_login_url(get_the_permalink()) . "'>login</a> before we can take the next step.</p>";
     }
     $form = "\n\t\t    <form id='altlab-motherblog-subscribe' action='" . get_the_permalink() . "' method='post'>\n\t\t\t\n\t\t\t\t<fieldset id='fs1'>\n\t\t\t\t\t<div id='have-network'>\n\t\t\t\t\t\t<p>\n\t\t\t\t\t\t\t<label>Do you have a " . get_network_name() . " blog?</label><br/>\n\t\t\t\t\t\t\t<input type='radio' name='have-network' value='Yes'>Yes</input><br/>\n\t\t\t\t\t\t\t<input type='radio' name='have-network' value='No'>No</input>\n\t\t\t\t\t\t</p>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<div id='have-network-yes' style='display:none;'>\n\t\t\t\t\t\t" . $blog_select . "\n\t\t\t\t\t\t" . $blog_select_login_prompt . "\n\t\t\t\t\t</div>\n\t\t\t\t</fieldset>\n\t\t\t\t\n\t\t\t\t<fieldset id='fs2'>\n\t\t\t\t\t<div id='want-network' style='display:none;'>\n\t\t\t\t\t\t<p>\n\t\t\t\t\t\t\t<label>Do you want one?</label><br/>\n\t\t\t\t\t\t\t<input type='radio' name='want-network' value='Yes'>Yes</input><br/>\n\t\t\t\t\t\t\t<input type='radio' name='want-network' value='No'>No</input>\n\t\t\t\t\t\t</p>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<div id='want-network-yes' style='display:none;'>\n\t\t\t\t\t\t<p>Awesome! You can <a href='" . get_network_signup_url() . "'>get one here.</a></p>\n\t\t\t\t\t</div>\n\t\t\t\t</fieldset>\n\n\t\t\t\t<fieldset id='fs3'>\n\t\t\t\t\t<div id='have-blog' style='display:none;'>\n\t\t\t\t\t\t<p>\n\t\t\t\t\t\t\t<label>Do you have a blog somewhere else already?</label><br/>\n\t\t\t\t\t\t\t<input type='radio' name='have-blog' value='Yes'>Yes</input><br/>\n\t\t\t\t\t\t\t<input type='radio' name='have-blog' value='No'>No</input>\n\t\t\t\t\t\t</p>\n\t\t\t\t\t</div>\n\n\t\t\t\t\t<div id='have-blog-yes' style='display:none;'>\n\t\t\t\t\t\t<p>Cool, we just need the RSS feed from your site for the category you will use for this site.</p>\n\t\t\t\t\t\t<input id='blog-feed' type='text' name='blog-feed'></input>\n\t\t\t\t\t\t<small>If you aren't sure how to find your RSS feed, check out <a href='http://thoughtvectors.net/rss-stream/i-have-a-blog/specific/'>this page for common feed structures</a>.</small>\n\t\t\t\t\t</div>\n\t\t\t\t\t\n\n\t\t\t\t\t<div id='have-blog-no' style='display:none;'>\n\t\t\t\t\t\t<p>Sorry, but you have to have a blog somewhere to register.</p>\n\n\t\t\t\t\t\t<p>Fear not, you can <a href='" . get_network_signup_url() . "'>get a " . get_network_name() . " blog</a> for free! Just come back when you're ready. 😊</p>\n\t\t\t\t\t</div>\n\t\t\t\t</fieldset>\n\n\t\t\t\t<input id='email' type='text' name='email' size='25' value='' />\n\n\t\t\t\t<fieldset id='submit' style='display:none;'>\n\t\t\t\t\t<input type='hidden' name='submit' value='1'/>\n\t\t\t\t\t<input type='submit' value='Submit' />\n\t\t\t\t</fieldset>\n\n\t\t\t</form>\n\t\t    ";
     if (!is_admin() && $_POST) {
         $form_response = "";
         if ($_POST['email']) {
             die('<p>An error occurred. You have not been connected.</p>');
         } else {
             if (is_user_logged_in() && $_POST['blog-select'] && !$_POST['email']) {
                 create_remote_category($a['category']);
                 $remote_blog = get_remote_blog_info($_POST['blog-select'], $mother_category);
                 check_for_dupes($remote_blog);
                 create_remote_sub_categories($sub_categories, $remote_blog);
                 add_current_user_to_mother_blog();
                 create_fwp_link($mother_category);
                 if ($a['get_comments'] === 'true') {
                     create_fwp_comments_link($mother_category);
                 }
                 if ($sub_categories) {
                     $form_response .= '<h2>SUCCESS!</h2>';
                     $form_response .= '<p>The following category and sub categories have been added your blog "<strong>' . $remote_blog->name . '</strong>".</p>';
                     $form_response .= list_created_categories($a['category'], $sub_categories);
                     $form_response .= '<p>Only posts you create in these categories on your blog "<strong>' . $remote_blog->name . '</strong>" will appear on this site.</p>';
                     $form_response .= '<a href="' . $remote_blog->url . '">Return to your site ' . $remote_blog->name . '</a>';
                 } else {
                     $form_response .= '<h2>SUCCESS!</h2>';
                     $form_response .= '<p>The category "<strong>' . $a['category'] . '</strong>" has been added to your blog "<strong>' . $remote_blog->name . '</strong>".</p>
             <p>Only posts you create in the "' . $a['category'] . '" category on your blog "<strong>' . $remote_blog->name . '</strong>" will appear on this site.</p>';
                     $form_response .= '<a href="' . $remote_blog->url . '">Return to your site ' . $remote_blog->name . '</a>';
                 }
                 return $form_response;
             } else {
                 if ($_POST['blog-feed'] && !$_POST['email']) {
                     create_fwp_link_off_network();
                     $form_response .= '<h2>SUCCESS!</h2>';
                     $form_response .= "<p>You submitted the feed <a href='" . $_POST['blog-feed'] . "'>" . $_POST['blog-feed'] . "</a> to this site.<br/>\n                \tOnly posts that appear in the feed you submitted will appear on this site.</p>";
                     return $form_response;
                 } else {
                     $form_response .= "<h2>CRUSHING DEFEAT!</h2>";
                     $form_response .= "<p>An error occurred. You have not been connected. But you should totally try again.</p>";
                     $form_response .= "<p>An error occurred. You have not been subscribed. But you should totally try again.</p>";
                     $form_response = "<p>An error occurred. You have not been connected.</p>";
                     $form_response .= "<h2>CRUSHING DEFEAT!</h2>";
                     $form_response .= "<p>An error occurred. You have not been subscribed. But you should totally try again.</p>";
                     return $form_response;
                 }
             }
         }
     }
     return $form;
 }
/**
 * Problem: the various *_site_options() functions operate only on the current network
 * Workaround: change the current network
 *
 * @since 1.3
 * @param integer $new_network ID of network to manipulate
 */
function switch_to_network($new_network = 0, $validate = false)
{
    global $wpdb, $switched_network, $switched_network_stack, $current_site;
    if (empty($new_network)) {
        $new_network = $current_site->id;
    }
    if (true == $validate && !network_exists($new_network)) {
        return false;
    }
    if (empty($switched_network_stack)) {
        $switched_network_stack = array();
    }
    array_push($switched_network_stack, $current_site);
    /**
     * If we're switching to the same network id that we're on,
     * set the right vars, do the associated actions, but skip
     * the extra unnecessary work
     */
    if ($current_site->id === $new_network) {
        do_action('switch_network', $current_site->id, $current_site->id);
        $switched_network = true;
        return true;
    }
    // Switch the current site over
    $prev_site_id = $current_site->id;
    $current_site = wp_get_network($new_network);
    // Figure out the current network's main site.
    if (!isset($current_site->blog_id)) {
        $current_site->blog_id = get_main_site_for_network($current_site);
    }
    if (!isset($current_site->site_name)) {
        $current_site->site_name = get_network_name();
    }
    $wpdb->siteid = $current_site->id;
    $GLOBALS['site_id'] = $current_site->id;
    do_action('switch_network', $current_site->id, $prev_site_id);
    $switched_network = true;
    return true;
}
Пример #3
0
 /**
  * Problem: the various *_site_options() functions operate only on the current network
  * Workaround: change the current network
  *
  * @since 1.3
  * @param integer $new_network ID of network to manipulate
  */
 function switch_to_network($new_network = 0, $validate = false)
 {
     global $wpdb, $switched_network, $switched_network_stack, $current_site;
     // Default to the current network ID
     if (empty($new_network)) {
         $new_network = $current_site->id;
     }
     // Bail if network does not exist
     if (true == $validate && !get_network($new_network)) {
         return false;
     }
     // Maybe initialize the network switching stack global
     if (empty($switched_network_stack)) {
         $switched_network_stack = array();
     }
     // Tack the current network at the end of the stack
     array_push($switched_network_stack, $current_site);
     /**
      * If we're switching to the same network ID that we're on,
      * set the right vars, do the associated actions, but skip
      * the extra unnecessary work.
      */
     if ($current_site->id === $new_network) {
         do_action('switch_network', $current_site->id, $current_site->id);
         $switched_network = true;
         return true;
     }
     // Switch the current site over
     $prev_site_id = $current_site->id;
     $current_site = get_network($new_network);
     // Maybe populate network's main site.
     if (!isset($current_site->blog_id)) {
         $current_site->blog_id = get_main_site_for_network($current_site);
     }
     // Maybe populate network's name
     if (!isset($current_site->site_name)) {
         $current_site->site_name = get_network_name();
     }
     // Update network globals
     $wpdb->siteid = $current_site->id;
     $GLOBALS['site_id'] = $current_site->id;
     $GLOBALS['domain'] = $current_site->domain;
     do_action('switch_network', $current_site->id, $prev_site_id);
     $switched_network = true;
     return true;
 }