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

212 lines 6.6 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 been updated or installed */
52 if ( ! version_compare( $prev_version, $this->current_version, '<' ) ) {
53 return;
54 }
55
56 $sample_snippets = $this->get_sample_content();
57
58 $this->db->create_table( $table_name );
59
60 /* Update the plugin version stored in the database */
61 update_option( 'code_snippets_version', $this->current_version );
62
63 /* Update the scope column of the database */
64 if ( version_compare( $prev_version, '2.10.0', '<' ) ) {
65 $this->migrate_scope_data( $table_name );
66 }
67
68 /* Custom capabilities were removed after version 2.9.5 */
69 if ( version_compare( $prev_version, '2.9.5', '<=' ) ) {
70 $role = get_role( apply_filters( 'code_snippets_role', 'administrator' ) );
71 $role->remove_cap( apply_filters( 'code_snippets_cap', 'manage_snippets' ) );
72 }
73
74 if ( false === $prev_version ) {
75 if ( apply_filters( 'code_snippets/create_sample_content', true ) ) {
76
77 foreach ( $sample_snippets as $sample_snippet ) {
78 save_snippet( $sample_snippet );
79 }
80
81 }
82 } elseif ( version_compare( $prev_version, '2.14.0', '<' ) ) {
83 save_snippet( $sample_snippets['orderby_date'] );
84 }
85 }
86
87 /**
88 * Perform multisite-only upgrades
89 */
90 private function do_multisite_upgrades() {
91 $table_name = $this->db->ms_table;
92 $prev_version = get_site_option( 'code_snippets_version' );
93
94 /* Do nothing if the plugin has not been updated or installed */
95 if ( ! version_compare( $prev_version, $this->current_version, '<' ) ) {
96 return;
97 }
98
99 /* Always attempt to create or upgrade the database tables */
100 $this->db->create_table( $table_name );
101
102 /* Update the plugin version stored in the database */
103 update_site_option( 'code_snippets_version', $this->current_version );
104
105 /* Update the scope column of the database */
106 if ( version_compare( $prev_version, '2.10.0', '<' ) ) {
107 $this->migrate_scope_data( $table_name );
108 }
109
110 /* Custom capabilities were removed after version 2.9.5 */
111 if ( version_compare( $prev_version, '2.9.5', '<=' ) ) {
112 $network_cap = apply_filters( 'code_snippets_network_cap', 'manage_network_snippets' );
113
114 foreach ( get_super_admins() as $admin ) {
115 $user = new WP_User( 0, $admin );
116 $user->remove_cap( $network_cap );
117 }
118 }
119 }
120
121 /**
122 * Migrate data from the old integer method of storing scopes to the new string method
123 *
124 * @param string $table_name
125 */
126 private function migrate_scope_data( $table_name ) {
127 global $wpdb;
128
129 $scopes = array(
130 0 => 'global',
131 1 => 'admin',
132 2 => 'front-end',
133 );
134
135 foreach ( $scopes as $scope_number => $scope_name ) {
136 $wpdb->query( sprintf(
137 "UPDATE %s SET scope = '%s' WHERE scope = %d",
138 $table_name, $scope_name, $scope_number
139 ) );
140 }
141 }
142
143 /**
144 * Build a collection of sample snippets for new users to try out.
145 *
146 * @return array List of Snippet objects.
147 */
148 private function get_sample_content() {
149 $tag = "\n\n" . esc_html__( 'You can remove it, or edit it to add your own content.', 'code-snippets' );
150
151 $snippets_data = array(
152
153 'example_html' => array(
154 'name' => esc_html__( 'Example HTML shortcode', 'code-snippets' ),
155 'code' => sprintf(
156 "\nadd_shortcode( 'shortcode_name', function () {\n\n\t\$out = '<p>%s</p>';\n\n\treturn \$out;\n} );",
157 wp_strip_all_tags( __( 'write your HTML shortcode content here', 'code-snippets' ) )
158 ),
159 'desc' => esc_html__( 'This is an example snippet for demonstrating how to add an HTML shortcode.', 'code-snippets' ) . $tag,
160 'tags' => array( 'shortcode' ),
161 ),
162
163 'example_css' => array(
164 'name' => esc_html__( 'Example CSS snippet', 'code-snippets' ),
165 'code' => sprintf(
166 "\nadd_action( 'wp_head', function () { ?>\n<style>\n\n\t/* %s */\n\n</style>\n<?php } );\n",
167 wp_strip_all_tags( __( 'write your CSS code here', 'code-snippets' ) )
168 ),
169 'desc' => esc_html__( 'This is an example snippet for demonstrating how to add custom CSS code to your website.', 'code-snippets' ) . $tag,
170 'tags' => array( 'css' ),
171 'scope' => 'front-end',
172 ),
173
174 'example_js' => array(
175 'name' => esc_html__( 'Example JavaScript snippet', 'code-snippets' ),
176 'code' => sprintf(
177 "\nadd_action( 'wp_head', function () { ?>\n<script>\n\n\t/* %s */\n\n</script>\n<?php } );\n",
178 wp_strip_all_tags( __( 'write your JavaScript code here', 'code-snippets' ) )
179 ),
180 'desc' => esc_html__( 'This is an example snippet for demonstrating how to add custom JavaScript code to your website.', 'code-snippets' ) . $tag,
181 'tags' => array( 'javascript' ),
182 'scope' => 'front-end',
183 ),
184
185 'orderby_name' => array(
186 'name' => esc_html__( 'Order snippets by name', 'code-snippets' ),
187 'code' => "\nadd_filter( 'code_snippets/list_table/default_orderby', function () {\n\treturn 'name';\n} );\n",
188 'desc' => esc_html__( 'Order snippets by name by default in the snippets table.', 'code-snippets' ),
189 'tags' => array( 'code-snippets-plugin' ),
190 'scope' => 'admin',
191 ),
192
193 'orderby_date' => array(
194 'name' => esc_html__( 'Order snippets by date', 'code-snippets' ),
195 'code' => "\nadd_filter( 'code_snippets/list_table/default_orderby', function () {\n\treturn 'modified';\n} );\n" .
196 "\nadd_filter( 'code_snippets/list_table/default_order', function () {\n\treturn 'desc';\n} );\n",
197 'desc' => esc_html__( 'Order snippets by last modification date by default in the snippets table.', 'code-snippets' ),
198 'tags' => array( 'code-snippets-plugin' ),
199 'scope' => 'admin',
200 ),
201 );
202
203 $snippets = array();
204
205 foreach ( $snippets_data as $sample_name => $snippet_data ) {
206 $snippets[ $sample_name ] = new Code_Snippet( $snippet_data );
207 }
208
209 return $snippets;
210 }
211 }
212