Skip to:
Content

bbPress.org

Changeset 1919


Ignore:
Timestamp:
01/20/2009 01:38:29 AM (18 years ago)
Author:
sambauers
Message:

Coding standards etc. for meta functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bb-includes/functions.bb-meta.php

    r1913 r1919  
    33/* Options/Meta */
    44
    5 function bb_option( $option ) {
     5
     6/* Internal */
     7
     8/**
     9 * Adds and updates meta data in the database
     10 *
     11 * @internal
     12 */
     13function bb_update_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false )
     14{
     15        global $bbdb;
     16        if ( !is_numeric( $object_id ) || empty( $object_id ) && !$global ) {
     17                return false;
     18        }
     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                        }
     31                        return $return;
     32                        break;
     33                case 'forum' :
     34                        $object_type = 'bb_forum';
     35                        break;
     36                case 'topic' :
     37                        $object_type = 'bb_topic';
     38                        break;
     39                case 'post' :
     40                        $object_type = 'bb_post';
     41                        break;
     42                default :
     43                        $object_type = $type;
     44                        break;
     45        }
     46
     47        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     48
     49        $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
     50        $meta_tuple = apply_filters( 'bb_update_meta', $meta_tuple );
     51        extract( $meta_tuple, EXTR_OVERWRITE );
     52
     53        $meta_value = $_meta_value = maybe_serialize( $meta_value );
     54        $meta_value = maybe_unserialize( $meta_value );
     55
     56        $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 ) );
     57        if ( !$cur ) {
     58                $bbdb->insert( $bbdb->meta, array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key, 'meta_value' => $_meta_value ) );
     59        } elseif ( $cur->meta_value != $meta_value ) {
     60                $bbdb->update( $bbdb->meta, array( 'meta_value' => $_meta_value), array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key ) );
     61        }
     62
     63        if ( $object_type == 'bb_option' ) {
     64                $cache_object_id = $meta_key;
     65        }
     66        wp_cache_delete( $cache_object_id, $object_type );
     67        if ( !$cur ) {
     68                return true;
     69        }
     70}
     71
     72/**
     73 * Deletes meta data from the database
     74 *
     75 * @internal
     76 */
     77function bb_delete_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false )
     78{
     79        global $bbdb;
     80        if ( !is_numeric( $object_id ) || empty( $object_id ) && !$global ) {
     81                return false;
     82        }
     83        $cache_object_id = $object_id = (int) $object_id;
     84        switch ( $type ) {
     85                case 'option':
     86                        $object_type = 'bb_option';
     87                        break;
     88                case 'user':
     89                        global $wp_users_object;
     90                        $id = $object_id;
     91                        return $wp_users_object->update_meta( compact( 'id', 'meta_key', 'meta_value' ) );
     92                        break;
     93                case 'forum':
     94                        $object_type = 'bb_forum';
     95                        break;
     96                case 'topic':
     97                        $object_type = 'bb_topic';
     98                        break;
     99                case 'post':
     100                        $object_type = 'bb_post';
     101                        break;
     102                default:
     103                        $object_type = $type;
     104                        break;
     105        }
     106
     107        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     108
     109        $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' );
     110        $meta_tuple = apply_filters( 'bb_delete_meta', $meta_tuple );
     111        extract( $meta_tuple, EXTR_OVERWRITE );
     112
     113        $meta_value = maybe_serialize( $meta_value );
     114
     115        if ( empty( $meta_value ) ) {
     116                $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 );
     117        } else {
     118                $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 );
     119        }
     120
     121        if ( !$meta_id = $bbdb->get_var( $meta_sql ) ) {
     122                return false;
     123        }
     124
     125        $bbdb->query( $bbdb->prepare( "DELETE FROM `$bbdb->meta` WHERE `meta_id` = %d", $meta_id ) );
     126
     127        if ( $object_type == 'bb_option' ) {
     128                $cache_object_id = $meta_key;
     129        }
     130        wp_cache_delete( $cache_object_id, $object_type );
     131        return true;
     132}
     133
     134/**
     135 * Adds an objects meta data to the object
     136 *
     137 * This is the only function that should add to user / topic - NOT bbdb::prepared
     138 *
     139 * @internal
     140 */
     141function bb_append_meta( $object, $type )
     142{
     143        global $bbdb;
     144        switch ( $type ) {
     145                case 'user':
     146                        global $wp_users_object;
     147                        return $wp_users_object->append_meta( $object );
     148                        break;
     149                case 'forum':
     150                        $object_id_column = 'forum_id';
     151                        $object_type = 'bb_forum';
     152                        $slug = 'forum_slug';
     153                        break;
     154                case 'topic':
     155                        $object_id_column = 'topic_id';
     156                        $object_type = 'bb_topic';
     157                        $slug = 'topic_slug';
     158                        break;
     159                case 'post':
     160                        $object_id_column = 'post_id';
     161                        $object_type = 'bb_post';
     162                        $slug = 'post_slug';
     163                        break;
     164        }
     165
     166        if ( is_array( $object ) && $object ) {
     167                $trans = array();
     168                foreach ( array_keys( $object ) as $i ) {
     169                        $trans[$object[$i]->$object_id_column] =& $object[$i];
     170                }
     171                $ids = join( ',', array_map( 'intval', array_keys( $trans ) ) );
     172                if ( $metas = $bbdb->get_results( "SELECT `object_id`, `meta_key`, `meta_value` FROM `$bbdb->meta` WHERE `object_type` = '$object_type' AND `object_id` IN ($ids) /* bb_append_meta */" ) ) {
     173                        usort( $metas, '_bb_append_meta_sort' );
     174                        foreach ( $metas as $meta ) {
     175                                $trans[$meta->object_id]->{$meta->meta_key} = maybe_unserialize( $meta->meta_value );
     176                                if ( strpos($meta->meta_key, $bbdb->prefix) === 0 ) {
     177                                        $trans[$meta->object_id]->{substr($meta->meta_key, strlen($bbdb->prefix))} = maybe_unserialize( $meta->meta_value );
     178                                }
     179                        }
     180                }
     181                foreach ( array_keys( $trans ) as $i ) {
     182                        wp_cache_add( $i, $trans[$i], $object_type );
     183                        if ( $slug ) {
     184                                wp_cache_add( $trans[$i]->$slug, $i, 'bb_' . $slug );
     185                        }
     186                }
     187                return $object;
     188        } elseif ( $object ) {
     189                if ( $metas = $bbdb->get_results( $bbdb->prepare( "SELECT `meta_key`, `meta_value` FROM `$bbdb->meta` WHERE `object_type` = '$object_type' AND `object_id` = %d /* bb_append_meta */", $object->$object_id_column ) ) ) {
     190                        usort( $metas, '_bb_append_meta_sort' );
     191                        foreach ( $metas as $meta ) {
     192                                $object->{$meta->meta_key} = maybe_unserialize( $meta->meta_value );
     193                                if ( strpos( $meta->meta_key, $bbdb->prefix ) === 0 ) {
     194                                        $object->{substr( $meta->meta_key, strlen( $bbdb->prefix ) )} = $object->{$meta->meta_key};
     195                                }
     196                        }
     197                }
     198                if ( $object->$object_id_column ) {
     199                        wp_cache_set( $object->$object_id_column, $object, $object_type );
     200                        if ( $slug ) {
     201                                wp_cache_add( $object->$slug, $object->$object_id_column, 'bb_' . $slug );
     202                        }
     203                }
     204                return $object;
     205        }
     206}
     207
     208/**
     209 * Sorts meta keys by length to ensure $appended_object->{$bbdb->prefix} key overwrites $appended_object->key as desired
     210 *
     211 * @internal
     212 */
     213function _bb_append_meta_sort( $a, $b )
     214{
     215        return strlen( $a->meta_key ) - strlen( $b->meta_key );
     216}
     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 */
     228function bb_option( $option )
     229{
    6230        echo apply_filters( 'bb_option_' . $option, bb_get_option( $option ) );
    7231}
    8232
    9 function bb_get_option( $option ) {
     233/**
     234 * Returns the requested bbPress option from the meta table or the $bb object
     235 *
     236 * @param string The option to be echoed
     237 * @return mixed The value of the option
     238 */
     239function bb_get_option( $option )
     240{
    10241        global $bb;
    11242
    12         switch ( $option ) :
    13         case 'language':
    14                 $r = str_replace('_', '-', get_locale());
    15                 break;
    16         case 'text_direction':
    17                 global $bb_locale;
    18                 $r = $bb_locale->text_direction;
    19                 break;
    20         case 'version' :
    21                 return '1.0-alpha-6'; // Don't filter
    22                 break;
    23         case 'bb_db_version' :
    24                 return '1884'; // Don't filter
    25                 break;
    26         case 'html_type' :
    27                 $r = 'text/html';
    28                 break;
    29         case 'charset' :
    30                 $r = 'UTF-8';
    31                 break;
    32         case 'url' :
    33                 $option = 'uri';
    34         case 'bb_table_prefix' :
    35         case 'table_prefix' :
    36                 global $bbdb;
    37                 return $bbdb->prefix; // Don't filter;
    38                 break;
    39         default :
    40                 if ( isset($bb->$option) ) {
    41                         $r = $bb->$option;
    42                         if ($option == 'mod_rewrite')
    43                                 if (is_bool($r))
    44                                         $r = (integer) $r;
    45                         break;
    46                 }
    47                
    48                 $r = bb_get_option_from_db( $option );
    49                
    50                 if (!$r) {
    51                         switch ($option) {
    52                                 case 'wp_table_prefix' :
    53                                         global $wp_table_prefix;
    54                                         return $wp_table_prefix; // Don't filter;
    55                                         break;
    56                                 case 'mod_rewrite':
    57                                         $r = 0;
    58                                         break;
    59                                 case 'page_topics':
    60                                         $r = 30;
    61                                         break;
    62                                 case 'edit_lock':
    63                                         $r = 60;
    64                                         break;
    65                                 case 'gmt_offset':
    66                                         $r = 0;
    67                                         break;
    68                                 case 'uri_ssl':
    69                                         $r = preg_replace('|^http://|i', 'https://', bb_get_option('uri'));
    70                                         break;
    71                         }
    72                 }
    73                
    74                 break;
    75         endswitch;
    76         return apply_filters( 'bb_get_option_' . $option, $r, $option);
    77 }
    78 
    79 function bb_get_option_from_db( $option ) {
     243        switch ( $option ) {
     244                case 'language':
     245                        $r = str_replace( '_', '-', get_locale() );
     246                        break;
     247                case 'text_direction':
     248                        global $bb_locale;
     249                        $r = $bb_locale->text_direction;
     250                        break;
     251                case 'version':
     252                        return '1.0-alpha-6'; // Don't filter
     253                        break;
     254                case 'bb_db_version' :
     255                        return '1884'; // Don't filter
     256                        break;
     257                case 'html_type':
     258                        $r = 'text/html';
     259                        break;
     260                case 'charset':
     261                        $r = 'UTF-8';
     262                        break;
     263                case 'bb_table_prefix':
     264                case 'table_prefix':
     265                        global $bbdb;
     266                        return $bbdb->prefix; // Don't filter;
     267                        break;
     268                case 'url':
     269                        $option = 'uri';
     270                default:
     271                        if ( isset( $bb->$option ) ) {
     272                                $r = $bb->$option;
     273                                if ( $option === 'mod_rewrite' ) {
     274                                        if ( is_bool( $r ) ) {
     275                                                $r = (integer) $r;
     276                                        }
     277                                }
     278                                break;
     279                        }
     280
     281                        $r = bb_get_option_from_db( $option );
     282
     283                        if ( !$r ) {
     284                                switch ( $option ) {
     285                                        case 'wp_table_prefix' :
     286                                                global $wp_table_prefix;
     287                                                return $wp_table_prefix; // Don't filter;
     288                                                break;
     289                                        case 'mod_rewrite':
     290                                                $r = 0;
     291                                                break;
     292                                        case 'page_topics':
     293                                                $r = 30;
     294                                                break;
     295                                        case 'edit_lock':
     296                                                $r = 60;
     297                                                break;
     298                                        case 'gmt_offset':
     299                                                $r = 0;
     300                                                break;
     301                                        case 'uri_ssl':
     302                                                $r = preg_replace( '|^http://|i', 'https://', bb_get_option( 'uri' ) );
     303                                                break;
     304                                }
     305                        }
     306                        break;
     307        }
     308
     309        return apply_filters( 'bb_get_option_' . $option, $r, $option );
     310}
     311
     312/**
     313 * Retrieves and returns the requested bbPress option from the meta table
     314 *
     315 * @param string The option to be echoed
     316 * @return void
     317 */
     318function bb_get_option_from_db( $option )
     319{
    80320        global $bbdb;
    81         $option = preg_replace('|[^a-z0-9_]|i', '', $option);
     321        $option = preg_replace( '|[^a-z0-9_]|i', '', $option );
    82322
    83323        if ( false === $r = wp_cache_get( $option, 'bb_option' ) ) {
    84324                if ( BB_INSTALLING ) $bbdb->suppress_errors();
    85                 $row = $bbdb->get_row( $bbdb->prepare( "SELECT meta_value FROM $bbdb->meta WHERE object_type = 'bb_option' AND meta_key = %s", $option ) );
     325                $row = $bbdb->get_row( $bbdb->prepare( "SELECT `meta_value` FROM `$bbdb->meta` WHERE `object_type` = 'bb_option' AND `meta_key` = %s", $option ) );
    86326                if ( BB_INSTALLING ) $bbdb->suppress_errors(false);
    87327
    88                 if ( is_object($row) ) {
     328                if ( is_object( $row ) ) {
    89329                        $r = maybe_unserialize( $row->meta_value );
    90330                        wp_cache_set( $option, $r, 'bb_option' );
     
    96336}
    97337
    98 function bb_form_option( $option ) {
     338function bb_form_option( $option )
     339{
    99340        echo bb_get_form_option( $option );
    100341}
    101342
    102 function bb_get_form_option( $option ) {
     343function bb_get_form_option( $option )
     344{
    103345        return attribute_escape( bb_get_option( $option ) );
    104346}
    105347
    106 function bb_cache_all_options() { // Don't use the return value; use the API.  Only returns options stored in DB.
     348// Don't use the return value; use the API. Only returns options stored in DB.
     349function bb_cache_all_options()
     350{
    107351        global $bbdb;
    108         $results = $bbdb->get_results( "SELECT meta_key, meta_value FROM $bbdb->meta WHERE object_type = 'bb_option'" );
    109 
    110         if (!$results || !is_array($results) || !count($results)) {
     352        $results = $bbdb->get_results( "SELECT `meta_key`, `meta_value` FROM `$bbdb->meta` WHERE `object_type` = 'bb_option'" );
     353
     354        if ( !$results || !is_array( $results ) || !count( $results ) ) {
    111355                // Let's assume that the options haven't been populated from the old topicmeta table
    112356                if ( !BB_INSTALLING ) {
    113                         $topicmeta_exists = $bbdb->query("SELECT * FROM $bbdb->topicmeta LIMIT 1");
     357                        $topicmeta_exists = $bbdb->query( "SELECT * FROM $bbdb->topicmeta LIMIT 1" );
    114358                        if ($topicmeta_exists) {
    115359                                require_once( BB_PATH . 'bb-admin/includes/defaults.bb-schema.php' );
    116360                                // Create the meta table
    117                                 $bbdb->query($bb_queries['meta']);
     361                                $bbdb->query( $bb_queries['meta'] );
    118362                                // Copy options
    119                                 $bbdb->query("INSERT INTO `$bbdb->meta` (`meta_key`, `meta_value`) SELECT `meta_key`, `meta_value` FROM `$bbdb->topicmeta` WHERE `topic_id` = 0;");
     363                                $bbdb->query( "INSERT INTO `$bbdb->meta` (`meta_key`, `meta_value`) SELECT `meta_key`, `meta_value` FROM `$bbdb->topicmeta` WHERE `topic_id` = 0;" );
    120364                                // Copy topic meta
    121                                 $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;");
     365                                $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;" );
    122366                                // Entries with an object_id are topic meta at this stage
    123                                 $bbdb->query("UPDATE `$bbdb->meta` SET `object_type` = 'bb_topic' WHERE `object_id` != 0");
     367                                $bbdb->query( "UPDATE `$bbdb->meta` SET `object_type` = 'bb_topic' WHERE `object_id` != 0" );
    124368                        }
    125369                        unset($topicmeta_exists);
    126                        
     370
    127371                        return bb_cache_all_options();
    128372                }
    129                
     373
    130374                return false;
    131375        } else {
    132                 foreach ( $results as $options )
     376                foreach ( $results as $options ) {
    133377                        wp_cache_set( $options->meta_key, maybe_unserialize($options->meta_value), 'bb_option' );
    134         }
    135        
     378                }
     379        }
     380
    136381        $base_options = array(
    137382                'bb_db_version' => 0,
     
    180425                'bb_xmlrpc_allow_user_switching' => false
    181426        );
    182        
    183         foreach ( $base_options as $base_option => $base_option_default )
    184                 if ( false === wp_cache_get( $base_option, 'bb_option' ) )
     427
     428        foreach ( $base_options as $base_option => $base_option_default ) {
     429                if ( false === wp_cache_get( $base_option, 'bb_option' ) ) {
    185430                        wp_cache_set( $base_option, $base_option_default, 'bb_option' );
    186        
     431                }
     432        }
     433
    187434        return true;
    188435}
    189436
    190437// Can store anything but NULL.
    191 function bb_update_option( $option, $value ) {
     438function bb_update_option( $option, $value )
     439{
    192440        return bb_update_meta( 0, $option, $value, 'option', true );
    193441}
    194442
    195 function bb_delete_option( $option, $value = '' ) {
     443function bb_delete_option( $option, $value = '' )
     444{
    196445        return bb_delete_meta( 0, $option, $value, 'option', true );
    197446}
     
    202451 * @since 1.0
    203452 */
    204 define('BB_URI_CONTEXT_HEADER',               1);
    205 define('BB_URI_CONTEXT_TEXT',                 2);
    206 define('BB_URI_CONTEXT_A_HREF',               4);
    207 define('BB_URI_CONTEXT_FORM_ACTION',          8);
    208 define('BB_URI_CONTEXT_IMG_SRC',              16);
    209 define('BB_URI_CONTEXT_LINK_STYLESHEET_HREF', 32);
    210 define('BB_URI_CONTEXT_LINK_ALTERNATE_HREF',  64);
    211 define('BB_URI_CONTEXT_LINK_OTHER',           128);
    212 define('BB_URI_CONTEXT_SCRIPT_SRC',           256);
    213 define('BB_URI_CONTEXT_IFRAME_SRC',           512);
    214 define('BB_URI_CONTEXT_BB_FEED',              1024);
    215 define('BB_URI_CONTEXT_BB_USER_FORMS',        2048);
    216 define('BB_URI_CONTEXT_BB_ADMIN',             4096);
    217 define('BB_URI_CONTEXT_BB_XMLRPC',            8192);
    218 define('BB_URI_CONTEXT_WP_HTTP_REQUEST',      16384);
    219 //define('BB_URI_CONTEXT_*',                    32768);  // Reserved for future definitions
    220 //define('BB_URI_CONTEXT_*',                    65536);  // Reserved for future definitions
    221 //define('BB_URI_CONTEXT_*',                    131072); // Reserved for future definitions
    222 //define('BB_URI_CONTEXT_*',                    262144); // Reserved for future definitions
    223 define('BB_URI_CONTEXT_AKISMET',              524288);
     453define( 'BB_URI_CONTEXT_HEADER',               1 );
     454define( 'BB_URI_CONTEXT_TEXT',                 2 );
     455define( 'BB_URI_CONTEXT_A_HREF',               4 );
     456define( 'BB_URI_CONTEXT_FORM_ACTION',          8 );
     457define( 'BB_URI_CONTEXT_IMG_SRC',              16 );
     458define( 'BB_URI_CONTEXT_LINK_STYLESHEET_HREF', 32 );
     459define( 'BB_URI_CONTEXT_LINK_ALTERNATE_HREF',  64 );
     460define( 'BB_URI_CONTEXT_LINK_OTHER',           128 );
     461define( 'BB_URI_CONTEXT_SCRIPT_SRC',           256 );
     462define( 'BB_URI_CONTEXT_IFRAME_SRC',           512 );
     463define( 'BB_URI_CONTEXT_BB_FEED',              1024 );
     464define( 'BB_URI_CONTEXT_BB_USER_FORMS',        2048 );
     465define( 'BB_URI_CONTEXT_BB_ADMIN',             4096 );
     466define( 'BB_URI_CONTEXT_BB_XMLRPC',            8192 );
     467define( 'BB_URI_CONTEXT_WP_HTTP_REQUEST',      16384 );
     468//define( 'BB_URI_CONTEXT_*',                    32768 );  // Reserved for future definitions
     469//define( 'BB_URI_CONTEXT_*',                    65536 );  // Reserved for future definitions
     470//define( 'BB_URI_CONTEXT_*',                    131072 ); // Reserved for future definitions
     471//define( 'BB_URI_CONTEXT_*',                    262144 ); // Reserved for future definitions
     472define( 'BB_URI_CONTEXT_AKISMET',              524288 );
    224473
    225474/**
     
    233482 * @return void
    234483 */
    235 function bb_uri($resource = null, $query = null, $context = BB_URI_CONTEXT_A_HREF) {
    236         echo apply_filters('bb_uri', bb_get_uri($resource, $query, $context), $resource, $query, $context);
     484function bb_uri( $resource = null, $query = null, $context = BB_URI_CONTEXT_A_HREF )
     485{
     486        echo apply_filters( 'bb_uri', bb_get_uri( $resource, $query, $context ), $resource, $query, $context );
    237487}
    238488
     
    247497 * @return string The complete URI
    248498 */
    249 function bb_get_uri($resource = null, $query = null, $context = BB_URI_CONTEXT_A_HREF) {
     499function bb_get_uri( $resource = null, $query = null, $context = BB_URI_CONTEXT_A_HREF )
     500{
    250501        // If there is a querystring in the resource then extract it
    251         if ($resource && strpos($resource, '?') !== false) {
    252                 list($_resource, $_query) = explode('?', trim($resource));
     502        if ( $resource && strpos( $resource, '?' ) !== false ) {
     503                list( $_resource, $_query ) = explode( '?', trim( $resource ), 2 );
    253504                $resource = $_resource;
    254                 $_query = wp_parse_args($_query);
     505                $_query = wp_parse_args( $_query );
    255506        } else {
    256507                // Make sure $_query is an array for array_merge()
    257508                $_query = array();
    258509        }
    259        
     510
    260511        // $query can be an array as well as a string
    261         if ($query) {
    262                 if (is_string($query)) {
    263                         $query = ltrim(trim($query), '?');
    264                 }
    265                 $query = wp_parse_args($query);
    266         }
    267        
     512        if ( $query ) {
     513                if ( is_string( $query ) ) {
     514                        $query = ltrim( trim( $query ), '?' );
     515                }
     516                $query = wp_parse_args( $query );
     517        }
     518
    268519        // Make sure $query is an array for array_merge()
    269         if (!$query) {
     520        if ( !$query ) {
    270521                $query = array();
    271522        }
    272        
     523
    273524        // Merge the queries into a single array
    274         $query = array_merge($_query, $query);
    275        
     525        $query = array_merge( $_query, $query );
     526
    276527        // Make sure context is an integer
    277         if (!$context || !is_integer($context)) {
     528        if ( !$context || !is_integer( $context ) ) {
    278529                $context = BB_URI_CONTEXT_A_HREF;
    279530        }
    280        
     531
    281532        // Get the base URI
    282         $uri = bb_get_option('uri');
    283        
    284         // Force https when required on user forms
    285         if (($context & BB_URI_CONTEXT_BB_USER_FORMS) && bb_force_ssl_user_forms()) {
    286                 $uri = bb_get_option('uri_ssl');
    287         }
    288        
    289         // Force https when required in admin
    290         if (($context & BB_URI_CONTEXT_BB_ADMIN) && bb_force_ssl_admin()) {
    291                 $uri = bb_get_option('uri_ssl');
    292         }
    293        
     533        static $_uri;
     534        if( !isset( $_uri ) ) {
     535                $_uri = bb_get_option( 'uri' );
     536        }
     537        $uri = $_uri;
     538
     539        // Use https?
     540        if (
     541                ( ( $context & BB_URI_CONTEXT_BB_USER_FORMS ) && bb_force_ssl_user_forms() ) // Force https when required on user forms
     542        ||
     543                ( ( $context & BB_URI_CONTEXT_BB_ADMIN ) && bb_force_ssl_admin() ) // Force https when required in admin
     544        ) {
     545                static $_uri_ssl;
     546                if( !isset( $_uri_ssl ) ) {
     547                        $_uri_ssl = bb_get_option( 'uri_ssl' );
     548                }
     549                $uri = $_uri_ssl;
     550        }
     551
    294552        // Add the directory
    295         $uri .= ltrim($resource, '/');
    296        
     553        $uri .= ltrim( $resource, '/' );
     554
    297555        // Add the query string to the URI
    298         $uri = add_query_arg($query, $uri);
    299        
    300         return apply_filters('bb_get_uri', $uri, $resource, $context);
     556        $uri = add_query_arg( $query, $uri );
     557
     558        return apply_filters( 'bb_get_uri', $uri, $resource, $context );
    301559}
    302560
     
    309567 * @return bool True if forced, false if not forced.
    310568 */
    311 function bb_force_ssl_user_forms($force = '') {
     569function bb_force_ssl_user_forms( $force = '' )
     570{
    312571        static $forced;
    313        
     572
    314573        if ( '' != $force ) {
    315574                $old_forced = $forced;
     
    317576                return $old_forced;
    318577        }
    319        
     578
    320579        return $forced;
    321580}
     
    329588 * @return bool True if forced, false if not forced.
    330589 */
    331 function bb_force_ssl_admin($force = '') {
     590function bb_force_ssl_admin( $force = '' )
     591{
    332592        static $forced;
    333        
     593
    334594        if ( '' != $force ) {
    335595                $old_forced = $forced;
     
    337597                return $old_forced;
    338598        }
    339        
     599
    340600        return $forced;
    341601}
     
    350610function bb_ssl_redirect()
    351611{
    352         do_action('bb_ssl_redirect');
    353        
     612        do_action( 'bb_ssl_redirect' );
     613
    354614        $page = bb_get_location();
    355        
    356         if (BB_IS_ADMIN && !bb_force_ssl_admin()) {
    357                 return;
    358         }
    359        
    360         switch ($page) {
     615
     616        if ( BB_IS_ADMIN && !bb_force_ssl_admin() ) {
     617                return;
     618        }
     619
     620        switch ( $page ) {
    361621                case 'login-page':
    362622                case 'register-page':
    363                         if (!bb_force_ssl_user_forms()) {
     623                        if ( !bb_force_ssl_user_forms() ) {
    364624                                return;
    365625                        }
    366626                        break;
    367                
    368627                case 'profile-page':
    369628                        global $self;
    370                         if ($self == 'profile-edit.php') {
    371                                 if (!bb_force_ssl_user_forms()) {
     629                        if ( $self == 'profile-edit.php' ) {
     630                                if ( !bb_force_ssl_user_forms() ) {
    372631                                        return;
    373632                                }
     
    376635                        }
    377636                        break;
    378                
    379637                default:
    380638                        return;
    381639                        break;
    382640        }
    383        
    384         if (bb_is_ssl()) {
    385                 return;
    386         }
    387        
    388         if ( 0 === strpos($_SERVER['REQUEST_URI'], bb_get_option('uri')) ) {
     641
     642        if ( bb_is_ssl() ) {
     643                return;
     644        }
     645
     646        if ( 0 === strpos( $_SERVER['REQUEST_URI'], bb_get_option( 'uri' ) ) ) {
    389647                $uri = $_SERVER['REQUEST_URI'];
    390648        } else {
    391649                $uri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    392650        }
    393        
    394         $uri = bb_get_option('uri_ssl') . substr($uri, strlen(bb_get_option('uri')));
    395        
    396         bb_safe_redirect($uri);
    397        
     651
     652        $uri = bb_get_option( 'uri_ssl' ) . substr( $uri, strlen( bb_get_option( 'uri' ) ) );
     653
     654        bb_safe_redirect( $uri );
     655
    398656        return;
    399657}
     
    406664 * @return bool True if SSL, false if not used.
    407665 */
    408 function bb_is_ssl() {
    409         return ( 'on' == strtolower(@$_SERVER['HTTPS']) ) ? true : false;
    410 }
    411 
    412 // This is the only function that should add to user / topic
    413 // NOT bbdb::prepared
    414 function bb_append_meta( $object, $type ) {
    415         global $bbdb;
    416         switch ( $type ) :
    417         case 'user' :
    418                 global $wp_users_object;
    419                 return $wp_users_object->append_meta( $object );
    420                 break;
    421         case 'forum' :
    422                 $object_id_column = 'forum_id';
    423                 $object_type = 'bb_forum';
    424                 $slug = 'forum_slug';
    425                 break;
    426         case 'topic' :
    427                 $object_id_column = 'topic_id';
    428                 $object_type = 'bb_topic';
    429                 $slug = 'topic_slug';
    430                 break;
    431         case 'post' :
    432                 $object_id_column = 'post_id';
    433                 $object_type = 'bb_post';
    434                 $slug = 'post_slug';
    435                 break;
    436         endswitch;
    437         if ( is_array($object) && $object ) {
    438                 $trans = array();
    439                 foreach ( array_keys($object) as $i )
    440                         $trans[$object[$i]->$object_id_column] =& $object[$i];
    441                 $ids = join(',', array_map('intval', array_keys($trans)));
    442                 if ( $metas = $bbdb->get_results("SELECT object_id, meta_key, meta_value FROM $bbdb->meta WHERE object_type = '$object_type' AND object_id IN ($ids) /* bb_append_meta */") ) {
    443                         usort( $metas, '_bb_append_meta_sort' );
    444                         foreach ( $metas as $meta ) {
    445                                 $trans[$meta->object_id]->{$meta->meta_key} = maybe_unserialize( $meta->meta_value );
    446                                 if ( strpos($meta->meta_key, $bbdb->prefix) === 0 )
    447                                         $trans[$meta->object_id]->{substr($meta->meta_key, strlen($bbdb->prefix))} = maybe_unserialize( $meta->meta_value );
    448                         }
    449                 }
    450                 foreach ( array_keys($trans) as $i ) {
    451                         wp_cache_add( $i, $trans[$i], $object_type );
    452                         if ($slug)
    453                                 wp_cache_add( $trans[$i]->$slug, $i, 'bb_' . $slug );
    454                 }
    455                 return $object;
    456         } elseif ( $object ) {
    457                 if ( $metas = $bbdb->get_results( $bbdb->prepare( "SELECT meta_key, meta_value FROM $bbdb->meta WHERE object_type = '$object_type' AND object_id = %d /* bb_append_meta */", $object->$object_id_column ) ) ) {
    458                         usort( $metas, '_bb_append_meta_sort' );
    459                         foreach ( $metas as $meta ) {
    460                                 $object->{$meta->meta_key} = maybe_unserialize( $meta->meta_value );
    461                                 if ( strpos($meta->meta_key, $bbdb->prefix) === 0 )
    462                                         $object->{substr($meta->meta_key, strlen($bbdb->prefix))} = $object->{$meta->meta_key};
    463                         }
    464                 }
    465                 if ( $object->$object_id_column ) {
    466                         wp_cache_set( $object->$object_id_column, $object, $object_type );
    467                         if ($slug)
    468                                 wp_cache_add( $object->$slug, $object->$object_id_column, 'bb_' . $slug );
    469                 }
    470                 return $object;
    471         }
    472 }
    473 
    474 /**
    475  * _bb_append_meta_sort() - sorts meta keys by length to ensure $appended_object->{$bbdb->prefix}key overwrites $appended_object->key as desired
    476  *
    477  * @internal
    478  */
    479 function _bb_append_meta_sort( $a, $b ) {
    480         return strlen( $a->meta_key ) - strlen( $b->meta_key );
    481 }
    482 
    483 function bb_get_forummeta( $forum_id, $meta_key ) {
    484         if ( !$forum = bb_get_forum( $forum_id ) )
    485                 return;
    486 
    487         $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    488         if ( !isset($forum->$meta_key) )
    489                 return;
     666function bb_is_ssl()
     667{
     668        return ( 'on' == strtolower( @$_SERVER['HTTPS'] ) ) ? true : false;
     669}
     670
     671
     672
     673/* User meta */
     674
     675function bb_get_usermeta( $user_id, $meta_key )
     676{
     677        if ( !$user = bb_get_user( $user_id ) ) {
     678                return;
     679        }
     680
     681        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     682        if ( !isset( $user->$meta_key ) ) {
     683                return;
     684        }
     685        return $user->$meta_key;
     686}
     687
     688function bb_update_usermeta( $user_id, $meta_key, $meta_value )
     689{
     690        return bb_update_meta( $user_id, $meta_key, $meta_value, 'user' );
     691}
     692
     693function bb_delete_usermeta( $user_id, $meta_key, $meta_value = '' )
     694{
     695        return bb_delete_meta( $user_id, $meta_key, $meta_value, 'user' );
     696}
     697
     698
     699
     700/* Forum meta */
     701
     702function bb_get_forummeta( $forum_id, $meta_key )
     703{
     704        if ( !$forum = bb_get_forum( $forum_id ) ) {
     705                return;
     706        }
     707
     708        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     709        if ( !isset( $forum->$meta_key ) ) {
     710                return;
     711        }
    490712        return $forum->$meta_key;
    491713}
    492714
    493 function bb_update_forummeta( $forum_id, $meta_key, $meta_value ) {
     715function bb_update_forummeta( $forum_id, $meta_key, $meta_value )
     716{
    494717        return bb_update_meta( $forum_id, $meta_key, $meta_value, 'forum' );
    495718}
    496719
    497 function bb_delete_forummeta( $forum_id, $meta_key, $meta_value = '' ) {
     720function bb_delete_forummeta( $forum_id, $meta_key, $meta_value = '' )
     721{
    498722        return bb_delete_meta( $forum_id, $meta_key, $meta_value, 'forum' );
    499723}
    500724
    501 function bb_get_usermeta( $user_id, $meta_key ) {
    502         if ( !$user = bb_get_user( $user_id ) )
    503                 return;
    504 
    505         $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    506         if ( !isset($user->$meta_key) )
    507                 return;
    508         return $user->$meta_key;
    509 }
    510 
    511 function bb_update_usermeta( $user_id, $meta_key, $meta_value ) {
    512         return bb_update_meta( $user_id, $meta_key, $meta_value, 'user' );
    513 }
    514 
    515 function bb_delete_usermeta( $user_id, $meta_key, $meta_value = '' ) {
    516         return bb_delete_meta( $user_id, $meta_key, $meta_value, 'user' );
    517 }
    518 
    519 function bb_get_topicmeta( $topic_id, $meta_key ) {
    520         if ( !$topic = get_topic( $topic_id ) )
    521                 return;
    522 
    523         $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    524         if ( !isset($topic->$meta_key) )
    525                 return;
     725
     726
     727/* Topic meta */
     728
     729function bb_get_topicmeta( $topic_id, $meta_key )
     730{
     731        if ( !$topic = get_topic( $topic_id ) ) {
     732                return;
     733        }
     734
     735        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     736        if ( !isset($topic->$meta_key) ) {
     737                return;
     738        }
    526739        return $topic->$meta_key;
    527740}
    528741
    529 function bb_update_topicmeta( $topic_id, $meta_key, $meta_value ) {
     742function bb_update_topicmeta( $topic_id, $meta_key, $meta_value )
     743{
    530744        return bb_update_meta( $topic_id, $meta_key, $meta_value, 'topic' );
    531745}
    532746
    533 function bb_delete_topicmeta( $topic_id, $meta_key, $meta_value = '' ) {
     747function bb_delete_topicmeta( $topic_id, $meta_key, $meta_value = '' )
     748{
    534749        return bb_delete_meta( $topic_id, $meta_key, $meta_value, 'topic' );
    535750}
    536751
    537 function bb_get_postmeta( $post_id, $meta_key ) {
    538         if ( !$post = bb_get_post( $post_id ) )
    539                 return;
    540 
    541         $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    542         if ( !isset($post->$meta_key) )
    543                 return;
     752
     753
     754/* Post meta */
     755
     756function bb_get_postmeta( $post_id, $meta_key )
     757{
     758        if ( !$post = bb_get_post( $post_id ) ) {
     759                return;
     760        }
     761
     762        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
     763        if ( !isset( $post->$meta_key ) ) {
     764                return;
     765        }
    544766        return $post->$meta_key;
    545767}
    546768
    547 function bb_update_postmeta( $post_id, $meta_key, $meta_value ) {
     769function bb_update_postmeta( $post_id, $meta_key, $meta_value )
     770{
    548771        return bb_update_meta( $post_id, $meta_key, $meta_value, 'post' );
    549772}
    550773
    551 function bb_delete_postmeta( $post_id, $meta_key, $meta_value = '' ) {
     774function bb_delete_postmeta( $post_id, $meta_key, $meta_value = '' )
     775{
    552776        return bb_delete_meta( $post_id, $meta_key, $meta_value, 'post' );
    553777}
    554 
    555 // Internal use only.  Use API.
    556 function bb_update_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) {
    557         global $bbdb;
    558         if ( !is_numeric( $object_id ) || empty($object_id) && !$global )
    559                 return false;
    560         $cache_object_id = $object_id = (int) $object_id;
    561         switch ( $type ) {
    562                 case 'option':
    563                         $object_type = 'bb_option';
    564                         break;
    565                 case 'user' :
    566                         global $wp_users_object;
    567                         $id = $object_id;
    568                         $return = $wp_users_object->update_meta( compact( 'id', 'meta_key', 'meta_value' ) );
    569                         if ( is_wp_error($return) )
    570                                 return false;
    571                         return $return;
    572                         break;
    573                 case 'forum' :
    574                         $object_type = 'bb_forum';
    575                         break;
    576                 case 'topic' :
    577                         $object_type = 'bb_topic';
    578                         break;
    579                 case 'post' :
    580                         $object_type = 'bb_post';
    581                         break;
    582                 default :
    583                         $object_type = $type;
    584                         break;
    585         }
    586 
    587         $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    588 
    589         $meta_tuple = compact('object_type', 'object_id', 'meta_key', 'meta_value', 'type');
    590         $meta_tuple = apply_filters('bb_update_meta', $meta_tuple);
    591         extract($meta_tuple, EXTR_OVERWRITE);
    592 
    593         $meta_value = $_meta_value = maybe_serialize( $meta_value );
    594         $meta_value = maybe_unserialize( $meta_value );
    595 
    596         $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 ) );
    597         if ( !$cur ) {
    598                 $bbdb->insert( $bbdb->meta, array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key, 'meta_value' => $_meta_value ) );
    599         } elseif ( $cur->meta_value != $meta_value ) {
    600                 $bbdb->update( $bbdb->meta, array( 'meta_value' => $_meta_value), array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key ) );
    601         }
    602 
    603         if ($object_type == 'bb_option') {
    604                 $cache_object_id = $meta_key;
    605         }
    606         wp_cache_delete( $cache_object_id, $object_type );
    607         if ( !$cur )
    608                 return true;
    609 }
    610 
    611 // Internal use only.  Use API.
    612 function bb_delete_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) {
    613         global $bbdb;
    614         if ( !is_numeric( $object_id ) || empty($object_id) && !$global )
    615                 return false;
    616         $cache_object_id = $object_id = (int) $object_id;
    617         switch ( $type ) {
    618                 case 'option':
    619                         $object_type = 'bb_option';
    620                         break;
    621                 case 'user' :
    622                         global $wp_users_object;
    623                         $id = $object_id;
    624                         return $wp_users_object->update_meta( compact( 'id', 'meta_key', 'meta_value' ) );
    625                         break;
    626                 case 'forum' :
    627                         $object_type = 'bb_forum';
    628                         break;
    629                 case 'topic' :
    630                         $object_type = 'bb_topic';
    631                         break;
    632                 case 'post' :
    633                         $object_type = 'bb_post';
    634                         break;
    635                 default :
    636                         $object_type = $type;
    637                         break;
    638         }
    639 
    640         $meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key);
    641 
    642         $meta_tuple = compact('object_type', 'object_id', 'meta_key', 'meta_value', 'type');
    643         $meta_tuple = apply_filters('bb_delete_meta', $meta_tuple);
    644         extract($meta_tuple, EXTR_OVERWRITE);
    645 
    646         $meta_value = maybe_serialize( $meta_value );
    647 
    648         $meta_sql = empty($meta_value) ?
    649                 $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 ) :
    650                 $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 );
    651 
    652         if ( !$meta_id = $bbdb->get_var( $meta_sql ) )
    653                 return false;
    654 
    655         $bbdb->query( $bbdb->prepare( "DELETE FROM $bbdb->meta WHERE meta_id = %d", $meta_id ) );
    656 
    657         if ($object_type == 'bb_option') {
    658                 $cache_object_id = $meta_key;
    659         }
    660         wp_cache_delete( $cache_object_id, $object_type );
    661         return true;
    662 }
Note: See TracChangeset for help on using the changeset viewer.