[ 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 3.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      if ( empty( $_POST['object'] ) ) {
  25          wp_send_json_error();
  26      }
  27  
  28      $object = sanitize_title( $_POST['object'] );
  29  
  30      // Bail if object is not an active component to prevent arbitrary file inclusion.
  31      if ( ! bp_is_active( $object ) ) {
  32          wp_send_json_error();
  33      }
  34  
  35      // Nonce check!
  36      if ( empty( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'bp_nouveau_' . $object ) ) {
  37          wp_send_json_error();
  38      }
  39  
  40      $result = array();
  41  
  42      if ( 'activity' === $object ) {
  43          $scope = '';
  44          if ( ! empty( $_POST['scope'] ) ) {
  45              $scope = sanitize_text_field( $_POST['scope'] );
  46          }
  47  
  48          // We need to calculate and return the feed URL for each scope.
  49          switch ( $scope ) {
  50              case 'friends':
  51                  $feed_url = bp_loggedin_user_domain() . bp_get_activity_slug() . '/friends/feed/';
  52                  break;
  53              case 'groups':
  54                  $feed_url = bp_loggedin_user_domain() . bp_get_activity_slug() . '/groups/feed/';
  55                  break;
  56              case 'favorites':
  57                  $feed_url = bp_loggedin_user_domain() . bp_get_activity_slug() . '/favorites/feed/';
  58                  break;
  59              case 'mentions':
  60                  $feed_url = bp_loggedin_user_domain() . bp_get_activity_slug() . '/mentions/feed/';
  61  
  62                  // Get user new mentions
  63                  $new_mentions = bp_get_user_meta( bp_loggedin_user_id(), 'bp_new_mentions', true );
  64  
  65                  // If we have some, include them into the returned json before deleting them
  66                  if ( is_array( $new_mentions ) ) {
  67                      $result['new_mentions'] = $new_mentions;
  68  
  69                      // Clear new mentions
  70                      bp_activity_clear_new_mentions( bp_loggedin_user_id() );
  71                  }
  72  
  73                  break;
  74              default:
  75                  $feed_url = bp_get_sitewide_activity_feed_link();
  76                  break;
  77          }
  78  
  79          /**
  80           * Filters the browser URL for the template loader.
  81           *
  82           * @since 3.0.0
  83           *
  84           * @param string $feed_url Template feed url.
  85           * @param string $scope    Current component scope.
  86           */
  87          $result['feed_url'] = apply_filters( 'bp_nouveau_ajax_object_template_loader', $feed_url, $scope );
  88      }
  89  
  90      /*
  91       * AJAX requests happen too early to be seen by bp_update_is_directory()
  92       * so we do it manually here to ensure templates load with the correct
  93       * context. Without this check, templates will load the 'single' version
  94       * of themselves rather than the directory version.
  95       */
  96      if ( ! bp_current_action() ) {
  97          bp_update_is_directory( true, bp_current_component() );
  98      }
  99  
 100      // Get the template path based on the 'template' variable via the AJAX request.
 101      $template = isset( $_POST['template'] ) ? wp_unslash( $_POST['template'] ) : '';
 102  
 103      switch ( $template ) {
 104          case 'group_members' :
 105          case 'groups/single/members' :
 106              $template_part = 'groups/single/members-loop.php';
 107          break;
 108  
 109          case 'group_requests' :
 110              $template_part = 'groups/single/requests-loop.php';
 111          break;
 112  
 113          case 'friend_requests' :
 114              $template_part = 'members/single/friends/requests-loop.php';
 115          break;
 116  
 117          case 'member_notifications' :
 118              $template_part = 'members/single/notifications/notifications-loop.php';
 119          break;
 120  
 121          default :
 122              $template_part = $object . '/' . $object . '-loop.php';
 123          break;
 124      }
 125  
 126      ob_start();
 127  
 128      $template_path = bp_locate_template( array( $template_part ), false );
 129  
 130      /**
 131       * Filters the server path for the template loader.
 132       *
 133       * @since 3.0.0
 134       *
 135       * @param string Template file path.
 136       */
 137      $template_path = apply_filters( 'bp_nouveau_object_template_path', $template_path );
 138  
 139      load_template( $template_path );
 140      $result['contents'] = ob_get_contents();
 141      ob_end_clean();
 142  
 143      // Locate the object template.
 144      wp_send_json_success( $result );
 145  }


Generated: Tue Oct 22 01:01:36 2019 Cross-referenced by PHPXref 0.7.1