| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Version management for database migrations. |
||
| 4 | * |
||
| 5 | * Database changes require special care: |
||
| 6 | * - the model has to be adjusted for users installing the plugin |
||
| 7 | * - the current setup has to be migrated for current users |
||
| 8 | * |
||
| 9 | * These migrations are a way to handle current users. They do *not* |
||
| 10 | * run on plugin activation. |
||
| 11 | * |
||
| 12 | * Pattern: |
||
| 13 | * |
||
| 14 | * - increment \PodloveSubscribeButton\DATABASE_VERSION constant by 1, e.g. |
||
| 15 | * ```php |
||
| 16 | * define( __NAMESPACE__ . '\DATABASE_VERSION', 2 ); |
||
| 17 | * ``` |
||
| 18 | * |
||
| 19 | * - add a case in `\PodloveSubscribeButton\run_migrations_for_version`, e.g. |
||
| 20 | * ```php |
||
| 21 | * function run_migrations_for_version( $version ) { |
||
| 22 | * global $wpdb; |
||
| 23 | * switch ( $version ) { |
||
| 24 | * case 2: |
||
| 25 | * $wbdb-> // run sql or whatever |
||
| 26 | * break; |
||
| 27 | * } |
||
| 28 | * } |
||
| 29 | * ``` |
||
| 30 | * |
||
| 31 | * Feel free to move the migration code into a separate function if it's |
||
| 32 | * rather complex. |
||
| 33 | * |
||
| 34 | * - adjust the main model / setup process so new users installing the plugin |
||
| 35 | * will have these changes too |
||
| 36 | * |
||
| 37 | * - Test the migrations! :) |
||
| 38 | */ |
||
|
0 ignored issues
–
show
Coding Style
Documentation
introduced
by
Loading history...
|
|||
| 39 | |||
| 40 | namespace PodloveSubscribeButton; |
||
| 41 | use \PodloveSubscribeButton\Model; |
||
| 42 | |||
| 43 | define( __NAMESPACE__ . '\DATABASE_VERSION', 2 ); |
||
| 44 | |||
| 45 | add_action( 'admin_init', '\PodloveSubscribeButton\maybe_run_database_migrations' ); |
||
| 46 | add_action( 'admin_init', '\PodloveSubscribeButton\run_database_migrations', 5 ); |
||
| 47 | |||
| 48 | function maybe_run_database_migrations() { |
||
| 49 | $database_version = get_option( 'podlove_subscribe_button_plugin_database_version' ); |
||
| 50 | |||
| 51 | if ( $database_version === false ) { |
||
| 52 | // plugin has just been installed or Plugin Version < 1.3 |
||
| 53 | update_option( 'podlove_subscribe_button_plugin_database_version', DATABASE_VERSION ); |
||
| 54 | } |
||
| 55 | |||
| 56 | } |
||
| 57 | |||
| 58 | function run_database_migrations() { |
||
| 59 | if ( get_option( 'podlove_subscribe_button_plugin_database_version' ) >= DATABASE_VERSION ) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | if ( is_multisite() ) { |
||
| 64 | set_time_limit( 0 ); // may take a while, depending on network size |
||
| 65 | \PodloveSubscribeButton\for_every_podcast_blog( function() { migrate_for_current_blog(); }); |
||
| 66 | } else { |
||
| 67 | migrate_for_current_blog(); |
||
| 68 | } |
||
| 69 | |||
| 70 | if ( isset( $_REQUEST[ '_wp_http_referer' ] ) && $_REQUEST[ '_wp_http_referer' ] ) { |
||
| 71 | wp_redirect( $_REQUEST[ '_wp_http_referer' ] ); |
||
| 72 | exit; |
||
| 73 | } |
||
| 74 | } |
||
| 75 | |||
| 76 | function migrate_for_current_blog() { |
||
| 77 | $database_version = get_option( 'podlove_subscribe_button_plugin_database_version' ); |
||
| 78 | |||
| 79 | for ( $i = $database_version + 1; $i <= DATABASE_VERSION; $i++ ) { |
||
| 80 | \PodloveSubscribeButton\run_migrations_for_version( $i ); |
||
| 81 | update_option( 'podlove_subscribe_button_plugin_database_version', $i ); |
||
| 82 | } |
||
| 83 | |||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Find and run migration for given version number. |
||
| 88 | * |
||
| 89 | * @todo move migrations into separate files |
||
| 90 | * |
||
| 91 | * @param int $version |
||
| 92 | */ |
||
| 93 | function run_migrations_for_version( $version ) { |
||
| 94 | global $wpdb; |
||
| 95 | |||
| 96 | switch ( $version ) {} |
||
| 97 | |||
| 98 | } |
||
| 99 |