Skip to content

Commit

Permalink
Don't rely on plugin_basename for db version option_name
Browse files Browse the repository at this point in the history
- Changed option_name to class KEY const = wp_stream_db
- Added get_db_version method to query for any nonstandard option_name if version is 1.3.0 or earlier
- If old option_name found update to new option name
- Fixes #388
  • Loading branch information
Chris Olbekson committed Apr 6, 2014
1 parent 4dcb552 commit c44fe90
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions includes/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

class WP_Stream_Install {

/**
* Option key to store database version
*
* @var string
*/
const KEY = 'wp_stream_db';

/**
* Holds the database table prefix
*
Expand Down Expand Up @@ -72,14 +79,14 @@ function __construct() {
global $wpdb;

self::$current = WP_Stream::VERSION;
self::$db_version = get_option( plugin_basename( WP_STREAM_DIR ) . '_db' );
self::$db_version = self::get_db_version();
self::$stream_url = self_admin_url( WP_Stream_Admin::ADMIN_PARENT_PAGE . '&page=' . WP_Stream_Admin::SETTINGS_PAGE_SLUG );

/**
* Allows devs to alter the tables prefix, default to base_prefix
* Allows developers to alter the tables prefix, default to base_prefix
*
* @var string $prefix database prefix
* @var string $table_prefix udpated database prefix
* @var string $table_prefix updated database prefix
*/
$prefix = $wpdb->prefix;

Expand All @@ -96,12 +103,30 @@ function __construct() {
*/
private static function check() {
if ( empty( self::$db_version ) ) {

$current = self::install( self::$current );
} elseif ( self::$db_version !== self::$current ) {
add_action( 'admin_notices', array( __CLASS__, 'update_notice_hook' ) );
}
}

public static function get_db_version() {
global $wpdb;

$version = get_option( self::KEY );

if ( ! $version && version_compare( self::$current, '1.3.1', '<=' ) ) {
$old_key = $wpdb->get_col( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE '%wp-stream%_db'" );
if ( ! empty( $old_key ) && is_array( $old_key ) ) {
$version = get_option( $old_key[0] );
update_option( self::KEY, $version );
delete_option( $old_key[0] );
}
}

return $version;
}

/**
* Action hook callback function
* Adds the user controlled database upgrade routine to the plugins updated page
Expand Down Expand Up @@ -133,7 +158,7 @@ public static function prompt_update_status() {
$success_db = self::update( self::$db_version, self::$current );

if ( $success_db && self::$current === $success_db ) {
$success_op = update_option( plugin_basename( WP_STREAM_DIR ) . '_db', $success_db );
$success_op = update_option( self::KEY. '_db', $success_db );
}

if ( empty( $success_db ) || empty( $success_op ) ) {
Expand Down Expand Up @@ -221,7 +246,10 @@ public static function update( $db_version, $current ) {

/**
* Initial database install routine
*
* @uses dbDelta()
* @param string $current Current version of plugin installed
* @return string Current version of plugin installed
*/
public static function install( $current ) {
global $wpdb;
Expand Down

0 comments on commit c44fe90

Please sign in to comment.