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