| 2150 | |
| 2151 | /** Visibility ***************************************************************/ |
| 2152 | |
| 2153 | /** |
| 2154 | * Registers a visiblity level with BuddyPress. |
| 2155 | * |
| 2156 | * Run this on the 'bp_setup_globals' action. |
| 2157 | * |
| 2158 | * @since BuddyPress (2.3.0) |
| 2159 | * |
| 2160 | * @param array $args { |
| 2161 | * Array of arguments. |
| 2162 | * @type string $id The ID for the visibility level. Required. |
| 2163 | * @type string $label Label for the visibility level. Required. |
| 2164 | * @type int $position Position to order the visibility level. Optional. Default: 99. |
| 2165 | * } |
| 2166 | * @return bool |
| 2167 | */ |
| 2168 | function bp_register_visibility_level( $args = array() ) { |
| 2169 | $r = bp_parse_args( $args, array( |
| 2170 | 'id' => '', |
| 2171 | 'label' => '', |
| 2172 | 'position' => 99 |
| 2173 | ), 'register_visibility_level' ); |
| 2174 | |
| 2175 | if ( empty( $r['id'] ) || empty( $r['label'] ) ) { |
| 2176 | return false; |
| 2177 | } |
| 2178 | |
| 2179 | $bp = buddypress(); |
| 2180 | |
| 2181 | if ( empty( $bp->core->visibility_levels ) ) { |
| 2182 | $bp->core->visibility_levels = array(); |
| 2183 | } |
| 2184 | |
| 2185 | $id = sanitize_key( $r['id'] ); |
| 2186 | unset( $r['id'] ); |
| 2187 | if ( ! empty( $bp->core->visibility_levels[ $id ] ) ) { |
| 2188 | return false; |
| 2189 | } |
| 2190 | |
| 2191 | // register the level |
| 2192 | $bp->core->visibility_levels[ $id ]['id'] = $id; |
| 2193 | |
| 2194 | // left open so plugins can add parameters if needed (callbacks, etc.) |
| 2195 | $bp->core->visibility_levels[ $id ] = array_merge( $bp->core->visibility_levels[ $id ], $r ); |
| 2196 | |
| 2197 | return true; |
| 2198 | } |
| 2199 | |
| 2200 | /** |
| 2201 | * Deregisters a BuddyPress visiblity level. |
| 2202 | * |
| 2203 | * Run this on the 'bp_setup_globals' if needed. |
| 2204 | * |
| 2205 | * @since BuddyPress (2.3.0) |
| 2206 | * |
| 2207 | * @param string $id The ID of the visibility level to deregister. |
| 2208 | * @return bool |
| 2209 | */ |
| 2210 | function bp_deregister_visibility_level( $id = '' ) { |
| 2211 | if ( empty( $id ) ) { |
| 2212 | return false; |
| 2213 | } |
| 2214 | |
| 2215 | $bp = buddypress(); |
| 2216 | |
| 2217 | if ( empty( $bp->core->visibility_levels[$id] ) ) { |
| 2218 | return false; |
| 2219 | } |
| 2220 | |
| 2221 | // Don't deregister core levels. Is this necessary? |
| 2222 | // Someone could use the 'bp_core_get_visibility_levels' filter as well... |
| 2223 | /* |
| 2224 | if ( true === in_array( $id, array( 'public', 'adminsonly', 'loggedin' ), true ) ) { |
| 2225 | return false; |
| 2226 | } |
| 2227 | */ |
| 2228 | |
| 2229 | unset( $bp->core->visibility_levels[$id] ); |
| 2230 | return true; |
| 2231 | } |
| 2232 | |
| 2233 | /** |
| 2234 | * Get available visibility levels for BuddyPress. |
| 2235 | * |
| 2236 | * @since BuddyPress (2.3.0) |
| 2237 | * |
| 2238 | * @return array |
| 2239 | */ |
| 2240 | function bp_core_get_visibility_levels() { |
| 2241 | |
| 2242 | /** |
| 2243 | * Filters the visibility levels. |
| 2244 | * |
| 2245 | * @since BuddyPress (2.3.0) |
| 2246 | * |
| 2247 | * @param array Array of visibility levels. |
| 2248 | */ |
| 2249 | return apply_filters( 'bp_core_get_visibility_levels', buddypress()->core->visibility_levels ); |
| 2250 | } |
| 2251 | |
| 2252 | /** |
| 2253 | * Sorts visibility levels by the 'position' subkey. |
| 2254 | * |
| 2255 | * Used only once. |
| 2256 | * |
| 2257 | * @since BuddyPress (2.3.0) |
| 2258 | * |
| 2259 | * @access private |
| 2260 | */ |
| 2261 | function _bp_core_sort_visibility_levels() { |
| 2262 | $bp = buddypress(); |
| 2263 | $temp = bp_sort_by_key( $bp->core->visibility_levels, 'position', 'num' ); |
| 2264 | |
| 2265 | $bp->core->visibility_levels = array(); |
| 2266 | foreach( $temp as $level ) { |
| 2267 | $bp->core->visibility_levels[ $level['id'] ] = $level; |
| 2268 | } |
| 2269 | unset( $temp ); |
| 2270 | } |
| 2271 | add_action( 'bp_setup_globals', '_bp_core_sort_visibility_levels', 999 ); |
| 2272 | No newline at end of file |