PluginProbe
Code Snippets / 2.13.3
Code Snippets v2.13.3
4.0.0-beta.2 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 All 65 releases
code-snippets / php / class-upgrade.php

class-upgrade.php in Code Snippets 2.13.3, at php/class-upgrade.php

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