PluginProbe
Stream – Activity Log & Audit Trail / 3.6.1
Stream – Activity Log & Audit Trail v3.6.1
4.4.0 4.3.0 4.2.2 4.2.1 trunk 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.1 3.1.1 3.10.0 3.2.0 3.2.1 3.2.2 3.2.3 All 50 releases
stream / classes / class-install.php

class-install.php in Stream – Activity Log & Audit Trail 3.6.1, at classes/class-install.php

433 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles database migrations
4 *
5 * @package WP_Stream
6 */
7
8 namespace WP_Stream;
9
10 /**
11 * Class - Install
12 */
13 class Install {
14 /**
15 * Holds Instance of plugin object
16 *
17 * @var Plugin
18 */
19 public $plugin;
20
21 /**
22 * Option key to store database version
23 *
24 * @var string
25 */
26 public $option_key = 'wp_stream_db';
27
28 /**
29 * Holds version of database at last update
30 *
31 * @var string
32 */
33 public $db_version;
34
35 /**
36 * URL to the Stream Admin settings page.
37 *
38 * @var string
39 */
40 public $stream_url;
41
42 /**
43 * Array of version numbers that require database update
44 *
45 * @var array
46 */
47 public $update_versions;
48
49 /**
50 * Holds status of whether it's safe to run Stream or not
51 *
52 * @var bool
53 */
54 public $update_required = false;
55
56 /**
57 * Holds status of whether the database update worked
58 *
59 * @var bool
60 */
61 public $success_db;
62
63 /**
64 * Class constructor
65 *
66 * @param Plugin $plugin Instance of plugin object.
67 */
68 public function __construct( $plugin ) {
69 $this->plugin = $plugin;
70
71 $this->db_version = $this->get_db_version();
72 $this->stream_url = self_admin_url( $this->plugin->admin->admin_parent_page . '&page=' . $this->plugin->admin->settings_page_slug );
73
74 register_activation_hook( $this->plugin->locations['plugin'], array( $this, 'check' ) );
75 }
76
77 /**
78 * Check db version, create/update table schema accordingly
79 * If database update required admin notice will be given
80 * on the plugin update screen
81 *
82 * @return void
83 */
84 public function check() {
85 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
86 return;
87 }
88
89 if ( empty( $this->db_version ) ) {
90 $this->install( $this->plugin->get_version() );
91 return;
92 }
93
94 if ( $this->plugin->get_version() === $this->db_version ) {
95 return;
96 }
97
98 $update = null;
99 if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) {
100 $update = esc_attr( $_REQUEST['wp_stream_update'] );
101 }
102
103 if ( ! $update ) {
104 $this->update_required = true;
105 $this->success_db = $this->update(
106 $this->db_version,
107 $this->plugin->get_version(),
108 array(
109 'type' => 'auto',
110 )
111 );
112 }
113
114 if ( 'update_and_continue' === $update ) {
115 $this->success_db = $this->update(
116 $this->db_version,
117 $this->plugin->get_version(),
118 array(
119 'type' => 'user',
120 )
121 );
122 }
123
124 $versions = $this->db_update_versions();
125
126 if ( ! $this->success_db && version_compare( end( $versions ), $this->db_version, '>' ) ) {
127 add_action( 'all_admin_notices', array( $this, 'update_notice_hook' ) );
128 return;
129 }
130
131 $this->update_db_option();
132 }
133
134 /**
135 * Register a routine to be called when stream or a stream connector has been updated
136 * It works by comparing the current version with the version previously stored in the database.
137 *
138 * @param string $file A reference to the main plugin file.
139 * @param string $callback The function to run when the hook is called.
140 * @param string $version The version to which the plugin is updating.
141 *
142 * @return void
143 */
144 public function register_update_hook( $file, $callback, $version ) {
145 if ( ! is_admin() ) {
146 return;
147 }
148
149 $plugin = plugin_basename( $file );
150
151 if ( $this->plugin->is_network_activated() ) {
152 $current_versions = get_site_option( $this->option_key . '_connectors', array() );
153 $network = true;
154 } elseif ( is_plugin_active( $plugin ) ) {
155 $current_versions = get_option( $this->option_key . '_connectors', array() );
156 $network = false;
157 } else {
158 return;
159 }
160
161 if ( version_compare( $version, $current_versions[ $plugin ], '>' ) ) {
162 call_user_func( $callback, $current_versions[ $plugin ], $network );
163
164 $current_versions[ $plugin ] = $version;
165 }
166
167 if ( $network ) {
168 update_site_option( $this->option_key . '_registered_connectors', $current_versions );
169 } else {
170 update_option( $this->option_key . '_registered_connectors', $current_versions );
171 }
172 }
173
174 /**
175 * Returns the database version.
176 *
177 * @return string
178 */
179 public function get_db_version() {
180 return get_site_option( $this->option_key );
181 }
182
183 /**
184 * Checks if migration was successful.
185 */
186 public function update_db_option() {
187 if ( $this->success_db ) {
188 $success_op = update_site_option( $this->option_key, $this->plugin->get_version() );
189 }
190
191 if ( ! empty( $this->success_db ) ) {
192 return;
193 }
194
195 wp_die(
196 esc_html__( 'There was an error updating the Stream database. Please try again.', 'stream' ),
197 esc_html__( 'Database Update Error', 'stream' ),
198 array(
199 'response' => 200,
200 'back_link' => 1,
201 )
202 );
203 }
204
205 /**
206 * Added to the admin_notices hook when file plugin version is higher than database plugin version
207 *
208 * @action admin_notices
209 *
210 * @return void
211 */
212 public function update_notice_hook() {
213 if ( ! current_user_can( $this->plugin->admin->view_cap ) ) {
214 return;
215 }
216
217 $update = null;
218 if ( isset( $_REQUEST['wp_stream_update'] ) && wp_verify_nonce( 'wp_stream_update_db' ) ) {
219 $update = esc_attr( $_REQUEST['wp_stream_update'] );
220 }
221
222 if ( ! $update ) {
223 $this->prompt_update();
224
225 return;
226 }
227
228 if ( 'update_and_continue' === $update ) {
229 $this->prompt_update_status();
230 }
231 }
232
233 /**
234 * Action hook callback function
235 *
236 * Adds the user controlled database upgrade routine to the plugins updated page.
237 * When database update is complete page will refresh with dismissible message to user.
238 *
239 * @return void
240 */
241 public function prompt_update() {
242 ?>
243 <div class="error">
244 <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>">
245 <?php wp_nonce_field( 'wp_stream_update_db' ); ?>
246 <input type="hidden" name="wp_stream_update" value="update_and_continue"/>
247 <p><strong><?php esc_html_e( 'Stream Database Update Required', 'stream' ); ?></strong></p>
248 <p><?php esc_html_e( 'Stream has updated! Before we send you on your way, we need to update your database to the newest version.', 'stream' ); ?></p>
249 <p><?php esc_html_e( 'This process could take a little while, so please be patient.', 'stream' ); ?></p>
250 <?php submit_button( esc_html__( 'Update Database', 'stream' ), 'primary', 'stream-update-db-submit' ); ?>
251 </form>
252 </div>
253 <?php
254 }
255
256 /**
257 * When user initiates a database update this function calls the update methods, checks for success
258 * updates the stream_db version number in the database and outputs a success and continue message
259 *
260 * @return void
261 */
262 public function prompt_update_status() {
263 check_admin_referer( 'wp_stream_update_db' );
264
265 $this->update_db_option();
266 ?>
267 <div class="updated">
268 <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>" style="display:inline;">
269 <p><strong><?php esc_html_e( 'Update Complete', 'stream' ); ?></strong></p>
270 <p>
271 <?php
272 printf(
273 /* translators: %1$s: old version, %2$s: new version (e.g. "4.2") */
274 esc_html__( 'Your Stream database has been successfully updated from %1$s to %2$s!', 'stream' ),
275 esc_html( $this->db_version ),
276 esc_html( $this->plugin->get_version() )
277 );
278 ?>
279 </p>
280 <?php submit_button( esc_html__( 'Continue', 'stream' ), 'secondary', false ); ?>
281 </form>
282 </div>
283 <?php
284 }
285
286 /**
287 * Array of database versions that require and updates
288 *
289 * To add your own stream extension database update routine
290 * use the filter and return the version that requires an update
291 * You must also make the callback function available in the global namespace on plugins loaded
292 * use the wp_stream_update_{version_number} version number must be a string of characters that represent the version with no periods
293 *
294 * @return array
295 */
296 public function db_update_versions() {
297 $db_update_versions = array(
298 '3.0.0', /* @version 3.0.0 Drop the stream_context table, changes to stream table */
299 '3.0.2', /* @version 3.0.2 Fix uppercase values in stream table, connector column */
300 '3.0.8', /* @version 3.0.8 Increase size of user role IDs, user_roll column */
301 );
302
303 /**
304 * Filter to alter the DB update versions array
305 *
306 * @param array $db_update_versions
307 *
308 * @return array
309 */
310 return apply_filters( 'wp_stream_db_update_versions', $db_update_versions );
311 }
312
313 /**
314 * Database user controlled update routine
315 *
316 * @param int $db_version Next database version.
317 * @param int $current_version Current database version.
318 * @param array $update_args Update options.
319 *
320 * @return mixed Version number on success, true on no update needed, mysql error message on error
321 */
322 public function update( $db_version, $current_version, $update_args ) {
323 $versions = $this->db_update_versions();
324 include_once $this->plugin->locations['inc_dir'] . 'db-updates.php';
325
326 foreach ( $versions as $version ) {
327 if ( ! isset( $update_args['type'] ) ) {
328 $update_args['type'] = 'user';
329 }
330
331 $function = 'wp_stream_update_' . ( 'user' === $update_args['type'] ? '' : $update_args['type'] . '_' ) . str_ireplace( '.', '', $version );
332
333 if ( version_compare( $db_version, $version, '<' ) ) {
334 $result = function_exists( $function ) ? call_user_func( $function, $db_version, $current_version ) : $current_version;
335
336 if ( $current_version !== $result ) {
337 return false;
338 }
339 }
340 }
341
342 return $current_version;
343 }
344
345 /**
346 * Initial database install routine
347 *
348 * @param string $current_version Current database version.
349 *
350 * @return string
351 */
352 public function install( $current_version ) {
353 global $wpdb;
354
355 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
356
357 $sql = "CREATE TABLE {$wpdb->base_prefix}stream (
358 ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
359 site_id bigint(20) unsigned NOT NULL DEFAULT '1',
360 blog_id bigint(20) unsigned NOT NULL DEFAULT '1',
361 object_id bigint(20) unsigned NULL,
362 user_id bigint(20) unsigned NOT NULL DEFAULT '0',
363 user_role varchar(50) NOT NULL DEFAULT '',
364 summary longtext NOT NULL,
365 created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
366 connector varchar(100) NOT NULL,
367 context varchar(100) NOT NULL,
368 action varchar(100) NOT NULL,
369 ip varchar(39) NULL,
370 PRIMARY KEY (ID),
371 KEY site_id (site_id),
372 KEY blog_id (blog_id),
373 KEY object_id (object_id),
374 KEY user_id (user_id),
375 KEY created (created),
376 KEY connector (connector),
377 KEY context (context),
378 KEY action (action)
379 )";
380
381 if ( ! empty( $wpdb->charset ) ) {
382 $sql .= " CHARACTER SET $wpdb->charset";
383 }
384
385 if ( ! empty( $wpdb->collate ) ) {
386 $sql .= " COLLATE $wpdb->collate";
387 }
388
389 $sql .= ';';
390
391 \dbDelta( $sql );
392
393 if ( ! empty( $wpdb->charset ) ) {
394 $sql .= " CHARACTER SET $wpdb->charset";
395 }
396
397 if ( ! empty( $wpdb->collate ) ) {
398 $sql .= " COLLATE $wpdb->collate";
399 }
400
401 $sql .= ';';
402
403 \dbDelta( $sql );
404
405 $sql = "CREATE TABLE {$wpdb->base_prefix}stream_meta (
406 meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
407 record_id bigint(20) unsigned NOT NULL,
408 meta_key varchar(200) NOT NULL,
409 meta_value varchar(200) NOT NULL,
410 PRIMARY KEY (meta_id),
411 KEY record_id (record_id),
412 KEY meta_key (meta_key(191)),
413 KEY meta_value (meta_value(191))
414 )";
415
416 if ( ! empty( $wpdb->charset ) ) {
417 $sql .= " CHARACTER SET $wpdb->charset";
418 }
419
420 if ( ! empty( $wpdb->collate ) ) {
421 $sql .= " COLLATE $wpdb->collate";
422 }
423
424 $sql .= ';';
425
426 \dbDelta( $sql );
427
428 update_site_option( $this->option_key, $this->plugin->get_version() );
429
430 return $current_version;
431 }
432 }
433