[ 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.0.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      $params['messages'] = array(
  94          'errors' => array(
  95              'send_to'         => __( 'Please add at least one recipient.', 'buddypress' ),
  96              'subject'         => __( 'Please add a subject to your message.', 'buddypress' ),
  97              'message_content' => __( 'Please add some content to your message.', 'buddypress' ),
  98          ),
  99          'nonces' => array(
 100              'send' => wp_create_nonce( 'messages_send_message' ),
 101          ),
 102          'loading'       => __( 'Loading messages. Please wait.', 'buddypress' ),
 103          'doingAction'   => array(
 104              'read'   => __( 'Marking messages as read. Please wait.', 'buddypress' ),
 105              'unread' => __( 'Marking messages as unread. Please wait.', 'buddypress' ),
 106              'delete' => __( 'Deleting messages. Please wait.', 'buddypress' ),
 107              'star'   => __( 'Starring messages. Please wait.', 'buddypress' ),
 108              'unstar' => __( 'Unstarring messages. Please wait.', 'buddypress' ),
 109          ),
 110          'bulk_actions'  => bp_nouveau_messages_get_bulk_actions(),
 111          'howto'         => __( 'Click on the message title to preview it in the Active conversation box below.', 'buddypress' ),
 112          'howtoBulk'     => __( 'Use the select box to define your bulk action and click on the &#10003; button to apply.', 'buddypress' ),
 113          'toOthers'      => array(
 114              'one'  => __( '(and 1 other)', 'buddypress' ),
 115  
 116              /* translators: %s: number of message recipients */
 117              'more' => __( '(and %d others)', 'buddypress' ),
 118          ),
 119          'rootUrl' => parse_url( trailingslashit( bp_displayed_user_domain() . bp_nouveau_get_component_slug( 'messages' ) ), PHP_URL_PATH ),
 120      );
 121  
 122      // Star private messages.
 123      if ( bp_is_active( 'messages', 'star' ) ) {
 124          $params['messages'] = array_merge( $params['messages'], array(
 125              'strings' => array(
 126                  'text_unstar'  => __( 'Unstar', 'buddypress' ),
 127                  'text_star'    => __( 'Star', 'buddypress' ),
 128                  'title_unstar' => __( 'Starred', 'buddypress' ),
 129                  'title_star'   => __( 'Not starred', 'buddypress' ),
 130                  'title_unstar_thread' => __( 'Remove all starred messages in this thread', 'buddypress' ),
 131                  'title_star_thread'   => __( 'Star the first message in this thread', 'buddypress' ),
 132              ),
 133              'is_single_thread' => (int) bp_is_messages_conversation(),
 134              'star_counter'     => 0,
 135              'unstar_counter'   => 0
 136          ) );
 137      }
 138  
 139      return $params;
 140  }
 141  
 142  /**
 143   * @since 3.0.0
 144   */
 145  function bp_nouveau_messages_adjust_nav() {
 146      $bp = buddypress();
 147  
 148      $secondary_nav_items = $bp->members->nav->get_secondary( array( 'parent_slug' => bp_nouveau_get_component_slug( 'messages' ) ), false );
 149  
 150      if ( empty( $secondary_nav_items ) ) {
 151          return;
 152      }
 153  
 154      foreach ( $secondary_nav_items as $secondary_nav_item ) {
 155          if ( empty( $secondary_nav_item->slug ) ) {
 156              continue;
 157          }
 158  
 159          if ( 'notices' === $secondary_nav_item->slug ) {
 160              bp_core_remove_subnav_item( bp_nouveau_get_component_slug( 'messages' ), $secondary_nav_item->slug, 'members' );
 161          } elseif ( 'compose' === $secondary_nav_item->slug ) {
 162              $bp->members->nav->edit_nav( array(
 163                  'user_has_access' => bp_is_my_profile()
 164              ), $secondary_nav_item->slug, bp_nouveau_get_component_slug( 'messages' ) );
 165          }
 166      }
 167  }
 168  
 169  /**
 170   * @since 3.0.0
 171   */
 172  function bp_nouveau_messages_adjust_admin_nav( $admin_nav ) {
 173      if ( empty( $admin_nav ) ) {
 174          return $admin_nav;
 175      }
 176  
 177      $user_messages_link = trailingslashit( bp_loggedin_user_domain() . bp_nouveau_get_component_slug( 'messages' ) );
 178  
 179      foreach ( $admin_nav as $nav_iterator => $nav ) {
 180          $nav_id = str_replace( 'my-account-messages-', '', $nav['id'] );
 181  
 182          if ( 'notices' === $nav_id ) {
 183              $admin_nav[ $nav_iterator ]['href'] = esc_url( add_query_arg( array(
 184                  'page' => 'bp-notices'
 185              ), bp_get_admin_url( 'users.php' ) ) );
 186          }
 187      }
 188  
 189      return $admin_nav;
 190  }
 191  
 192  /**
 193   * Prepend a notification about the active Sitewide notice.
 194   *
 195   * @since 3.0.0
 196   *
 197   * @param false|array $notifications False if there are no items, an array of notification items otherwise.
 198   * @param int         $user_id       The user ID.
 199   * @return false|array               False if there are no items, an array of notification items otherwise.
 200   */
 201  function bp_nouveau_add_notice_notification_for_user( $notifications, $user_id ) {
 202      if ( ! bp_is_active( 'messages' ) || ! doing_action( 'admin_bar_menu' ) ) {
 203          return $notifications;
 204      }
 205  
 206      $notice = BP_Messages_Notice::get_active();
 207      if ( empty( $notice->id ) ) {
 208          return $notifications;
 209      }
 210  
 211      $closed_notices = bp_get_user_meta( $user_id, 'closed_notices', true );
 212      if ( empty( $closed_notices ) ) {
 213          $closed_notices = array();
 214      }
 215  
 216      if ( in_array( $notice->id, $closed_notices, true ) ) {
 217          return $notifications;
 218      }
 219  
 220      $notice_notification = (object) array(
 221          'id'                => 0,
 222          'user_id'           => $user_id,
 223          'item_id'           => $notice->id,
 224          'secondary_item_id' => 0,
 225          'component_name'    => 'messages',
 226          'component_action'  => 'new_notice',
 227          'date_notified'     => $notice->date_sent,
 228          'is_new'            => 1,
 229          'total_count'       => 1,
 230          'content'           => __( 'New sitewide notice', 'buddypress' ),
 231          'href'              => bp_loggedin_user_domain(),
 232      );
 233  
 234      if ( ! is_array( $notifications ) ) {
 235          $notifications = array( $notice_notification );
 236      } else {
 237          array_unshift( $notifications, $notice_notification );
 238      }
 239  
 240      return $notifications;
 241  }
 242  
 243  /**
 244   * Format the notice notifications.
 245   *
 246   * @since 3.0.0
 247   * @deprecated 10.0.0
 248   *
 249   * @param array $array.
 250   */
 251  function bp_nouveau_format_notice_notification_for_user( $array ) {
 252      _deprecated_function( __FUNCTION__, '10.0.0' );
 253  }
 254  
 255  /**
 256   * @since 3.0.0
 257   */
 258  function bp_nouveau_unregister_notices_widget() {
 259      unregister_widget( 'BP_Messages_Sitewide_Notices_Widget' );
 260  }
 261  
 262  /**
 263   * Add active sitewide notices to the BP template_message global.
 264   *
 265   * @since 3.0.0
 266   */
 267  function bp_nouveau_push_sitewide_notices() {
 268      // Do not show notices if user is not logged in.
 269      if ( ! is_user_logged_in() || ! bp_is_my_profile() ) {
 270          return;
 271      }
 272  
 273      $notice = BP_Messages_Notice::get_active();
 274      if ( empty( $notice ) ) {
 275          return;
 276      }
 277  
 278      $user_id = bp_loggedin_user_id();
 279  
 280      $closed_notices = bp_get_user_meta( $user_id, 'closed_notices', true );
 281      if ( empty( $closed_notices ) ) {
 282          $closed_notices = array();
 283      }
 284  
 285      if ( $notice->id && is_array( $closed_notices ) && ! in_array( $notice->id, $closed_notices, true ) ) {
 286          // Inject the notice into the template_message if no other message has priority.
 287          $bp = buddypress();
 288  
 289          if ( empty( $bp->template_message ) ) {
 290              $message = sprintf(
 291                  '<strong class="subject">%s</strong>
 292                  %s',
 293                  stripslashes( $notice->subject ),
 294                  stripslashes( $notice->message )
 295              );
 296              $bp->template_message      = $message;
 297              $bp->template_message_type = 'bp-sitewide-notice';
 298          }
 299      }
 300  }
 301  
 302  /**
 303   * Disable the WP Editor buttons not allowed in messages content.
 304   *
 305   * @since 3.0.0
 306   *
 307   * @param array $buttons The WP Editor buttons list.
 308   * @param array          The filtered WP Editor buttons list.
 309   */
 310  function bp_nouveau_messages_mce_buttons( $buttons = array() ) {
 311      $remove_buttons = array(
 312          'wp_more',
 313          'spellchecker',
 314          'wp_adv',
 315          'fullscreen',
 316          'alignleft',
 317          'alignright',
 318          'aligncenter',
 319          'formatselect',
 320      );
 321  
 322      // Remove unused buttons
 323      $buttons = array_diff( $buttons, $remove_buttons );
 324  
 325      // Add the image button
 326      array_push( $buttons, 'image' );
 327  
 328      return $buttons;
 329  }
 330  
 331  /**
 332   * @since 3.0.0
 333   */
 334  function bp_nouveau_messages_at_on_tinymce_init( $settings, $editor_id ) {
 335      // We only apply the mentions init to the visual post editor in the WP dashboard.
 336      if ( 'message_content' === $editor_id ) {
 337          $settings['init_instance_callback'] = 'window.bp.Nouveau.Messages.tinyMCEinit';
 338      }
 339  
 340      return $settings;
 341  }
 342  
 343  /**
 344   * @since 3.0.0
 345   */
 346  function bp_nouveau_get_message_date( $date ) {
 347      $now  = bp_core_current_time( true, 'timestamp' );
 348      $date = strtotime( $date );
 349  
 350      $now_date    = getdate( $now );
 351      $date_date   = getdate( $date );
 352      $compare     = array_diff( $date_date, $now_date );
 353      $date_format = 'Y/m/d';
 354  
 355      // Use Timezone string if set.
 356      $timezone_string = bp_get_option( 'timezone_string' );
 357      if ( ! empty( $timezone_string ) ) {
 358          $timezone_object = timezone_open( $timezone_string );
 359          $datetime_object = date_create( "@{$date}" );
 360          $timezone_offset = timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS;
 361  
 362      // Fall back on less reliable gmt_offset
 363      } else {
 364          $timezone_offset = bp_get_option( 'gmt_offset' );
 365      }
 366  
 367      // Calculate time based on the offset
 368      $calculated_time = $date + ( $timezone_offset * HOUR_IN_SECONDS );
 369  
 370      if ( empty( $compare['mday'] ) && empty( $compare['mon'] ) && empty( $compare['year'] ) ) {
 371          $date_format = 'H:i';
 372  
 373      } elseif ( empty( $compare['mon'] ) || empty( $compare['year'] ) ) {
 374          $date_format = 'M j';
 375      }
 376  
 377      /**
 378       * Filters the message date for BuddyPress Nouveau display.
 379       *
 380       * @since 3.0.0
 381       *
 382       * @param string $value           Internationalization-ready formatted date value.
 383       * @param mixed  $calculated_time Calculated time.
 384       * @param string $date            Date value.
 385       * @param string $date_format     Format to convert the calcuated date to.
 386       */
 387      return apply_filters( 'bp_nouveau_get_message_date', date_i18n( $date_format, $calculated_time, true ), $calculated_time, $date, $date_format );
 388  }
 389  
 390  /**
 391   * @since 3.0.0
 392   */
 393  function bp_nouveau_messages_get_bulk_actions() {
 394      ob_start();
 395      bp_messages_bulk_management_dropdown();
 396  
 397      $bulk_actions = array();
 398      $bulk_options = ob_get_clean();
 399  
 400      $matched = preg_match_all( '/<option value="(.*?)"\s*>(.*?)<\/option>/', $bulk_options, $matches, PREG_SET_ORDER );
 401  
 402      if ( $matched && is_array( $matches ) ) {
 403          foreach ( $matches as $i => $match ) {
 404              if ( 0 === $i ) {
 405                  continue;
 406              }
 407  
 408              if ( isset( $match[1] ) && isset( $match[2] ) ) {
 409                  $bulk_actions[] = array(
 410                      'value' => trim( $match[1] ),
 411                      'label' => trim( $match[2] ),
 412                  );
 413              }
 414          }
 415      }
 416  
 417      return $bulk_actions;
 418  }
 419  
 420  /**
 421   * Register notifications filters for the messages component.
 422   *
 423   * @since 3.0.0
 424   */
 425  function bp_nouveau_messages_notification_filters() {
 426      bp_nouveau_notifications_register_filter(
 427          array(
 428              'id'       => 'new_message',
 429              'label'    => __( 'New private messages', 'buddypress' ),
 430              'position' => 115,
 431          )
 432      );
 433  }
 434  
 435  /**
 436   * Fires Messages Legacy hooks to catch the content and add them
 437   * as extra keys to the JSON Messages UI reply.
 438   *
 439   * @since 3.0.1
 440   *
 441   * @param array $hooks The list of hooks to fire.
 442   * @return array       An associative containing the caught content.
 443   */
 444  function bp_nouveau_messages_catch_hook_content( $hooks = array() ) {
 445      $content = array();
 446  
 447      ob_start();
 448      foreach ( $hooks as $js_key => $hook ) {
 449          if ( ! has_action( $hook ) ) {
 450              continue;
 451          }
 452  
 453          // Fire the hook.
 454          do_action( $hook );
 455  
 456          // Catch the sanitized content.
 457          $content[ $js_key ] = bp_strip_script_and_style_tags( ob_get_contents() );
 458  
 459          // Clean the buffer.
 460          ob_clean();
 461      }
 462      ob_end_clean();
 463  
 464      return $content;
 465  }


Generated: Sun Oct 24 01:00:54 2021 Cross-referenced by PHPXref 0.7.1