[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Activity functions
   4   *
   5   * @since 3.0.0
   6   * @version 5.0.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * Register Scripts for the Activity component
  14   *
  15   * @since 3.0.0
  16   *
  17   * @param array $scripts  The array of scripts to register.
  18   *
  19   * @return array The same array with the specific activity scripts.
  20   */
  21  function bp_nouveau_activity_register_scripts( $scripts = array() ) {
  22      if ( ! isset( $scripts['bp-nouveau'] ) ) {
  23          return $scripts;
  24      }
  25  
  26      return array_merge( $scripts, array(
  27          'bp-nouveau-activity' => array(
  28              'file'         => 'js/buddypress-activity%s.js',
  29              'dependencies' => array( 'bp-nouveau' ),
  30              'footer'       => true,
  31          ),
  32          'bp-nouveau-activity-post-form' => array(
  33              'file'         => 'js/buddypress-activity-post-form%s.js',
  34              'dependencies' => array( 'bp-nouveau', 'bp-nouveau-activity', 'json2', 'wp-backbone' ),
  35              'footer'       => true,
  36          ),
  37      ) );
  38  }
  39  
  40  /**
  41   * Enqueue the activity scripts
  42   *
  43   * @since 3.0.0
  44   */
  45  function bp_nouveau_activity_enqueue_scripts() {
  46      if ( ! bp_is_activity_component() && ! bp_is_group_activity() ) {
  47          return;
  48      }
  49  
  50      wp_enqueue_script( 'bp-nouveau-activity' );
  51  }
  52  
  53  /**
  54   * Localize the strings needed for the Activity Post form UI
  55   *
  56   * @since 3.0.0
  57   *
  58   * @param array $params Associative array containing the JS Strings needed by scripts.
  59   *
  60   * @return array The same array with specific strings for the Activity Post form UI if needed.
  61   */
  62  function bp_nouveau_activity_localize_scripts( $params = array() ) {
  63      if ( ! bp_is_activity_component() && ! bp_is_group_activity() ) {
  64          return $params;
  65      }
  66  
  67      $activity_params = array(
  68          'user_id'     => bp_loggedin_user_id(),
  69          'object'      => 'user',
  70          'backcompat'  => array(
  71              'before_post_form'  => (bool) has_action( 'bp_before_activity_post_form' ),
  72              'post_form_options' => (bool) has_action( 'bp_activity_post_form_options' ),
  73          ),
  74          'post_nonce'  => wp_create_nonce( 'post_update', '_wpnonce_post_update' ),
  75      );
  76  
  77      $user_displayname = bp_get_loggedin_user_fullname();
  78  
  79      if ( buddypress()->avatar->show_avatars ) {
  80          $width  = bp_core_avatar_thumb_width();
  81          $height = bp_core_avatar_thumb_height();
  82          $activity_params = array_merge( $activity_params, array(
  83              'avatar_url'    => bp_get_loggedin_user_avatar( array(
  84                  'width'  => $width,
  85                  'height' => $height,
  86                  'html'   => false,
  87              ) ),
  88              'avatar_width'  => $width,
  89              'avatar_height' => $height,
  90              'user_domain'   => bp_loggedin_user_domain(),
  91              'avatar_alt'    => sprintf(
  92                  /* translators: %s = member name */
  93                  __( 'Profile photo of %s', 'buddypress' ),
  94                  $user_displayname
  95              ),
  96          ) );
  97      }
  98  
  99      /**
 100       * Filters the included, specific, Action buttons.
 101       *
 102       * @since 3.0.0
 103       *
 104       * @param array $value The array containing the button params. Must look like:
 105       * array( 'buttonid' => array(
 106       *  'id'      => 'buttonid',                            // Id for your action
 107       *  'caption' => __( 'Button caption', 'text-domain' ),
 108       *  'icon'    => 'dashicons-*',                         // The dashicon to use
 109       *  'order'   => 0,
 110       *  'handle'  => 'button-script-handle',                // The handle of the registered script to enqueue
 111       * );
 112       */
 113      $activity_buttons = apply_filters( 'bp_nouveau_activity_buttons', array() );
 114  
 115      if ( ! empty( $activity_buttons ) ) {
 116          $activity_params['buttons'] = bp_sort_by_key( $activity_buttons, 'order', 'num' );
 117  
 118          // Enqueue Buttons scripts and styles
 119          foreach ( $activity_params['buttons'] as $key_button => $buttons ) {
 120              if ( empty( $buttons['handle'] ) ) {
 121                  continue;
 122              }
 123  
 124              if ( wp_style_is( $buttons['handle'], 'registered' ) ) {
 125                  wp_enqueue_style( $buttons['handle'] );
 126              }
 127  
 128              if ( wp_script_is( $buttons['handle'], 'registered' ) ) {
 129                  wp_enqueue_script( $buttons['handle'] );
 130              }
 131  
 132              unset( $activity_params['buttons'][ $key_button ]['handle'] );
 133          }
 134      }
 135  
 136      // Activity Objects
 137      if ( ! bp_is_single_item() && ! bp_is_user() ) {
 138          $activity_objects = array(
 139              'profile' => array(
 140                  'text'                     => __( 'Post in: Profile', 'buddypress' ),
 141                  'autocomplete_placeholder' => '',
 142                  'priority'                 => 5,
 143              ),
 144          );
 145  
 146          // the groups component is active & the current user is at least a member of 1 group
 147          if ( bp_is_active( 'groups' ) && bp_has_groups( array( 'user_id' => bp_loggedin_user_id(), 'max' => 1 ) ) ) {
 148              $activity_objects['group'] = array(
 149                  'text'                     => __( 'Post in: Group', 'buddypress' ),
 150                  'autocomplete_placeholder' => __( 'Start typing the group name...', 'buddypress' ),
 151                  'priority'                 => 10,
 152              );
 153          }
 154  
 155          /**
 156           * Filters the activity objects to apply for localized javascript data.
 157           *
 158           * @since 3.0.0
 159           *
 160           * @param array $activity_objects Array of activity objects.
 161           */
 162          $activity_params['objects'] = apply_filters( 'bp_nouveau_activity_objects', $activity_objects );
 163      }
 164  
 165      $activity_strings = array(
 166          'whatsnewPlaceholder' => sprintf( __( "What's new, %s?", 'buddypress' ), bp_get_user_firstname( $user_displayname ) ),
 167          'whatsnewLabel'       => __( 'Post what\'s new', 'buddypress' ),
 168          'whatsnewpostinLabel' => __( 'Post in', 'buddypress' ),
 169          'postUpdateButton'    => __( 'Post Update', 'buddypress' ),
 170          'cancelButton'        => __( 'Cancel', 'buddypress' ),
 171      );
 172  
 173      if ( bp_is_group() ) {
 174          $activity_params = array_merge(
 175              $activity_params,
 176              array(
 177                  'object'  => 'group',
 178                  'item_id' => bp_get_current_group_id(),
 179              )
 180          );
 181      }
 182  
 183      $params['activity'] = array(
 184          'params'  => $activity_params,
 185          'strings' => $activity_strings,
 186      );
 187  
 188      return $params;
 189  }
 190  
 191  /**
 192   * @since 3.0.0
 193   */
 194  function bp_nouveau_get_activity_directory_nav_items() {
 195      $nav_items = array();
 196  
 197      $nav_items['all'] = array(
 198          'component' => 'activity',
 199          'slug'      => 'all', // slug is used because BP_Core_Nav requires it, but it's the scope
 200          'li_class'  => array( 'dynamic' ),
 201          'link'      => bp_get_activity_directory_permalink(),
 202          'text'      => __( 'All Members', 'buddypress' ),
 203          'count'     => '',
 204          'position'  => 5,
 205      );
 206  
 207      // deprecated hooks
 208      $deprecated_hooks = array(
 209          array( 'bp_before_activity_type_tab_all', 'activity', 0 ),
 210          array( 'bp_activity_type_tabs', 'activity', 46 ),
 211      );
 212  
 213      if ( is_user_logged_in() ) {
 214          $deprecated_hooks = array_merge(
 215              $deprecated_hooks,
 216              array(
 217                  array( 'bp_before_activity_type_tab_friends', 'activity', 6 ),
 218                  array( 'bp_before_activity_type_tab_groups', 'activity', 16 ),
 219                  array( 'bp_before_activity_type_tab_favorites', 'activity', 26 ),
 220              )
 221          );
 222  
 223          // If the user has favorite create a nav item
 224          if ( bp_get_total_favorite_count_for_user( bp_loggedin_user_id() ) ) {
 225              $nav_items['favorites'] = array(
 226                  'component' => 'activity',
 227                  'slug'      => 'favorites', // slug is used because BP_Core_Nav requires it, but it's the scope
 228                  'li_class'  => array(),
 229                  'link'      => bp_loggedin_user_domain() . bp_get_activity_slug() . '/favorites/',
 230                  'text'      => __( 'My Favorites', 'buddypress' ),
 231                  'count'     => false,
 232                  'position'  => 35,
 233              );
 234          }
 235  
 236          // The friends component is active and user has friends
 237          if ( bp_is_active( 'friends' ) && bp_get_total_friend_count( bp_loggedin_user_id() ) ) {
 238              $nav_items['friends'] = array(
 239                  'component' => 'activity',
 240                  'slug'      => 'friends', // slug is used because BP_Core_Nav requires it, but it's the scope
 241                  'li_class'  => array( 'dynamic' ),
 242                  'link'      => bp_loggedin_user_domain() . bp_get_activity_slug() . '/' . bp_get_friends_slug() . '/',
 243                  'text'      => __( 'My Friends', 'buddypress' ),
 244                  'count'     => '',
 245                  'position'  => 15,
 246              );
 247          }
 248  
 249          // The groups component is active and user has groups
 250          if ( bp_is_active( 'groups' ) && bp_get_total_group_count_for_user( bp_loggedin_user_id() ) ) {
 251              $nav_items['groups'] = array(
 252                  'component' => 'activity',
 253                  'slug'      => 'groups', // slug is used because BP_Core_Nav requires it, but it's the scope
 254                  'li_class'  => array( 'dynamic' ),
 255                  'link'      => bp_loggedin_user_domain() . bp_get_activity_slug() . '/' . bp_get_groups_slug() . '/',
 256                  'text'      => __( 'My Groups', 'buddypress' ),
 257                  'count'     => '',
 258                  'position'  => 25,
 259              );
 260          }
 261  
 262          // Mentions are allowed
 263          if ( bp_activity_do_mentions() ) {
 264              $deprecated_hooks[] = array( 'bp_before_activity_type_tab_mentions', 'activity', 36 );
 265  
 266              $count = '';
 267              if ( bp_get_total_mention_count_for_user( bp_loggedin_user_id() ) ) {
 268                  $count = bp_get_total_mention_count_for_user( bp_loggedin_user_id() );
 269              }
 270  
 271              $nav_items['mentions'] = array(
 272                  'component' => 'activity',
 273                  'slug'      => 'mentions', // slug is used because BP_Core_Nav requires it, but it's the scope
 274                  'li_class'  => array( 'dynamic' ),
 275                  'link'      => bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/',
 276                  'text'      => __( 'Mentions', 'buddypress' ),
 277                  'count'     => $count,
 278                  'position'  => 45,
 279              );
 280          }
 281      }
 282  
 283      // Check for deprecated hooks :
 284      foreach ( $deprecated_hooks as $deprectated_hook ) {
 285          list( $hook, $component, $position ) = $deprectated_hook;
 286  
 287          $extra_nav_items = bp_nouveau_parse_hooked_dir_nav( $hook, $component, $position );
 288  
 289          if ( ! empty( $extra_nav_items ) ) {
 290              $nav_items = array_merge( $nav_items, $extra_nav_items );
 291          }
 292      }
 293  
 294      /**
 295       * Filters the activity directory navigation items.
 296       *
 297       * Use this filter to introduce your custom nav items for the activity directory.
 298       *
 299       * @since 3.0.0
 300       *
 301       * @param array $nav_items The list of the activity directory nav items.
 302       */
 303      return apply_filters( 'bp_nouveau_get_activity_directory_nav_items', $nav_items );
 304  }
 305  
 306  /**
 307   * Make sure bp_get_activity_show_filters() will return the filters and the context
 308   * instead of the output.
 309   *
 310   * @since 3.0.0
 311   *
 312   * @param string $output  HTML output
 313   * @param array  $filters Optional.
 314   * @param string $context
 315   *
 316   * @return array
 317   */
 318  function bp_nouveau_get_activity_filters_array( $output = '', $filters = array(), $context = '' ) {
 319      return array(
 320          'filters' => $filters,
 321          'context' => $context,
 322      );
 323  }
 324  
 325  /**
 326   * Get Dropdown filters of the activity component
 327   *
 328   * @since 3.0.0
 329   *
 330   * @return array the filters
 331   */
 332  function bp_nouveau_get_activity_filters() {
 333      add_filter( 'bp_get_activity_show_filters', 'bp_nouveau_get_activity_filters_array', 10, 3 );
 334  
 335      $filters_data = bp_get_activity_show_filters();
 336  
 337      remove_filter( 'bp_get_activity_show_filters', 'bp_nouveau_get_activity_filters_array', 10, 3 );
 338  
 339      $action = '';
 340      if ( 'group' === $filters_data['context'] ) {
 341          $action = 'bp_group_activity_filter_options';
 342      } elseif ( 'member' === $filters_data['context'] || 'member_groups' === $filters_data['context'] ) {
 343          $action = 'bp_member_activity_filter_options';
 344      } else {
 345          $action = 'bp_activity_filter_options';
 346      }
 347  
 348      $filters = $filters_data['filters'];
 349  
 350      if ( $action ) {
 351          return bp_nouveau_parse_hooked_options( $action, $filters );
 352      }
 353  
 354      return $filters;
 355  }
 356  
 357  /**
 358   * @since 3.0.0
 359   */
 360  function bp_nouveau_activity_secondary_avatars( $action, $activity ) {
 361      switch ( $activity->component ) {
 362          case 'groups':
 363          case 'friends':
 364              $secondary_avatar = bp_get_activity_secondary_avatar( array( 'linked' => false ) );
 365  
 366              // Only insert avatar if one exists.
 367              if ( $secondary_avatar ) {
 368                  $link_close  = '">';
 369                  $first_link  = strpos( $action, $link_close );
 370                  $second_link = strpos( $action, $link_close, $first_link + strlen( $link_close ) );
 371                  $action      = substr_replace( $action, $secondary_avatar, $second_link + 2, 0 );
 372              }
 373              break;
 374      }
 375  
 376      return $action;
 377  }
 378  
 379  /**
 380   * @since 3.0.0
 381   */
 382  function bp_nouveau_activity_scope_newest_class( $classes = '' ) {
 383      if ( ! is_user_logged_in() ) {
 384          return $classes;
 385      }
 386  
 387      $user_id    = bp_loggedin_user_id();
 388      $my_classes = array();
 389  
 390      /*
 391       * HeartBeat requests will transport the scope.
 392       * See bp_nouveau_ajax_querystring().
 393       */
 394      $scope = '';
 395  
 396      if ( ! empty( $_POST['data']['bp_heartbeat']['scope'] ) ) {
 397          $scope = sanitize_key( $_POST['data']['bp_heartbeat']['scope'] );
 398      }
 399  
 400      // Add specific classes to perform specific actions on the client side.
 401      if ( $scope && bp_is_activity_directory() ) {
 402          $component = bp_get_activity_object_name();
 403  
 404          /*
 405           * These classes will be used to count the number of newest activities for
 406           * the 'Mentions', 'My Groups' & 'My Friends' tabs
 407           */
 408          if ( 'all' === $scope ) {
 409              if ( 'groups' === $component && bp_is_active( $component ) ) {
 410                  // Is the current user a member of the group the activity is attached to?
 411                  if ( groups_is_user_member( $user_id, bp_get_activity_item_id() ) ) {
 412                      $my_classes[] = 'bp-my-groups';
 413                  }
 414              }
 415  
 416              // Friends can post in groups the user is a member of
 417              if ( bp_is_active( 'friends' ) && (int) $user_id !== (int) bp_get_activity_user_id() ) {
 418                  if ( friends_check_friendship( $user_id, bp_get_activity_user_id() ) ) {
 419                      $my_classes[] = 'bp-my-friends';
 420                  }
 421              }
 422  
 423              // A mention can be posted by a friend within a group
 424              if ( true === bp_activity_do_mentions() ) {
 425                  $new_mentions = bp_get_user_meta( $user_id, 'bp_new_mentions', true );
 426  
 427                  // The current activity is one of the new mentions
 428                  if ( is_array( $new_mentions ) && in_array( bp_get_activity_id(), $new_mentions, true ) ) {
 429                      $my_classes[] = 'bp-my-mentions';
 430                  }
 431              }
 432  
 433          /*
 434           * This class will be used to highlight the newest activities when
 435           * viewing the 'Mentions', 'My Groups' or the 'My Friends' tabs
 436           */
 437          } elseif ( 'friends' === $scope || 'groups' === $scope || 'mentions' === $scope ) {
 438              $my_classes[] = 'newest_' . $scope . '_activity';
 439          }
 440  
 441          // Leave other components do their specific stuff if needed.
 442          /**
 443           * Filters the classes to be applied to the newest activity item.
 444           *
 445           * Leave other components do their specific stuff if needed.
 446           *
 447           * @since 3.0.0
 448           *
 449           * @param array  $my_classes Array of classes to output to class attribute.
 450           * @param string $scope      Current scope for the activity type.
 451           */
 452          $my_classes = (array) apply_filters( 'bp_nouveau_activity_scope_newest_class', $my_classes, $scope );
 453  
 454          if ( ! empty( $my_classes ) ) {
 455              $classes .= ' ' . join( ' ', $my_classes );
 456          }
 457      }
 458  
 459      return $classes;
 460  }
 461  
 462  /**
 463   * Get the activity query args for the widget.
 464   *
 465   * @since 3.0.0
 466   *
 467   * @return array The activity arguments.
 468   */
 469  function bp_nouveau_activity_widget_query() {
 470      $args       = array();
 471      $bp_nouveau = bp_nouveau();
 472  
 473      if ( isset( $bp_nouveau->activity->widget_args ) ) {
 474          $args = $bp_nouveau->activity->widget_args;
 475      }
 476  
 477      /**
 478       * Filter to edit the activity widget arguments.
 479       *
 480       * @since 3.0.0
 481       *
 482       * @param array $args The activity arguments.
 483       */
 484      return apply_filters( 'bp_nouveau_activity_widget_query', $args );
 485  }
 486  
 487  /**
 488   * Register notifications filters for the activity component.
 489   *
 490   * @since 3.0.0
 491   */
 492  function bp_nouveau_activity_notification_filters() {
 493      $notifications = array(
 494          array(
 495              'id'       => 'new_at_mention',
 496              'label'    => __( 'New mentions', 'buddypress' ),
 497              'position' => 5,
 498          ),
 499          array(
 500              'id'       => 'update_reply',
 501              'label'    => __( 'New update replies', 'buddypress' ),
 502              'position' => 15,
 503          ),
 504          array(
 505              'id'       => 'comment_reply',
 506              'label'    => __( 'New update comment replies', 'buddypress' ),
 507              'position' => 25,
 508          ),
 509      );
 510  
 511      foreach ( $notifications as $notification ) {
 512          bp_nouveau_notifications_register_filter( $notification );
 513      }
 514  }
 515  
 516  /**
 517   * Add controls for the settings of the customizer for the activity component.
 518   *
 519   * @since 3.0.0
 520   *
 521   * @param array $controls Optional. The controls to add.
 522   *
 523   * @return array the controls to add.
 524   */
 525  function bp_nouveau_activity_customizer_controls( $controls = array() ) {
 526      return array_merge( $controls, array(
 527          'act_dir_layout' => array(
 528              'label'      => __( 'Use column navigation for the Activity directory.', 'buddypress' ),
 529              'section'    => 'bp_nouveau_dir_layout',
 530              'settings'   => 'bp_nouveau_appearance[activity_dir_layout]',
 531              'type'       => 'checkbox',
 532          ),
 533          'act_dir_tabs' => array(
 534              'label'      => __( 'Use tab styling for Activity directory navigation.', 'buddypress' ),
 535              'section'    => 'bp_nouveau_dir_layout',
 536              'settings'   => 'bp_nouveau_appearance[activity_dir_tabs]',
 537              'type'       => 'checkbox',
 538          ),
 539      ) );
 540  }


Generated: Wed Jul 24 01:01:43 2019 Cross-referenced by PHPXref 0.7.1