DPSP-Platform /code/cake/app/webroot/cp/wp-content/plugins/commentpress-core/commentpress-core/class_commentpress_db.php

Language PHP Lines 4316
MD5 Hash 21ee3acc0aca0c858965c056b799cec3
Repository https://github.com/DigitalPaulScholtenProject/DPSP-Platform.git View Raw File View Project SPDX
   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
<?php /*
================================================================================
Class CommentpressCoreDatabase
================================================================================
AUTHOR: Christian Wach <needle@haystack.co.uk>
--------------------------------------------------------------------------------
NOTES
=====

This class is a wrapper for the majority of database operations.

--------------------------------------------------------------------------------
*/






/*
================================================================================
Class Name
================================================================================
*/

class CommentpressCoreDatabase {






	/*
	============================================================================
	Properties
	============================================================================
	*/
	
	// parent object reference
	var $parent_obj;
	
	// standard mobile browser
	var $is_mobile = false;
	
	// touch-based mobile browser
	var $is_mobile_touch = false;
	
	// touch-based tablet browser
	var $is_tablet = false;
	
	// ---------- options ----------
	var $commentpress_options = array();
	
	// TOC content ('post' or 'page')
	var $toc_content = 'page';
	
	// TOC chapters are pages by default
	var $toc_chapter_is_page = 1;

	// Show extended TOC content for posts lists
	var $show_extended_toc = 1;
		
	// TOC shows subpages by default
	var $show_subpages = 1;
	
	// show page titles by default
	var $title_visibility = 'show';
	
	// hide page meta by default
	var $page_meta_visibility = 'hide';
	
	// default editor (tinyMCE)
	var $comment_editor = 1;
	
	// promote reading (1) or commenting (0)
	var $promote_reading = 0;
	
	// default excerpt length
	var $excerpt_length = 55;
	
	// default header background colour (hex, same as in layout.css)
	var $header_bg_colour = '2c2622';
	
	// default scroll speed (ms)
	var $js_scroll_speed = '800';
	
	// default type of blog - eg, array('0' => 'Poetry','1' => 'Prose')
	var $blog_type = false;
		
	// default blog workflow (like translation, for example), off by default
	var $blog_workflow = 0;
	
	// default sidebar ('toc' => Contents tab)
	var $sidebar_default = 'toc';
		
	// default minimum page width (px)
	var $min_page_width = '447';
		
	// "live" comment refreshing off by default
	var $para_comments_live = 0;
		
	// prevent save_post hook firing more than once
	var $saved_post = false;
	






	/** 
	 * @description: initialises this object
	 * @param object $parent_obj a reference to the parent object
	 * @return object
	 * @todo: 
	 *
	 */
	function __construct( $parent_obj ) {
	
		// store reference to parent
		$this->parent_obj = $parent_obj;
	
		// init
		$this->_init();

		// --<
		return $this;

	}






	/**
	 * @description: PHP 4 constructor
	 */
	function CommentpressCoreDatabase( $parent_obj ) {
		
		// is this php5?
		if ( version_compare( PHP_VERSION, "5.0.0", "<" ) ) {
		
			// call php5 constructor
			$this->__construct( $parent_obj );
			
		}
		
		// --<
		return $this;

	}






	/** 
	 * @description: set up all items associated with this object
	 * @todo: 
	 *
	 */
	function activate() {
		
		// have we already got a modified database?
		$modified = $this->db_is_modified( 'comment_text_signature' ) ? 'y' : 'n';
		
		// if  we have an existing comment_text_signature column...
		if ( $modified == 'y' ) {
		
			// upgrade old Commentpress schema to new
			if ( !$this->schema_upgrade() ) {
			
				// kill plugin activation
				_cpdie( 'CommentPress Core Error: could not upgrade the database' );
				
			}
			
		} else {
			
			// update db schema
			$this->schema_update();
			
		}
		
		
		
		// test if we have our version
		if ( !$this->option_wp_get( 'commentpress_version' ) ) {
		
			// store CommentPress Core version
			$this->option_wp_set( 'commentpress_version', COMMENTPRESS_VERSION );
		
		}
		
		
		
		// test that we aren't reactivating
		if ( !$this->option_wp_get( 'commentpress_options' ) ) {
			
			// test if we have a existing pre-3.4 Commentpress instance
			if ( commentpress_is_legacy_plugin_active() ) {
				
				// yes: add options with existing values
				$this->_options_migrate();
				
			} else {
				
				// no: add options with default values
				$this->_options_create();
				
			}
			
		}
		
		
		
		// retrieve data on special pages
		$special_pages = $this->option_get( 'cp_special_pages', array() );

		// if we haven't created any...
		if ( count( $special_pages ) == 0 ) {

			// create special pages
			$this->create_special_pages();
		
		}
		


		// turn comment paging option off
		$this->_cancel_comment_paging();

		// override widgets
		$this->_clear_widgets();

	}







	/** 
	 * @description: upgrade Commentpress plugin from 3.1 options to latest set
	 * @return boolean $result
	 * @todo: 
	 *
	 */
	function upgrade() {
		
		// database object
		global $wpdb;
		
		// init return
		$result = false;



		// if we have a commentpress install (or we're forcing)
		if ( $this->check_upgrade() ) {
		


			// are we missing the commentpress_options option?
			if ( !$this->option_wp_exists( 'commentpress_options' ) ) {
			
				// upgrade to the single array
				$this->_options_upgrade();
			
			}
			
			
			
			// checkboxes send no value if not checked, so use a default
			$cp_blog_workflow = $this->blog_workflow;
			
			// default blog type
			$cp_blog_type = $this->blog_type;
			
			
			
			// get variables
			extract( $_POST );
			
			
			
			// Removed in CP 3.4 - do we still have the legacy cp_para_comments_enabled option?
			if ( $this->option_exists( 'cp_para_comments_enabled' ) ) {
			
				// delete old cp_para_comments_enabled option
				$this->option_delete( 'cp_para_comments_enabled' );
				
			}
			


			// Removed in CP 3.4 - do we still have the legacy cp_minimise_sidebar option?
			if ( $this->option_exists( 'cp_minimise_sidebar' ) ) {
			
				// delete old cp_minimise_sidebar option
				$this->option_delete( 'cp_minimise_sidebar' );
				
			}
			


			// New in CP 3.4 - has AJAX "live" comment refreshing been migrated?
			if ( !$this->option_exists( 'cp_para_comments_live' ) ) {
	
				// "live" comment refreshing, off by default
				$this->option_set( 'cp_para_comments_live', $this->para_comments_live );
				
			}
			
			
			
			// New in CP 3.3.3 - changed the way the welcome page works
			if ( $this->option_exists( 'cp_special_pages' ) ) {
			
				// do we have the cp_welcome_page option?
				if ( $this->option_exists( 'cp_welcome_page' ) ) {
				
					// get it
					$page_id = $this->option_get( 'cp_welcome_page' );
				
					// retrieve data on special pages
					$special_pages = $this->option_get( 'cp_special_pages', array() );
					
					// is it in our special pages array?
					if ( in_array( $page_id, $special_pages ) ) {
					
						// remove page id from array
						$special_pages = array_diff( $special_pages, array( $page_id ) );
			
						// reset option
						$this->option_set( 'cp_special_pages', $special_pages );
					
					}
					
				}
	
			}
			


			// New in CP 3.3.3 - are we missing the cp_sidebar_default option?
			if ( !$this->option_exists( 'cp_sidebar_default' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_sidebar_default );
			
				// add chosen cp_page_meta_visibility option
				$this->option_set( 'cp_sidebar_default', $_choice );
				
			}
			


			// New in CP 3.3.2 - are we missing the cp_page_meta_visibility option?
			if ( !$this->option_exists( 'cp_page_meta_visibility' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_page_meta_visibility );
			
				// add chosen cp_page_meta_visibility option
				$this->option_set( 'cp_page_meta_visibility', $_choice );
				
			}
			


			// New in CP 3.3.1 - are we missing the cp_blog_workflow option?
			if ( !$this->option_exists( 'cp_blog_workflow' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_blog_workflow );
			
				// add chosen cp_blog_workflow option
				$this->option_set( 'cp_blog_workflow', $_choice );
				
			}
			


			// New in CP 3.3.1 - are we missing the cp_blog_type option?
			if ( !$this->option_exists( 'cp_blog_type' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_blog_type );
			
				// add chosen cp_blog_type option
				$this->option_set( 'cp_blog_type', $_choice );
				
			}
			


			// New in CP 3.3 - are we missing the cp_show_extended_toc option?
			if ( !$this->option_exists( 'cp_show_extended_toc' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_show_extended_toc );
			
				// add chosen cp_show_extended_toc option
				$this->option_set( 'cp_show_extended_toc', $_choice );
				
			}
			


			// are we missing the cp_comment_editor option?
			if ( !$this->option_exists( 'cp_comment_editor' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_comment_editor );
			
				// add chosen cp_comment_editor option
				$this->option_set( 'cp_comment_editor', $_choice );
				
			}
			


			// are we missing the cp_promote_reading option?
			if ( !$this->option_exists( 'cp_promote_reading' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_promote_reading );
			
				// add chosen cp_promote_reading option
				$this->option_set( 'cp_promote_reading', $_choice );
				
			}
			


			// are we missing the cp_title_visibility option?
			if ( !$this->option_exists( 'cp_title_visibility' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_title_visibility );
			
				// add chosen cp_title_visibility option
				$this->option_set( 'cp_title_visibility', $_choice );
				
			}
			


			// are we missing the cp_header_bg_colour option?
			if ( !$this->option_exists( 'cp_header_bg_colour' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_header_bg_colour );
			
				// strip our rgb #
				if ( stristr( $_choice, '#' ) ) {
					$_choice = substr( $_choice, 1 );
				}
				
				// reset to default if blank
				if ( $_choice == '' ) {
					$_choice = $this->header_bg_colour;
				}
				
				// add chosen cp_header_bg_colour option
				$this->option_set( 'cp_header_bg_colour', $_choice );
				
			}
			


			// are we missing the cp_js_scroll_speed option?
			if ( !$this->option_exists( 'cp_js_scroll_speed' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_js_scroll_speed );
			
				// add chosen cp_js_scroll_speed option
				$this->option_set( 'cp_js_scroll_speed', $_choice );
				
			}
			


			// are we missing the cp_min_page_width option?
			if ( !$this->option_exists( 'cp_min_page_width' ) ) {
			
				// get choice
				$_choice = $wpdb->escape( $cp_min_page_width );
			
				// add chosen cp_min_page_width option
				$this->option_set( 'cp_min_page_width', $_choice );
				
			}
			


			// do we still have the legacy cp_allow_users_to_minimize option?
			if ( $this->option_exists( 'cp_allow_users_to_minimize' ) ) {
			
				// delete old cp_allow_users_to_minimize option
				$this->option_delete( 'cp_allow_users_to_minimize' );
				
			}
			


			// do we have special pages?
			if ( $this->option_exists( 'cp_special_pages' ) ) {
			
				// if we don't have the toc page...
				if ( !$this->option_exists( 'cp_toc_page' ) ) {
				
					// get special pages array
					$special_pages = $this->option_get( 'cp_special_pages', array() );
				
					// create TOC page -> a convenience, let's us define a logo as attachment
					$special_pages[] = $this->_create_toc_page();
		
					// store the array of page IDs that were created
					$this->option_set( 'cp_special_pages', $special_pages );
					
				}
	
			}
			


			// save new CommentPress Core options
			$this->options_save();
			
			// store new CommentPress Core version
			$this->option_wp_set( 'commentpress_version', COMMENTPRESS_VERSION );
			
		}
		
		

		// --<
		return $result;
	}
	
	
	
	
	


	/** 
	 * @description: reset Wordpress to prior state, but retain options
	 * @todo: 
	 *
	 */
	function deactivate() {
		
		// reset comment paging option
		$this->_reset_comment_paging();
		
		// restore widgets
		$this->_reset_widgets();
		
		// always remove special pages
		$this->delete_special_pages();
		
	}







//##############################################################################







	/*
	============================================================================
	PUBLIC METHODS
	============================================================================
	*/
	




	/** 
	 * @description: update Wordpress database schema
	 * @return boolean $result
	 * @todo: 
	 *
	 */
	function schema_update() {
		
		// database object
		global $wpdb;
		


		// include Wordpress upgrade script
		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
		
		// add the column, if not already there
		$result = maybe_add_column(
		
			$wpdb->comments, 
			'comment_signature', 
			"ALTER TABLE `$wpdb->comments` ADD `comment_signature` VARCHAR(255) NULL;"
			
		);
		


		// --<
		return $result;
	}
	
	
	
	
	


	/** 
	 * @description: upgrade Wordpress database schema
	 * @return boolean $result
	 * @todo: 
	 *
	 */
	function schema_upgrade() {
		
		// database object
		global $wpdb;
		
		// init
		$return = false;
		
		// construct query
		$query = "ALTER TABLE `$wpdb->comments` CHANGE `comment_text_signature` `comment_signature` VARCHAR(255) NULL;";
		
		// do the query to rename the column
		$wpdb->query( $query );
		
		// test if we now have the correct column name...
		if ( $this->db_is_modified( 'comment_signature' ) ) {
			
			// yes
			$result = true;
		
		}
		
		// --<
		return $result;
	}
	
	
	
	
	


	/** 
	 * @description: do we have a column in the comments table?
	 * @return boolean $result
	 * @todo: 
	 *
	 */
	function db_is_modified( $column_name ) {
		
		// database object
		global $wpdb;
		
		// init
		$result = false;
		
		
		
		// define query
		$query = "DESCRIBE $wpdb->comments";
		
		// get columns
		$cols = $wpdb->get_results( $query );
		
		// loop
		foreach( $cols AS $col ) {
		
			// is it our desired column?
			if ( $col->Field == $column_name ) {
				
				// we got it
				$result = true;
				break;
			
			}
		
		}
		


		// --<
		return $result;
	}
	
	
	
	
	


	/** 
	 * @description: check for plugin upgrade
	 * @return boolean $result
	 * @todo: 
	 *
	 */
	function check_upgrade() {
	
		// init
		$result = false;
		
		// get installed version cast as string
		$_version = (string) $this->option_wp_get( 'commentpress_version' );
		
		// if we have a commentpress install and it's lower than this one
		if ( $_version !== false AND version_compare( COMMENTPRESS_VERSION, $_version, '>' ) ) {
		
			// override
			$result = true;

		}
		


		// --<
		return $result;

	}
	
	
	
	
	


	/** 
	 * @description: save the settings set by the administrator
	 * @return boolean success or failure
	 * @todo: do more error checking?
	 *
	 */
	function options_update() {
	
		// database object
		global $wpdb;
		
		
	
		// init result
		$result = false;
		


	 	// was the form submitted?
		if( isset( $_POST[ 'commentpress_submit' ] ) ) {
			


			// check that we trust the source of the data
			check_admin_referer( 'commentpress_admin_action', 'commentpress_nonce' );
		
			
			
			// init vars
			$cp_activate = '0';
			$cp_upgrade = '';
			$cp_reset = '';
			$cp_create_pages = '';
			$cp_delete_pages = '';
			$cp_para_comments_live = 0;
			$cp_show_subpages = 0;
			$cp_show_extended_toc = 0;
			$cp_blog_type = 0;
			$cp_blog_workflow = 0;
			$cp_sidebar_default = 'comments';
			
			
			
			// get variables
			extract( $_POST );
			
			
			
			// hand off to Multisite first, in case we're deactivating
			do_action( 'cpmu_deactivate_commentpress' );
			
			// is Multisite activating CommentPress Core?
			if ( $cp_activate == '1' ) { return true; }
			
			
			
			// did we ask to upgrade CommentPress Core?
			if ( $cp_upgrade == '1' ) {
			
				// do upgrade
				$this->upgrade();
				
				// --<
				return true;
			
			}
			
			
			
			// did we ask to reset?
			if ( $cp_reset == '1' ) {
			
				// reset theme options
				$this->_options_reset();
		
				// --<
				return true;
			
			}


			
			// did we ask to auto-create special pages?
			if ( $cp_create_pages == '1' ) {
			
				// remove any existing special pages
				$this->delete_special_pages();
				
				// create special pages
				$this->create_special_pages();
				
			}
			
			
			
			// did we ask to delete special pages?
			if ( $cp_delete_pages == '1' ) {
			
				// remove special pages
				$this->delete_special_pages();

			}
			
			
			
			// let's deal with our params now

			// individual special pages
			//$cp_welcome_page = $wpdb->escape( $cp_welcome_page );
			//$cp_blog_page = $wpdb->escape( $cp_blog_page );
			//$cp_general_comments_page = $wpdb->escape( $cp_general_comments_page );
			//$cp_all_comments_page = $wpdb->escape( $cp_all_comments_page );
			//$cp_comments_by_page = $wpdb->escape( $cp_comments_by_page );
			//$this->option_set( 'cp_welcome_page', $cp_welcome_page );
			//$this->option_set( 'cp_blog_page', $cp_blog_page );
			//$this->option_set( 'cp_general_comments_page', $cp_general_comments_page );
			//$this->option_set( 'cp_all_comments_page', $cp_all_comments_page );
			//$this->option_set( 'cp_comments_by_page', $cp_comments_by_page );
			
			// TOC content
			$cp_show_posts_or_pages_in_toc = $wpdb->escape( $cp_show_posts_or_pages_in_toc );
			$this->option_set( 'cp_show_posts_or_pages_in_toc', $cp_show_posts_or_pages_in_toc );
			
			// if we have pages in TOC and a value for the next param...
			if ( $cp_show_posts_or_pages_in_toc == 'page' AND isset( $cp_toc_chapter_is_page ) ) {
				
				$cp_toc_chapter_is_page = $wpdb->escape( $cp_toc_chapter_is_page );
				$this->option_set( 'cp_toc_chapter_is_page', $cp_toc_chapter_is_page );
				
				// if chapters are not pages and we have a value for the next param...
				if ( $cp_toc_chapter_is_page == '0' ) {
					
					$cp_show_subpages = $wpdb->escape( $cp_show_subpages );
					$this->option_set( 'cp_show_subpages', ( $cp_show_subpages ? 1 : 0 ) );

				} else {
				
					// always set to show subpages
					$this->option_set( 'cp_show_subpages', 1 );
				
				}

			}
			
			// extended or vanilla posts TOC
			if ( $cp_show_posts_or_pages_in_toc == 'post' ) {
				
				$cp_show_extended_toc = $wpdb->escape( $cp_show_extended_toc );
				$this->option_set( 'cp_show_extended_toc', ( $cp_show_extended_toc ? 1 : 0 ) );

			}

			// excerpt length
			$this->option_set( 'cp_excerpt_length', $cp_excerpt_length );
			$cp_excerpt_length = $wpdb->escape( $cp_excerpt_length );
			
			// comment editor
			$cp_comment_editor = $wpdb->escape( $cp_comment_editor );
			$this->option_set( 'cp_comment_editor', ( $cp_comment_editor ? 1 : 0 ) );
			
			// has AJAX "live" comment refreshing been migrated?
			if ( $this->option_exists( 'cp_para_comments_live' ) ) {
	
				// "live" comment refreshing
				$cp_para_comments_live = $wpdb->escape( $cp_para_comments_live );
				$this->option_set( 'cp_para_comments_live', ( $cp_para_comments_live ? 1 : 0 ) );
				
			}
			
			// behaviour
			$cp_promote_reading = $wpdb->escape( $cp_promote_reading );
			$this->option_set( 'cp_promote_reading', ( $cp_promote_reading ? 1 : 0 ) );
			
			// title visibility
			$cp_title_visibility = $wpdb->escape( $cp_title_visibility );
			$this->option_set( 'cp_title_visibility', $cp_title_visibility );
			
			// page meta visibility
			$cp_page_meta_visibility = $wpdb->escape( $cp_page_meta_visibility );
			$this->option_set( 'cp_page_meta_visibility', $cp_page_meta_visibility );
			
			// header background colour
			
			// strip our rgb #
			if ( stristr( $cp_header_bg_colour, '#' ) ) {
				$cp_header_bg_colour = substr( $cp_header_bg_colour, 1 );
			}
			
			// reset to default if blank
			if ( $cp_header_bg_colour == '' ) {
				$cp_header_bg_colour = $this->header_bg_colour;
			}
			
			// save it
			$cp_header_bg_colour = $wpdb->escape( $cp_header_bg_colour );
			$this->option_set( 'cp_header_bg_colour', $cp_header_bg_colour );
			
			// save scroll speed
			$cp_js_scroll_speed = $wpdb->escape( $cp_js_scroll_speed );
			$this->option_set( 'cp_js_scroll_speed', $cp_js_scroll_speed );
			
			// save min page width
			$cp_min_page_width = $wpdb->escape( $cp_min_page_width );
			$this->option_set( 'cp_min_page_width', $cp_min_page_width );
			
			// save workflow
			$cp_blog_workflow = $wpdb->escape( $cp_blog_workflow );
			$this->option_set( 'cp_blog_workflow', ( $cp_blog_workflow ? 1 : 0 ) );

			// save blog type
			$cp_blog_type = $wpdb->escape( $cp_blog_type );
			$this->option_set( 'cp_blog_type', $cp_blog_type );
			
			// if it's a groupblog
			if ( $this->parent_obj->is_groupblog() ) {
				
				// get the group's id
				$group_id = get_groupblog_group_id( get_current_blog_id() );
				if ( is_numeric( $group_id ) ) {
				
					// allow plugins to override the blog type - for example if workflow is enabled, 
					// it might become a new blog type as far as buddypress is concerned
					$_blog_type = apply_filters( 'cp_get_group_meta_for_blog_type', $cp_blog_type, $cp_blog_workflow );
	
					// set the type as group meta info
					groups_update_groupmeta( $group_id, 'groupblogtype', 'groupblogtype-'.$_blog_type );
					
				}
			
			}

			// save default sidebar
			$cp_sidebar_default = $wpdb->escape( $cp_sidebar_default );
			$this->option_set( 'cp_sidebar_default', $cp_sidebar_default );
			


			// save
			$this->options_save();
			
			

			// set flag
			$result = true;
	
		}
		
		
		
		// --<
		return $result;
		
	}
	
	
	
	
	
	
	
	/** 
	 * @description: upgrade CommentPress Core options to array
	 * @todo: 
	 *
	 */
	function options_save() {
		
		// set option
		return $this->option_wp_set( 'commentpress_options', $this->commentpress_options );
		
	}
	
	
	
	
	


	/** 
	 * @description: return a value for a specified option
	 * @todo: 
	 */
	function option_exists( $option_name = '' ) {
	
		// test for null
		if ( $option_name == '' ) {
		
			// oops
			die( __( 'You must supply an option to option_exists()', 'commentpress-core' ) );
		
		}
	
		// get option with unlikely default
		return array_key_exists( $option_name, $this->commentpress_options );
		
	}
	
	
	
	
	
	
	/** 
	 * @description: return a value for a specified option
	 * @todo: 
	 */
	function option_get( $option_name = '', $default = false ) {
	
		// test for null
		if ( $option_name == '' ) {
		
			// oops
			die( __( 'You must supply an option to option_get()', 'commentpress-core' ) );
		
		}
	
		// get option
		return ( array_key_exists( $option_name, $this->commentpress_options ) ) ? $this->commentpress_options[ $option_name ] : $default;
		
	}
	
	
	
	
	
	
	/** 
	 * @description: sets a value for a specified option
	 * @todo: 
	 */
	function option_set( $option_name = '', $value = '' ) {
	
		// test for null
		if ( $option_name == '' ) {
		
			// oops
			die( __( 'You must supply an option to option_set()', 'commentpress-core' ) );
		
		}
	
		// test for other than string
		if ( !is_string( $option_name ) ) {
		
			// oops
			die( __( 'You must supply the option as a string to option_set()', 'commentpress-core' ) );
		
		}
	
		// set option
		$this->commentpress_options[ $option_name ] = $value;
		
	}
	
	
	
	
	
	
	/** 
	 * @description: deletes a specified option
	 * @todo: 
	 */
	function option_delete( $option_name = '' ) {
	
		// test for null
		if ( $option_name == '' ) {
		
			// oops
			die( __( 'You must supply an option to option_delete()', 'commentpress-core' ) );
		
		}
	
		// unset option
		unset( $this->commentpress_options[ $option_name ] );
		
	}
	
	
	
	
	
	
	/** 
	 * @description: return a value for a specified option
	 * @todo: 
	 */
	function option_wp_exists( $option_name = '' ) {
	
		// test for null
		if ( $option_name == '' ) {
		
			// oops
			die( __( 'You must supply an option to option_wp_exists()', 'commentpress-core' ) );
		
		}
	
		// get option with unlikely default
		if ( $this->option_wp_get( $option_name, 'fenfgehgejgrkj' ) == 'fenfgehgejgrkj' ) {
		
			// no
			return false;
		
		} else {
		
			// yes
			return true;
		
		}
		
	}
	
	
	
	
	
	
	/** 
	 * @description: return a value for a specified option
	 * @todo: 
	 */
	function option_wp_get( $option_name = '', $default = false ) {
	
		// test for null
		if ( $option_name == '' ) {
		
			// oops
			die( __( 'You must supply an option to option_wp_get()', 'commentpress-core' ) );
		
		}
	
		// get option
		return get_option( $option_name, $default );
		
	}
	
	
	
	
	
	
	/** 
	 * @description: sets a value for a specified option
	 * @todo: 
	 */
	function option_wp_set( $option_name = '', $value = null ) {
	
		// test for null
		if ( $option_name == '' ) {
		
			// oops
			die( __( 'You must supply an option to option_wp_set()', 'commentpress-core' ) );
		
		}
	
		// set option
		return update_option( $option_name, $value );
		
	}
	
	
	
	
	
	
	/** 
	 * @description: get default header bg colour
	 * @todo: 
	 */
	function option_get_header_bg() {
	
		// test for option
		if ( $this->option_exists( 'cp_header_bg_colour' ) ) {
		
			// --<
			return $this->option_get( 'cp_header_bg_colour' );
			
		} else {
		
			// --<
			return $this->header_bg_colour;
			
		}
	
	}
	
	
	
	
	
	
	/** 
	 * @description: when a page is saved, this also saves the CP options
	 * @param object $post_obj the post object
	 * @return boolean $result
	 * @todo: 
	 *
	 */
	function save_meta( $post_obj ) {
	
		// if no post, kick out
		if ( !$post_obj ) { return; }
		
		// if page...
		if ( $post_obj->post_type == 'page' ) {
		
			$this->save_page_meta( $post_obj );
		
		}
		
		// if post...
		if ( $post_obj->post_type == 'post' ) {
		
			$this->save_post_meta( $post_obj );
		
		}
		
	}
	
	
	
	
	
	
	
	/** 
	 * @description: when a page is saved, this also saves the CP options
	 * @param object $post_obj the post object
	 * @return boolean $result
	 * @todo: 
	 *
	 */
	function save_page_meta( $post_obj ) {
		
		//print_r( 'data: '.$_data ); die();
		//print_r( '$post_obj->post_type: '.$post_obj->post_type ); die();
		//print_r( '$post_obj->ID: '.$post_obj->ID ); die();
		
		// if no post, kick out
		if ( !$post_obj ) { return; }
		
		// if not page, kick out
		if ( $post_obj->post_type != 'page' ) { return; }
		
		
		
		// authenticate
		$_nonce = isset( $_POST['commentpress_nonce'] ) ? $_POST['commentpress_nonce'] : '';
		if ( !wp_verify_nonce( $_nonce, 'commentpress_page_settings' ) ) { return; }
		
		// is this an auto save routine?
		if ( defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE ) { return; }
		
		// check permissions - 'edit_pages' is available to editor+
		if ( !current_user_can( 'edit_pages' ) ) { return; }
		

		
		// OK, we're authenticated
		
		
		
		// check for revision
		if ( $post_obj->post_type == 'revision' ) {
		
			// get parent
			if ( $post_obj->post_parent != 0 ) {
				$post = get_post( $post_obj->post_parent );
			} else {
				$post = $post_obj;
			}
	
		} else {
			$post = $post_obj;
		}
		


		// database object and post
		global $wpdb;
		


		// ---------------------------------------------------------------------
		// Show or Hide Page Meta
		// ---------------------------------------------------------------------
		
		// find and save the data
		$_data = ( isset( $_POST['cp_page_meta_visibility'] ) ) ? $_POST['cp_page_meta_visibility'] : 'hide';

		//print_r( '$_data: '.$_data ); die();
		//print_r( $post ); die();

		// set key
		$key = '_cp_page_meta_visibility';
		
		// if the custom field already has a value...
		if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
		
			// if empty string...
			if ( $_data === '' ) {
		
				// delete the meta_key
				delete_post_meta( $post->ID, $key );
			
			} else {
			
				// update the data
				update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
				
			}
			
		} else {
		
			// add the data
			add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
			
		}



		// ---------------------------------------------------------------------
		// Show or Hide Page Title
		// ---------------------------------------------------------------------
		
		// find and save the data
		$_data = ( isset( $_POST['cp_title_visibility'] ) ) ? $_POST['cp_title_visibility'] : 'show';

		//print_r( '$_data: '.$_data ); die();
		//print_r( $post ); die();

		// set key
		$key = '_cp_title_visibility';
		
		// if the custom field already has a value...
		if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
		
			// if empty string...
			if ( $_data === '' ) {
		
				// delete the meta_key
				delete_post_meta( $post->ID, $key );
			
			} else {
			
				// update the data
				update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
				
			}
			
		} else {
		
			// add the data
			add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
			
		}



		// ---------------------------------------------------------------------
		// Page Numbering - only first top level page is allowed to send this
		// ---------------------------------------------------------------------
		
		// was the value sent?
		if ( isset( $_POST['cp_number_format'] ) ) {
		
			// set meta key
			$key = '_cp_number_format';
			
			if ( 
				
				// do we need to check this, since only the first top level page
				// can now send this data? doesn't hurt to validate, I guess.
				$post->post_parent == '0' AND 
				!$this->is_special_page() AND 
				$post->ID == $this->parent_obj->nav->get_first_page() 
				
			) { // -->
	
				// get the data
				$_data = $_POST['cp_number_format'];
		
				//print_r( $post->ID ); die();
				
				// if the custom field already has a value...
				if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
				
					// if empty string...
					if ( $_data === '' ) {
				
						// delete the meta_key
						delete_post_meta( $post->ID, $key );
					
					} else {
					
						// update the data
						update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
						
					}
					
				} else {
				
					// add the data
					add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
					
				}

			}
			
			// delete this meta value from all other pages, because we may have altered
			// the relationship between pages, thus causing the page numbering to fail
			
			// get all pages including chapters
			$all_pages = $this->parent_obj->nav->get_book_pages( 'structural' );
			
			// if we have any pages...
			if ( count( $all_pages ) > 0 ) {
			
				// loop
				foreach( $all_pages AS $_page ) {
				
					// exclude first top level page
					if ( $post->ID != $_page->ID ) {
				
						// delete the meta value
						delete_post_meta( $_page->ID, $key );
					
					}
				
				}
			
			}
			
		}
		
		
		
		// ---------------------------------------------------------------------
		// Page Layout for Title Page -> to allow for Book Cover image
		// ---------------------------------------------------------------------
		
		// is this the title page?
		if ( $post->ID == $this->option_get( 'cp_welcome_page' ) ) {
		
			// find and save the data
			$_data = ( isset( $_POST['cp_page_layout'] ) ) ? $_POST['cp_page_layout'] : 'text';
	
			// set key
			$key = '_cp_page_layout';
			
			// if the custom field already has a value...
			if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
			
				// if empty string...
				if ( $_data === '' ) {
			
					// delete the meta_key
					delete_post_meta( $post->ID, $key );
				
				} else {
				
					// update the data
					update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
					
				}
				
			} else {
			
				// add the data
				add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
				
			}
			
		}



		// ---------------------------------------------------------------------
		// Override post formatter (override blog_type)
		// ---------------------------------------------------------------------
		
		// get the data
		$_data = ( isset( $_POST['cp_post_type_override'] ) ) ? $_POST['cp_post_type_override'] : '';

		//print_r( '$_data: '.$_data ); die();
		//print_r( $post ); die();

		// set key
		$key = '_cp_post_type_override';
		
		// if the custom field already has a value...
		if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
		
			// if empty string...
			if ( $_data === '' ) {
		
				// delete the meta_key
				delete_post_meta( $post->ID, $key );
			
			} else {
			
				// update the data
				update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
				
			}
			
		} else {
		
			// add the data
			add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
			
		}



		// ---------------------------------------------------------------------
		// Default Sidebar
		// ---------------------------------------------------------------------
		
		// do we have the option to choose the default sidebar (new in 3.3.3)?
		if ( $this->option_exists( 'cp_sidebar_default' ) ) {
		
			// find and save the data
			$_data = ( isset( $_POST['cp_sidebar_default'] ) ) ? 
					 $_POST['cp_sidebar_default'] : 
					 $this->db->option_get( 'cp_sidebar_default' );
	
			//print_r( '$_data: '.$_data ); die();
			//print_r( $post ); die();
	
			// set key
			$key = '_cp_sidebar_default';
			
			// if the custom field already has a value...
			if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
			
				// if empty string...
				if ( $_data === '' ) {
			
					// delete the meta_key
					delete_post_meta( $post->ID, $key );
				
				} else {
				
					// update the data
					update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
					
				}
				
			} else {
			
				// add the data
				add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
				
			}
			
		}



	}
	
	
	
	
	


	/** 
	 * @description: when a post is saved, this also saves the CP options
	 * @param object $post_obj the post object
	 * @return boolean $result
	 * @todo: 
	 *
	 */
	function save_post_meta( $post_obj ) {
		
		//print_r( 'data: '.$_data ); die();
		//print_r( '$post_obj->post_type: '.$post_obj->post_type ); die();
		//print_r( '$post_obj->ID: '.$post_obj->ID ); die();
		
		// if no post, kick out
		if ( !$post_obj ) { return; }
		
		// if not page, kick out
		if ( $post_obj->post_type != 'post' ) { return; }
		
		
		
		// authenticate
		$_nonce = isset( $_POST['commentpress_nonce'] ) ? $_POST['commentpress_nonce'] : '';
		if ( !wp_verify_nonce( $_nonce, 'commentpress_post_settings' ) ) { return; }
		
		// is this an auto save routine?
		if ( defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE ) { return; }
		
		// check permissions - 'edit_posts' is available to contributor+
		if ( !current_user_can( 'edit_posts', $post_obj->ID ) ) { return; }
		

		
		// OK, we're authenticated
		
		
		
		// check for revision
		if ( $post_obj->post_type == 'revision' ) {
		
			// get parent
			if ( $post_obj->post_parent != 0 ) {
				$post = get_post( $post_obj->post_parent );
			} else {
				$post = $post_obj;
			}
	
		} else {
			$post = $post_obj;
		}
		


		// database object and post
		global $wpdb;
		


		// ---------------------------------------------------------------------
		// Override post formatter (override blog_type)
		// ---------------------------------------------------------------------
		
		// get the data
		$_formatter = ( isset( $_POST['cp_post_type_override'] ) ) ? $_POST['cp_post_type_override'] : '';

		//print_r( '$_data: '.$_data ); die();
		//print_r( $post ); die();

		// set key
		$key = '_cp_post_type_override';
		
		// if the custom field already has a value...
		if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
		
			// if empty string...
			if ( $_formatter === '' ) {
		
				// delete the meta_key
				delete_post_meta( $post->ID, $key );
			
			} else {
			
				// update the data
				update_post_meta( $post->ID, $key, $wpdb->escape( $_formatter ) );
				
			}
			
		} else {
		
			// add the data
			add_post_meta( $post->ID, $key, $wpdb->escape( $_formatter ) );
			
		}



		// ---------------------------------------------------------------------
		// Workflow
		// ---------------------------------------------------------------------
		
		// do we have the option to set workflow (new in 3.3.1)?
		if ( $this->option_exists( 'cp_blog_workflow' ) ) {
		
			// get workflow setting for the blog
			$_workflow = $this->option_get( 'cp_blog_workflow' );
			
			/*
			// ----------------
			// WORK IN PROGRESS
			
			// set key
			$key = '_cp_blog_workflow_override';
			
			// if the custom field already has a value...
			if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
			
				// get existing value
				$_workflow = get_post_meta( $post->ID, $key, true );
				
			}
			// ----------------
			*/
			
			// if it's enabled for the blog or the post...
			if ( $_workflow == '1' ) {
			
				// notify plugins that workflow stuff needs saving
				do_action( 'cp_workflow_save_post', $post );
			
			}
			
			/*
			// ----------------
			// WORK IN PROGRESS
			
			// get the setting for the post (we do this after saving the extra
			// post data because 
			$_formatter = ( isset( $_POST['cp_post_type_override'] ) ) ? $_POST['cp_post_type_override'] : '';
	
			// if the custom field already has a value...
			if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
			
				// if empty string...
				if ( $_data === '' ) {
			
					// delete the meta_key
					delete_post_meta( $post->ID, $key );
				
				} else {
				
					// update the data
					update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
					
				}
				
			} else {
			
				// add the data
				add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
				
			}
			// ----------------
			*/
			
		}
		


		// ---------------------------------------------------------------------
		// Default Sidebar
		// ---------------------------------------------------------------------
		
		// do we have the option to choose the default sidebar (new in 3.3.3)?
		if ( $this->option_exists( 'cp_sidebar_default' ) ) {
		
			// find and save the data
			$_data = ( isset( $_POST['cp_sidebar_default'] ) ) ? 
					 $_POST['cp_sidebar_default'] : 
					 $this->db->option_get( 'cp_sidebar_default' );
	
			//print_r( '$_data: '.$_data ); die();
			//print_r( $post ); die();
	
			// set key
			$key = '_cp_sidebar_default';
			
			// if the custom field already has a value...
			if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
			
				// if empty string...
				if ( $_data === '' ) {
			
					// delete the meta_key
					delete_post_meta( $post->ID, $key );
				
				} else {
				
					// update the data
					update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
					
				}
				
			} else {
			
				// add the data
				add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
				
			}
			
		}



		// ---------------------------------------------------------------------
		// Create new post with content of current
		// ---------------------------------------------------------------------
		
		// find and save the data
		$_data = ( isset( $_POST['commentpress_new_post'] ) ) ? $_POST['commentpress_new_post'] : '0';
		
		/*
		print_r( array( 
		
			'$_data' => $_data,
			'$post ' => $post 
			
		) ); die();
		*/
		
		// do we want to create a new revision?
		if ( $_data == '0' ) { return; }
		
		

		// we need to make sure this only runs once
		//print_r( $this->saved_post === false ? 'f' : 't' ); die();
		if ( $this->saved_post === false ) {
			$this->saved_post = true;
		} else {
			return;
		}
		


		// ---------------------------------------------------------------------



		// we're through: create it
		$new_post_id = $this->_create_new_post( $post );
		
		
		
		// ---------------------------------------------------------------------
		// Store ID of new version in current version
		// ---------------------------------------------------------------------
		
		// set key
		$key = '_cp_newer_version';
		
		// if the custom field already has a value...
		if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
		
			// if empty string...
			if ( $_data === '' ) {
		
				// delete the meta_key
				delete_post_meta( $post->ID, $key );
			
			} else {
			
				// update the data
				update_post_meta( $post->ID, $key, $new_post_id );
				
			}
			
		} else {
		
			// add the data
			add_post_meta( $post->ID, $key, $new_post_id );
			
		}



		// ---------------------------------------------------------------------
		// Store incremental version number in new version
		// ---------------------------------------------------------------------
		
		// set key
		$key = '_cp_version_count';
		
		// if the custom field of our current post has a value...
		if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
		
			// get current value
			$value = get_post_meta( $post->ID, $key, true );
		
			// increment
			$value++;
			
		} else {
		
			// this must be the first new version (Draft 2)
			$value = 2;
		
		}

		// add the data
		add_post_meta( $new_post_id, $key, $value );
		
		
		
		// ---------------------------------------------------------------------
		// Store formatter in new version
		// ---------------------------------------------------------------------
		
		// set key
		$key = '_cp_post_type_override';
		
		// if we have one set...
		if ( $_formatter != '' ) {
		
			// add the data
			add_post_meta( $new_post_id, $key, $wpdb->escape( $_formatter ) );
			
		}
		
		
		
		// allow plugins to hook into this
		do_action( 'cp_workflow_save_copy', $new_post_id );
			


		// get the edit post link
		//$edit_link = get_edit_post_link( $new_post_id );
		
		// redirect there?

	}
	
	
	
	
	


	/** 
	 * @description: when a page is deleted, this makes sure that the CP options are synced
	 * @param object $post_id the post ID
	 * @return none
	 * @todo: 
	 *
	 */
	function delete_meta( $post_id ) {
	
		// if no post, kick out
		if ( !$post_id ) { return; }
		
		
		
		// if it's our welcome page...
		if ( $post_id == $this->option_get( 'cp_welcome_page' ) ) {
		
			// delete option
			$this->option_delete( 'cp_welcome_page' );
			
			// save
			$this->options_save();
			
		}
		
		
		
		// for posts with versions, we need to delete the version data for the previous version
		
		// define key
		$key = '_cp_newer_version';
		
		// get posts with the about-to-be-deleted post_id (there will be only one, if at all)
		$previous_versions = get_posts( array(
			
			'meta_key' => $key,
			'meta_value' => $post_id
			
		) );
		
		// did we get one?
		if ( count( $previous_versions ) > 0 ) {
		
			// get it
			$previous_version = $previous_versions[0];
		
			// if the custom field has a value...
			if ( get_post_meta( $previous_version->ID, $key, true ) !== '' ) {
			
				// delete it
				delete_post_meta( $previous_version->ID, $key );
			
			}
			
		}
		
	}
	
	
	
	
	
	
	
	/** 
	 * @description: create all "special" pages
	 * @todo: 
	 *
	 */
	function create_special_pages() {
		
		// one of the CommentPress Core themes MUST be active...
		// or WordPress will fail to set the page templates for the pages that require them.
		// Also, a user must be logged in for these pages to be associated with them.
	
		// get special pages array, if it's there
		$special_pages = $this->option_get( 'cp_special_pages', array() );
	


		// create welcome/title page, but don't add to special pages
		$welcome = $this->_create_title_page();
		
		// create general comments page
		$special_pages[] = $this->_create_general_comments_page();

		// create all comments page
		$special_pages[] = $this->_create_all_comments_page();
		
		// create comments by author page
		$special_pages[] = $this->_create_comments_by_author_page();

		// create blog page
		$special_pages[] = $this->_create_blog_page();
		
		// create blog archive page
		$special_pages[] = $this->_create_blog_archive_page();
		
		// create TOC page -> a convenience, let's us define a logo as attachment
		$special_pages[] = $this->_create_toc_page();



		// store the array of page IDs that were created
		$this->option_set( 'cp_special_pages', $special_pages );
		
		// save changes
		$this->options_save();

	}
	
	
	
	
	


	/** 
	 * @description: create a particular "special" page
	 * @todo: 
	 *
	 */
	function create_special_page( $_page ) {
	
		// init
		$new_id = false;
		
		
	
		// get special pages array, if it's there
		$special_pages = $this->option_get( 'cp_special_pages', array() );
	


		// switch by page
		switch( $_page ) {
		
			case 'title':
			
				// create welcome/title page
				$new_id = $this->_create_title_page();
				break;
		
			case 'general_comments':
			
				// create general comments page
				$new_id = $this->_create_general_comments_page();
				break;
		
			case 'all_comments':
			
				// create all comments page
				$new_id = $this->_create_all_comments_page();
				break;
		
			case 'comments_by_author':
			
				// create comments by author page
				$new_id = $this->_create_comments_by_author_page();
				break;
		
			case 'blog':
			
				// create blog page
				$new_id = $this->_create_blog_page();
				break;
		
			case 'blog_archive':
			
				// create blog page
				$new_id = $this->_create_blog_archive_page();
				break;
		
			case 'toc':
			
				// create TOC page
				$new_id = $this->_create_toc_page();
				break;
		
		}
		
		
		
		// add to special pages
		$special_pages[] = $new_id;

		// reset option
		$this->option_set( 'cp_special_pages', $special_pages );
		
		// save changes
		$this->options_save();


		
		// --<
		return $new_id;
	
	}
	
	
	
	
	


	/** 
	 * @description: delete "special" pages
	 * @return boolean $success
	 * @todo: 
	 *
	 */
	function delete_special_pages() {
	
		// init success flag
		$success = true;
		


		// only delete special pages if we have one of the CommentPress Core themes active
		// because other themes may have a totally different way of presenting the
		// content of the blog

		// retrieve data on special pages
		$special_pages = $this->option_get( 'cp_special_pages', array() );
		
		// if we have created any...
		if ( is_array( $special_pages ) AND count( $special_pages ) > 0 ) {
		
			// loop through them
			foreach( $special_pages AS $special_page ) {
			
				// bypass trash
				$force_delete = true;
			
				// try and delete each page...
				if ( !wp_delete_post( $special_page, $force_delete ) ) {
				
					// oops, set success flag to false
					$success = false;
				
				}
			
			}
		
			// delete the corresponding options
			$this->option_delete( 'cp_special_pages' );

			$this->option_delete( 'cp_blog_page' );
			$this->option_delete( 'cp_blog_archive_page' );
			$this->option_delete( 'cp_general_comments_page' );
			$this->option_delete( 'cp_all_comments_page' );
			$this->option_delete( 'cp_comments_by_page' );
			$this->option_delete( 'cp_toc_page' );
			
			// for now, keep welcome page - delete option when page is deleted
			//$this->option_delete( 'cp_welcome_page' );
			
			// save changes
			$this->options_save();
			
			// reset Wordpress internal page references
			$this->_reset_wordpress_option( 'show_on_front' );
			$this->_reset_wordpress_option( 'page_on_front' );
			$this->_reset_wordpress_option( 'page_for_posts' );
	
		}
	


		// --<
		return $success;

	}
	
	
	
	
	


	/** 
	 * @description: delete a particular "special" page
	 * @return boolean $success
	 * @todo: 
	 *
	 */
	function delete_special_page( $_page ) {
	
		// init success flag
		$success = true;
		


		// only delete a special page if we have one of the CommentPress Core themes active
		// because other themes may have a totally different way of presenting the
		// content of the blog
		


		// get id of special page
		switch( $_page ) {
		
			case 'title':
			
				// set flag
				$flag = 'cp_welcome_page';

				// reset Wordpress internal page references
				$this->_reset_wordpress_option( 'show_on_front' );
				$this->_reset_wordpress_option( 'page_on_front' );
	
				break;
		
			case 'general_comments':
			
				// set flag
				$flag = 'cp_general_comments_page';
				break;
		
			case 'all_comments':
			
				// set flag
				$flag = 'cp_all_comments_page';
				break;
		
			case 'comments_by_author':
			
				// set flag
				$flag = 'cp_comments_by_page';
				break;
		
			case 'blog':
			
				// set flag
				$flag = 'cp_blog_page';

				// reset Wordpress internal page reference
				$this->_reset_wordpress_option( 'page_for_posts' );
			
				break;
		
			case 'blog_archive':
			
				// set flag
				$flag = 'cp_blog_archive_page';
				break;
		
			case 'toc':
			
				// set flag
				$flag = 'cp_toc_page';
				break;
		
		}
		


		// get page id
		$page_id = $this->option_get( $flag );
		
		// kick out if it doesn't exist
		if ( !$page_id ) { return true; }



		// delete option
		$this->option_delete( $flag );



		// bypass trash
		$force_delete = true;
	
		// try and delete the page...
		if ( !wp_delete_post( $page_id, $force_delete ) ) {
		
			// oops, set success flag to false
			$success = false;
		
		}
	


		// retrieve data on special pages
		$special_pages = $this->option_get( 'cp_special_pages', array() );
		
		// is it in our special pages array?
		if ( in_array( $page_id, $special_pages ) ) {
		
			// remove page id from array
			$special_pages = array_diff( $special_pages, array( $page_id ) );

			// reset option
			$this->option_set( 'cp_special_pages', $special_pages );
		
		}
		
		// save changes
		$this->options_save();



		// --<
		return $success;

	}
	
	
	
	
	


	/** 
	 * @description: test if a page is a "special" page
	 * @return boolean $is_special_page
	 * @todo: 
	 *
	 */
	function is_special_page() {
	
		// init flag
		$is_special_page = false;
		


		// access post object
		global $post;
		
		// do we have one?
		if ( !is_object( $post ) ) {
		
			// --<
			return $is_special_page;
			
		}
	


		// get special pages
		$special_pages = $this->option_get( 'cp_special_pages', array() );
	
		// do we have a special page array?
		if ( is_array( $special_pages ) AND count( $special_pages ) > 0 ) {
		
			// is the current page one?
			if ( in_array( $post->ID, $special_pages ) ) {
			
				// it is...
				$is_special_page = true;
			
			}
		
		}



		// --<
		return $is_special_page;

	}
	
	
	
	
	


	/** 
	 * @description: check if a post allows comments to be posted
	 * @return boolean $allowed
	 * @todo: 
	 *
	 */
	function comments_enabled() {
	
		// init return
		$allowed = false;
		


		// access post object
		global $post;
		
		// do we have one?
		if ( !is_object( $post ) ) {
		
			// --<
			return $allowed;
			
		}
	


		// are comments enabled on this post?
		if ( $post->comment_status == 'open' ) {
		
			// set return
			$allowed = true;
			
		}



		// --<
		return $allowed;
	}
	
	
	
	
	


	/** 
	 * @description: get Wordpress approved comments
	 * @param integer $post_id the ID of the post
	 * @return array $comments
	 * @todo: 
	 *
	 */
	function get_approved_comments( $post_ID ) {
		
		// for Wordpress, we use the API
		$comments = get_approved_comments( $post_ID );



		// --<
		return $comments;
	}
	
	
	
	
	


	/** 
	 * @description: get all Wordpress comments for a post, unless paged
	 * @param integer $post_id the ID of the post
	 * @return array $comments
	 * @todo: 
	 *
	 */
	function get_all_comments( $post_ID ) {
	
		// access post
		global $post;

		// get all by default
		$pings = '';
		
		// check what we're allowing
		if ( is_object( $post ) AND $post->ping_status != 'open' ) {
		
			$pings = '&type=comment';

		}
		
		// for Wordpress, we use the API
		$comments = get_comments( 'post_id='.$post_ID.'&order=ASC'.$pings );
		
		
		
		// --<
		return $comments;
	}
	
	
	
	
	


	/** 
	 * @description: get all comments for a post
	 * @param integer $post_id the ID of the post
	 * @return array $comments
	 * @todo: 
	 *
	 */
	function get_comments( $post_ID ) {
	
		// database object
		global $wpdb;
		
		
		
		// get comments from db
		$comments = $wpdb->get_results(
		
			$wpdb->prepare(
			
				"SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d", 
				$post_ID
				
			)
			
		);
		

		
		// --<
		return $comments;	
	
	}
	






	/** 
	 * @description: when a comment is saved, this also saves the text signature
	 * @param integer $comment_id the ID of the comment
	 * @return boolean $result
	 * @todo: 
	 *
	 */
	function save_comment_signature( $comment_ID ) {
		
		// database object
		global $wpdb;
		
		
		// get text signature
		$text_signature = ( isset( $_POST['text_signature'] ) ) ? $_POST['text_signature'] : '';
		
		// did we get one?
		if ( $text_signature != '' ) {
		
			// escape it
			$text_signature = $wpdb->escape( $text_signature );
			
			// construct query
			$query = $wpdb->prepare(
					
				"UPDATE $wpdb->comments SET comment_signature = %s WHERE comment_ID = %d", 
				$text_signature, 
				$comment_ID
			
			);
	
			//var_dump( $query );
	
	
	
			// store comment signature
			$result = $wpdb->query( $query );
		
		} else {
		
			// set result to true... why not, eh?
			$result = true;
		
		}
		
		
		
		// --<
		return $result;
		
	}
	
	
	
	
	


	/** 
	 * @description: when a comment is saved, this also saves the page it was submitted on. this allows
	 * us to point to the correct page of a multipage post without parsing the content every time
	 * @param integer $comment_id the ID of the comment
	 * @todo: 
	 *
	 */
	function save_comment_page( $comment_ID ) {
	
		// database object
		global $wpdb;

		// is this a paged post?
		if ( isset( $_POST['page'] ) AND is_numeric( $_POST['page'] ) ) {
		
			// get text signature
			$text_signature = ( isset( $_POST['text_signature'] ) ) ? $_POST['text_signature'] : '';
			
			// is it a para-level comment?
			if ( $text_signature != '' ) {
			
				// get page number
				$page_number = $wpdb->escape( $_POST['page'] );
				
				// set key
				$key = '_cp_comment_page';
				
				// if the custom field already has a value...
				if ( get_comment_meta( $comment_ID, $key, true ) != '' ) {
				
					// update the data
					update_comment_meta( $comment_ID, $key, $page_number );
					
				} else {
				
					// add the data
					add_comment_meta( $comment_ID, $key, $page_number, true );
					
				}
	
			} else {
			
				// top level comments are always page 1
				//$page_number = 1;
				
			}
			
		}
		
	}
	
	
	
	
	


	/** 
	 * @description: retrieves text signature by comment ID
	 * @param integer $comment_id the ID of the comment
	 * @return string $text_signature
	 * @todo: 
	 *
	 */
	function get_text_signature_by_comment_id( $comment_ID ) {
	
		// database object
		global $wpdb;


		
		// query for signature
		$text_signature = $wpdb->get_var( 
		
			$wpdb->prepare(
			
				"SELECT comment_signature FROM $wpdb->comments WHERE comment_ID = %s", 
				$comment_ID
				
			) 
			
		);
		
		
		
		// --<
		return $text_signature;
		
	}
	
	
	
	
	
	

	/** 
	 * @description: store text sigs in a global - because some versions of PHP do not save properties!
	 * @param: array $sigs array of text signatures
	 * @todo: 
	 *
	 */
	function set_text_sigs( $sigs ) {
	
		global $ffffff_sigs;
	
		// store them
		$ffffff_sigs = $sigs;
		
		//var_dump( $ffffff_sigs );
		
	}
	
	
	
	
	




	/** 
	 * @description: retrieve text sigs
	 * @return array $text_signatures
	 * @todo: 
	 *
	 */
	function get_text_sigs() {
	
		global $ffffff_sigs;
	
		//var_dump( $ffffff_sigs );

		// get them
		return $ffffff_sigs;
		
	}
	
	
	
	
	



	/** 
	 * @description: get javascript for the plugin, context dependent
	 * @return string $script
	 * @todo: 
	 *
	 */
	function get_javascript_vars() {
	
		// init return
		$vars = array();
		
		
	
		// add comments open
		global $post;
		
		// if we don't have a post (like on the 404 page)
		if ( !is_object( $post ) ) {
		
			// comments must be closed
			$vars['cp_comments_open'] = 'n';

		} else {
			
			// check for post comment_status
			$vars['cp_comments_open'] = ( $post->comment_status == 'open' ) ? 'y' : 'n';
			
		}
		
		
		
		// assume no admin bars
		$vars['cp_wp_adminbar'] = 'n';
		$vars['cp_bp_adminbar'] = 'n';

		// are we showing the WP admin bar?
		if ( function_exists( 'is_admin_bar_showing' ) AND is_admin_bar_showing() ) {
			
			// we have it...
			$vars['cp_wp_adminbar'] = 'y';

		}
		
		// are we logged in AND in a BuddyPress scenario?
		if ( is_user_logged_in() AND $this->parent_obj->is_buddypress() ) {
		
			// regardless of version, settings can be made in bp-custom.php
			if ( defined( 'BP_DISABLE_ADMIN_BAR' ) AND BP_DISABLE_ADMIN_BAR ) {
			
				// we've killed both admin bars
				$vars['cp_bp_adminbar'] = 'n';
				$vars['cp_wp_adminbar'] = 'n';
	
			}
			
			// check for BP versions prior to 1.6 (1.6 uses the WP admin bar instead of a custom one)
			if ( !function_exists( 'bp_get_version' ) ) {
				
				// but, this can already be overridden in bp-custom.php
				if ( defined( 'BP_USE_WP_ADMIN_BAR' ) AND BP_USE_WP_ADMIN_BAR ) {
					
					// not present
					$vars['cp_bp_adminbar'] = 'n';
					$vars['cp_wp_adminbar'] = 'y';
					
				} else {
				
					// let our javascript know
					$vars['cp_bp_adminbar'] = 'y';
				
					// recheck 'BP_DISABLE_ADMIN_BAR'
					if ( defined( 'BP_DISABLE_ADMIN_BAR' ) AND BP_DISABLE_ADMIN_BAR ) {
					
						// we've killed both admin bars
						$vars['cp_bp_adminbar'] = 'n';
						$vars['cp_wp_adminbar'] = 'n';
			
					}
				
				}
				
			}
			
		}
		
		// add rich text editor
		$vars['cp_tinymce'] = 1;
		
		// check option
		if ( 
		
			$this->option_exists( 'cp_comment_editor' ) AND
			$this->option_get( 'cp_comment_editor' ) != '1'
			
		) {
		
			// don't add rich text editor
			$vars['cp_tinymce'] = 0;
			
		}
		
		// add mobile var
		$vars['cp_is_mobile'] = 0;

		// is it a mobile?
		if ( isset( $this->is_mobile ) AND $this->is_mobile ) {
			
			// is mobile
			$vars['cp_is_mobile'] = 1;
			
			// don't add rich text editor
			$vars['cp_tinymce'] = 0;
			
		}

		// add touch var
		$vars['cp_is_touch'] = 0;

		// is it a touch device?
		if ( isset( $this->is_mobile_touch ) AND $this->is_mobile_touch ) {
			
			// is touch
			$vars['cp_is_touch'] = 1;
			
			// don't add rich text editor
			$vars['cp_tinymce'] = 0;
			
		}
		
		// add tablet var
		$vars['cp_is_tablet'] = 0;

		// is it a touch device?
		if ( isset( $this->is_tablet ) AND $this->is_tablet ) {
			
			// is touch
			$vars['cp_is_tablet'] = 1;
			
			// don't add rich text editor
			$vars['cp_tinymce'] = 0;
			
		}
		


		// add rich text editor behaviour
		$vars['cp_promote_reading'] = 1;
		
		// check option
		if ( 
		
			$this->option_exists( 'cp_promote_reading' ) AND
			$this->option_get( 'cp_promote_reading' ) != '1'
			
		) {
		
			// promote commenting
			$vars['cp_promote_reading'] = 0;
			
		}
		
		// add special page var
		$vars['cp_special_page'] = ( $this->is_special_page() ) ? '1' : '0';

		// are we in a BuddyPress scenario?
		if ( $this->parent_obj->is_buddypress() ) {
			
			// is it a component homepage?
			if ( $this->parent_obj->is_buddypress_special_page() ) {
			
				// treat them the way we do ours
				$vars['cp_special_page'] = '1';
			
			}
			
		}
		
		// get path
		$url_info = parse_url( get_option('siteurl') );
		
		// add path for cookies
		$vars['cp_cookie_path'] = '/';
		if ( isset( $url_info['path'] ) ) {
			$vars['cp_cookie_path'] = trailingslashit( $url_info['path'] );
		}
		
		// add page
		global $page;
		$vars['cp_multipage_page'] = ( !empty( $page ) ) ? $page : 0;
		
		// add path to template directory
		$vars['cp_template_dir'] = get_template_directory_uri();
		
		// add path to plugin directory
		$vars['cp_plugin_dir'] = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__));
		
		// are chapters pages?
		$vars['cp_toc_chapter_is_page'] = $this->option_get( 'cp_toc_chapter_is_page' );
		
		// are subpages shown?
		$vars['cp_show_subpages'] = $this->option_get( 'cp_show_subpages' );
	
		// set default sidebar
		$vars['cp_default_sidebar'] = $this->parent_obj->get_default_sidebar();
		
		// set scroll speed
		$vars['cp_js_scroll_speed'] = $this->option_get( 'cp_js_scroll_speed' );
		
		// set min page width
		$vars['cp_min_page_width'] = $this->option_get( 'cp_min_page_width' );
		
		// set signup flag
		$vars['cp_is_signup_page'] = '0';
		
		// test for signup
		if ( $this->parent_obj->is_signup_page() ) {
		
			// set flag
			$vars['cp_is_signup_page'] = '1';
			
		}
		
		
		
		// --<
		return $vars;
			
	}
	
	
	
	
	
	

	/** 
	 * @description: sets class properties for mobile browsers
	 * @todo: 
	 *
	 */
	function test_for_mobile() {
	
		// do we have a user agent?
		if ( isset( $_SERVER["HTTP_USER_AGENT"] ) ) {
		
			// NOTE: keep an eye on touchphone agents
		
			// get agent
			$agent = $_SERVER["HTTP_USER_AGENT"];
			
			// init touchphone array
			$touchphones = array(
				'iPhone',
				'iPod',
				'Android',
				'BlackBerry9530',
				'LG-TU915 Obigo', // LG touch browser
				'LGE VX',
				'webOS', // Palm Pre, etc.
			);
			
			// loop through them
			foreach( $touchphones AS $phone ) {

				// test for its name in the agent string
				if ( strpos( $agent, $phone ) !== false ) {
				
					// set flag
					$this->is_mobile_touch = true;
				
				}
			
			}
			
			// the old Commentpress also includes Mobile_Detect
			if ( !class_exists( 'Mobile_Detect' ) ) {
			
				// use code from http://code.google.com/p/php-mobile-detect/
				include( COMMENTPRESS_PLUGIN_PATH . 'commentpress-core/assets/includes/mobile-detect/Mobile_Detect.php' );
			
			}
			
			// init
			$detect = new Mobile_Detect();
			
			// is it mobile?
			if ( $detect->isMobile() ) {
			
				// set flag
				$this->is_mobile = true;

			}
			
			// is it a tablet?
			if ( $detect->isTablet() ) {
			
				// set flag
				$this->is_tablet = true;

			}
			
		}

	}
	
	





//##############################################################################







	/*
	============================================================================
	PRIVATE METHODS
	============================================================================
	*/
	
	
	



	/*
	---------------------------------------------------------------
	Object Initialisation
	---------------------------------------------------------------
	*/
	
	/** 
	 * @description: object initialisation
	 * @todo:
	 *
	 */
	function _init() {
		
		// load options array
		$this->commentpress_options = $this->option_wp_get( 'commentpress_options', $this->commentpress_options );
		
		// if we don't have one
		if ( count( $this->commentpress_options ) == 0 ) {
		
			// if not in backend
			if ( !is_admin() ) {
		
				// init upgrade
				//die( 'CommentPress Core upgrade required.' );
				
			}
		
		}
		
	}







	/** 
	 * @description: create new post with content of existing
	 * @todo: 
	 *
	 */
	function _create_new_post( $post ) {
	
		// define basics
		$new_post = array(
			'post_status' => 'draft',
			'post_type' => 'post',
			'comment_status' => 'open',
			'ping_status' => 'open',
			'to_ping' => '', // quick fix for Windows
			'pinged' => '', // quick fix for Windows
			'post_content_filtered' => '', // quick fix for Windows
			'post_excerpt' => '' // quick fix for Windows
		);
		
		// add post-specific stuff
		//print_r( $post ); die();
		
		// default page title
		$prefix = __( 'Copy of ', 'commentpress-core' );

		// allow overrides of prefix
		$prefix = apply_filters( 'commentpress_new_post_title_prefix', $prefix );
		
		// set title, but allow overrides
		$new_post['post_title'] = apply_filters( 'commentpress_new_post_title', $prefix.$post->post_title, $post );
		
		// set excerpt, but allow overrides
		$new_post['post_excerpt'] = apply_filters( 'commentpress_new_post_excerpt', $post->post_excerpt );

		// set content, but allow overrides
		$new_post['post_content'] = apply_filters( 'commentpress_new_post_content', $post->post_content );

		// set post author, but allow overrides
		$new_post['post_author'] = apply_filters( 'commentpress_new_post_author', $post->post_author );
		
		
		
		// Insert the post into the database
		$new_post_id = wp_insert_post( $new_post );
		
		
		
		// --<
		return $new_post_id;

	}
	
	
	
	
	


	/** 
	 * @description: create "title" page
	 * @todo: 
	 *
	 */
	function _create_title_page() {
	
		// get the option, if it exists
		$page_exists = $this->option_get( 'cp_welcome_page' );
		
		// don't create if we already have the option set
		if ( $page_exists !== false AND is_numeric( $page_exists ) ) {
			
			// get the page (the plugin may have been deactivated, then the page deleted)
			$welcome = get_post( $page_exists );
			
			// check that the page exists 
			if ( !is_null( $welcome ) ) {
			
				// got it...
		
				// we still ought to set Wordpress internal page references
				$this->_store_wordpress_option( 'show_on_front', 'page' );
				$this->_store_wordpress_option( 'page_on_front', $page_exists );
		
				// --<
				return $page_exists;
				
			} else {
			
				// page does not exist, continue on and create it
			
			}
			
		}
		
		
		
		// define welcome/title page
		$title = array(
			'post_status' => 'publish',
			'post_type' => 'page',
			'post_parent' => 0,
			'comment_status' => 'open',
			'ping_status' => 'closed',
			'to_ping' => '', // quick fix for Windows
			'pinged' => '', // quick fix for Windows
			'post_content_filtered' => '', // quick fix for Windows
			'post_excerpt' => '', // quick fix for Windows
			'menu_order' => 0
		);
		
		// add post-specific stuff
		
		// default page title
		$_title = __( 'Title Page', 'commentpress-core' );

		// set, but allow overrides
		$title['post_title'] = apply_filters( 'cp_title_page_title', $_title );
		
		// default content
		$content = __( 
		
		'Welcome to your new CommentPress site, which allows your readers to comment paragraph-by-paragraph or line-by-line in the margins of a text. Annotate, gloss, workshop, debate: with CommentPress you can do all of these things on a finer-grained level, turning a document into a conversation.

This is your title page. Edit it to suit your needs. It has been automatically set as your homepage but if you want another page as your homepage, set it in <em>Wordpress</em> &#8594; <em>Settings</em> &#8594; <em>Reading</em>.

You can also set a number of options in <em>Wordpress</em> &#8594; <em>Settings</em> &#8594; <em>CommentPress</em> to make the site work the way you want it to. Use the Theme Customizer to change the way your site looks in <em>Wordpress</em> &#8594; <em>Appearance</em> &#8594; <em>Customize</em>. For help with structuring, formatting and reading text in CommentPress, please refer to the <a href="http://www.futureofthebook.org/commentpress/">CommentPress website</a>.', 'commentpress-core' 
			
		);
		
		// set, but allow overrides
		$title['post_content'] = apply_filters( 'cp_title_page_content', $content );

		// set template, but allow overrides
		$title['page_template'] = apply_filters( 'cp_title_page_template', 'welcome.php' );

		// Insert the post into the database
		$title_id = wp_insert_post( $title );
		
		// make sure it has the default formatter (0 = prose)
		add_post_meta( $title_id, '_cp_post_type_override', '0' );
		
		// store the option
		$this->option_set( 'cp_welcome_page', $title_id );
		
		// set Wordpress internal page references
		$this->_store_wordpress_option( 'show_on_front', 'page' );
		$this->_store_wordpress_option( 'page_on_front', $title_id );

		// --<
		return $title_id;

	}
	
	
	
	
	


	/** 
	 * @description: create "general comments" page
	 * @todo: 
	 *
	 */
	function _create_general_comments_page() {
	
		// define general comments page
		$general_comments = array(
			'post_status' => 'publish',
			'post_type' => 'page',
			'post_parent' => 0,
			'comment_status' => 'open',
			'ping_status' => 'open',
			'to_ping' => '', // quick fix for Windows
			'pinged' => '', // quick fix for Windows
			'post_content_filtered' => '', // quick fix for Windows
			'post_excerpt' => '', // quick fix for Windows
			'menu_order' => 0
		);

		// add post-specific stuff

		// default page title
		$title = __( 'General Comments', 'commentpress-core' );

		// set, but allow overrides
		$general_comments['post_title'] = apply_filters( 'cp_general_comments_title', $title );

		// default content
		$content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
		
		// set, but allow overrides
		$general_comments['post_content'] = apply_filters( 'cp_general_comments_content', $content );

		// set template, but allow overrides
		$general_comments['page_template'] = apply_filters( 'cp_general_comments_template', 'comments-general.php' );

		// Insert the post into the database
		$general_comments_id = wp_insert_post( $general_comments );
		
		// store the option
		$this->option_set( 'cp_general_comments_page', $general_comments_id );

		// --<
		return $general_comments_id;

	}
	
	
	
	
	


	/** 
	 * @description: create "all comments" page
	 * @todo: 
	 *
	 */
	function _create_all_comments_page() {
	
		// define all comments page
		$all_comments = array(
			'post_status' => 'publish',
			'post_type' => 'page',
			'post_parent' => 0,
			'comment_status' => 'closed',
			'ping_status' => 'closed',
			'to_ping' => '', // quick fix for Windows
			'pinged' => '', // quick fix for Windows
			'post_content_filtered' => '', // quick fix for Windows
			'post_excerpt' => '', // quick fix for Windows
			'menu_order' => 0
		);

		// add post-specific stuff

		// default page title
		$title = __( 'All Comments', 'commentpress-core' );

		// set, but allow overrides
		$all_comments['post_title'] = apply_filters( 'cp_all_comments_title', $title );
		
		// default content
		$content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
		
		// set, but allow overrides
		$all_comments['post_content'] = apply_filters( 'cp_all_comments_content', $content );

		// set template, but allow overrides
		$all_comments['page_template'] = apply_filters( 'cp_all_comments_template', 'comments-all.php' );

		// Insert the post into the database
		$all_comments_id = wp_insert_post( $all_comments );
		
		// store the option
		$this->option_set( 'cp_all_comments_page', $all_comments_id );

		// --<
		return $all_comments_id;

	}
	
	
	
	
	


	/** 
	 * @description: create "comments by author" page
	 * @todo: 
	 *
	 */
	function _create_comments_by_author_page() {
	
		// define comments by author page
		$group = array(
			'post_status' => 'publish',
			'post_type' => 'page',
			'post_parent' => 0,
			'comment_status' => 'closed',
			'ping_status' => 'closed',
			'to_ping' => '', // quick fix for Windows
			'pinged' => '', // quick fix for Windows
			'post_content_filtered' => '', // quick fix for Windows
			'post_excerpt' => '', // quick fix for Windows
			'menu_order' => 0
		);
		
		// add post-specific stuff

		// default page title
		$title = __( 'Comments by Commenter', 'commentpress-core' );

		// set, but allow overrides
		$group['post_title'] = apply_filters( 'cp_comments_by_title', $title );
		
		// default content
		$content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
		
		// set, but allow overrides
		$group['post_content'] = apply_filters( 'cp_comments_by_content', $content );

		// set template, but allow overrides
		$group['page_template'] = apply_filters( 'cp_comments_by_template', 'comments-by.php' );

		// Insert the post into the database
		$group_id = wp_insert_post( $group );
		
		// store the option
		$this->option_set( 'cp_comments_by_page', $group_id );

		// --<
		return $group_id;
	
	}
	
	
	
	
	


	/** 
	 * @description: create "blog" page
	 * @todo: 
	 *
	 */
	function _create_blog_page() {

		// define blog page
		$blog = array(
			'post_status' => 'publish',
			'post_type' => 'page',
			'post_parent' => 0,
			'comment_status' => 'closed',
			'ping_status' => 'closed',
			'to_ping' => '', // quick fix for Windows
			'pinged' => '', // quick fix for Windows
			'post_content_filtered' => '', // quick fix for Windows
			'post_excerpt' => '', // quick fix for Windows
			'menu_order' => 0
		);
		
		// add post-specific stuff

		// default page title
		$title = __( 'Blog', 'commentpress-core' );

		// set, but allow overrides
		$blog['post_title'] = apply_filters( 'cp_blog_page_title', $title );
		
		// default content
		$content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
		
		// set, but allow overrides
		$blog['post_content'] = apply_filters( 'cp_blog_page_content', $content );

		// set template, but allow overrides
		$blog['page_template'] = apply_filters( 'cp_blog_page_template', 'blog.php' );

		// Insert the post into the database
		$blog_id = wp_insert_post( $blog );
		
		// store the option
		$this->option_set( 'cp_blog_page', $blog_id );

		// set Wordpress internal page reference
		$this->_store_wordpress_option( 'page_for_posts', $blog_id );

		// --<
		return $blog_id;

	}
	
	
	
	
	


	/** 
	 * @description: create "blog archive" page
	 * @todo: 
	 *
	 */
	function _create_blog_archive_page() {

		// define blog page
		$blog = array(
			'post_status' => 'publish',
			'post_type' => 'page',
			'post_parent' => 0,
			'comment_status' => 'closed',
			'ping_status' => 'closed',
			'to_ping' => '', // quick fix for Windows
			'pinged' => '', // quick fix for Windows
			'post_content_filtered' => '', // quick fix for Windows
			'post_excerpt' => '', // quick fix for Windows
			'menu_order' => 0
		);
		
		// add post-specific stuff

		// default page title
		$title = __( 'Blog Archive', 'commentpress-core' );

		// set, but allow overrides
		$blog['post_title'] = apply_filters( 'cp_blog_archive_page_title', $title );
		
		// default content
		$content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
		
		// set, but allow overrides
		$blog['post_content'] = apply_filters( 'cp_blog_archive_page_content', $content );

		// set template, but allow overrides
		$blog['page_template'] = apply_filters( 'cp_blog_archive_page_template', 'archives.php' );

		// Insert the post into the database
		$blog_id = wp_insert_post( $blog );
		
		// store the option
		$this->option_set( 'cp_blog_archive_page', $blog_id );

		// --<
		return $blog_id;

	}
	
	
	
	
	


	/** 
	 * @description: create "table of contents" page
	 * @todo: NOT USED
	 *
	 */
	function _create_toc_page() {
	
		// define TOC page
		$toc = array(
			'post_status' => 'publish',
			'post_type' => 'page',
			'post_parent' => 0,
			'comment_status' => 'closed',
			'ping_status' => 'closed',
			'to_ping' => '', // quick fix for Windows
			'pinged' => '', // quick fix for Windows
			'post_content_filtered' => '', // quick fix for Windows
			'post_excerpt' => '', // quick fix for Windows
			'menu_order' => 0
		);
		
		// default page title
		$title = __( 'Table of Contents', 'commentpress-core' );

		// set, but allow overrides
		$toc['post_title'] = apply_filters( 'cp_toc_page_title', $title );
		
		// default content
		$content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
		
		// set, but allow overrides
		$toc['post_content'] = apply_filters( 'cp_toc_page_content', $content );

		// set template, but allow overrides
		$toc['page_template'] = apply_filters( 'cp_toc_page_template', 'toc.php' );

		// Insert the post into the database
		$toc_id = wp_insert_post( $toc );
		
		// store the option
		$this->option_set( 'cp_toc_page', $toc_id );

		// --<
		return $toc_id;
	
	}
	
	
	
	
	


	/** 
	 * @description: cancels comment paging because CP will not work with comment paging
	 * @todo: 
	 */
	function _cancel_comment_paging() {
	
		// store option
		$this->_store_wordpress_option( 'page_comments', '' );
	
	}
	
	
	
	
	
	
	/** 
	 * @description: resets comment paging option when plugin is deactivated
	 * @todo: 
	 */
	function _reset_comment_paging() {
	
		// reset option
		$this->_reset_wordpress_option( 'page_comments' );
	
	}
	
	
	
	
	
	
	/** 
	 * @description: clears widgets for a fresh start
	 * @todo: 
	 */
	function _clear_widgets() {
	
		// set backup option
		add_option( 'commentpress_sidebars_widgets', $this->option_wp_get( 'sidebars_widgets' ) );

		// clear them
		update_option( 'sidebars_widgets', null );

	}
	
	
	
	
	
	
	/** 
	 * @description: resets widgets when plugin is deactivated
	 * @todo: 
	 */
	function _reset_widgets() {
	
		// reset option
		$this->_reset_wordpress_option( 'sidebars_widgets' );
	
	}
	
	
	
	
	
	
	/** 
	 * @description: store Wordpress option
	 * @param string $name the name of the option
	 * @param mixed $value the value of the option
	 * @todo: 
	 */
	function _store_wordpress_option( $name, $value ) {
	
		// set backup option
		add_option( 'commentpress_'.$name, $this->option_wp_get( $name ) );

		// set the Wordpress option
		$this->option_wp_set( $name, $value );
	
	}
	
	
	
	
	
	
	/** 
	 * @description: reset Wordpress option
	 * @param string $name the name of the option
	 * @todo: 
	 */
	function _reset_wordpress_option( $name ) {
	
		// set the Wordpress option
		$this->option_wp_set( $name, $this->option_wp_get( 'cp_'.$name ) );
	
		// remove backup option
		delete_option( 'commentpress_'.$name );

	}
	
	
	
	
	
	
	/** 
	 * @description: create all basic CommentPress Core options
	 * @todo:
	 *
	 */
	function _options_create() {
	
		// init options array
		$this->commentpress_options = array(
		
			'cp_show_posts_or_pages_in_toc' => $this->toc_content,
			'cp_toc_chapter_is_page' => $this->toc_chapter_is_page,
			'cp_show_subpages' => $this->show_subpages,
			'cp_show_extended_toc' => $this->show_extended_toc,
			'cp_title_visibility' => $this->title_visibility,
			'cp_page_meta_visibility' => $this->page_meta_visibility,
			'cp_header_bg_colour' => $this->header_bg_colour,
			'cp_js_scroll_speed' => $this->js_scroll_speed,
			'cp_min_page_width' => $this->min_page_width,
			'cp_comment_editor' => $this->comment_editor,
			'cp_promote_reading' => $this->promote_reading,
			'cp_excerpt_length' => $this->excerpt_length,
			'cp_para_comments_live' => $this->para_comments_live,
			'cp_blog_type' => $this->blog_type,
			'cp_blog_workflow' => $this->blog_workflow,
			'cp_sidebar_default' => $this->sidebar_default
		
		);

		// Paragraph-level comments enabled by default
		add_option( 'commentpress_options', $this->commentpress_options );
		
	}
	
	
	
	
	


	/** 
	 * @description: reset CommentPress Core options
	 * @todo: 
	 *
	 */
	function _options_reset() {
		
		// TOC: show posts by default
		$this->option_set( 'cp_show_posts_or_pages_in_toc', $this->toc_content );

		// TOC: are chapters pages
		$this->option_set( 'cp_toc_chapter_is_page', $this->toc_chapter_is_page );

		// TOC: if pages are shown, show subpages by default
		$this->option_set( 'cp_show_subpages', $this->show_subpages );
		
		// TOC: show extended post list
		$this->option_set( 'cp_show_extended_toc', $this->show_extended_toc );

		// comment editor
		$this->option_set( 'cp_comment_editor', $this->comment_editor );

		// promote reading or commenting
		$this->option_set( 'cp_promote_reading', $this->promote_reading );

		// show or hide titles
		$this->option_set( 'cp_title_visibility', $this->title_visibility );

		// show or hide page meta
		$this->option_set( 'cp_page_meta_visibility', $this->page_meta_visibility );

		// header background colour
		$this->option_set( 'cp_header_bg_colour', $this->header_bg_colour );

		// js scroll speed
		$this->option_set( 'cp_js_scroll_speed', $this->js_scroll_speed );

		// minimum page width
		$this->option_set( 'cp_min_page_width', $this->min_page_width );

		// "live" comment refeshing
		$this->option_set( 'cp_para_comments_live', $this->para_comments_live );

		// Blog: excerpt length
		$this->option_set( 'cp_excerpt_length', $this->excerpt_length );
		
		// workflow
		$this->option_set( 'cp_blog_workflow', $this->blog_workflow );
		
		// blog type
		$this->option_set( 'cp_blog_type', $this->blog_type );
		
		// default sidebar
		$this->option_set( 'cp_sidebar_default', $this->sidebar_default );
		
		// store it
		$this->options_save();
		
	}
	
	
	
	
	


	/** 
	 * @description: migrate all CommentPress Core options from old plugin
	 * @todo:
	 *
	 */
	function _options_migrate() {
	
		// get existing options
		$old = get_option( 'cp_options', array() );
		
		
		
		// ---------------------------------------------------------------------
		// retrieve new ones, if they exist, or use defaults otherwise
		// ---------------------------------------------------------------------
		$this->toc_content = 			isset( $old[ 'cp_show_posts_or_pages_in_toc' ] ) ?
										$old[ 'cp_show_posts_or_pages_in_toc' ] :
										$this->toc_content;
	
		$this->toc_chapter_is_page = 	isset( $old[ 'cp_toc_chapter_is_page' ] ) ?
										$old[ 'cp_toc_chapter_is_page' ] :
										$this->toc_chapter_is_page;
	
		$this->show_subpages =		 	isset( $old[ 'cp_show_subpages' ] ) ?
										$old[ 'cp_show_subpages' ] :
										$this->show_subpages;
		
		$this->show_extended_toc = 		isset( $old[ 'cp_show_extended_toc' ] ) ?
										$old[ 'cp_show_extended_toc' ] :
										$this->show_extended_toc;
	
		$this->title_visibility =	 	isset( $old[ 'cp_title_visibility' ] ) ?
										$old[ 'cp_title_visibility' ] :
										$this->title_visibility;
	
		$this->page_meta_visibility = 	isset( $old[ 'cp_page_meta_visibility' ] ) ?
										$old[ 'cp_page_meta_visibility' ] :
										$this->page_meta_visibility;
		
		// header background colour
		$header_bg_colour =	 			isset( $old[ 'cp_header_bg_colour' ] ) ?
										$old[ 'cp_header_bg_colour' ] :
										$this->header_bg_colour;
		
		// if it's the old default, upgrade to new default
		if ( $header_bg_colour == '819565' ) {
			$header_bg_colour = $this->header_bg_colour;
		}
		
		$this->js_scroll_speed =	 	isset( $old[ 'cp_js_scroll_speed' ] ) ?
										$old[ 'cp_js_scroll_speed' ] :
										$this->js_scroll_speed;
	
		$this->min_page_width =		 	isset( $old[ 'cp_min_page_width' ] ) ?
										$old[ 'cp_min_page_width' ] :
										$this->min_page_width;
	
		$this->comment_editor =		 	isset( $old[ 'cp_comment_editor' ] ) ?
										$old[ 'cp_comment_editor' ] :
										$this->comment_editor;
	
		$this->promote_reading =	 	isset( $old[ 'cp_promote_reading' ] ) ?
										$old[ 'cp_promote_reading' ] :
										$this->promote_reading;
	
		$this->excerpt_length =		 	isset( $old[ 'cp_excerpt_length' ] ) ?
										$old[ 'cp_excerpt_length' ] :
										$this->excerpt_length;
	
		$this->para_comments_live = 	isset( $old[ 'cp_para_comments_live' ] ) ?
										$old[ 'cp_para_comments_live' ] :
										$this->para_comments_live;
	
		$blog_type = 					isset( $old[ 'cp_blog_type' ] ) ?
										$old[ 'cp_blog_type' ] :
										$this->blog_type;
	
		$blog_workflow =		 		isset( $old[ 'cp_blog_workflow' ] ) ?
										$old[ 'cp_blog_workflow' ] :
										$this->blog_workflow;
	
		$this->sidebar_default =	 	isset( $old[ 'cp_sidebar_default' ] ) ?
										$old[ 'cp_sidebar_default' ] :
										$this->sidebar_default;
		


		// ---------------------------------------------------------------------
		// special pages
		// ---------------------------------------------------------------------
		$special_pages =	 		isset( $old[ 'cp_special_pages' ] ) ?
									$old[ 'cp_special_pages' ] :
									null;

		$blog_page =		 		isset( $old[ 'cp_blog_page' ] ) ?
									$old[ 'cp_blog_page' ] :
									null;

		$blog_archive_page = 		isset( $old[ 'cp_blog_archive_page' ] ) ?
									$old[ 'cp_blog_archive_page' ] :
									null;

		$general_comments_page =	isset( $old[ 'cp_general_comments_page' ] ) ?
									$old[ 'cp_general_comments_page' ] :
									null;

		$all_comments_page = 		isset( $old[ 'cp_all_comments_page' ] ) ?
									$old[ 'cp_all_comments_page' ] :
									null;

		$comments_by_page = 		isset( $old[ 'cp_comments_by_page' ] ) ?
									$old[ 'cp_comments_by_page' ] :
									null;

		$toc_page =		 			isset( $old[ 'cp_toc_page' ] ) ?
									$old[ 'cp_toc_page' ] :
									null;
		


		// init options array
		$this->commentpress_options = array(
			
			// basic options
			'cp_show_posts_or_pages_in_toc' => $this->toc_content,
			'cp_toc_chapter_is_page' => $this->toc_chapter_is_page,
			'cp_show_subpages' => $this->show_subpages,
			'cp_show_extended_toc' => $this->show_extended_toc,
			'cp_title_visibility' => $this->title_visibility,
			'cp_page_meta_visibility' => $this->page_meta_visibility,
			'cp_header_bg_colour' => $header_bg_colour,
			'cp_js_scroll_speed' => $this->js_scroll_speed,
			'cp_min_page_width' => $this->min_page_width,
			'cp_comment_editor' => $this->comment_editor,
			'cp_promote_reading' => $this->promote_reading,
			'cp_excerpt_length' => $this->excerpt_length,
			'cp_para_comments_live' => $this->para_comments_live,
			'cp_blog_type' => $blog_type,
			'cp_blog_workflow' => $blog_workflow,
			'cp_sidebar_default' => $this->sidebar_default
			
		);
			
		
		
		// if we have special pages
		if ( !is_null( $special_pages ) AND is_array( $special_pages ) ) {
		
			// let's have them as well
			$pages = array( 
			
				// special pages
				'cp_special_pages' => $special_pages,
				'cp_blog_page' => $blog_page,
				'cp_blog_archive_page' => $blog_archive_page,
				'cp_general_comments_page' => $general_comments_page,
				'cp_all_comments_page' => $all_comments_page,
				'cp_comments_by_page' => $comments_by_page,
				'cp_toc_page' => $toc_page
			
			);
			
			// merge
			$this->commentpress_options = array_merge( $this->commentpress_options, $pages );
			
			
			
			// access old plugin
			global $commentpress_obj;
			
			// did we get it?
			if ( is_object( $commentpress_obj ) ) {
			
				// now delete the old Commentpress options
				$commentpress_obj->db->option_delete( 'cp_special_pages' );
				$commentpress_obj->db->option_delete( 'cp_blog_page' );
				$commentpress_obj->db->option_delete( 'cp_blog_archive_page' );
				$commentpress_obj->db->option_delete( 'cp_general_comments_page' );
				$commentpress_obj->db->option_delete( 'cp_all_comments_page' );
				$commentpress_obj->db->option_delete( 'cp_comments_by_page' );
				$commentpress_obj->db->option_delete( 'cp_toc_page' );
				
				// save changes
				$commentpress_obj->db->options_save();
				
				// extra page options: get existing backup, delete it, create new backup
				
				// backup blog page reference
				$page_for_posts = get_option( 'cp_page_for_posts' );
				delete_option( 'cp_page_for_posts' );
				add_option( 'commentpress_page_for_posts', $page_for_posts );
				
			}
			
		}
		


		// ---------------------------------------------------------------------
		// welcome page
		// ---------------------------------------------------------------------
		
		// welcome page is a bit of an oddity: deal with it here...
		$welcome_page =	isset( $old[ 'cp_welcome_page' ] ) ? $old[ 'cp_welcome_page' ] : null;
		
		// did we get a welcome page?
		if ( !is_null( $welcome_page ) ) {
		
			// if the custom field already has a value...
			if ( get_post_meta( $welcome_page, '_cp_post_type_override', true ) !== '' ) {
			
				// leave the selected formatter alone
				
			} else {
			
				// make sure it has the default formatter (0 = prose)
				add_post_meta( $welcome_page, '_cp_post_type_override', '0' );
				
			}
			
			// add it to our options
			$this->commentpress_options['cp_welcome_page'] = $welcome_page;
			
		}
		
		
		
		// add the options to WordPress
		add_option( 'commentpress_options', $this->commentpress_options );
		
		
		
		// ---------------------------------------------------------------------
		// backups
		// ---------------------------------------------------------------------

		// backup what to show as homepage
		$show_on_front = get_option( 'cp_show_on_front' );
		delete_option( 'cp_show_on_front' );
		add_option( 'commentpress_show_on_front', $show_on_front );

		// backup homepage id
		$page_on_front = get_option( 'cp_page_on_front' );
		delete_option( 'cp_page_on_front' );
		add_option( 'commentpress_page_on_front', $page_on_front );

		// backup comment paging
		$page_comments = get_option( 'cp_page_comments' );
		delete_option( 'cp_page_comments' );
		add_option( 'commentpress_page_comments', $page_comments );
		
		
		
		// ---------------------------------------------------------------------
		// Theme Customizations
		// ---------------------------------------------------------------------

		// migrate Theme Customizations
		$theme_settings = get_option( 'cp_theme_settings', array() );
		
		// did we get any?
		if ( !empty( $theme_settings ) ) {
		
			// migrate them
			add_option( 'commentpress_theme_settings', $theme_settings );
			
		}

		// migrate Theme Mods
		$theme_mods = get_option( 'theme_mods_commentpress', array() );

		// did we get any?
		if ( is_array( $theme_mods ) AND count( $theme_mods ) > 0 ) {
		
			// get header background image...
			if ( isset( $theme_mods['header_image'] ) ) {
				
				//print_r( 'here' ); die();

				// is it a Commentpress one?
				if ( strstr( $theme_mods['header_image'], 'style/images/header/caves.jpg' ) !== false ) {
				
					// point it at the equivalent new version
					$theme_mods['header_image'] = COMMENTPRESS_PLUGIN_URL.
												  'themes/commentpress-theme'.
												  '/assets/images/header/caves-green.jpg';
					
				}
				
			}
			
			/*
			// if we wanted to clear widgets widgets...
			if ( isset( $theme_mods['sidebars_widgets'] ) ) {
				
				// remove them
				unset( $theme_mods['sidebars_widgets'] );
			
			}
	
			// override widgets
			$this->_clear_widgets();
			*/
			
			// update theme mods (will create if it doesn't exist)
			update_option( 'theme_mods_commentpress-theme', $theme_mods );
						
		}
		
		
		

		// ---------------------------------------------------------------------
		// deactivate old Commentpress and Commentpress Ajaxified
		// ---------------------------------------------------------------------
		
		// get old Commentpress Ajaxified
		$cpajax_old = commentpress_find_plugin_by_name( 'Commentpress Ajaxified' );
		if ( $cpajax_old AND is_plugin_active( $cpajax_old ) ) { deactivate_plugins( $cpajax_old ); }
		
		// get old Commentpress
		$cp_old = commentpress_find_plugin_by_name( 'Commentpress' );
		if ( $cp_old AND is_plugin_active( $cp_old ) ) { deactivate_plugins( $cp_old ); }
		
	}
	
	
	
	
	

	/** 
	 * @description: upgrade Commentpress options to array (only for pre-CP3.2 upgrades)
	 * @todo: 
	 *
	 */
	function _options_upgrade() {
	
		// populate options array with current values
		$this->commentpress_options = array(
			
			// theme settings we want to keep
			'cp_show_posts_or_pages_in_toc' => $this->option_wp_get( 'cp_show_posts_or_pages_in_toc' ),
			'cp_toc_chapter_is_page' => $this->option_wp_get( 'cp_toc_chapter_is_page'),
			'cp_show_subpages' => $this->option_wp_get( 'cp_show_subpages'),
			'cp_excerpt_length' => $this->option_wp_get( 'cp_excerpt_length'),
			
			// migrate special pages
			'cp_special_pages' => $this->option_wp_get( 'cp_special_pages'),
			'cp_welcome_page' => $this->option_wp_get( 'cp_welcome_page'),
			'cp_general_comments_page' => $this->option_wp_get( 'cp_general_comments_page'),
			'cp_all_comments_page' => $this->option_wp_get( 'cp_all_comments_page'),
			'cp_comments_by_page' => $this->option_wp_get( 'cp_comments_by_page'),
			'cp_blog_page' => $this->option_wp_get( 'cp_blog_page'),
			
			// store setting for what was independently set by the ajax commenting plugin, "off" by default
			'cp_para_comments_live' => $this->para_comments_live
		
		);
		
		// save options array
		$this->options_save();
		
		// delete all old options
		$this->_options_delete_legacy();
		
	}
	
	
	
	
	


	/** 
	 * @description: delete all legacy Commentpress options
	 * @todo: 
	 *
	 */
	function _options_delete_legacy() {

		// delete paragraph-level commenting option
		delete_option( 'cp_para_comments_enabled' );
		
		// delete TOC options
		delete_option( 'cp_show_posts_or_pages_in_toc' );
		delete_option( 'cp_show_subpages' );
		delete_option( 'cp_toc_chapter_is_page' );
		
		// delete comment editor
		delete_option( 'cp_comment_editor' );
		
		// promote reading or commenting
		delete_option( 'cp_promote_reading' );
		
		// show or hide titles
		delete_option( 'cp_title_visibility' );
		
		// header bg colour
		delete_option( 'cp_header_bg_colour' );
		
		// header bg colour
		delete_option( 'cp_js_scroll_speed' );
		
		// header bg colour
		delete_option( 'cp_min_page_width' );
		
		// delete skin
		delete_option( 'cp_default_skin' );
		
		// window appearance options
		delete_option( 'cp_default_left_position' );
		delete_option( 'cp_default_top_position' );
		delete_option( 'cp_default_width' );
		delete_option( 'cp_default_height' );

		// window behaviour options		
		delete_option( 'cp_allow_users_to_iconize' );
		delete_option( 'cp_allow_users_to_minimize' );
		delete_option( 'cp_allow_users_to_resize' );
		delete_option( 'cp_allow_users_to_drag' );
		delete_option( 'cp_allow_users_to_save_position' );

		// blog options
		delete_option( 'cp_excerpt_length' );
		
		// "live" comment refreshing
		delete_option( 'cp_para_comments_live' );
		
		// special pages options
		delete_option( 'cp_special_pages' );
		delete_option( 'cp_welcome_page' );
		delete_option( 'cp_general_comments_page' );
		delete_option( 'cp_all_comments_page' );
		delete_option( 'cp_comments_by_page' );
		delete_option( 'cp_blog_page' );

	}
	
	
	
	
	


//##############################################################################







} // class ends






?>
Back to Top