PluginProbe
Code Snippets / 2.14.6
Code Snippets v2.14.6
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.6, at php/class-upgrade.php

231 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 } elseif ( version_compare( $prev_version, '2.14.0', '<' ) ) {
99 save_snippet( $sample_snippets['orderby_date'] );
100 }
101 }
102
103 /**
104 * Perform multisite-only upgrades
105 */
106 private function do_multisite_upgrades() {
107 $table_name = $this->db->ms_table;
108 $prev_version = get_site_option( 'code_snippets_version' );
109
110 /* Do nothing if the plugin has not been updated or installed */
111 if ( ! version_compare( $prev_version, $this->current_version, '<' ) ) {
112 return;
113 }
114
115 /* Always attempt to create or upgrade the database tables */
116 $this->db->create_table( $table_name );
117
118 /* Update the plugin version stored in the database */
119 update_site_option( 'code_snippets_version', $this->current_version );
120
121 /* Update the scope column of the database */
122 if ( version_compare( $prev_version, '2.10.0', '<' ) ) {
123 $this->migrate_scope_data( $table_name );
124 }
125
126 /* Custom capabilities were removed after version 2.9.5 */
127 if ( version_compare( $prev_version, '2.9.5', '<=' ) ) {
128 $network_cap = apply_filters( 'code_snippets_network_cap', 'manage_network_snippets' );
129
130 foreach ( get_super_admins() as $admin ) {
131 $user = new WP_User( 0, $admin );
132 $user->remove_cap( $network_cap );
133 }
134 }
135 }
136
137 /**
138 * Migrate data from the old integer method of storing scopes to the new string method
139 *
140 * @param string $table_name
141 */
142 private function migrate_scope_data( $table_name ) {
143 global $wpdb;
144
145 $scopes = array(
146 0 => 'global',
147 1 => 'admin',
148 2 => 'front-end',
149 );
150
151 foreach ( $scopes as $scope_number => $scope_name ) {
152 $wpdb->query(
153 $wpdb->prepare(
154 "UPDATE $table_name SET scope = %s WHERE scope = %d",
155 $scope_name,
156 $scope_number
157 )
158 );
159 }
160 }
161
162 /**
163 * Build a collection of sample snippets for new users to try out.
164 *
165 * @return array List of Snippet objects.
166 */
167 private function get_sample_content() {
168 $tag = "\n\n" . esc_html__( 'You can remove it, or edit it to add your own content.', 'code-snippets' );
169
170 $snippets_data = array(
171
172 'example_html' => array(
173 'name' => esc_html__( 'Example HTML shortcode', 'code-snippets' ),
174 'code' => sprintf(
175 "\nadd_shortcode( 'shortcode_name', function () {\n\n\t\$out = '<p>%s</p>';\n\n\treturn \$out;\n} );",
176 wp_strip_all_tags( __( 'write your HTML shortcode content here', 'code-snippets' ) )
177 ),
178 'desc' => esc_html__( 'This is an example snippet for demonstrating how to add an HTML shortcode.', 'code-snippets' ) . $tag,
179 'tags' => array( 'shortcode' ),
180 ),
181
182 'example_css' => array(
183 'name' => esc_html__( 'Example CSS snippet', 'code-snippets' ),
184 'code' => sprintf(
185 "\nadd_action( 'wp_head', function () { ?>\n<style>\n\n\t/* %s */\n\n</style>\n<?php } );\n",
186 wp_strip_all_tags( __( 'write your CSS code here', 'code-snippets' ) )
187 ),
188 'desc' => esc_html__( 'This is an example snippet for demonstrating how to add custom CSS code to your website.', 'code-snippets' ) . $tag,
189 'tags' => array( 'css' ),
190 'scope' => 'front-end',
191 ),
192
193 'example_js' => array(
194 'name' => esc_html__( 'Example JavaScript snippet', 'code-snippets' ),
195 'code' => sprintf(
196 "\nadd_action( 'wp_head', function () { ?>\n<script>\n\n\t/* %s */\n\n</script>\n<?php } );\n",
197 wp_strip_all_tags( __( 'write your JavaScript code here', 'code-snippets' ) )
198 ),
199 'desc' => esc_html__( 'This is an example snippet for demonstrating how to add custom JavaScript code to your website.', 'code-snippets' ) . $tag,
200 'tags' => array( 'javascript' ),
201 'scope' => 'front-end',
202 ),
203
204 'orderby_name' => array(
205 'name' => esc_html__( 'Order snippets by name', 'code-snippets' ),
206 'code' => "\nadd_filter( 'code_snippets/list_table/default_orderby', function () {\n\treturn 'name';\n} );\n",
207 'desc' => esc_html__( 'Order snippets by name by default in the snippets table.', 'code-snippets' ),
208 'tags' => array( 'code-snippets-plugin' ),
209 'scope' => 'admin',
210 ),
211
212 'orderby_date' => array(
213 'name' => esc_html__( 'Order snippets by date', 'code-snippets' ),
214 'code' => "\nadd_filter( 'code_snippets/list_table/default_orderby', function () {\n\treturn 'modified';\n} );\n" .
215 "\nadd_filter( 'code_snippets/list_table/default_order', function () {\n\treturn 'desc';\n} );\n",
216 'desc' => esc_html__( 'Order snippets by last modification date by default in the snippets table.', 'code-snippets' ),
217 'tags' => array( 'code-snippets-plugin' ),
218 'scope' => 'admin',
219 ),
220 );
221
222 $snippets = array();
223
224 foreach ( $snippets_data as $sample_name => $snippet_data ) {
225 $snippets[ $sample_name ] = new Code_Snippet( $snippet_data );
226 }
227
228 return $snippets;
229 }
230 }
231