web/wp-content/plugins/bbpress/templates/default/bbpress-functions.php
changeset 196 5e8dcbe22c24
equal deleted inserted replaced
195:c7c0fbc09788 196:5e8dcbe22c24
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * Functions of bbPress's Default theme
       
     5  *
       
     6  * @package bbPress
       
     7  * @subpackage BBP_Theme_Compat
       
     8  * @since bbPress (r3732)
       
     9  */
       
    10 
       
    11 // Exit if accessed directly
       
    12 if ( !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 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 bbPress (r3732)
       
    43 	 *
       
    44 	 * @uses BBP_Default::setup_globals()
       
    45 	 * @uses BBP_Default::setup_actions()
       
    46 	 */
       
    47 	public function __construct() {
       
    48 		$this->setup_globals();
       
    49 		$this->setup_actions();
       
    50 	}
       
    51 
       
    52 	/**
       
    53 	 * Component global variables
       
    54 	 *
       
    55 	 * Note that this function is currently commented out in the constructor.
       
    56 	 * It will only be used if you copy this file into your current theme and
       
    57 	 * uncomment the line above.
       
    58 	 *
       
    59 	 * You'll want to customize the values in here, so they match whatever your
       
    60 	 * needs are.
       
    61 	 *
       
    62 	 * @since bbPress (r3732)
       
    63 	 * @access private
       
    64 	 */
       
    65 	private function setup_globals() {
       
    66 		$bbp           = bbpress();
       
    67 		$this->id      = 'default';
       
    68 		$this->name    = __( 'bbPress Default', 'bbpress' );
       
    69 		$this->version = bbp_get_version();
       
    70 		$this->dir     = trailingslashit( $bbp->themes_dir . 'default' );
       
    71 		$this->url     = trailingslashit( $bbp->themes_url . 'default' );
       
    72 	}
       
    73 
       
    74 	/**
       
    75 	 * Setup the theme hooks
       
    76 	 *
       
    77 	 * @since bbPress (r3732)
       
    78 	 * @access private
       
    79 	 *
       
    80 	 * @uses add_filter() To add various filters
       
    81 	 * @uses add_action() To add various actions
       
    82 	 */
       
    83 	private function setup_actions() {
       
    84 
       
    85 		/** Scripts ***********************************************************/
       
    86 
       
    87 		add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_styles'        ) ); // Enqueue theme CSS
       
    88 		add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_scripts'       ) ); // Enqueue theme JS
       
    89 		add_filter( 'bbp_enqueue_scripts',      array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization
       
    90 		add_action( 'bbp_head',                 array( $this, 'head_scripts'          ) ); // Output some extra JS in the <head>
       
    91 		add_action( 'wp_ajax_dim-favorite',     array( $this, 'ajax_favorite'         ) ); // Handles the ajax favorite/unfavorite
       
    92 		add_action( 'wp_ajax_dim-subscription', array( $this, 'ajax_subscription'     ) ); // Handles the ajax subscribe/unsubscribe
       
    93 
       
    94 		/** Template Wrappers *************************************************/
       
    95 
       
    96 		add_action( 'bbp_before_main_content',  array( $this, 'before_main_content'   ) ); // Top wrapper HTML
       
    97 		add_action( 'bbp_after_main_content',   array( $this, 'after_main_content'    ) ); // Bottom wrapper HTML
       
    98 
       
    99 		/** Override **********************************************************/
       
   100 
       
   101 		do_action_ref_array( 'bbp_theme_compat_actions', array( &$this ) );
       
   102 	}
       
   103 
       
   104 	/**
       
   105 	 * Inserts HTML at the top of the main content area to be compatible with
       
   106 	 * the Twenty Twelve theme.
       
   107 	 *
       
   108 	 * @since bbPress (r3732)
       
   109 	 */
       
   110 	public function before_main_content() {
       
   111 	?>
       
   112 
       
   113 		<div id="bbp-container">
       
   114 			<div id="bbp-content" role="main">
       
   115 
       
   116 	<?php
       
   117 	}
       
   118 
       
   119 	/**
       
   120 	 * Inserts HTML at the bottom of the main content area to be compatible with
       
   121 	 * the Twenty Twelve theme.
       
   122 	 *
       
   123 	 * @since bbPress (r3732)
       
   124 	 */
       
   125 	public function after_main_content() {
       
   126 	?>
       
   127 
       
   128 			</div><!-- #bbp-content -->
       
   129 		</div><!-- #bbp-container -->
       
   130 
       
   131 	<?php
       
   132 	}
       
   133 
       
   134 	/**
       
   135 	 * Load the theme CSS
       
   136 	 *
       
   137 	 * @since bbPress (r3732)
       
   138 	 *
       
   139 	 * @uses wp_enqueue_style() To enqueue the styles
       
   140 	 */
       
   141 	public function enqueue_styles() {
       
   142 
       
   143 		// LTR or RTL
       
   144 		$file = is_rtl() ? 'css/bbpress-rtl.css' : 'css/bbpress.css';
       
   145 
       
   146 		// Check child theme
       
   147 		if ( file_exists( trailingslashit( get_stylesheet_directory() ) . $file ) ) {
       
   148 			$location = trailingslashit( get_stylesheet_directory_uri() );
       
   149 			$handle   = 'bbp-child-bbpress';
       
   150 
       
   151 		// Check parent theme
       
   152 		} elseif ( file_exists( trailingslashit( get_template_directory() ) . $file ) ) {
       
   153 			$location = trailingslashit( get_template_directory_uri() );
       
   154 			$handle   = 'bbp-parent-bbpress';
       
   155 
       
   156 		// bbPress Theme Compatibility
       
   157 		} else {
       
   158 			$location = trailingslashit( $this->url );
       
   159 			$handle   = 'bbp-default-bbpress';
       
   160 		}
       
   161 
       
   162 		// Enqueue the bbPress styling
       
   163 		wp_enqueue_style( $handle, $location . $file, array(), $this->version, 'screen' );
       
   164 	}
       
   165 
       
   166 	/**
       
   167 	 * Enqueue the required Javascript files
       
   168 	 *
       
   169 	 * @since bbPress (r3732)
       
   170 	 *
       
   171 	 * @uses bbp_is_single_topic() To check if it's the topic page
       
   172 	 * @uses bbp_is_single_user_edit() To check if it's the profile edit page
       
   173 	 * @uses wp_enqueue_script() To enqueue the scripts
       
   174 	 */
       
   175 	public function enqueue_scripts() {
       
   176 
       
   177 		if ( bbp_is_single_topic() )
       
   178 			wp_enqueue_script( 'bbpress-topic', $this->url . 'js/topic.js', array( 'wp-lists' ), $this->version, true );
       
   179 
       
   180 		elseif ( bbp_is_single_user_edit() )
       
   181 			wp_enqueue_script( 'user-profile' );
       
   182 	}
       
   183 
       
   184 	/**
       
   185 	 * Put some scripts in the header, like AJAX url for wp-lists
       
   186 	 *
       
   187 	 * @since bbPress (r3732)
       
   188 	 *
       
   189 	 * @uses bbp_is_single_topic() To check if it's the topic page
       
   190 	 * @uses admin_url() To get the admin url
       
   191 	 * @uses bbp_is_single_user_edit() To check if it's the profile edit page
       
   192 	 */
       
   193 	public function head_scripts() {
       
   194 	?>
       
   195 
       
   196 		<script type="text/javascript">
       
   197 			/* <![CDATA[ */
       
   198 			var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
       
   199 
       
   200 			<?php if ( bbp_is_single_user_edit() ) : ?>
       
   201 			if ( window.location.hash == '#password' ) {
       
   202 				document.getElementById('pass1').focus();
       
   203 			}
       
   204 			<?php endif; ?>
       
   205 			/* ]]> */
       
   206 		</script>
       
   207 
       
   208 	<?php
       
   209 	}
       
   210 
       
   211 	/**
       
   212 	 * Load localizations for topic script
       
   213 	 *
       
   214 	 * These localizations require information that may not be loaded even by init.
       
   215 	 *
       
   216 	 * @since bbPress (r3732)
       
   217 	 *
       
   218 	 * @uses bbp_is_single_topic() To check if it's the topic page
       
   219 	 * @uses is_user_logged_in() To check if user is logged in
       
   220 	 * @uses bbp_get_current_user_id() To get the current user id
       
   221 	 * @uses bbp_get_topic_id() To get the topic id
       
   222 	 * @uses bbp_get_favorites_permalink() To get the favorites permalink
       
   223 	 * @uses bbp_is_user_favorite() To check if the topic is in user's favorites
       
   224 	 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
       
   225 	 * @uses bbp_is_user_subscribed() To check if the user is subscribed to topic
       
   226 	 * @uses bbp_get_topic_permalink() To get the topic permalink
       
   227 	 * @uses wp_localize_script() To localize the script
       
   228 	 */
       
   229 	public function localize_topic_script() {
       
   230 
       
   231 		// Bail if not viewing a single topic
       
   232 		if ( !bbp_is_single_topic() )
       
   233 			return;
       
   234 
       
   235 		// Bail if user is not logged in
       
   236 		if ( !is_user_logged_in() )
       
   237 			return;
       
   238 
       
   239 		$user_id = bbp_get_current_user_id();
       
   240 
       
   241 		$localizations = array(
       
   242 			'currentUserId' => $user_id,
       
   243 			'topicId'       => bbp_get_topic_id(),
       
   244 		);
       
   245 
       
   246 		// Favorites
       
   247 		if ( bbp_is_favorites_active() ) {
       
   248 			$localizations['favoritesActive'] = 1;
       
   249 			$localizations['favoritesLink']   = bbp_get_favorites_permalink( $user_id );
       
   250 			$localizations['isFav']           = (int) bbp_is_user_favorite( $user_id );
       
   251 			$localizations['favLinkYes']      = __( 'favorites',                                         'bbpress' );
       
   252 			$localizations['favLinkNo']       = __( '?',                                                 'bbpress' );
       
   253 			$localizations['favYes']          = __( 'This topic is one of your %favLinkYes% [%favDel%]', 'bbpress' );
       
   254 			$localizations['favNo']           = __( '%favAdd% (%favLinkNo%)',                            'bbpress' );
       
   255 			$localizations['favDel']          = __( '&times;',                                           'bbpress' );
       
   256 			$localizations['favAdd']          = __( 'Add this topic to your favorites',                  'bbpress' );
       
   257 		} else {
       
   258 			$localizations['favoritesActive'] = 0;
       
   259 		}
       
   260 
       
   261 		// Subscriptions
       
   262 		if ( bbp_is_subscriptions_active() ) {
       
   263 			$localizations['subsActive']   = 1;
       
   264 			$localizations['isSubscribed'] = (int) bbp_is_user_subscribed( $user_id );
       
   265 			$localizations['subsSub']      = __( 'Subscribe',   'bbpress' );
       
   266 			$localizations['subsUns']      = __( 'Unsubscribe', 'bbpress' );
       
   267 			$localizations['subsLink']     = bbp_get_topic_permalink();
       
   268 		} else {
       
   269 			$localizations['subsActive'] = 0;
       
   270 		}
       
   271 
       
   272 		wp_localize_script( 'bbpress-topic', 'bbpTopicJS', $localizations );
       
   273 	}
       
   274 
       
   275 	/**
       
   276 	 * Add or remove a topic from a user's favorites
       
   277 	 *
       
   278 	 * @since bbPress (r3732)
       
   279 	 *
       
   280 	 * @uses bbp_get_current_user_id() To get the current user id
       
   281 	 * @uses current_user_can() To check if the current user can edit the user
       
   282 	 * @uses bbp_get_topic() To get the topic
       
   283 	 * @uses check_ajax_referer() To verify the nonce & check the referer
       
   284 	 * @uses bbp_is_user_favorite() To check if the topic is user's favorite
       
   285 	 * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
       
   286 	 * @uses bbp_add_user_favorite() To add the topic from user's favorites
       
   287 	 */
       
   288 	public function ajax_favorite() {
       
   289 		$user_id = bbp_get_current_user_id();
       
   290 		$id      = intval( $_POST['id'] );
       
   291 
       
   292 		if ( !current_user_can( 'edit_user', $user_id ) )
       
   293 			die( '-1' );
       
   294 
       
   295 		$topic = bbp_get_topic( $id );
       
   296 
       
   297 		if ( empty( $topic ) )
       
   298 			die( '0' );
       
   299 
       
   300 		check_ajax_referer( 'toggle-favorite_' . $topic->ID );
       
   301 
       
   302 		if ( bbp_is_user_favorite( $user_id, $topic->ID ) ) {
       
   303 			if ( bbp_remove_user_favorite( $user_id, $topic->ID ) ) {
       
   304 				die( '1' );
       
   305 			}
       
   306 		} else {
       
   307 			if ( bbp_add_user_favorite( $user_id, $topic->ID ) ) {
       
   308 				die( '1' );
       
   309 			}
       
   310 		}
       
   311 
       
   312 		die( '0' );
       
   313 	}
       
   314 
       
   315 	/**
       
   316 	 * Subscribe/Unsubscribe a user from a topic
       
   317 	 *
       
   318 	 * @since bbPress (r3732)
       
   319 	 *
       
   320 	 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
       
   321 	 * @uses bbp_get_current_user_id() To get the current user id
       
   322 	 * @uses current_user_can() To check if the current user can edit the user
       
   323 	 * @uses bbp_get_topic() To get the topic
       
   324 	 * @uses check_ajax_referer() To verify the nonce & check the referer
       
   325 	 * @uses bbp_is_user_subscribed() To check if the topic is in user's
       
   326 	 *                                 subscriptions
       
   327 	 * @uses bbp_remove_user_subscriptions() To remove the topic from user's
       
   328 	 *                                        subscriptions
       
   329 	 * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions
       
   330 	 */
       
   331 	public function ajax_subscription() {
       
   332 		if ( !bbp_is_subscriptions_active() )
       
   333 			return;
       
   334 
       
   335 		$user_id = bbp_get_current_user_id();
       
   336 		$id      = intval( $_POST['id'] );
       
   337 
       
   338 		if ( !current_user_can( 'edit_user', $user_id ) )
       
   339 			die( '-1' );
       
   340 
       
   341 		$topic = bbp_get_topic( $id );
       
   342 
       
   343 		if ( empty( $topic ) )
       
   344 			die( '0' );
       
   345 
       
   346 		check_ajax_referer( 'toggle-subscription_' . $topic->ID );
       
   347 
       
   348 		if ( bbp_is_user_subscribed( $user_id, $topic->ID ) ) {
       
   349 			if ( bbp_remove_user_subscription( $user_id, $topic->ID ) ) {
       
   350 				die( '1' );
       
   351 			}
       
   352 		} else {
       
   353 			if ( bbp_add_user_subscription( $user_id, $topic->ID ) ) {
       
   354 				die( '1' );
       
   355 			}
       
   356 		}
       
   357 
       
   358 		die( '0' );
       
   359 	}
       
   360 }
       
   361 new BBP_Default();
       
   362 endif;