/svntrunk/bp-core/bp-core-options.php

https://bitbucket.org/simplemediacode/bptrunk · PHP · 538 lines · 163 code · 87 blank · 288 comment · 14 complexity · 11d6a912ec7ab0cf2ce4d8603a82f0b1 MD5 · raw file

  1. <?php
  2. /**
  3. * BuddyPress Options
  4. *
  5. * @package BuddyPress
  6. * @subpackage Options
  7. */
  8. // Exit if accessed directly
  9. if ( !defined( 'ABSPATH' ) ) exit;
  10. /**
  11. * Get the default site options and their values
  12. *
  13. * @since BuddyPress (1.6)
  14. *
  15. * @return array Filtered option names and values
  16. */
  17. function bp_get_default_options() {
  18. // Default options
  19. $options = array (
  20. /** Components ********************************************************/
  21. 'bp-deactivated-components' => array(),
  22. /** bbPress ***********************************************************/
  23. // Legacy bbPress config location
  24. 'bb-config-location' => ABSPATH . 'bb-config.php',
  25. /** XProfile **********************************************************/
  26. // Base profile groups name
  27. 'bp-xprofile-base-group-name' => 'Base',
  28. // Base fullname field name
  29. 'bp-xprofile-fullname-field-name' => 'Name',
  30. /** Blogs *************************************************************/
  31. // Used to decide if blogs need indexing
  32. 'bp-blogs-first-install' => false,
  33. /** Settings **********************************************************/
  34. // Disable the WP to BP profile sync
  35. 'bp-disable-profile-sync' => false,
  36. // Hide the Toolbar for logged out users
  37. 'hide-loggedout-adminbar' => false,
  38. // Avatar uploads
  39. 'bp-disable-avatar-uploads' => false,
  40. // Allow users to delete their own accounts
  41. 'bp-disable-account-deletion' => false,
  42. // Allow comments on blog and forum activity items
  43. 'bp-disable-blogforum-comments' => true,
  44. // The ID for the current theme package.
  45. '_bp_theme_package_id' => 'legacy',
  46. /** Groups ************************************************************/
  47. // @todo Move this into the groups component
  48. // Restrict group creation to super admins
  49. 'bp_restrict_group_creation' => false,
  50. /** Akismet ***********************************************************/
  51. // Users from all sites can post
  52. '_bp_enable_akismet' => true,
  53. /** BuddyBar **********************************************************/
  54. // Force the BuddyBar
  55. '_bp_force_buddybar' => false
  56. );
  57. return apply_filters( 'bp_get_default_options', $options );
  58. }
  59. /**
  60. * Add default options
  61. *
  62. * Hooked to bp_activate, it is only called once when BuddyPress is activated.
  63. * This is non-destructive, so existing settings will not be overridden.
  64. *
  65. * @since BuddyPress (1.6)
  66. *
  67. * @uses bp_get_default_options() To get default options
  68. * @uses add_option() Adds default options
  69. * @uses do_action() Calls 'bp_add_options'
  70. */
  71. function bp_add_options() {
  72. // Get the default options and values
  73. $options = bp_get_default_options();
  74. // Add default options
  75. foreach ( $options as $key => $value )
  76. add_option( $key, $value );
  77. // Allow previously activated plugins to append their own options.
  78. do_action( 'bp_add_options' );
  79. }
  80. /**
  81. * Delete default options
  82. *
  83. * Hooked to bp_uninstall, it is only called once when BuddyPress is uninstalled.
  84. * This is destructive, so existing settings will be destroyed.
  85. *
  86. * @since BuddyPress (1.6)
  87. *
  88. * @uses bp_get_default_options() To get default options
  89. * @uses delete_option() Removes default options
  90. * @uses do_action() Calls 'bp_delete_options'
  91. */
  92. function bp_delete_options() {
  93. // Get the default options and values
  94. $options = bp_get_default_options();
  95. // Add default options
  96. foreach ( $options as $key => $value )
  97. delete_option( $key );
  98. // Allow previously activated plugins to append their own options.
  99. do_action( 'bp_delete_options' );
  100. }
  101. /**
  102. * Add filters to each BuddyPress option and allow them to be overloaded from
  103. * inside the $bp->options array.
  104. *
  105. * @since BuddyPress (1.6)
  106. *
  107. * @uses bp_get_default_options() To get default options
  108. * @uses add_filter() To add filters to 'pre_option_{$key}'
  109. * @uses do_action() Calls 'bp_add_option_filters'
  110. */
  111. function bp_setup_option_filters() {
  112. // Get the default options and values
  113. $options = bp_get_default_options();
  114. // Add filters to each BuddyPress option
  115. foreach ( $options as $key => $value )
  116. add_filter( 'pre_option_' . $key, 'bp_pre_get_option' );
  117. // Allow previously activated plugins to append their own options.
  118. do_action( 'bp_setup_option_filters' );
  119. }
  120. /**
  121. * Filter default options and allow them to be overloaded from inside the
  122. * $bp->options array.
  123. *
  124. * @since BuddyPress (1.6)
  125. *
  126. * @global BuddyPress $bp
  127. * @param bool $value Optional. Default value false
  128. * @return mixed false if not overloaded, mixed if set
  129. */
  130. function bp_pre_get_option( $value = false ) {
  131. global $bp;
  132. // Get the name of the current filter so we can manipulate it
  133. $filter = current_filter();
  134. // Remove the filter prefix
  135. $option = str_replace( 'pre_option_', '', $filter );
  136. // Check the options global for preset value
  137. if ( !empty( $bp->options[$option] ) )
  138. $value = $bp->options[$option];
  139. // Always return a value, even if false
  140. return $value;
  141. }
  142. /**
  143. * Retrieve an option
  144. *
  145. * This is a wrapper for get_blog_option(), which in turn stores settings data (such as bp-pages)
  146. * on the appropriate blog, given your current setup.
  147. *
  148. * The 'bp_get_option' filter is primarily for backward-compatibility.
  149. *
  150. * @package BuddyPress
  151. * @since BuddyPress (1.5)
  152. *
  153. * @uses bp_get_root_blog_id()
  154. * @param string $option_name The option to be retrieved
  155. * @param string $default Optional. Default value to be returned if the option isn't set
  156. * @return mixed The value for the option
  157. */
  158. function bp_get_option( $option_name, $default = '' ) {
  159. $value = get_blog_option( bp_get_root_blog_id(), $option_name, $default );
  160. return apply_filters( 'bp_get_option', $value );
  161. }
  162. /**
  163. * Save an option
  164. *
  165. * This is a wrapper for update_blog_option(), which in turn stores settings data (such as bp-pages)
  166. * on the appropriate blog, given your current setup.
  167. *
  168. * @package BuddyPress
  169. * @since BuddyPress (1.5)
  170. *
  171. * @uses bp_get_root_blog_id()
  172. * @param string $option_name The option key to be set
  173. * @param string $value The value to be set
  174. */
  175. function bp_update_option( $option_name, $value ) {
  176. update_blog_option( bp_get_root_blog_id(), $option_name, $value );
  177. }
  178. /**
  179. * Delete an option
  180. *
  181. * This is a wrapper for delete_blog_option(), which in turn deletes settings data (such as
  182. * bp-pages) on the appropriate blog, given your current setup.
  183. *
  184. * @package BuddyPress
  185. * @since BuddyPress (1.5)
  186. *
  187. * @uses bp_get_root_blog_id()
  188. * @param string $option_name The option key to be set
  189. */
  190. function bp_delete_option( $option_name ) {
  191. delete_blog_option( bp_get_root_blog_id(), $option_name );
  192. }
  193. /**
  194. * When switching from single to multisite we need to copy blog options to
  195. * site options.
  196. *
  197. * This function is no longer used
  198. *
  199. * @package BuddyPress Core
  200. * @deprecated Since BuddyPress (1.6)
  201. */
  202. function bp_core_activate_site_options( $keys = array() ) {
  203. global $bp;
  204. if ( !empty( $keys ) && is_array( $keys ) ) {
  205. $errors = false;
  206. foreach ( $keys as $key => $default ) {
  207. if ( empty( $bp->site_options[ $key ] ) ) {
  208. $bp->site_options[ $key ] = bp_get_option( $key, $default );
  209. if ( !bp_update_option( $key, $bp->site_options[ $key ] ) ) {
  210. $errors = true;
  211. }
  212. }
  213. }
  214. if ( empty( $errors ) ) {
  215. return true;
  216. }
  217. }
  218. return false;
  219. }
  220. /**
  221. * BuddyPress uses common options to store configuration settings. Many of these
  222. * settings are needed at run time. Instead of fetching them all and adding many
  223. * initial queries to each page load, let's fetch them all in one go.
  224. *
  225. * @package BuddyPress Core
  226. * @todo Use settings API and audit these methods
  227. */
  228. function bp_core_get_root_options() {
  229. global $wpdb;
  230. // Get all the BuddyPress settings, and a few useful WP ones too
  231. $root_blog_options = bp_get_default_options();
  232. $root_blog_options['registration'] = '0';
  233. $root_blog_options['avatar_default'] = 'mysteryman';
  234. $root_blog_option_keys = array_keys( $root_blog_options );
  235. // Do some magic to get all the root blog options in 1 swoop
  236. $blog_options_keys = "'" . join( "', '", (array) $root_blog_option_keys ) . "'";
  237. $blog_options_table = bp_is_multiblog_mode() ? $wpdb->options : $wpdb->get_blog_prefix( bp_get_root_blog_id() ) . 'options';
  238. $blog_options_query = "SELECT option_name AS name, option_value AS value FROM {$blog_options_table} WHERE option_name IN ( {$blog_options_keys} )";
  239. $root_blog_options_meta = $wpdb->get_results( $blog_options_query );
  240. // On Multisite installations, some options must always be fetched from sitemeta
  241. if ( is_multisite() ) {
  242. $network_options = apply_filters( 'bp_core_network_options', array(
  243. 'tags_blog_id' => '0',
  244. 'sitewide_tags_blog' => '',
  245. 'registration' => '0',
  246. 'fileupload_maxk' => '1500'
  247. ) );
  248. $current_site = get_current_site();
  249. $network_option_keys = array_keys( $network_options );
  250. $sitemeta_options_keys = "'" . join( "', '", (array) $network_option_keys ) . "'";
  251. $sitemeta_options_query = $wpdb->prepare( "SELECT meta_key AS name, meta_value AS value FROM {$wpdb->sitemeta} WHERE meta_key IN ( {$sitemeta_options_keys} ) AND site_id = %d", $current_site->id );
  252. $network_options_meta = $wpdb->get_results( $sitemeta_options_query );
  253. // Sitemeta comes second in the merge, so that network 'registration' value wins
  254. $root_blog_options_meta = array_merge( $root_blog_options_meta, $network_options_meta );
  255. }
  256. // Missing some options, so do some one-time fixing
  257. if ( empty( $root_blog_options_meta ) || ( count( $root_blog_options_meta ) < count( $root_blog_option_keys ) ) ) {
  258. // Get a list of the keys that are already populated
  259. $existing_options = array();
  260. foreach( $root_blog_options_meta as $already_option ) {
  261. $existing_options[$already_option->name] = $already_option->value;
  262. }
  263. // Unset the query - We'll be resetting it soon
  264. unset( $root_blog_options_meta );
  265. // Loop through options
  266. foreach ( $root_blog_options as $old_meta_key => $old_meta_default ) {
  267. // Clear out the value from the last time around
  268. unset( $old_meta_value );
  269. if ( isset( $existing_options[$old_meta_key] ) ) {
  270. continue;
  271. }
  272. // Get old site option
  273. if ( is_multisite() )
  274. $old_meta_value = get_site_option( $old_meta_key );
  275. // No site option so look in root blog
  276. if ( empty( $old_meta_value ) )
  277. $old_meta_value = bp_get_option( $old_meta_key, $old_meta_default );
  278. // Update the root blog option
  279. bp_update_option( $old_meta_key, $old_meta_value );
  280. // Update the global array
  281. $root_blog_options_meta[$old_meta_key] = $old_meta_value;
  282. }
  283. $root_blog_options_meta = array_merge( $root_blog_options_meta, $existing_options );
  284. unset( $existing_options );
  285. // We're all matched up
  286. } else {
  287. // Loop through our results and make them usable
  288. foreach ( $root_blog_options_meta as $root_blog_option )
  289. $root_blog_options[$root_blog_option->name] = $root_blog_option->value;
  290. // Copy the options no the return val
  291. $root_blog_options_meta = $root_blog_options;
  292. // Clean up our temporary copy
  293. unset( $root_blog_options );
  294. }
  295. return apply_filters( 'bp_core_get_root_options', $root_blog_options_meta );
  296. }
  297. /** Active? *******************************************************************/
  298. /**
  299. * Is profile sycing disabled?
  300. *
  301. * @since BuddyPress (1.6)
  302. *
  303. * @param bool $default Optional.Default value true
  304. *
  305. * @uses bp_get_option() To get the profile sync option
  306. * @return bool Is profile sync enabled or not
  307. */
  308. function bp_disable_profile_sync( $default = true ) {
  309. return (bool) apply_filters( 'bp_disable_profile_sync', (bool) bp_get_option( 'bp-disable-profile-sync', $default ) );
  310. }
  311. /**
  312. * Is the Toolbar hidden for logged out users?
  313. *
  314. * @since BuddyPress (1.6)
  315. *
  316. * @param bool $default Optional.Default value true
  317. *
  318. * @uses bp_get_option() To get the logged out Toolbar option
  319. * @return bool Is logged out Toolbar enabled or not
  320. */
  321. function bp_hide_loggedout_adminbar( $default = true ) {
  322. return (bool) apply_filters( 'bp_hide_loggedout_adminbar', (bool) bp_get_option( 'hide-loggedout-adminbar', $default ) );
  323. }
  324. /**
  325. * Are members able to upload their own avatars?
  326. *
  327. * @since BuddyPress (1.6)
  328. *
  329. * @param bool $default Optional. Default value true
  330. *
  331. * @uses bp_get_option() To get the avatar uploads option
  332. * @return bool Are avatar uploads allowed?
  333. */
  334. function bp_disable_avatar_uploads( $default = true ) {
  335. return (bool) apply_filters( 'bp_disable_avatar_uploads', (bool) bp_get_option( 'bp-disable-avatar-uploads', $default ) );
  336. }
  337. /**
  338. * Are members able to delete their own accounts?
  339. *
  340. * @since BuddyPress (1.6)
  341. *
  342. * @param bool $default Optional. Default value
  343. *
  344. * @uses bp_get_option() To get the account deletion option
  345. * @return bool Is account deletion allowed?
  346. */
  347. function bp_disable_account_deletion( $default = false ) {
  348. return apply_filters( 'bp_disable_account_deletion', (bool) bp_get_option( 'bp-disable-account-deletion', $default ) );
  349. }
  350. /**
  351. * Are blog and forum activity stream comments disabled?
  352. *
  353. * @since BuddyPress (1.6)
  354. *
  355. * @param bool $default Optional. Default value false
  356. * @todo split and move into blog and forum components
  357. * @uses bp_get_option() To get the blog/forum comments option
  358. * @return bool Is blog/forum comments allowed?
  359. */
  360. function bp_disable_blogforum_comments( $default = false ) {
  361. return (bool) apply_filters( 'bp_disable_blogforum_comments', (bool) bp_get_option( 'bp-disable-blogforum-comments', $default ) );
  362. }
  363. /**
  364. * Is group creation turned off?
  365. *
  366. * @since BuddyPress (1.6)
  367. *
  368. * @param bool $default Optional. Default value true
  369. *
  370. * @todo Move into groups component
  371. * @uses bp_get_option() To get the group creation
  372. * @return bool Allow group creation?
  373. */
  374. function bp_restrict_group_creation( $default = true ) {
  375. return (bool) apply_filters( 'bp_restrict_group_creation', (bool) bp_get_option( 'bp_restrict_group_creation', $default ) );
  376. }
  377. /**
  378. * Have we migrated to using the WordPress Toolbar?
  379. *
  380. * @since BuddyPress (1.6)
  381. *
  382. * @param bool $default Optional. Default value true
  383. *
  384. * @todo Move into groups component
  385. * @uses bp_get_option() To get the WP editor option
  386. * @return bool Use WP editor?
  387. */
  388. function bp_force_buddybar( $default = true ) {
  389. return (bool) apply_filters( 'bp_force_buddybar', (bool) bp_get_option( '_bp_force_buddybar', $default ) );
  390. }
  391. /**
  392. * Output the group forums root parent forum id
  393. *
  394. * @since BuddyPress (1.6)
  395. *
  396. * @param bool $default Optional. Default value
  397. */
  398. function bp_group_forums_root_id( $default = '0' ) {
  399. echo bp_get_group_forums_root_id( $default );
  400. }
  401. /**
  402. * Return the group forums root parent forum id
  403. *
  404. * @since BuddyPress (1.6)
  405. *
  406. * @param bool $default Optional. Default value 0
  407. *
  408. * @uses bp_get_option() To get the maximum title length
  409. * @return int Is anonymous posting allowed?
  410. */
  411. function bp_get_group_forums_root_id( $default = '0' ) {
  412. return (int) apply_filters( 'bp_get_group_forums_root_id', (int) bp_get_option( '_bp_group_forums_root_id', $default ) );
  413. }
  414. /**
  415. * Checks if BuddyPress Group Forums are enabled
  416. *
  417. * @since BuddyPress (1.6)
  418. *
  419. * @param bool $default Optional. Default value true
  420. *
  421. * @uses bp_get_option() To get the group forums option
  422. * @return bool Is group forums enabled or not
  423. */
  424. function bp_is_group_forums_active( $default = true ) {
  425. return (bool) apply_filters( 'bp_is_group_forums_active', (bool) bp_get_option( '_bp_enable_group_forums', $default ) );
  426. }
  427. /**
  428. * Checks if Akismet is enabled
  429. *
  430. * @since BuddyPress (1.6)
  431. *
  432. * @param bool $default Optional. Default value true
  433. *
  434. * @uses bp_get_option() To get the Akismet option
  435. * @return bool Is Akismet enabled or not
  436. */
  437. function bp_is_akismet_active( $default = true ) {
  438. return (bool) apply_filters( 'bp_is_akismet_active', (bool) bp_get_option( '_bp_enable_akismet', $default ) );
  439. }
  440. /**
  441. * Get the current theme package ID
  442. *
  443. * @since BuddyPress (1.7)
  444. *
  445. * @param string $default Optional. Default value 'default'
  446. * @uses get_option() To get the subtheme option
  447. * @return string ID of the subtheme
  448. */
  449. function bp_get_theme_package_id( $default = 'legacy' ) {
  450. return apply_filters( 'bp_get_theme_package_id', bp_get_option( '_bp_theme_package_id', $default ) );
  451. }