PluginProbe
Stream – Activity Log & Audit Trail / 3.7.0
Stream – Activity Log & Audit Trail v3.7.0
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
← All changes | classes/class-install.php +67 -106 3.2.13.7.0 View file →
@@ -1,10 +1,20 @@
1 1 <?php
2 +/**
3 + * Handles database migrations
4 + *
5 + * @package WP_Stream
6 + */
7 +
2 8 namespace WP_Stream;
3 9
10 +/**
11 + * Class - Install
12 + */
4 13 class Install {
5 14 /**
6 - * Hold Plugin class
15 + * Holds Instance of plugin object
16 + *
7 17 * @var Plugin
8 18 */
9 19 public $plugin;
10 20
@@ -51,8 +61,10 @@
51 61 public $success_db;
52 62
53 63 /**
54 64 * Class constructor
65 + *
66 + * @param Plugin $plugin Instance of plugin object.
55 67 */
56 68 public function __construct( $plugin ) {
57 69 $this->plugin = $plugin;
58 70
@@ -58,14 +70,8 @@
58 70
59 71 $this->db_version = $this->get_db_version();
60 72 $this->stream_url = self_admin_url( $this->plugin->admin->admin_parent_page . '&page=' . $this->plugin->admin->settings_page_slug );
61 73
62 - // Check DB and display an admin notice if there are tables missing
63 - add_action( 'init', array( $this, 'verify_db' ) );
64 -
65 - // Install the plugin
66 - add_action( 'wp_stream_before_db_notices', array( $this, 'check' ) );
67 -
68 74 register_activation_hook( $this->plugin->locations['plugin'], array( $this, 'check' ) );
69 75 }
70 76
71 77 /**
@@ -81,9 +87,8 @@
81 87 }
82 88
83 89 if ( empty( $this->db_version ) ) {
84 90 $this->install( $this->plugin->get_version() );
85 -
86 91 return;
87 92 }
88 93
89 94 if ( $this->plugin->get_version() === $this->db_version ) {
@@ -89,17 +94,32 @@
89 94 if ( $this->plugin->get_version() === $this->db_version ) {
90 95 return;
91 96 }
92 97
93 - $update = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null;
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 + }
94 102
95 103 if ( ! $update ) {
96 104 $this->update_required = true;
97 - $this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'auto' ) );
105 + $this->success_db = $this->update(
106 + $this->db_version,
107 + $this->plugin->get_version(),
108 + array(
109 + 'type' => 'auto',
110 + )
111 + );
98 112 }
99 113
100 114 if ( 'update_and_continue' === $update ) {
101 - $this->success_db = $this->update( $this->db_version, $this->plugin->get_version(), array( 'type' => 'user' ) );
115 + $this->success_db = $this->update(
116 + $this->db_version,
117 + $this->plugin->get_version(),
118 + array(
119 + 'type' => 'user',
120 + )
121 + );
102 122 }
103 123
104 124 $versions = $this->db_update_versions();
105 125
@@ -111,83 +131,12 @@
111 131 $this->update_db_option();
112 132 }
113 133
114 134 /**
115 - * Verify that the required DB tables exists
116 - *
117 - * @return void
118 - */
119 - public function verify_db() {
120 - /**
121 - * Filter will halt install() if set to true
122 - *
123 - * @param bool
124 - *
125 - * @return bool
126 - */
127 - if ( apply_filters( 'wp_stream_no_tables', false ) ) {
128 - return;
129 - }
130 -
131 - if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
132 - require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
133 - }
134 -
135 - /**
136 - * Fires before admin notices are triggered for missing database tables.
137 - */
138 - do_action( 'wp_stream_before_db_notices' );
139 -
140 - global $wpdb;
141 -
142 - $database_message = '';
143 - $uninstall_message = '';
144 -
145 - // Check if all needed DB is present
146 - $missing_tables = array();
147 -
148 - foreach ( $this->plugin->db->get_table_names() as $table_name ) {
149 - $table_search = $wpdb->get_var(
150 - $wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name )
151 - );
152 - if ( $table_search !== $table_name ) {
153 - $missing_tables[] = $table_name;
154 - }
155 - }
156 -
157 - if ( $missing_tables ) {
158 - $database_message .= sprintf(
159 - '%s <strong>%s</strong>',
160 - _n(
161 - 'The following table is not present in the WordPress database:',
162 - 'The following tables are not present in the WordPress database:',
163 - count( $missing_tables ),
164 - 'stream'
165 - ),
166 - esc_html( implode( ', ', $missing_tables ) )
167 - );
168 - }
169 -
170 - if ( is_plugin_active_for_network( $this->plugin->locations['plugin'] ) && current_user_can( 'manage_network_plugins' ) ) {
171 - $uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), network_admin_url( 'plugins.php#stream' ) );
172 - } elseif ( current_user_can( 'activate_plugins' ) ) {
173 - $uninstall_message = sprintf( __( 'Please <a href="%s">uninstall</a> the Stream plugin and activate it again.', 'stream' ), admin_url( 'plugins.php#stream' ) );
174 - }
175 -
176 - if ( ! empty( $database_message ) ) {
177 - $this->plugin->admin->notice( $database_message );
178 -
179 - if ( ! empty( $uninstall_message ) ) {
180 - $this->plugin->admin->notice( $uninstall_message );
181 - }
182 - }
183 - }
184 -
185 - /**
186 135 * Register a routine to be called when stream or a stream connector has been updated
187 136 * It works by comparing the current version with the version previously stored in the database.
188 137 *
189 - * @param string $file A reference to the main plugin file
138 + * @param string $file A reference to the main plugin file.
190 139 * @param string $callback The function to run when the hook is called.
191 140 * @param string $version The version to which the plugin is updating.
192 141 *
193 142 * @return void
@@ -198,9 +147,9 @@
198 147 }
199 148
200 149 $plugin = plugin_basename( $file );
201 150
202 - if ( is_plugin_active_for_network( $plugin ) ) {
151 + if ( $this->plugin->is_network_activated() ) {
203 152 $current_versions = get_site_option( $this->option_key . '_connectors', array() );
204 153 $network = true;
205 154 } elseif ( is_plugin_active( $plugin ) ) {
206 155 $current_versions = get_option( $this->option_key . '_connectors', array() );
@@ -219,13 +168,13 @@
219 168 update_site_option( $this->option_key . '_registered_connectors', $current_versions );
220 169 } else {
221 170 update_option( $this->option_key . '_registered_connectors', $current_versions );
222 171 }
223 -
224 - return;
225 172 }
226 173
227 174 /**
175 + * Returns the database version.
176 + *
228 177 * @return string
229 178 */
230 179 public function get_db_version() {
231 180 return get_site_option( $this->option_key );
@@ -231,9 +180,9 @@
231 180 return get_site_option( $this->option_key );
232 181 }
233 182
234 183 /**
235 - * @return void
184 + * Checks if migration was successful.
236 185 */
237 186 public function update_db_option() {
238 187 if ( $this->success_db ) {
239 188 $success_op = update_site_option( $this->option_key, $this->plugin->get_version() );
@@ -264,9 +213,12 @@
264 213 if ( ! current_user_can( $this->plugin->admin->view_cap ) ) {
265 214 return;
266 215 }
267 216
268 - $update = isset( $_REQUEST['wp_stream_update'] ) ? $_REQUEST['wp_stream_update'] : null;
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 + }
269 221
270 222 if ( ! $update ) {
271 223 $this->prompt_update();
272 224
@@ -288,15 +240,15 @@
288 240 */
289 241 public function prompt_update() {
290 242 ?>
291 243 <div class="error">
292 - <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ) ?>">
293 - <?php wp_nonce_field( 'wp_stream_update_db' ) ?>
244 + <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ); ?>">
245 + <?php wp_nonce_field( 'wp_stream_update_db' ); ?>
294 246 <input type="hidden" name="wp_stream_update" value="update_and_continue"/>
295 - <p><strong><?php esc_html_e( 'Stream Database Update Required', 'stream' ) ?></strong></p>
296 - <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>
297 - <p><?php esc_html_e( 'This process could take a little while, so please be patient.', 'stream' ) ?></p>
298 - <?php submit_button( esc_html__( 'Update Database', 'stream' ), 'primary', 'stream-update-db-submit' ) ?>
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' ); ?>
299 251 </form>
300 252 </div>
301 253 <?php
302 254 }
@@ -312,12 +264,21 @@
312 264
313 265 $this->update_db_option();
314 266 ?>
315 267 <div class="updated">
316 - <form method="post" action="<?php echo esc_url( remove_query_arg( 'wp_stream_update' ) ) ?>" style="display:inline;">
317 - <p><strong><?php esc_html_e( 'Update Complete', 'stream' ) ?></strong></p>
318 - <p><?php printf( esc_html__( 'Your Stream database has been successfully updated from %1$s to %2$s!', 'stream' ), esc_html( $this->db_version ), esc_html( $this->plugin->get_version() ) ); ?></p>
319 - <?php submit_button( esc_html__( 'Continue', 'stream' ), 'secondary', false ) ?>
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 ); ?>
320 281 </form>
321 282 </div>
322 283 <?php
323 284 }
@@ -333,11 +294,11 @@
333 294 * @return array
334 295 */
335 296 public function db_update_versions() {
336 297 $db_update_versions = array(
337 - '3.0.0' /* @version 3.0.0 Drop the stream_context table, changes to stream table */,
338 - '3.0.2' /* @version 3.0.2 Fix uppercase values in stream table, connector column */,
339 - '3.0.8' /* @version 3.0.8 Increase size of user role IDs, user_roll column */,
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 */
340 301 );
341 302
342 303 /**
343 304 * Filter to alter the DB update versions array
@@ -351,17 +312,17 @@
351 312
352 313 /**
353 314 * Database user controlled update routine
354 315 *
355 - * @param int $db_version
356 - * @param int $current_version
357 - * @param array $update_args
316 + * @param int $db_version Next database version.
317 + * @param int $current_version Current database version.
318 + * @param array $update_args Update options.
358 319 *
359 320 * @return mixed Version number on success, true on no update needed, mysql error message on error
360 321 */
361 322 public function update( $db_version, $current_version, $update_args ) {
362 323 $versions = $this->db_update_versions();
363 - include_once( $this->plugin->locations['inc_dir'] . 'db-updates.php' );
324 + include_once $this->plugin->locations['inc_dir'] . 'db-updates.php';
364 325
365 326 foreach ( $versions as $version ) {
366 327 if ( ! isset( $update_args['type'] ) ) {
367 328 $update_args['type'] = 'user';
@@ -383,9 +344,9 @@
383 344
384 345 /**
385 346 * Initial database install routine
386 347 *
387 - * @param string $current_version
348 + * @param string $current_version Current database version.
388 349 *
389 350 * @return string
390 351 */
391 352 public function install( $current_version ) {