| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Manages upgrade tasks such as deleting and updating options |
| 5 |
*/ |
| 6 |
class Code_Snippets_Upgrade { |
| 7 |
|
| 8 |
/** |
| 9 |
* Instance of database class |
| 10 |
* @var Code_Snippets_DB |
| 11 |
*/ |
| 12 |
private $db; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class constructor |
| 16 |
* |
| 17 |
* @param Code_Snippets_DB $db Instance of database class |
| 18 |
*/ |
| 19 |
public function __construct( Code_Snippets_DB $db ) { |
| 20 |
$this->db = $db; |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Run the upgrade functions |
| 25 |
*/ |
| 26 |
public function run() { |
| 27 |
|
| 28 |
/* Always run multisite upgrades, even if not on the main site, as subsites depend on the network snippet table */ |
| 29 |
if ( is_multisite() ) { |
| 30 |
$this->do_multisite_upgrades(); |
| 31 |
} |
| 32 |
|
| 33 |
$this->do_site_upgrades(); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Perform upgrades for the current site |
| 38 |
*/ |
| 39 |
private function do_site_upgrades() { |
| 40 |
$table_name = $this->db->table; |
| 41 |
$prev_version = get_option( 'code_snippets_version' ); |
| 42 |
|
| 43 |
/* Do nothing if the plugin has not been updated or installed */ |
| 44 |
if ( ! version_compare( $prev_version, CODE_SNIPPETS_VERSION, '<' ) ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$this->db->create_table( $table_name ); |
| 49 |
|
| 50 |
if ( false !== $prev_version ) { |
| 51 |
$this->db->create_missing_columns( $table_name ); |
| 52 |
} |
| 53 |
|
| 54 |
/* Update the plugin version stored in the database */ |
| 55 |
update_option( 'code_snippets_version', CODE_SNIPPETS_VERSION ); |
| 56 |
|
| 57 |
/* Update the scope column of the database */ |
| 58 |
if ( version_compare( $prev_version, '2.10.0', '<' ) ) { |
| 59 |
$this->migrate_scope_data( $table_name ); |
| 60 |
} |
| 61 |
|
| 62 |
/* Custom capabilities were removed after version 2.9.5 */ |
| 63 |
if ( version_compare( $prev_version, '2.9.5', '<=' ) ) { |
| 64 |
$role = get_role( apply_filters( 'code_snippets_role', 'administrator' ) ); |
| 65 |
$role->remove_cap( apply_filters( 'code_snippets_cap', 'manage_snippets' ) ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( false === $prev_version ) { |
| 69 |
$this->create_sample_content(); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Perform multisite-only upgrades |
| 75 |
*/ |
| 76 |
private function do_multisite_upgrades() { |
| 77 |
$table_name = $this->db->ms_table; |
| 78 |
$prev_version = get_site_option( 'code_snippets_version' ); |
| 79 |
|
| 80 |
/* Do nothing if the plugin has not been updated or installed */ |
| 81 |
if ( ! version_compare( $prev_version, CODE_SNIPPETS_VERSION, '<' ) ) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
/* Always attempt to create or upgrade the database tables */ |
| 86 |
$this->db->create_table( $table_name ); |
| 87 |
|
| 88 |
/* If the plugin has been upgraded, also attempt to create the new columns */ |
| 89 |
if ( false !== $prev_version ) { |
| 90 |
$this->db->create_missing_columns( $table_name ); |
| 91 |
} |
| 92 |
|
| 93 |
/* Update the plugin version stored in the database */ |
| 94 |
update_site_option( 'code_snippets_version', CODE_SNIPPETS_VERSION ); |
| 95 |
|
| 96 |
/* Update the scope column of the database */ |
| 97 |
if ( version_compare( $prev_version, '2.10.0', '<' ) ) { |
| 98 |
$this->migrate_scope_data( $table_name ); |
| 99 |
} |
| 100 |
|
| 101 |
/* Custom capabilities were removed after version 2.9.5 */ |
| 102 |
if ( version_compare( $prev_version, '2.9.5', '<=' ) ) { |
| 103 |
$network_cap = apply_filters( 'code_snippets_network_cap', 'manage_network_snippets' ); |
| 104 |
|
| 105 |
foreach ( get_super_admins() as $admin ) { |
| 106 |
$user = new WP_User( 0, $admin ); |
| 107 |
$user->remove_cap( $network_cap ); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Migrate data from the old integer method of storing scopes to the new string method |
| 114 |
* |
| 115 |
* @param string $table_name |
| 116 |
*/ |
| 117 |
private function migrate_scope_data( $table_name ) { |
| 118 |
global $wpdb; |
| 119 |
|
| 120 |
$scopes = array( |
| 121 |
0 => 'global', |
| 122 |
1 => 'admin', |
| 123 |
2 => 'front-end', |
| 124 |
); |
| 125 |
|
| 126 |
foreach ( $scopes as $scope_number => $scope_name ) { |
| 127 |
$wpdb->query( sprintf( |
| 128 |
"UPDATE %s SET scope = '%s' WHERE scope = %d", |
| 129 |
$table_name, $scope_name, $scope_number |
| 130 |
) ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Add sample snippet content to the database |
| 136 |
*/ |
| 137 |
public function create_sample_content() { |
| 138 |
|
| 139 |
if ( ! apply_filters( 'code_snippets/create_sample_content', true ) ) { |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
$snippets = array( |
| 144 |
|
| 145 |
array( |
| 146 |
'name' => __( 'Example HTML shortcode', 'code-snippets' ), |
| 147 |
'code' => sprintf( |
| 148 |
"\nadd_shortcode( 'shortcode_name', function () { ?>\n\n\t<p>%s</p>\n\n<?php } );", |
| 149 |
strip_tags( __( 'write your HTML shortcode content here', 'code-snippets' ) ) |
| 150 |
), |
| 151 |
'desc' => __( 'This is an example snippet for demonstrating how to add an HTML shortcode.', 'code-snippets' ), |
| 152 |
'tags' => array( 'shortcode' ), |
| 153 |
), |
| 154 |
|
| 155 |
array( |
| 156 |
'name' => __( 'Example CSS snippet', 'code-snippets' ), |
| 157 |
'code' => sprintf( |
| 158 |
"\nadd_action( 'wp_head', function () { ?>\n\t<style>\n\n\t\t/* %s */\n\n\t</style>\n<?php } );\n", |
| 159 |
strip_tags( __( 'write your CSS code here', 'code-snippets' ) ) |
| 160 |
), |
| 161 |
'desc' => __( 'This is an example snippet for demonstrating how to add custom CSS code to your website.', 'code-snippets' ), |
| 162 |
'tags' => array( 'css' ), |
| 163 |
'scope' => 'front-end', |
| 164 |
), |
| 165 |
|
| 166 |
array( |
| 167 |
'name' => __( 'Example JavaScript snippet', 'code-snippets' ), |
| 168 |
'code' => sprintf( |
| 169 |
"\nadd_action( 'wp_head', function () { ?>\n\t<script>\n\n\t\t/* %s */\n\n\t</script>\n<?php } );\n", |
| 170 |
strip_tags( __( 'write your JavaScript code here', 'code-snippets' ) ) |
| 171 |
), |
| 172 |
'desc' => __( 'This is an example snippet for demonstrating how to add custom JavaScript code to your website.', 'code-snippets' ), |
| 173 |
'tags' => array( 'javascript' ), |
| 174 |
'scope' => 'front-end', |
| 175 |
), |
| 176 |
); |
| 177 |
|
| 178 |
foreach ( $snippets as $snippet ) { |
| 179 |
$snippet = new Code_Snippet( $snippet ); |
| 180 |
$snippet->desc .= ' ' . __( 'You can remove it, or edit it to add your own content.', 'code-snippets' ); |
| 181 |
save_snippet( $snippet ); |
| 182 |
} |
| 183 |
} |
| 184 |
} |
| 185 |
|