[ 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 3.1.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_get_messages_slug() ), 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_get_messages_slug() ), 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_get_messages_slug(), $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_get_messages_slug() );
 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_get_messages_slug() );
 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   * @since 3.0.0
 194   */
 195  function bp_nouveau_add_notice_notification_for_user( $notifications, $user_id ) {
 196      if ( ! bp_is_active( 'messages' ) || ! doing_action( 'admin_bar_menu' ) ) {
 197          return $notifications;
 198      }
 199  
 200      $notice = BP_Messages_Notice::get_active();
 201      if ( empty( $notice->id ) ) {
 202          return $notifications;
 203      }
 204  
 205      $closed_notices = bp_get_user_meta( $user_id, 'closed_notices', true );
 206      if ( empty( $closed_notices ) ) {
 207          $closed_notices = array();
 208      }
 209  
 210      if ( in_array( $notice->id, $closed_notices, true ) ) {
 211          return $notifications;
 212      }
 213  
 214      $notice_notification                    = new stdClass;
 215      $notice_notification->id                = 0;
 216      $notice_notification->user_id           = $user_id;
 217      $notice_notification->item_id           = $notice->id;
 218      $notice_notification->secondary_item_id = '';
 219      $notice_notification->component_name    = 'messages';
 220      $notice_notification->component_action  = 'new_notice';
 221      $notice_notification->date_notified     = $notice->date_sent;
 222      $notice_notification->is_new            = '1';
 223  
 224      return array_merge( $notifications, array( $notice_notification ) );
 225  }
 226  
 227  /**
 228   * @since 3.0.0
 229   */
 230  function bp_nouveau_format_notice_notification_for_user( $array ) {
 231      if ( ! empty( $array['text'] ) || ! doing_action( 'admin_bar_menu' ) ) {
 232          return $array;
 233      }
 234  
 235      return array(
 236          'text' => __( 'New sitewide notice', 'buddypress' ),
 237          'link' => bp_loggedin_user_domain(),
 238      );
 239  }
 240  
 241  /**
 242   * @since 3.0.0
 243   */
 244  function bp_nouveau_unregister_notices_widget() {
 245      unregister_widget( 'BP_Messages_Sitewide_Notices_Widget' );
 246  }
 247  
 248  /**
 249   * Add active sitewide notices to the BP template_message global.
 250   *
 251   * @since 3.0.0
 252   */
 253  function bp_nouveau_push_sitewide_notices() {
 254      // Do not show notices if user is not logged in.
 255      if ( ! is_user_logged_in() || ! bp_is_my_profile() ) {
 256          return;
 257      }
 258  
 259      $notice = BP_Messages_Notice::get_active();
 260      if ( empty( $notice ) ) {
 261          return;
 262      }
 263  
 264      $user_id = bp_loggedin_user_id();
 265  
 266      $closed_notices = bp_get_user_meta( $user_id, 'closed_notices', true );
 267      if ( empty( $closed_notices ) ) {
 268          $closed_notices = array();
 269      }
 270  
 271      if ( $notice->id && is_array( $closed_notices ) && ! in_array( $notice->id, $closed_notices, true ) ) {
 272          // Inject the notice into the template_message if no other message has priority.
 273          $bp = buddypress();
 274  
 275          if ( empty( $bp->template_message ) ) {
 276              $message = sprintf(
 277                  '<strong class="subject">%s</strong>
 278                  %s',
 279                  stripslashes( $notice->subject ),
 280                  stripslashes( $notice->message )
 281              );
 282              $bp->template_message      = $message;
 283              $bp->template_message_type = 'bp-sitewide-notice';
 284          }
 285      }
 286  }
 287  
 288  /**
 289   * Disable the WP Editor buttons not allowed in messages content.
 290   *
 291   * @since 3.0.0
 292   *
 293   * @param array $buttons The WP Editor buttons list.
 294   * @param array          The filtered WP Editor buttons list.
 295   */
 296  function bp_nouveau_messages_mce_buttons( $buttons = array() ) {
 297      $remove_buttons = array(
 298          'wp_more',
 299          'spellchecker',
 300          'wp_adv',
 301          'fullscreen',
 302          'alignleft',
 303          'alignright',
 304          'aligncenter',
 305          'formatselect',
 306      );
 307  
 308      // Remove unused buttons
 309      $buttons = array_diff( $buttons, $remove_buttons );
 310  
 311      // Add the image button
 312      array_push( $buttons, 'image' );
 313  
 314      return $buttons;
 315  }
 316  
 317  /**
 318   * @since 3.0.0
 319   */
 320  function bp_nouveau_messages_at_on_tinymce_init( $settings, $editor_id ) {
 321      // We only apply the mentions init to the visual post editor in the WP dashboard.
 322      if ( 'message_content' === $editor_id ) {
 323          $settings['init_instance_callback'] = 'window.bp.Nouveau.Messages.tinyMCEinit';
 324      }
 325  
 326      return $settings;
 327  }
 328  
 329  /**
 330   * @since 3.0.0
 331   */
 332  function bp_nouveau_get_message_date( $date ) {
 333      $now  = bp_core_current_time( true, 'timestamp' );
 334      $date = strtotime( $date );
 335  
 336      $now_date    = getdate( $now );
 337      $date_date   = getdate( $date );
 338      $compare     = array_diff( $date_date, $now_date );
 339      $date_format = 'Y/m/d';
 340  
 341      // Use Timezone string if set.
 342      $timezone_string = bp_get_option( 'timezone_string' );
 343      if ( ! empty( $timezone_string ) ) {
 344          $timezone_object = timezone_open( $timezone_string );
 345          $datetime_object = date_create( "@{$date}" );
 346          $timezone_offset = timezone_offset_get( $timezone_object, $datetime_object ) / HOUR_IN_SECONDS;
 347  
 348      // Fall back on less reliable gmt_offset
 349      } else {
 350          $timezone_offset = bp_get_option( 'gmt_offset' );
 351      }
 352  
 353      // Calculate time based on the offset
 354      $calculated_time = $date + ( $timezone_offset * HOUR_IN_SECONDS );
 355  
 356      if ( empty( $compare['mday'] ) && empty( $compare['mon'] ) && empty( $compare['year'] ) ) {
 357          $date_format = 'H:i';
 358  
 359      } elseif ( empty( $compare['mon'] ) || empty( $compare['year'] ) ) {
 360          $date_format = 'M j';
 361      }
 362  
 363      /**
 364       * Filters the message date for BuddyPress Nouveau display.
 365       *
 366       * @since 3.0.0
 367       *
 368       * @param string $value           Internationalization-ready formatted date value.
 369       * @param mixed  $calculated_time Calculated time.
 370       * @param string $date            Date value.
 371       * @param string $date_format     Format to convert the calcuated date to.
 372       */
 373      return apply_filters( 'bp_nouveau_get_message_date', date_i18n( $date_format, $calculated_time, true ), $calculated_time, $date, $date_format );
 374  }
 375  
 376  /**
 377   * @since 3.0.0
 378   */
 379  function bp_nouveau_messages_get_bulk_actions() {
 380      ob_start();
 381      bp_messages_bulk_management_dropdown();
 382  
 383      $bulk_actions = array();
 384      $bulk_options = ob_get_clean();
 385  
 386      $matched = preg_match_all( '/<option value="(.*?)"\s*>(.*?)<\/option>/', $bulk_options, $matches, PREG_SET_ORDER );
 387  
 388      if ( $matched && is_array( $matches ) ) {
 389          foreach ( $matches as $i => $match ) {
 390              if ( 0 === $i ) {
 391                  continue;
 392              }
 393  
 394              if ( isset( $match[1] ) && isset( $match[2] ) ) {
 395                  $bulk_actions[] = array(
 396                      'value' => trim( $match[1] ),
 397                      'label' => trim( $match[2] ),
 398                  );
 399              }
 400          }
 401      }
 402  
 403      return $bulk_actions;
 404  }
 405  
 406  /**
 407   * Register notifications filters for the messages component.
 408   *
 409   * @since 3.0.0
 410   */
 411  function bp_nouveau_messages_notification_filters() {
 412      bp_nouveau_notifications_register_filter(
 413          array(
 414              'id'       => 'new_message',
 415              'label'    => __( 'New private messages', 'buddypress' ),
 416              'position' => 115,
 417          )
 418      );
 419  }
 420  
 421  /**
 422   * Fires Messages Legacy hooks to catch the content and add them
 423   * as extra keys to the JSON Messages UI reply.
 424   *
 425   * @since 3.0.1
 426   *
 427   * @param array $hooks The list of hooks to fire.
 428   * @return array       An associative containing the caught content.
 429   */
 430  function bp_nouveau_messages_catch_hook_content( $hooks = array() ) {
 431      $content = array();
 432  
 433      ob_start();
 434      foreach ( $hooks as $js_key => $hook ) {
 435          if ( ! has_action( $hook ) ) {
 436              continue;
 437          }
 438  
 439          // Fire the hook.
 440          do_action( $hook );
 441  
 442          // Catch the sanitized content.
 443          $content[ $js_key ] = bp_strip_script_and_style_tags( ob_get_contents() );
 444  
 445          // Clean the buffer.
 446          ob_clean();
 447      }
 448      ob_end_clean();
 449  
 450      return $content;
 451  }


Generated: Sun Sep 20 01:01:32 2020 Cross-referenced by PHPXref 0.7.1