[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-templates/bp-nouveau/ -> buddypress-functions.php (source)

   1  <?php
   2  /**
   3   * Functions of BuddyPress's "Nouveau" template pack.
   4   *
   5   * @since 3.0.0
   6   * @version 3.1.0
   7   *
   8   * @buddypress-template-pack {
   9   *   Template Pack ID:       nouveau
  10   *   Template Pack Name:     BP Nouveau
  11   *   Version:                1.0.0
  12   *   WP required version:    4.5.0
  13   *   BP required version:    3.0.0
  14   *   Description:            A new template pack for BuddyPress!
  15   *   Text Domain:            bp-nouveau
  16   *   Domain Path:            /languages/
  17   *   Author:                 The BuddyPress community
  18   *   Template Pack Supports: activity, blogs, friends, groups, messages, notifications, settings, xprofile
  19   * }}
  20   */
  21  
  22  // Exit if accessed directly.
  23  defined( 'ABSPATH' ) || exit;
  24  
  25  /** Theme Setup ***************************************************************/
  26  
  27  /**
  28   * Loads BuddyPress Nouveau Template pack functionality.
  29   *
  30   * See @link BP_Theme_Compat() for more.
  31   *
  32   * @since 3.0.0
  33   */
  34  class BP_Nouveau extends BP_Theme_Compat {
  35      /**
  36       * Instance of this class.
  37       */
  38      protected static $instance = null;
  39  
  40      /**
  41       * Return the instance of this class.
  42       *
  43       * @since 3.0.0
  44       */
  45  	public static function get_instance() {
  46          if ( null === self::$instance ) {
  47              self::$instance = new self;
  48          }
  49  
  50          return self::$instance;
  51      }
  52  
  53      /**
  54       * The BP Nouveau constructor.
  55       *
  56       * @since 3.0.0
  57       */
  58  	public function __construct() {
  59          parent::start();
  60  
  61          $this->includes();
  62          $this->setup_support();
  63      }
  64  
  65      /**
  66       * BP Nouveau global variables.
  67       *
  68       * @since 3.0.0
  69       */
  70  	protected function setup_globals() {
  71          $bp = buddypress();
  72  
  73          foreach ( $bp->theme_compat->packages['nouveau'] as $property => $value ) {
  74              $this->{$property} = $value;
  75          }
  76  
  77          $this->includes_dir  = trailingslashit( $this->dir ) . 'includes/';
  78          $this->directory_nav = new BP_Core_Nav();
  79      }
  80  
  81      /**
  82       * Includes!
  83       *
  84       * @since 3.0.0
  85       */
  86  	protected function includes() {
  87          require $this->includes_dir . 'functions.php';
  88          require $this->includes_dir . 'classes.php';
  89          require $this->includes_dir . 'template-tags.php';
  90  
  91          // Test suite requires the AJAX functions early.
  92          if ( function_exists( 'tests_add_filter' ) ) {
  93              require $this->includes_dir . 'ajax.php';
  94  
  95          // Load AJAX code only on AJAX requests.
  96          } else {
  97              add_action( 'admin_init', function() {
  98                  if ( defined( 'DOING_AJAX' ) && true === DOING_AJAX ) {
  99                      require bp_nouveau()->includes_dir . 'ajax.php';
 100                  }
 101              }, 0 );
 102          }
 103  
 104          add_action( 'bp_customize_register', function() {
 105              if ( bp_is_root_blog() && current_user_can( 'customize' ) ) {
 106                  require bp_nouveau()->includes_dir . 'customizer.php';
 107              }
 108          }, 0 );
 109  
 110          foreach ( bp_core_get_packaged_component_ids() as $component ) {
 111              $component_loader = trailingslashit( $this->includes_dir ) . $component . '/loader.php';
 112  
 113              if ( ! bp_is_active( $component ) || ! file_exists( $component_loader ) ) {
 114                  continue;
 115              }
 116  
 117              require( $component_loader );
 118          }
 119  
 120          /**
 121           * Fires after all of the BuddyPress Nouveau includes have been loaded. Passed by reference.
 122           *
 123           * @since 3.0.0
 124           *
 125           * @param BP_Nouveau $value Current BP_Nouveau instance.
 126           */
 127          do_action_ref_array( 'bp_nouveau_includes', array( &$this ) );
 128      }
 129  
 130      /**
 131       * Setup the Template Pack features support.
 132       *
 133       * @since 3.0.0
 134       */
 135  	protected function setup_support() {
 136          $width         = 1300;
 137          $top_offset    = 150;
 138  
 139          /** This filter is documented in bp-core/bp-core-avatars.php. */
 140          $avatar_height = apply_filters( 'bp_core_avatar_full_height', $top_offset );
 141  
 142          if ( $avatar_height > $top_offset ) {
 143              $top_offset = $avatar_height;
 144          }
 145  
 146          bp_set_theme_compat_feature( $this->id, array(
 147              'name'     => 'cover_image',
 148              'settings' => array(
 149                  'components'   => array( 'xprofile', 'groups' ),
 150                  'width'        => $width,
 151                  'height'       => $top_offset + round( $avatar_height / 2 ),
 152                  'callback'     => 'bp_nouveau_theme_cover_image',
 153                  'theme_handle' => 'bp-nouveau',
 154              ),
 155          ) );
 156      }
 157  
 158      /**
 159       * Setup the Template Pack common actions.
 160       *
 161       * @since 3.0.0
 162       */
 163  	protected function setup_actions() {
 164          // Filter BuddyPress template hierarchy and look for page templates.
 165          add_filter( 'bp_get_buddypress_template', array( $this, 'theme_compat_page_templates' ), 10, 1 );
 166  
 167          // Add our "buddypress" div wrapper to theme compat template parts.
 168          add_filter( 'bp_replace_the_content', array( $this, 'theme_compat_wrapper' ), 999 );
 169  
 170          // We need to neutralize the BuddyPress core "bp_core_render_message()" once it has been added.
 171          add_action( 'bp_actions', array( $this, 'neutralize_core_template_notices' ), 6 );
 172  
 173          // Scripts
 174          add_action( 'bp_enqueue_scripts', array( $this, 'register_scripts' ), 2 ); // Register theme JS
 175          remove_action( 'bp_enqueue_scripts', 'bp_core_confirmation_js' );
 176          add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_styles' ) ); // Enqueue theme CSS
 177          add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); // Enqueue theme JS
 178          add_filter( 'bp_enqueue_scripts', array( $this, 'localize_scripts' ) ); // Enqueue theme script localization
 179  
 180          // Body no-js class
 181          add_filter( 'body_class', array( $this, 'add_nojs_body_class' ), 20, 1 );
 182  
 183          // Ajax querystring
 184          add_filter( 'bp_ajax_querystring', 'bp_nouveau_ajax_querystring', 10, 2 );
 185  
 186          // Register directory nav items
 187          add_action( 'bp_screens', array( $this, 'setup_directory_nav' ), 15 );
 188  
 189          // Register the Default front pages Dynamic Sidebars
 190          add_action( 'widgets_init', 'bp_nouveau_register_sidebars', 11 );
 191  
 192          // Register the Primary Object nav widget
 193          add_action( 'bp_widgets_init', array( 'BP_Nouveau_Object_Nav_Widget', 'register_widget' ) );
 194  
 195          // Set the BP Uri for the Ajax customizer preview
 196          add_filter( 'bp_uri', array( $this, 'customizer_set_uri' ), 10, 1 );
 197  
 198          /** Override **********************************************************/
 199  
 200          /**
 201           * Fires after all of the BuddyPress theme compat actions have been added.
 202           *
 203           * @since 3.0.0
 204           *
 205           * @param BP_Nouveau $this Current BP_Nouveau instance.
 206           */
 207          do_action_ref_array( 'bp_theme_compat_actions', array( &$this ) );
 208      }
 209  
 210      /**
 211       * Enqueue the template pack css files
 212       *
 213       * @since 3.0.0
 214       */
 215  	public function enqueue_styles() {
 216          $min = bp_core_get_minified_asset_suffix();
 217          $rtl = '';
 218  
 219          if ( is_rtl() ) {
 220              $rtl = '-rtl';
 221          }
 222  
 223          /**
 224           * Filters the BuddyPress Nouveau CSS dependencies.
 225           *
 226           * @since 3.0.0
 227           *
 228           * @param array $value Array of style dependencies. Default Dashicons.
 229           */
 230          $css_dependencies = apply_filters( 'bp_nouveau_css_dependencies', array( 'dashicons' ) );
 231  
 232          /**
 233           * Filters the styles to enqueue for BuddyPress Nouveau.
 234           *
 235           * This filter provides a multidimensional array that will map to arguments used for wp_enqueue_style().
 236           * The primary index should have the stylesheet handle to use, and be assigned an array that has indexes for
 237           * file location, dependencies, and version.
 238           *
 239           * @since 3.0.0
 240           *
 241           * @param array $value Array of styles to enqueue.
 242           */
 243          $styles = apply_filters( 'bp_nouveau_enqueue_styles', array(
 244              'bp-nouveau' => array(
 245                  'file' => 'css/buddypress%1$s%2$s.css', 'dependencies' => $css_dependencies, 'version' => $this->version,
 246              ),
 247          ) );
 248  
 249          if ( $styles ) {
 250  
 251              foreach ( $styles as $handle => $style ) {
 252                  if ( ! isset( $style['file'] ) ) {
 253                      continue;
 254                  }
 255  
 256                  $file = sprintf( $style['file'], $rtl, $min );
 257  
 258                  // Locate the asset if needed.
 259                  if ( false === strpos( $style['file'], '://' ) ) {
 260                      $asset = bp_locate_template_asset( $file );
 261  
 262                      if ( empty( $asset['uri'] ) || false === strpos( $asset['uri'], '://' ) ) {
 263                          continue;
 264                      }
 265  
 266                      $file = $asset['uri'];
 267                  }
 268  
 269                  $data = bp_parse_args(
 270                      $style,
 271                      array(
 272                          'dependencies' => array(),
 273                          'version'      => $this->version,
 274                          'type'         => 'screen',
 275                      ),
 276                      'nouveau_enqueue_styles'
 277                  );
 278  
 279                  wp_enqueue_style( $handle, $file, $data['dependencies'], $data['version'], $data['type'] );
 280  
 281                  if ( $min ) {
 282                      wp_style_add_data( $handle, 'suffix', $min );
 283                  }
 284              }
 285          }
 286      }
 287  
 288      /**
 289       * Register Template Pack JavaScript files
 290       *
 291       * @since 3.0.0
 292       */
 293  	public function register_scripts() {
 294          $min          = bp_core_get_minified_asset_suffix();
 295          $dependencies = bp_core_get_js_dependencies();
 296          $bp_confirm   = array_search( 'bp-confirm', $dependencies );
 297  
 298          unset( $dependencies[ $bp_confirm ] );
 299  
 300          /**
 301           * Filters the scripts to enqueue for BuddyPress Nouveau.
 302           *
 303           * This filter provides a multidimensional array that will map to arguments used for wp_register_script().
 304           * The primary index should have the script handle to use, and be assigned an array that has indexes for
 305           * file location, dependencies, version and if it should load in the footer or not.
 306           *
 307           * @since 3.0.0
 308           *
 309           * @param array $value Array of scripts to register.
 310           */
 311          $scripts = apply_filters( 'bp_nouveau_register_scripts', array(
 312              'bp-nouveau' => array(
 313                  'file'         => 'js/buddypress-nouveau%s.js',
 314                  'dependencies' => $dependencies,
 315                  'version'      => $this->version,
 316                  'footer'       => true,
 317              ),
 318          ) );
 319  
 320          // Bail if no scripts
 321          if ( empty( $scripts ) ) {
 322              return;
 323          }
 324  
 325          // Add The password verify if needed.
 326          if ( bp_is_active( 'settings' ) || bp_get_signup_allowed() ) {
 327              /**
 328               * BP Nouveau is now directly using the `wp-admin/js/user-profile.js` script.
 329               *
 330               * Setting the user password is now more consistent with how WordPress handles it.
 331               *
 332               * @deprecated 5.0.0
 333               */
 334              $scripts['bp-nouveau-password-verify'] = array(
 335                  'file'         => 'js/password-verify%s.js',
 336                  'dependencies' => array( 'bp-nouveau', 'password-strength-meter' ),
 337                  'footer'       => true,
 338              );
 339          }
 340  
 341          foreach ( $scripts as $handle => $script ) {
 342              if ( ! isset( $script['file'] ) ) {
 343                  continue;
 344              }
 345  
 346              $file = sprintf( $script['file'], $min );
 347  
 348              // Locate the asset if needed.
 349              if ( false === strpos( $script['file'], '://' ) ) {
 350                  $asset = bp_locate_template_asset( $file );
 351  
 352                  if ( empty( $asset['uri'] ) || false === strpos( $asset['uri'], '://' ) ) {
 353                      continue;
 354                  }
 355  
 356                  $file = $asset['uri'];
 357              }
 358  
 359              $data = bp_parse_args(
 360                  $script,
 361                  array(
 362                      'dependencies' => array(),
 363                      'version'      => $this->version,
 364                      'footer'       => false,
 365                  ),
 366                  'nouveau_register_scripts'
 367              );
 368  
 369              wp_register_script( $handle, $file, $data['dependencies'], $data['version'], $data['footer'] );
 370          }
 371      }
 372  
 373      /**
 374       * Enqueue the required JavaScript files
 375       *
 376       * @since 3.0.0
 377       */
 378  	public function enqueue_scripts() {
 379          wp_enqueue_script( 'bp-nouveau' );
 380  
 381          if ( bp_is_register_page() || bp_is_user_settings_general() ) {
 382              wp_enqueue_script( 'user-profile' );
 383          }
 384  
 385          if ( is_singular() && bp_is_blog_page() && get_option( 'thread_comments' ) ) {
 386              wp_enqueue_script( 'comment-reply' );
 387          }
 388  
 389          /**
 390           * Fires after all of the BuddyPress Nouveau scripts have been enqueued.
 391           *
 392           * @since 3.0.0
 393           */
 394          do_action( 'bp_nouveau_enqueue_scripts' );
 395      }
 396  
 397      /**
 398       * Adds the no-js class to the body tag.
 399       *
 400       * This function ensures that the <body> element will have the 'no-js' class by default. If you're
 401       * using JavaScript for some visual functionality in your theme, and you want to provide noscript
 402       * support, apply those styles to body.no-js.
 403       *
 404       * The no-js class is removed by the JavaScript created in buddypress.js.
 405       *
 406       * @since 3.0.0
 407       *
 408       * @param array $classes Array of classes to append to body tag.
 409       *
 410       * @return array $classes
 411       */
 412  	public function add_nojs_body_class( $classes ) {
 413          $classes[] = 'no-js';
 414          return array_unique( $classes );
 415      }
 416  
 417      /**
 418       * Load localizations for topic script.
 419       *
 420       * These localizations require information that may not be loaded even by init.
 421       *
 422       * @since 3.0.0
 423       */
 424  	public function localize_scripts() {
 425          $params = array(
 426              'ajaxurl'             => bp_core_ajax_url(),
 427              'confirm'             => __( 'Are you sure?', 'buddypress' ),
 428              'show_x_comments'     => __( 'Show all %d comments', 'buddypress' ),
 429              'unsaved_changes'     => __( 'Your profile has unsaved changes. If you leave the page, the changes will be lost.', 'buddypress' ),
 430              'object_nav_parent'   => '#buddypress',
 431          );
 432  
 433          // If the Object/Item nav are in the sidebar
 434          if ( bp_nouveau_is_object_nav_in_sidebar() ) {
 435              $params['object_nav_parent'] = '.buddypress_object_nav';
 436          }
 437  
 438          /**
 439           * Filters the supported BuddyPress Nouveau components.
 440           *
 441           * @since 3.0.0
 442           *
 443           * @param array $value Array of supported components.
 444           */
 445          $supported_objects = (array) apply_filters( 'bp_nouveau_supported_components', bp_core_get_packaged_component_ids() );
 446          $object_nonces     = array();
 447  
 448          foreach ( $supported_objects as $key_object => $object ) {
 449              if ( ! bp_is_active( $object ) || 'forums' === $object ) {
 450                  unset( $supported_objects[ $key_object ] );
 451                  continue;
 452              }
 453  
 454              $object_nonces[ $object ] = wp_create_nonce( 'bp_nouveau_' . $object );
 455          }
 456  
 457          // Groups require some additional objects.
 458          if ( bp_is_active( 'groups' ) ) {
 459              $supported_objects = array_merge( $supported_objects, array( 'group_members', 'group_requests' ) );
 460          }
 461  
 462          // Add components & nonces
 463          $params['objects'] = $supported_objects;
 464          $params['nonces']  = $object_nonces;
 465  
 466          // Used to transport the settings inside the Ajax requests
 467          if ( is_customize_preview() ) {
 468              $params['customizer_settings'] = bp_nouveau_get_temporary_setting( 'any' );
 469          }
 470  
 471          /**
 472           * Filters core JavaScript strings for internationalization before AJAX usage.
 473           *
 474           * @since 3.0.0
 475           *
 476           * @param array $params Array of key/value pairs for AJAX usage.
 477           */
 478          wp_localize_script( 'bp-nouveau', 'BP_Nouveau', apply_filters( 'bp_core_get_js_strings', $params ) );
 479      }
 480  
 481      /**
 482       * Filter the default theme compatibility root template hierarchy, and prepend
 483       * a page template to the front if it's set.
 484       *
 485       * @see https://buddypress.trac.wordpress.org/ticket/6065
 486       *
 487       * @since 3.0.0
 488       *
 489       * @param array $templates Array of templates.
 490       *
 491       * @return array
 492       */
 493  	public function theme_compat_page_templates( $templates = array() ) {
 494          /**
 495           * Filters whether or not we are looking at a directory to determine if to return early.
 496           *
 497           * @since 3.0.0
 498           *
 499           * @param bool $value Whether or not we are viewing a directory.
 500           */
 501          if ( true === (bool) apply_filters( 'bp_nouveau_theme_compat_page_templates_directory_only', ! bp_is_directory() ) ) {
 502              return $templates;
 503          }
 504  
 505          // No page ID yet.
 506          $page_id = 0;
 507  
 508          // Get the WordPress Page ID for the current view.
 509          foreach ( (array) buddypress()->pages as $component => $bp_page ) {
 510  
 511              // Handles the majority of components.
 512              if ( bp_is_current_component( $component ) ) {
 513                  $page_id = (int) $bp_page->id;
 514              }
 515  
 516              // Stop if not on a user page.
 517              if ( ! bp_is_user() && ! empty( $page_id ) ) {
 518                  break;
 519              }
 520  
 521              // The Members component requires an explicit check due to overlapping components.
 522              if ( bp_is_user() && ( 'members' === $component ) ) {
 523                  $page_id = (int) $bp_page->id;
 524                  break;
 525              }
 526          }
 527  
 528          // Bail if no directory page set.
 529          if ( 0 === $page_id ) {
 530              return $templates;
 531          }
 532  
 533          // Check for page template.
 534          $page_template = get_page_template_slug( $page_id );
 535  
 536          // Add it to the beginning of the templates array so it takes precedence over the default hierarchy.
 537          if ( ! empty( $page_template ) ) {
 538  
 539              /**
 540               * Check for existence of template before adding it to template
 541               * stack to avoid accidentally including an unintended file.
 542               *
 543               * @see https://buddypress.trac.wordpress.org/ticket/6190
 544               */
 545              if ( '' !== locate_template( $page_template ) ) {
 546                  array_unshift( $templates, $page_template );
 547              }
 548          }
 549  
 550          return $templates;
 551      }
 552  
 553      /**
 554       * Add our special 'buddypress' div wrapper to the theme compat template part.
 555       *
 556       * @since 3.0.0
 557       *
 558       * @see bp_buffer_template_part()
 559       *
 560       * @param string $retval Current template part contents.
 561       *
 562       * @return string
 563       */
 564  	public function theme_compat_wrapper( $retval ) {
 565          if ( false !== strpos( $retval, '<div id="buddypress"' ) ) {
 566              return $retval;
 567          }
 568  
 569          // Add our 'buddypress' div wrapper.
 570          return sprintf(
 571              '<div id="buddypress" class="%1$s">%2$s</div><!-- #buddypress -->%3$s',
 572              esc_attr( bp_nouveau_get_container_classes() ),
 573              $retval,  // Constructed HTML.
 574              "\n"
 575          );
 576      }
 577  
 578      /**
 579       * Define the directory nav items
 580       *
 581       * @since 3.0.0
 582       */
 583  	public function setup_directory_nav() {
 584          $nav_items = array();
 585  
 586          if ( bp_is_members_directory() ) {
 587              $nav_items = bp_nouveau_get_members_directory_nav_items();
 588          } elseif ( bp_is_activity_directory() ) {
 589              $nav_items = bp_nouveau_get_activity_directory_nav_items();
 590          } elseif ( bp_is_groups_directory() ) {
 591              $nav_items = bp_nouveau_get_groups_directory_nav_items();
 592          } elseif ( bp_is_blogs_directory() ) {
 593              $nav_items = bp_nouveau_get_blogs_directory_nav_items();
 594          }
 595  
 596          if ( empty( $nav_items ) ) {
 597              return;
 598          }
 599  
 600          foreach ( $nav_items as $nav_item ) {
 601              if ( empty( $nav_item['component'] ) || $nav_item['component'] !== bp_current_component() ) {
 602                  continue;
 603              }
 604  
 605              // Define the primary nav for the current component's directory
 606              $this->directory_nav->add_nav( $nav_item );
 607          }
 608      }
 609  
 610      /**
 611       * We'll handle template notices from BP Nouveau.
 612       *
 613       * @since 3.0.0
 614       */
 615  	public function neutralize_core_template_notices() {
 616          remove_action( 'template_notices', 'bp_core_render_message' );
 617      }
 618  
 619      /**
 620       * Set the BP Uri for the customizer in case of Ajax requests.
 621       *
 622       * @since 3.0.0
 623       *
 624       * @param  string $path the BP Uri.
 625       * @return string       the BP Uri.
 626       */
 627  	public function customizer_set_uri( $path ) {
 628          if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
 629              return $path;
 630          }
 631  
 632          $uri = parse_url( $path );
 633  
 634          if ( false === strpos( $uri['path'], 'customize.php' ) ) {
 635              return $path;
 636          } else {
 637              $vars = bp_parse_args(
 638                  $uri['query'],
 639                  array(),
 640                  'customizer_set_uri'
 641              );
 642  
 643              if ( ! empty( $vars['url'] ) ) {
 644                  $path = str_replace( get_site_url(), '', urldecode( $vars['url'] ) );
 645              }
 646          }
 647  
 648          return $path;
 649      }
 650  }
 651  
 652  /**
 653   * Get a unique instance of BP Nouveau
 654   *
 655   * @since 3.0.0
 656   *
 657   * @return BP_Nouveau the main instance of the class
 658   */
 659  function bp_nouveau() {
 660      return BP_Nouveau::get_instance();
 661  }
 662  
 663  /**
 664   * Launch BP Nouveau!
 665   */
 666  bp_nouveau();


Generated: Mon Nov 11 01:01:38 2019 Cross-referenced by PHPXref 0.7.1