[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-templates/bp-nouveau/includes/ -> ajax.php (source)

   1  <?php
   2  /**
   3   * Common functions only loaded on AJAX requests.
   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   * Load the template loop for the current object.
  14   *
  15   * @since 3.0.0
  16   *
  17   * @return string Template loop for the specified object
  18   */
  19  function bp_nouveau_ajax_object_template_loader() {
  20      if ( ! bp_is_post_request() ) {
  21          wp_send_json_error();
  22      }
  23  
  24      $post_vars = bp_parse_args(
  25          $_POST,
  26          array(
  27              'action'   => '',
  28              'object'   => '',
  29              'scope'    => '',
  30              'filter'   => '',
  31              'nonce'    => '',
  32              'template' => '',
  33          )
  34      );
  35  
  36      $object = sanitize_title( $post_vars['object'] );
  37  
  38      // Bail if object is not an active component to prevent arbitrary file inclusion.
  39      if ( ! bp_is_active( $object ) ) {
  40          wp_send_json_error();
  41      }
  42  
  43      // Nonce check!
  44      if ( ! $post_vars['nonce'] || ! wp_verify_nonce( $post_vars['nonce'], 'bp_nouveau_' . $object ) ) {
  45          wp_send_json_error();
  46      }
  47  
  48      $result = array();
  49  
  50      if ( 'activity' === $object ) {
  51          $scope = '';
  52          if ( $post_vars['scope'] ) {
  53              $scope = sanitize_text_field( $post_vars['scope'] );
  54          }
  55  
  56          // We need to calculate and return the feed URL for each scope.
  57          switch ( $scope ) {
  58              case 'friends':
  59                  $feed_url = bp_loggedin_user_domain() . bp_nouveau_get_component_slug( 'activity' ) . '/friends/feed/';
  60                  break;
  61              case 'groups':
  62                  $feed_url = bp_loggedin_user_domain() . bp_nouveau_get_component_slug( 'activity' ) . '/groups/feed/';
  63                  break;
  64              case 'favorites':
  65                  $feed_url = bp_loggedin_user_domain() . bp_nouveau_get_component_slug( 'activity' ) . '/favorites/feed/';
  66                  break;
  67              case 'mentions':
  68                  $feed_url = bp_loggedin_user_domain() . bp_nouveau_get_component_slug( 'activity' ) . '/mentions/feed/';
  69  
  70                  // Get user new mentions
  71                  $new_mentions = bp_get_user_meta( bp_loggedin_user_id(), 'bp_new_mentions', true );
  72  
  73                  // If we have some, include them into the returned json before deleting them
  74                  if ( is_array( $new_mentions ) ) {
  75                      $result['new_mentions'] = $new_mentions;
  76  
  77                      // Clear new mentions
  78                      bp_activity_clear_new_mentions( bp_loggedin_user_id() );
  79                  }
  80  
  81                  break;
  82              default:
  83                  $feed_url = bp_get_sitewide_activity_feed_link();
  84                  break;
  85          }
  86  
  87          /**
  88           * Filters the browser URL for the template loader.
  89           *
  90           * @since 3.0.0
  91           *
  92           * @param string $feed_url Template feed url.
  93           * @param string $scope    Current component scope.
  94           */
  95          $result['feed_url'] = apply_filters( 'bp_nouveau_ajax_object_template_loader', $feed_url, $scope );
  96      }
  97  
  98      /*
  99       * AJAX requests happen too early to be seen by bp_update_is_directory()
 100       * so we do it manually here to ensure templates load with the correct
 101       * context. Without this check, templates will load the 'single' version
 102       * of themselves rather than the directory version.
 103       */
 104      if ( ! bp_current_action() ) {
 105          bp_update_is_directory( true, bp_current_component() );
 106      }
 107  
 108      // Get the template path based on the 'template' variable via the AJAX request.
 109      $template = '';
 110      if ( $post_vars['template'] ) {
 111          $template = wp_unslash( $post_vars['template'] );
 112      }
 113  
 114      switch ( $template ) {
 115          case 'group_members' :
 116          case 'groups/single/members' :
 117              $template_part = 'groups/single/members-loop.php';
 118          break;
 119  
 120          case 'group_requests' :
 121              $template_part = 'groups/single/requests-loop.php';
 122          break;
 123  
 124          case 'friend_requests' :
 125              $template_part = 'members/single/friends/requests-loop.php';
 126          break;
 127  
 128          case 'member_notifications' :
 129              $template_part = 'members/single/notifications/notifications-loop.php';
 130          break;
 131  
 132          default :
 133              $template_part = $object . '/' . $object . '-loop.php';
 134          break;
 135      }
 136  
 137      ob_start();
 138  
 139      $template_path = bp_locate_template( array( $template_part ), false );
 140  
 141      /**
 142       * Filters the server path for the template loader.
 143       *
 144       * @since 3.0.0
 145       *
 146       * @param string Template file path.
 147       */
 148      $template_path = apply_filters( 'bp_nouveau_object_template_path', $template_path );
 149  
 150      load_template( $template_path );
 151      $result['contents'] = ob_get_contents();
 152      ob_end_clean();
 153  
 154      /**
 155       * Add additional info to the Ajax response.
 156       *
 157       * @since 10.0.0
 158       *
 159       * @param array $value     An associative array with additional information to include in the Ajax response.
 160       * @param array $post_vars An associative array containing the Ajax request arguments.
 161       */
 162      $additional_info = apply_filters( "bp_nouveau_{$object}_ajax_object_template_response", array(), $post_vars );
 163      if ( $additional_info ) {
 164          // Prevents content overrides.
 165          if ( isset( $additional_info['contents'] ) ) {
 166              unset( $additional_info['contents'] );
 167          }
 168  
 169          $result = array_merge( $result, $additional_info );
 170      }
 171  
 172      // Locate the object template.
 173      wp_send_json_success( $result );
 174  }


Generated: Sun Nov 24 01:00:53 2024 Cross-referenced by PHPXref 0.7.1