[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Messages functions
   4   *
   5   * @since 3.0.0
   6   * @version 10.3.0
   7   */
   8  
   9  // Exit if accessed directly.
  10  defined( 'ABSPATH' ) || exit;
  11  
  12  /**
  13   * Enqueue styles for the Messages UI (mentions).
  14   *
  15   * @since 3.0.0
  16   *
  17   * @param array $styles Optional. The array of styles to enqueue.
  18   *
  19   * @return array The same array with the specific messages styles.
  20   */
  21  function bp_nouveau_messages_enqueue_styles( $styles = array() ) {
  22      if ( ! bp_is_user_messages() ) {
  23          return $styles;
  24      }
  25  
  26      return array_merge( $styles, array(
  27          'bp-nouveau-messages-at' => array(
  28              'file'         => buddypress()->plugin_url . 'bp-activity/css/mentions%1$s%2$s.css',
  29              'dependencies' => array( 'bp-nouveau' ),
  30              'version'      => bp_get_version(),
  31          ),
  32      ) );
  33  }
  34  
  35  /**
  36   * Register Scripts for the Messages component
  37   *
  38   * @since 3.0.0
  39   *
  40   * @param array $scripts The array of scripts to register
  41   *
  42   * @return array The same array with the specific messages scripts.
  43   */
  44  function bp_nouveau_messages_register_scripts( $scripts = array() ) {
  45      if ( ! isset( $scripts['bp-nouveau'] ) ) {
  46          return $scripts;
  47      }
  48  
  49      return array_merge( $scripts, array(
  50          'bp-nouveau-messages-at' => array(
  51              'file'         => buddypress()->plugin_url . 'bp-activity/js/mentions%s.js',
  52              'dependencies' => array( 'bp-nouveau', 'jquery', 'jquery-atwho' ),
  53              'version'      => bp_get_version(),
  54              'footer'       => true,
  55          ),
  56          'bp-nouveau-messages' => array(
  57              'file'         => 'js/buddypress-messages%s.js',
  58              'dependencies' => array( 'bp-nouveau', 'json2', 'wp-backbone', 'bp-nouveau-messages-at' ),
  59              'footer'       => true,
  60          ),
  61      ) );
  62  }
  63  
  64  /**
  65   * Enqueue the messages scripts
  66   *
  67   * @since 3.0.0
  68   */
  69  function bp_nouveau_messages_enqueue_scripts() {
  70      if ( ! bp_is_user_messages() ) {
  71          return;
  72      }
  73  
  74      wp_enqueue_script( 'bp-nouveau-messages' );
  75  
  76      // Add The tiny MCE init specific function.
  77      add_filter( 'tiny_mce_before_init', 'bp_nouveau_messages_at_on_tinymce_init', 10, 2 );
  78  }
  79  
  80  /**
  81   * Localize the strings needed for the messages UI
  82   *
  83   * @since 3.0.0
  84   *
  85   * @param  array $params Associative array containing the JS Strings needed by scripts
  86   * @return array         The same array with specific strings for the messages UI if needed.
  87   */
  88  function bp_nouveau_messages_localize_scripts( $params = array() ) {
  89      if ( ! bp_is_user_messages() ) {
  90          return $params;
  91      }
  92  
  93      $bp   = buddypress();
  94      $slug = bp_nouveau_get_component_slug( 'messages' );
  95  
  96      // Use the primary nav to get potential custom slugs.
  97      $primary_nav = $bp->members->nav->get( $slug );
  98      if ( isset( $primary_nav->link ) && $primary_nav->link ) {
  99          $root_url = $primary_nav->link;
 100  
 101          // Make sure to use the displayed user domain.
 102          if ( bp_loggedin_user_domain() ) {
 103              $root_url = str_replace( bp_loggedin_user_domain(), bp_displayed_user_domain(), $root_url );
 104          }
 105      } else {
 106          $root_url = trailingslashit( bp_displayed_user_domain() . $slug );
 107      }
 108  
 109      // Build default routes list.
 110      $routes = array(
 111          'inbox'   => 'inbox',
 112          'sentbox' => 'sentbox',
 113          'compose' => 'compose',
 114      );
 115  
 116      if ( bp_is_active( 'messages', 'star' ) ) {
 117          $routes['starred'] = 'starred';
 118      }
 119  
 120      // Use the secondary nav to get potential custom slugs.
 121      $secondary_nav = $bp->members->nav->get_secondary( array( 'parent_slug' => $slug ), false );
 122  
 123      // Resets the routes list using link slugs.
 124      if ( $secondary_nav ) {
 125          foreach ( $secondary_nav as $subnav_item ) {
 126              $routes[ $subnav_item->slug ] = trim( str_replace( $root_url, '', $subnav_item->link ), '/' );
 127  
 128              if ( ! $routes[ $subnav_item->slug ] ) {
 129                  $routes[ $subnav_item->slug ] = $subnav_item->slug;
 130              }
 131          }
 132      }
 133  
 134      $params['messages'] = array(
 135          'errors'            => array(
 136              'send_to'         => __( 'Please add at least one recipient.', 'buddypress' ),
 137              'subject'         => __( 'Please add a subject to your message.', 'buddypress' ),
 138              'message_content' => __( 'Please add some content to your message.', 'buddypress' ),
 139          ),
 140          'nonces'            => array(
 141              'send' => wp_create_nonce( 'messages_send_message' ),
 142          ),
 143          'loading'           => __( 'Loading messages. Please wait.', 'buddypress' ),
 144          'doingAction'       => array(
 145              'read'   => __( 'Marking messages as read. Please wait.', 'buddypress' ),
 146              'unread' => __( 'Marking messages as unread. Please wait.', 'buddypress' ),
 147              'delete' => __( 'Deleting messages. Please wait.', 'buddypress' ),
 148              'star'   => __( 'Starring messages. Please wait.', 'buddypress' ),
 149              'unstar' => __( 'Unstarring messages. Please wait.', 'buddypress' ),
 150          ),
 151          'bulk_actions'      => bp_nouveau_messages_get_bulk_actions(),
 152          'howto'             => __( 'Click on the message title to preview it in the Active conversation box below.', 'buddypress' ),
 153          'howtoBulk'         => __( 'Use the select box to define your bulk action and click on the &#10003; button to apply.', 'buddypress' ),
 154          'toOthers'          => array(
 155              'one'  => __( '(and 1 other)', 'buddypress' ),
 156  
 157              /* translators: %s: number of message recipients */
 158              'more' => __( '(and %d others)', 'buddypress' ),
 159          ),
 160          'rootUrl'           => parse_url( $root_url, PHP_URL_PATH ),
 161          'supportedRoutes'   => $routes,
 162      );
 163  
 164      // Star private messages.
 165      if ( bp_is_active( 'messages', 'star' ) ) {
 166          $params['messages'] = array_merge( $params['messages'], array(
 167              'strings' => array(
 168                  'text_unstar'  => __( 'Unstar', 'buddypress' ),
 169                  'text_star'    => __( 'Star', 'buddypress' ),
 170                  'title_unstar' => __( 'Starred', 'buddypress' ),
 171                  'title_star'   => __( 'Not starred', 'buddypress' ),
 172                  'title_unstar_thread' => __( 'Remove all starred messages in this thread', 'buddypress' ),
 173                  'title_star_thread'   => __( 'Star the first message in this thread', 'buddypress' ),
 174              ),
 175              'is_single_thread' => (int) bp_is_messages_conversation(),
 176              'star_counter'     => 0,
 177              'unstar_counter'   => 0
 178          ) );
 179      }
 180  
 181      return $params;
 182  }
 183  
 184  /**
 185   * @since 3.0.0
 186   */
 187  function bp_nouveau_messages_adjust_nav() {
 188      $bp = buddypress();
 189  
 190      $secondary_nav_items = $bp->members->nav->get_secondary( array( 'parent_slug' => bp_nouveau_get_component_slug( 'messages' ) ), false );
 191  
 192      if ( empty( $secondary_nav_items ) ) {
 193          return;
 194      }
 195  
 196      foreach ( $secondary_nav_items as $secondary_nav_item ) {
 197          if ( empty( $secondary_nav_item->slug ) ) {
 198              continue;
 199          }
 200  
 201          if ( 'notices' === $secondary_nav_item->slug ) {
 202              bp_core_remove_subnav_item( bp_nouveau_get_component_slug( 'messages' ), $secondary_nav_item->slug, 'members' );
 203          } elseif ( 'compose' === $secondary_nav_item->slug ) {
 204              $bp->members->nav->edit_nav( array(
 205                  'user_has_access' => bp_is_my_profile()
 206              ), $secondary_nav_item->slug, bp_nouveau_get_component_slug( 'messages' ) );
 207          }
 208      }
 209  }
 210  
 211  /**
 212   * @since 3.0.0
 213   */
 214  function bp_nouveau_messages_adjust_admin_nav( $admin_nav ) {
 215      if ( empty( $admin_nav ) ) {
 216          return $admin_nav;
 217      }
 218  
 219      $user_messages_link = trailingslashit( bp_loggedin_user_domain() . bp_nouveau_get_component_slug( 'messages' ) );
 220  
 221      foreach ( $admin_nav as $nav_iterator => $nav ) {
 222          $nav_id = str_replace( 'my-account-messages-', '', $nav['id'] );
 223  
 224          if ( 'notices' === $nav_id ) {
 225              $admin_nav[ $nav_iterator ]['href'] = esc_url( add_query_arg( array(
 226                  'page' => 'bp-notices'
 227              ), bp_get_admin_url( 'users.php' ) ) );
 228          }
 229      }
 230  
 231      return $admin_nav;
 232  }
 233  
 234  /**
 235   * Prepend a notification about the active Sitewide notice.
 236   *
 237   * @since 3.0.0
 238   *
 239   * @param false|array $notifications False if there are no items, an array of notification items otherwise.
 240   * @param int         $user_id       The user ID.
 241   * @return false|array               False if there are no items, an array of notification items otherwise.
 242   */
 243  function bp_nouveau_add_notice_notification_for_user( $notifications, $user_id ) {
 244      if ( ! bp_is_active( 'messages' ) || ! doing_action( 'admin_bar_menu' ) ) {
 245          return $notifications;
 246      }
 247  
 248      $notice = BP_Messages_Notice::get_active();
 249      if ( empty( $notice->id ) ) {
 250          return $notifications;
 251      }
 252  
 253      $closed_notices = bp_get_user_meta( $user_id, 'closed_notices', true );
 254      if ( empty( $closed_notices ) ) {
 255          $closed_notices = array();
 256      }
 257  
 258      if ( in_array( $notice->id, $closed_notices, true ) ) {
 259          return $notifications;
 260      }
 261  
 262      $notice_notification = (object) array(
 263          'id'                => 0,
 264          'user_id'           => $user_id,
 265          'item_id'           => $notice->id,
 266          'secondary_item_id' => 0,
 267          'component_name'    => 'messages',
 268          'component_action'  => 'new_notice',
 269          'date_notified'     => $notice->date_sent,
 270          'is_new'            => 1,
 271          'total_count'       => 1,
 272          'content'           => __( 'New sitewide notice', 'buddypress' ),
 273          'href'              => bp_loggedin_user_domain(),
 274      );
 275  
 276      if ( ! is_array( $notifications ) ) {
 277          $notifications = array( $notice_notification );
 278      } else {
 279          array_unshift( $notifications, $notice_notification );
 280      }
 281  
 282      return $notifications;
 283  }
 284  
 285  /**
 286   * Format the notice notifications.
 287   *
 288   * @since 3.0.0
 289   * @deprecated 10.0.0
 290   *
 291   * @param array $array.
 292   */
 293  function bp_nouveau_format_notice_notification_for_user( $array ) {
 294      _deprecated_function( __FUNCTION__, '10.0.0' );
 295  }
 296  
 297  /**
 298   * @since 3.0.0
 299   */
 300  function bp_nouveau_unregister_notices_widget() {
 301      unregister_widget( 'BP_Messages_Sitewide_Notices_Widget' );
 302  }
 303  
 304  /**
 305   * Add active sitewide notices to the BP template_message global.
 306   *
 307   * @since 3.0.0
 308   */
 309  function bp_nouveau_push_sitewide_notices() {
 310      // Do not show notices if user is not logged in.
 311      if ( ! is_user_logged_in() || ! bp_is_my_profile() ) {
 312          return;
 313      }
 314  
 315      $notice = BP_Messages_Notice::get_active();
 316      if ( empty( $notice ) ) {
 317          return;
 318      }
 319  
 320      $user_id = bp_loggedin_user_id();
 321  
 322      $closed_notices = bp_get_user_meta( $user_id, 'closed_notices', true );
 323      if ( empty( $closed_notices ) ) {
 324          $closed_notices = array();
 325      }
 326  
 327      if ( $notice->id && is_array( $closed_notices ) && ! in_array( $notice->id, $closed_notices, true ) ) {
 328          // Inject the notice into the template_message if no other message has priority.
 329          $bp = buddypress();
 330  
 331          if ( empty( $bp->template_message ) ) {
 332              $message = sprintf(
 333                  '<strong class="subject">%s</strong>
 334                  %s',
 335                  stripslashes( $notice->subject ),
 336                  stripslashes( $notice->message )
 337              );
 338              $bp->template_message      = $message;
 339              $bp->template_message_type = 'bp-sitewide-notice';
 340          }
 341      }
 342  }
 343  
 344  /**
 345   * Disable the WP Editor buttons not allowed in messages content.
 346   *
 347   * @since 3.0.0
 348   *
 349   * @param array $buttons The WP Editor buttons list.
 350   * @param array          The filtered WP Editor buttons list.
 351   */
 352  function bp_nouveau_messages_mce_buttons( $buttons = array() ) {
 353      $remove_buttons = array(
 354          'wp_more',
 355          'spellchecker',
 356          'wp_adv',
 357          'fullscreen',
 358          'alignleft',
 359          'alignright',
 360          'aligncenter',
 361          'formatselect',
 362      );
 363  
 364      // Remove unused buttons
 365      $buttons = array_diff( $buttons, $remove_buttons );
 366  
 367      // Add the image button
 368      array_push( $buttons, 'image' );
 369  
 370      return $buttons;
 371  }
 372  
 373  /**
 374   * @since 3.0.0
 375   */
 376  function bp_nouveau_messages_at_on_tinymce_init( $settings, $editor_id ) {
 377      // We only apply the mentions init to the visual post editor in the WP dashboard.
 378      if ( 'message_content' === $editor_id ) {
 379          $settings['init_instance_callback'] = 'window.bp.Nouveau.Messages.tinyMCEinit';
 380      }
 381  
 382      return $settings;
 383  }
 384  
 385  /**
 386   * @since 3.0.0
 387   */
 388  function bp_nouveau_get_message_date( $date ) {
 389      $now  = bp_core_current_time( true, 'timestamp' );
 390      $date = strtotime( $date );
 391  
 392      $now_date    = getdate( $now );
 393      $date_date   = getdate( $date );
 394      $compare     = array_diff( $date_date, $now_date );
 395      $date_format = 'Y/m/d';
 396  
 397      // Use Timezone string if set.
 398      $timezone_string = bp_get_option( 'timezone_string' );
 399      if ( ! empty( $timezone_string ) ) {
 400          $timezone_object = timezone_open( $timezone_string );
 401          $datetime_object = date_create( "@{$date}" );
 402          $timezone_offset = timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS;
 403  
 404      // Fall back on less reliable gmt_offset
 405      } else {
 406          $timezone_offset = bp_get_option( 'gmt_offset' );
 407      }
 408  
 409      // Calculate time based on the offset
 410      $calculated_time = $date + ( $timezone_offset * HOUR_IN_SECONDS );
 411  
 412      if ( empty( $compare['mday'] ) && empty( $compare['mon'] ) && empty( $compare['year'] ) ) {
 413          $date_format = 'H:i';
 414  
 415      } elseif ( empty( $compare['mon'] ) || empty( $compare['year'] ) ) {
 416          $date_format = 'M j';
 417      }
 418  
 419      /**
 420       * Filters the message date for BuddyPress Nouveau display.
 421       *
 422       * @since 3.0.0
 423       *
 424       * @param string $value           Internationalization-ready formatted date value.
 425       * @param mixed  $calculated_time Calculated time.
 426       * @param string $date            Date value.
 427       * @param string $date_format     Format to convert the calcuated date to.
 428       */
 429      return apply_filters( 'bp_nouveau_get_message_date', date_i18n( $date_format, $calculated_time, true ), $calculated_time, $date, $date_format );
 430  }
 431  
 432  /**
 433   * @since 3.0.0
 434   */
 435  function bp_nouveau_messages_get_bulk_actions() {
 436      ob_start();
 437      bp_messages_bulk_management_dropdown();
 438  
 439      $bulk_actions = array();
 440      $bulk_options = ob_get_clean();
 441  
 442      $matched = preg_match_all( '/<option value="(.*?)"\s*>(.*?)<\/option>/', $bulk_options, $matches, PREG_SET_ORDER );
 443  
 444      if ( $matched && is_array( $matches ) ) {
 445          foreach ( $matches as $i => $match ) {
 446              if ( 0 === $i ) {
 447                  continue;
 448              }
 449  
 450              if ( isset( $match[1] ) && isset( $match[2] ) ) {
 451                  $bulk_actions[] = array(
 452                      'value' => trim( $match[1] ),
 453                      'label' => trim( $match[2] ),
 454                  );
 455              }
 456          }
 457      }
 458  
 459      return $bulk_actions;
 460  }
 461  
 462  /**
 463   * Register notifications filters for the messages component.
 464   *
 465   * @since 3.0.0
 466   */
 467  function bp_nouveau_messages_notification_filters() {
 468      bp_nouveau_notifications_register_filter(
 469          array(
 470              'id'       => 'new_message',
 471              'label'    => __( 'New private messages', 'buddypress' ),
 472              'position' => 115,
 473          )
 474      );
 475  }
 476  
 477  /**
 478   * Fires Messages Legacy hooks to catch the content and add them
 479   * as extra keys to the JSON Messages UI reply.
 480   *
 481   * @since 3.0.1
 482   *
 483   * @param array $hooks The list of hooks to fire.
 484   * @return array       An associative containing the caught content.
 485   */
 486  function bp_nouveau_messages_catch_hook_content( $hooks = array() ) {
 487      $content = array();
 488  
 489      ob_start();
 490      foreach ( $hooks as $js_key => $hook ) {
 491          if ( ! has_action( $hook ) ) {
 492              continue;
 493          }
 494  
 495          // Fire the hook.
 496          do_action( $hook );
 497  
 498          // Catch the sanitized content.
 499          $content[ $js_key ] = bp_strip_script_and_style_tags( ob_get_contents() );
 500  
 501          // Clean the buffer.
 502          ob_clean();
 503      }
 504      ob_end_clean();
 505  
 506      return $content;
 507  }


Generated: Sat May 21 01:00:54 2022 Cross-referenced by PHPXref 0.7.1