| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file manages upgrades to the database between plugin versions |
| 5 |
*/ |
| 6 |
|
| 7 |
/* Bail on direct access */ |
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
return; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Preform upgrade tasks such as deleting and updating options |
| 14 |
* @since 2.0 |
| 15 |
*/ |
| 16 |
function code_snippets_upgrader() { |
| 17 |
global $wpdb; |
| 18 |
$db = code_snippets()->db; |
| 19 |
|
| 20 |
/* Get the current plugin version from the database */ |
| 21 |
$prev_version = get_option( 'code_snippets_version' ); |
| 22 |
|
| 23 |
/* Check if we have upgraded from an older version */ |
| 24 |
if ( version_compare( $prev_version, CODE_SNIPPETS_VERSION, '<' ) ) { |
| 25 |
|
| 26 |
/* Upgrade the database tables */ |
| 27 |
$db->create_tables( true ); |
| 28 |
|
| 29 |
/* Update the plugin version stored in the database */ |
| 30 |
update_option( 'code_snippets_version', CODE_SNIPPETS_VERSION ); |
| 31 |
|
| 32 |
/* Update the scope column of the database */ |
| 33 |
if ( version_compare( $prev_version, '2.10.0', '<' ) ) { |
| 34 |
$wpdb->query( "UPDATE {$wpdb->snippets} SET scope = 'global' WHERE scope = 0" ); |
| 35 |
$wpdb->query( "UPDATE {$wpdb->snippets} SET scope = 'admin' WHERE scope = 1" ); |
| 36 |
$wpdb->query( "UPDATE {$wpdb->snippets} SET scope = 'front-end' WHERE scope = 2" ); |
| 37 |
} |
| 38 |
|
| 39 |
/* Custom capabilities were removed after version 2.9.5 */ |
| 40 |
if ( version_compare( $prev_version, '2.9.5', '<=' ) ) { |
| 41 |
$role = get_role( apply_filters( 'code_snippets_role', 'administrator' ) ); |
| 42 |
$role->remove_cap( apply_filters( 'code_snippets_cap', 'manage_snippets' ) ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
if ( false === $prev_version ) { |
| 47 |
$db->create_sample_content(); |
| 48 |
} |
| 49 |
|
| 50 |
/* Run multisite-only upgrades */ |
| 51 |
|
| 52 |
if ( is_multisite() && is_main_site() ) { |
| 53 |
|
| 54 |
/* Get the current plugin version from the database */ |
| 55 |
$prev_ms_version = get_site_option( 'code_snippets_version' ); |
| 56 |
|
| 57 |
/* Check if we have upgraded from an older version */ |
| 58 |
if ( version_compare( $prev_ms_version, CODE_SNIPPETS_VERSION, '<' ) ) { |
| 59 |
|
| 60 |
/* Update the plugin version stored in the database */ |
| 61 |
update_site_option( 'code_snippets_version', CODE_SNIPPETS_VERSION ); |
| 62 |
|
| 63 |
/* Update the scope column of the database */ |
| 64 |
if ( version_compare( $prev_version, '2.10.0', '<' ) ) { |
| 65 |
$wpdb->query( "UPDATE {$wpdb->ms_snippets} SET scope = 'global' WHERE scope = 0" ); |
| 66 |
$wpdb->query( "UPDATE {$wpdb->ms_snippets} SET scope = 'admin' WHERE scope = 1" ); |
| 67 |
$wpdb->query( "UPDATE {$wpdb->ms_snippets} SET scope = 'front-end' WHERE scope = 2" ); |
| 68 |
} |
| 69 |
|
| 70 |
/* Custom capabilities were removed after version 2.9.5 */ |
| 71 |
if ( version_compare( $prev_ms_version, '2.9.5', '<=' ) ) { |
| 72 |
$network_cap = apply_filters( 'code_snippets_network_cap', 'manage_network_snippets' ); |
| 73 |
|
| 74 |
foreach ( get_super_admins() as $admin ) { |
| 75 |
$user = new WP_User( 0, $admin ); |
| 76 |
$user->remove_cap( $network_cap ); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
add_action( 'plugins_loaded', 'code_snippets_upgrader', 0 ); |
| 84 |
|