Make WordPress Core

Ticket #5287: set_prefix.diff

File set_prefix.diff, 3.3 KB (added by ryan, 18 years ago)
  • wp-includes/wp-db.php

     
    2020        var $last_query;
    2121        var $col_info;
    2222        var $queries;
     23        var $prefix = '';
    2324
    2425        // Our tables
    2526        var $posts;
     
    2930        var $comments;
    3031        var $links;
    3132        var $options;
    32         var $optiontypes;
    33         var $optionvalues;
    34         var $optiongroups;
    35         var $optiongroup_options;
    3633        var $postmeta;
    3734        var $usermeta;
    3835        var $terms;
    3936        var $term_taxonomy;
    4037        var $term_relationships;
    41 
     38        var $tables = array('users', 'usermeta', 'posts', 'categories', 'post2cat', 'comments', 'links', 'link2cat', 'options',
     39                        'postmeta', 'terms', 'term_taxonomy', 'term_relationships');
    4240        var $charset;
    4341        var $collate;
    4442
     
    8684                return true;
    8785        }
    8886
     87        function set_prefix($prefix) {
     88
     89                if ( preg_match('|[^a-z0-9_]|i', $prefix) )
     90                        return new WP_Error('invalid_db_prefix', 'Invalid database prefix'); // No gettext here
     91
     92                $old_prefix = $this->prefix;
     93                $this->prefix = $prefix;
     94
     95                foreach ( $this->tables as $table )
     96                        $this->$table = $this->prefix . $table;
     97
     98                if ( defined('CUSTOM_USER_TABLE') )
     99                        $this->users = CUSTOM_USER_TABLE;
     100
     101                if ( defined('CUSTOM_USER_META_TABLE') )
     102                        $this->usermeta = CUSTOM_USER_META_TABLE;
     103
     104                return $old_prefix;
     105        }
     106
    89107        /**
    90108         * Selects a database using the current class's $this->dbh
    91109         * @param string $db name
  • wp-settings.php

     
    118118
    119119require (ABSPATH . WPINC . '/compat.php');
    120120require (ABSPATH . WPINC . '/functions.php');
     121require (ABSPATH . WPINC . '/classes.php');
    121122
    122123require_wp_db();
    123 // $table_prefix is deprecated as of 2.1
    124 $wpdb->prefix = $table_prefix;
     124$prefix = $wpdb->set_prefix($table_prefix);
    125125
    126 if ( preg_match('|[^a-z0-9_]|i', $wpdb->prefix) && !file_exists(ABSPATH . 'wp-content/db.php') )
     126if ( is_wp_error($prefix) )
    127127        wp_die("<strong>ERROR</strong>: <code>$table_prefix</code> in <code>wp-config.php</code> can only contain numbers, letters, and underscores.");
    128128
    129 // Table names
    130 $wpdb->posts          = $wpdb->prefix . 'posts';
    131 $wpdb->users          = $wpdb->prefix . 'users';
    132 $wpdb->categories     = $wpdb->prefix . 'categories';
    133 $wpdb->post2cat       = $wpdb->prefix . 'post2cat';
    134 $wpdb->comments       = $wpdb->prefix . 'comments';
    135 $wpdb->link2cat       = $wpdb->prefix . 'link2cat';
    136 $wpdb->links          = $wpdb->prefix . 'links';
    137 $wpdb->options        = $wpdb->prefix . 'options';
    138 $wpdb->postmeta       = $wpdb->prefix . 'postmeta';
    139 $wpdb->usermeta       = $wpdb->prefix . 'usermeta';
    140 $wpdb->terms          = $wpdb->prefix . 'terms';
    141 $wpdb->term_taxonomy  = $wpdb->prefix . 'term_taxonomy';
    142 $wpdb->term_relationships = $wpdb->prefix . 'term_relationships';
    143 
    144 if ( defined('CUSTOM_USER_TABLE') )
    145         $wpdb->users = CUSTOM_USER_TABLE;
    146 if ( defined('CUSTOM_USER_META_TABLE') )
    147         $wpdb->usermeta = CUSTOM_USER_META_TABLE;
    148 
    149129if ( file_exists(ABSPATH . 'wp-content/object-cache.php') )
    150130        require_once (ABSPATH . 'wp-content/object-cache.php');
    151131else
     
    153133
    154134wp_cache_init();
    155135
    156 require (ABSPATH . WPINC . '/classes.php');
    157136require (ABSPATH . WPINC . '/plugin.php');
    158137require (ABSPATH . WPINC . '/default-filters.php');
    159138include_once(ABSPATH . WPINC . '/streams.php');