web/wp-content/plugins/bbpress/includes/admin/functions.php
changeset 196 5e8dcbe22c24
equal deleted inserted replaced
195:c7c0fbc09788 196:5e8dcbe22c24
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * bbPress Admin Functions
       
     5  *
       
     6  * @package bbPress
       
     7  * @subpackage Administration
       
     8  */
       
     9 
       
    10 // Exit if accessed directly
       
    11 if ( !defined( 'ABSPATH' ) ) exit;
       
    12 
       
    13 /** Admin Menus ***************************************************************/
       
    14 
       
    15 /**
       
    16  * Add a separator to the WordPress admin menus
       
    17  *
       
    18  * @since bbPress (r2957)
       
    19  */
       
    20 function bbp_admin_separator() {
       
    21 
       
    22 	// Prevent duplicate separators when no new menu items exist
       
    23 	if ( !current_user_can( 'edit_forums' ) && !current_user_can( 'edit_topics' ) && !current_user_can( 'edit_replies' ) )
       
    24 		return;
       
    25 
       
    26 	// Prevent duplicate separators when no core menu items exist
       
    27 	if ( !current_user_can( 'manage_options' ) )
       
    28 		return;
       
    29 
       
    30 	global $menu;
       
    31 
       
    32 	$menu[] = array( '', 'read', 'separator-bbpress', '', 'wp-menu-separator bbpress' );
       
    33 }
       
    34 
       
    35 /**
       
    36  * Tell WordPress we have a custom menu order
       
    37  *
       
    38  * @since bbPress (r2957)
       
    39  *
       
    40  * @param bool $menu_order Menu order
       
    41  * @return bool Always true
       
    42  */
       
    43 function bbp_admin_custom_menu_order( $menu_order = false ) {
       
    44 	if ( !current_user_can( 'edit_forums' ) && !current_user_can( 'edit_topics' ) && !current_user_can( 'edit_replies' ) )
       
    45 		return $menu_order;
       
    46 
       
    47 	return true;
       
    48 }
       
    49 
       
    50 /**
       
    51  * Move our custom separator above our custom post types
       
    52  *
       
    53  * @since bbPress (r2957)
       
    54  *
       
    55  * @param array $menu_order Menu Order
       
    56  * @uses bbp_get_forum_post_type() To get the forum post type
       
    57  * @return array Modified menu order
       
    58  */
       
    59 function bbp_admin_menu_order( $menu_order ) {
       
    60 
       
    61 	// Bail if user cannot see any top level bbPress menus
       
    62 	if ( empty( $menu_order ) || ( !current_user_can( 'edit_forums' ) && !current_user_can( 'edit_topics' ) && !current_user_can( 'edit_replies' ) ) )
       
    63 		return $menu_order;
       
    64 
       
    65 	// Initialize our custom order array
       
    66 	$bbp_menu_order = array();
       
    67 
       
    68 	// Menu values
       
    69 	$second_sep   = 'separator2';
       
    70 	$custom_menus = array(
       
    71 		'separator-bbpress',                               // Separator
       
    72 		'edit.php?post_type=' . bbp_get_forum_post_type(), // Forums
       
    73 		'edit.php?post_type=' . bbp_get_topic_post_type(), // Topics
       
    74 		'edit.php?post_type=' . bbp_get_reply_post_type()  // Replies
       
    75 	);
       
    76 
       
    77 	// Loop through menu order and do some rearranging
       
    78 	foreach ( $menu_order as $item ) {
       
    79 
       
    80 		// Position bbPress menus above appearance
       
    81 		if ( $second_sep == $item ) {
       
    82 
       
    83 			// Add our custom menus
       
    84 			foreach( $custom_menus as $custom_menu ) {
       
    85 				if ( array_search( $custom_menu, $menu_order ) ) {
       
    86 					$bbp_menu_order[] = $custom_menu;
       
    87 				}
       
    88 			}
       
    89 
       
    90 			// Add the appearance separator
       
    91 			$bbp_menu_order[] = $second_sep;
       
    92 
       
    93 		// Skip our menu items
       
    94 		} elseif ( ! in_array( $item, $custom_menus ) ) {
       
    95 			$bbp_menu_order[] = $item;
       
    96 		}
       
    97 	}
       
    98 
       
    99 	// Return our custom order
       
   100 	return $bbp_menu_order;
       
   101 }
       
   102 
       
   103 /**
       
   104  * Filter sample permalinks so that certain languages display properly.
       
   105  *
       
   106  * @since bbPress (r3336)
       
   107  *
       
   108  * @param string $post_link Custom post type permalink
       
   109  * @param object $_post Post data object
       
   110  * @param bool $leavename Optional, defaults to false. Whether to keep post name or page name.
       
   111  * @param bool $sample Optional, defaults to false. Is it a sample permalink.
       
   112  *
       
   113  * @uses is_admin() To make sure we're on an admin page
       
   114  * @uses bbp_is_custom_post_type() To get the forum post type
       
   115  *
       
   116  * @return string The custom post type permalink
       
   117  */
       
   118 function bbp_filter_sample_permalink( $post_link, $_post, $leavename = false, $sample = false ) {
       
   119 
       
   120 	// Bail if not on an admin page and not getting a sample permalink
       
   121 	if ( !empty( $sample ) && is_admin() && bbp_is_custom_post_type() )
       
   122 		return urldecode( $post_link );
       
   123 
       
   124 	// Return post link
       
   125 	return $post_link;
       
   126 }
       
   127 
       
   128 /**
       
   129  * Uninstall all bbPress options and capabilities from a specific site.
       
   130  *
       
   131  * @since bbPress (r3765)
       
   132  * @param type $site_id
       
   133  */
       
   134 function bbp_do_uninstall( $site_id = 0 ) {
       
   135 	if ( empty( $site_id ) )
       
   136 		$site_id = get_current_blog_id();
       
   137 
       
   138 	switch_to_blog( $site_id );
       
   139 	bbp_delete_options();
       
   140 	bbp_remove_caps();
       
   141 	flush_rewrite_rules();
       
   142 	restore_current_blog();
       
   143 }
       
   144 
       
   145 /**
       
   146  * Redirect user to bbPress's What's New page on activation
       
   147  *
       
   148  * @since bbPress (r4389)
       
   149  *
       
   150  * @internal Used internally to redirect bbPress to the about page on activation
       
   151  *
       
   152  * @uses get_transient() To see if transient to redirect exists
       
   153  * @uses delete_transient() To delete the transient if it exists
       
   154  * @uses is_network_admin() To bail if being network activated
       
   155  * @uses wp_safe_redirect() To redirect
       
   156  * @uses add_query_arg() To help build the URL to redirect to
       
   157  * @uses admin_url() To get the admin URL to index.php
       
   158  *
       
   159  * @return If no transient, or in network admin, or is bulk activation
       
   160  */
       
   161 function bbp_do_activation_redirect() {
       
   162 
       
   163 	// Bail if no activation redirect
       
   164     if ( ! get_transient( '_bbp_activation_redirect' ) )
       
   165 		return;
       
   166 
       
   167 	// Delete the redirect transient
       
   168 	delete_transient( '_bbp_activation_redirect' );
       
   169 
       
   170 	// Bail if activating from network, or bulk
       
   171 	if ( is_network_admin() || isset( $_GET['activate-multi'] ) )
       
   172 		return;
       
   173 
       
   174 	// Redirect to bbPress about page
       
   175 	wp_safe_redirect( add_query_arg( array( 'page' => 'bbp-about' ), admin_url( 'index.php' ) ) );
       
   176 }
       
   177 
       
   178 /**
       
   179  * This tells WP to highlight the Tools > Forums menu item,
       
   180  * regardless of which actual bbPress Tools screen we are on.
       
   181  *
       
   182  * The conditional prevents the override when the user is viewing settings or
       
   183  * any third-party plugins.
       
   184  *
       
   185  * @since bbPress (r3888)
       
   186  * @global string $plugin_page
       
   187  * @global array $submenu_file
       
   188  */
       
   189 function bbp_tools_modify_menu_highlight() {
       
   190 	global $plugin_page, $submenu_file;
       
   191 
       
   192 	// This tweaks the Tools subnav menu to only show one bbPress menu item
       
   193 	if ( ! in_array( $plugin_page, array( 'bbp-settings' ) ) )
       
   194 		$submenu_file = 'bbp-repair';
       
   195 }
       
   196 
       
   197 /**
       
   198  * Output the tabs in the admin area
       
   199  *
       
   200  * @since bbPress (r3872)
       
   201  * @param string $active_tab Name of the tab that is active
       
   202  */
       
   203 function bbp_tools_admin_tabs( $active_tab = '' ) {
       
   204 	echo bbp_get_tools_admin_tabs( $active_tab );
       
   205 }
       
   206 
       
   207 	/**
       
   208 	 * Output the tabs in the admin area
       
   209 	 *
       
   210 	 * @since bbPress (r3872)
       
   211 	 * @param string $active_tab Name of the tab that is active
       
   212 	 */
       
   213 	function bbp_get_tools_admin_tabs( $active_tab = '' ) {
       
   214 
       
   215 		// Declare local variables
       
   216 		$tabs_html    = '';
       
   217 		$idle_class   = 'nav-tab';
       
   218 		$active_class = 'nav-tab nav-tab-active';
       
   219 
       
   220 		// Setup core admin tabs
       
   221 		$tabs = apply_filters( 'bbp_tools_admin_tabs', array(
       
   222 			'0' => array(
       
   223 				'href' => get_admin_url( '', add_query_arg( array( 'page' => 'bbp-repair'    ), 'tools.php' ) ),
       
   224 				'name' => __( 'Repair Forums', 'bbpress' )
       
   225 			),
       
   226 			'1' => array(
       
   227 				'href' => get_admin_url( '', add_query_arg( array( 'page' => 'bbp-converter' ), 'tools.php' ) ),
       
   228 				'name' => __( 'Import Forums', 'bbpress' )
       
   229 			),
       
   230 			'2' => array(
       
   231 				'href' => get_admin_url( '', add_query_arg( array( 'page' => 'bbp-reset'     ), 'tools.php' ) ),
       
   232 				'name' => __( 'Reset Forums', 'bbpress' )
       
   233 			)
       
   234 		) );
       
   235 
       
   236 		// Loop through tabs and build navigation
       
   237 		foreach( $tabs as $tab_id => $tab_data ) {
       
   238 			$is_current = (bool) ( $tab_data['name'] == $active_tab );
       
   239 			$tab_class  = $is_current ? $active_class : $idle_class;
       
   240 			$tabs_html .= '<a href="' . $tab_data['href'] . '" class="' . $tab_class . '">' . $tab_data['name'] . '</a>';
       
   241 		}
       
   242 
       
   243 		// Output the tabs
       
   244 		return $tabs_html;
       
   245 	}