| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress Admin Functions. |
| 5 |
* |
| 6 |
* @package bbPress |
| 7 |
* @subpackage Administration |
| 8 |
*/ |
| 9 |
|
| 10 |
// Exit if accessed directly |
| 11 |
defined( 'ABSPATH' ) || exit; |
| 12 |
|
| 13 |
/** Admin Menus ***************************************************************/ |
| 14 |
|
| 15 |
/** |
| 16 |
* Add a separator to the WordPress admin menus. |
| 17 |
* |
| 18 |
* @since 2.0.0 bbPress (r2957) |
| 19 |
*/ |
| 20 |
function bbp_admin_separator() { |
| 21 |
|
| 22 |
// Caps necessary where a separator is necessary |
| 23 |
$caps = array( |
| 24 |
'bbp_forums_admin', |
| 25 |
'bbp_topics_admin', |
| 26 |
'bbp_replies_admin', |
| 27 |
); |
| 28 |
|
| 29 |
// Loop through caps, and look for a reason to show the separator |
| 30 |
foreach ( $caps as $cap ) { |
| 31 |
if ( current_user_can( $cap ) ) { |
| 32 |
bbp_admin()->show_separator = true; |
| 33 |
break; |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
// Bail if no separator |
| 38 |
if ( false === bbp_admin()->show_separator ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
global $menu; |
| 43 |
|
| 44 |
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 45 |
$menu[] = array( '', 'read', 'separator-bbpress', '', 'wp-menu-separator bbpress' ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Tell WordPress we have a custom menu order. |
| 50 |
* |
| 51 |
* @since 2.0.0 bbPress (r2957) |
| 52 |
* |
| 53 |
* @param bool $menu_order Menu order. |
| 54 |
* @return mixed True if separator, false if not. |
| 55 |
*/ |
| 56 |
function bbp_admin_custom_menu_order( $menu_order = false ) { |
| 57 |
if ( false === bbp_admin()->show_separator ) { |
| 58 |
return $menu_order; |
| 59 |
} |
| 60 |
|
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Move our custom separator above our custom post types. |
| 66 |
* |
| 67 |
* @since 2.0.0 bbPress (r2957) |
| 68 |
* |
| 69 |
* @param array $menu_order Menu Order. |
| 70 |
* @return array Modified menu order. |
| 71 |
*/ |
| 72 |
function bbp_admin_menu_order( $menu_order ) { |
| 73 |
|
| 74 |
// Bail if user cannot see any top level bbPress menus |
| 75 |
if ( empty( $menu_order ) || ( false === bbp_admin()->show_separator ) ) { |
| 76 |
return $menu_order; |
| 77 |
} |
| 78 |
|
| 79 |
// Initialize our custom order array |
| 80 |
$bbp_menu_order = array(); |
| 81 |
|
| 82 |
// Menu values |
| 83 |
$second_sep = 'separator2'; |
| 84 |
$custom_menus = array( |
| 85 |
'separator-bbpress', // Separator |
| 86 |
'edit.php?post_type=' . bbp_get_forum_post_type(), // Forums |
| 87 |
'edit.php?post_type=' . bbp_get_topic_post_type(), // Topics |
| 88 |
'edit.php?post_type=' . bbp_get_reply_post_type() // Replies |
| 89 |
); |
| 90 |
|
| 91 |
// Loop through menu order and do some rearranging |
| 92 |
foreach ( $menu_order as $item ) { |
| 93 |
|
| 94 |
// Position bbPress menus above appearance |
| 95 |
if ( $second_sep === $item ) { |
| 96 |
|
| 97 |
// Add our custom menus |
| 98 |
foreach ( $custom_menus as $custom_menu ) { |
| 99 |
if ( array_search( $custom_menu, $menu_order, true ) ) { |
| 100 |
$bbp_menu_order[] = $custom_menu; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Add the appearance separator |
| 105 |
$bbp_menu_order[] = $second_sep; |
| 106 |
|
| 107 |
// Skip our menu items |
| 108 |
} elseif ( ! in_array( $item, $custom_menus, true ) ) { |
| 109 |
$bbp_menu_order[] = $item; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// Return our custom order |
| 114 |
return $bbp_menu_order; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Sanitize permalink slugs when saving the settings page. |
| 119 |
* |
| 120 |
* @since 2.6.0 bbPress (r5364) |
| 121 |
* |
| 122 |
* @param string $slug |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
function bbp_sanitize_slug( $slug = '' ) { |
| 126 |
|
| 127 |
// Don't allow multiple slashes in a row |
| 128 |
$value = preg_replace( '#/+#', '/', str_replace( '#', '', $slug ) ); |
| 129 |
|
| 130 |
// Strip out unsafe or unusable chars |
| 131 |
$value = esc_url_raw( $value ); |
| 132 |
|
| 133 |
// esc_url_raw() adds a scheme via esc_url(), so let's remove it |
| 134 |
$value = str_replace( 'http://', '', $value ); |
| 135 |
|
| 136 |
// Trim off first and last slashes. |
| 137 |
// |
| 138 |
// We already prevent double slashing elsewhere, but let's prevent |
| 139 |
// accidental poisoning of options values where we can. |
| 140 |
$value = ltrim( $value, '/' ); |
| 141 |
$value = rtrim( $value, '/' ); |
| 142 |
|
| 143 |
/** |
| 144 |
* Filters the sanitized slug value. |
| 145 |
* |
| 146 |
* @since 2.6.0 bbPress (r5364) |
| 147 |
* |
| 148 |
* @param string $value The sanitized slug. |
| 149 |
* @param string $slug The original slug value. |
| 150 |
*/ |
| 151 |
return apply_filters( 'bbp_sanitize_slug', $value, $slug ); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Uninstall all bbPress options and capabilities from a specific site. |
| 156 |
* |
| 157 |
* @since 2.1.0 bbPress (r3765) |
| 158 |
* |
| 159 |
* @param int $site_id Site id. |
| 160 |
*/ |
| 161 |
function bbp_do_uninstall( $site_id = 0 ) { |
| 162 |
if ( empty( $site_id ) ) { |
| 163 |
$site_id = get_current_blog_id(); |
| 164 |
} |
| 165 |
|
| 166 |
bbp_switch_to_site( $site_id ); |
| 167 |
bbp_delete_options(); |
| 168 |
bbp_remove_roles(); |
| 169 |
bbp_remove_caps(); |
| 170 |
flush_rewrite_rules(); |
| 171 |
bbp_restore_current_site(); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* This tells WP to highlight the Tools > Forums menu item, |
| 176 |
* regardless of which actual bbPress Tools screen we are on. |
| 177 |
* |
| 178 |
* The conditional prevents the override when the user is viewing settings or |
| 179 |
* any third-party plugins. |
| 180 |
* |
| 181 |
* @since 2.1.0 bbPress (r3888) |
| 182 |
* |
| 183 |
* @global string $plugin_page |
| 184 |
* @global array $submenu_file |
| 185 |
*/ |
| 186 |
function bbp_tools_modify_menu_highlight() { |
| 187 |
global $plugin_page, $submenu_file; |
| 188 |
|
| 189 |
// This tweaks the Tools subnav menu to only show one bbPress menu item |
| 190 |
if ( ! in_array( $plugin_page, array( 'bbp-settings' ), true ) ) { |
| 191 |
|
| 192 |
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 193 |
$submenu_file = 'bbp-repair'; |
| 194 |
} |
| 195 |
} |
| 196 |
|