[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-blogs/classes/ -> class-bp-blogs-component.php (source)

   1  <?php
   2  /**
   3   * BuddyPress Blogs Loader
   4   *
   5   * The blogs component tracks posts and comments to member activity streams,
   6   * shows blogs the member can post to in their profiles, and caches useful
   7   * information from those blogs to make querying blogs in bulk more performant.
   8   *
   9   * @package BuddyPress
  10   * @subpackage BlogsCore
  11   * @since 1.5.0
  12   */
  13  
  14  // Exit if accessed directly.
  15  defined( 'ABSPATH' ) || exit;
  16  
  17  /**
  18   * Creates our Blogs component.
  19   */
  20  class BP_Blogs_Component extends BP_Component {
  21  
  22      /**
  23       * Start the blogs component creation process.
  24       *
  25       * @since 1.5.0
  26       */
  27  	public function __construct() {
  28          parent::start(
  29              'blogs',
  30              __( 'Site Directory', 'buddypress' ),
  31              buddypress()->plugin_dir,
  32              array(
  33                  'adminbar_myaccount_order' => 30,
  34                  'search_query_arg' => 'sites_search',
  35                  'features' => array( 'site-icon' )
  36              )
  37          );
  38      }
  39  
  40      /**
  41       * Set up global settings for the blogs component.
  42       *
  43       * The BP_BLOGS_SLUG constant is deprecated, and only used here for
  44       * backwards compatibility.
  45       *
  46       * @since 1.5.0
  47       *
  48       * @see BP_Component::setup_globals() for description of parameters.
  49       *
  50       * @param array $args See {@link BP_Component::setup_globals()}.
  51       */
  52  	public function setup_globals( $args = array() ) {
  53          $bp = buddypress();
  54  
  55          if ( ! defined( 'BP_BLOGS_SLUG' ) ) {
  56              define ( 'BP_BLOGS_SLUG', $this->id );
  57          }
  58  
  59          // Global tables for messaging component.
  60          $global_tables = array(
  61              'table_name'          => $bp->table_prefix . 'bp_user_blogs',
  62              'table_name_blogmeta' => $bp->table_prefix . 'bp_user_blogs_blogmeta',
  63          );
  64  
  65          $meta_tables = array(
  66              'bp_blog' => $bp->table_prefix . 'bp_user_blogs_blogmeta',
  67          );
  68  
  69          // Fetch the default directory title.
  70          $default_directory_titles = bp_core_get_directory_page_default_titles();
  71          $default_directory_title  = $default_directory_titles[$this->id];
  72  
  73          // All globals for blogs component.
  74          $args = array(
  75              'slug'                  => BP_BLOGS_SLUG,
  76              'root_slug'             => isset( $bp->pages->blogs->slug ) ? $bp->pages->blogs->slug : BP_BLOGS_SLUG,
  77              'has_directory'         => is_multisite(), // Non-multisite installs don't need a top-level Sites directory, since there's only one site.
  78              'directory_title'       => isset( $bp->pages->blogs->title ) ? $bp->pages->blogs->title : $default_directory_title,
  79              'notification_callback' => 'bp_blogs_format_notifications',
  80              'search_string'         => __( 'Search sites...', 'buddypress' ),
  81              'autocomplete_all'      => defined( 'BP_MESSAGES_AUTOCOMPLETE_ALL' ),
  82              'global_tables'         => $global_tables,
  83              'meta_tables'           => $meta_tables,
  84              'block_globals'         => array(
  85                  'bp/recent-posts' => array(
  86                      'widget_classnames' => array( 'widget_bp_blogs_widget', 'buddypress' ),
  87                  ),
  88              ),
  89          );
  90  
  91          // Setup the globals.
  92          parent::setup_globals( $args );
  93  
  94          /**
  95           * Filters if a blog is public.
  96           *
  97           * In case the config is not multisite, the blog_public option is ignored.
  98           *
  99           * @since 2.3.0
 100           *
 101           * @param int $value Whether or not the blog is public.
 102           */
 103          if ( 0 !== apply_filters( 'bp_is_blog_public', (int) get_option( 'blog_public' ) ) || ! is_multisite() ) {
 104  
 105              /**
 106               * Filters the post types to track for the Blogs component.
 107               *
 108               * @since 1.5.0
 109               * @deprecated 2.3.0
 110               *
 111               * @param array $value Array of post types to track.
 112               */
 113              $post_types = apply_filters( 'bp_blogs_record_post_post_types', array( 'post' ) );
 114  
 115              foreach ( $post_types as $post_type ) {
 116                  add_post_type_support( $post_type, 'buddypress-activity' );
 117              }
 118          }
 119      }
 120  
 121      /**
 122       * Include bp-blogs files.
 123       *
 124       * @see BP_Component::includes() for description of parameters.
 125       *
 126       * @param array $includes See {@link BP_Component::includes()}.
 127       */
 128  	public function includes( $includes = array() ) {
 129  
 130          // Files to include.
 131          $includes = array(
 132              'cache',
 133              'template',
 134              'filters',
 135              'functions',
 136          );
 137  
 138          if ( bp_is_active( 'activity' ) ) {
 139              $includes[] = 'activity';
 140  
 141              if ( is_multisite() ) {
 142                  $includes[] = 'widgets';
 143                  $includes[] = 'blocks';
 144              }
 145          }
 146  
 147          // Include the files.
 148          parent::includes( $includes );
 149      }
 150  
 151      /**
 152       * Late includes method.
 153       *
 154       * Only load up certain code when on specific pages.
 155       *
 156       * @since 3.0.0
 157       */
 158  	public function late_includes() {
 159          // Bail if PHPUnit is running.
 160          if ( defined( 'BP_TESTS_DIR' ) ) {
 161              return;
 162          }
 163  
 164          // Bail if not on a blogs page or not multisite.
 165          if ( ! bp_is_blogs_component() || ! is_multisite() ) {
 166              return;
 167          }
 168  
 169          // Actions.
 170          if ( isset( $_GET['random-blog'] ) ) {
 171              require $this->path . 'bp-blogs/actions/random.php';
 172          }
 173  
 174          // Screens.
 175          if ( bp_is_user() ) {
 176              require $this->path . 'bp-blogs/screens/my-blogs.php';
 177          } else {
 178              if ( bp_is_blogs_directory() ) {
 179                  require $this->path . 'bp-blogs/screens/directory.php';
 180              }
 181  
 182              if ( is_user_logged_in() && bp_is_current_action( 'create' ) ) {
 183                  require $this->path . 'bp-blogs/screens/create.php';
 184              }
 185  
 186              // Theme compatibility.
 187              new BP_Blogs_Theme_Compat();
 188          }
 189      }
 190  
 191      /**
 192       * Set up component navigation for bp-blogs.
 193       *
 194       * @see BP_Component::setup_nav() for a description of arguments.
 195       *
 196       * @param array $main_nav Optional. See BP_Component::setup_nav() for
 197       *                        description.
 198       * @param array $sub_nav  Optional. See BP_Component::setup_nav() for
 199       *                        description.
 200       */
 201  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 202  
 203          /**
 204           * Blog/post/comment menus should not appear on single WordPress setups.
 205           * Although comments and posts made by users will still show on their
 206           * activity stream.
 207           */
 208          if ( ! is_multisite() ) {
 209              return false;
 210          }
 211  
 212          // Determine user to use.
 213          if ( bp_displayed_user_domain() ) {
 214              $user_domain = bp_displayed_user_domain();
 215          } elseif ( bp_loggedin_user_domain() ) {
 216              $user_domain = bp_loggedin_user_domain();
 217          } else {
 218              return;
 219          }
 220  
 221          $slug       = bp_get_blogs_slug();
 222          $parent_url = trailingslashit( $user_domain . $slug );
 223  
 224          // Add 'Sites' to the main navigation.
 225          $count    = (int) bp_get_total_blog_count_for_user();
 226          $class    = ( 0 === $count ) ? 'no-count' : 'count';
 227          $nav_text = sprintf(
 228              /* translators: %s: Site count for the current user */
 229              __( 'Sites %s', 'buddypress' ),
 230              sprintf(
 231                  '<span class="%s">%s</span>',
 232                  esc_attr( $class ),
 233                  esc_html( $count )
 234              )
 235          );
 236          $main_nav = array(
 237              'name'                => $nav_text,
 238              'slug'                => $slug,
 239              'position'            => 30,
 240              'screen_function'     => 'bp_blogs_screen_my_blogs',
 241              'default_subnav_slug' => 'my-sites',
 242              'item_css_id'         => $this->id
 243          );
 244  
 245          $sub_nav[] = array(
 246              'name'            => __( 'My Sites', 'buddypress' ),
 247              'slug'            => 'my-sites',
 248              'parent_url'      => $parent_url,
 249              'parent_slug'     => $slug,
 250              'screen_function' => 'bp_blogs_screen_my_blogs',
 251              'position'        => 10
 252          );
 253  
 254          // Setup navigation.
 255          parent::setup_nav( $main_nav, $sub_nav );
 256      }
 257  
 258      /**
 259       * Set up bp-blogs integration with the WordPress admin bar.
 260       *
 261       * @since 1.5.0
 262       *
 263       * @see BP_Component::setup_admin_bar() for a description of arguments.
 264       *
 265       * @param array $wp_admin_nav See BP_Component::setup_admin_bar()
 266       *                            for description.
 267       * @return bool
 268       */
 269  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 270  
 271          /**
 272           * Site/post/comment menus should not appear on single WordPress setups.
 273           *
 274           * Comments and posts made by users will still show in their activity.
 275           */
 276          if ( ! is_multisite() ) {
 277              return false;
 278          }
 279  
 280          // Menus for logged in user.
 281          if ( is_user_logged_in() ) {
 282  
 283              // Setup the logged in user variables.
 284              $blogs_link = trailingslashit( bp_loggedin_user_domain() . bp_get_blogs_slug() );
 285  
 286              // Add the "Sites" sub menu.
 287              $wp_admin_nav[] = array(
 288                  'parent' => buddypress()->my_account_menu_id,
 289                  'id'     => 'my-account-' . $this->id,
 290                  'title'  => __( 'Sites', 'buddypress' ),
 291                  'href'   => $blogs_link
 292              );
 293  
 294              // My Sites.
 295              $wp_admin_nav[] = array(
 296                  'parent'   => 'my-account-' . $this->id,
 297                  'id'       => 'my-account-' . $this->id . '-my-sites',
 298                  'title'    => __( 'My Sites', 'buddypress' ),
 299                  'href'     => trailingslashit( $blogs_link . 'my-sites' ),
 300                  'position' => 10
 301              );
 302  
 303              // Create a Site.
 304              if ( bp_blog_signup_enabled() ) {
 305                  $wp_admin_nav[] = array(
 306                      'parent'   => 'my-account-' . $this->id,
 307                      'id'       => 'my-account-' . $this->id . '-create',
 308                      'title'    => __( 'Create a Site', 'buddypress' ),
 309                      'href'     => trailingslashit( bp_get_blogs_directory_permalink() . 'create' ),
 310                      'position' => 99
 311                  );
 312              }
 313          }
 314  
 315          parent::setup_admin_bar( $wp_admin_nav );
 316      }
 317  
 318      /**
 319       * Set up the title for pages and <title>.
 320       */
 321  	public function setup_title() {
 322  
 323          // Set up the component options navigation for Site.
 324          if ( bp_is_blogs_component() ) {
 325              $bp = buddypress();
 326  
 327              if ( bp_is_my_profile() ) {
 328                  if ( bp_is_active( 'xprofile' ) ) {
 329                      $bp->bp_options_title = __( 'My Sites', 'buddypress' );
 330                  }
 331  
 332              // If we are not viewing the logged in user, set up the current
 333              // users avatar and name.
 334              } else {
 335                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 336                      'item_id' => bp_displayed_user_id(),
 337                      'type'    => 'thumb',
 338                      'alt'     => sprintf(
 339                          /* translators: %s: member name */
 340                          __( 'Profile picture of %s', 'buddypress' ),
 341                          bp_get_displayed_user_fullname()
 342                      ),
 343                  ) );
 344                  $bp->bp_options_title = bp_get_displayed_user_fullname();
 345              }
 346          }
 347  
 348          parent::setup_title();
 349      }
 350  
 351      /**
 352       * Setup cache groups
 353       *
 354       * @since 2.2.0
 355       */
 356  	public function setup_cache_groups() {
 357  
 358          // Global groups.
 359          wp_cache_add_global_groups( array(
 360              'bp_blog_meta'
 361          ) );
 362  
 363          parent::setup_cache_groups();
 364      }
 365  
 366      /**
 367       * Init the BP REST API.
 368       *
 369       * @since 6.0.0
 370       *
 371       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 372       *                           description.
 373       */
 374  	public function rest_api_init( $controllers = array() ) {
 375          if ( is_multisite() ) {
 376              $controllers = array(
 377                  'BP_REST_Blogs_Endpoint',
 378              );
 379  
 380              // Support to Blog Avatar.
 381              if ( bp_is_active( 'blogs', 'site-icon' ) ) {
 382                  $controllers[] = 'BP_REST_Attachments_Blog_Avatar_Endpoint';
 383              }
 384          }
 385  
 386          parent::rest_api_init( $controllers );
 387      }
 388  
 389      /**
 390       * Register the BP Blogs Blocks.
 391       *
 392       * @since 9.0.0
 393       *
 394       * @param array $blocks Optional. See BP_Component::blocks_init() for
 395       *                      description.
 396       */
 397  	public function blocks_init( $blocks = array() ) {
 398          $blocks = array();
 399  
 400          if ( is_multisite() && bp_is_active( 'activity' ) ) {
 401              $blocks['bp/recent-posts'] = array(
 402                  'name'               => 'bp/recent-posts',
 403                  'editor_script'      => 'bp-recent-posts-block',
 404                  'editor_script_url'  => plugins_url( 'js/blocks/recent-posts.js', dirname( __FILE__ ) ),
 405                  'editor_script_deps' => array(
 406                      'wp-blocks',
 407                      'wp-element',
 408                      'wp-components',
 409                      'wp-i18n',
 410                      'wp-block-editor',
 411                      'wp-server-side-render',
 412                  ),
 413                  'style'              => 'bp-recent-posts-block',
 414                  'style_url'          => plugins_url( 'css/blocks/recent-posts.css', dirname( __FILE__ ) ),
 415                  'attributes'         => array(
 416                      'title'     => array(
 417                          'type'    => 'string',
 418                          'default' => __( 'Recent Networkwide Posts', 'buddypress' ),
 419                      ),
 420                      'maxPosts'  => array(
 421                          'type'    => 'number',
 422                          'default' => 10,
 423                      ),
 424                      'linkTitle' => array(
 425                          'type'    => 'boolean',
 426                          'default' => false,
 427                      ),
 428                  ),
 429                  'render_callback'    => 'bp_blogs_render_recent_posts_block',
 430              );
 431          }
 432  
 433          parent::blocks_init( $blocks );
 434      }
 435  
 436      /**
 437       * Add the Sites directory states.
 438       *
 439       * @since 10.0.0
 440       *
 441       * @param array   $states Optional. See BP_Component::admin_directory_states() for description.
 442       * @param WP_Post $post   Optional. See BP_Component::admin_directory_states() for description.
 443       * @return array          See BP_Component::admin_directory_states() for description.
 444       */
 445  	public function admin_directory_states( $states = array(), $post = null ) {
 446          $bp = buddypress();
 447  
 448          if ( isset( $bp->pages->blogs->id ) && (int) $bp->pages->blogs->id === (int) $post->ID ) {
 449              $states['page_for_sites_directory'] = _x( 'BP Sites Page', 'page label', 'buddypress' );
 450          }
 451  
 452          return parent::admin_directory_states( $states, $post );
 453      }
 454  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1