BP_Blogs_Component

Creates our Blogs component.

Description

Source

File: bp-blogs/classes/class-bp-blogs-component.php

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
class BP_Blogs_Component extends BP_Component {
 
    /**
     * Start the blogs component creation process.
     *
     * @since BuddyPress 1.5.0
     */
    public function __construct() {
        parent::start(
            'blogs',
            __( 'Site Directory', 'buddyboss' ),
            buddypress()->plugin_dir,
            array(
                'adminbar_myaccount_order' => 30,
                'search_query_arg' => 'sites_search',
                'features' => array( 'site-icon' )
            )
        );
    }
 
    /**
     * Set up global settings for the blogs component.
     *
     * The BP_BLOGS_SLUG constant is deprecated, and only used here for
     * backwards compatibility.
     *
     * @since BuddyPress 1.5.0
     *
     * @see BP_Component::setup_globals() for description of parameters.
     *
     * @param array $args See {@link BP_Component::setup_globals()}.
     */
    public function setup_globals( $args = array() ) {
        $bp = buddypress();
 
        if ( ! defined( 'BP_BLOGS_SLUG' ) ) {
            define ( 'BP_BLOGS_SLUG', $this->id );
        }
 
        // Global tables for messaging component.
        $global_tables = array(
            'table_name'          => $bp->table_prefix . 'bp_user_blogs',
            'table_name_blogmeta' => $bp->table_prefix . 'bp_user_blogs_blogmeta',
        );
 
        $meta_tables = array(
            'blog' => $bp->table_prefix . 'bp_user_blogs_blogmeta',
        );
 
        // Fetch the default directory title.
        $default_directory_titles = bp_core_get_directory_page_default_titles();
        $default_directory_title  = $default_directory_titles[$this->id];
 
        // All globals for blogs component.
        $args = array(
            'slug'                  => BP_BLOGS_SLUG,
            'root_slug'             => isset( $bp->pages->blogs->slug ) ? $bp->pages->blogs->slug : BP_BLOGS_SLUG,
            'has_directory'         => is_multisite(), // Non-multisite installs don't need a top-level Sites directory, since there's only one site.
            'directory_title'       => isset( $bp->pages->blogs->title ) ? $bp->pages->blogs->title : $default_directory_title,
            'notification_callback' => 'bp_blogs_format_notifications',
            'search_string'         => __( 'Search sites…', 'buddyboss' ),
            'autocomplete_all'      => defined( 'BP_MESSAGES_AUTOCOMPLETE_ALL' ),
            'global_tables'         => $global_tables,
            'meta_tables'           => $meta_tables,
        );
 
        // Setup the globals.
        parent::setup_globals( $args );
 
        /**
         * Filters if a blog is public.
         *
         * In case the config is not multisite, the blog_public option is ignored.
         *
         * @since BuddyPress 2.3.0
         *
         * @param int $value Whether or not the blog is public.
         */
        if ( 0 !== apply_filters( 'bp_is_blog_public', (int) get_option( 'blog_public' ) ) || ! is_multisite() ) {
 
            /**
             * Filters the post types to track for the Blogs component.
             *
             * @since BuddyPress 1.5.0
             * @deprecated 2.3.0
             *
             * @param array $value Array of post types to track.
             */
            $post_types = apply_filters( 'bp_blogs_record_post_post_types', bp_core_get_active_custom_post_type_feed() );
 
            foreach ( $post_types as $post_type ) {
                add_post_type_support( $post_type, 'buddypress-activity' );
            }
        }
    }
 
    /**
     * Include bp-blogs files.
     *
     * @see BP_Component::includes() for description of parameters.
     *
     * @param array $includes See {@link BP_Component::includes()}.
     */
    public function includes( $includes = array() ) {
 
        // Files to include.
        $includes = array(
            'cache',
            'template',
            'filters',
            'functions',
        );
 
        if ( bp_is_active( 'activity' ) ) {
            $includes[] = 'activity';
        }
 
        if ( is_multisite() ) {
            $includes[] = 'widgets';
        }
 
        // Include the files.
        parent::includes( $includes );
    }
 
    /**
     * Late includes method.
     *
     * Only load up certain code when on specific pages.
     *
     * @since BuddyPress 3.0.0
     */
    public function late_includes() {
        // Bail if PHPUnit is running.
        if ( defined( 'BP_TESTS_DIR' ) ) {
            return;
        }
 
        // Bail if not on a blogs page or not multisite.
        if ( ! bp_is_blogs_component() || ! is_multisite() ) {
            return;
        }
 
        // Actions.
        if ( isset( $_GET['random-blog'] ) ) {
            require $this->path . 'bp-blogs/actions/random.php';
        }
 
        // Screens.
        if ( bp_is_user() ) {
            require $this->path . 'bp-blogs/screens/my-blogs.php';
        } else {
            if ( bp_is_blogs_directory() ) {
                require $this->path . 'bp-blogs/screens/directory.php';
            }
 
            if ( is_user_logged_in() && bp_is_current_action( 'create' ) ) {
                require $this->path . 'bp-blogs/screens/create.php';
            }
 
            // Theme compatibility.
            new BP_Blogs_Theme_Compat();
        }
    }
 
    /**
     * Set up component navigation for bp-blogs.
     *
     * @see BP_Component::setup_nav() for a description of arguments.
     *
     * @param array $main_nav Optional. See BP_Component::setup_nav() for
     *                        description.
     * @param array $sub_nav  Optional. See BP_Component::setup_nav() for
     *                        description.
     */
    public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 
        /**
         * Blog/post/comment menus should not appear on single WordPress setups.
         * Although comments and posts made by users will still show on their
         * activity feed.
         */
        if ( ! is_multisite() ) {
            return false;
        }
 
        // Determine user to use.
        if ( bp_displayed_user_domain() ) {
            $user_domain = bp_displayed_user_domain();
        } elseif ( bp_loggedin_user_domain() ) {
            $user_domain = bp_loggedin_user_domain();
        } else {
            return;
        }
 
        $slug       = bp_get_blogs_slug();
        $parent_url = trailingslashit( $user_domain . $slug );
 
        // Add 'Sites' to the main navigation.
        $count    = (int) bp_get_total_blog_count_for_user();
        $class    = ( 0 === $count ) ? 'no-count' : 'count';
        $nav_text = sprintf(
            /* translators: %s: Site count for the current user */
            __( 'Sites %s', 'buddyboss' ),
            sprintf(
                '<span class="%s">%s</span>',
                esc_attr( $class ),
                bp_core_number_format( $count )
            )
        );
        $main_nav = array(
            'name'                => $nav_text,
            'slug'                => $slug,
            'position'            => 30,
            'screen_function'     => 'bp_blogs_screen_my_blogs',
            'default_subnav_slug' => 'my-sites',
            'item_css_id'         => $this->id
        );
 
        $sub_nav[] = array(
            'name'            => __( 'My Sites', 'buddyboss' ),
            'slug'            => 'my-sites',
            'parent_url'      => $parent_url,
            'parent_slug'     => $slug,
            'screen_function' => 'bp_blogs_screen_my_blogs',
            'position'        => 10
        );
 
        // Setup navigation.
        parent::setup_nav( $main_nav, $sub_nav );
    }
 
    /**
     * Set up bp-blogs integration with the WordPress admin bar.
     *
     * @since BuddyPress 1.5.0
     *
     * @see BP_Component::setup_admin_bar() for a description of arguments.
     *
     * @param array $wp_admin_nav See BP_Component::setup_admin_bar()
     *                            for description.
     * @return bool
     */
    public function setup_admin_bar( $wp_admin_nav = array() ) {
 
        /**
         * Site/post/comment menus should not appear on single WordPress setups.
         *
         * Comments and posts made by users will still show in their activity.
         */
        if ( ! is_multisite() ) {
            return false;
        }
 
        // Menus for logged in user.
        if ( is_user_logged_in() ) {
 
            // Setup the logged in user variables.
            $blogs_link = trailingslashit( bp_loggedin_user_domain() . bp_get_blogs_slug() );
 
            // Add the "Sites" sub menu.
            $wp_admin_nav[] = array(
                'parent' => buddypress()->my_account_menu_id,
                'id'     => 'my-account-' . $this->id,
                'title'  => __( 'Sites', 'buddyboss' ),
                'href'   => $blogs_link
            );
 
            // My Sites.
            $wp_admin_nav[] = array(
                'parent'   => 'my-account-' . $this->id,
                'id'       => 'my-account-' . $this->id . '-my-sites',
                'title'    => __( 'My Sites', 'buddyboss' ),
                'href'     => $blogs_link,
                'position' => 10
            );
 
            // Create a Site.
            if ( bp_blog_signup_enabled() ) {
                $wp_admin_nav[] = array(
                    'parent'   => 'my-account-' . $this->id,
                    'id'       => 'my-account-' . $this->id . '-create',
                    'title'    => __( 'Create a Site', 'buddyboss' ),
                    'href'     => trailingslashit( bp_get_blogs_directory_permalink() . 'create' ),
                    'position' => 99
                );
            }
        }
 
        parent::setup_admin_bar( $wp_admin_nav );
    }
 
    /**
     * Set up the title for pages and <title>.
     */
    public function setup_title() {
 
        // Set up the component options navigation for Site.
        if ( bp_is_blogs_component() ) {
            $bp = buddypress();
 
            if ( bp_is_my_profile() ) {
                if ( bp_is_active( 'xprofile' ) ) {
                    $bp->bp_options_title = __( 'My Sites', 'buddyboss' );
                }
 
            // If we are not viewing the logged in user, set up the current
            // users avatar and name.
            } else {
                $bp->bp_options_avatar = bp_core_fetch_avatar( array(
                    'item_id' => bp_displayed_user_id(),
                    'type'    => 'thumb',
                    'alt'     => sprintf( __( 'Profile photo of %s', 'buddyboss' ), bp_get_displayed_user_fullname() )
                ) );
                $bp->bp_options_title = bp_get_displayed_user_fullname();
            }
        }
 
        parent::setup_title();
    }
 
    /**
     * Setup cache groups
     *
     * @since BuddyPress 2.2.0
     */
    public function setup_cache_groups() {
 
        // Global groups.
        wp_cache_add_global_groups( array(
            'blog_meta'
        ) );
 
        parent::setup_cache_groups();
    }
}

Methods

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.