[ 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_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          // RTL and/or minified
 122          $suffix  = is_rtl() ? '-rtl' : '';
 123  
 124          // Get and filter the bbp-default style
 125          $styles = apply_filters( 'bbp_default_styles', array(
 126              'bbp-default' => array(
 127                  'file'         => 'css/bbpress' . $suffix . '.css',
 128                  'dependencies' => array()
 129              )
 130          ) );
 131  
 132          // Enqueue the styles
 133          foreach ( $styles as $handle => $attributes ) {
 134              bbp_enqueue_style( $handle, $attributes['file'], $attributes['dependencies'], $this->version );
 135          }
 136      }
 137  
 138      /**
 139       * Enqueue the required JavaScript files
 140       *
 141       * @since 2.1.0 bbPress (r3732)
 142       */
 143  	public function enqueue_scripts() {
 144  
 145          // Setup scripts array
 146          $scripts = array();
 147  
 148          // Editor scripts
 149          // @see https://bbpress.trac.wordpress.org/ticket/2930
 150          if ( bbp_use_wp_editor() && is_bbpress() ) {
 151              $scripts['bbpress-editor'] = array(
 152                  'file'         => 'js/editor.js',
 153                  'dependencies' => array( 'jquery' )
 154              );
 155          }
 156  
 157          // Forum-specific scripts
 158          if ( bbp_is_single_forum() ) {
 159              $scripts['bbpress-engagements'] = array(
 160                  'file'         => 'js/engagements.js',
 161                  'dependencies' => array( 'jquery' )
 162              );
 163          }
 164  
 165          // Topic-specific scripts
 166          if ( bbp_is_single_topic() || bbp_is_topic_edit() ) {
 167  
 168              // Engagements
 169              $scripts['bbpress-engagements'] = array(
 170                  'file'         => 'js/engagements.js',
 171                  'dependencies' => array( 'jquery' )
 172              );
 173  
 174              // Hierarchical replies
 175              if ( bbp_thread_replies() ) {
 176                  $scripts['bbpress-reply'] = array(
 177                      'file'         => 'js/reply.js',
 178                      'dependencies' => array( 'jquery' )
 179                  );
 180              }
 181          }
 182  
 183          // User Profile edit
 184          if ( bbp_is_single_user_edit() ) {
 185              wp_enqueue_script( 'user-profile' );
 186          }
 187  
 188          // Filter the scripts
 189          $scripts = apply_filters( 'bbp_default_scripts', $scripts );
 190  
 191          // Enqueue the scripts
 192          foreach ( $scripts as $handle => $attributes ) {
 193              bbp_enqueue_script( $handle, $attributes['file'], $attributes['dependencies'], $this->version, true );
 194          }
 195      }
 196  
 197      /**
 198       * Load localizations for topic script
 199       *
 200       * These localizations require information that may not be loaded even by init.
 201       *
 202       * @since 2.1.0 bbPress (r3732)
 203       */
 204  	public function localize_topic_script() {
 205  
 206          // Single forum or topic
 207          if ( bbp_is_single_forum() || bbp_is_single_topic() ) {
 208              wp_localize_script( 'bbpress-engagements', 'bbpEngagementJS', array(
 209                  'object_id'          => get_the_ID(),
 210                  'bbp_ajaxurl'        => bbp_get_ajax_url(),
 211                  'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
 212              ) );
 213          }
 214      }
 215  
 216      /**
 217       * AJAX handler to add or remove a topic from a user's favorites
 218       *
 219       * @since 2.1.0 bbPress (r3732)
 220       */
 221  	public function ajax_favorite() {
 222  
 223          // Bail if favorites are not active
 224          if ( ! bbp_is_favorites_active() ) {
 225              bbp_ajax_response( false, esc_html__( 'Favorites are no longer active.', 'bbpress' ), 300 );
 226          }
 227  
 228          // Bail if user is not logged in
 229          if ( ! is_user_logged_in() ) {
 230              bbp_ajax_response( false, esc_html__( 'Please login to favorite.', 'bbpress' ), 301 );
 231          }
 232  
 233          // Get user and topic data
 234          $user_id = bbp_get_current_user_id();
 235          $id      = ! empty( $_POST['id']   ) ? intval( $_POST['id'] )         : 0;
 236          $type    = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post';
 237  
 238          // Bail if user cannot add favorites for this user
 239          if ( ! current_user_can( 'edit_user', $user_id ) ) {
 240              bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 );
 241          }
 242  
 243          // Get the object
 244          if ( 'post' === $type ) {
 245              $object = get_post( $id );
 246          }
 247  
 248          // Bail if topic cannot be found
 249          if ( empty( $object ) ) {
 250              bbp_ajax_response( false, esc_html__( 'Favorite failed.', 'bbpress' ), 303 );
 251          }
 252  
 253          // Bail if user did not take this action
 254          if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $object->ID ) ) {
 255              bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
 256          }
 257  
 258          // Take action
 259          $status = bbp_is_user_favorite( $user_id, $object->ID )
 260              ? bbp_remove_user_favorite( $user_id, $object->ID )
 261              :    bbp_add_user_favorite( $user_id, $object->ID );
 262  
 263          // Bail if action failed
 264          if ( empty( $status ) ) {
 265              bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
 266          }
 267  
 268          // Put subscription attributes in convenient array
 269          $attrs = array(
 270              'object_id'   => $object->ID,
 271              'object_type' => $type,
 272              'user_id'     => $user_id
 273          );
 274  
 275          // Action succeeded
 276          bbp_ajax_response( true, bbp_get_user_favorites_link( $attrs, $user_id, false ), 200 );
 277      }
 278  
 279      /**
 280       * AJAX handler to Subscribe/Unsubscribe a user from a topic
 281       *
 282       * @since 2.1.0 bbPress (r3732)
 283       */
 284  	public function ajax_subscription() {
 285  
 286          // Bail if subscriptions are not active
 287          if ( ! bbp_is_subscriptions_active() ) {
 288              bbp_ajax_response( false, esc_html__( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
 289          }
 290  
 291          // Bail if user is not logged in
 292          if ( ! is_user_logged_in() ) {
 293              bbp_ajax_response( false, esc_html__( 'Please login to subscribe.', 'bbpress' ), 301 );
 294          }
 295  
 296          // Get user and topic data
 297          $user_id = bbp_get_current_user_id();
 298          $id      = ! empty( $_POST['id']   ) ? intval( $_POST['id'] )         : 0;
 299          $type    = ! empty( $_POST['type'] ) ? sanitize_key( $_POST['type'] ) : 'post';
 300  
 301          // Bail if user cannot add favorites for this user
 302          if ( ! current_user_can( 'edit_user', $user_id ) ) {
 303              bbp_ajax_response( false, esc_html__( 'You do not have permission to do this.', 'bbpress' ), 302 );
 304          }
 305  
 306          // Get the object
 307          if ( 'post' === $type ) {
 308              $object = get_post( $id );
 309          }
 310  
 311          // Bail if topic cannot be found
 312          if ( empty( $object ) ) {
 313              bbp_ajax_response( false, esc_html__( 'Subcription failed.', 'bbpress' ), 303 );
 314          }
 315  
 316          // Bail if user did not take this action
 317          if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $object->ID ) ) {
 318              bbp_ajax_response( false, esc_html__( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
 319          }
 320  
 321          // Take action
 322          $status = bbp_is_user_subscribed( $user_id, $object->ID )
 323              ? bbp_remove_user_subscription( $user_id, $object->ID )
 324              :    bbp_add_user_subscription( $user_id, $object->ID );
 325  
 326          // Bail if action failed
 327          if ( empty( $status ) ) {
 328              bbp_ajax_response( false, esc_html__( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
 329          }
 330  
 331          // Put subscription attributes in convenient array
 332          $attrs = array(
 333              'object_id'   => $object->ID,
 334              'object_type' => $type,
 335              'user_id'     => $user_id
 336          );
 337  
 338          // Add separator to topic if favorites is active
 339          if ( ( 'post' === $type ) && ( bbp_get_topic_post_type() === get_post_type( $object ) ) && bbp_is_favorites_active() ) {
 340              $attrs['before'] = '&nbsp;|&nbsp;';
 341          }
 342  
 343          // Action succeeded
 344          bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 );
 345      }
 346  }
 347  new BBP_Default();
 348  endif;


Generated: Sun Nov 17 01:01:25 2019 Cross-referenced by PHPXref 0.7.1