/** * Returns a list of all sitewide comments. * * If desired, this function can be passed a boolean indicating whether or * not to return only approved comments, which is the action performed when * `$approved_only` is true. By default, this value is true. * * @param bool $approved_only only return approved comments * @return array all sitewide comments * * @since 0.1 */ public function get_sitewide_comments($approved_only = true) { // Set a proper cache key based upon which sort of comments are allowed $cache_key = 'comments_'; $cache_key .= $approved_only ? 'approved' : 'all'; // Return the cached comments if possible $cached = $this->get_site_cache($cache_key); if ($cached !== null) { return $cached; } // Filter out any unapproved comments if we're only allowing approved ones $approved_filter = ""; if ($approved_only) { $approved_filter = "AND c.comment_approved = '1'"; } global $nxtdb; $comments = $nxtdb->get_results("\n\t\t\tSELECT c.*, p.post_title\n\t\t\tFROM {$this->sw_tables->comments} AS c, {$this->sw_tables->posts} AS p\n\t\t\tWHERE p.ID = c.comment_post_ID AND c.cb_sw_blog_id = p.cb_sw_blog_id {$approved_filter}\n\t\t\tORDER BY c.comment_date DESC"); // Even if all comments are allowed, don't display spam comments if (!$approved_only) { global $blog_id; $current_blog_id = $blog_id; $no_spam = array(); foreach ($comments as $comment) { ClassBlogs_NXTClass::switch_to_blog($comment->cb_sw_blog_id); if (nxt_get_comment_status($comment->comment_ID) != 'spam') { $no_spam[] = $comment; } } ClassBlogs_Utils::restore_blog($current_blog_id); $comments = $no_spam; } $this->set_site_cache($cache_key, $comments); return $comments; }
/** * Returns a random image from one of the blogs on the site. * * The returned image object, if not null, will have the following properties: * * blog_id - the ID of the blog on which the image was posted * title - the image's title * url - the absolute URL to the image * * If the image is associated with a particular post, it will also have * the following properties on it: * * post_id - the ID of the post that uses the image * user_id - the ID of the user who created a post using the image * * @return mixed the random image object, or null if none can be found * * @since 0.1 */ public function get_random_image() { global $blog_id, $nxtdb; $current_blog_id = $blog_id; $image = null; $urls = array(); // Search through every blog for a usable image. If an image is found, build // the link to it and add a possible caption. $blogs = ClassBlogs_Utils::get_all_blog_ids(); shuffle($blogs); foreach ($blogs as $blog) { ClassBlogs_NXTClass::switch_to_blog($blog); $images = $nxtdb->get_results("\n\t\t\t\tSELECT ID, post_title, GUID FROM {$nxtdb->posts}\n\t\t\t\tWHERE post_mime_type LIKE 'image/%%'\n\t\t\t\tAND post_content <> guid"); if ($images) { $image = $images[array_rand($images)]; $urls[] = $image->GUID; $info = nxt_get_attachment_image_src($image->ID); if (!empty($info)) { $image = array('blog_id' => $blog, 'title' => $image->post_title, 'url' => $info[0]); $urls[] = $info[0]; } break; } } ClassBlogs_Utils::restore_blog($current_blog_id); // If we have a valid image, try to find the first post on which it was // used and add its ID to the image data if ($image) { $info = array(); $post_id = null; $user_id = null; foreach ($urls as $url) { $post = $this->_find_first_post_to_use_image($image['blog_id'], $url); if (!empty($post)) { break; } } if (!empty($post)) { $post_id = $post->ID; $user_id = $post->post_author; } $image['post_id'] = $post_id; $image['user_id'] = $user_id; $image = (object) $image; } return $image; }
/** * Registers hooks and sets default options for the plugin. * * @access private * @since 0.3 */ public function _register_hooks() { // If we're not running multisite, this plugin has no purpose, so we // should abort if (!ClassBlogs_Utils::is_multisite()) { return; } // Update the default links options to be a single link pointing back // to the main class blog ClassBlogs_NXTClass::switch_to_blog(ClassBlogs_Settings::get_root_blog_id()); $this->default_options['links'] = array(array('url' => site_url(), 'title' => __('Return to Course Blog', 'classblogs'))); ClassBlogs_NXTClass::restore_current_blog(); // If there are any links defined and we're not in the admin side, // inject the professor's links into a widget in the first widgetized // area of all student blogs in the network $links = $this->get_option('links'); if (!is_admin() && !empty($links)) { add_action('init', array($this, '_register_widget')); add_filter('sidebars_widgets', array($this, '_add_widget')); } }
/** * Applies the student's chosen pseudonym. * * This updates the student's user and blog information, then attempts to * update any references to the old URL, such as those used by media embedded * into posts on the blog whose URL is being changed. * * The inputs for this function will have already been validated by another * method, so it can be assumed that they are valid. * * @param int $user_id the student's user ID * @param int $blog_id the ID of the student's primary blog * @param string $new_username the student's new username * * @access private * @since 0.1 */ private function _apply_pseudonym($user_id, $blog_id, $new_username) { global $nxtdb; // Update the student's username $nxtdb->update($nxtdb->users, array('user_login' => $new_username), array('ID' => $user_id), array('%s'), array('%d')); // Deal with the implications of the updated username in multisite if (ClassBlogs_Utils::is_multisite()) { ClassBlogs_NXTClass::switch_to_blog($blog_id); $old_url = trailingslashit(home_url()); // Update the blog URL to reflect their new username $site = get_current_site(); $new_path = trailingslashit($site->path . $new_username); $new_url = 'http://' . $site->domain . $new_path; update_option('siteurl', $new_url); update_option('home', $new_url); update_blog_details($blog_id, array('path' => $new_path)); delete_option('rewrite_rules'); // Replace any occurrences of the old URL in the blog's posts $referring_posts = $nxtdb->get_results("\n\t\t\t\tSELECT ID, post_content FROM {$nxtdb->posts}\n\t\t\t\tWHERE post_content LIKE '%%" . like_escape($old_url) . "%%' "); foreach ($referring_posts as $post) { $nxtdb->update($nxtdb->posts, array('post_content' => str_replace($old_url, $new_url, $post->post_content)), array('ID' => $post->ID), array('%s'), array('%d')); } ClassBlogs_NXTClass::restore_current_blog(); } // Flag that the user has changed their username $changed = $this->get_option('changed_users'); $changed[$new_username] = true; $this->update_option('changed_users', $changed); }
/** * Sets the correct blog if a sitewide post is being displayed. * * If the current post has an attribute indicating which blog it was made on, * it means that it is a sitewide post, and the blog that it exists on should * be made active so that the post's permalinks, tags, etc. can be determined. * * @since 0.3 */ public function use_correct_blog_for_sitewide_post() { global $post, $nxt_rewrite; if (property_exists($post, 'cb_sw_blog_id')) { // Store the original rewrite rules for later if (!isset($this->_rewrite)) { $this->_rewrite = $nxt_rewrite; } // Switch to the post's blog ClassBlogs_NXTClass::restore_current_blog(); ClassBlogs_NXTClass::switch_to_blog($post->cb_sw_blog_id); // Generate new rewrite rules for the blog, which will allow things // like categories and tags to display properly $nxt_rewrite = new nxt_Rewrite(); } }
/** * Restores an arbitrary blog. * * This functions identically to `restore_current_blog`, but with the option * of passing a blog ID to restore to. This switches to that blog, then * clears the switched stack and resets the switched state flag to false. * * @param int $blog_id the ID of the blog to restore to * * @since 0.2 */ public static function restore_blog($blog_id) { global $switched_stack, $switched; ClassBlogs_NXTClass::switch_to_blog($blog_id); $switched_stack = array(); $switched = false; }
/** * Returns markup for the local videos page. * * @return string markup for the local playlist page * * @access private * @since 0.1 */ public function _render_playlist_page() { $markup = ""; foreach ($this->get_playlist_videos() as $index => $video) { // Add the video with a title $markup .= '<div class="cb-youtube-local-playlist-page-video post hentry">'; $markup .= '<h2 class="cb-youtube-local-playlist-page-title"><a href="' . esc_url($video->link) . '" title="' . __('View on YouTube', 'classblogs') . '">' . esc_html($video->title) . '</a></h2>'; $markup .= '<div class="cb-youtube-local-playlist-page-video-thumbnail-container">'; $markup .= sprintf('<a href="%1$s"><img src="%2$s" title="%3$s" alt="%3$s" data-youtube-id="%4$s" class="cb-youtube-local-playlist-page-video-thumbnail" /></a>', esc_url($video->link), esc_url($this->_get_large_thumbnail_url($video->youtube_id)), esc_attr($video->title), esc_attr($video->youtube_id)); $markup .= '</div>'; // Add metadata for the video $markup .= sprintf('<p class="cb-youtube-local-playlist-page-meta">%s</p>', sprintf(__('Added to the playlist on %s', 'classblogs'), sprintf('<time datetime="%s" class="cb-youtube-local-playlist-page-date">%s</time>', date('c', strtotime($video->date_added)), esc_html(date_i18n(get_option('date_format'), strtotime($video->date_added)))))); if (!empty($video->used_by)) { $markup .= '<p class="cb-youtube-local-playlist-page-usage">' . __('Embedded in', 'classblogs') . ' '; $links = array(); foreach ($video->used_by as $usage) { $link = '<a class="cb-youtube-local-playlist-page-usage-post" '; ClassBlogs_NXTClass::switch_to_blog($usage->blog_id); $link .= sprintf(' href="%s">%s</a>', esc_url(get_permalink($usage->post_id)), get_post($usage->post_id)->post_title); ClassBlogs_NXTClass::restore_current_blog(); $links[] = $link; } $markup .= implode(', ', $links) . '</p>'; } $markup .= '</div>'; } return $markup; }
/** * Returns the URL for the tag-list page. * * @return string the URL for the tag-list page * * @access private * @since 0.2 */ private function _get_tag_page_url() { ClassBlogs_NXTClass::switch_to_blog(ClassBlogs_Settings::get_root_blog_id()); $url = get_permalink($this->get_option('tag_page_id')); ClassBlogs_NXTClass::restore_current_blog(); return $url; }
/** * Toggles the active status of the class blogs suite. * * @param bool $activate whether to activate or deactivate the plugin suite * * @since 0.4 */ public static function _set_activation($activate) { $method = $activate ? 'activate' : 'deactivate'; ClassBlogs_NXTClass::switch_to_blog(ClassBlogs_Settings::get_root_blog_id()); foreach (self::get_all_plugins() as $plugin) { if (is_callable(array($plugin->plugin, $method))) { call_user_func(array($plugin->plugin, $method)); } } ClassBlogs_NXTClass::restore_current_blog(); }
/** * Aggregates all sitewide posts, comments and tags into separate tables. * * @access private * @since 0.2 */ private function _sync_tables() { global $nxtdb; // Drop all data before proceeding $this->_clear_tables(); $this->clear_site_cache(); // Export the post and comment data for each blog to our tables, which // will also update the tag-usage data foreach ($this->_get_usable_blog_ids() as $blog_id) { ClassBlogs_NXTClass::switch_to_blog($blog_id); foreach ($nxtdb->get_results("SELECT ID FROM {$nxtdb->posts}") as $post) { $this->_track_single_post($post->ID); } foreach ($nxtdb->get_results("SELECT comment_ID FROM {$nxtdb->comments}") as $comment) { $this->_track_single_comment($comment->comment_ID); } ClassBlogs_NXTClass::restore_current_blog(); } }