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


Generated: Tue Nov 19 01:01:36 2019 Cross-referenced by PHPXref 0.7.1