[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-messages/ -> bp-messages-star.php (source)

   1  <?php
   2  /**
   3   * Functions related to starring private messages.
   4   *
   5   * @package BuddyPress
   6   * @subpackage MessagesStar
   7   * @since 2.3.0
   8   */
   9  
  10  // Exit if accessed directly.
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  /** UTILITY ****************************************************************/
  14  
  15  /**
  16   * Return the starred messages slug. Defaults to 'starred'.
  17   *
  18   * @since 2.3.0
  19   *
  20   * @return string
  21   */
  22  function bp_get_messages_starred_slug() {
  23  
  24      /**
  25       * Filters the starred message slug.
  26       *
  27       * @since 2.3.0
  28       *
  29       * @param string
  30       */
  31      return sanitize_title( apply_filters( 'bp_get_messages_starred_slug', 'starred' ) );
  32  }
  33  
  34  /**
  35   * Function to determine if a message ID is starred.
  36   *
  37   * @since 2.3.0
  38   *
  39   * @param  int $mid     The message ID. Please note that this isn't the message thread ID.
  40   * @param  int $user_id The user ID.
  41   * @return bool
  42   */
  43  function bp_messages_is_message_starred( $mid = 0, $user_id = 0 ) {
  44      if ( empty( $user_id ) ) {
  45          $user_id = bp_displayed_user_id();
  46      }
  47  
  48      if ( empty( $mid ) ) {
  49          return false;
  50      }
  51  
  52      $starred = array_flip( (array) bp_messages_get_meta( $mid, 'starred_by_user', false ) );
  53  
  54      return isset( $starred[ $user_id ] );
  55  }
  56  
  57  /**
  58   * Output the link or raw URL for starring or unstarring a message.
  59   *
  60   * @since 2.3.0
  61   *
  62   * @param array $args See bp_get_the_message_star_action_link() for full documentation.
  63   */
  64  function bp_the_message_star_action_link( $args = array() ) {
  65      echo bp_get_the_message_star_action_link( $args );
  66  }
  67      /**
  68       * Return the link or raw URL for starring or unstarring a message.
  69       *
  70       * @since 2.3.0
  71       *
  72       * @param array $args {
  73       *     Array of arguments.
  74       *     @type int    $user_id       The user ID. Defaults to the logged-in user ID.
  75       *     @type int    $thread_id     The message thread ID. Default: 0. If not zero, this takes precedence over
  76       *                                 $message_id.
  77       *     @type int    $message_id    The individual message ID. If on a single thread page, defaults to the
  78       *                                 current message ID in the message loop.
  79       *     @type bool   $url_only      Whether to return the URL only. If false, returns link with markup.
  80       *                                 Default: false.
  81       *     @type string $text_unstar   Link text for the 'unstar' action. Only applicable if $url_only is false.
  82       *     @type string $text_star     Link text for the 'star' action. Only applicable if $url_only is false.
  83       *     @type string $title_unstar  Link title for the 'unstar' action. Only applicable if $url_only is false.
  84       *     @type string $title_star    Link title for the 'star' action. Only applicable if $url_only is false.
  85       *     @type string $title_unstar_thread Link title for the 'unstar' action when displayed in a thread loop.
  86       *                                       Only applicable if $message_id is set and if $url_only is false.
  87       *     @type string $title_star_thread   Link title for the 'star' action when displayed in a thread loop.
  88       *                                       Only applicable if $message_id is set and if $url_only is false.
  89       * }
  90       * @return string
  91       */
  92  	function bp_get_the_message_star_action_link( $args = array() ) {
  93  
  94          // Default user ID.
  95          $user_id = bp_displayed_user_id()
  96              ? bp_displayed_user_id()
  97              : bp_loggedin_user_id();
  98  
  99          $r = bp_parse_args(
 100              $args,
 101              array(
 102                  'user_id'             => (int) $user_id,
 103                  'thread_id'           => 0,
 104                  'message_id'          => (int) bp_get_the_thread_message_id(),
 105                  'url_only'            => false,
 106                  'text_unstar'         => __( 'Unstar', 'buddypress' ),
 107                  'text_star'           => __( 'Star', 'buddypress' ),
 108                  'title_unstar'        => __( 'Starred', 'buddypress' ),
 109                  'title_star'          => __( 'Not starred', 'buddypress' ),
 110                  'title_unstar_thread' => __( 'Remove all starred messages in this thread', 'buddypress' ),
 111                  'title_star_thread'   => __( 'Star the first message in this thread', 'buddypress' ),
 112              ),
 113              'messages_star_action_link'
 114          );
 115  
 116          // Check user ID and determine base user URL.
 117          switch ( $r['user_id'] ) {
 118  
 119              // Current user.
 120              case bp_loggedin_user_id() :
 121                  $user_domain = bp_loggedin_user_domain();
 122                  break;
 123  
 124              // Displayed user.
 125              case bp_displayed_user_id() :
 126                  $user_domain = bp_displayed_user_domain();
 127                  break;
 128  
 129              // Empty or other.
 130              default :
 131                  $user_domain = bp_core_get_user_domain( $r['user_id'] );
 132                  break;
 133          }
 134  
 135          // Bail if no user domain was calculated.
 136          if ( empty( $user_domain ) ) {
 137              return '';
 138          }
 139  
 140          // Define local variables.
 141          $retval = $bulk_attr = '';
 142  
 143          // Thread ID.
 144          if ( (int) $r['thread_id'] > 0 ) {
 145  
 146              // See if we're in the loop.
 147              if ( bp_get_message_thread_id() == $r['thread_id'] ) {
 148  
 149                  // Grab all message ids.
 150                  $mids = wp_list_pluck( $GLOBALS['messages_template']->thread->messages, 'id' );
 151  
 152                  // Make sure order is ASC.
 153                  // Order is DESC when used in the thread loop by default.
 154                  $mids = array_reverse( $mids );
 155  
 156              // Pull up the thread.
 157              } else {
 158                  $thread = new BP_Messages_Thread( $r['thread_id'] );
 159                  $mids   = wp_list_pluck( $thread->messages, 'id' );
 160              }
 161  
 162              $is_starred = false;
 163              $message_id = 0;
 164              foreach ( $mids as $mid ) {
 165  
 166                  // Try to find the first msg that is starred in a thread.
 167                  if ( true === bp_messages_is_message_starred( $mid ) ) {
 168                      $is_starred = true;
 169                      $message_id = $mid;
 170                      break;
 171                  }
 172              }
 173  
 174              // No star, so default to first message in thread.
 175              if ( empty( $message_id ) ) {
 176                  $message_id = $mids[0];
 177              }
 178  
 179              $message_id = (int) $message_id;
 180  
 181              // Nonce.
 182              $nonce = wp_create_nonce( "bp-messages-star-{$message_id}" );
 183  
 184              if ( true === $is_starred ) {
 185                  $action    = 'unstar';
 186                  $bulk_attr = ' data-star-bulk="1"';
 187                  $retval    = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/all/';
 188              } else {
 189                  $action    = 'star';
 190                  $retval    = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/';
 191              }
 192  
 193              $title = $r["title_{$action}_thread"];
 194  
 195          // Message ID.
 196          } else {
 197              $message_id = (int) $r['message_id'];
 198              $is_starred = bp_messages_is_message_starred( $message_id );
 199              $nonce      = wp_create_nonce( "bp-messages-star-{$message_id}" );
 200  
 201              if ( true === $is_starred ) {
 202                  $action = 'unstar';
 203                  $retval = $user_domain . bp_get_messages_slug() . '/unstar/' . $message_id . '/' . $nonce . '/';
 204              } else {
 205                  $action = 'star';
 206                  $retval = $user_domain . bp_get_messages_slug() . '/star/' . $message_id . '/' . $nonce . '/';
 207              }
 208  
 209              $title = $r["title_{$action}"];
 210          }
 211  
 212          /**
 213           * Filters the star action URL for starring / unstarring a message.
 214           *
 215           * @since 2.3.0
 216           *
 217           * @param string $retval URL for starring / unstarring a message.
 218           * @param array  $r      Parsed link arguments. See $args in bp_get_the_message_star_action_link().
 219           */
 220          $retval = esc_url( apply_filters( 'bp_get_the_message_star_action_urlonly', $retval, $r ) );
 221          if ( true === (bool) $r['url_only'] ) {
 222              return $retval;
 223          }
 224  
 225          /**
 226           * Filters the star action link, including markup.
 227           *
 228           * @since 2.3.0
 229           *
 230           * @param string $retval Link for starring / unstarring a message, including markup.
 231           * @param array  $r      Parsed link arguments. See $args in bp_get_the_message_star_action_link().
 232           */
 233          return apply_filters( 'bp_get_the_message_star_action_link', '<a data-bp-tooltip="' . esc_attr( $title ) . '" class="bp-tooltip message-action-' . esc_attr( $action ) . '" data-star-status="' . esc_attr( $action ) .'" data-star-nonce="' . esc_attr( $nonce ) . '"' . $bulk_attr . ' data-message-id="' . esc_attr( (int) $message_id ) . '" href="' . $retval . '" role="button" aria-pressed="false"><span class="icon"></span> <span class="bp-screen-reader-text">' . $r['text_' . $action] . '</span></a>', $r );
 234      }
 235  
 236  /**
 237   * Save or delete star message meta according to a message's star status.
 238   *
 239   * @since 2.3.0
 240   *
 241   * @param array $args {
 242   *     Array of arguments.
 243   *     @type string $action     The star action. Either 'star' or 'unstar'. Default: 'star'.
 244   *     @type int    $thread_id  The message thread ID. Default: 0. If not zero, this takes precedence over
 245   *                              $message_id.
 246   *     @type int    $message_id The indivudal message ID to star or unstar.  Default: 0.
 247   *     @type int    $user_id    The user ID. Defaults to the logged-in user ID.
 248   *     @type bool   $bulk       Whether to mark all messages in a thread as a certain action. Only relevant
 249   *                              when $action is 'unstar' at the moment. Default: false.
 250   * }
 251   * @return bool
 252   */
 253  function bp_messages_star_set_action( $args = array() ) {
 254      $r = bp_parse_args(
 255          $args,
 256          array(
 257              'action'     => 'star',
 258              'thread_id'  => 0,
 259              'message_id' => 0,
 260              'user_id'    => bp_displayed_user_id(),
 261              'bulk'       => false,
 262          )
 263      );
 264  
 265      // Set thread ID.
 266      if ( ! empty( $r['thread_id'] ) ) {
 267          $thread_id = (int) $r['thread_id'];
 268      } else {
 269          $thread_id = messages_get_message_thread_id( $r['message_id'] );
 270      }
 271      if ( empty( $thread_id ) ) {
 272          return false;
 273      }
 274  
 275      // Check if user has access to thread.
 276      if( ! messages_check_thread_access( $thread_id, $r['user_id'] ) ) {
 277          return false;
 278      }
 279  
 280      $is_starred = bp_messages_is_message_starred( $r['message_id'], $r['user_id'] );
 281  
 282      // Star.
 283      if ( 'star' == $r['action'] ) {
 284          if ( true === $is_starred ) {
 285              return true;
 286          } else {
 287              bp_messages_add_meta( $r['message_id'], 'starred_by_user', $r['user_id'] );
 288              return true;
 289          }
 290      // Unstar.
 291      } else {
 292          // Unstar one message.
 293          if ( false === $r['bulk'] ) {
 294              if ( false === $is_starred ) {
 295                  return true;
 296              } else {
 297                  bp_messages_delete_meta( $r['message_id'], 'starred_by_user', $r['user_id'] );
 298                  return true;
 299              }
 300  
 301          // Unstar all messages in a thread.
 302          } else {
 303              $thread = new BP_Messages_Thread( $thread_id );
 304              $mids = wp_list_pluck( $thread->messages, 'id' );
 305  
 306              foreach ( $mids as $mid ) {
 307                  if ( true === bp_messages_is_message_starred( $mid, $r['user_id'] ) ) {
 308                      bp_messages_delete_meta( $mid, 'starred_by_user', $r['user_id'] );
 309                  }
 310              }
 311  
 312              return true;
 313          }
 314      }
 315  }
 316  
 317  /** HOOKS ****************************************************************/
 318  
 319  /**
 320   * Enqueues the dashicons font.
 321   *
 322   * The dashicons font is used for the star / unstar icon.
 323   *
 324   * @since 2.3.0
 325   */
 326  function bp_messages_star_enqueue_scripts() {
 327      if ( ! bp_is_user_messages() ) {
 328          return;
 329      }
 330  
 331      wp_enqueue_style( 'dashicons' );
 332  }
 333  add_action( 'bp_enqueue_scripts', 'bp_messages_star_enqueue_scripts' );
 334  
 335  /**
 336   * Add the "Add star" and "Remove star" options to the bulk management list.
 337   *
 338   * @since 2.3.0
 339   */
 340  function bp_messages_star_bulk_management_dropdown() {
 341  ?>
 342  
 343      <option value="star"><?php _e( 'Add star', 'buddypress' ); ?></option>
 344      <option value="unstar"><?php _e( 'Remove star', 'buddypress' ); ?></option>
 345  
 346  <?php
 347  }
 348  add_action( 'bp_messages_bulk_management_dropdown', 'bp_messages_star_bulk_management_dropdown', 1 );
 349  
 350  /**
 351   * Add CSS class for the current message depending on starred status.
 352   *
 353   * @since 2.3.0
 354   *
 355   * @param  array $retval Current CSS classes.
 356   * @return array
 357   */
 358  function bp_messages_star_message_css_class( $retval = array() ) {
 359      if ( true === bp_messages_is_message_starred( bp_get_the_thread_message_id() ) ) {
 360          $status = 'starred';
 361      } else {
 362          $status = 'not-starred';
 363      }
 364  
 365      // Add css class based on star status for the current message.
 366      $retval[] = "message-{$status}";
 367  
 368      return $retval;
 369  }
 370  add_filter( 'bp_get_the_thread_message_css_class', 'bp_messages_star_message_css_class' );
 371  
 372  /**
 373   * Filter message threads by those starred by the logged-in user.
 374   *
 375   * @since 2.3.0
 376   *
 377   * @param  array $r Current message thread arguments.
 378   * @return array $r Array of starred message threads.
 379   */
 380  function bp_messages_filter_starred_message_threads( $r = array() ) {
 381      $r['box'] = 'starred';
 382      $r['meta_query'] = array( array(
 383          'key'   => 'starred_by_user',
 384          'value' => $r['user_id']
 385      ) );
 386  
 387      return $r;
 388  }


Generated: Thu Apr 25 01:01:12 2024 Cross-referenced by PHPXref 0.7.1