| 1 |
<?php |
| 2 |
/** |
| 3 |
* Fired during plugin activation. |
| 4 |
* |
| 5 |
* This class defines all code necessary to run during the plugin's activation. |
| 6 |
* |
| 7 |
* @since 1.1.0 |
| 8 |
* @package Plugin_Notes_Plus |
| 9 |
* @subpackage Plugin_Notes_Plus/includes |
| 10 |
* @author Jamie Bergen <jamie.bergen@gmail.com> |
| 11 |
*/ |
| 12 |
class Plugin_Notes_Plus_Activator { |
| 13 |
|
| 14 |
/** |
| 15 |
* Create a custom database table to hold plugin notes. |
| 16 |
* |
| 17 |
* Migrate existing notes from options table |
| 18 |
* |
| 19 |
* @since 1.1.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
public static function migrate_old_notes( $old_id, $new_id ) { |
| 23 |
|
| 24 |
$plugin_note_obj = new Plugin_Notes_Plus_The_Note( $old_id ); |
| 25 |
$plugin_notes = $plugin_note_obj->retrieve_notes_from_options(); |
| 26 |
|
| 27 |
if ( empty( $plugin_notes ) ) { |
| 28 |
return; |
| 29 |
} |
| 30 |
|
| 31 |
ksort( $plugin_notes ); |
| 32 |
|
| 33 |
foreach ( $plugin_notes as $note_index => $note ) { |
| 34 |
$note_text = $note[ 'note' ]; |
| 35 |
$icon_class = $note[ 'icon' ]; |
| 36 |
$username = $note[ 'user' ]; |
| 37 |
$note_time = $note[ 'time' ]; |
| 38 |
|
| 39 |
$plugin_note_obj->migrate_plugin_note( $new_id, $note_text, $icon_class, $username, $note_time ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public static function activate() { |
| 44 |
|
| 45 |
global $wpdb; |
| 46 |
global $plugin_notes_plus_db_version; |
| 47 |
|
| 48 |
$table_name = $wpdb->prefix . Plugin_Notes_Plus::get_table_name(); |
| 49 |
$charset_collate = $wpdb->get_charset_collate(); |
| 50 |
|
| 51 |
$sql = "CREATE TABLE $table_name ( |
| 52 |
id mediumint(9) NOT NULL AUTO_INCREMENT, |
| 53 |
plugin_id varchar(1024) NOT NULL, |
| 54 |
note_content longtext NOT NULL, |
| 55 |
note_icon varchar(255) NOT NULL, |
| 56 |
user_name varchar(255) NOT NULL, |
| 57 |
time bigint(20) NOT NULL, |
| 58 |
PRIMARY KEY (id) |
| 59 |
) $charset_collate;"; |
| 60 |
|
| 61 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 62 |
dbDelta( $sql ); |
| 63 |
|
| 64 |
// Migrate notes from _options to new table if upgrading from 1.0.0 |
| 65 |
if ( !get_option( 'plugin_notes_plus_db_version' ) ) { |
| 66 |
|
| 67 |
$plugins_array = get_plugins(); |
| 68 |
|
| 69 |
$plugin_keys = array_keys($plugins_array); |
| 70 |
|
| 71 |
foreach ( $plugin_keys as $key ) { |
| 72 |
|
| 73 |
// Look for notes under Linux version of plugin id |
| 74 |
$key_linux = str_replace( '\\', '/', $key ); |
| 75 |
$plugin_unique_id_linux = 'plugin_notes_plus_' . sanitize_title( $key_linux ); |
| 76 |
|
| 77 |
$new_id = wp_normalize_path( $key ); |
| 78 |
|
| 79 |
self::migrate_old_notes( $plugin_unique_id_linux, $new_id ); |
| 80 |
|
| 81 |
// Look for notes under Windows version of plugin id |
| 82 |
$key_windows = str_replace( '/', '\\', $key ); |
| 83 |
$plugin_unique_id_windows = 'plugin_notes_plus_' . sanitize_title( $key_windows ); |
| 84 |
|
| 85 |
if ( $plugin_unique_id_windows != $plugin_unique_id_linux ) { |
| 86 |
self::migrate_old_notes( $plugin_unique_id_windows, $new_id ); |
| 87 |
} |
| 88 |
} |
| 89 |
add_option( 'plugin_notes_plus_db_version', 1.0 ); |
| 90 |
} |
| 91 |
|
| 92 |
// Clean up old options entries after migration is complete |
| 93 |
if ( get_option( 'plugin_notes_plus_db_version' ) == 1.0 ) { |
| 94 |
$all_options = wp_load_alloptions(); |
| 95 |
$pnp_options = array(); |
| 96 |
|
| 97 |
foreach ( $all_options as $name => $value ) { |
| 98 |
if ( stristr( $name, 'plugin_notes_plus_' ) && $name !== 'plugin_notes_plus_db_version' ) { |
| 99 |
$pnp_options[] = $name; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// Remove notes from options table |
| 104 |
foreach ( $pnp_options as $pnp_option ) { |
| 105 |
delete_option( $pnp_option ); |
| 106 |
} |
| 107 |
|
| 108 |
update_option( 'plugin_notes_plus_db_version', $plugin_notes_plus_db_version ); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|