PluginProbe
Code Snippets / 3.10.1
Code Snippets v3.10.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 / Core / Upgrader.php

Upgrader.php in Code Snippets 3.10.1, at php/Core/Upgrader.php

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