PluginProbe
Code Snippets / 2.14.3
Code Snippets v2.14.3
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 2.14.3, at php/class-upgrade.php

229 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * The current plugin version number
16 * @var string
17 */
18 private $current_version;
19
20 /**
21 * Class constructor
22 *
23 * @param string $version Current plugin version
24 * @param Code_Snippets_DB $db Instance of database class
25 */
26 public function __construct( $version, Code_Snippets_DB $db ) {
27 $this->db = $db;
28 $this->current_version = $version;
29 }
30
31 /**
32 * Run the upgrade functions
33 */
34 public function run() {
35
36 /* Always run multisite upgrades, even if not on the main site, as subsites depend on the network snippet table */
37 if ( is_multisite() ) {
38 $this->do_multisite_upgrades();
39 }
40
41 $this->do_site_upgrades();
42 }
43
44 /**
45 * Perform upgrades for the current site
46 */
47 private function do_site_upgrades() {
48 $table_name = $this->db->table;
49 $prev_version = get_option( 'code_snippets_version' );
50
51 /* Do nothing if the plugin has not just been updated or installed */
52 if ( ! version_compare( $prev_version, $this->current_version, '<' ) ) {
53 return;
54 }
55
56 /* Update the plugin version stored in the database */
57 $updated = update_option( 'code_snippets_version', $this->current_version );
58
59 if ( ! $updated ) {
60 return; // bail if the data was not successfully saved to prevent this process from repeating
61 }
62
63 $sample_snippets = $this->get_sample_content();
64 $this->db->create_table( $table_name );
65
66 /* Remove outdated user meta */
67 if ( version_compare( $prev_version, '2.14.1', '<' ) ) {
68 global $wpdb;
69
70 $prefix = $wpdb->get_blog_prefix();
71 $menu_slug = code_snippets()->get_menu_slug();
72 $option_name = "{$prefix}managetoplevel_page_{$menu_slug}columnshidden";
73
74 // loop through each user ID and remove all matching user meta
75 foreach ( get_users( array( 'fields' => 'ID' ) ) as $user_id ) {
76 delete_metadata( 'user', $user_id, $option_name, '', true );
77 }
78 }
79
80 /* Update the scope column of the database */
81 if ( version_compare( $prev_version, '2.10.0', '<' ) ) {
82 $this->migrate_scope_data( $table_name );
83 }
84
85 /* Custom capabilities were removed after version 2.9.5 */
86 if ( version_compare( $prev_version, '2.9.5', '<=' ) ) {
87 $role = get_role( apply_filters( 'code_snippets_role', 'administrator' ) );
88 $role->remove_cap( apply_filters( 'code_snippets_cap', 'manage_snippets' ) );
89 }
90
91 if ( false === $prev_version ) {
92 if ( apply_filters( 'code_snippets/create_sample_content', true ) ) {
93
94 foreach ( $sample_snippets as $sample_snippet ) {
95 save_snippet( $sample_snippet );
96 }
97
98 }
99 } elseif ( version_compare( $prev_version, '2.14.0', '<' ) ) {
100 save_snippet( $sample_snippets['orderby_date'] );
101 }
102 }
103
104 /**
105 * Perform multisite-only upgrades
106 */
107 private function do_multisite_upgrades() {
108 $table_name = $this->db->ms_table;
109 $prev_version = get_site_option( 'code_snippets_version' );
110
111 /* Do nothing if the plugin has not been updated or installed */
112 if ( ! version_compare( $prev_version, $this->current_version, '<' ) ) {
113 return;
114 }
115
116 /* Always attempt to create or upgrade the database tables */
117 $this->db->create_table( $table_name );
118
119 /* Update the plugin version stored in the database */
120 update_site_option( 'code_snippets_version', $this->current_version );
121
122 /* Update the scope column of the database */
123 if ( version_compare( $prev_version, '2.10.0', '<' ) ) {
124 $this->migrate_scope_data( $table_name );
125 }
126
127 /* Custom capabilities were removed after version 2.9.5 */
128 if ( version_compare( $prev_version, '2.9.5', '<=' ) ) {
129 $network_cap = apply_filters( 'code_snippets_network_cap', 'manage_network_snippets' );
130
131 foreach ( get_super_admins() as $admin ) {
132 $user = new WP_User( 0, $admin );
133 $user->remove_cap( $network_cap );
134 }
135 }
136 }
137
138 /**
139 * Migrate data from the old integer method of storing scopes to the new string method
140 *
141 * @param string $table_name
142 */
143 private function migrate_scope_data( $table_name ) {
144 global $wpdb;
145
146 $scopes = array(
147 0 => 'global',
148 1 => 'admin',
149 2 => 'front-end',
150 );
151
152 foreach ( $scopes as $scope_number => $scope_name ) {
153 $wpdb->query( sprintf(
154 "UPDATE %s SET scope = '%s' WHERE scope = %d",
155 $table_name, $scope_name, $scope_number
156 ) );
157 }
158 }
159
160 /**
161 * Build a collection of sample snippets for new users to try out.
162 *
163 * @return array List of Snippet objects.
164 */
165 private function get_sample_content() {
166 $tag = "\n\n" . esc_html__( 'You can remove it, or edit it to add your own content.', 'code-snippets' );
167
168 $snippets_data = array(
169
170 'example_html' => array(
171 'name' => esc_html__( 'Example HTML shortcode', 'code-snippets' ),
172 'code' => sprintf(
173 "\nadd_shortcode( 'shortcode_name', function () {\n\n\t\$out = '<p>%s</p>';\n\n\treturn \$out;\n} );",
174 wp_strip_all_tags( __( 'write your HTML shortcode content here', 'code-snippets' ) )
175 ),
176 'desc' => esc_html__( 'This is an example snippet for demonstrating how to add an HTML shortcode.', 'code-snippets' ) . $tag,
177 'tags' => array( 'shortcode' ),
178 ),
179
180 'example_css' => array(
181 'name' => esc_html__( 'Example CSS snippet', 'code-snippets' ),
182 'code' => sprintf(
183 "\nadd_action( 'wp_head', function () { ?>\n<style>\n\n\t/* %s */\n\n</style>\n<?php } );\n",
184 wp_strip_all_tags( __( 'write your CSS code here', 'code-snippets' ) )
185 ),
186 'desc' => esc_html__( 'This is an example snippet for demonstrating how to add custom CSS code to your website.', 'code-snippets' ) . $tag,
187 'tags' => array( 'css' ),
188 'scope' => 'front-end',
189 ),
190
191 'example_js' => array(
192 'name' => esc_html__( 'Example JavaScript snippet', 'code-snippets' ),
193 'code' => sprintf(
194 "\nadd_action( 'wp_head', function () { ?>\n<script>\n\n\t/* %s */\n\n</script>\n<?php } );\n",
195 wp_strip_all_tags( __( 'write your JavaScript code here', 'code-snippets' ) )
196 ),
197 'desc' => esc_html__( 'This is an example snippet for demonstrating how to add custom JavaScript code to your website.', 'code-snippets' ) . $tag,
198 'tags' => array( 'javascript' ),
199 'scope' => 'front-end',
200 ),
201
202 'orderby_name' => array(
203 'name' => esc_html__( 'Order snippets by name', 'code-snippets' ),
204 'code' => "\nadd_filter( 'code_snippets/list_table/default_orderby', function () {\n\treturn 'name';\n} );\n",
205 'desc' => esc_html__( 'Order snippets by name by default in the snippets table.', 'code-snippets' ),
206 'tags' => array( 'code-snippets-plugin' ),
207 'scope' => 'admin',
208 ),
209
210 'orderby_date' => array(
211 'name' => esc_html__( 'Order snippets by date', 'code-snippets' ),
212 'code' => "\nadd_filter( 'code_snippets/list_table/default_orderby', function () {\n\treturn 'modified';\n} );\n" .
213 "\nadd_filter( 'code_snippets/list_table/default_order', function () {\n\treturn 'desc';\n} );\n",
214 'desc' => esc_html__( 'Order snippets by last modification date by default in the snippets table.', 'code-snippets' ),
215 'tags' => array( 'code-snippets-plugin' ),
216 'scope' => 'admin',
217 ),
218 );
219
220 $snippets = array();
221
222 foreach ( $snippets_data as $sample_name => $snippet_data ) {
223 $snippets[ $sample_name ] = new Code_Snippet( $snippet_data );
224 }
225
226 return $snippets;
227 }
228 }
229