/bp-forums/bbpress/bb-includes/functions.bb-meta.php

  1. <?php 
  2.  
  3. /** Options/Meta */ 
  4.  
  5. /** Internal */ 
  6.  
  7. function bb_sanitize_meta_key( $key ) 
  8. return preg_replace( '|[^a-z0-9_]|i', '', $key ); 
  9.  
  10. /** 
  11. * Adds and updates meta data in the database 
  12. * 
  13. * @internal 
  14. */ 
  15. function bb_update_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) 
  16. global $bbdb; 
  17. if ( !is_numeric( $object_id ) || empty( $object_id ) && !$global ) { 
  18. return false; 
  19. $cache_object_id = $object_id = (int) $object_id; 
  20. switch ( $type ) { 
  21. case 'option': 
  22. $object_type = 'bb_option'; 
  23. break; 
  24. case 'user' : 
  25. global $wp_users_object; 
  26. $id = $object_id; 
  27. $return = $wp_users_object->update_meta( compact( 'id', 'meta_key', 'meta_value' ) ); 
  28. if ( is_wp_error( $return ) ) { 
  29. return false; 
  30. return $return; 
  31. break; 
  32. case 'forum' : 
  33. $object_type = 'bb_forum'; 
  34. break; 
  35. case 'topic' : 
  36. $object_type = 'bb_topic'; 
  37. break; 
  38. case 'post' : 
  39. $object_type = 'bb_post'; 
  40. break; 
  41. default : 
  42. $object_type = $type; 
  43. break; 
  44.  
  45. $meta_key = bb_sanitize_meta_key( $meta_key ); 
  46.  
  47. $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' ); 
  48. $meta_tuple = apply_filters( 'bb_update_meta', $meta_tuple ); 
  49. extract( $meta_tuple, EXTR_OVERWRITE ); 
  50.  
  51. $meta_value = $_meta_value = maybe_serialize( $meta_value ); 
  52. $meta_value = maybe_unserialize( $meta_value ); 
  53.  
  54. $cur = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM `$bbdb->meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key ) ); 
  55. if ( !$cur ) { 
  56. $bbdb->query( $bbdb->prepare( 
  57. "INSERT INTO `$bbdb->meta` ( `object_type`, `object_id`, `meta_key`, `meta_value` ) VALUES( %s, %d, %s, %s ) 
  58. ON DUPLICATE KEY UPDATE `meta_value` = VALUES( `meta_value` )",  
  59. $object_type, $object_id, $meta_key, $_meta_value 
  60. ) ); 
  61. } elseif ( $cur->meta_value != $meta_value ) { 
  62. $bbdb->update( $bbdb->meta, array( 'meta_value' => $_meta_value), array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key ) ); 
  63.  
  64. if ( $object_type === 'bb_option' ) { 
  65. $cache_object_id = $meta_key; 
  66. wp_cache_delete( $cache_object_id, 'bb_option_not_set' ); 
  67. wp_cache_delete( $cache_object_id, $object_type ); 
  68.  
  69. if ( !$cur ) { 
  70. return true; 
  71.  
  72. /** 
  73. * Deletes meta data from the database 
  74. * 
  75. * @internal 
  76. */ 
  77. function bb_delete_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) 
  78. global $bbdb; 
  79. if ( !is_numeric( $object_id ) || empty( $object_id ) && !$global ) { 
  80. return false; 
  81. $cache_object_id = $object_id = (int) $object_id; 
  82. switch ( $type ) { 
  83. case 'option': 
  84. $object_type = 'bb_option'; 
  85. break; 
  86. case 'user': 
  87. global $wp_users_object; 
  88. $id = $object_id; 
  89. return $wp_users_object->delete_meta( compact( 'id', 'meta_key', 'meta_value' ) ); 
  90. break; 
  91. case 'forum': 
  92. $object_type = 'bb_forum'; 
  93. break; 
  94. case 'topic': 
  95. $object_type = 'bb_topic'; 
  96. break; 
  97. case 'post': 
  98. $object_type = 'bb_post'; 
  99. break; 
  100. default: 
  101. $object_type = $type; 
  102. break; 
  103.  
  104. $meta_key = bb_sanitize_meta_key( $meta_key ); 
  105.  
  106. $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' ); 
  107. $meta_tuple = apply_filters( 'bb_delete_meta', $meta_tuple ); 
  108. extract( $meta_tuple, EXTR_OVERWRITE ); 
  109.  
  110. $meta_value = maybe_serialize( $meta_value ); 
  111.  
  112. if ( empty( $meta_value ) ) { 
  113. $meta_sql = $bbdb->prepare( "SELECT `meta_id` FROM `$bbdb->meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key ); 
  114. } else { 
  115. $meta_sql = $bbdb->prepare( "SELECT `meta_id` FROM `$bbdb->meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s AND `meta_value` = %s", $object_type, $object_id, $meta_key, $meta_value ); 
  116.  
  117. if ( !$meta_id = $bbdb->get_var( $meta_sql ) ) { 
  118. return false; 
  119.  
  120. $bbdb->query( $bbdb->prepare( "DELETE FROM `$bbdb->meta` WHERE `meta_id` = %d", $meta_id ) ); 
  121.  
  122. if ( $object_type == 'bb_option' ) { 
  123. $cache_object_id = $meta_key; 
  124. wp_cache_delete( $cache_object_id, 'bb_option_not_set' ); 
  125. wp_cache_delete( $cache_object_id, $object_type ); 
  126. return true; 
  127.  
  128. /** 
  129. * Adds an objects meta data to the object 
  130. * 
  131. * This is the only function that should add to user / topic - NOT bbdb::prepared 
  132. * 
  133. * @internal 
  134. */ 
  135. function bb_append_meta( $object, $type ) 
  136. global $bbdb; 
  137. switch ( $type ) { 
  138. case 'user': 
  139. global $wp_users_object; 
  140. return $wp_users_object->append_meta( $object ); 
  141. break; 
  142. case 'forum': 
  143. $object_id_column = 'forum_id'; 
  144. $object_type = 'bb_forum'; 
  145. $slug = 'forum_slug'; 
  146. break; 
  147. case 'topic': 
  148. $object_id_column = 'topic_id'; 
  149. $object_type = 'bb_topic'; 
  150. $slug = 'topic_slug'; 
  151. break; 
  152. case 'post': 
  153. $object_id_column = 'post_id'; 
  154. $object_type = 'bb_post'; 
  155. $slug = false; 
  156. break; 
  157.  
  158. if ( is_array( $object ) && count( $object ) ) { 
  159. $trans = array(); 
  160. foreach ( array_keys( $object ) as $i ) { 
  161. $trans[$object[$i]->$object_id_column] =& $object[$i]; 
  162. $ids = array_map( 'intval', array_keys( $trans ) ); 
  163. $query_ids = array(); 
  164. $cached_objects = array(); 
  165. $position = 0; 
  166. foreach ( $ids as $_id ) { 
  167. if ( false !== $_cached_object = wp_cache_get( $_id, $object_type ) ) { 
  168. $cached_objects[$position] = $_cached_object; 
  169. } else { 
  170. $query_ids[$position] = $_id; 
  171. $position++; 
  172. if ( !count( $query_ids ) ) { 
  173. return $cached_objects; 
  174.  
  175. $_query_ids = $query_ids; 
  176. sort( $_query_ids ); 
  177. $_query_ids = join( ', ', $_query_ids ); 
  178.  
  179. if ( $metas = $bbdb->get_results( "SELECT `object_id`, `meta_key`, `meta_value` FROM `$bbdb->meta` WHERE `object_type` = '$object_type' AND `object_id` IN ($_query_ids) /* bb_append_meta */" ) ) { 
  180. usort( $metas, '_bb_append_meta_sort' ); 
  181. foreach ( $metas as $meta ) { 
  182. $trans[$meta->object_id]->{$meta->meta_key} = maybe_unserialize( $meta->meta_value ); 
  183. if ( strpos($meta->meta_key, $bbdb->prefix) === 0 ) { 
  184. $trans[$meta->object_id]->{substr($meta->meta_key, strlen($bbdb->prefix))} = maybe_unserialize( $meta->meta_value ); 
  185. foreach ( $query_ids as $position => $i ) { 
  186. $cached_objects[$position] = $trans[$i]; 
  187. wp_cache_add( $i, $trans[$i], $object_type ); 
  188. if ( $slug ) { 
  189. wp_cache_add( $trans[$i]->$slug, $i, 'bb_' . $slug ); 
  190. if ( !count( $cached_objects ) ) { 
  191. return $object; 
  192. ksort( $cached_objects ); 
  193.  
  194. return $cached_objects; 
  195. } elseif ( $object ) { 
  196. if ( false !== $cached_object = wp_cache_get( $object->$object_id_column, $object_type ) ) { 
  197. return $cached_object; 
  198. if ( $metas = $bbdb->get_results( $bbdb->prepare( "SELECT `object_id`, `meta_key`, `meta_value` FROM `$bbdb->meta` WHERE `object_type` = '$object_type' AND `object_id` = %d /* bb_append_meta */", $object->$object_id_column ) ) ) { 
  199. usort( $metas, '_bb_append_meta_sort' ); 
  200. foreach ( $metas as $meta ) { 
  201. $object->{$meta->meta_key} = maybe_unserialize( $meta->meta_value ); 
  202. if ( strpos( $meta->meta_key, $bbdb->prefix ) === 0 ) { 
  203. $object->{substr( $meta->meta_key, strlen( $bbdb->prefix ) )} = $object->{$meta->meta_key}; 
  204. if ( $object->$object_id_column ) { 
  205. wp_cache_add( $object->$object_id_column, $object, $object_type ); 
  206. if ( $slug ) { 
  207. wp_cache_add( $object->$slug, $object->$object_id_column, 'bb_' . $slug ); 
  208. return $object; 
  209.  
  210. /** 
  211. * Sorts meta keys by length to ensure $appended_object->{$bbdb->prefix} key overwrites $appended_object->key as desired 
  212. * 
  213. * @internal 
  214. */ 
  215. function _bb_append_meta_sort( $a, $b ) 
  216. return strlen( $a->meta_key ) - strlen( $b->meta_key ); 
  217.  
  218.  
  219.  
  220. /** Options */ 
  221.  
  222. /** 
  223. * Echoes the requested bbPress option by calling bb_get_option() 
  224. * 
  225. * @param string The option to be echoed 
  226. * @return void 
  227. */ 
  228. function bb_option( $option ) 
  229. echo apply_filters( 'bb_option_' . $option, bb_get_option( $option ) ); 
  230.  
  231. /** 
  232. * Returns the requested bbPress option from the meta table or the $bb object 
  233. * 
  234. * @param string The option to be echoed 
  235. * @return mixed The value of the option 
  236. */ 
  237. function bb_get_option( $option ) 
  238. // Allow plugins to short-circuit options. 
  239. if ( false !== $r = apply_filters( 'bb_pre_get_option_' . $option, false, $option ) ) { 
  240. return $r; 
  241.  
  242. global $bb; 
  243.  
  244. switch ( $option ) { 
  245. case 'site_id': 
  246. if ( isset( $bb->site_id ) && is_numeric( $bb->site_id ) ) { 
  247. $r = (int) $bb->site_id; 
  248. } else { 
  249. $r = 1; 
  250. break; 
  251. case 'language': 
  252. $r = str_replace( '_', '-', bb_get_locale() ); 
  253. break; 
  254. case 'text_direction': 
  255. global $bb_locale; 
  256. $r = $bb_locale->text_direction; 
  257. break; 
  258. case 'version': 
  259. return '1.2'; // Don't filter 
  260. break; 
  261. case 'bb_db_version' : 
  262. return '2471'; // Don't filter 
  263. break; 
  264. case 'html_type': 
  265. $r = 'text/html'; 
  266. break; 
  267. case 'charset': 
  268. $r = 'UTF-8'; 
  269. break; 
  270. case 'bb_table_prefix': 
  271. case 'table_prefix': 
  272. global $bbdb; 
  273. return $bbdb->prefix; // Don't filter
  274. break; 
  275. case 'url': 
  276. $option = 'uri'; 
  277. default: 
  278. if ( isset( $bb->$option ) ) { 
  279. $r = $bb->$option; 
  280. if ( $option === 'mod_rewrite' ) { 
  281. if ( is_bool( $r ) ) { 
  282. $r = (int) $r; 
  283. break; 
  284.  
  285. $r = bb_get_option_from_db( $option ); 
  286.  
  287. if ( !$r ) { 
  288. switch ( $option ) { 
  289. case 'name': 
  290. $r = __( 'Please give me a name!' ); 
  291. break; 
  292. case 'wp_table_prefix' : 
  293. global $wp_table_prefix; // This global is deprecated 
  294. return $wp_table_prefix; // Don't filter
  295. break; 
  296. case 'mod_rewrite': 
  297. $r = 0; 
  298. break; 
  299. case 'page_topics': 
  300. $r = 30; 
  301. break; 
  302. case 'edit_lock': 
  303. $r = 60; 
  304. break; 
  305. case 'gmt_offset': 
  306. $r = 0; 
  307. break; 
  308. case 'uri_ssl': 
  309. $r = preg_replace( '|^http://|i', 'https://', bb_get_option( 'uri' ) ); 
  310. break; 
  311. case 'throttle_time': 
  312. $r = 30; 
  313. break; 
  314. case 'email_login': 
  315. $r = false; 
  316. break; 
  317. break; 
  318.  
  319. return apply_filters( 'bb_get_option_' . $option, $r, $option ); 
  320.  
  321. /** 
  322. * Retrieves and returns the requested bbPress option from the meta table 
  323. * 
  324. * @param string The option to be echoed 
  325. * @return void 
  326. */ 
  327. function bb_get_option_from_db( $option ) 
  328. global $bbdb; 
  329. $option = bb_sanitize_meta_key( $option ); 
  330.  
  331. if ( wp_cache_get( $option, 'bb_option_not_set' ) ) { 
  332. $r = null; 
  333. } elseif ( false !== $_r = wp_cache_get( $option, 'bb_option' ) ) { 
  334. $r = $_r; 
  335. } else { 
  336. if ( BB_INSTALLING ) { 
  337. $bbdb->suppress_errors(); 
  338. $row = $bbdb->get_row( $bbdb->prepare( "SELECT `meta_value` FROM `$bbdb->meta` WHERE `object_type` = 'bb_option' AND `meta_key` = %s", $option ) ); 
  339. if ( BB_INSTALLING ) { 
  340. $bbdb->suppress_errors( false ); 
  341.  
  342. if ( is_object( $row ) ) { 
  343. $r = maybe_unserialize( $row->meta_value ); 
  344. } else { 
  345. $r = null; 
  346.  
  347. if ( $r === null ) { 
  348. wp_cache_set( $option, true, 'bb_option_not_set' ); 
  349. } else { 
  350. wp_cache_set( $option, $r, 'bb_option' ); 
  351.  
  352. return apply_filters( 'bb_get_option_from_db_' . $option, $r, $option ); 
  353.  
  354. function bb_form_option( $option ) 
  355. echo bb_get_form_option( $option ); 
  356.  
  357. function bb_get_form_option( $option ) 
  358. return esc_attr( bb_get_option( $option ) ); 
  359.  
  360. // Don't use the return value; use the API. Only returns options stored in DB. 
  361. function bb_cache_all_options() 
  362. $base_options = array( 
  363. 'site_id',  
  364. 'bb_db_version',  
  365. 'name',  
  366. 'description',  
  367. 'uri_ssl',  
  368. 'from_email',  
  369. 'bb_auth_salt',  
  370. 'bb_secure_auth_salt',  
  371. 'bb_logged_in_salt',  
  372. 'bb_nonce_salt',  
  373. 'page_topics',  
  374. 'edit_lock',  
  375. 'bb_active_theme',  
  376. 'active_plugins',  
  377. 'mod_rewrite',  
  378. 'datetime_format',  
  379. 'date_format',  
  380. 'avatars_show',  
  381. 'avatars_default',  
  382. 'avatars_rating',  
  383. 'wp_table_prefix',  
  384. 'user_bbdb_name',  
  385. 'user_bbdb_user',  
  386. 'user_bbdb_password',  
  387. 'user_bbdb_host',  
  388. 'user_bbdb_charset',  
  389. 'user_bbdb_collate',  
  390. 'custom_user_table',  
  391. 'custom_user_meta_table',  
  392. 'wp_siteurl',  
  393. 'wp_home',  
  394. 'cookiedomain',  
  395. 'usercookie',  
  396. 'passcookie',  
  397. 'authcookie',  
  398. 'cookiepath',  
  399. 'sitecookiepath',  
  400. 'secure_auth_cookie',  
  401. 'logged_in_cookie',  
  402. 'admin_cookie_path',  
  403. 'core_plugins_cookie_path',  
  404. 'user_plugins_cookie_path',  
  405. 'wp_admin_cookie_path',  
  406. 'wp_plugins_cookie_path',  
  407. 'wordpress_mu_primary_blog_id',  
  408. 'enable_loginless',  
  409. 'enable_subscriptions',  
  410. 'enable_xmlrpc',  
  411. 'enable_pingback',  
  412. 'throttle_time',  
  413. 'bb_xmlrpc_allow_user_switching',  
  414. 'bp_bbpress_cron',  
  415. 'email_login',  
  416. 'static_title',  
  417. 'plugin_cookie_paths',  
  418. 'wp_roles_map',  
  419. 'gmt_offset',  
  420. 'timezone_string',  
  421. 'name_link_profile',  
  422. 'bp_bbpress_cron_check',  
  423. ); 
  424.  
  425. // Check that these aren't already in the cache 
  426. $query_options = array(); 
  427. foreach ( $base_options as $base_option ) { 
  428. if ( isset( $bb->$base_option ) ) { 
  429. continue; 
  430. if ( wp_cache_get( $base_option, 'bb_option_not_set' ) ) { 
  431. continue; 
  432. if ( false === wp_cache_get( $base_option, 'bb_option' ) ) { 
  433. $query_options[] = $base_option; 
  434. wp_cache_set( $base_option, true, 'bb_option_not_set' ); 
  435. if ( !count( $query_options ) ) { 
  436. // It's all in cache 
  437. return true; 
  438.  
  439. $query_keys = "('" . join( "', '", $query_options ) . "')"; 
  440.  
  441. global $bbdb; 
  442. $results = $bbdb->get_results( "SELECT `meta_key`, `meta_value` FROM `$bbdb->meta` WHERE `object_type` = 'bb_option' AND `meta_key` IN $query_keys;" ); 
  443.  
  444. if ( count( $base_options ) === count( $query_options ) && ( !$results || !is_array( $results ) || !count( $results ) ) ) { 
  445. // Let's assume that the options haven't been populated from the old topicmeta table 
  446. if ( !BB_INSTALLING && ( !defined( 'BB_DO_NOT_UPGRADE_TOPICMETA' ) || !BB_DO_NOT_UPGRADE_TOPICMETA ) ) { 
  447. $topicmeta_exists = $bbdb->query( "SELECT * FROM $bbdb->topicmeta LIMIT 1" ); 
  448. if ($topicmeta_exists) { 
  449. require_once( BB_PATH . 'bb-admin/includes/defaults.bb-schema.php' ); 
  450. // Create the meta table 
  451. $bbdb->query( $bb_queries['meta'] ); 
  452. // Copy options 
  453. $bbdb->query( "INSERT INTO `$bbdb->meta` (`meta_key`, `meta_value`) SELECT `meta_key`, `meta_value` FROM `$bbdb->topicmeta` WHERE `topic_id` = 0;" ); 
  454. // Copy topic meta 
  455. $bbdb->query( "INSERT INTO `$bbdb->meta` (`object_id`, `meta_key`, `meta_value`) SELECT `topic_id`, `meta_key`, `meta_value` FROM `$bbdb->topicmeta` WHERE `topic_id` != 0;" ); 
  456. // Entries with an object_id are topic meta at this stage 
  457. $bbdb->query( "UPDATE `$bbdb->meta` SET `object_type` = 'bb_topic' WHERE `object_id` != 0" ); 
  458. unset( $topicmeta_exists ); 
  459.  
  460. return bb_cache_all_options(); 
  461.  
  462. return false; 
  463. } else { 
  464. foreach ( $results as $options ) { 
  465. wp_cache_delete( $options->meta_key, 'bb_option_not_set' ); 
  466. wp_cache_set( $options->meta_key, maybe_unserialize( $options->meta_value ), 'bb_option' ); 
  467.  
  468. return true; 
  469.  
  470. // Can store anything but NULL. 
  471. function bb_update_option( $option, $value ) 
  472. return bb_update_meta( 0, $option, $value, 'option', true ); 
  473.  
  474. function bb_delete_option( $option, $value = '' ) 
  475. return bb_delete_meta( 0, $option, $value, 'option', true ); 
  476.  
  477. /** 
  478. * Delete a transient 
  479. * 
  480. * @since 1.0 
  481. * @package bbPress 
  482. * @subpackage Transient 
  483. * 
  484. * @param string $transient Transient name. Expected to not be SQL-escaped 
  485. * @return bool true if successful, false otherwise 
  486. */ 
  487. function bb_delete_transient( $transient ) 
  488. global $_bb_using_ext_object_cache, $bbdb; 
  489.  
  490. if ( $_bb_using_ext_object_cache ) { 
  491. return wp_cache_delete( $transient, 'transient' ); 
  492. } else { 
  493. $transient = '_transient_' . $bbdb->escape( $transient ); 
  494. return bb_delete_option( $transient ); 
  495.  
  496. /** 
  497. * Get the value of a transient 
  498. * 
  499. * If the transient does not exist or does not have a value, then the return value 
  500. * will be false. 
  501. *  
  502. * @since 1.0 
  503. * @package bbPress 
  504. * @subpackage Transient 
  505. * 
  506. * @param string $transient Transient name. Expected to not be SQL-escaped 
  507. * @return mixed Value of transient 
  508. */ 
  509. function bb_get_transient( $transient ) 
  510. global $_bb_using_ext_object_cache, $bbdb; 
  511.  
  512. if ( $_bb_using_ext_object_cache ) { 
  513. $value = wp_cache_get( $transient, 'transient' ); 
  514. } else { 
  515. $transient_option = '_transient_' . $bbdb->escape( $transient ); 
  516. $transient_timeout = '_transient_timeout_' . $bbdb->escape( $transient ); 
  517. $timeout = bb_get_option( $transient_timeout ); 
  518. if ( $timeout && $timeout < time() ) { 
  519. bb_delete_option( $transient_option ); 
  520. bb_delete_option( $transient_timeout ); 
  521. return false; 
  522.  
  523. $value = bb_get_option( $transient_option ); 
  524.  
  525. return apply_filters( 'transient_' . $transient, $value ); 
  526.  
  527. /** 
  528. * Set/update the value of a transient 
  529. * 
  530. * You do not need to serialize values, if the value needs to be serialize, then 
  531. * it will be serialized before it is set. 
  532. * 
  533. * @since 1.0 
  534. * @package bbPress 
  535. * @subpackage Transient 
  536. * 
  537. * @param string $transient Transient name. Expected to not be SQL-escaped 
  538. * @param mixed $value Transient value. 
  539. * @param int $expiration Time until expiration in seconds, default 0 
  540. * @return bool False if value was not set and true if value was set. 
  541. */ 
  542. function bb_set_transient( $transient, $value, $expiration = 0 ) 
  543. global $_bb_using_ext_object_cache, $bbdb; 
  544.  
  545. if ( $_bb_using_ext_object_cache ) { 
  546. return wp_cache_set( $transient, $value, 'transient', $expiration ); 
  547. } else { 
  548. $transient_timeout = '_transient_timeout_' . $bbdb->escape( $transient ); 
  549. $transient = '_transient_' . $bbdb->escape( $transient ); 
  550. if ( 0 != $expiration ) { 
  551. bb_update_option( $transient_timeout, time() + $expiration ); 
  552. return bb_update_option( $transient, $value ); 
  553.  
  554.  
  555.  
  556. /** User meta */ 
  557.  
  558. function bb_get_usermeta( $user_id, $meta_key ) 
  559. if ( !$user = bb_get_user( $user_id ) ) { 
  560. return; 
  561.  
  562. $meta_key = bb_sanitize_meta_key( $meta_key ); 
  563. if ( !isset( $user->$meta_key ) ) { 
  564. return; 
  565. return $user->$meta_key; 
  566.  
  567. function bb_update_usermeta( $user_id, $meta_key, $meta_value ) 
  568. return bb_update_meta( $user_id, $meta_key, $meta_value, 'user' ); 
  569.  
  570. // $meta_value defaults to null to conform to BackPress' WP_User::delete_meta() 
  571. function bb_delete_usermeta( $user_id, $meta_key, $meta_value = null ) 
  572. return bb_delete_meta( $user_id, $meta_key, $meta_value, 'user' ); 
  573.  
  574. /** 
  575. * Saves and restores user interface settings stored in a cookie. 
  576. * 
  577. * Checks if the current user-settings cookie is updated and stores it. When no 
  578. * cookie exists (different browser used), adds the last saved cookie restoring 
  579. * the settings. 
  580. * 
  581. * @package bbPress 
  582. * @subpackage Meta 
  583. * @since 1.0 
  584. */ 
  585. function bb_user_settings() 
  586. if ( !defined( 'BB_IS_ADMIN' ) || !BB_IS_ADMIN ) { 
  587. return; 
  588.  
  589. if ( defined( 'DOING_AJAX' ) ) { 
  590. return; 
  591.  
  592. if ( !$user = bb_get_current_user() ) { 
  593. return; 
  594.  
  595. $settings = bb_get_usermeta( $user->ID, 'bb_user_settings' ); 
  596.  
  597. if ( isset( $_COOKIE['bb-user-settings-' . $user->ID] ) ) { 
  598. $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['bb-user-settings-' . $user->ID] ); 
  599.  
  600. if ( !empty( $cookie ) && strpos( $cookie, '=' ) ) { 
  601. if ( $cookie == $settings ) { 
  602. return; 
  603.  
  604. $last_time = (int) bb_get_usermeta( $user->ID, 'bb_user_settings_time' ); 
  605. $saved = isset( $_COOKIE['bb-user-settings-time-' . $user->ID] ) ? preg_replace( '/[^0-9]/', '', $_COOKIE['bb-user-settings-time-' . $user->ID] ) : 0; 
  606.  
  607. if ( $saved > $last_time ) { 
  608. bb_update_usermeta( $user->ID, 'bb_user_settings', $cookie ); 
  609. bb_update_usermeta( $user->ID, 'bb_user_settings_time', time() - 5 ); 
  610. return; 
  611.  
  612. setcookie( 'bb-user-settings-' . $user->ID, $settings, time() + 31536000, $bb->cookiepath ); 
  613. setcookie( 'bb-user-settings-time-' . $user->ID, time(), time() + 31536000, $bb->cookiepath ); 
  614.  
  615. /** 
  616. * Retrieve user interface setting value based on setting name. 
  617. * 
  618. * @package bbPress 
  619. * @subpackage Meta 
  620. * @since 1.0 
  621. * 
  622. * @param string $name The name of the setting. 
  623. * @param string $default Optional default value to return when $name is not set. 
  624. * @return mixed the last saved user setting or the default value/false if it doesn't exist. 
  625. */ 
  626. function bb_get_user_setting( $name, $default = false ) 
  627. $arr = bb_get_all_user_settings(); 
  628.  
  629. return isset( $arr[$name] ) ? $arr[$name] : $default; 
  630.  
  631. /** 
  632. * Adds or updates a user interface setting value based on setting name. 
  633. * 
  634. * @package bbPress 
  635. * @subpackage Meta 
  636. * @since 1.0 
  637. * 
  638. * @param string $name The name of the setting. 
  639. * @param string $value The value of the setting. 
  640. */ 
  641. function bb_update_user_setting( $name, $value ) 
  642. if ( is_null( $value ) || $value === '' ) { 
  643. return; 
  644.  
  645. if ( !$user = bb_get_current_user() ) { 
  646. return; 
  647.  
  648. $name = (string) $name; 
  649. $name = preg_replace( '/[^A-Za-z0-9_]/', '', $name ); 
  650.  
  651. $value = (string) $value; 
  652. $value = preg_replace( '/[^A-Za-z0-9_]/', '', $value ); 
  653.  
  654. $arr = bb_get_all_user_settings(); 
  655. $arr[$name] = $value; 
  656.  
  657. foreach ( $arr as $k => $v ) { 
  658. $settings .= $k . '=' . $v . '&'; 
  659. $settings = rtrim( $settings, '&' ); 
  660.  
  661. bb_update_usermeta( $user->ID, 'bb_user_settings', $settings ); 
  662. setcookie( 'bb-user-settings-' . $user->ID, $settings, time() + 31536000, $bb->cookiepath ); 
  663.  
  664. /** 
  665. * Delete user interface settings. 
  666. * 
  667. * Deleting settings would reset them to the defaults. 
  668. * 
  669. * @package bbPress 
  670. * @subpackage Meta 
  671. * @since 1.0 
  672. * 
  673. * @param mixed $names The name or array of names of the setting to be deleted. 
  674. */ 
  675. function bb_delete_user_setting( $names ) 
  676. $arr = bb_get_all_user_settings(); 
  677. $names = (array) $names; 
  678.  
  679. if ( !$user = bb_get_current_user() ) { 
  680. return; 
  681.  
  682. foreach ( $names as $name ) { 
  683. if ( isset( $arr[$name] ) ) { 
  684. unset( $arr[$name] ); 
  685. $settings = ''; 
  686.  
  687. if ( isset( $settings ) ) { 
  688. foreach ( $arr as $k => $v ) { 
  689. $settings .= $k . '=' . $v . '&'; 
  690. $settings = rtrim( $settings, '&' ); 
  691.  
  692. bb_update_usermeta( $user->ID, 'bb_user_settings', $settings ); 
  693. setcookie( 'bb-user-settings-' . $user->ID, $settings, time() + 31536000, $bb->cookiepath ); 
  694.  
  695. /** 
  696. * Retrieve all user interface settings. 
  697. * 
  698. * @package bbPress 
  699. * @subpackage Meta 
  700. * @since 1.0 
  701. * 
  702. * @return array the last saved user settings or empty array. 
  703. */ 
  704. function bb_get_all_user_settings() 
  705. if ( ! $user = bb_get_current_user() ) { 
  706. return array(); 
  707.  
  708. $arr = array(); 
  709. if ( isset( $_COOKIE['bb-user-settings-' . $user->ID] ) ) { 
  710. $cookie = preg_replace( '/[^A-Za-z0-9=&_]/', '', $_COOKIE['bb-user-settings-' . $user->ID] ); 
  711.  
  712. if ( $cookie && strpos( $cookie, '=' ) ) { // the '=' cannot be 1st char 
  713. parse_str( $cookie, $arr ); 
  714. } elseif ( isset( $user->bb_user_settings ) && is_string( $user->bb_user_settings ) ) { 
  715. parse_str( $user->bb_user_settings, $arr ); 
  716.  
  717. return $arr; 
  718.  
  719. /** 
  720. * Delete the user settings of the current user. 
  721. * 
  722. * @package bbPress 
  723. * @subpackage Meta 
  724. * @since 1.0 
  725. */ 
  726. function bb_delete_all_user_settings() 
  727. if ( !$user = bb_get_current_user() ) { 
  728. return; 
  729.  
  730. bb_delete_usermeta( $user->ID, 'bb_user_settings' ); 
  731. setcookie( 'bb-user-settings-' . $user->ID, ' ', time() - 31536000, $bb->cookiepath ); 
  732.  
  733.  
  734.  
  735.  
  736. /** Forum meta */ 
  737.  
  738. function bb_get_forummeta( $forum_id, $meta_key ) 
  739. if ( !$forum = bb_get_forum( $forum_id ) ) { 
  740. return; 
  741.  
  742. $meta_key = bb_sanitize_meta_key( $meta_key ); 
  743. if ( !isset( $forum->$meta_key ) ) { 
  744. return; 
  745. return $forum->$meta_key; 
  746.  
  747. function bb_update_forummeta( $forum_id, $meta_key, $meta_value ) 
  748. return bb_update_meta( $forum_id, $meta_key, $meta_value, 'forum' ); 
  749.  
  750. function bb_delete_forummeta( $forum_id, $meta_key, $meta_value = '' ) 
  751. return bb_delete_meta( $forum_id, $meta_key, $meta_value, 'forum' ); 
  752.  
  753.  
  754.  
  755. /** Topic meta */ 
  756.  
  757. function bb_get_topicmeta( $topic_id, $meta_key ) 
  758. if ( !$topic = get_topic( $topic_id ) ) { 
  759. return; 
  760.  
  761. $meta_key = bb_sanitize_meta_key( $meta_key ); 
  762. if ( !isset($topic->$meta_key) ) { 
  763. return; 
  764. return $topic->$meta_key; 
  765.  
  766. function bb_update_topicmeta( $topic_id, $meta_key, $meta_value ) 
  767. return bb_update_meta( $topic_id, $meta_key, $meta_value, 'topic' ); 
  768.  
  769. function bb_delete_topicmeta( $topic_id, $meta_key, $meta_value = '' ) 
  770. return bb_delete_meta( $topic_id, $meta_key, $meta_value, 'topic' ); 
  771.  
  772.  
  773.  
  774. /** Post meta */ 
  775.  
  776. function bb_get_postmeta( $post_id, $meta_key ) 
  777. if ( !$post = bb_get_post( $post_id ) ) { 
  778. return; 
  779.  
  780. $meta_key = bb_sanitize_meta_key( $meta_key ); 
  781. if ( !isset( $post->$meta_key ) ) { 
  782. return; 
  783. return $post->$meta_key; 
  784.  
  785. function bb_update_postmeta( $post_id, $meta_key, $meta_value ) 
  786. return bb_update_meta( $post_id, $meta_key, $meta_value, 'post' ); 
  787.  
  788. function bb_delete_postmeta( $post_id, $meta_key, $meta_value = '' ) 
  789. return bb_delete_meta( $post_id, $meta_key, $meta_value, 'post' ); 
.