[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-activity/screens/ -> permalink.php (source)

   1  <?php
   2  /**
   3   * Activity: Single permalink screen handler
   4   *
   5   * @package BuddyPress
   6   * @subpackage ActivityScreens
   7   * @since 3.0.0
   8   */
   9  
  10  /**
  11   * Catch and route requests for single activity item permalinks.
  12   *
  13   * @since 1.2.0
  14   *
  15   * @return bool False on failure.
  16   */
  17  function bp_activity_action_permalink_router() {
  18      // Not viewing activity.
  19      if ( ! bp_is_activity_component() || ! bp_is_current_action( 'p' ) )
  20          return false;
  21  
  22      // No activity to display.
  23      if ( ! bp_action_variable( 0 ) || ! is_numeric( bp_action_variable( 0 ) ) )
  24          return false;
  25  
  26      // Get the activity details.
  27      $activity = bp_activity_get_specific( array( 'activity_ids' => bp_action_variable( 0 ), 'show_hidden' => true ) );
  28  
  29      // 404 if activity does not exist.
  30      if ( empty( $activity['activities'][0] ) ) {
  31          bp_do_404();
  32          return;
  33      } else {
  34          $activity = $activity['activities'][0];
  35      }
  36  
  37      // Do not redirect at default.
  38      $redirect = false;
  39  
  40      // Redirect based on the type of activity.
  41      if ( bp_is_active( 'groups' ) && $activity->component == buddypress()->groups->id ) {
  42  
  43          // Activity is a user update.
  44          if ( ! empty( $activity->user_id ) ) {
  45              $redirect = bp_core_get_user_domain( $activity->user_id, $activity->user_nicename, $activity->user_login ) . bp_get_activity_slug() . '/' . $activity->id . '/';
  46  
  47          // Activity is something else.
  48          } else {
  49  
  50              // Set redirect to group activity stream.
  51              if ( $group = groups_get_group( $activity->item_id ) ) {
  52                  $redirect = bp_get_group_permalink( $group ) . bp_get_activity_slug() . '/' . $activity->id . '/';
  53              }
  54          }
  55  
  56      // Set redirect to users' activity stream.
  57      } elseif ( ! empty( $activity->user_id ) ) {
  58          $redirect = bp_core_get_user_domain( $activity->user_id, $activity->user_nicename, $activity->user_login ) . bp_get_activity_slug() . '/' . $activity->id . '/';
  59      }
  60  
  61      // If set, add the original query string back onto the redirect URL.
  62      if ( ! empty( $_SERVER['QUERY_STRING'] ) ) {
  63          $query_frags = array();
  64          wp_parse_str( $_SERVER['QUERY_STRING'], $query_frags );
  65          $redirect = add_query_arg( urlencode_deep( $query_frags ), $redirect );
  66      }
  67  
  68      /**
  69       * Filter the intended redirect url before the redirect occurs for the single activity item.
  70       *
  71       * @since 1.2.2
  72       *
  73       * @param array $value Array with url to redirect to and activity related to the redirect.
  74       */
  75      if ( ! $redirect = apply_filters_ref_array( 'bp_activity_permalink_redirect_url', array( $redirect, &$activity ) ) ) {
  76          bp_core_redirect( bp_get_root_domain() );
  77      }
  78  
  79      // Redirect to the actual activity permalink page.
  80      bp_core_redirect( $redirect );
  81  }
  82  add_action( 'bp_actions', 'bp_activity_action_permalink_router' );
  83  
  84  /**
  85   * Load the page for a single activity item.
  86   *
  87   * @since 1.2.0
  88   *
  89   * @return bool|string Boolean on false or the template for a single activity item on success.
  90   */
  91  function bp_activity_screen_single_activity_permalink() {
  92      // No displayed user or not viewing activity component.
  93      if ( ! bp_is_activity_component() ) {
  94          return false;
  95      }
  96  
  97      $action = bp_current_action();
  98      if ( ! $action || ! is_numeric( $action ) ) {
  99          return false;
 100      }
 101  
 102      // Get the activity details.
 103      $activity = bp_activity_get_specific( array(
 104          'activity_ids' => $action,
 105          'show_hidden'  => true,
 106          'spam'         => 'ham_only',
 107      ) );
 108  
 109      // 404 if activity does not exist.
 110      if ( empty( $activity['activities'][0] ) || bp_action_variables() ) {
 111          bp_do_404();
 112          return;
 113  
 114      } else {
 115          $activity = $activity['activities'][0];
 116      }
 117  
 118      /**
 119       * Check user access to the activity item.
 120       *
 121       * @since 3.0.0
 122       */
 123      $has_access = bp_activity_user_can_read( $activity );
 124  
 125      // If activity author does not match displayed user, block access.
 126      // More info:https://buddypress.trac.wordpress.org/ticket/7048#comment:28
 127      if ( true === $has_access && bp_displayed_user_id() !== $activity->user_id ) {
 128          $has_access = false;
 129      }
 130  
 131      /**
 132       * Fires before the loading of a single activity template file.
 133       *
 134       * @since 1.2.0
 135       *
 136       * @param BP_Activity_Activity $activity   Object representing the current activity item being displayed.
 137       * @param bool                 $has_access Whether or not the current user has access to view activity.
 138       */
 139      do_action( 'bp_activity_screen_single_activity_permalink', $activity, $has_access );
 140  
 141      // Access is specifically disallowed.
 142      if ( false === $has_access ) {
 143          // If not logged in, prompt for login.
 144          if ( ! is_user_logged_in() ) {
 145              bp_core_no_access();
 146  
 147          // Redirect away.
 148          } else {
 149              bp_core_add_message( __( 'You do not have access to this activity.', 'buddypress' ), 'error' );
 150              bp_core_redirect( bp_loggedin_user_domain() );
 151          }
 152      }
 153  
 154      /**
 155       * Filters the template to load for a single activity screen.
 156       *
 157       * @since 1.0.0
 158       *
 159       * @param string $template Path to the activity template to load.
 160       */
 161      $template = apply_filters( 'bp_activity_template_profile_activity_permalink', 'members/single/activity/permalink' );
 162  
 163      // Load the template.
 164      bp_core_load_template( $template );
 165  }
 166  add_action( 'bp_screens', 'bp_activity_screen_single_activity_permalink' );


Generated: Thu Nov 21 01:00:57 2024 Cross-referenced by PHPXref 0.7.1