PluginProbe
Code Snippets / 3.2.1
Code Snippets v3.2.1
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / class-upgrade.php

class-upgrade.php in Code Snippets 3.2.1, at php/class-upgrade.php

215 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets;
4
5 use WP_User;
6
7 /**
8 * Manages upgrade tasks such as deleting and updating options
9 */
10 class Upgrade {
11
12 /**
13 * Instance of database class
14 *
15 * @var DB
16 */
17 private $db;
18
19 /**
20 * The current plugin version number
21 *
22 * @var string
23 */
24 private $current_version;
25
26 /**
27 * Class constructor
28 *
29 * @param string $version Current plugin version.
30 * @param DB $db Instance of database class.
31 */
32 public function __construct( $version, DB $db ) {
33 $this->db = $db;
34 $this->current_version = $version;
35 }
36
37 /**
38 * Run the upgrade functions
39 */
40 public function run() {
41
42 /* Always run multisite upgrades, even if not on the main site, as subsites depend on the network snippet table */
43 if ( is_multisite() ) {
44 $this->do_multisite_upgrades();
45 }
46
47 $this->do_site_upgrades();
48 }
49
50 /**
51 * Perform upgrades for the current site
52 */
53 private function do_site_upgrades() {
54 $table_name = $this->db->table;
55 $prev_version = get_option( 'code_snippets_version' );
56
57 /* Do nothing if the plugin has not just been updated or installed */
58 if ( ! version_compare( $prev_version, $this->current_version, '<' ) ) {
59 return;
60 }
61
62 /* Update the plugin version stored in the database */
63 $updated = update_option( 'code_snippets_version', $this->current_version );
64
65 if ( ! $updated ) {
66 return; // Bail if the data was not successfully saved to prevent this process from repeating.
67 }
68
69 $sample_snippets = $this->get_sample_content();
70 $this->db->create_table( $table_name );
71
72 /* Remove outdated user meta */
73 if ( version_compare( $prev_version, '2.14.1', '<' ) ) {
74 global $wpdb;
75
76 $prefix = $wpdb->get_blog_prefix();
77 $menu_slug = code_snippets()->get_menu_slug();
78 $option_name = "{$prefix}managetoplevel_page_{$menu_slug}columnshidden";
79
80 // Loop through each user ID and remove all matching user meta.
81 foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
82 delete_metadata( 'user', $user_id, $option_name, '', true );
83 }
84 }
85
86 /* Update the scope column of the database */
87 if ( version_compare( $prev_version, '2.10.0', '<' ) ) {
88 $this->migrate_scope_data( $table_name );
89 }
90
91 /* Custom capabilities were removed after version 2.9.5 */
92 if ( version_compare( $prev_version, '2.9.5', '<=' ) ) {
93 $role = get_role( apply_filters( 'code_snippets_role', 'administrator' ) );
94 $role->remove_cap( apply_filters( 'code_snippets_cap', 'manage_snippets' ) );
95 }
96
97 if ( false === $prev_version ) {
98 if ( apply_filters( 'code_snippets/create_sample_content', true ) ) {
99 foreach ( $sample_snippets as $sample_snippet ) {
100 save_snippet( $sample_snippet );
101 }
102 }
103 }
104
105 clean_snippets_cache( $table_name );
106 }
107
108 /**
109 * Perform multisite-only upgrades
110 */
111 private function do_multisite_upgrades() {
112 $table_name = $this->db->ms_table;
113 $prev_version = get_site_option( 'code_snippets_version' );
114
115 /* Do nothing if the plugin has not been updated or installed */
116 if ( ! version_compare( $prev_version, $this->current_version, '<' ) ) {
117 return;
118 }
119
120 /* Always attempt to create or upgrade the database tables */
121 $this->db->create_table( $table_name );
122
123 /* Update the plugin version stored in the database */
124 update_site_option( 'code_snippets_version', $this->current_version );
125
126 /* Update the scope column of the database */
127 if ( version_compare( $prev_version, '2.10.0', '<' ) ) {
128 $this->migrate_scope_data( $table_name );
129 }
130
131 /* Custom capabilities were removed after version 2.9.5 */
132 if ( version_compare( $prev_version, '2.9.5', '<=' ) ) {
133 $network_cap = apply_filters( 'code_snippets_network_cap', 'manage_network_snippets' );
134
135 foreach ( get_super_admins() as $admin ) {
136 $user = new WP_User( 0, $admin );
137 $user->remove_cap( $network_cap );
138 }
139 }
140
141 clean_snippets_cache( $table_name );
142 }
143
144 /**
145 * Migrate data from the old integer method of storing scopes to the new string method
146 *
147 * @param string $table_name Name of database table.
148 */
149 private function migrate_scope_data( $table_name ) {
150 global $wpdb;
151
152 $scopes = array(
153 0 => 'global',
154 1 => 'admin',
155 2 => 'front-end',
156 );
157
158 foreach ( $scopes as $scope_number => $scope_name ) {
159 $wpdb->query(
160 $wpdb->prepare(
161 "UPDATE $table_name SET scope = %s WHERE scope = %d",
162 $scope_name,
163 $scope_number
164 )
165 ); // cache ok, will flush at end of process; db call ok.
166 }
167 }
168
169 /**
170 * Build a collection of sample snippets for new users to try out.
171 *
172 * @return array List of Snippet objects.
173 */
174 private function get_sample_content() {
175 $tag = "\n\n" . esc_html__( 'This is a sample snippet. Feel free to use it, edit it, or remove it.', 'code-snippets' );
176
177 $snippets_data = array(
178 array(
179 'name' => esc_html__( 'Make upload filenames lowercase', 'code-snippets' ),
180 'code' => "add_filter( 'sanitize_file_name', 'mb_strtolower' );",
181 'desc' => esc_html__( 'Makes sure that image and file uploads have lowercase filenames.', 'code-snippets' ) . $tag,
182 'tags' => array( 'sample', 'media' ),
183 ),
184 array(
185 'name' => esc_html__( 'Disable admin bar', 'code-snippets' ),
186 'code' => "add_action( 'wp', function () {\n\tif ( ! current_user_can( 'manage_options' ) ) {\n\t\tshow_admin_bar( false );\n\t}\n} );",
187 'desc' => esc_html__( 'Turns off the WordPress admin bar for everyone except administrators.', 'code-snippets' ) . $tag,
188 'tags' => array( 'sample', 'admin-bar' ),
189 'scope' => 'front-end',
190 ),
191 array(
192 'name' => esc_html__( 'Allow smilies', 'code-snippets' ),
193 'code' => "add_filter( 'widget_text', 'convert_smilies' );\nadd_filter( 'the_title', 'convert_smilies' );\nadd_filter( 'wp_title', 'convert_smilies' );\nadd_filter( 'get_bloginfo', 'convert_smilies' );",
194 'desc' => esc_html__( 'Allows smiley conversion in obscure places.', 'code-snippets' ) . $tag,
195 'tags' => array( 'sample' ),
196 ),
197 array(
198 'name' => esc_html__( 'Current year', 'code-snippets' ),
199 'code' => "<?php echo date( 'Y' ); ?>",
200 'desc' => esc_html__( 'Shortcode for inserting the current year into a post or page..', 'code-snippets' ) . $tag,
201 'tags' => array( 'sample', 'dates' ),
202 'scope' => 'content',
203 ),
204 );
205
206 $snippets = array();
207
208 foreach ( $snippets_data as $sample_name => $snippet_data ) {
209 $snippets[ $sample_name ] = new Snippet( $snippet_data );
210 }
211
212 return $snippets;
213 }
214 }
215