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);
        }
    }
 /**
  * Formats the blog display name based upon the formatting string.
  *
  * @param  string $format  the formatting string for the display name
  * @param  int    $user_id the ID of the student who owns the blog
  * @param  int    $blog_id the ID of the blog
  * @return string          the formatted blog display name
  *
  * @access private
  * @since 0.2
  */
 private function _format_blog_display_name($format, $user_id, $blog_id)
 {
     $blog_title = ClassBlogs_NXTClass::get_blog_option($blog_id, 'blogname');
     $first_name = get_user_meta($user_id, 'first_name', true);
     $last_name = get_user_meta($user_id, 'last_name', true);
     $blog_name = ClassBlogs_Utils::format_user_string($format, array('blog' => $blog_title, 'firstname' => $first_name, 'lastname' => $last_name, 'nickname' => get_user_meta($user_id, 'nickname', true)), 'cb-student-blog');
     // If the blog name is the same as that of the main blog, use the
     // student's full name instead
     $main_blog_title = ClassBlogs_NXTClass::get_blog_option(ClassBlogs_Settings::get_root_blog_id(), 'blogname');
     if ($blog_title === $main_blog_title) {
         $blog_name = $first_name . " " . $last_name;
     }
     return $blog_name;
 }
 /**
  * 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. 4
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;
 }