PluginProbe
Code Snippets / 3.0.0
Code Snippets v3.0.0
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.0.0, at php/class-upgrade.php

217 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
106 /**
107 * Perform multisite-only upgrades
108 */
109 private function do_multisite_upgrades() {
110 $table_name = $this->db->ms_table;
111 $prev_version = get_site_option( 'code_snippets_version' );
112
113 /* Do nothing if the plugin has not been updated or installed */
114 if ( ! version_compare( $prev_version, $this->current_version, '<' ) ) {
115 return;
116 }
117
118 /* Always attempt to create or upgrade the database tables */
119 $this->db->create_table( $table_name );
120
121 /* Update the plugin version stored in the database */
122 update_site_option( 'code_snippets_version', $this->current_version );
123
124 /* Update the scope column of the database */
125 if ( version_compare( $prev_version, '2.10.0', '<' ) ) {
126 $this->migrate_scope_data( $table_name );
127 }
128
129 /* Custom capabilities were removed after version 2.9.5 */
130 if ( version_compare( $prev_version, '2.9.5', '<=' ) ) {
131 $network_cap = apply_filters( 'code_snippets_network_cap', 'manage_network_snippets' );
132
133 foreach ( get_super_admins() as $admin ) {
134 $user = new WP_User( 0, $admin );
135 $user->remove_cap( $network_cap );
136 }
137 }
138 }
139
140 /**
141 * Migrate data from the old integer method of storing scopes to the new string method
142 *
143 * @param string $table_name Name of database table.
144 *
145 * @phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
146 */
147 private function migrate_scope_data( $table_name ) {
148 global $wpdb;
149
150 $scopes = array(
151 0 => 'global',
152 1 => 'admin',
153 2 => 'front-end',
154 );
155
156 foreach ( $scopes as $scope_number => $scope_name ) {
157 $wpdb->query(
158 $wpdb->prepare(
159 "UPDATE $table_name SET scope = %s WHERE scope = %d",
160 $scope_name,
161 $scope_number
162 )
163 );
164 }
165 }
166
167 /**
168 * Build a collection of sample snippets for new users to try out.
169 *
170 * @return array List of Snippet objects.
171 */
172 private function get_sample_content() {
173 $tag = "\n\n" . esc_html__( 'This is a sample snippet. Feel free to use it, edit it, or remove it.', 'code-snippets' );
174
175 $snippets_data = array(
176
177 'lowercase_filenames' => array(
178 'name' => esc_html__( 'Make upload filenames lowercase', 'code-snippets' ),
179 'code' => "add_filter( 'sanitize_file_name', 'mb_strtolower' );",
180 'desc' => esc_html__( 'Makes sure that image and file uploads have lowercase filenames.', 'code-snippets' ) . $tag,
181 'tags' => array( 'sample', 'media' ),
182 ),
183
184 'disable_admin_bar' => 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
192 'allow_smilies' => array(
193 'name' => esc_html__( 'Allow smilies', 'code-snippets' ),
194 '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' );",
195 'desc' => esc_html__( 'Allows smiley conversion in obscure places.', 'code-snippets' ) . $tag,
196 'tags' => array( 'sample' ),
197 ),
198
199 'current_year' => array(
200 'name' => esc_html__( 'Current year', 'code-snippets' ),
201 'code' => "<?php echo date( 'Y' ); ?>",
202 'desc' => esc_html__( 'Shortcode for inserting the current year into a post or page..', 'code-snippets' ) . $tag,
203 'tags' => array( 'sample', 'dates' ),
204 'scope' => 'content',
205 ),
206 );
207
208 $snippets = array();
209
210 foreach ( $snippets_data as $sample_name => $snippet_data ) {
211 $snippets[ $sample_name ] = new Snippet( $snippet_data );
212 }
213
214 return $snippets;
215 }
216 }
217