Esempio n. 1
1
    /**
     * Displays the random-image widget.
     *
     * @uses ClassBlogs_Plugins_RandomImage to get a random image to display
     */
    public function widget($args, $instance)
    {
        $instance = $this->maybe_apply_instance_defaults($instance);
        $plugin = ClassBlogs::get_plugin('random_image');
        $image = $plugin->get_random_image();
        if ($image) {
            $this->start_widget($args, $instance);
            ClassBlogs_NXTClass::switch_to_blog($image->blog_id);
            // If the image is associated with a specific post, provide a link
            // to the post.  If it has no post linkages, show a link to the blog.
            if ($image->post_id) {
                $post_link = sprintf('<a href="%s">%s</a>', esc_url(get_permalink($image->post_id)), esc_html(get_post($image->post_id)->post_title));
                // Show a link to a post on a blog when running in multisite
                // mode, or a link to the post by a particular user when not
                if (ClassBlogs_Utils::is_multisite()) {
                    $caption = sprintf(__('From the post %1$s on %2$s', 'classblogs'), $post_link, sprintf('<a href="%s">%s</a>', esc_url(ClassBlogs_NXTClass::get_blogaddress_by_id($image->blog_id)), esc_html(ClassBlogs_NXTClass::get_blog_option($image->blog_id, 'blogname'))));
                } else {
                    $user = get_userdata($image->user_id);
                    $caption = sprintf(__('From the post %1$s by %2$s', 'classblogs'), $post_link, sprintf('<a href="%s">%s</a>', esc_url(get_author_posts_url($image->user_id)), esc_html($user->display_name)));
                }
            } else {
                // Show a link to the source blog when running in multisite mode,
                // or show the image's title when not
                if (ClassBlogs_Utils::is_multisite()) {
                    $caption = sprintf(__('From the blog %s', 'classblogs'), sprintf('<a href="%s">%s</a>', esc_url(ClassBlogs_NXTClass::get_blogaddress_by_id($image->blog_id)), esc_html(ClassBlogs_NXTClass::get_blog_option($image->blog_id, 'blogname'))));
                } else {
                    $caption = sprintf('<a href="%s">%s</a>', esc_url($image->url), esc_attr($image->title));
                }
            }
            // Display the link to the image with an appropriate caption
            printf('<ul>
					<li>
						<a href="%1$s"><img src="%1$s" alt="%2$s" title="%2$s" width="80%%" /></a>
						<br />
						%3$s
					</li>
				</ul>', esc_url($image->url), esc_attr($image->title), $caption);
            ClassBlogs_NXTClass::restore_current_blog();
            $this->end_widget($args);
        }
    }
 /**
  * Gets a list of recent comments formatted for display in a widget.
  *
  * The array of returned comments contains custom object instances with the
  * following properties that can be used by the widget:
  *
  *      author_name - the display name of the comment's author
  *      content     - the content of the comment
  *      meta        - a string describing the comment's meta, constructed from
  *                    the meta formatting string passed to this method
  *      permalink   - the permalink URL for the comment
  *      post_title  - the name of the post on which the comment was made
  *
  * @param  int    $max_comments          the maximum number of comments to return
  * @param  int    $max_comments_per_blog the most comments allowed per blog
  * @param  string $meta_format           the formatting string for the comment meta
  * @return array                         an array of formatted comments
  *
  * @since 0.2
  */
 public function get_comments_for_widget($max_comments, $max_comments_per_blog, $meta_format)
 {
     // Use cached values if possible
     $cached = $this->get_site_cache('widget');
     if ($cached !== null) {
         return $cached;
     }
     $comments = array();
     $raw_comments = $this->limit_sitewide_resources($this->get_sitewide_comments(), $max_comments, $max_comments_per_blog);
     $student_ids = ClassBlogs_Students::get_student_user_ids();
     foreach ($raw_comments as $comment) {
         // Ignore the comment if it's not by a student
         if (!in_array($comment->user_id, $student_ids)) {
             continue;
         }
         // Create a string for the comment metadata
         $meta = "";
         if ($meta_format) {
             $blog = sprintf('<a href="%s">%s</a>', ClassBlogs_NXTClass::get_blogaddress_by_id($comment->cb_sw_blog_id), ClassBlogs_NXTClass::get_blog_option($comment->cb_sw_blog_id, 'blogname'));
             $meta = ClassBlogs_Utils::format_user_string($meta_format, array('blog' => $blog, 'date' => mysql2date(get_option('date_format'), $comment->comment_date), 'time' => mysql2date(get_option('time_format'), $comment->comment_date)), 'cb-sitewide-comment');
         }
         // Build the permalink to the comment using the post URL and an anchor
         $permalink = sprintf('%s#comment-%d', ClassBlogs_NXTClass::get_blog_permalink($comment->cb_sw_blog_id, $comment->comment_post_ID), $comment->comment_ID);
         $comments[] = (object) array('author_name' => $comment->comment_author, 'content' => $comment->comment_content, 'meta' => $meta, 'permalink' => $permalink, 'post_title' => $comment->post_title);
     }
     $this->set_site_cache('widget', $comments);
     return $comments;
 }
Esempio n. 3
0
 /**
  * Gets a list of recent posts formatted for display in a widget.
  *
  * The array of returned posts contains custom object instances with the
  * following properties that can be used by the widget:
  *
  *      content   - the content of the post
  *      meta      - a string describing the post's meta, constructed from
  *                  the meta formatting string passed to this method
  *      permalink - the permalink URL for the post
  *      title     - the title of the post
  *
  * @param  int    $max_posts          the maximum number of posts to return
  * @param  int    $max_posts_per_blog the most posts allowed per blog
  * @param  string $meta_format        the formatting string for the post meta
  * @return array                      an array of formatted posts
  *
  * @since 0.2
  */
 public function get_posts_for_widget($max_posts, $max_posts_per_blog, $meta_format)
 {
     // Use cache values if possible
     $cached = $this->get_site_cache('widget');
     if ($cached !== null) {
         return $cached;
     }
     $posts = array();
     $raw_posts = $this->limit_sitewide_resources($this->get_sitewide_posts(), $max_posts, $max_posts_per_blog);
     $student_ids = ClassBlogs_Students::get_student_user_ids();
     foreach ($raw_posts as $post) {
         // Ignore the post if it's not by a student
         if (!in_array($post->post_author, $student_ids)) {
             continue;
         }
         // Create a string for the post metadata
         $meta = "";
         if ($meta_format) {
             $user_data = get_userdata($post->post_author);
             $blog = sprintf('<a href="%s" class="cb-sitewide-post-blog">%s</a>', ClassBlogs_NXTClass::get_blogaddress_by_id($post->cb_sw_blog_id), ClassBlogs_NXTClass::get_blog_option($post->cb_sw_blog_id, 'blogname'));
             $meta = ClassBlogs_Utils::format_user_string($meta_format, array('author' => $user_data->display_name, 'blog' => $blog, 'date' => mysql2date(get_option('date_format'), $post->post_date), 'time' => mysql2date(get_option('time_format'), $post->post_date)), 'cb-sitewide-post');
         }
         $posts[] = (object) array('content' => $post->post_content, 'meta' => $meta, 'permalink' => ClassBlogs_NXTClass::get_blog_permalink($post->cb_sw_blog_id, $post->ID), 'title' => $post->post_title);
     }
     $this->set_site_cache('widget', $posts);
     return $posts;
 }
Esempio n. 4
0
 /**
  * Returns a list of information about each student blog.
  *
  * The blog information will be returned as an array, with keys of user IDs.
  * Each key's values will be an object with the following attributes:
  *
  *     blog_id - the possible ID of the blog
  *     url     - the URL of the blog
  *
  * If running in multisite mode, `blog_id` will be the ID of the user's
  * blog.  If running in single-site mode, however, it will be null, as only
  * one blog exists, and the blog URL is of the author archive page.
  *
  * @return array information on all student blogs
  *
  * @since 0.5
  */
 public static function get_student_blogs()
 {
     $blogs = array();
     // Cycle through every student
     foreach (self::get_student_user_ids() as $student_id) {
         // Add the first non-root blog on which the student is an admin if
         // running in multisite mode, or use their author archive URL if
         // running in single-site mode
         $blog_info = array();
         if (ClassBlogs_Utils::is_multisite()) {
             foreach (ClassBlogs_Utils::get_non_root_blog_ids() as $blog_id) {
                 $admins = get_users("blog_id={$blog_id}&include={$student_id}&role=administrator");
                 if (count($admins) == 1) {
                     $blog_info = array('blog_id' => $blog_id, 'url' => ClassBlogs_NXTClass::get_blogaddress_by_id($blog_id));
                     break;
                 }
             }
         } else {
             $blog_info = array('blog_id' => null, 'url' => get_author_posts_url($student_id));
         }
         if (!empty($blog_info)) {
             $blogs[$student_id] = (object) $blog_info;
         }
     }
     return $blogs;
 }