Skip to:
Content

BuddyPress.org

Ticket #5272: bp-blogs-template.php

File bp-blogs-template.php, 40.5 KB (added by lenasterg, 11 years ago)

bp-blogs-template.php file with suggested changes

Line 
1<?php
2/**
3 * BuddyPress Blogs Template Tags.
4 *
5 * @package BuddyPress
6 * @subpackage BlogsTemplate
7 */
8// Exit if accessed directly
9if (!defined('ABSPATH'))
10    exit;
11
12/**
13 * Output the blogs component slug.
14 *
15 * @since BuddyPress (1.5.0)
16 *
17 * @uses bp_get_blogs_slug()
18 */
19function bp_blogs_slug() {
20    echo bp_get_blogs_slug();
21}
22
23/**
24 * Return the blogs component slug.
25 *
26 * @since BuddyPress (1.5.0)
27 *
28 * @return string The 'blogs' slug.
29 */
30function bp_get_blogs_slug() {
31    return apply_filters('bp_get_blogs_slug', buddypress()->blogs->slug);
32}
33
34/**
35 * Output the blogs component root slug.
36 *
37 * @since BuddyPress (1.5.0)
38 *
39 * @uses bp_get_blogs_root_slug()
40 */
41function bp_blogs_root_slug() {
42    echo bp_get_blogs_root_slug();
43}
44
45/**
46 * Return the blogs component root slug.
47 *
48 * @since BuddyPress (1.5.0)
49 *
50 * @return string The 'blogs' root slug.
51 */
52function bp_get_blogs_root_slug() {
53    return apply_filters('bp_get_blogs_root_slug', buddypress()->blogs->root_slug);
54}
55
56/**
57 * Output blog directory permalink.
58 *
59 * @since BuddyPress (1.5.0)
60 *
61 * @uses bp_get_blogs_directory_permalink()
62 */
63function bp_blogs_directory_permalink() {
64    echo bp_get_blogs_directory_permalink();
65}
66
67/**
68 * Return blog directory permalink.
69 *
70 * @since BuddyPress (1.5.0)
71 *
72 * @uses apply_filters()
73 * @uses trailingslashit()
74 * @uses bp_get_root_domain()
75 * @uses bp_get_blogs_root_slug()
76 * @return string The URL of the Blogs directory.
77 */
78function bp_get_blogs_directory_permalink() {
79    return apply_filters('bp_get_blogs_directory_permalink', trailingslashit(bp_get_root_domain() . '/' . bp_get_blogs_root_slug()));
80}
81
82/**
83 * The main blog template loop class.
84 *
85 * Responsible for loading a group of blogs into a loop for display.
86 */
87class BP_Blogs_Template {
88
89    /**
90     * The loop iterator.
91     *
92     * @access public
93     * @var int
94     */
95    var $current_blog = -1;
96
97    /**
98     * The number of blogs returned by the paged query.
99     *
100     * @access public
101     * @var int
102     */
103    var $blog_count;
104
105    /**
106     * Array of blogs located by the query..
107     *
108     * @access public
109     * @var array
110     */
111    var $blogs;
112
113    /**
114     * The blog object currently being iterated on.
115     *
116     * @access public
117     * @var object
118     */
119    var $blog;
120
121    /**
122     * A flag for whether the loop is currently being iterated.
123     *
124     * @access public
125     * @var bool
126     */
127    var $in_the_loop;
128
129    /**
130     * The page number being requested.
131     *
132     * @access public
133     * @var public
134     */
135    var $pag_page;
136
137    /**
138     * The number of items being requested per page.
139     *
140     * @access public
141     * @var public
142     */
143    var $pag_num;
144
145    /**
146     * An HTML string containing pagination links.
147     *
148     * @access public
149     * @var string
150     */
151    var $pag_links;
152
153    /**
154     * The total number of blogs matching the query parameters.
155     *
156     * @access public
157     * @var int
158     */
159    var $total_blog_count;
160
161    /**
162     * Constructor method.
163     *
164     * @see BP_Blogs_Blog::get() for a description of parameters.
165     * @param array $args {
166     *     An array of arguments. All items are optional.
167     * @param string $type See {@link BP_Blogs_Blog::get()}.
168     * @param string $page See {@link BP_Blogs_Blog::get()}.
169     * @param string $per_page See {@link BP_Blogs_Blog::get()}.
170     * @param string $max See {@link BP_Blogs_Blog::get()}.
171     * @param string $user_id See {@link BP_Blogs_Blog::get()}.
172     * @param string $search_terms See {@link BP_Blogs_Blog::get()}.
173     * @param string $page_arg The string used as a query parameter in
174     *        pagination links. Default: 'bpage'.
175     * @param boolean $show_hidden
176     *
177     * }
178     * @version 2, by stergatu, merged all args to an array and added $show_hidden into args
179     */
180    function __construct($args) {
181        // Backward compatibility with old method of passing arguments
182        if (!is_array($args) || func_num_args() > 1) {
183            _deprecated_argument(__METHOD__, '2.0', sprintf(__('Arguments passed to %1$s should be in an associative array. See the inline documentation at %2$s for more details.', 'buddypress'), __METHOD__, __FILE__));
184
185            $old_args_keys = array(
186                0 => 'type',
187                1 => 'page',
188                2 => 'per_page',
189                3 => 'max',
190                4 => 'user_id',
191                5 => 'search_terms',
192                6 => 'page_arg',
193                7 => 'show_hidden'
194            );
195
196            $func_args = func_get_args();
197            $args = bp_core_parse_args_array($old_args_keys, $func_args);
198        }
199
200        $defaults = array(
201            'type' => 'active',
202            'page' => 1,
203            'per_page' => 20,
204            'max' => false,
205            'user_id' => '0',
206            'search_terms' => false, // Pass search terms to filter on the blog title or description.
207            'page_arg' => 'bpage', // See https://buddypress.trac.wordpress.org/ticket/3679
208            'show_hidden' => false
209        );
210        $r = wp_parse_args($args, $defaults);
211        extract($r);
212
213        $this->pag_page = isset($_REQUEST[$page_arg]) ? intval($_REQUEST[$page_arg]) : $page;
214        $this->pag_num = isset($_REQUEST['num']) ? intval($_REQUEST['num']) : $per_page;
215
216        if (isset($_REQUEST['letter']) && '' != $_REQUEST['letter']) {
217            $this->blogs = BP_Blogs_Blog::get_by_letter($_REQUEST['letter'], $this->pag_num, $this->pag_page, $show_hidden);
218        } else {
219            $this->blogs = bp_blogs_get_blogs(array('type' => $type, 'per_page' => $this->pag_num, 'page' => $this->pag_page, 'user_id' => $user_id, 'search_terms' => $search_terms, 'show_hidden' => $show_hidden));
220        }
221        if (!$max || $max >= (int) $this->blogs['total'])
222            $this->total_blog_count = (int) $this->blogs['total'];
223        else
224            $this->total_blog_count = (int) $max;
225
226        $this->blogs = $this->blogs['blogs'];
227
228        if ($max) {
229            if ($max >= count($this->blogs)) {
230                $this->blog_count = count($this->blogs);
231            } else {
232                $this->blog_count = (int) $max;
233            }
234        } else {
235            $this->blog_count = count($this->blogs);
236        }
237
238        if ((int) $this->total_blog_count && (int) $this->pag_num) {
239            $this->pag_links = paginate_links(array(
240                'base' => add_query_arg($page_arg, '%#%'),
241                'format' => '',
242                'total' => ceil((int) $this->total_blog_count / (int) $this->pag_num),
243                'current' => (int) $this->pag_page,
244                'prev_text' => _x('&larr;', 'Blog pagination previous text', 'buddypress'),
245                'next_text' => _x('&rarr;', 'Blog pagination next text', 'buddypress'),
246                'mid_size' => 1
247            ));
248        }
249    }
250
251    /**
252     * Whether there are blogs available in the loop.
253     *
254     * @see bp_has_blogs()
255     *
256     * @return bool True if there are items in the loop, otherwise false.
257     */
258    function has_blogs() {
259        if ($this->blog_count)
260            return true;
261
262        return false;
263    }
264
265    /**
266     * Set up the next blog and iterate index.
267     *
268     * @return object The next blog to iterate over.
269     */
270    function next_blog() {
271        $this->current_blog++;
272        $this->blog = $this->blogs[$this->current_blog];
273
274        return $this->blog;
275    }
276
277    /**
278     * Rewind the blogs and reset blog index.
279     */
280    function rewind_blogs() {
281        $this->current_blog = -1;
282        if ($this->blog_count > 0) {
283            $this->blog = $this->blogs[0];
284        }
285    }
286
287    /**
288     * Whether there are blogs left in the loop to iterate over.
289     *
290     * This method is used by {@link bp_blogs()} as part of the while loop
291     * that controls iteration inside the blogs loop, eg:
292     *     while ( bp_blogs() ) { ...
293     *
294     * @see bp_blogs()
295     *
296     * @return bool True if there are more blogs to show, otherwise false.
297     */
298    function blogs() {
299        if ($this->current_blog + 1 < $this->blog_count) {
300            return true;
301        } elseif ($this->current_blog + 1 == $this->blog_count) {
302            do_action('blog_loop_end');
303            // Do some cleaning up after the loop
304            $this->rewind_blogs();
305        }
306
307        $this->in_the_loop = false;
308        return false;
309    }
310
311    /**
312     * Set up the current blog inside the loop.
313     *
314     * Used by {@link bp_the_blog()} to set up the current blog data while
315     * looping, so that template tags used during that iteration make
316     * reference to the current blog.
317     *
318     * @see bp_the_blog()
319     */
320    function the_blog() {
321
322        $this->in_the_loop = true;
323        $this->blog = $this->next_blog();
324
325        if (0 == $this->current_blog) // loop has just started
326            do_action('blog_loop_start');
327    }
328
329}
330
331/**
332 * Rewind the blogs and reset blog index.
333 */
334function bp_rewind_blogs() {
335    global $blogs_template;
336
337    $blogs_template->rewind_blogs();
338}
339
340/**
341 * Initialize the blogs loop.
342 *
343 * Based on the $args passed, bp_has_blogs() populates the $blogs_template
344 * global, enabling the use of BuddyPress templates and template functions to
345 * display a list of activity items.
346 *
347 * @global object $blogs_template {@link BP_Blogs_Template}
348 *
349 * @param array $args {
350 *     Arguments for limiting the contents of the blogs loop. Most arguments
351 *     are in the same format as {@link BP_Blogs_Blog::get()}. However, because
352 *     the format of the arguments accepted here differs in a number of ways,
353 *     and because bp_has_blogs() determines some default arguments in a
354 *     dynamic fashion, we list all accepted arguments here as well.
355 *
356 *     Arguments can be passed as an associative array, or as a URL query
357 *     string (eg, 'user_id=4&per_page=3').
358 *
359 *     @type int $page Which page of results to fetch. Using page=1 without
360 *           per_page will result in no pagination. Default: 1.
361 *     @type int|bool $per_page Number of results per page. Default: 20.
362 *     @type string $page_arg The string used as a query parameter in
363 *           pagination links. Default: 'bpage'.
364 *     @type int|bool $max Maximum number of results to return.
365 *           Default: false (unlimited).
366 *     @type string $type The order in which results should be fetched.
367  'active', 'alphabetical', 'newest', or 'random'.
368 *     @type string $sort 'ASC' or 'DESC'. Default: 'DESC'.
369 *     @type string $search_terms Limit results by a search term. Default: null.
370 *     @type int $user_id The ID of the user whose blogs should be retrieved.
371 *           When viewing a user profile page, 'user_id' defaults to the ID of
372 *           the displayed user. Otherwise the default is false.
373 *              @type bool $show_hidden. Whether to show private blogs or not
374 * }
375 * @return bool Returns true when blogs are found, otherwise false.
376 * @version 2, stergatu added $show_hidden, add passes array to BP_Blogs_Template
377 */
378function bp_has_blogs($args = '') {
379    global $blogs_template;
380
381    /*     * *
382     * Set the defaults based on the current page. Any of these will be overridden
383     * if arguments are directly passed into the loop. Custom plugins should always
384     * pass their parameters directly to the loop.
385     */
386    $type = 'active';
387    $user_id = 0;
388    $search_terms = null;
389
390// User filtering
391    if (bp_displayed_user_id())
392        $user_id = bp_displayed_user_id();
393
394    $defaults = array(
395        'type' => $type,
396        'page' => 1,
397        'per_page' => 20,
398        'max' => false,
399        'page_arg' => 'bpage', // See https://buddypress.trac.wordpress.org/ticket/3679
400        'user_id' => $user_id, // Pass a user_id to limit to only blogs this user has higher than subscriber access to
401        'search_terms' => $search_terms, // Pass search terms to filter on the blog title or description.
402        'show_hidden' => false
403    );
404
405    $r = wp_parse_args($args, $defaults);
406    extract($r);
407
408    if (is_null($search_terms)) {
409        if (isset($_REQUEST['s']) && !empty($_REQUEST['s']))
410            $search_terms = $_REQUEST['s'];
411        else
412            $search_terms = false;
413    }
414
415    if ($max) {
416        if ($per_page > $max) {
417            $per_page = $max;
418        }
419    }
420
421    $args = array('type' => $type, 'page' => $page, 'per_page' => $per_page, 'max' => $max, 'user_id' => $user_id, 'search_terms' => $search_terms, 'page_arg' => $page_arg, 'show_hidden' => $show_hidden);
422    $blogs_template = new BP_Blogs_Template($args);
423    return apply_filters('bp_has_blogs', $blogs_template->has_blogs(), $blogs_template);
424}
425
426/**
427 * Determine if there are still blogs left in the loop.
428 *
429 * @global object $blogs_template {@link BP_Blogs_Template}
430 *
431 * @return bool Returns true when blogs are found.
432 */
433function bp_blogs() {
434    global $blogs_template;
435
436    return $blogs_template->blogs();
437}
438
439/**
440 * Get the current blog object in the loop.
441 *
442 * @global object $blogs_template {@link BP_Blogs_Template}
443 *
444 * @return object The current blog within the loop.
445 */
446function bp_the_blog() {
447    global $blogs_template;
448
449    return $blogs_template->the_blog();
450}
451
452/**
453 * Output the blogs pagination count.
454 *
455 * @global object $blogs_template {@link BP_Blogs_Template}
456 */
457function bp_blogs_pagination_count() {
458    global $blogs_template;
459    $start_num = intval(( $blogs_template->pag_page - 1 ) * $blogs_template->pag_num) + 1;
460    $from_num = bp_core_number_format($start_num);
461    $to_num = bp_core_number_format(( $start_num + ( $blogs_template->pag_num - 1 ) > $blogs_template->total_blog_count ) ? $blogs_template->total_blog_count : $start_num + ( $blogs_template->pag_num - 1 ) );
462    $total = bp_core_number_format($blogs_template->total_blog_count);
463
464    echo sprintf(_n('Viewing site %1$s to %2$s (of %3$s site)', 'Viewing site %1$s to %2$s (of %3$s sites)', $total, 'buddypress'), $from_num, $to_num, $total);
465}
466
467/**
468 * Output the blogs pagination links.
469 */
470function bp_blogs_pagination_links() {
471    echo bp_get_blogs_pagination_links();
472}
473
474/**
475 * Return the blogs pagination links.
476 *
477 * @global object $blogs_template {@link BP_Blogs_Template}
478 *
479 * @return string HTML pagination links.
480 */
481function bp_get_blogs_pagination_links() {
482    global $blogs_template;
483
484    return apply_filters('bp_get_blogs_pagination_links', $blogs_template->pag_links);
485}
486
487/**
488 * Output a blog's avatar.
489 *
490 * @see bp_get_blog_avatar() for description of arguments.
491 *
492 * @param array $args See {@link bp_get_blog_avatar()}.
493 */
494function bp_blog_avatar($args = '') {
495    echo bp_get_blog_avatar($args);
496}
497
498/**
499 * Get a blog's avatar.
500 *
501 * At the moment, blog avatars are simply the user avatars of the blog
502 * admin. Filter 'bp_get_blog_avatar_' . $blog_id to customize.
503 *
504 * @see bp_core_fetch_avatar() For a description of arguments and
505 *      return values.
506 *
507 * @param array $args  {
508 *     Arguments are listed here with an explanation of their defaults.
509 *     For more information about the arguments, see
510 *     {@link bp_core_fetch_avatar()}.
511 *     @type string $alt Default: 'Profile picture of site author
512 *           [user name]'.
513 *     @type string $class Default: 'avatar'.
514 *     @type string $type Default: 'full'.
515 *     @type int|bool $width Default: false.
516 *     @type int|bool $height Default: false.
517 *     @type bool $id Currently unused.
518 *     @type bool $no_grav Default: false.
519 * }
520 * @return string User avatar string.
521 */
522function bp_get_blog_avatar($args = '') {
523    global $blogs_template;
524
525    $defaults = array(
526        'type' => 'full',
527        'width' => false,
528        'height' => false,
529        'class' => 'avatar',
530        'id' => false,
531        'alt' => sprintf(__('Profile picture of site author %s', 'buddypress'), bp_core_get_user_displayname($blogs_template->blog->admin_user_id)),
532        'no_grav' => true
533    );
534
535    $r = wp_parse_args($args, $defaults);
536    extract($r, EXTR_SKIP);
537
538    /*     * *
539     * In future BuddyPress versions you will be able to set the avatar for a blog.
540     * Right now you can use a filter with the ID of the blog to change it if you wish.
541     * By default it will return the avatar for the primary blog admin.
542     *
543     * This filter is deprecated as of BuddyPress 1.5 and may be removed in a future version.
544     * Use the 'bp_get_blog_avatar' filter instead.
545     */
546    $avatar = apply_filters('bp_get_blog_avatar_' . $blogs_template->blog->blog_id, bp_core_fetch_avatar(array('item_id' => $blogs_template->blog->admin_user_id, 'type' => $type, 'alt' => $alt, 'width' => $width, 'height' => $height, 'class' => $class, 'email' => $blogs_template->blog->admin_user_email)));
547
548    return apply_filters('bp_get_blog_avatar', $avatar, $blogs_template->blog->blog_id, array('item_id' => $blogs_template->blog->admin_user_id, 'type' => $type, 'alt' => $alt, 'width' => $width, 'height' => $height, 'class' => $class, 'email' => $blogs_template->blog->admin_user_email));
549}
550
551function bp_blog_permalink() {
552    echo bp_get_blog_permalink();
553}
554
555function bp_get_blog_permalink() {
556    global $blogs_template;
557
558    if (empty($blogs_template->blog->domain))
559        $permalink = bp_get_root_domain() . $blogs_template->blog->path;
560    else {
561        $protocol = 'http://';
562        if (is_ssl())
563            $protocol = 'https://';
564
565        $permalink = $protocol . $blogs_template->blog->domain . $blogs_template->blog->path;
566    }
567
568    return apply_filters('bp_get_blog_permalink', $permalink);
569}
570
571/**
572 * Output the name of the current blog in the loop.
573 */
574function bp_blog_name() {
575    echo bp_get_blog_name();
576}
577
578/**
579 * Return the name of the current blog in the loop.
580 *
581 * @return string The name of the current blog in the loop.
582 */
583function bp_get_blog_name() {
584    global $blogs_template;
585
586    return apply_filters('bp_get_blog_name', $blogs_template->blog->name);
587}
588
589/**
590 * Output the ID of the current blog in the loop.
591 *
592 * @since BuddyPress (1.7.0)
593 */
594function bp_blog_id() {
595    echo bp_get_blog_id();
596}
597
598/**
599 * Return the ID of the current blog in the loop.
600 *
601 * @since BuddyPress (1.7.0)
602 *
603 * @return int ID of the current blog in the loop.
604 */
605function bp_get_blog_id() {
606    global $blogs_template;
607
608    return apply_filters('bp_get_blog_id', $blogs_template->blog->blog_id);
609}
610
611/**
612 * Output the description of the current blog in the loop.
613 */
614function bp_blog_description() {
615    echo apply_filters('bp_blog_description', bp_get_blog_description());
616}
617
618/**
619 * Return the description of the current blog in the loop.
620 *
621 * @ret
622  urn string Description of the current blog in the loop.
623 */
624function bp_get_blog_description() {
625    global $blogs_template;
626
627    return apply_filters('bp_get_blog_description', $blogs_template->blog->description);
628}
629
630/**
631 * Output the row class of the current blog in the loop.
632 *
633 * @since BuddyPress (1.7.0)
634 */
635function bp_blog_class() {
636    echo bp_get_blog_class();
637}
638
639/**
640 * Return the row class of the current blog in the loop.
641 *
642 * @since BuddyPress (1.7.0)
643 *
644 * @global BP_Blogs_Template $blogs_template
645 *
646 * @return string Row class of the site.
647 */
648function bp_get_blog_class() {
649    global $blogs_template;
650
651    $classes = array();
652    $pos_in_loop = (int) $blogs_template->current_blog;
653
654// If we've only one site in the loop, don't bother with odd and even.
655    if ($blogs_template->blog_count > 1)
656        $classes[] = ( $pos_in_loop % 2 ) ? 'even' : 'odd';
657    else
658        $classes[] = 'bp-single-blog';
659
660    $classes = apply_filters('bp_get_blog_class', $classes);
661    $classes = array_merge($classes, array());
662
663    $retval = 'class="' . join(' ', $classes) . '"';
664    return $retval;
665}
666
667/**
668 * Output the last active date of the current blog in the loop.
669 */
670function bp_blog_last_active() {
671    echo bp_get_blog_last_active();
672}
673
674/**
675 * Return the last active date of the current blog in the loop.
676 *
677 * @return string Last active date.
678 */
679function bp_get_blog_last_active() {
680    global $blogs_template;
681
682    return apply_filters('bp_blog_last_active', bp_core_get_last_activity($blogs_template->blog->last_activity, __('active %s', 'buddypress')));
683}
684
685/**
686 * Output the latest post from the current blog in the loop.
687 */
688function bp_blog_latest_post() {
689    echo bp_get_blog_latest_post();
690}
691
692/**
693 * Return the latest post from the current blog in the loop.
694 *
695 * @return string $retval String of the form 'Latest Post: [link to post]'.
696 */
697function bp_get_blog_latest_post() {
698    global $blogs_template;
699
700    $retval = bp_get_blog_latest_post_title();
701
702    if (!empty($retval))
703        $retval = sprintf(__('Latest Post: %s', 'buddypress'), '<a href="' . $blogs_template->blog->latest_post->guid . '">' . apply_filters('the_title', $retval) . '</a>');
704
705    return apply_filters('bp_get_blog_latest_post', $retval);
706}
707
708/**
709 * Output the title of the latest post on the current blog in the loop.
710 *
711 * @since BuddyPress (1.7.0)
712 *
713 * @see bp_get_blog_latest_post_title()
714 */
715function bp_blog_latest_post_title() {
716    echo bp_get_blog_latest_post_title();
717}
718
719/**
720 * Return the title of the latest post on the current blog in the loop.
721 *
722 * @since BuddyP
723  ress (1.7.0)
724 *
725 * @global BP_Blogs_Template
726 *
727 * @return string Post title.
728 */
729function bp_get_blog_latest_post_title() {
730    global $blogs_template;
731
732    $retval = '';
733
734    if (!empty($blogs_template->blog->latest_post) && !empty($blogs_template->blog->latest_post->post_title))
735        $retval = $blogs_template->blog->latest_post->post_title;
736
737    return apply_filters('bp_get_blog_latest_post_title', $retval);
738}
739
740/**
741 * Output the permalink of the latest post on the current blog in the loop.
742 *
743 * @since BuddyPress (1.7.0)
744 *
745 * @see bp_get_blog_latest_post_title()
746 */
747function bp_blog_latest_post_permalink() {
748    echo bp_get_blog_latest_post_permalink();
749}
750
751/**
752 * Return the permalink of the latest post on the current blog in the loop.
753 *
754 * @since BuddyPress (1.7.0)
755 *
756 * @global BP_Blogs_Template
757 *
758 * @return string URL of the blog's latest post.
759 */
760function bp_get_blog_latest_post_permalink() {
761    global $blogs_template;
762
763    $retval = '';
764
765    if (!empty($blogs_template->blog->latest_post) && !empty($blogs_template->blog->latest_post->ID))
766        $retval = add_query_arg('p', $blogs_template->blog->latest_post->ID, bp_get_blog_permalink());
767
768    return apply_filters('bp_get_blog_latest_post_permalink', $retval);
769}
770
771/**
772 * Output the content of the latest post on the current blog in the l
773  oop.
774 *
775 * @since BuddyPress (1.7.0)
776 *
777 * @uses bp_get_blog_latest_post_content()
778 */
779function bp_blog_latest_post_content() {
780    echo bp_get_blog_latest_post_content();
781}
782
783/**
784 * Return the content of the latest post on the current blog in the loop.
785 *
786 * @since BuddyPress (1.7.0)
787 *
788 * @global BP_Blogs_Template
789 *
790 * @return string Content of the blog's latest post.
791 */
792function bp_get_blog_latest_post_content() {
793    global $blogs_template;
794
795    $retval = '';
796
797    if (!empty($blogs_template->blog->latest_post) && !empty($blogs_template->blog->latest_post->post_content))
798        $retval = $blogs_template->blog->latest_post->post_content;
799
800    return apply_filters('bp_get_blog_latest_post_content', $retval);
801}
802
803/**
804 * Output the featured image of the latest post on the current blog in the loop.
805 *
806 * @since BuddyPress (1.7.0)
807 *
808 * @see bp_get_blog_latest_post_conten
809  t() For description of parameters.
810 *
811 * @param string $size See {@link bp_get_blog_latest_post_featured_image()}.
812 */
813function bp_blog_latest_post_featured_image($size = 'thumbnail') {
814    echo bp_get_blog_latest_post_featured_image($size);
815}
816
817/**
818 * Return the featured image of the latest post on the current blog in the loop.
819 *
820 * @since BuddyPress (1.7.0)
821 *
822 * @global BP_Blogs_Template
823 *
824 * @param string $size Image version to return. 'thumbnail', 'medium',
825 *        'large', or 'post-thumbnail'. Default: 'thumbnail'.
826 * @return string URL of the image.
827 */
828function bp_get_blog_latest_post_featured_image($size = 'thumbnail') {
829    global $blogs_template;
830
831    $retval = '';
832
833    if (!empty($blogs_template->blog->latest_post) && !empty($blogs_template->blog->latest_post->images[$size]))
834        $retval = $blogs_template->blog->latest_post->images[$size];
835
836    return apply_filters('bp_get_blog_latest_post_featured_image', $retval);
837}
838
839/**
840 * Does the latest blog post have a featured image?
841 *
842 * @since BuddyPress (1.7.0)
843 *
844 * @param string $size Image version to return. 'thumbnail', 'medium', 'large',
845 *        or 'post-thumbnail'. Default: 'thumbnail'.
846 * @return bool True if the latest blog post from the current blog has a
847 *         featured image of the given size.
848 */
849function bp_blog_latest_post_has_featured_image($thumbnail = 'thumbnail') {
850    $image = bp_get_blog_latest_post_featured_image($thumbnail);
851
852    return apply_filters('bp_blog_latest_post_has_featured_image', !empty($image), $thumbnail, $image);
853}
854
855/**
856 * Output hidden fields to help with form submissions in Sit
857  es directory.
858 *
859 * This function detects whether 's', 'letter', or 'blogs_search' requests are
860 * currently being made (as in a URL parameter), and creates corresponding
861 * hidden fields.
862 */
863function bp_blog_hidden_fields() {
864    if (isset($_REQUEST['s']))
865        echo '<input type="hidden" id="search_terms" value="' . esc_attr($_REQUEST['s']) . '" name="search_terms" />';
866
867    if (isset($_REQUEST['letter']))
868        echo '<input type="hidden" id="selected_letter" value="' . esc_attr($_REQUEST['letter']) . '" name="selected_letter" />';
869
870    if (isset($_REQUEST['blogs_search']))
871        echo '<input type="hidden" id="search_terms" value="' . esc_attr($_REQUEST['blogs_search']) . '" name="search_terms" />';
872}
873
874/**
875 * Output the total number of blogs on the site.
876 */
877function bp_total_blog_count() {
878    echo bp_get_total_blog_count();
879}
880
881/**
882 * Return the total number of blogs on the site.
883 *
884 * @return int Total number of blogs.
885 */
886function bp_get_total_blog_count() {
887    return apply_filters('bp_get_total_blog_count', bp_blogs_total_blogs());
888}
889
890add_filter('bp_get_total_blog_count', 'bp_core_number_format');
891
892/**
893 * Output the total number of blogs for a given user.
894 *
895 * @param int $user_id ID of the user.
896 * @param bool $show_hidden  Whether to count also hidden blogs.
897 * @version 2, stergatu added @param $show_hidden
898 */
899function bp_total_blog_count_for_user($user_id = 0, $show_hidden = false) {
900    echo bp_get_total_blog_count_for_user($user_id, $show_hidden);
901}
902
903/**
904 * Return the total number of blogs for a given user.
905 *
906 * @param int $user_id ID of the user.
907 * @param bool $show_hidden  whether to count also hidden blogs
908 * @return int Total number of blogs for the user.
909 * @version 2, stergatu added @param $show_hidden
910 */
911function bp_get_total_blog_count_for_user($user_id = 0, $show_hidden = false) {
912    return apply_filters('bp_get_total_blog_count_for_user', bp_blogs_total_blogs_for_user($user_id, $show_hidden));
913}
914
915add_filter('bp_get_total_blog_count_for_user', 'bp_core_number_format');
916
917
918/** Blog Registration ******************************************************* */
919
920/**
921 * Checks whether blog creation is enabled.
922 *
923 * Returns true
924  when blog creation is enabled for logged-in users only, or
925 * when it's enabled for new registrations.
926 *
927 * @return bool True if blog registration is e
928  nabled.
929 */
930function bp_blog_signup_enabled() {
931    global $bp;
932
933    $active_signup = isset($bp->site_options['registration']) ? $bp->site_options['registration'] : 'all';
934
935    $active_signup = apply_filters('wpmu_active_signup', $active_signup); // return "all", "none", "blog" or "user"
936    if ('none' == $active_signup || 'user' == $active_signup)
937        return false;
938
939    return true;
940}
941
942/**
943 * Output the wrapper markup for the blog signup form.
944 *
945 * @param string $blogname Optional. The default blog name (path or domain).
946 * @param string $blog_title Optional. The default blog title.
947 * @param string|WP_Error Optional. The WP_Error object returned by a previous
948 *        submission attempt.
949 */
950function bp_show_blog_signup_form($blogname = '', $blog_title = '', $errors = '') {
951    global $current_user;
952
953    if (isset($_POST['submit'])) {
954        bp_blogs_validate_blog_signup();
955    } else {
956        if (!is_wp_error($errors)) {
957            $errors = new WP_Error();
958        }
959
960// allow definition of default variables
961        $filtered_results = apply_filters('signup_another_blog_init', array('blogname' => $blogname, 'blog_title' => $blog_title, 'errors' => $errors));
962        $blogname = $filtered_results['blogname'];
963        $blog_title = $filtered_results['blog_title'];
964        $errors = $filtered_results['errors'];
965
966        if ($errors->get_error_code()) {
967            echo "<p>" . __('There was a problem, please correct the form below and try again.', 'buddypress') . "</p>";
968        }
969        ?>
970                <p><?php printf(__("By filling out the form below, you can <strong>add a site to your account</strong>. There is no limit to the number of sites that you can have, so create to your heart's content, but blog responsibly!", 'buddypress'), $current_user->display_name) ?></p>
971
972                        <p><?php _e("If you&#8217;re not going to use a great domain, leave it for a new user. Now have at it!", 'buddypress') ?></p>
973
974                        <form class="standard-form" id="setupform" method="post" action="">
975
976                            <input type="hidden" name="stage" value="gimmeanotherblog" />
977                    <?php do_action('signup_hidden_fields'); ?>
978
979                    <?php bp_blogs_signup_blog($blogname, $blog_title, $errors); ?>
980                    <p>
981                        <input id="submit" type="submit" name="submit" class="submit" value="<?php _e('Create Site', 'buddypress') ?>" />
982                    </p>
983
984                    <?php wp_nonce_field('bp_blog_signup_form') ?>
985                </form>
986                <?php
987            }
988        }
989
990        /**
991         * Output the input fields for the blog creation form.
992         *
993         * @param string $blogname Optional. The default blog name (path or domain).
994         * @param string $blog_title Optional. The default blog title.
995         * @param string|WP_Error Optional. The WP_Error object returned by a previous
996         *        submission attempt.
997         */
998        function bp_blogs_signup_blog($blogname = '', $blog_title = '', $errors = '') {
999            global $current_site;
1000
1001            // Blog name
1002            if (!is_subdomain_install())
1003                echo '<label for="blogname">' . __('Site Name:', 'buddypress') . '</label>';
1004            else
1005                echo '<label for="blogname">' . __('Site Domain:', 'buddypress') . '</label>';
1006
1007            if ($errmsg = $errors->get_error_message('blogname')) {
1008                ?>
1009
1010                        <p class="error"><?php echo $errmsg ?></p>
1011
1012                <?php
1013            }
1014
1015            if (!is_subdomain_install())
1016                echo '<span class="prefix_address">' . $current_site->domain . $current_site->path . '</span> <input name="blogname" type="text" id="blogname" value="' . $blogname . '" maxlength="63" /><br />';
1017            else
1018                echo '<input name="blogname" type="text" id="blogname" value="' . $blogname . '" maxlength="63" /> <span class="suffix_address">.' . bp_blogs_get_subdomain_base() . '</span><br />';
1019
1020            if (!is_user_logged_in()) {
1021                print '(<strong>' . __('Your address will be ', 'buddypress');
1022
1023        if (!is_subdomain_install()) {
1024                    print $current_site->domain . $current_site->path . __('blogname', 'buddypress');
1025                } else {
1026                    print __('domain.', 'buddypress') . $current_site->domain . $current_site->path;
1027                }
1028
1029        echo '.</strong> ' . __('Must be at least 4 characters, letters and numbers only. It cannot be changed so choose carefully!)', 'buddypress') . '</p>';
1030            }
1031
1032            // Blog Title
1033            ?>
1034
1035        <label for="blog_title"><?php _e('Site Title:', 'buddypress') ?></label>
1036
1037        <?php if ($errmsg = $errors->get_error_message('blog_title')) { ?>
1038
1039    <p class="error"><?php echo $errmsg ?></p>
1040
1041                <?php
1042            }
1043            echo '<input name="blog_title" type="text" id="blog_title" value="' . esc_html($blog_title, 1) . '" /></p>';
1044            ?>
1045
1046        <p>
1047                <label for="blog_public_on"><?php _e('Privacy:', 'buddypress') ?></label>
1048                <?php _e('I wou
1049        ld like my site to appear in search engines, and in public listings around this network.', 'buddypress'); ?>
1050
1051                <label class="checkbox" for="blog_public_on">
1052                <input type="radio" id="blog_public_on" name="blog_public" value="1" <?php
1053                if (!isset($_POST['blog_public']) ||
1054                        '1' == $_POST['blog_public']) {
1055                    ?>checked="checked"<?php } ?> />
1056
1057                    <strong><?php _e('Yes', 'buddypress'); ?></strong>
1058            </label>
1059            <label class="checkbox" for="blog_public_off">
1060                <input type="radio" id="blog_public_off" name="blog_public" value="0" <?php if (isset($_POST['blog_public']) && '0' == $_POST['blog_public']) { ?>checked="checked"<?php } ?> />
1061                <strong><?php _e('No', 'buddypress'); ?></strong>
1062            </label>
1063        </p>
1064
1065        <?php
1066        do_action('signup_blogform', $errors);
1067    }
1068
1069    /**
1070     * Output the base URL for subdomain installations of WordPress Multisite.
1071     *
1072     * @since BuddyPress (1.6.0)
1073     */
1074    function bp_blogs_subdomain_base() {
1075        echo bp_blogs_get_subdomain_base();
1076    }
1077
1078    /**
1079     * Return the base URL for subdomain installations of WordPress Multisite.
1080     *
1081     * @since BuddyPress (1.6.0)
1082     *
1083     * @return string The base URL - eg, 'example.com' for site_url() example.com or www.example.com.
1084     */
1085    function bp_blogs_get_subdomain_base() {
1086        global $current_site;
1087
1088        return apply_filters('bp_blogs_subdomain_base', preg_replace('|^www\.|', '', $current_site->domain) . $current_site->path);
1089    }
1090
1091    /**
1092     * Process a blog registration submission.
1093     *
1094     * Passes submitted values to {@link wpmu_create_blog()}.
1095     *
1096     * @return bool True on success, false on failure.
1097     */
1098    function bp_blogs_validate_blog_signup() {
1099        global $wpdb, $current_user, $blogname, $blog_title, $errors, $domain, $path, $current_site;
1100
1101        if (!check_admin_referer('bp_blog_signup_form'))
1102            return false;
1103
1104        $current_user = wp_get_current_user();
1105
1106        if (!is_user_logged_in())
1107            die();
1108
1109        $result = bp_blogs_validate_blog_form();
1110        extract($result);
1111
1112        if ($errors->get_error_code()) {
1113            unset($_POST['submit']);
1114            bp_show_blog_signup_form($blogname, $blog_title, $errors);
1115            return false;
1116        }
1117
1118        $public = (int) $_POST['blog_public'];
1119
1120        $meta = apply_filters('signup_create_blog_meta', array('lang_id' => 1, 'public' => $public)); // depreciated
1121        $meta = apply_filters('add_signup_meta', $meta);
1122
1123        // If this is a subdomain install, set up the site inside the root domain.
1124        if (is_subdomain_install())
1125            $domain = $blogname . '.' . preg_replace('|^www\.|', '', $current_site->domain);
1126
1127        wpmu_create_blog($domain, $path, $blog_title, $current_user->ID, $meta, $wpdb->siteid);
1128        bp_blogs_confirm_blog_signup($domain, $path, $blog_title, $current_user->user_login, $current_user->user_email, $meta);
1129        return true;
1130    }
1131
1132    /**
1133     * Validate a blog creation submission.
1134     *
1135     * Essentially, a wrapper for {@link wpmu_validate_blog_signup()}.
1136     *
1137     * @return array Contains the new site data and error messages.
1138
1139     */
1140    function bp_blogs_validate_blog_form() {
1141        $user = '';
1142
1143        if (is_user_logged_in())
1144            $user = wp_get_current_user();
1145
1146        return wpmu_validate_blog_signup($_POST['blogname'], $_POST['blog_title'], $user);
1147    }
1148
1149    /**
1150     * Display a message after successful blog registration.
1151     *
1152     * @param string $domain The new blog's domain.
1153     * @param string $path The new blog's path.
1154     * @param string $blog_title The new blog's title.
1155     * @param string $user_name The user name of the user who created the blog. Unused.
1156
1157     * @param string $user_email The email of the user who created the blog. Unused.
1158     * @param string|array $meta Meta values associated with the new blog. Unused.
1159     */
1160    function bp_blogs_confirm_blog_signup($domain, $path, $blog_title, $user_name, $user_email = '', $meta = '') {
1161        $protocol = is_ssl() ? 'https://' : 'http://';
1162        $blog_url = $protocol . $domain . $path;
1163        ?>
1164        <p><?php _e('Congratulations! You have successfully registered a new site.', 'buddypress') ?></p>
1165        <p>
1166            <?php printf(__('<a href="%1$s">%2$s</a> is your new site.  <a href="%3$s">Login</a> as "%4$s" using your existing password.', 'buddypress'), $blog_url, $blog_url, $blog_url . "wp-login.php", $user_name); ?>
1167        </p>
1168
1169        <?php
1170        do_action('signup_finished');
1171    }
1172
1173    /**
1174     * Output a "Create a Site" link for users viewing their own profiles.
1175     */
1176    function bp_create_blog_link() {
1177        if (bp_is_my_profile())
1178            echo apply_filters('bp_create_blog_link', '<a href="' . bp_get_root_domain() . '/' . bp_get_blogs_root_slug() . '/create/">' . __('Create a Site', 'buddypress') . '</a>');
1179    }
1180
1181    /**
1182     * Output navigation tabs for a user Blogs page.
1183     *
1184     * Currently unused by BuddyPress.
1185     */
1186    function bp_blogs_blog_tabs() {
1187
1188        // Don't show these tabs on a user's own profile
1189        if (bp_is_my_profile())
1190            return false;
1191        ?>
1192
1193            <ul class="content-header-nav">
1194            <li<?php if (bp_is_current_action('my-blogs') || !bp_current_action()) : ?> class="current"<?php endif; ?>><a href="<?php echo trailingslashit(bp_displayed_user_domain() . bp_get_blogs_slug() . '/my-blogs'); ?>"><?php printf(__("%s's Sites", 'buddypress'), bp_get_displayed_user_fullname()); ?></a></li>
1195            <li<?php if (bp_is_current_action('recent-posts')) : ?> class="current"<?php endif; ?>><a href="<?php echo trailingslashit(bp_displayed_user_domain() . bp_get_blogs_slug() . '/recent-posts'); ?>"><?php printf(__("%s's Recent Posts", 'buddypress'), bp_get_displayed_user_fullname()); ?></a></li>
1196            <li<?php if (bp_is_current_action('recent-comments')) : ?> class="current"<?php endif; ?>><a href="<?php echo trailingslashit(bp_displayed_user_domain() . bp_get_blogs_slug() . '/recent-comments'); ?>"><?php printf(__("%s's Recent Comments", 'buddypress'), bp_get_displayed_user_fullname()); ?></a></li>
1197        </ul>
1198
1199        <?php
1200        do_action('bp_blogs_blog_tabs');
1201    }
1202
1203    /**
1204     * Output the blog directory search form.
1205     */
1206    function bp_directory_blogs_search_form() {
1207        $default_search_value = bp_get_search_default_text();
1208        $search_value = !empty($_REQUEST['s']) ? stripslashes($_REQUEST['s'
1209                ]) : $default_search_value;
1210
1211        $search_form_html = '<form action="" method="get" id="search-blogs-form">
1212                <label><input type="text" name="s" id="blogs_search" placeholder="' . esc_attr($search_value) . '" /></label>
1213                <input type="submit" id="blogs_search_submit" name="blogs_search_submit" value="' . __('Search', 'buddypress') . '" />
1214        </form>';
1215
1216    echo apply_filters('bp_directory_blogs_search_form', $search_form_html);
1217}
1218
1219/**
1220 * Output button for visiting a blog in a loop.
1221 *
1222 * @see bp_get_blogs_visit_blog_button() for description of arguments.
1223 *
1224 * @param array $args See {@link bp_get_blogs_visit_blog_button()}.
1225 */
1226function bp_blogs_visit_blog_button($args = '') {
1227    echo bp_get_blogs_visit_blog_button($args);
1228}
1229
1230/**
1231 * Return button for visiting a blog in a loop.
1232 *
1233 * @see BP_Button for a complete description of arguments and return
1234 *      value.
1235 *
1236 * @param array $args {
1237 *     Arguments are listed below, with their default values. For a
1238 *     complete description of arguments, see {@link BP_Button}.
1239 *     @type string $id Default: 'visit_blog'.
1240 *     @type string $component Default: 'blogs'.
1241 *     @type bool $must_be_logged_in Default: false.
1242 *     @type bool $block_self Default: false.
1243 *     @type string $wrapper_class Default: 'blog-button visit'.
1244 *     @type string $link_href Permalink of the current blog in the loop.
1245 *     @type string $link_class Default: 'blog-button visit'.
1246 *     @type string $link_text Default: 'Visit Site'.
1247 *     @type string $link_title Default: 'Visit Site'.
1248 * }
1249 * @return string The HTML for the Visit button.
1250 */
1251function bp_get_blogs_visit_blog_button($args = '') {
1252    $defaults = array(
1253        'id' => 'visit_blog',
1254        'component' => 'blogs',
1255        'must_be_logged_in' => false,
1256        'block_self' => false,
1257        'wrapper_class' => 'blog-button visit',
1258        'link_href' => bp_get_blog_permalink(),
1259        'link_class' => 'blog-button visit',
1260        'link_text' => __('Visit Site', 'buddypress'),
1261        'link_title' => __('Visit Site', 'buddypress'),
1262    );
1263
1264    $button = wp_parse_args($args, $defaults);
1265
1266    // Filter and return the HTML button
1267    return bp_get_button(apply_filters('bp_get_blogs_visit_blog_button', $button));
1268}