-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKvdb.php
1720 lines (1404 loc) · 59.2 KB
/
Kvdb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Kvvaradha - A Supportive files of $kvdb. Which helps to create tables with another database.
*
* * */
if ( file_exists(WP_CONTENT_DIR . '/install.php') )
require (WP_CONTENT_DIR . '/install.php');
require_once(ABSPATH . 'wp-admin/includes/admin.php');
require_once(ABSPATH . 'wp-admin/includes/schema.php');
if ( !function_exists('wp_install') ) :
function wp_install( $blog_title, $user_name, $user_email, $public, $deprecated = '', $user_password = '' ) {
if ( !empty( $deprecated ) )
_deprecated_argument( __FUNCTION__, '2.6' );
wp_check_mysql_version();
wp_cache_flush();
make_db_current_silent();
populate_options();
populate_roles();
update_option('blogname', $blog_title);
update_option('admin_email', $user_email);
update_option('blog_public', $public);
$guessurl = wp_guess_url();
update_option('siteurl', $guessurl);
if ( ! $public )
update_option('default_pingback_flag', 0);
$user_id = username_exists($user_name);
$user_password = trim($user_password);
$email_password = false;
if ( !$user_id && empty($user_password) ) {
$user_password = wp_generate_password( 12, false );
$message = __('<strong><em>Note that password</em></strong> carefully! It is a <em>random</em> password that was generated just for you.');
$user_id = wp_create_user($user_name, $user_password, $user_email);
update_user_option($user_id, 'default_password_nag', true, true);
$email_password = true;
} else if ( !$user_id ) {
// Password has been provided
$message = '<em>'.__('Your chosen password.').'</em>';
$user_id = wp_create_user($user_name, $user_password, $user_email);
} else {
$message = __('User already exists. Password inherited.');
}
$user = new WP_User($user_id);
$user->set_role('administrator');
wp_install_defaults($user_id);
flush_rewrite_rules();
wp_new_blog_notification($blog_title, $guessurl, $user_id, ($email_password ? $user_password : __('The password you chose during the install.') ) );
wp_cache_flush();
do_action( 'wp_install', $user );
return array('url' => $guessurl, 'user_id' => $user_id, 'password' => $user_password, 'password_message' => $message);
}
endif;
if ( !function_exists('wp_install_defaults') ) :
function wp_install_defaults( $user_id ) {
global $kvdb, $wp_rewrite, $table_prefix;
// Default category
$cat_name = __('Uncategorized');
/* translators: Default category slug */
$cat_slug = sanitize_title(_x('Uncategorized', 'Default category slug'));
if ( global_terms_enabled() ) {
$cat_id = $kvdb->get_var( $kvdb->prepare( "SELECT cat_ID FROM {$kvdb->sitecategories} WHERE category_nicename = %s", $cat_slug ) );
if ( $cat_id == null ) {
$kvdb->insert( $kvdb->sitecategories, array('cat_ID' => 0, 'cat_name' => $cat_name, 'category_nicename' => $cat_slug, 'last_updated' => current_time('mysql', true)) );
$cat_id = $kvdb->insert_id;
}
update_option('default_category', $cat_id);
} else {
$cat_id = 1;
}
$kvdb->insert( $kvdb->terms, array('term_id' => $cat_id, 'name' => $cat_name, 'slug' => $cat_slug, 'term_group' => 0) );
$kvdb->insert( $kvdb->term_taxonomy, array('term_id' => $cat_id, 'taxonomy' => 'category', 'description' => '', 'parent' => 0, 'count' => 1));
$cat_tt_id = $kvdb->insert_id;
// First post
$now = date('Y-m-d H:i:s');
$now_gmt = gmdate('Y-m-d H:i:s');
$first_post_guid = get_option('home') . '/?p=1';
if ( is_multisite() ) {
$first_post = get_site_option( 'first_post' );
if ( empty($first_post) )
$first_post = __( 'Welcome to <a href="SITE_URL">SITE_NAME</a>. This is your first post. Edit or delete it, then start blogging!' );
$first_post = str_replace( "SITE_URL", esc_url( network_home_url() ), $first_post );
$first_post = str_replace( "SITE_NAME", get_current_site()->site_name, $first_post );
} else {
$first_post = __('Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!');
}
$kvdb->insert( $kvdb->posts, array(
'post_author' => $user_id,
'post_date' => $now,
'post_date_gmt' => $now_gmt,
'post_content' => $first_post,
'post_excerpt' => '',
'post_title' => __('Hello world!'),
/* translators: Default post slug */
'post_name' => sanitize_title( _x('hello-world', 'Default post slug') ),
'post_modified' => $now,
'post_modified_gmt' => $now_gmt,
'guid' => $first_post_guid,
'comment_count' => 1,
'to_ping' => '',
'pinged' => '',
'post_content_filtered' => ''
));
$kvdb->insert( $kvdb->term_relationships, array('term_taxonomy_id' => $cat_tt_id, 'object_id' => 1) );
// Default comment
$first_comment_author = __('Mr WordPress');
$first_comment_url = 'https://wordpress.org/';
$first_comment = __('Hi, this is a comment.
To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.');
if ( is_multisite() ) {
$first_comment_author = get_site_option( 'first_comment_author', $first_comment_author );
$first_comment_url = get_site_option( 'first_comment_url', network_home_url() );
$first_comment = get_site_option( 'first_comment', $first_comment );
}
$kvdb->insert( $kvdb->comments, array(
'comment_post_ID' => 1,
'comment_author' => $first_comment_author,
'comment_author_email' => '',
'comment_author_url' => $first_comment_url,
'comment_date' => $now,
'comment_date_gmt' => $now_gmt,
'comment_content' => $first_comment
));
// First Page
$first_page = sprintf( __( "This is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:
<blockquote>Hi there! I'm a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin' caught in the rain.)</blockquote>
...or something like this:
<blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickeys to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</blockquote>
As a new WordPress user, you should go to <a href=\"%s\">your dashboard</a> to delete this page and create new pages for your content. Have fun!" ), admin_url() );
if ( is_multisite() )
$first_page = get_site_option( 'first_page', $first_page );
$first_post_guid = get_option('home') . '/?page_id=2';
$kvdb->insert( $kvdb->posts, array(
'post_author' => $user_id,
'post_date' => $now,
'post_date_gmt' => $now_gmt,
'post_content' => $first_page,
'post_excerpt' => '',
'post_title' => __( 'Sample Page' ),
/* translators: Default page slug */
'post_name' => __( 'sample-page' ),
'post_modified' => $now,
'post_modified_gmt' => $now_gmt,
'guid' => $first_post_guid,
'post_type' => 'page',
'to_ping' => '',
'pinged' => '',
'post_content_filtered' => ''
));
$kvdb->insert( $kvdb->postmeta, array( 'post_id' => 2, 'meta_key' => '_wp_page_template', 'meta_value' => 'default' ) );
// Set up default widgets for default theme.
update_option( 'widget_search', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
update_option( 'widget_recent-posts', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
update_option( 'widget_recent-comments', array ( 2 => array ( 'title' => '', 'number' => 5 ), '_multiwidget' => 1 ) );
update_option( 'widget_archives', array ( 2 => array ( 'title' => '', 'count' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
update_option( 'widget_categories', array ( 2 => array ( 'title' => '', 'count' => 0, 'hierarchical' => 0, 'dropdown' => 0 ), '_multiwidget' => 1 ) );
update_option( 'widget_meta', array ( 2 => array ( 'title' => '' ), '_multiwidget' => 1 ) );
update_option( 'sidebars_widgets', array ( 'wp_inactive_widgets' => array (), 'sidebar-1' => array ( 0 => 'search-2', 1 => 'recent-posts-2', 2 => 'recent-comments-2', 3 => 'archives-2', 4 => 'categories-2', 5 => 'meta-2', ), 'sidebar-2' => array (), 'sidebar-3' => array (), 'array_version' => 3 ) );
if ( ! is_multisite() )
update_user_meta( $user_id, 'show_welcome_panel', 1 );
elseif ( ! is_super_admin( $user_id ) && ! metadata_exists( 'user', $user_id, 'show_welcome_panel' ) )
update_user_meta( $user_id, 'show_welcome_panel', 2 );
if ( is_multisite() ) {
// Flush rules to pick up the new page.
$wp_rewrite->init();
$wp_rewrite->flush_rules();
$user = new WP_User($user_id);
$kvdb->update( $kvdb->options, array('option_value' => $user->user_email), array('option_name' => 'admin_email') );
// Remove all perms except for the login user.
$kvdb->query( $kvdb->prepare("DELETE FROM $kvdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'user_level') );
$kvdb->query( $kvdb->prepare("DELETE FROM $kvdb->usermeta WHERE user_id != %d AND meta_key = %s", $user_id, $table_prefix.'capabilities') );
// Delete any caps that snuck into the previously active blog. (Hardcoded to blog 1 for now.) TODO: Get previous_blog_id.
if ( !is_super_admin( $user_id ) && $user_id != 1 )
$kvdb->delete( $kvdb->usermeta, array( 'user_id' => $user_id , 'meta_key' => $kvdb->base_prefix.'1_capabilities' ) );
}
}
endif;
if ( !function_exists('wp_new_blog_notification') ) :
function wp_new_blog_notification($blog_title, $blog_url, $user_id, $password) {
$user = new WP_User( $user_id );
$email = $user->user_email;
$name = $user->user_login;
$message = sprintf(__("Your new WordPress site has been successfully set up at:
%1\$s
You can log in to the administrator account with the following information:
Username: %2\$s
Password: %3\$s
We hope you enjoy your new site. Thanks!
--The WordPress Team
https://wordpress.org/
"), $blog_url, $name, $password);
@wp_mail($email, __('New WordPress Site'), $message);
}
endif;
if ( !function_exists('wp_upgrade') ) :
function wp_upgrade() {
global $wp_current_db_version, $wp_db_version, $kvdb;
$wp_current_db_version = __get_option('db_version');
// We are up-to-date. Nothing to do.
if ( $wp_db_version == $wp_current_db_version )
return;
if ( ! is_blog_installed() )
return;
wp_check_mysql_version();
wp_cache_flush();
pre_schema_upgrade();
make_db_current_silent();
upgrade_all();
if ( is_multisite() && is_main_site() )
upgrade_network();
wp_cache_flush();
if ( is_multisite() ) {
if ( $kvdb->get_row( "SELECT blog_id FROM {$kvdb->blog_versions} WHERE blog_id = '{$kvdb->blogid}'" ) )
$kvdb->query( "UPDATE {$kvdb->blog_versions} SET db_version = '{$wp_db_version}' WHERE blog_id = '{$kvdb->blogid}'" );
else
$kvdb->query( "INSERT INTO {$kvdb->blog_versions} ( `blog_id` , `db_version` , `last_updated` ) VALUES ( '{$kvdb->blogid}', '{$wp_db_version}', NOW());" );
}
do_action( 'wp_upgrade', $wp_db_version, $wp_current_db_version );
}
endif;
function upgrade_all() {
global $wp_current_db_version, $wp_db_version;
$wp_current_db_version = __get_option('db_version');
// We are up-to-date. Nothing to do.
if ( $wp_db_version == $wp_current_db_version )
return;
// If the version is not set in the DB, try to guess the version.
if ( empty($wp_current_db_version) ) {
$wp_current_db_version = 0;
// If the template option exists, we have 1.5.
$template = __get_option('template');
if ( !empty($template) )
$wp_current_db_version = 2541;
}
if ( $wp_current_db_version < 6039 )
upgrade_230_options_table();
populate_options();
if ( $wp_current_db_version < 2541 ) {
upgrade_100();
upgrade_101();
upgrade_110();
upgrade_130();
}
if ( $wp_current_db_version < 3308 )
upgrade_160();
if ( $wp_current_db_version < 4772 )
upgrade_210();
if ( $wp_current_db_version < 4351 )
upgrade_old_slugs();
if ( $wp_current_db_version < 5539 )
upgrade_230();
if ( $wp_current_db_version < 6124 )
upgrade_230_old_tables();
if ( $wp_current_db_version < 7499 )
upgrade_250();
if ( $wp_current_db_version < 7935 )
upgrade_252();
if ( $wp_current_db_version < 8201 )
upgrade_260();
if ( $wp_current_db_version < 8989 )
upgrade_270();
if ( $wp_current_db_version < 10360 )
upgrade_280();
if ( $wp_current_db_version < 11958 )
upgrade_290();
if ( $wp_current_db_version < 15260 )
upgrade_300();
if ( $wp_current_db_version < 19389 )
upgrade_330();
if ( $wp_current_db_version < 20080 )
upgrade_340();
if ( $wp_current_db_version < 22422 )
upgrade_350();
if ( $wp_current_db_version < 25824 )
upgrade_370();
if ( $wp_current_db_version < 26148 )
upgrade_372();
if ( $wp_current_db_version < 26691 )
upgrade_380();
maybe_disable_link_manager();
maybe_disable_automattic_widgets();
update_option( 'db_version', $wp_db_version );
update_option( 'db_upgraded', true );
}
function upgrade_100() {
global $kvdb;
// Get the title and ID of every post, post_name to check if it already has a value
$posts = $kvdb->get_results("SELECT ID, post_title, post_name FROM $kvdb->posts WHERE post_name = ''");
if ($posts) {
foreach($posts as $post) {
if ('' == $post->post_name) {
$newtitle = sanitize_title($post->post_title);
$kvdb->query( $kvdb->prepare("UPDATE $kvdb->posts SET post_name = %s WHERE ID = %d", $newtitle, $post->ID) );
}
}
}
$categories = $kvdb->get_results("SELECT cat_ID, cat_name, category_nicename FROM $kvdb->categories");
foreach ($categories as $category) {
if ('' == $category->category_nicename) {
$newtitle = sanitize_title($category->cat_name);
$kvdb->update( $kvdb->categories, array('category_nicename' => $newtitle), array('cat_ID' => $category->cat_ID) );
}
}
$kvdb->query("UPDATE $kvdb->options SET option_value = REPLACE(option_value, 'wp-links/links-images/', 'wp-images/links/')
WHERE option_name LIKE 'links_rating_image%'
AND option_value LIKE 'wp-links/links-images/%'");
$done_ids = $kvdb->get_results("SELECT DISTINCT post_id FROM $kvdb->post2cat");
if ($done_ids) :
foreach ($done_ids as $done_id) :
$done_posts[] = $done_id->post_id;
endforeach;
$catwhere = ' AND ID NOT IN (' . implode(',', $done_posts) . ')';
else:
$catwhere = '';
endif;
$allposts = $kvdb->get_results("SELECT ID, post_category FROM $kvdb->posts WHERE post_category != '0' $catwhere");
if ($allposts) :
foreach ($allposts as $post) {
// Check to see if it's already been imported
$cat = $kvdb->get_row( $kvdb->prepare("SELECT * FROM $kvdb->post2cat WHERE post_id = %d AND category_id = %d", $post->ID, $post->post_category) );
if (!$cat && 0 != $post->post_category) { // If there's no result
$kvdb->insert( $kvdb->post2cat, array('post_id' => $post->ID, 'category_id' => $post->post_category) );
}
}
endif;
}
function upgrade_101() {
global $kvdb;
// Clean up indices, add a few
add_clean_index($kvdb->posts, 'post_name');
add_clean_index($kvdb->posts, 'post_status');
add_clean_index($kvdb->categories, 'category_nicename');
add_clean_index($kvdb->comments, 'comment_approved');
add_clean_index($kvdb->comments, 'comment_post_ID');
add_clean_index($kvdb->links , 'link_category');
add_clean_index($kvdb->links , 'link_visible');
}
function upgrade_110() {
global $kvdb;
// Set user_nicename.
$users = $kvdb->get_results("SELECT ID, user_nickname, user_nicename FROM $kvdb->users");
foreach ($users as $user) {
if ('' == $user->user_nicename) {
$newname = sanitize_title($user->user_nickname);
$kvdb->update( $kvdb->users, array('user_nicename' => $newname), array('ID' => $user->ID) );
}
}
$users = $kvdb->get_results("SELECT ID, user_pass from $kvdb->users");
foreach ($users as $row) {
if (!preg_match('/^[A-Fa-f0-9]{32}$/', $row->user_pass)) {
$kvdb->update( $kvdb->users, array('user_pass' => md5($row->user_pass)), array('ID' => $row->ID) );
}
}
// Get the GMT offset, we'll use that later on
$all_options = get_alloptions_110();
$time_difference = $all_options->time_difference;
$server_time = time()+date('Z');
$weblogger_time = $server_time + $time_difference * HOUR_IN_SECONDS;
$gmt_time = time();
$diff_gmt_server = ($gmt_time - $server_time) / HOUR_IN_SECONDS;
$diff_weblogger_server = ($weblogger_time - $server_time) / HOUR_IN_SECONDS;
$diff_gmt_weblogger = $diff_gmt_server - $diff_weblogger_server;
$gmt_offset = -$diff_gmt_weblogger;
// Add a gmt_offset option, with value $gmt_offset
add_option('gmt_offset', $gmt_offset);
$got_gmt_fields = ! ($kvdb->get_var("SELECT MAX(post_date_gmt) FROM $kvdb->posts") == '0000-00-00 00:00:00');
if (!$got_gmt_fields) {
// Add or subtract time to all dates, to get GMT dates
$add_hours = intval($diff_gmt_weblogger);
$add_minutes = intval(60 * ($diff_gmt_weblogger - $add_hours));
$kvdb->query("UPDATE $kvdb->posts SET post_date_gmt = DATE_ADD(post_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
$kvdb->query("UPDATE $kvdb->posts SET post_modified = post_date");
$kvdb->query("UPDATE $kvdb->posts SET post_modified_gmt = DATE_ADD(post_modified, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE) WHERE post_modified != '0000-00-00 00:00:00'");
$kvdb->query("UPDATE $kvdb->comments SET comment_date_gmt = DATE_ADD(comment_date, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
$kvdb->query("UPDATE $kvdb->users SET user_registered = DATE_ADD(user_registered, INTERVAL '$add_hours:$add_minutes' HOUR_MINUTE)");
}
}
function upgrade_130() {
global $kvdb;
$posts = $kvdb->get_results("SELECT ID, post_title, post_content, post_excerpt, guid, post_date, post_name, post_status, post_author FROM $kvdb->posts");
if ($posts) {
foreach($posts as $post) {
$post_content = addslashes(deslash($post->post_content));
$post_title = addslashes(deslash($post->post_title));
$post_excerpt = addslashes(deslash($post->post_excerpt));
if ( empty($post->guid) )
$guid = get_permalink($post->ID);
else
$guid = $post->guid;
$kvdb->update( $kvdb->posts, compact('post_title', 'post_content', 'post_excerpt', 'guid'), array('ID' => $post->ID) );
}
}
// Remove extraneous backslashes.
$comments = $kvdb->get_results("SELECT comment_ID, comment_author, comment_content FROM $kvdb->comments");
if ($comments) {
foreach($comments as $comment) {
$comment_content = deslash($comment->comment_content);
$comment_author = deslash($comment->comment_author);
$kvdb->update($kvdb->comments, compact('comment_content', 'comment_author'), array('comment_ID' => $comment->comment_ID) );
}
}
// Remove extraneous backslashes.
$links = $kvdb->get_results("SELECT link_id, link_name, link_description FROM $kvdb->links");
if ($links) {
foreach($links as $link) {
$link_name = deslash($link->link_name);
$link_description = deslash($link->link_description);
$kvdb->update( $kvdb->links, compact('link_name', 'link_description'), array('link_id' => $link->link_id) );
}
}
$active_plugins = __get_option('active_plugins');
// If plugins are not stored in an array, they're stored in the old
// newline separated format. Convert to new format.
if ( !is_array( $active_plugins ) ) {
$active_plugins = explode("\n", trim($active_plugins));
update_option('active_plugins', $active_plugins);
}
// Obsolete tables
$kvdb->query('DROP TABLE IF EXISTS ' . $kvdb->prefix . 'optionvalues');
$kvdb->query('DROP TABLE IF EXISTS ' . $kvdb->prefix . 'optiontypes');
$kvdb->query('DROP TABLE IF EXISTS ' . $kvdb->prefix . 'optiongroups');
$kvdb->query('DROP TABLE IF EXISTS ' . $kvdb->prefix . 'optiongroup_options');
// Update comments table to use comment_type
$kvdb->query("UPDATE $kvdb->comments SET comment_type='trackback', comment_content = REPLACE(comment_content, '<trackback />', '') WHERE comment_content LIKE '<trackback />%'");
$kvdb->query("UPDATE $kvdb->comments SET comment_type='pingback', comment_content = REPLACE(comment_content, '<pingback />', '') WHERE comment_content LIKE '<pingback />%'");
// Some versions have multiple duplicate option_name rows with the same values
$options = $kvdb->get_results("SELECT option_name, COUNT(option_name) AS dupes FROM `$kvdb->options` GROUP BY option_name");
foreach ( $options as $option ) {
if ( 1 != $option->dupes ) { // Could this be done in the query?
$limit = $option->dupes - 1;
$dupe_ids = $kvdb->get_col( $kvdb->prepare("SELECT option_id FROM $kvdb->options WHERE option_name = %s LIMIT %d", $option->option_name, $limit) );
if ( $dupe_ids ) {
$dupe_ids = join($dupe_ids, ',');
$kvdb->query("DELETE FROM $kvdb->options WHERE option_id IN ($dupe_ids)");
}
}
}
make_site_theme();
}
function upgrade_160() {
global $kvdb, $wp_current_db_version;
populate_roles_160();
$users = $kvdb->get_results("SELECT * FROM $kvdb->users");
foreach ( $users as $user ) :
if ( !empty( $user->user_firstname ) )
update_user_meta( $user->ID, 'first_name', wp_slash($user->user_firstname) );
if ( !empty( $user->user_lastname ) )
update_user_meta( $user->ID, 'last_name', wp_slash($user->user_lastname) );
if ( !empty( $user->user_nickname ) )
update_user_meta( $user->ID, 'nickname', wp_slash($user->user_nickname) );
if ( !empty( $user->user_level ) )
update_user_meta( $user->ID, $kvdb->prefix . 'user_level', $user->user_level );
if ( !empty( $user->user_icq ) )
update_user_meta( $user->ID, 'icq', wp_slash($user->user_icq) );
if ( !empty( $user->user_aim ) )
update_user_meta( $user->ID, 'aim', wp_slash($user->user_aim) );
if ( !empty( $user->user_msn ) )
update_user_meta( $user->ID, 'msn', wp_slash($user->user_msn) );
if ( !empty( $user->user_yim ) )
update_user_meta( $user->ID, 'yim', wp_slash($user->user_icq) );
if ( !empty( $user->user_description ) )
update_user_meta( $user->ID, 'description', wp_slash($user->user_description) );
if ( isset( $user->user_idmode ) ):
$idmode = $user->user_idmode;
if ($idmode == 'nickname') $id = $user->user_nickname;
if ($idmode == 'login') $id = $user->user_login;
if ($idmode == 'firstname') $id = $user->user_firstname;
if ($idmode == 'lastname') $id = $user->user_lastname;
if ($idmode == 'namefl') $id = $user->user_firstname.' '.$user->user_lastname;
if ($idmode == 'namelf') $id = $user->user_lastname.' '.$user->user_firstname;
if (!$idmode) $id = $user->user_nickname;
$kvdb->update( $kvdb->users, array('display_name' => $id), array('ID' => $user->ID) );
endif;
// FIXME: RESET_CAPS is temporary code to reset roles and caps if flag is set.
$caps = get_user_meta( $user->ID, $kvdb->prefix . 'capabilities');
if ( empty($caps) || defined('RESET_CAPS') ) {
$level = get_user_meta($user->ID, $kvdb->prefix . 'user_level', true);
$role = translate_level_to_role($level);
update_user_meta( $user->ID, $kvdb->prefix . 'capabilities', array($role => true) );
}
endforeach;
$old_user_fields = array( 'user_firstname', 'user_lastname', 'user_icq', 'user_aim', 'user_msn', 'user_yim', 'user_idmode', 'user_ip', 'user_domain', 'user_browser', 'user_description', 'user_nickname', 'user_level' );
$kvdb->hide_errors();
foreach ( $old_user_fields as $old )
$kvdb->query("ALTER TABLE $kvdb->users DROP $old");
$kvdb->show_errors();
// populate comment_count field of posts table
$comments = $kvdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $kvdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" );
if ( is_array( $comments ) )
foreach ($comments as $comment)
$kvdb->update( $kvdb->posts, array('comment_count' => $comment->c), array('ID' => $comment->comment_post_ID) );
// Some alpha versions used a post status of object instead of attachment and put
// the mime type in post_type instead of post_mime_type.
if ( $wp_current_db_version > 2541 && $wp_current_db_version <= 3091 ) {
$objects = $kvdb->get_results("SELECT ID, post_type FROM $kvdb->posts WHERE post_status = 'object'");
foreach ($objects as $object) {
$kvdb->update( $kvdb->posts, array( 'post_status' => 'attachment',
'post_mime_type' => $object->post_type,
'post_type' => ''),
array( 'ID' => $object->ID ) );
$meta = get_post_meta($object->ID, 'imagedata', true);
if ( ! empty($meta['file']) )
update_attached_file( $object->ID, $meta['file'] );
}
}
}
function upgrade_210() {
global $kvdb, $wp_current_db_version;
if ( $wp_current_db_version < 3506 ) {
// Update status and type.
$posts = $kvdb->get_results("SELECT ID, post_status FROM $kvdb->posts");
if ( ! empty($posts) ) foreach ($posts as $post) {
$status = $post->post_status;
$type = 'post';
if ( 'static' == $status ) {
$status = 'publish';
$type = 'page';
} else if ( 'attachment' == $status ) {
$status = 'inherit';
$type = 'attachment';
}
$kvdb->query( $kvdb->prepare("UPDATE $kvdb->posts SET post_status = %s, post_type = %s WHERE ID = %d", $status, $type, $post->ID) );
}
}
if ( $wp_current_db_version < 3845 ) {
populate_roles_210();
}
if ( $wp_current_db_version < 3531 ) {
// Give future posts a post_status of future.
$now = gmdate('Y-m-d H:i:59');
$kvdb->query ("UPDATE $kvdb->posts SET post_status = 'future' WHERE post_status = 'publish' AND post_date_gmt > '$now'");
$posts = $kvdb->get_results("SELECT ID, post_date FROM $kvdb->posts WHERE post_status ='future'");
if ( !empty($posts) )
foreach ( $posts as $post )
wp_schedule_single_event(mysql2date('U', $post->post_date, false), 'publish_future_post', array($post->ID));
}
}
function upgrade_230() {
global $wp_current_db_version, $kvdb;
if ( $wp_current_db_version < 5200 ) {
populate_roles_230();
}
// Convert categories to terms.
$tt_ids = array();
$have_tags = false;
$categories = $kvdb->get_results("SELECT * FROM $kvdb->categories ORDER BY cat_ID");
foreach ($categories as $category) {
$term_id = (int) $category->cat_ID;
$name = $category->cat_name;
$description = $category->category_description;
$slug = $category->category_nicename;
$parent = $category->category_parent;
$term_group = 0;
// Associate terms with the same slug in a term group and make slugs unique.
if ( $exists = $kvdb->get_results( $kvdb->prepare("SELECT term_id, term_group FROM $kvdb->terms WHERE slug = %s", $slug) ) ) {
$term_group = $exists[0]->term_group;
$id = $exists[0]->term_id;
$num = 2;
do {
$alt_slug = $slug . "-$num";
$num++;
$slug_check = $kvdb->get_var( $kvdb->prepare("SELECT slug FROM $kvdb->terms WHERE slug = %s", $alt_slug) );
} while ( $slug_check );
$slug = $alt_slug;
if ( empty( $term_group ) ) {
$term_group = $kvdb->get_var("SELECT MAX(term_group) FROM $kvdb->terms GROUP BY term_group") + 1;
$kvdb->query( $kvdb->prepare("UPDATE $kvdb->terms SET term_group = %d WHERE term_id = %d", $term_group, $id) );
}
}
$kvdb->query( $kvdb->prepare("INSERT INTO $kvdb->terms (term_id, name, slug, term_group) VALUES
(%d, %s, %s, %d)", $term_id, $name, $slug, $term_group) );
$count = 0;
if ( !empty($category->category_count) ) {
$count = (int) $category->category_count;
$taxonomy = 'category';
$kvdb->query( $kvdb->prepare("INSERT INTO $kvdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
$tt_ids[$term_id][$taxonomy] = (int) $kvdb->insert_id;
}
if ( !empty($category->link_count) ) {
$count = (int) $category->link_count;
$taxonomy = 'link_category';
$kvdb->query( $kvdb->prepare("INSERT INTO $kvdb->term_taxonomy (term_id, taxonomy, description, parent, count) VALUES ( %d, %s, %s, %d, %d)", $term_id, $taxonomy, $description, $parent, $count) );
$tt_ids[$term_id][$taxonomy] = (int) $kvdb->insert_id;
}
if ( !empty($category->tag_count) ) {
$have_tags = true;
$count = (int) $category->tag_count;
$taxonomy = 'post_tag';
$kvdb->insert( $kvdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') );
$tt_ids[$term_id][$taxonomy] = (int) $kvdb->insert_id;
}
if ( empty($count) ) {
$count = 0;
$taxonomy = 'category';
$kvdb->insert( $kvdb->term_taxonomy, compact('term_id', 'taxonomy', 'description', 'parent', 'count') );
$tt_ids[$term_id][$taxonomy] = (int) $kvdb->insert_id;
}
}
$select = 'post_id, category_id';
if ( $have_tags )
$select .= ', rel_type';
$posts = $kvdb->get_results("SELECT $select FROM $kvdb->post2cat GROUP BY post_id, category_id");
foreach ( $posts as $post ) {
$post_id = (int) $post->post_id;
$term_id = (int) $post->category_id;
$taxonomy = 'category';
if ( !empty($post->rel_type) && 'tag' == $post->rel_type)
$taxonomy = 'tag';
$tt_id = $tt_ids[$term_id][$taxonomy];
if ( empty($tt_id) )
continue;
$kvdb->insert( $kvdb->term_relationships, array('object_id' => $post_id, 'term_taxonomy_id' => $tt_id) );
}
if ( $wp_current_db_version < 3570 ) {
$link_cat_id_map = array();
$default_link_cat = 0;
$tt_ids = array();
$link_cats = $kvdb->get_results("SELECT cat_id, cat_name FROM " . $kvdb->prefix . 'linkcategories');
foreach ( $link_cats as $category) {
$cat_id = (int) $category->cat_id;
$term_id = 0;
$name = wp_slash($category->cat_name);
$slug = sanitize_title($name);
$term_group = 0;
// Associate terms with the same slug in a term group and make slugs unique.
if ( $exists = $kvdb->get_results( $kvdb->prepare("SELECT term_id, term_group FROM $kvdb->terms WHERE slug = %s", $slug) ) ) {
$term_group = $exists[0]->term_group;
$term_id = $exists[0]->term_id;
}
if ( empty($term_id) ) {
$kvdb->insert( $kvdb->terms, compact('name', 'slug', 'term_group') );
$term_id = (int) $kvdb->insert_id;
}
$link_cat_id_map[$cat_id] = $term_id;
$default_link_cat = $term_id;
$kvdb->insert( $kvdb->term_taxonomy, array('term_id' => $term_id, 'taxonomy' => 'link_category', 'description' => '', 'parent' => 0, 'count' => 0) );
$tt_ids[$term_id] = (int) $kvdb->insert_id;
}
// Associate links to cats.
$links = $kvdb->get_results("SELECT link_id, link_category FROM $kvdb->links");
if ( !empty($links) ) foreach ( $links as $link ) {
if ( 0 == $link->link_category )
continue;
if ( ! isset($link_cat_id_map[$link->link_category]) )
continue;
$term_id = $link_cat_id_map[$link->link_category];
$tt_id = $tt_ids[$term_id];
if ( empty($tt_id) )
continue;
$kvdb->insert( $kvdb->term_relationships, array('object_id' => $link->link_id, 'term_taxonomy_id' => $tt_id) );
}
// Set default to the last category we grabbed during the upgrade loop.
update_option('default_link_category', $default_link_cat);
} else {
$links = $kvdb->get_results("SELECT link_id, category_id FROM $kvdb->link2cat GROUP BY link_id, category_id");
foreach ( $links as $link ) {
$link_id = (int) $link->link_id;
$term_id = (int) $link->category_id;
$taxonomy = 'link_category';
$tt_id = $tt_ids[$term_id][$taxonomy];
if ( empty($tt_id) )
continue;
$kvdb->insert( $kvdb->term_relationships, array('object_id' => $link_id, 'term_taxonomy_id' => $tt_id) );
}
}
if ( $wp_current_db_version < 4772 ) {
// Obsolete linkcategories table
$kvdb->query('DROP TABLE IF EXISTS ' . $kvdb->prefix . 'linkcategories');
}
$terms = $kvdb->get_results("SELECT term_taxonomy_id, taxonomy FROM $kvdb->term_taxonomy");
foreach ( (array) $terms as $term ) {
if ( ('post_tag' == $term->taxonomy) || ('category' == $term->taxonomy) )
$count = $kvdb->get_var( $kvdb->prepare("SELECT COUNT(*) FROM $kvdb->term_relationships, $kvdb->posts WHERE $kvdb->posts.ID = $kvdb->term_relationships.object_id AND post_status = 'publish' AND post_type = 'post' AND term_taxonomy_id = %d", $term->term_taxonomy_id) );
else
$count = $kvdb->get_var( $kvdb->prepare("SELECT COUNT(*) FROM $kvdb->term_relationships WHERE term_taxonomy_id = %d", $term->term_taxonomy_id) );
$kvdb->update( $kvdb->term_taxonomy, array('count' => $count), array('term_taxonomy_id' => $term->term_taxonomy_id) );
}
}
function upgrade_230_options_table() {
global $kvdb;
$old_options_fields = array( 'option_can_override', 'option_type', 'option_width', 'option_height', 'option_description', 'option_admin_level' );
$kvdb->hide_errors();
foreach ( $old_options_fields as $old )
$kvdb->query("ALTER TABLE $kvdb->options DROP $old");
$kvdb->show_errors();
}
function upgrade_230_old_tables() {
global $kvdb;
$kvdb->query('DROP TABLE IF EXISTS ' . $kvdb->prefix . 'categories');
$kvdb->query('DROP TABLE IF EXISTS ' . $kvdb->prefix . 'link2cat');
$kvdb->query('DROP TABLE IF EXISTS ' . $kvdb->prefix . 'post2cat');
}
function upgrade_old_slugs() {
// upgrade people who were using the Redirect Old Slugs plugin
global $kvdb;
$kvdb->query("UPDATE $kvdb->postmeta SET meta_key = '_wp_old_slug' WHERE meta_key = 'old_slug'");
}
function upgrade_250() {
global $wp_current_db_version;
if ( $wp_current_db_version < 6689 ) {
populate_roles_250();
}
}
function upgrade_252() {
global $kvdb;
$kvdb->query("UPDATE $kvdb->users SET user_activation_key = ''");
}
function upgrade_260() {
global $wp_current_db_version;
if ( $wp_current_db_version < 8000 )
populate_roles_260();
}
function upgrade_270() {
global $kvdb, $wp_current_db_version;
if ( $wp_current_db_version < 8980 )
populate_roles_270();
// Update post_date for unpublished posts with empty timestamp
if ( $wp_current_db_version < 8921 )
$kvdb->query( "UPDATE $kvdb->posts SET post_date = post_modified WHERE post_date = '0000-00-00 00:00:00'" );
}
function upgrade_280() {
global $wp_current_db_version, $kvdb;
if ( $wp_current_db_version < 10360 )
populate_roles_280();
if ( is_multisite() ) {
$start = 0;
while( $rows = $kvdb->get_results( "SELECT option_name, option_value FROM $kvdb->options ORDER BY option_id LIMIT $start, 20" ) ) {
foreach( $rows as $row ) {
$value = $row->option_value;
if ( !@unserialize( $value ) )
$value = stripslashes( $value );
if ( $value !== $row->option_value ) {
update_option( $row->option_name, $value );
}
}
$start += 20;
}
refresh_blog_details( $kvdb->blogid );
}
}
function upgrade_290() {
global $wp_current_db_version;
if ( $wp_current_db_version < 11958 ) {
// Previously, setting depth to 1 would redundantly disable threading, but now 2 is the minimum depth to avoid confusion
if ( get_option( 'thread_comments_depth' ) == '1' ) {
update_option( 'thread_comments_depth', 2 );
update_option( 'thread_comments', 0 );
}
}
}
function upgrade_300() {
global $wp_current_db_version, $kvdb;
if ( $wp_current_db_version < 15093 )
populate_roles_300();
if ( $wp_current_db_version < 14139 && is_multisite() && is_main_site() && ! defined( 'MULTISITE' ) && get_site_option( 'siteurl' ) === false )
add_site_option( 'siteurl', '' );
// 3.0 screen options key name changes.
if ( is_main_site() && !defined('DO_NOT_UPGRADE_GLOBAL_TABLES') ) {
$prefix = like_escape($kvdb->base_prefix);
$kvdb->query( "DELETE FROM $kvdb->usermeta WHERE meta_key LIKE '{$prefix}%meta-box-hidden%' OR meta_key LIKE '{$prefix}%closedpostboxes%' OR meta_key LIKE '{$prefix}%manage-%-columns-hidden%' OR meta_key LIKE '{$prefix}%meta-box-order%' OR meta_key LIKE '{$prefix}%metaboxorder%' OR meta_key LIKE '{$prefix}%screen_layout%'
OR meta_key = 'manageedittagscolumnshidden' OR meta_key='managecategoriescolumnshidden' OR meta_key = 'manageedit-tagscolumnshidden' OR meta_key = 'manageeditcolumnshidden' OR meta_key = 'categories_per_page' OR meta_key = 'edit_tags_per_page'" );
}
}
function upgrade_330() {
global $wp_current_db_version, $kvdb, $wp_registered_widgets, $sidebars_widgets;
if ( $wp_current_db_version < 19061 && is_main_site() && ! defined( 'DO_NOT_UPGRADE_GLOBAL_TABLES' ) ) {
$kvdb->query( "DELETE FROM $kvdb->usermeta WHERE meta_key IN ('show_admin_bar_admin', 'plugins_last_view')" );
}
if ( $wp_current_db_version >= 11548 )
return;
$sidebars_widgets = get_option( 'sidebars_widgets', array() );
$_sidebars_widgets = array();
if ( isset($sidebars_widgets['wp_inactive_widgets']) || empty($sidebars_widgets) )
$sidebars_widgets['array_version'] = 3;
elseif ( !isset($sidebars_widgets['array_version']) )
$sidebars_widgets['array_version'] = 1;
switch ( $sidebars_widgets['array_version'] ) {
case 1 :
foreach ( (array) $sidebars_widgets as $index => $sidebar )
if ( is_array($sidebar) )
foreach ( (array) $sidebar as $i => $name ) {
$id = strtolower($name);
if ( isset($wp_registered_widgets[$id]) ) {
$_sidebars_widgets[$index][$i] = $id;
continue;
}
$id = sanitize_title($name);
if ( isset($wp_registered_widgets[$id]) ) {
$_sidebars_widgets[$index][$i] = $id;
continue;
}
$found = false;
foreach ( $wp_registered_widgets as $widget_id => $widget ) {
if ( strtolower($widget['name']) == strtolower($name) ) {
$_sidebars_widgets[$index][$i] = $widget['id'];
$found = true;
break;
} elseif ( sanitize_title($widget['name']) == sanitize_title($name) ) {
$_sidebars_widgets[$index][$i] = $widget['id'];
$found = true;
break;
}
}
if ( $found )
continue;