PluginProbe ʕ •ᴥ•ʔ
MainWP Child Reports / 1.7
MainWP Child Reports v1.7
0.0.1 1.0 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9.1 1.9.2 1.9.3 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1 2.1.1 2.2 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.3 2.3.1 trunk
mainwp-child-reports / includes / install.php
mainwp-child-reports / includes Last commit date
vendor 10 years ago admin.php 9 years ago class-wp-stream-author.php 10 years ago connector.php 9 years ago connectors.php 9 years ago context-query.php 10 years ago dashboard.php 10 years ago date-interval.php 10 years ago db.php 9 years ago filter-input.php 10 years ago functions.php 10 years ago install.php 9 years ago list-table.php 9 years ago live-update.php 9 years ago log.php 10 years ago network.php 10 years ago query.php 9 years ago settings.php 9 years ago
install.php
382 lines
1 <?php
2
3 class MainWP_WP_Stream_Install {
4
5 const KEY = 'mainwp_child_reports_db';
6
7 public static $table_prefix;
8
9 public static $db_version;
10
11 public static $current;
12
13 public static $update_versions;
14
15 public static $update_required = false;
16
17 public static $success_db;
18
19 private static $instance = false;
20
21 public static $import_connectors;
22
23 public static function get_instance() {
24 if ( empty( self::$instance ) ) {
25 self::$instance = new self();
26 }
27
28 return self::$instance;
29 }
30
31 function __construct() {
32 global $wpdb;
33
34 self::$current = MainWP_WP_Stream::VERSION;
35 self::$db_version = self::get_db_version();
36
37 $prefix = $wpdb->base_prefix;
38
39 self::$table_prefix = apply_filters( 'mainwp_wp_stream_db_tables_prefix', $prefix );
40 self::$import_connectors = array('comment', 'editor', 'installer', 'media', 'menus', 'posts', 'users', 'widgets');
41 self::check();
42 }
43
44 private static function check() {
45 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
46 return;
47 }
48
49 global $wpdb;
50
51 if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $wpdb->prefix . "mainwp_stream'" ) !== $wpdb->prefix . "mainwp_stream" ||
52 $wpdb->get_var( "SHOW TABLES LIKE '" . $wpdb->prefix . "mainwp_stream_meta'" ) !== $wpdb->prefix . "mainwp_stream_meta"
53 ) {
54 self::$db_version = false;
55 } else {
56 if (false === get_option('mainwp_creport_first_time_activated')) {
57 $sql = "SELECT MIN( created ) AS first_time " .
58 "FROM {$wpdb->prefix}mainwp_stream " .
59 "WHERE created != '0000-00-00 00:00:00'";
60 $result = $wpdb->get_results( $sql, ARRAY_A );
61 $time = time();
62 if (isset($result[0]) && !empty($result[0]['first_time'])) {
63 $time = strtotime( $result[0]['first_time'] );
64 }
65 update_option('mainwp_creport_first_time_activated', $time);
66 }
67 }
68
69 if ( empty( self::$db_version ) ) {
70 self::install( self::$current );
71 self::copy_stream_db();
72 } elseif ( version_compare( self::$db_version, self::$current, '!=') ) {
73 self::check_updates();
74 update_site_option( self::KEY, self::$current );
75 }
76
77 if ('yes' == get_option('mainwp_child_reports_check_to_copy_data', false)) {
78 add_action( 'all_admin_notices', array( __CLASS__, 'update_notice_hook' ) );
79 }
80 }
81
82 public static function check_to_copy_data() {
83 $stream_db_version = get_site_option( 'wp_stream_db' ); // store db version of the plugin stream 1.4.9
84 update_option('mainwp_child_reports_check_to_copy_data', 'yes');
85 return;
86 }
87
88 public static function copy_stream_db() {
89 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
90 return;
91 }
92 $stream_db_version = get_site_option( 'wp_stream_db' ); // store db version of the plugin stream 1.4.9
93 if (version_compare($stream_db_version, '1.4.9', '='))
94 self::copy_stream_149_db();
95 else if (version_compare($stream_db_version, '3.0' , '>=')) {
96 self::copy_stream_300_db();
97 }
98 update_option('mainwp_child_reports_copied_data_ok', 'yes');
99 update_option('mainwp_child_reports_check_to_copy_data', '');
100 }
101
102 public static function copy_stream_149_db() {
103 global $wpdb;
104 if ( is_multisite() ) {
105 return;
106 }
107 if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $wpdb->prefix . "stream'" ) !== $wpdb->prefix . "stream" )
108 return;
109 if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $wpdb->prefix . "stream_context'" ) !== $wpdb->prefix . "stream_context" )
110 return;
111 if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $wpdb->prefix . "stream_meta'" ) !== $wpdb->prefix . "stream_meta" )
112 return;
113
114 $sql = "SELECT * FROM {$wpdb->prefix}stream";
115
116 $blog_stream = $wpdb->get_results( $sql, ARRAY_A );
117
118 foreach ( $blog_stream as $key => $stream_entry ) {
119 $prev_entry_id = $stream_entry['ID'];
120
121 unset( $stream_entry['ID'] );
122
123 $wpdb->insert( $wpdb->prefix . 'mainwp_stream', $stream_entry );
124 $stream_entry_id = $wpdb->insert_id;
125
126 $sql = "SELECT * FROM {$wpdb->prefix}stream_context WHERE record_id = $prev_entry_id";
127
128 $blog_stream_context = $wpdb->get_results( $sql, ARRAY_A );
129
130 foreach ( $blog_stream_context as $key => $stream_context ) {
131 unset( $stream_context['meta_id'] );
132 $stream_context['record_id'] = $stream_entry_id;
133
134 $wpdb->insert( $wpdb->prefix . 'mainwp_stream_context', $stream_context );
135 }
136
137 $sql = "SELECT * FROM {$wpdb->prefix}stream_meta WHERE record_id = $prev_entry_id";
138
139 $blog_stream_meta = $wpdb->get_results( $sql, ARRAY_A );
140
141 foreach ( $blog_stream_meta as $key => $stream_meta ) {
142 unset( $stream_meta['meta_id'] );
143 $stream_meta['record_id'] = $stream_entry_id;
144
145 $wpdb->insert( $wpdb->prefix . 'mainwp_stream_meta', $stream_meta );
146 }
147 }
148
149 }
150
151 public static function copy_stream_300_db() {
152 global $wpdb;
153 if ( is_multisite() ) {
154 return;
155 }
156 if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $wpdb->prefix . "stream'" ) !== $wpdb->prefix . "stream" )
157 return;
158 if ( $wpdb->get_var( "SHOW TABLES LIKE '" . $wpdb->prefix . "stream_meta'" ) !== $wpdb->prefix . "stream_meta" )
159 return;
160
161 $timeout = 20 * 60 * 60; /*20 minutes*/
162 $mem = '512M';
163 // @codingStandardsIgnoreStart
164 @ini_set( 'memory_limit', $mem );
165 @set_time_limit( $timeout );
166 @ini_set( 'max_execution_time', $timeout );
167
168 $sql = "SELECT * FROM {$wpdb->prefix}stream";
169
170 $blog_stream = $wpdb->get_results( $sql, ARRAY_A );
171 $printed_connector = array();
172
173 foreach ( $blog_stream as $key => $stream_entry ) {
174
175 if (!in_array($stream_entry['connector'], self::$import_connectors)) {
176 continue;
177 }
178
179 if ('users' == $stream_entry['connector'] && 'login' == $stream_entry['action']) {
180 continue;
181 }
182
183 $prev_entry_id = $stream_entry['ID'];
184
185 $insert_entry = array(
186 'site_id' => $stream_entry['site_id'],
187 'blog_id' => $stream_entry['blog_id'],
188 'object_id' => $stream_entry['object_id'],
189 'author' => $stream_entry['user_id'],
190 'author_role' => $stream_entry['user_role'],
191 'author_role' => $stream_entry['user_role'],
192 'summary' => $stream_entry['summary'],
193 'visibility' => 'publish',
194 'parent' => 0,
195 'type' => 'stream',
196 'created' => $stream_entry['created'],
197 'ip' => $stream_entry['ip']
198 );
199
200 $wpdb->insert( $wpdb->prefix . 'mainwp_stream', $insert_entry );
201 $stream_entry_id = $wpdb->insert_id;
202
203 $insert_context = array(
204 'record_id' => $stream_entry_id,
205 'context' => $stream_entry['context'],
206 'action' => $stream_entry['action'],
207 'connector' => $stream_entry['connector']
208 );
209 $wpdb->insert( $wpdb->prefix . 'mainwp_stream_context', $insert_context );
210
211 $sql = "SELECT * FROM {$wpdb->prefix}stream_meta WHERE record_id = $prev_entry_id";
212
213 $blog_stream_meta = $wpdb->get_results( $sql, ARRAY_A );
214
215 foreach ( $blog_stream_meta as $key => $stream_meta ) {
216 unset( $stream_meta['meta_id'] );
217 $stream_meta['record_id'] = $stream_entry_id;
218 $wpdb->insert( $wpdb->prefix . 'mainwp_stream_meta', $stream_meta );
219 }
220 }
221
222 }
223
224 public static function update_notice_hook() {
225 // if ( ! current_user_can( WP_Stream_Admin::VIEW_CAP ) ) {
226 // return;
227 // }
228
229 if ( ! isset( $_REQUEST['mainwp_wp_stream_update'] ) ) {
230 self::prompt_copy_data();
231 } else {
232 check_admin_referer( 'mainwp_wp_stream_update_db' );
233 if ( isset( $_REQUEST['mainwp_reports_copy_db_submit'] ) ) {
234 self::copy_stream_db();
235
236 } else if ( isset( $_REQUEST['mainwp_reports_continue_submit'] ) ) {
237 update_option('mainwp_child_reports_check_to_copy_data', '');
238 }
239 }
240
241 if ('yes' == get_option('mainwp_child_reports_copied_data_ok')) {
242 self::prompt_copy_data_status();
243 }
244 }
245
246 public static function prompt_copy_data() {
247 ?>
248 <div class="updated">
249 <form method="post" action="<?php echo esc_url( remove_query_arg( 'message' ) ) ?>">
250 <?php wp_nonce_field( 'mainwp_wp_stream_update_db' ) ?>
251 <input type="hidden" name="mainwp_wp_stream_update" value="not_update_and_continue"/>
252 <p><strong><?php esc_html_e( 'Do you want to import logs from the Stream plugin?', 'mainwp-child-reports' ) ?></strong></p>
253 <p class="submit">
254 <?php submit_button( esc_html__( 'Yes', 'mainwp-child-reports' ), 'primary', 'mainwp_reports_copy_db_submit', false ) ?>
255 <?php submit_button( esc_html__( 'No', 'mainwp-child-reports' ), 'primary', 'mainwp_reports_continue_submit', false ) ?>
256 </p
257 </form>
258 </div>
259 <?php
260 }
261
262 public static function prompt_copy_data_status() {
263 printf( '<div class="updated"><p>%s</p></div>', __( 'Logs have been successfully imported.', 'mainwp-child-reports' ) );
264 delete_option('mainwp_child_reports_copied_data_ok');
265 }
266
267 public static function get_db_version() {
268
269 $version = get_site_option( self::KEY );
270
271 return $version;
272 }
273
274 public static function install( $current ) {
275 global $wpdb;
276
277 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
278
279 $prefix = self::$table_prefix;
280
281 $sql = "CREATE TABLE {$prefix}mainwp_stream (
282 ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
283 site_id bigint(20) unsigned NOT NULL DEFAULT '1',
284 blog_id bigint(20) unsigned NOT NULL DEFAULT '0',
285 object_id bigint(20) unsigned NULL,
286 author bigint(20) unsigned NOT NULL DEFAULT '0',
287 author_role varchar(20) NOT NULL DEFAULT '',
288 summary longtext NOT NULL,
289 visibility varchar(20) NOT NULL DEFAULT 'publish',
290 parent bigint(20) unsigned NOT NULL DEFAULT '0',
291 type varchar(20) NOT NULL DEFAULT 'stream',
292 created datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
293 ip varchar(39) NULL,
294 PRIMARY KEY (ID),
295 KEY site_id (site_id),
296 KEY blog_id (blog_id),
297 KEY parent (parent),
298 KEY author (author),
299 KEY created (created)
300 )";
301
302 if ( ! empty( $wpdb->charset ) ) {
303 $sql .= " CHARACTER SET $wpdb->charset";
304 }
305
306 if ( ! empty( $wpdb->collate ) ) {
307 $sql .= " COLLATE $wpdb->collate";
308 }
309
310 $sql .= ';';
311
312 dbDelta( $sql );
313
314 $sql = "CREATE TABLE {$prefix}mainwp_stream_context (
315 meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
316 record_id bigint(20) unsigned NOT NULL,
317 context varchar(100) NOT NULL,
318 action varchar(100) NOT NULL,
319 connector varchar(100) NOT NULL,
320 PRIMARY KEY (meta_id),
321 KEY context (context),
322 KEY action (action),
323 KEY connector (connector)
324 )";
325
326 if ( ! empty( $wpdb->charset ) ) {
327 $sql .= " CHARACTER SET $wpdb->charset";
328 }
329
330 if ( ! empty( $wpdb->collate ) ) {
331 $sql .= " COLLATE $wpdb->collate";
332 }
333
334 $sql .= ';';
335
336 dbDelta( $sql );
337
338 $sql = "CREATE TABLE {$prefix}mainwp_stream_meta (
339 meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
340 record_id bigint(20) unsigned NOT NULL,
341 meta_key varchar(200) NOT NULL,
342 meta_value text NOT NULL,
343 PRIMARY KEY (meta_id),
344 KEY record_id (record_id),
345 KEY meta_key (meta_key(100))
346 )";
347
348 if ( ! empty( $wpdb->charset ) ) {
349 $sql .= " CHARACTER SET $wpdb->charset";
350 }
351
352 if ( ! empty( $wpdb->collate ) ) {
353 $sql .= " COLLATE $wpdb->collate";
354 }
355
356 $sql .= ';';
357
358 dbDelta( $sql );
359
360 update_site_option( self::KEY, self::$current );
361
362 return $current;
363 }
364
365 public static function check_updates() {
366 global $wpdb;
367
368 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
369 $current_version = self::get_db_version();
370
371 $prefix = self::$table_prefix;
372
373 if (version_compare($current_version, '0.0.4', '<')) {
374 $wpdb->query( "ALTER TABLE {$prefix}mainwp_stream_meta CHANGE `meta_value` `meta_value` TEXT " . ( !empty($wpdb->charset) ? "CHARACTER SET " . $wpdb->charset : "" ) . ( !empty($wpdb->collate) ? " COLLATE " . $wpdb->collate : "" ) . " NOT NULL;");
375 if ( $wpdb->get_var( "SHOW INDEX FROM {$prefix}mainwp_stream_meta WHERE column_name = 'meta_value'")) {
376 $wpdb->query( "ALTER TABLE {$prefix}mainwp_stream_meta DROP INDEX meta_value");
377 }
378 }
379
380 }
381 }
382