[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/templates/default/ -> bbpress-functions.php (source)

   1  <?php
   2  
   3  /**
   4   * Functions of the Default template-pack
   5   *
   6   * @package bbPress
   7   * @subpackage BBP_Theme_Compat
   8   * @since 2.1.0 bbPress (r3732)
   9   */
  10  
  11  // Exit if accessed directly
  12  defined( 'ABSPATH' ) || exit;
  13  
  14  /** Theme Setup ***************************************************************/
  15  
  16  if ( ! class_exists( 'BBP_Default' ) ) :
  17  
  18  /**
  19   * Loads bbPress Default Theme functionality
  20   *
  21   * This is not a real theme by WordPress standards, and is instead used as the
  22   * fallback for any WordPress theme that does not have bbPress templates in it.
  23   *
  24   * To make your custom theme bbPress compatible and customize the templates, you
  25   * can copy these files into your theme without needing to merge anything
  26   * together; bbPress should safely handle the rest.
  27   *
  28   * See @link BBP_Theme_Compat() for more.
  29   *
  30   * @since 2.1.0 bbPress (r3732)
  31   *
  32   * @package bbPress
  33   * @subpackage BBP_Theme_Compat
  34   */
  35  class BBP_Default extends BBP_Theme_Compat {
  36  
  37      /** Functions *************************************************************/
  38  
  39      /**
  40       * The main bbPress (Default) Loader
  41       *
  42       * @since 2.1.0 bbPress (r3732)
  43       */
  44  	public function __construct( $properties = array() ) {
  45  
  46          parent::__construct( bbp_parse_args( $properties, array(
  47              'id'      => 'default',
  48              'name'    => 'bbPress Default',
  49              'version' => bbp_get_asset_version(),
  50              'dir'     => trailingslashit( bbpress()->themes_dir . 'default' ),
  51              'url'     => trailingslashit( bbpress()->themes_url . 'default' ),
  52          ), 'default_theme' ) );
  53  
  54          $this->setup_actions();
  55      }
  56  
  57      /**
  58       * Setup the theme hooks
  59       *
  60       * @since 2.1.0 bbPress (r3732)
  61       *
  62       * @access private
  63       */
  64  	private function setup_actions() {
  65  
  66          /** Scripts ***********************************************************/
  67  
  68          add_action( 'bbp_enqueue_scripts',         array( $this, 'enqueue_styles'        ) ); // Enqueue theme CSS
  69          add_action( 'bbp_enqueue_scripts',         array( $this, 'enqueue_scripts'       ) ); // Enqueue theme JS
  70          add_filter( 'bbp_enqueue_scripts',         array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization
  71          add_action( 'bbp_ajax_favorite',           array( $this, 'ajax_favorite'         ) ); // Handles the topic ajax favorite/unfavorite
  72          add_action( 'bbp_ajax_subscription',       array( $this, 'ajax_subscription'     ) ); // Handles the topic ajax subscribe/unsubscribe
  73  
  74          /** Template Wrappers *************************************************/
  75  
  76          add_action( 'bbp_before_main_content',  array( $this, 'before_main_content'   ) ); // Top wrapper HTML
  77          add_action( 'bbp_after_main_content',   array( $this, 'after_main_content'    ) ); // Bottom wrapper HTML
  78  
  79          /** Override **********************************************************/
  80  
  81          do_action_ref_array( 'bbp_theme_compat_actions', array( &$this ) );
  82      }
  83  
  84      /**
  85       * Inserts HTML at the top of the main content area to be compatible with
  86       * the Twenty Twelve theme.
  87       *
  88       * @since 2.1.0 bbPress (r3732)
  89       */
  90  	public function before_main_content() {
  91      ?>
  92  
  93          <div id="bbp-container">
  94              <div id="bbp-content" role="main">
  95  
  96      <?php
  97      }
  98  
  99      /**
 100       * Inserts HTML at the bottom of the main content area to be compatible with
 101       * the Twenty Twelve theme.
 102       *
 103       * @since 2.1.0 bbPress (r3732)
 104       */
 105  	public function after_main_content() {
 106      ?>
 107  
 108              </div><!-- #bbp-content -->
 109          </div><!-- #bbp-container -->
 110  
 111      <?php
 112      }
 113  
 114      /**
 115       * Load the theme CSS
 116       *
 117       * @since 2.1.0 bbPress (r3732)
 118       */
 119  	public function enqueue_styles() {
 120  
 121          // Setup the default styling
 122          $defaults = array(
 123              'bbp-default' => array(
 124                  'file'         => 'css/bbpress.css',
 125                  'dependencies' => array()
 126              )
 127          );
 128  
 129          // Optionally support an RTL variant
 130          if ( is_rtl() ) {
 131              $defaults['bbp-default-rtl'] = array(
 132                  'file'         => 'css/bbpress-rtl.css',
 133                  'dependencies' => array()
 134              );
 135          }
 136  
 137          // Get and filter the bbp-default style
 138          $styles = apply_filters( 'bbp_default_styles', $defaults );
 139  
 140          // Enqueue the styles
 141          foreach ( $styles as $handle => $attributes ) {
 142              bbp_enqueue_style( $handle, $attributes['file'], $attributes['dependencies'], $this->version );
 143          }
 144      }
 145  
 146      /**
 147       * Enqueue the required JavaScript files
 148       *
 149       * @since 2.1.0 bbPress (r3732)
 150       */
 151  	public function enqueue_scripts() {
 152  
 153          // Setup scripts array
 154          $scripts = array();
 155  
 156          // Editor scripts
 157          // @see https://bbpress.trac.wordpress.org/ticket/2930
 158          if ( bbp_use_wp_editor() && is_bbpress() ) {
 159              $scripts['bbpress-editor'] = array(
 160                  'file'         => 'js/editor.js',
 161                  'dependencies' => array( 'jquery' )
 162              );
 163          }
 164  
 165          // Forum-specific scripts
 166          if ( bbp_is_single_forum() ) {
 167              $scripts['bbpress-engagements'] = array(
 168                  'file'         => 'js/engagements.js',
 169                  'dependencies' => array( 'jquery' )
 170              );
 171          }
 172  
 173          // Topic-specific scripts
 174          if ( bbp_is_single_topic() || bbp_is_topic_edit() ) {
 175  
 176              // Engagements
 177              $scripts['bbpress-engagements'] = array(
 178                  'file'         => 'js/engagements.js',
 179                  'dependencies' => array( 'jquery' )
 180              );
 181  
 182              // Hierarchical replies
 183              if ( bbp_thread_replies() ) {
 184                  $scripts['bbpress-reply'] = array(
 185                      'file'         => 'js/reply.js',
 186                      'dependencies' => array( 'jquery' )
 187                  );
 188              }
 189          }
 190  
 191          // User Profile edit
 192          if ( bbp_is_single_user_edit() ) {
 193              wp_enqueue_script( 'user-profile' );
 194          }
 195  
 196          // Filter the scripts
 197          $scripts = apply_filters( 'bbp_default_scripts', $scripts );
 198  
 199          // Enqueue the scripts
 200          foreach ( $scripts as $handle => $attributes ) {
 201              bbp_enqueue_script( $handle, $attributes['file'], $attributes['dependencies'], $this->version, true );
 202          }
 203      }
 204  
 205      /**
 206       * Load localizations for topic script
 207       *
 208       * These localizations require information that may not be loaded even by init.
 209       *
 210       * @since 2.1.0 bbPress (r3732)
 211       */
 212  	public function localize_topic_script() {
 213  
 214          // Single forum or topic
 215          if ( bbp_is_single_forum() || bbp_is_single_topic() ) {
 216              wp_localize_script( 'bbpress-engagements', 'bbpEngagementJS', array(
 217                  'object_id'          => get_the_ID(),
 218                  'bbp_ajaxurl'        => bbp_get_ajax_url(),
 219                  'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
 220              ) );
 221          }
 222      }
 223  
 224      /**
 225       * AJAX handler to add or remove a topic from a user's favorites
 226       *
 227       * @since 2.1.0 bbPress (r3732)
 228       */
 229  	public function ajax_favorite() {
 230  
 231          // Bail if favorites are not active
 232          if ( ! bbp_is_favorites_active() ) {
 233              bbp_ajax_response( false, esc_html__( 'Favorites are no longer active.', 'bbpress' ), 300 );
 234          }
 235  
 236          // Bail if user is not logged in
 237          if ( ! is_user_logged_in() ) {
 238              bbp_ajax_response( false, esc_html__( 'Please login to favorite.', 'bbpress' ), 301 );
 239          }
 240  
 241          // Get user and topic data
 242          $user_id = bbp_get_current_user_id();
 243          $id      = ! empty( $_POST['id']   ) ? intval( $_POST['id'] )         : 0;
 244          $type    = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post';
 245  
 246          // Bail if user cannot add favorites for this user
 247          if ( ! current_user_can( 'edit_user', $user_id ) ) {
 248              bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 );
 249          }
 250  
 251          // Get the object
 252          if ( 'post' === $type ) {
 253              $object = get_post( $id );
 254          }
 255  
 256          // Bail if topic cannot be found
 257          if ( empty( $object ) ) {
 258              bbp_ajax_response( false, esc_html__( 'Favorite failed.', 'bbpress' ), 303 );
 259          }
 260  
 261          // Bail if user did not take this action
 262          if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $object->ID ) ) {
 263              bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
 264          }
 265  
 266          // Take action
 267          $status = bbp_is_user_favorite( $user_id, $object->ID )
 268              ? bbp_remove_user_favorite( $user_id, $object->ID )
 269              :    bbp_add_user_favorite( $user_id, $object->ID );
 270  
 271          // Bail if action failed
 272          if ( empty( $status ) ) {
 273              bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
 274          }
 275  
 276          // Put subscription attributes in convenient array
 277          $attrs = array(
 278              'object_id'   => $object->ID,
 279              'object_type' => $type,
 280              'user_id'     => $user_id
 281          );
 282  
 283          // Action succeeded
 284          bbp_ajax_response( true, bbp_get_user_favorites_link( $attrs, $user_id, false ), 200 );
 285      }
 286  
 287      /**
 288       * AJAX handler to Subscribe/Unsubscribe a user from a topic
 289       *
 290       * @since 2.1.0 bbPress (r3732)
 291       */
 292  	public function ajax_subscription() {
 293  
 294          // Bail if subscriptions are not active
 295          if ( ! bbp_is_subscriptions_active() ) {
 296              bbp_ajax_response( false, esc_html__( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
 297          }
 298  
 299          // Bail if user is not logged in
 300          if ( ! is_user_logged_in() ) {
 301              bbp_ajax_response( false, esc_html__( 'Please login to subscribe.', 'bbpress' ), 301 );
 302          }
 303  
 304          // Get user and topic data
 305          $user_id = bbp_get_current_user_id();
 306          $id      = ! empty( $_POST['id']   ) ? intval( $_POST['id'] )         : 0;
 307          $type    = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post';
 308  
 309          // Bail if user cannot add favorites for this user
 310          if ( ! current_user_can( 'edit_user', $user_id ) ) {
 311              bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 );
 312          }
 313  
 314          // Get the object
 315          if ( 'post' === $type ) {
 316              $object = get_post( $id );
 317          }
 318  
 319          // Bail if topic cannot be found
 320          if ( empty( $object ) ) {
 321              bbp_ajax_response( false, esc_html__( 'Subscription failed.', 'bbpress' ), 303 );
 322          }
 323  
 324          // Bail if user did not take this action
 325          if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $object->ID ) ) {
 326              bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
 327          }
 328  
 329          // Take action
 330          $status = bbp_is_user_subscribed( $user_id, $object->ID )
 331              ? bbp_remove_user_subscription( $user_id, $object->ID )
 332              :    bbp_add_user_subscription( $user_id, $object->ID );
 333  
 334          // Bail if action failed
 335          if ( empty( $status ) ) {
 336              bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
 337          }
 338  
 339          // Put subscription attributes in convenient array
 340          $attrs = array(
 341              'object_id'   => $object->ID,
 342              'object_type' => $type,
 343              'user_id'     => $user_id
 344          );
 345  
 346          // Add separator to topic if favorites is active
 347          if ( ( 'post' === $type ) && ( bbp_get_topic_post_type() === get_post_type( $object ) ) && bbp_is_favorites_active() ) {
 348              $attrs['before'] = '&nbsp;|&nbsp;';
 349          }
 350  
 351          // Action succeeded
 352          bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 );
 353      }
 354  }
 355  new BBP_Default();
 356  endif;


Generated: Tue Apr 23 01:01:01 2024 Cross-referenced by PHPXref 0.7.1