PageRenderTime 61ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://github.com/DigitalPaulScholtenProject/DPSP-Platform
PHP | 4316 lines | 1358 code | 1731 blank | 1227 comment | 244 complexity | 21ee3acc0aca0c858965c056b799cec3 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php /*
  2. ================================================================================
  3. Class CommentpressCoreDatabase
  4. ================================================================================
  5. AUTHOR: Christian Wach <needle@haystack.co.uk>
  6. --------------------------------------------------------------------------------
  7. NOTES
  8. =====
  9. This class is a wrapper for the majority of database operations.
  10. --------------------------------------------------------------------------------
  11. */
  12. /*
  13. ================================================================================
  14. Class Name
  15. ================================================================================
  16. */
  17. class CommentpressCoreDatabase {
  18. /*
  19. ============================================================================
  20. Properties
  21. ============================================================================
  22. */
  23. // parent object reference
  24. var $parent_obj;
  25. // standard mobile browser
  26. var $is_mobile = false;
  27. // touch-based mobile browser
  28. var $is_mobile_touch = false;
  29. // touch-based tablet browser
  30. var $is_tablet = false;
  31. // ---------- options ----------
  32. var $commentpress_options = array();
  33. // TOC content ('post' or 'page')
  34. var $toc_content = 'page';
  35. // TOC chapters are pages by default
  36. var $toc_chapter_is_page = 1;
  37. // Show extended TOC content for posts lists
  38. var $show_extended_toc = 1;
  39. // TOC shows subpages by default
  40. var $show_subpages = 1;
  41. // show page titles by default
  42. var $title_visibility = 'show';
  43. // hide page meta by default
  44. var $page_meta_visibility = 'hide';
  45. // default editor (tinyMCE)
  46. var $comment_editor = 1;
  47. // promote reading (1) or commenting (0)
  48. var $promote_reading = 0;
  49. // default excerpt length
  50. var $excerpt_length = 55;
  51. // default header background colour (hex, same as in layout.css)
  52. var $header_bg_colour = '2c2622';
  53. // default scroll speed (ms)
  54. var $js_scroll_speed = '800';
  55. // default type of blog - eg, array('0' => 'Poetry','1' => 'Prose')
  56. var $blog_type = false;
  57. // default blog workflow (like translation, for example), off by default
  58. var $blog_workflow = 0;
  59. // default sidebar ('toc' => Contents tab)
  60. var $sidebar_default = 'toc';
  61. // default minimum page width (px)
  62. var $min_page_width = '447';
  63. // "live" comment refreshing off by default
  64. var $para_comments_live = 0;
  65. // prevent save_post hook firing more than once
  66. var $saved_post = false;
  67. /**
  68. * @description: initialises this object
  69. * @param object $parent_obj a reference to the parent object
  70. * @return object
  71. * @todo:
  72. *
  73. */
  74. function __construct( $parent_obj ) {
  75. // store reference to parent
  76. $this->parent_obj = $parent_obj;
  77. // init
  78. $this->_init();
  79. // --<
  80. return $this;
  81. }
  82. /**
  83. * @description: PHP 4 constructor
  84. */
  85. function CommentpressCoreDatabase( $parent_obj ) {
  86. // is this php5?
  87. if ( version_compare( PHP_VERSION, "5.0.0", "<" ) ) {
  88. // call php5 constructor
  89. $this->__construct( $parent_obj );
  90. }
  91. // --<
  92. return $this;
  93. }
  94. /**
  95. * @description: set up all items associated with this object
  96. * @todo:
  97. *
  98. */
  99. function activate() {
  100. // have we already got a modified database?
  101. $modified = $this->db_is_modified( 'comment_text_signature' ) ? 'y' : 'n';
  102. // if we have an existing comment_text_signature column...
  103. if ( $modified == 'y' ) {
  104. // upgrade old Commentpress schema to new
  105. if ( !$this->schema_upgrade() ) {
  106. // kill plugin activation
  107. _cpdie( 'CommentPress Core Error: could not upgrade the database' );
  108. }
  109. } else {
  110. // update db schema
  111. $this->schema_update();
  112. }
  113. // test if we have our version
  114. if ( !$this->option_wp_get( 'commentpress_version' ) ) {
  115. // store CommentPress Core version
  116. $this->option_wp_set( 'commentpress_version', COMMENTPRESS_VERSION );
  117. }
  118. // test that we aren't reactivating
  119. if ( !$this->option_wp_get( 'commentpress_options' ) ) {
  120. // test if we have a existing pre-3.4 Commentpress instance
  121. if ( commentpress_is_legacy_plugin_active() ) {
  122. // yes: add options with existing values
  123. $this->_options_migrate();
  124. } else {
  125. // no: add options with default values
  126. $this->_options_create();
  127. }
  128. }
  129. // retrieve data on special pages
  130. $special_pages = $this->option_get( 'cp_special_pages', array() );
  131. // if we haven't created any...
  132. if ( count( $special_pages ) == 0 ) {
  133. // create special pages
  134. $this->create_special_pages();
  135. }
  136. // turn comment paging option off
  137. $this->_cancel_comment_paging();
  138. // override widgets
  139. $this->_clear_widgets();
  140. }
  141. /**
  142. * @description: upgrade Commentpress plugin from 3.1 options to latest set
  143. * @return boolean $result
  144. * @todo:
  145. *
  146. */
  147. function upgrade() {
  148. // database object
  149. global $wpdb;
  150. // init return
  151. $result = false;
  152. // if we have a commentpress install (or we're forcing)
  153. if ( $this->check_upgrade() ) {
  154. // are we missing the commentpress_options option?
  155. if ( !$this->option_wp_exists( 'commentpress_options' ) ) {
  156. // upgrade to the single array
  157. $this->_options_upgrade();
  158. }
  159. // checkboxes send no value if not checked, so use a default
  160. $cp_blog_workflow = $this->blog_workflow;
  161. // default blog type
  162. $cp_blog_type = $this->blog_type;
  163. // get variables
  164. extract( $_POST );
  165. // Removed in CP 3.4 - do we still have the legacy cp_para_comments_enabled option?
  166. if ( $this->option_exists( 'cp_para_comments_enabled' ) ) {
  167. // delete old cp_para_comments_enabled option
  168. $this->option_delete( 'cp_para_comments_enabled' );
  169. }
  170. // Removed in CP 3.4 - do we still have the legacy cp_minimise_sidebar option?
  171. if ( $this->option_exists( 'cp_minimise_sidebar' ) ) {
  172. // delete old cp_minimise_sidebar option
  173. $this->option_delete( 'cp_minimise_sidebar' );
  174. }
  175. // New in CP 3.4 - has AJAX "live" comment refreshing been migrated?
  176. if ( !$this->option_exists( 'cp_para_comments_live' ) ) {
  177. // "live" comment refreshing, off by default
  178. $this->option_set( 'cp_para_comments_live', $this->para_comments_live );
  179. }
  180. // New in CP 3.3.3 - changed the way the welcome page works
  181. if ( $this->option_exists( 'cp_special_pages' ) ) {
  182. // do we have the cp_welcome_page option?
  183. if ( $this->option_exists( 'cp_welcome_page' ) ) {
  184. // get it
  185. $page_id = $this->option_get( 'cp_welcome_page' );
  186. // retrieve data on special pages
  187. $special_pages = $this->option_get( 'cp_special_pages', array() );
  188. // is it in our special pages array?
  189. if ( in_array( $page_id, $special_pages ) ) {
  190. // remove page id from array
  191. $special_pages = array_diff( $special_pages, array( $page_id ) );
  192. // reset option
  193. $this->option_set( 'cp_special_pages', $special_pages );
  194. }
  195. }
  196. }
  197. // New in CP 3.3.3 - are we missing the cp_sidebar_default option?
  198. if ( !$this->option_exists( 'cp_sidebar_default' ) ) {
  199. // get choice
  200. $_choice = $wpdb->escape( $cp_sidebar_default );
  201. // add chosen cp_page_meta_visibility option
  202. $this->option_set( 'cp_sidebar_default', $_choice );
  203. }
  204. // New in CP 3.3.2 - are we missing the cp_page_meta_visibility option?
  205. if ( !$this->option_exists( 'cp_page_meta_visibility' ) ) {
  206. // get choice
  207. $_choice = $wpdb->escape( $cp_page_meta_visibility );
  208. // add chosen cp_page_meta_visibility option
  209. $this->option_set( 'cp_page_meta_visibility', $_choice );
  210. }
  211. // New in CP 3.3.1 - are we missing the cp_blog_workflow option?
  212. if ( !$this->option_exists( 'cp_blog_workflow' ) ) {
  213. // get choice
  214. $_choice = $wpdb->escape( $cp_blog_workflow );
  215. // add chosen cp_blog_workflow option
  216. $this->option_set( 'cp_blog_workflow', $_choice );
  217. }
  218. // New in CP 3.3.1 - are we missing the cp_blog_type option?
  219. if ( !$this->option_exists( 'cp_blog_type' ) ) {
  220. // get choice
  221. $_choice = $wpdb->escape( $cp_blog_type );
  222. // add chosen cp_blog_type option
  223. $this->option_set( 'cp_blog_type', $_choice );
  224. }
  225. // New in CP 3.3 - are we missing the cp_show_extended_toc option?
  226. if ( !$this->option_exists( 'cp_show_extended_toc' ) ) {
  227. // get choice
  228. $_choice = $wpdb->escape( $cp_show_extended_toc );
  229. // add chosen cp_show_extended_toc option
  230. $this->option_set( 'cp_show_extended_toc', $_choice );
  231. }
  232. // are we missing the cp_comment_editor option?
  233. if ( !$this->option_exists( 'cp_comment_editor' ) ) {
  234. // get choice
  235. $_choice = $wpdb->escape( $cp_comment_editor );
  236. // add chosen cp_comment_editor option
  237. $this->option_set( 'cp_comment_editor', $_choice );
  238. }
  239. // are we missing the cp_promote_reading option?
  240. if ( !$this->option_exists( 'cp_promote_reading' ) ) {
  241. // get choice
  242. $_choice = $wpdb->escape( $cp_promote_reading );
  243. // add chosen cp_promote_reading option
  244. $this->option_set( 'cp_promote_reading', $_choice );
  245. }
  246. // are we missing the cp_title_visibility option?
  247. if ( !$this->option_exists( 'cp_title_visibility' ) ) {
  248. // get choice
  249. $_choice = $wpdb->escape( $cp_title_visibility );
  250. // add chosen cp_title_visibility option
  251. $this->option_set( 'cp_title_visibility', $_choice );
  252. }
  253. // are we missing the cp_header_bg_colour option?
  254. if ( !$this->option_exists( 'cp_header_bg_colour' ) ) {
  255. // get choice
  256. $_choice = $wpdb->escape( $cp_header_bg_colour );
  257. // strip our rgb #
  258. if ( stristr( $_choice, '#' ) ) {
  259. $_choice = substr( $_choice, 1 );
  260. }
  261. // reset to default if blank
  262. if ( $_choice == '' ) {
  263. $_choice = $this->header_bg_colour;
  264. }
  265. // add chosen cp_header_bg_colour option
  266. $this->option_set( 'cp_header_bg_colour', $_choice );
  267. }
  268. // are we missing the cp_js_scroll_speed option?
  269. if ( !$this->option_exists( 'cp_js_scroll_speed' ) ) {
  270. // get choice
  271. $_choice = $wpdb->escape( $cp_js_scroll_speed );
  272. // add chosen cp_js_scroll_speed option
  273. $this->option_set( 'cp_js_scroll_speed', $_choice );
  274. }
  275. // are we missing the cp_min_page_width option?
  276. if ( !$this->option_exists( 'cp_min_page_width' ) ) {
  277. // get choice
  278. $_choice = $wpdb->escape( $cp_min_page_width );
  279. // add chosen cp_min_page_width option
  280. $this->option_set( 'cp_min_page_width', $_choice );
  281. }
  282. // do we still have the legacy cp_allow_users_to_minimize option?
  283. if ( $this->option_exists( 'cp_allow_users_to_minimize' ) ) {
  284. // delete old cp_allow_users_to_minimize option
  285. $this->option_delete( 'cp_allow_users_to_minimize' );
  286. }
  287. // do we have special pages?
  288. if ( $this->option_exists( 'cp_special_pages' ) ) {
  289. // if we don't have the toc page...
  290. if ( !$this->option_exists( 'cp_toc_page' ) ) {
  291. // get special pages array
  292. $special_pages = $this->option_get( 'cp_special_pages', array() );
  293. // create TOC page -> a convenience, let's us define a logo as attachment
  294. $special_pages[] = $this->_create_toc_page();
  295. // store the array of page IDs that were created
  296. $this->option_set( 'cp_special_pages', $special_pages );
  297. }
  298. }
  299. // save new CommentPress Core options
  300. $this->options_save();
  301. // store new CommentPress Core version
  302. $this->option_wp_set( 'commentpress_version', COMMENTPRESS_VERSION );
  303. }
  304. // --<
  305. return $result;
  306. }
  307. /**
  308. * @description: reset Wordpress to prior state, but retain options
  309. * @todo:
  310. *
  311. */
  312. function deactivate() {
  313. // reset comment paging option
  314. $this->_reset_comment_paging();
  315. // restore widgets
  316. $this->_reset_widgets();
  317. // always remove special pages
  318. $this->delete_special_pages();
  319. }
  320. //##############################################################################
  321. /*
  322. ============================================================================
  323. PUBLIC METHODS
  324. ============================================================================
  325. */
  326. /**
  327. * @description: update Wordpress database schema
  328. * @return boolean $result
  329. * @todo:
  330. *
  331. */
  332. function schema_update() {
  333. // database object
  334. global $wpdb;
  335. // include Wordpress upgrade script
  336. require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
  337. // add the column, if not already there
  338. $result = maybe_add_column(
  339. $wpdb->comments,
  340. 'comment_signature',
  341. "ALTER TABLE `$wpdb->comments` ADD `comment_signature` VARCHAR(255) NULL;"
  342. );
  343. // --<
  344. return $result;
  345. }
  346. /**
  347. * @description: upgrade Wordpress database schema
  348. * @return boolean $result
  349. * @todo:
  350. *
  351. */
  352. function schema_upgrade() {
  353. // database object
  354. global $wpdb;
  355. // init
  356. $return = false;
  357. // construct query
  358. $query = "ALTER TABLE `$wpdb->comments` CHANGE `comment_text_signature` `comment_signature` VARCHAR(255) NULL;";
  359. // do the query to rename the column
  360. $wpdb->query( $query );
  361. // test if we now have the correct column name...
  362. if ( $this->db_is_modified( 'comment_signature' ) ) {
  363. // yes
  364. $result = true;
  365. }
  366. // --<
  367. return $result;
  368. }
  369. /**
  370. * @description: do we have a column in the comments table?
  371. * @return boolean $result
  372. * @todo:
  373. *
  374. */
  375. function db_is_modified( $column_name ) {
  376. // database object
  377. global $wpdb;
  378. // init
  379. $result = false;
  380. // define query
  381. $query = "DESCRIBE $wpdb->comments";
  382. // get columns
  383. $cols = $wpdb->get_results( $query );
  384. // loop
  385. foreach( $cols AS $col ) {
  386. // is it our desired column?
  387. if ( $col->Field == $column_name ) {
  388. // we got it
  389. $result = true;
  390. break;
  391. }
  392. }
  393. // --<
  394. return $result;
  395. }
  396. /**
  397. * @description: check for plugin upgrade
  398. * @return boolean $result
  399. * @todo:
  400. *
  401. */
  402. function check_upgrade() {
  403. // init
  404. $result = false;
  405. // get installed version cast as string
  406. $_version = (string) $this->option_wp_get( 'commentpress_version' );
  407. // if we have a commentpress install and it's lower than this one
  408. if ( $_version !== false AND version_compare( COMMENTPRESS_VERSION, $_version, '>' ) ) {
  409. // override
  410. $result = true;
  411. }
  412. // --<
  413. return $result;
  414. }
  415. /**
  416. * @description: save the settings set by the administrator
  417. * @return boolean success or failure
  418. * @todo: do more error checking?
  419. *
  420. */
  421. function options_update() {
  422. // database object
  423. global $wpdb;
  424. // init result
  425. $result = false;
  426. // was the form submitted?
  427. if( isset( $_POST[ 'commentpress_submit' ] ) ) {
  428. // check that we trust the source of the data
  429. check_admin_referer( 'commentpress_admin_action', 'commentpress_nonce' );
  430. // init vars
  431. $cp_activate = '0';
  432. $cp_upgrade = '';
  433. $cp_reset = '';
  434. $cp_create_pages = '';
  435. $cp_delete_pages = '';
  436. $cp_para_comments_live = 0;
  437. $cp_show_subpages = 0;
  438. $cp_show_extended_toc = 0;
  439. $cp_blog_type = 0;
  440. $cp_blog_workflow = 0;
  441. $cp_sidebar_default = 'comments';
  442. // get variables
  443. extract( $_POST );
  444. // hand off to Multisite first, in case we're deactivating
  445. do_action( 'cpmu_deactivate_commentpress' );
  446. // is Multisite activating CommentPress Core?
  447. if ( $cp_activate == '1' ) { return true; }
  448. // did we ask to upgrade CommentPress Core?
  449. if ( $cp_upgrade == '1' ) {
  450. // do upgrade
  451. $this->upgrade();
  452. // --<
  453. return true;
  454. }
  455. // did we ask to reset?
  456. if ( $cp_reset == '1' ) {
  457. // reset theme options
  458. $this->_options_reset();
  459. // --<
  460. return true;
  461. }
  462. // did we ask to auto-create special pages?
  463. if ( $cp_create_pages == '1' ) {
  464. // remove any existing special pages
  465. $this->delete_special_pages();
  466. // create special pages
  467. $this->create_special_pages();
  468. }
  469. // did we ask to delete special pages?
  470. if ( $cp_delete_pages == '1' ) {
  471. // remove special pages
  472. $this->delete_special_pages();
  473. }
  474. // let's deal with our params now
  475. // individual special pages
  476. //$cp_welcome_page = $wpdb->escape( $cp_welcome_page );
  477. //$cp_blog_page = $wpdb->escape( $cp_blog_page );
  478. //$cp_general_comments_page = $wpdb->escape( $cp_general_comments_page );
  479. //$cp_all_comments_page = $wpdb->escape( $cp_all_comments_page );
  480. //$cp_comments_by_page = $wpdb->escape( $cp_comments_by_page );
  481. //$this->option_set( 'cp_welcome_page', $cp_welcome_page );
  482. //$this->option_set( 'cp_blog_page', $cp_blog_page );
  483. //$this->option_set( 'cp_general_comments_page', $cp_general_comments_page );
  484. //$this->option_set( 'cp_all_comments_page', $cp_all_comments_page );
  485. //$this->option_set( 'cp_comments_by_page', $cp_comments_by_page );
  486. // TOC content
  487. $cp_show_posts_or_pages_in_toc = $wpdb->escape( $cp_show_posts_or_pages_in_toc );
  488. $this->option_set( 'cp_show_posts_or_pages_in_toc', $cp_show_posts_or_pages_in_toc );
  489. // if we have pages in TOC and a value for the next param...
  490. if ( $cp_show_posts_or_pages_in_toc == 'page' AND isset( $cp_toc_chapter_is_page ) ) {
  491. $cp_toc_chapter_is_page = $wpdb->escape( $cp_toc_chapter_is_page );
  492. $this->option_set( 'cp_toc_chapter_is_page', $cp_toc_chapter_is_page );
  493. // if chapters are not pages and we have a value for the next param...
  494. if ( $cp_toc_chapter_is_page == '0' ) {
  495. $cp_show_subpages = $wpdb->escape( $cp_show_subpages );
  496. $this->option_set( 'cp_show_subpages', ( $cp_show_subpages ? 1 : 0 ) );
  497. } else {
  498. // always set to show subpages
  499. $this->option_set( 'cp_show_subpages', 1 );
  500. }
  501. }
  502. // extended or vanilla posts TOC
  503. if ( $cp_show_posts_or_pages_in_toc == 'post' ) {
  504. $cp_show_extended_toc = $wpdb->escape( $cp_show_extended_toc );
  505. $this->option_set( 'cp_show_extended_toc', ( $cp_show_extended_toc ? 1 : 0 ) );
  506. }
  507. // excerpt length
  508. $this->option_set( 'cp_excerpt_length', $cp_excerpt_length );
  509. $cp_excerpt_length = $wpdb->escape( $cp_excerpt_length );
  510. // comment editor
  511. $cp_comment_editor = $wpdb->escape( $cp_comment_editor );
  512. $this->option_set( 'cp_comment_editor', ( $cp_comment_editor ? 1 : 0 ) );
  513. // has AJAX "live" comment refreshing been migrated?
  514. if ( $this->option_exists( 'cp_para_comments_live' ) ) {
  515. // "live" comment refreshing
  516. $cp_para_comments_live = $wpdb->escape( $cp_para_comments_live );
  517. $this->option_set( 'cp_para_comments_live', ( $cp_para_comments_live ? 1 : 0 ) );
  518. }
  519. // behaviour
  520. $cp_promote_reading = $wpdb->escape( $cp_promote_reading );
  521. $this->option_set( 'cp_promote_reading', ( $cp_promote_reading ? 1 : 0 ) );
  522. // title visibility
  523. $cp_title_visibility = $wpdb->escape( $cp_title_visibility );
  524. $this->option_set( 'cp_title_visibility', $cp_title_visibility );
  525. // page meta visibility
  526. $cp_page_meta_visibility = $wpdb->escape( $cp_page_meta_visibility );
  527. $this->option_set( 'cp_page_meta_visibility', $cp_page_meta_visibility );
  528. // header background colour
  529. // strip our rgb #
  530. if ( stristr( $cp_header_bg_colour, '#' ) ) {
  531. $cp_header_bg_colour = substr( $cp_header_bg_colour, 1 );
  532. }
  533. // reset to default if blank
  534. if ( $cp_header_bg_colour == '' ) {
  535. $cp_header_bg_colour = $this->header_bg_colour;
  536. }
  537. // save it
  538. $cp_header_bg_colour = $wpdb->escape( $cp_header_bg_colour );
  539. $this->option_set( 'cp_header_bg_colour', $cp_header_bg_colour );
  540. // save scroll speed
  541. $cp_js_scroll_speed = $wpdb->escape( $cp_js_scroll_speed );
  542. $this->option_set( 'cp_js_scroll_speed', $cp_js_scroll_speed );
  543. // save min page width
  544. $cp_min_page_width = $wpdb->escape( $cp_min_page_width );
  545. $this->option_set( 'cp_min_page_width', $cp_min_page_width );
  546. // save workflow
  547. $cp_blog_workflow = $wpdb->escape( $cp_blog_workflow );
  548. $this->option_set( 'cp_blog_workflow', ( $cp_blog_workflow ? 1 : 0 ) );
  549. // save blog type
  550. $cp_blog_type = $wpdb->escape( $cp_blog_type );
  551. $this->option_set( 'cp_blog_type', $cp_blog_type );
  552. // if it's a groupblog
  553. if ( $this->parent_obj->is_groupblog() ) {
  554. // get the group's id
  555. $group_id = get_groupblog_group_id( get_current_blog_id() );
  556. if ( is_numeric( $group_id ) ) {
  557. // allow plugins to override the blog type - for example if workflow is enabled,
  558. // it might become a new blog type as far as buddypress is concerned
  559. $_blog_type = apply_filters( 'cp_get_group_meta_for_blog_type', $cp_blog_type, $cp_blog_workflow );
  560. // set the type as group meta info
  561. groups_update_groupmeta( $group_id, 'groupblogtype', 'groupblogtype-'.$_blog_type );
  562. }
  563. }
  564. // save default sidebar
  565. $cp_sidebar_default = $wpdb->escape( $cp_sidebar_default );
  566. $this->option_set( 'cp_sidebar_default', $cp_sidebar_default );
  567. // save
  568. $this->options_save();
  569. // set flag
  570. $result = true;
  571. }
  572. // --<
  573. return $result;
  574. }
  575. /**
  576. * @description: upgrade CommentPress Core options to array
  577. * @todo:
  578. *
  579. */
  580. function options_save() {
  581. // set option
  582. return $this->option_wp_set( 'commentpress_options', $this->commentpress_options );
  583. }
  584. /**
  585. * @description: return a value for a specified option
  586. * @todo:
  587. */
  588. function option_exists( $option_name = '' ) {
  589. // test for null
  590. if ( $option_name == '' ) {
  591. // oops
  592. die( __( 'You must supply an option to option_exists()', 'commentpress-core' ) );
  593. }
  594. // get option with unlikely default
  595. return array_key_exists( $option_name, $this->commentpress_options );
  596. }
  597. /**
  598. * @description: return a value for a specified option
  599. * @todo:
  600. */
  601. function option_get( $option_name = '', $default = false ) {
  602. // test for null
  603. if ( $option_name == '' ) {
  604. // oops
  605. die( __( 'You must supply an option to option_get()', 'commentpress-core' ) );
  606. }
  607. // get option
  608. return ( array_key_exists( $option_name, $this->commentpress_options ) ) ? $this->commentpress_options[ $option_name ] : $default;
  609. }
  610. /**
  611. * @description: sets a value for a specified option
  612. * @todo:
  613. */
  614. function option_set( $option_name = '', $value = '' ) {
  615. // test for null
  616. if ( $option_name == '' ) {
  617. // oops
  618. die( __( 'You must supply an option to option_set()', 'commentpress-core' ) );
  619. }
  620. // test for other than string
  621. if ( !is_string( $option_name ) ) {
  622. // oops
  623. die( __( 'You must supply the option as a string to option_set()', 'commentpress-core' ) );
  624. }
  625. // set option
  626. $this->commentpress_options[ $option_name ] = $value;
  627. }
  628. /**
  629. * @description: deletes a specified option
  630. * @todo:
  631. */
  632. function option_delete( $option_name = '' ) {
  633. // test for null
  634. if ( $option_name == '' ) {
  635. // oops
  636. die( __( 'You must supply an option to option_delete()', 'commentpress-core' ) );
  637. }
  638. // unset option
  639. unset( $this->commentpress_options[ $option_name ] );
  640. }
  641. /**
  642. * @description: return a value for a specified option
  643. * @todo:
  644. */
  645. function option_wp_exists( $option_name = '' ) {
  646. // test for null
  647. if ( $option_name == '' ) {
  648. // oops
  649. die( __( 'You must supply an option to option_wp_exists()', 'commentpress-core' ) );
  650. }
  651. // get option with unlikely default
  652. if ( $this->option_wp_get( $option_name, 'fenfgehgejgrkj' ) == 'fenfgehgejgrkj' ) {
  653. // no
  654. return false;
  655. } else {
  656. // yes
  657. return true;
  658. }
  659. }
  660. /**
  661. * @description: return a value for a specified option
  662. * @todo:
  663. */
  664. function option_wp_get( $option_name = '', $default = false ) {
  665. // test for null
  666. if ( $option_name == '' ) {
  667. // oops
  668. die( __( 'You must supply an option to option_wp_get()', 'commentpress-core' ) );
  669. }
  670. // get option
  671. return get_option( $option_name, $default );
  672. }
  673. /**
  674. * @description: sets a value for a specified option
  675. * @todo:
  676. */
  677. function option_wp_set( $option_name = '', $value = null ) {
  678. // test for null
  679. if ( $option_name == '' ) {
  680. // oops
  681. die( __( 'You must supply an option to option_wp_set()', 'commentpress-core' ) );
  682. }
  683. // set option
  684. return update_option( $option_name, $value );
  685. }
  686. /**
  687. * @description: get default header bg colour
  688. * @todo:
  689. */
  690. function option_get_header_bg() {
  691. // test for option
  692. if ( $this->option_exists( 'cp_header_bg_colour' ) ) {
  693. // --<
  694. return $this->option_get( 'cp_header_bg_colour' );
  695. } else {
  696. // --<
  697. return $this->header_bg_colour;
  698. }
  699. }
  700. /**
  701. * @description: when a page is saved, this also saves the CP options
  702. * @param object $post_obj the post object
  703. * @return boolean $result
  704. * @todo:
  705. *
  706. */
  707. function save_meta( $post_obj ) {
  708. // if no post, kick out
  709. if ( !$post_obj ) { return; }
  710. // if page...
  711. if ( $post_obj->post_type == 'page' ) {
  712. $this->save_page_meta( $post_obj );
  713. }
  714. // if post...
  715. if ( $post_obj->post_type == 'post' ) {
  716. $this->save_post_meta( $post_obj );
  717. }
  718. }
  719. /**
  720. * @description: when a page is saved, this also saves the CP options
  721. * @param object $post_obj the post object
  722. * @return boolean $result
  723. * @todo:
  724. *
  725. */
  726. function save_page_meta( $post_obj ) {
  727. //print_r( 'data: '.$_data ); die();
  728. //print_r( '$post_obj->post_type: '.$post_obj->post_type ); die();
  729. //print_r( '$post_obj->ID: '.$post_obj->ID ); die();
  730. // if no post, kick out
  731. if ( !$post_obj ) { return; }
  732. // if not page, kick out
  733. if ( $post_obj->post_type != 'page' ) { return; }
  734. // authenticate
  735. $_nonce = isset( $_POST['commentpress_nonce'] ) ? $_POST['commentpress_nonce'] : '';
  736. if ( !wp_verify_nonce( $_nonce, 'commentpress_page_settings' ) ) { return; }
  737. // is this an auto save routine?
  738. if ( defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE ) { return; }
  739. // check permissions - 'edit_pages' is available to editor+
  740. if ( !current_user_can( 'edit_pages' ) ) { return; }
  741. // OK, we're authenticated
  742. // check for revision
  743. if ( $post_obj->post_type == 'revision' ) {
  744. // get parent
  745. if ( $post_obj->post_parent != 0 ) {
  746. $post = get_post( $post_obj->post_parent );
  747. } else {
  748. $post = $post_obj;
  749. }
  750. } else {
  751. $post = $post_obj;
  752. }
  753. // database object and post
  754. global $wpdb;
  755. // ---------------------------------------------------------------------
  756. // Show or Hide Page Meta
  757. // ---------------------------------------------------------------------
  758. // find and save the data
  759. $_data = ( isset( $_POST['cp_page_meta_visibility'] ) ) ? $_POST['cp_page_meta_visibility'] : 'hide';
  760. //print_r( '$_data: '.$_data ); die();
  761. //print_r( $post ); die();
  762. // set key
  763. $key = '_cp_page_meta_visibility';
  764. // if the custom field already has a value...
  765. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  766. // if empty string...
  767. if ( $_data === '' ) {
  768. // delete the meta_key
  769. delete_post_meta( $post->ID, $key );
  770. } else {
  771. // update the data
  772. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  773. }
  774. } else {
  775. // add the data
  776. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  777. }
  778. // ---------------------------------------------------------------------
  779. // Show or Hide Page Title
  780. // ---------------------------------------------------------------------
  781. // find and save the data
  782. $_data = ( isset( $_POST['cp_title_visibility'] ) ) ? $_POST['cp_title_visibility'] : 'show';
  783. //print_r( '$_data: '.$_data ); die();
  784. //print_r( $post ); die();
  785. // set key
  786. $key = '_cp_title_visibility';
  787. // if the custom field already has a value...
  788. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  789. // if empty string...
  790. if ( $_data === '' ) {
  791. // delete the meta_key
  792. delete_post_meta( $post->ID, $key );
  793. } else {
  794. // update the data
  795. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  796. }
  797. } else {
  798. // add the data
  799. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  800. }
  801. // ---------------------------------------------------------------------
  802. // Page Numbering - only first top level page is allowed to send this
  803. // ---------------------------------------------------------------------
  804. // was the value sent?
  805. if ( isset( $_POST['cp_number_format'] ) ) {
  806. // set meta key
  807. $key = '_cp_number_format';
  808. if (
  809. // do we need to check this, since only the first top level page
  810. // can now send this data? doesn't hurt to validate, I guess.
  811. $post->post_parent == '0' AND
  812. !$this->is_special_page() AND
  813. $post->ID == $this->parent_obj->nav->get_first_page()
  814. ) { // -->
  815. // get the data
  816. $_data = $_POST['cp_number_format'];
  817. //print_r( $post->ID ); die();
  818. // if the custom field already has a value...
  819. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  820. // if empty string...
  821. if ( $_data === '' ) {
  822. // delete the meta_key
  823. delete_post_meta( $post->ID, $key );
  824. } else {
  825. // update the data
  826. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  827. }
  828. } else {
  829. // add the data
  830. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  831. }
  832. }
  833. // delete this meta value from all other pages, because we may have altered
  834. // the relationship between pages, thus causing the page numbering to fail
  835. // get all pages including chapters
  836. $all_pages = $this->parent_obj->nav->get_book_pages( 'structural' );
  837. // if we have any pages...
  838. if ( count( $all_pages ) > 0 ) {
  839. // loop
  840. foreach( $all_pages AS $_page ) {
  841. // exclude first top level page
  842. if ( $post->ID != $_page->ID ) {
  843. // delete the meta value
  844. delete_post_meta( $_page->ID, $key );
  845. }
  846. }
  847. }
  848. }
  849. // ---------------------------------------------------------------------
  850. // Page Layout for Title Page -> to allow for Book Cover image
  851. // ---------------------------------------------------------------------
  852. // is this the title page?
  853. if ( $post->ID == $this->option_get( 'cp_welcome_page' ) ) {
  854. // find and save the data
  855. $_data = ( isset( $_POST['cp_page_layout'] ) ) ? $_POST['cp_page_layout'] : 'text';
  856. // set key
  857. $key = '_cp_page_layout';
  858. // if the custom field already has a value...
  859. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  860. // if empty string...
  861. if ( $_data === '' ) {
  862. // delete the meta_key
  863. delete_post_meta( $post->ID, $key );
  864. } else {
  865. // update the data
  866. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  867. }
  868. } else {
  869. // add the data
  870. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  871. }
  872. }
  873. // ---------------------------------------------------------------------
  874. // Override post formatter (override blog_type)
  875. // ---------------------------------------------------------------------
  876. // get the data
  877. $_data = ( isset( $_POST['cp_post_type_override'] ) ) ? $_POST['cp_post_type_override'] : '';
  878. //print_r( '$_data: '.$_data ); die();
  879. //print_r( $post ); die();
  880. // set key
  881. $key = '_cp_post_type_override';
  882. // if the custom field already has a value...
  883. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  884. // if empty string...
  885. if ( $_data === '' ) {
  886. // delete the meta_key
  887. delete_post_meta( $post->ID, $key );
  888. } else {
  889. // update the data
  890. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  891. }
  892. } else {
  893. // add the data
  894. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  895. }
  896. // ---------------------------------------------------------------------
  897. // Default Sidebar
  898. // ---------------------------------------------------------------------
  899. // do we have the option to choose the default sidebar (new in 3.3.3)?
  900. if ( $this->option_exists( 'cp_sidebar_default' ) ) {
  901. // find and save the data
  902. $_data = ( isset( $_POST['cp_sidebar_default'] ) ) ?
  903. $_POST['cp_sidebar_default'] :
  904. $this->db->option_get( 'cp_sidebar_default' );
  905. //print_r( '$_data: '.$_data ); die();
  906. //print_r( $post ); die();
  907. // set key
  908. $key = '_cp_sidebar_default';
  909. // if the custom field already has a value...
  910. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  911. // if empty string...
  912. if ( $_data === '' ) {
  913. // delete the meta_key
  914. delete_post_meta( $post->ID, $key );
  915. } else {
  916. // update the data
  917. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  918. }
  919. } else {
  920. // add the data
  921. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  922. }
  923. }
  924. }
  925. /**
  926. * @description: when a post is saved, this also saves the CP options
  927. * @param object $post_obj the post object
  928. * @return boolean $result
  929. * @todo:
  930. *
  931. */
  932. function save_post_meta( $post_obj ) {
  933. //print_r( 'data: '.$_data ); die();
  934. //print_r( '$post_obj->post_type: '.$post_obj->post_type ); die();
  935. //print_r( '$post_obj->ID: '.$post_obj->ID ); die();
  936. // if no post, kick out
  937. if ( !$post_obj ) { return; }
  938. // if not page, kick out
  939. if ( $post_obj->post_type != 'post' ) { return; }
  940. // authenticate
  941. $_nonce = isset( $_POST['commentpress_nonce'] ) ? $_POST['commentpress_nonce'] : '';
  942. if ( !wp_verify_nonce( $_nonce, 'commentpress_post_settings' ) ) { return; }
  943. // is this an auto save routine?
  944. if ( defined('DOING_AUTOSAVE') AND DOING_AUTOSAVE ) { return; }
  945. // check permissions - 'edit_posts' is available to contributor+
  946. if ( !current_user_can( 'edit_posts', $post_obj->ID ) ) { return; }
  947. // OK, we're authenticated
  948. // check for revision
  949. if ( $post_obj->post_type == 'revision' ) {
  950. // get parent
  951. if ( $post_obj->post_parent != 0 ) {
  952. $post = get_post( $post_obj->post_parent );
  953. } else {
  954. $post = $post_obj;
  955. }
  956. } else {
  957. $post = $post_obj;
  958. }
  959. // database object and post
  960. global $wpdb;
  961. // ---------------------------------------------------------------------
  962. // Override post formatter (override blog_type)
  963. // ---------------------------------------------------------------------
  964. // get the data
  965. $_formatter = ( isset( $_POST['cp_post_type_override'] ) ) ? $_POST['cp_post_type_override'] : '';
  966. //print_r( '$_data: '.$_data ); die();
  967. //print_r( $post ); die();
  968. // set key
  969. $key = '_cp_post_type_override';
  970. // if the custom field already has a value...
  971. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  972. // if empty string...
  973. if ( $_formatter === '' ) {
  974. // delete the meta_key
  975. delete_post_meta( $post->ID, $key );
  976. } else {
  977. // update the data
  978. update_post_meta( $post->ID, $key, $wpdb->escape( $_formatter ) );
  979. }
  980. } else {
  981. // add the data
  982. add_post_meta( $post->ID, $key, $wpdb->escape( $_formatter ) );
  983. }
  984. // ---------------------------------------------------------------------
  985. // Workflow
  986. // ---------------------------------------------------------------------
  987. // do we have the option to set workflow (new in 3.3.1)?
  988. if ( $this->option_exists( 'cp_blog_workflow' ) ) {
  989. // get workflow setting for the blog
  990. $_workflow = $this->option_get( 'cp_blog_workflow' );
  991. /*
  992. // ----------------
  993. // WORK IN PROGRESS
  994. // set key
  995. $key = '_cp_blog_workflow_override';
  996. // if the custom field already has a value...
  997. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  998. // get existing value
  999. $_workflow = get_post_meta( $post->ID, $key, true );
  1000. }
  1001. // ----------------
  1002. */
  1003. // if it's enabled for the blog or the post...
  1004. if ( $_workflow == '1' ) {
  1005. // notify plugins that workflow stuff needs saving
  1006. do_action( 'cp_workflow_save_post', $post );
  1007. }
  1008. /*
  1009. // ----------------
  1010. // WORK IN PROGRESS
  1011. // get the setting for the post (we do this after saving the extra
  1012. // post data because
  1013. $_formatter = ( isset( $_POST['cp_post_type_override'] ) ) ? $_POST['cp_post_type_override'] : '';
  1014. // if the custom field already has a value...
  1015. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  1016. // if empty string...
  1017. if ( $_data === '' ) {
  1018. // delete the meta_key
  1019. delete_post_meta( $post->ID, $key );
  1020. } else {
  1021. // update the data
  1022. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  1023. }
  1024. } else {
  1025. // add the data
  1026. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  1027. }
  1028. // ----------------
  1029. */
  1030. }
  1031. // ---------------------------------------------------------------------
  1032. // Default Sidebar
  1033. // ---------------------------------------------------------------------
  1034. // do we have the option to choose the default sidebar (new in 3.3.3)?
  1035. if ( $this->option_exists( 'cp_sidebar_default' ) ) {
  1036. // find and save the data
  1037. $_data = ( isset( $_POST['cp_sidebar_default'] ) ) ?
  1038. $_POST['cp_sidebar_default'] :
  1039. $this->db->option_get( 'cp_sidebar_default' );
  1040. //print_r( '$_data: '.$_data ); die();
  1041. //print_r( $post ); die();
  1042. // set key
  1043. $key = '_cp_sidebar_default';
  1044. // if the custom field already has a value...
  1045. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  1046. // if empty string...
  1047. if ( $_data === '' ) {
  1048. // delete the meta_key
  1049. delete_post_meta( $post->ID, $key );
  1050. } else {
  1051. // update the data
  1052. update_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  1053. }
  1054. } else {
  1055. // add the data
  1056. add_post_meta( $post->ID, $key, $wpdb->escape( $_data ) );
  1057. }
  1058. }
  1059. // ---------------------------------------------------------------------
  1060. // Create new post with content of current
  1061. // ---------------------------------------------------------------------
  1062. // find and save the data
  1063. $_data = ( isset( $_POST['commentpress_new_post'] ) ) ? $_POST['commentpress_new_post'] : '0';
  1064. /*
  1065. print_r( array(
  1066. '$_data' => $_data,
  1067. '$post ' => $post
  1068. ) ); die();
  1069. */
  1070. // do we want to create a new revision?
  1071. if ( $_data == '0' ) { return; }
  1072. // we need to make sure this only runs once
  1073. //print_r( $this->saved_post === false ? 'f' : 't' ); die();
  1074. if ( $this->saved_post === false ) {
  1075. $this->saved_post = true;
  1076. } else {
  1077. return;
  1078. }
  1079. // ---------------------------------------------------------------------
  1080. // we're through: create it
  1081. $new_post_id = $this->_create_new_post( $post );
  1082. // ---------------------------------------------------------------------
  1083. // Store ID of new version in current version
  1084. // ---------------------------------------------------------------------
  1085. // set key
  1086. $key = '_cp_newer_version';
  1087. // if the custom field already has a value...
  1088. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  1089. // if empty string...
  1090. if ( $_data === '' ) {
  1091. // delete the meta_key
  1092. delete_post_meta( $post->ID, $key );
  1093. } else {
  1094. // update the data
  1095. update_post_meta( $post->ID, $key, $new_post_id );
  1096. }
  1097. } else {
  1098. // add the data
  1099. add_post_meta( $post->ID, $key, $new_post_id );
  1100. }
  1101. // ---------------------------------------------------------------------
  1102. // Store incremental version number in new version
  1103. // ---------------------------------------------------------------------
  1104. // set key
  1105. $key = '_cp_version_count';
  1106. // if the custom field of our current post has a value...
  1107. if ( get_post_meta( $post->ID, $key, true ) !== '' ) {
  1108. // get current value
  1109. $value = get_post_meta( $post->ID, $key, true );
  1110. // increment
  1111. $value++;
  1112. } else {
  1113. // this must be the first new version (Draft 2)
  1114. $value = 2;
  1115. }
  1116. // add the data
  1117. add_post_meta( $new_post_id, $key, $value );
  1118. // ---------------------------------------------------------------------
  1119. // Store formatter in new version
  1120. // ---------------------------------------------------------------------
  1121. // set key
  1122. $key = '_cp_post_type_override';
  1123. // if we have one set...
  1124. if ( $_formatter != '' ) {
  1125. // add the data
  1126. add_post_meta( $new_post_id, $key, $wpdb->escape( $_formatter ) );
  1127. }
  1128. // allow plugins to hook into this
  1129. do_action( 'cp_workflow_save_copy', $new_post_id );
  1130. // get the edit post link
  1131. //$edit_link = get_edit_post_link( $new_post_id );
  1132. // redirect there?
  1133. }
  1134. /**
  1135. * @description: when a page is deleted, this makes sure that the CP options are synced
  1136. * @param object $post_id the post ID
  1137. * @return none
  1138. * @todo:
  1139. *
  1140. */
  1141. function delete_meta( $post_id ) {
  1142. // if no post, kick out
  1143. if ( !$post_id ) { return; }
  1144. // if it's our welcome page...
  1145. if ( $post_id == $this->option_get( 'cp_welcome_page' ) ) {
  1146. // delete option
  1147. $this->option_delete( 'cp_welcome_page' );
  1148. // save
  1149. $this->options_save();
  1150. }
  1151. // for posts with versions, we need to delete the version data for the previous version
  1152. // define key
  1153. $key = '_cp_newer_version';
  1154. // get posts with the about-to-be-deleted post_id (there will be only one, if at all)
  1155. $previous_versions = get_posts( array(
  1156. 'meta_key' => $key,
  1157. 'meta_value' => $post_id
  1158. ) );
  1159. // did we get one?
  1160. if ( count( $previous_versions ) > 0 ) {
  1161. // get it
  1162. $previous_version = $previous_versions[0];
  1163. // if the custom field has a value...
  1164. if ( get_post_meta( $previous_version->ID, $key, true ) !== '' ) {
  1165. // delete it
  1166. delete_post_meta( $previous_version->ID, $key );
  1167. }
  1168. }
  1169. }
  1170. /**
  1171. * @description: create all "special" pages
  1172. * @todo:
  1173. *
  1174. */
  1175. function create_special_pages() {
  1176. // one of the CommentPress Core themes MUST be active...
  1177. // or WordPress will fail to set the page templates for the pages that require them.
  1178. // Also, a user must be logged in for these pages to be associated with them.
  1179. // get special pages array, if it's there
  1180. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1181. // create welcome/title page, but don't add to special pages
  1182. $welcome = $this->_create_title_page();
  1183. // create general comments page
  1184. $special_pages[] = $this->_create_general_comments_page();
  1185. // create all comments page
  1186. $special_pages[] = $this->_create_all_comments_page();
  1187. // create comments by author page
  1188. $special_pages[] = $this->_create_comments_by_author_page();
  1189. // create blog page
  1190. $special_pages[] = $this->_create_blog_page();
  1191. // create blog archive page
  1192. $special_pages[] = $this->_create_blog_archive_page();
  1193. // create TOC page -> a convenience, let's us define a logo as attachment
  1194. $special_pages[] = $this->_create_toc_page();
  1195. // store the array of page IDs that were created
  1196. $this->option_set( 'cp_special_pages', $special_pages );
  1197. // save changes
  1198. $this->options_save();
  1199. }
  1200. /**
  1201. * @description: create a particular "special" page
  1202. * @todo:
  1203. *
  1204. */
  1205. function create_special_page( $_page ) {
  1206. // init
  1207. $new_id = false;
  1208. // get special pages array, if it's there
  1209. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1210. // switch by page
  1211. switch( $_page ) {
  1212. case 'title':
  1213. // create welcome/title page
  1214. $new_id = $this->_create_title_page();
  1215. break;
  1216. case 'general_comments':
  1217. // create general comments page
  1218. $new_id = $this->_create_general_comments_page();
  1219. break;
  1220. case 'all_comments':
  1221. // create all comments page
  1222. $new_id = $this->_create_all_comments_page();
  1223. break;
  1224. case 'comments_by_author':
  1225. // create comments by author page
  1226. $new_id = $this->_create_comments_by_author_page();
  1227. break;
  1228. case 'blog':
  1229. // create blog page
  1230. $new_id = $this->_create_blog_page();
  1231. break;
  1232. case 'blog_archive':
  1233. // create blog page
  1234. $new_id = $this->_create_blog_archive_page();
  1235. break;
  1236. case 'toc':
  1237. // create TOC page
  1238. $new_id = $this->_create_toc_page();
  1239. break;
  1240. }
  1241. // add to special pages
  1242. $special_pages[] = $new_id;
  1243. // reset option
  1244. $this->option_set( 'cp_special_pages', $special_pages );
  1245. // save changes
  1246. $this->options_save();
  1247. // --<
  1248. return $new_id;
  1249. }
  1250. /**
  1251. * @description: delete "special" pages
  1252. * @return boolean $success
  1253. * @todo:
  1254. *
  1255. */
  1256. function delete_special_pages() {
  1257. // init success flag
  1258. $success = true;
  1259. // only delete special pages if we have one of the CommentPress Core themes active
  1260. // because other themes may have a totally different way of presenting the
  1261. // content of the blog
  1262. // retrieve data on special pages
  1263. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1264. // if we have created any...
  1265. if ( is_array( $special_pages ) AND count( $special_pages ) > 0 ) {
  1266. // loop through them
  1267. foreach( $special_pages AS $special_page ) {
  1268. // bypass trash
  1269. $force_delete = true;
  1270. // try and delete each page...
  1271. if ( !wp_delete_post( $special_page, $force_delete ) ) {
  1272. // oops, set success flag to false
  1273. $success = false;
  1274. }
  1275. }
  1276. // delete the corresponding options
  1277. $this->option_delete( 'cp_special_pages' );
  1278. $this->option_delete( 'cp_blog_page' );
  1279. $this->option_delete( 'cp_blog_archive_page' );
  1280. $this->option_delete( 'cp_general_comments_page' );
  1281. $this->option_delete( 'cp_all_comments_page' );
  1282. $this->option_delete( 'cp_comments_by_page' );
  1283. $this->option_delete( 'cp_toc_page' );
  1284. // for now, keep welcome page - delete option when page is deleted
  1285. //$this->option_delete( 'cp_welcome_page' );
  1286. // save changes
  1287. $this->options_save();
  1288. // reset Wordpress internal page references
  1289. $this->_reset_wordpress_option( 'show_on_front' );
  1290. $this->_reset_wordpress_option( 'page_on_front' );
  1291. $this->_reset_wordpress_option( 'page_for_posts' );
  1292. }
  1293. // --<
  1294. return $success;
  1295. }
  1296. /**
  1297. * @description: delete a particular "special" page
  1298. * @return boolean $success
  1299. * @todo:
  1300. *
  1301. */
  1302. function delete_special_page( $_page ) {
  1303. // init success flag
  1304. $success = true;
  1305. // only delete a special page if we have one of the CommentPress Core themes active
  1306. // because other themes may have a totally different way of presenting the
  1307. // content of the blog
  1308. // get id of special page
  1309. switch( $_page ) {
  1310. case 'title':
  1311. // set flag
  1312. $flag = 'cp_welcome_page';
  1313. // reset Wordpress internal page references
  1314. $this->_reset_wordpress_option( 'show_on_front' );
  1315. $this->_reset_wordpress_option( 'page_on_front' );
  1316. break;
  1317. case 'general_comments':
  1318. // set flag
  1319. $flag = 'cp_general_comments_page';
  1320. break;
  1321. case 'all_comments':
  1322. // set flag
  1323. $flag = 'cp_all_comments_page';
  1324. break;
  1325. case 'comments_by_author':
  1326. // set flag
  1327. $flag = 'cp_comments_by_page';
  1328. break;
  1329. case 'blog':
  1330. // set flag
  1331. $flag = 'cp_blog_page';
  1332. // reset Wordpress internal page reference
  1333. $this->_reset_wordpress_option( 'page_for_posts' );
  1334. break;
  1335. case 'blog_archive':
  1336. // set flag
  1337. $flag = 'cp_blog_archive_page';
  1338. break;
  1339. case 'toc':
  1340. // set flag
  1341. $flag = 'cp_toc_page';
  1342. break;
  1343. }
  1344. // get page id
  1345. $page_id = $this->option_get( $flag );
  1346. // kick out if it doesn't exist
  1347. if ( !$page_id ) { return true; }
  1348. // delete option
  1349. $this->option_delete( $flag );
  1350. // bypass trash
  1351. $force_delete = true;
  1352. // try and delete the page...
  1353. if ( !wp_delete_post( $page_id, $force_delete ) ) {
  1354. // oops, set success flag to false
  1355. $success = false;
  1356. }
  1357. // retrieve data on special pages
  1358. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1359. // is it in our special pages array?
  1360. if ( in_array( $page_id, $special_pages ) ) {
  1361. // remove page id from array
  1362. $special_pages = array_diff( $special_pages, array( $page_id ) );
  1363. // reset option
  1364. $this->option_set( 'cp_special_pages', $special_pages );
  1365. }
  1366. // save changes
  1367. $this->options_save();
  1368. // --<
  1369. return $success;
  1370. }
  1371. /**
  1372. * @description: test if a page is a "special" page
  1373. * @return boolean $is_special_page
  1374. * @todo:
  1375. *
  1376. */
  1377. function is_special_page() {
  1378. // init flag
  1379. $is_special_page = false;
  1380. // access post object
  1381. global $post;
  1382. // do we have one?
  1383. if ( !is_object( $post ) ) {
  1384. // --<
  1385. return $is_special_page;
  1386. }
  1387. // get special pages
  1388. $special_pages = $this->option_get( 'cp_special_pages', array() );
  1389. // do we have a special page array?
  1390. if ( is_array( $special_pages ) AND count( $special_pages ) > 0 ) {
  1391. // is the current page one?
  1392. if ( in_array( $post->ID, $special_pages ) ) {
  1393. // it is...
  1394. $is_special_page = true;
  1395. }
  1396. }
  1397. // --<
  1398. return $is_special_page;
  1399. }
  1400. /**
  1401. * @description: check if a post allows comments to be posted
  1402. * @return boolean $allowed
  1403. * @todo:
  1404. *
  1405. */
  1406. function comments_enabled() {
  1407. // init return
  1408. $allowed = false;
  1409. // access post object
  1410. global $post;
  1411. // do we have one?
  1412. if ( !is_object( $post ) ) {
  1413. // --<
  1414. return $allowed;
  1415. }
  1416. // are comments enabled on this post?
  1417. if ( $post->comment_status == 'open' ) {
  1418. // set return
  1419. $allowed = true;
  1420. }
  1421. // --<
  1422. return $allowed;
  1423. }
  1424. /**
  1425. * @description: get Wordpress approved comments
  1426. * @param integer $post_id the ID of the post
  1427. * @return array $comments
  1428. * @todo:
  1429. *
  1430. */
  1431. function get_approved_comments( $post_ID ) {
  1432. // for Wordpress, we use the API
  1433. $comments = get_approved_comments( $post_ID );
  1434. // --<
  1435. return $comments;
  1436. }
  1437. /**
  1438. * @description: get all Wordpress comments for a post, unless paged
  1439. * @param integer $post_id the ID of the post
  1440. * @return array $comments
  1441. * @todo:
  1442. *
  1443. */
  1444. function get_all_comments( $post_ID ) {
  1445. // access post
  1446. global $post;
  1447. // get all by default
  1448. $pings = '';
  1449. // check what we're allowing
  1450. if ( is_object( $post ) AND $post->ping_status != 'open' ) {
  1451. $pings = '&type=comment';
  1452. }
  1453. // for Wordpress, we use the API
  1454. $comments = get_comments( 'post_id='.$post_ID.'&order=ASC'.$pings );
  1455. // --<
  1456. return $comments;
  1457. }
  1458. /**
  1459. * @description: get all comments for a post
  1460. * @param integer $post_id the ID of the post
  1461. * @return array $comments
  1462. * @todo:
  1463. *
  1464. */
  1465. function get_comments( $post_ID ) {
  1466. // database object
  1467. global $wpdb;
  1468. // get comments from db
  1469. $comments = $wpdb->get_results(
  1470. $wpdb->prepare(
  1471. "SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d",
  1472. $post_ID
  1473. )
  1474. );
  1475. // --<
  1476. return $comments;
  1477. }
  1478. /**
  1479. * @description: when a comment is saved, this also saves the text signature
  1480. * @param integer $comment_id the ID of the comment
  1481. * @return boolean $result
  1482. * @todo:
  1483. *
  1484. */
  1485. function save_comment_signature( $comment_ID ) {
  1486. // database object
  1487. global $wpdb;
  1488. // get text signature
  1489. $text_signature = ( isset( $_POST['text_signature'] ) ) ? $_POST['text_signature'] : '';
  1490. // did we get one?
  1491. if ( $text_signature != '' ) {
  1492. // escape it
  1493. $text_signature = $wpdb->escape( $text_signature );
  1494. // construct query
  1495. $query = $wpdb->prepare(
  1496. "UPDATE $wpdb->comments SET comment_signature = %s WHERE comment_ID = %d",
  1497. $text_signature,
  1498. $comment_ID
  1499. );
  1500. //var_dump( $query );
  1501. // store comment signature
  1502. $result = $wpdb->query( $query );
  1503. } else {
  1504. // set result to true... why not, eh?
  1505. $result = true;
  1506. }
  1507. // --<
  1508. return $result;
  1509. }
  1510. /**
  1511. * @description: when a comment is saved, this also saves the page it was submitted on. this allows
  1512. * us to point to the correct page of a multipage post without parsing the content every time
  1513. * @param integer $comment_id the ID of the comment
  1514. * @todo:
  1515. *
  1516. */
  1517. function save_comment_page( $comment_ID ) {
  1518. // database object
  1519. global $wpdb;
  1520. // is this a paged post?
  1521. if ( isset( $_POST['page'] ) AND is_numeric( $_POST['page'] ) ) {
  1522. // get text signature
  1523. $text_signature = ( isset( $_POST['text_signature'] ) ) ? $_POST['text_signature'] : '';
  1524. // is it a para-level comment?
  1525. if ( $text_signature != '' ) {
  1526. // get page number
  1527. $page_number = $wpdb->escape( $_POST['page'] );
  1528. // set key
  1529. $key = '_cp_comment_page';
  1530. // if the custom field already has a value...
  1531. if ( get_comment_meta( $comment_ID, $key, true ) != '' ) {
  1532. // update the data
  1533. update_comment_meta( $comment_ID, $key, $page_number );
  1534. } else {
  1535. // add the data
  1536. add_comment_meta( $comment_ID, $key, $page_number, true );
  1537. }
  1538. } else {
  1539. // top level comments are always page 1
  1540. //$page_number = 1;
  1541. }
  1542. }
  1543. }
  1544. /**
  1545. * @description: retrieves text signature by comment ID
  1546. * @param integer $comment_id the ID of the comment
  1547. * @return string $text_signature
  1548. * @todo:
  1549. *
  1550. */
  1551. function get_text_signature_by_comment_id( $comment_ID ) {
  1552. // database object
  1553. global $wpdb;
  1554. // query for signature
  1555. $text_signature = $wpdb->get_var(
  1556. $wpdb->prepare(
  1557. "SELECT comment_signature FROM $wpdb->comments WHERE comment_ID = %s",
  1558. $comment_ID
  1559. )
  1560. );
  1561. // --<
  1562. return $text_signature;
  1563. }
  1564. /**
  1565. * @description: store text sigs in a global - because some versions of PHP do not save properties!
  1566. * @param: array $sigs array of text signatures
  1567. * @todo:
  1568. *
  1569. */
  1570. function set_text_sigs( $sigs ) {
  1571. global $ffffff_sigs;
  1572. // store them
  1573. $ffffff_sigs = $sigs;
  1574. //var_dump( $ffffff_sigs );
  1575. }
  1576. /**
  1577. * @description: retrieve text sigs
  1578. * @return array $text_signatures
  1579. * @todo:
  1580. *
  1581. */
  1582. function get_text_sigs() {
  1583. global $ffffff_sigs;
  1584. //var_dump( $ffffff_sigs );
  1585. // get them
  1586. return $ffffff_sigs;
  1587. }
  1588. /**
  1589. * @description: get javascript for the plugin, context dependent
  1590. * @return string $script
  1591. * @todo:
  1592. *
  1593. */
  1594. function get_javascript_vars() {
  1595. // init return
  1596. $vars = array();
  1597. // add comments open
  1598. global $post;
  1599. // if we don't have a post (like on the 404 page)
  1600. if ( !is_object( $post ) ) {
  1601. // comments must be closed
  1602. $vars['cp_comments_open'] = 'n';
  1603. } else {
  1604. // check for post comment_status
  1605. $vars['cp_comments_open'] = ( $post->comment_status == 'open' ) ? 'y' : 'n';
  1606. }
  1607. // assume no admin bars
  1608. $vars['cp_wp_adminbar'] = 'n';
  1609. $vars['cp_bp_adminbar'] = 'n';
  1610. // are we showing the WP admin bar?
  1611. if ( function_exists( 'is_admin_bar_showing' ) AND is_admin_bar_showing() ) {
  1612. // we have it...
  1613. $vars['cp_wp_adminbar'] = 'y';
  1614. }
  1615. // are we logged in AND in a BuddyPress scenario?
  1616. if ( is_user_logged_in() AND $this->parent_obj->is_buddypress() ) {
  1617. // regardless of version, settings can be made in bp-custom.php
  1618. if ( defined( 'BP_DISABLE_ADMIN_BAR' ) AND BP_DISABLE_ADMIN_BAR ) {
  1619. // we've killed both admin bars
  1620. $vars['cp_bp_adminbar'] = 'n';
  1621. $vars['cp_wp_adminbar'] = 'n';
  1622. }
  1623. // check for BP versions prior to 1.6 (1.6 uses the WP admin bar instead of a custom one)
  1624. if ( !function_exists( 'bp_get_version' ) ) {
  1625. // but, this can already be overridden in bp-custom.php
  1626. if ( defined( 'BP_USE_WP_ADMIN_BAR' ) AND BP_USE_WP_ADMIN_BAR ) {
  1627. // not present
  1628. $vars['cp_bp_adminbar'] = 'n';
  1629. $vars['cp_wp_adminbar'] = 'y';
  1630. } else {
  1631. // let our javascript know
  1632. $vars['cp_bp_adminbar'] = 'y';
  1633. // recheck 'BP_DISABLE_ADMIN_BAR'
  1634. if ( defined( 'BP_DISABLE_ADMIN_BAR' ) AND BP_DISABLE_ADMIN_BAR ) {
  1635. // we've killed both admin bars
  1636. $vars['cp_bp_adminbar'] = 'n';
  1637. $vars['cp_wp_adminbar'] = 'n';
  1638. }
  1639. }
  1640. }
  1641. }
  1642. // add rich text editor
  1643. $vars['cp_tinymce'] = 1;
  1644. // check option
  1645. if (
  1646. $this->option_exists( 'cp_comment_editor' ) AND
  1647. $this->option_get( 'cp_comment_editor' ) != '1'
  1648. ) {
  1649. // don't add rich text editor
  1650. $vars['cp_tinymce'] = 0;
  1651. }
  1652. // add mobile var
  1653. $vars['cp_is_mobile'] = 0;
  1654. // is it a mobile?
  1655. if ( isset( $this->is_mobile ) AND $this->is_mobile ) {
  1656. // is mobile
  1657. $vars['cp_is_mobile'] = 1;
  1658. // don't add rich text editor
  1659. $vars['cp_tinymce'] = 0;
  1660. }
  1661. // add touch var
  1662. $vars['cp_is_touch'] = 0;
  1663. // is it a touch device?
  1664. if ( isset( $this->is_mobile_touch ) AND $this->is_mobile_touch ) {
  1665. // is touch
  1666. $vars['cp_is_touch'] = 1;
  1667. // don't add rich text editor
  1668. $vars['cp_tinymce'] = 0;
  1669. }
  1670. // add tablet var
  1671. $vars['cp_is_tablet'] = 0;
  1672. // is it a touch device?
  1673. if ( isset( $this->is_tablet ) AND $this->is_tablet ) {
  1674. // is touch
  1675. $vars['cp_is_tablet'] = 1;
  1676. // don't add rich text editor
  1677. $vars['cp_tinymce'] = 0;
  1678. }
  1679. // add rich text editor behaviour
  1680. $vars['cp_promote_reading'] = 1;
  1681. // check option
  1682. if (
  1683. $this->option_exists( 'cp_promote_reading' ) AND
  1684. $this->option_get( 'cp_promote_reading' ) != '1'
  1685. ) {
  1686. // promote commenting
  1687. $vars['cp_promote_reading'] = 0;
  1688. }
  1689. // add special page var
  1690. $vars['cp_special_page'] = ( $this->is_special_page() ) ? '1' : '0';
  1691. // are we in a BuddyPress scenario?
  1692. if ( $this->parent_obj->is_buddypress() ) {
  1693. // is it a component homepage?
  1694. if ( $this->parent_obj->is_buddypress_special_page() ) {
  1695. // treat them the way we do ours
  1696. $vars['cp_special_page'] = '1';
  1697. }
  1698. }
  1699. // get path
  1700. $url_info = parse_url( get_option('siteurl') );
  1701. // add path for cookies
  1702. $vars['cp_cookie_path'] = '/';
  1703. if ( isset( $url_info['path'] ) ) {
  1704. $vars['cp_cookie_path'] = trailingslashit( $url_info['path'] );
  1705. }
  1706. // add page
  1707. global $page;
  1708. $vars['cp_multipage_page'] = ( !empty( $page ) ) ? $page : 0;
  1709. // add path to template directory
  1710. $vars['cp_template_dir'] = get_template_directory_uri();
  1711. // add path to plugin directory
  1712. $vars['cp_plugin_dir'] = WP_PLUGIN_URL.'/'.str_replace(basename( __FILE__),"",plugin_basename(__FILE__));
  1713. // are chapters pages?
  1714. $vars['cp_toc_chapter_is_page'] = $this->option_get( 'cp_toc_chapter_is_page' );
  1715. // are subpages shown?
  1716. $vars['cp_show_subpages'] = $this->option_get( 'cp_show_subpages' );
  1717. // set default sidebar
  1718. $vars['cp_default_sidebar'] = $this->parent_obj->get_default_sidebar();
  1719. // set scroll speed
  1720. $vars['cp_js_scroll_speed'] = $this->option_get( 'cp_js_scroll_speed' );
  1721. // set min page width
  1722. $vars['cp_min_page_width'] = $this->option_get( 'cp_min_page_width' );
  1723. // set signup flag
  1724. $vars['cp_is_signup_page'] = '0';
  1725. // test for signup
  1726. if ( $this->parent_obj->is_signup_page() ) {
  1727. // set flag
  1728. $vars['cp_is_signup_page'] = '1';
  1729. }
  1730. // --<
  1731. return $vars;
  1732. }
  1733. /**
  1734. * @description: sets class properties for mobile browsers
  1735. * @todo:
  1736. *
  1737. */
  1738. function test_for_mobile() {
  1739. // do we have a user agent?
  1740. if ( isset( $_SERVER["HTTP_USER_AGENT"] ) ) {
  1741. // NOTE: keep an eye on touchphone agents
  1742. // get agent
  1743. $agent = $_SERVER["HTTP_USER_AGENT"];
  1744. // init touchphone array
  1745. $touchphones = array(
  1746. 'iPhone',
  1747. 'iPod',
  1748. 'Android',
  1749. 'BlackBerry9530',
  1750. 'LG-TU915 Obigo', // LG touch browser
  1751. 'LGE VX',
  1752. 'webOS', // Palm Pre, etc.
  1753. );
  1754. // loop through them
  1755. foreach( $touchphones AS $phone ) {
  1756. // test for its name in the agent string
  1757. if ( strpos( $agent, $phone ) !== false ) {
  1758. // set flag
  1759. $this->is_mobile_touch = true;
  1760. }
  1761. }
  1762. // the old Commentpress also includes Mobile_Detect
  1763. if ( !class_exists( 'Mobile_Detect' ) ) {
  1764. // use code from http://code.google.com/p/php-mobile-detect/
  1765. include( COMMENTPRESS_PLUGIN_PATH . 'commentpress-core/assets/includes/mobile-detect/Mobile_Detect.php' );
  1766. }
  1767. // init
  1768. $detect = new Mobile_Detect();
  1769. // is it mobile?
  1770. if ( $detect->isMobile() ) {
  1771. // set flag
  1772. $this->is_mobile = true;
  1773. }
  1774. // is it a tablet?
  1775. if ( $detect->isTablet() ) {
  1776. // set flag
  1777. $this->is_tablet = true;
  1778. }
  1779. }
  1780. }
  1781. //##############################################################################
  1782. /*
  1783. ============================================================================
  1784. PRIVATE METHODS
  1785. ============================================================================
  1786. */
  1787. /*
  1788. ---------------------------------------------------------------
  1789. Object Initialisation
  1790. ---------------------------------------------------------------
  1791. */
  1792. /**
  1793. * @description: object initialisation
  1794. * @todo:
  1795. *
  1796. */
  1797. function _init() {
  1798. // load options array
  1799. $this->commentpress_options = $this->option_wp_get( 'commentpress_options', $this->commentpress_options );
  1800. // if we don't have one
  1801. if ( count( $this->commentpress_options ) == 0 ) {
  1802. // if not in backend
  1803. if ( !is_admin() ) {
  1804. // init upgrade
  1805. //die( 'CommentPress Core upgrade required.' );
  1806. }
  1807. }
  1808. }
  1809. /**
  1810. * @description: create new post with content of existing
  1811. * @todo:
  1812. *
  1813. */
  1814. function _create_new_post( $post ) {
  1815. // define basics
  1816. $new_post = array(
  1817. 'post_status' => 'draft',
  1818. 'post_type' => 'post',
  1819. 'comment_status' => 'open',
  1820. 'ping_status' => 'open',
  1821. 'to_ping' => '', // quick fix for Windows
  1822. 'pinged' => '', // quick fix for Windows
  1823. 'post_content_filtered' => '', // quick fix for Windows
  1824. 'post_excerpt' => '' // quick fix for Windows
  1825. );
  1826. // add post-specific stuff
  1827. //print_r( $post ); die();
  1828. // default page title
  1829. $prefix = __( 'Copy of ', 'commentpress-core' );
  1830. // allow overrides of prefix
  1831. $prefix = apply_filters( 'commentpress_new_post_title_prefix', $prefix );
  1832. // set title, but allow overrides
  1833. $new_post['post_title'] = apply_filters( 'commentpress_new_post_title', $prefix.$post->post_title, $post );
  1834. // set excerpt, but allow overrides
  1835. $new_post['post_excerpt'] = apply_filters( 'commentpress_new_post_excerpt', $post->post_excerpt );
  1836. // set content, but allow overrides
  1837. $new_post['post_content'] = apply_filters( 'commentpress_new_post_content', $post->post_content );
  1838. // set post author, but allow overrides
  1839. $new_post['post_author'] = apply_filters( 'commentpress_new_post_author', $post->post_author );
  1840. // Insert the post into the database
  1841. $new_post_id = wp_insert_post( $new_post );
  1842. // --<
  1843. return $new_post_id;
  1844. }
  1845. /**
  1846. * @description: create "title" page
  1847. * @todo:
  1848. *
  1849. */
  1850. function _create_title_page() {
  1851. // get the option, if it exists
  1852. $page_exists = $this->option_get( 'cp_welcome_page' );
  1853. // don't create if we already have the option set
  1854. if ( $page_exists !== false AND is_numeric( $page_exists ) ) {
  1855. // get the page (the plugin may have been deactivated, then the page deleted)
  1856. $welcome = get_post( $page_exists );
  1857. // check that the page exists
  1858. if ( !is_null( $welcome ) ) {
  1859. // got it...
  1860. // we still ought to set Wordpress internal page references
  1861. $this->_store_wordpress_option( 'show_on_front', 'page' );
  1862. $this->_store_wordpress_option( 'page_on_front', $page_exists );
  1863. // --<
  1864. return $page_exists;
  1865. } else {
  1866. // page does not exist, continue on and create it
  1867. }
  1868. }
  1869. // define welcome/title page
  1870. $title = array(
  1871. 'post_status' => 'publish',
  1872. 'post_type' => 'page',
  1873. 'post_parent' => 0,
  1874. 'comment_status' => 'open',
  1875. 'ping_status' => 'closed',
  1876. 'to_ping' => '', // quick fix for Windows
  1877. 'pinged' => '', // quick fix for Windows
  1878. 'post_content_filtered' => '', // quick fix for Windows
  1879. 'post_excerpt' => '', // quick fix for Windows
  1880. 'menu_order' => 0
  1881. );
  1882. // add post-specific stuff
  1883. // default page title
  1884. $_title = __( 'Title Page', 'commentpress-core' );
  1885. // set, but allow overrides
  1886. $title['post_title'] = apply_filters( 'cp_title_page_title', $_title );
  1887. // default content
  1888. $content = __(
  1889. '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.
  1890. 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>.
  1891. 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'
  1892. );
  1893. // set, but allow overrides
  1894. $title['post_content'] = apply_filters( 'cp_title_page_content', $content );
  1895. // set template, but allow overrides
  1896. $title['page_template'] = apply_filters( 'cp_title_page_template', 'welcome.php' );
  1897. // Insert the post into the database
  1898. $title_id = wp_insert_post( $title );
  1899. // make sure it has the default formatter (0 = prose)
  1900. add_post_meta( $title_id, '_cp_post_type_override', '0' );
  1901. // store the option
  1902. $this->option_set( 'cp_welcome_page', $title_id );
  1903. // set Wordpress internal page references
  1904. $this->_store_wordpress_option( 'show_on_front', 'page' );
  1905. $this->_store_wordpress_option( 'page_on_front', $title_id );
  1906. // --<
  1907. return $title_id;
  1908. }
  1909. /**
  1910. * @description: create "general comments" page
  1911. * @todo:
  1912. *
  1913. */
  1914. function _create_general_comments_page() {
  1915. // define general comments page
  1916. $general_comments = array(
  1917. 'post_status' => 'publish',
  1918. 'post_type' => 'page',
  1919. 'post_parent' => 0,
  1920. 'comment_status' => 'open',
  1921. 'ping_status' => 'open',
  1922. 'to_ping' => '', // quick fix for Windows
  1923. 'pinged' => '', // quick fix for Windows
  1924. 'post_content_filtered' => '', // quick fix for Windows
  1925. 'post_excerpt' => '', // quick fix for Windows
  1926. 'menu_order' => 0
  1927. );
  1928. // add post-specific stuff
  1929. // default page title
  1930. $title = __( 'General Comments', 'commentpress-core' );
  1931. // set, but allow overrides
  1932. $general_comments['post_title'] = apply_filters( 'cp_general_comments_title', $title );
  1933. // default content
  1934. $content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
  1935. // set, but allow overrides
  1936. $general_comments['post_content'] = apply_filters( 'cp_general_comments_content', $content );
  1937. // set template, but allow overrides
  1938. $general_comments['page_template'] = apply_filters( 'cp_general_comments_template', 'comments-general.php' );
  1939. // Insert the post into the database
  1940. $general_comments_id = wp_insert_post( $general_comments );
  1941. // store the option
  1942. $this->option_set( 'cp_general_comments_page', $general_comments_id );
  1943. // --<
  1944. return $general_comments_id;
  1945. }
  1946. /**
  1947. * @description: create "all comments" page
  1948. * @todo:
  1949. *
  1950. */
  1951. function _create_all_comments_page() {
  1952. // define all comments page
  1953. $all_comments = array(
  1954. 'post_status' => 'publish',
  1955. 'post_type' => 'page',
  1956. 'post_parent' => 0,
  1957. 'comment_status' => 'closed',
  1958. 'ping_status' => 'closed',
  1959. 'to_ping' => '', // quick fix for Windows
  1960. 'pinged' => '', // quick fix for Windows
  1961. 'post_content_filtered' => '', // quick fix for Windows
  1962. 'post_excerpt' => '', // quick fix for Windows
  1963. 'menu_order' => 0
  1964. );
  1965. // add post-specific stuff
  1966. // default page title
  1967. $title = __( 'All Comments', 'commentpress-core' );
  1968. // set, but allow overrides
  1969. $all_comments['post_title'] = apply_filters( 'cp_all_comments_title', $title );
  1970. // default content
  1971. $content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
  1972. // set, but allow overrides
  1973. $all_comments['post_content'] = apply_filters( 'cp_all_comments_content', $content );
  1974. // set template, but allow overrides
  1975. $all_comments['page_template'] = apply_filters( 'cp_all_comments_template', 'comments-all.php' );
  1976. // Insert the post into the database
  1977. $all_comments_id = wp_insert_post( $all_comments );
  1978. // store the option
  1979. $this->option_set( 'cp_all_comments_page', $all_comments_id );
  1980. // --<
  1981. return $all_comments_id;
  1982. }
  1983. /**
  1984. * @description: create "comments by author" page
  1985. * @todo:
  1986. *
  1987. */
  1988. function _create_comments_by_author_page() {
  1989. // define comments by author page
  1990. $group = array(
  1991. 'post_status' => 'publish',
  1992. 'post_type' => 'page',
  1993. 'post_parent' => 0,
  1994. 'comment_status' => 'closed',
  1995. 'ping_status' => 'closed',
  1996. 'to_ping' => '', // quick fix for Windows
  1997. 'pinged' => '', // quick fix for Windows
  1998. 'post_content_filtered' => '', // quick fix for Windows
  1999. 'post_excerpt' => '', // quick fix for Windows
  2000. 'menu_order' => 0
  2001. );
  2002. // add post-specific stuff
  2003. // default page title
  2004. $title = __( 'Comments by Commenter', 'commentpress-core' );
  2005. // set, but allow overrides
  2006. $group['post_title'] = apply_filters( 'cp_comments_by_title', $title );
  2007. // default content
  2008. $content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
  2009. // set, but allow overrides
  2010. $group['post_content'] = apply_filters( 'cp_comments_by_content', $content );
  2011. // set template, but allow overrides
  2012. $group['page_template'] = apply_filters( 'cp_comments_by_template', 'comments-by.php' );
  2013. // Insert the post into the database
  2014. $group_id = wp_insert_post( $group );
  2015. // store the option
  2016. $this->option_set( 'cp_comments_by_page', $group_id );
  2017. // --<
  2018. return $group_id;
  2019. }
  2020. /**
  2021. * @description: create "blog" page
  2022. * @todo:
  2023. *
  2024. */
  2025. function _create_blog_page() {
  2026. // define blog page
  2027. $blog = array(
  2028. 'post_status' => 'publish',
  2029. 'post_type' => 'page',
  2030. 'post_parent' => 0,
  2031. 'comment_status' => 'closed',
  2032. 'ping_status' => 'closed',
  2033. 'to_ping' => '', // quick fix for Windows
  2034. 'pinged' => '', // quick fix for Windows
  2035. 'post_content_filtered' => '', // quick fix for Windows
  2036. 'post_excerpt' => '', // quick fix for Windows
  2037. 'menu_order' => 0
  2038. );
  2039. // add post-specific stuff
  2040. // default page title
  2041. $title = __( 'Blog', 'commentpress-core' );
  2042. // set, but allow overrides
  2043. $blog['post_title'] = apply_filters( 'cp_blog_page_title', $title );
  2044. // default content
  2045. $content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
  2046. // set, but allow overrides
  2047. $blog['post_content'] = apply_filters( 'cp_blog_page_content', $content );
  2048. // set template, but allow overrides
  2049. $blog['page_template'] = apply_filters( 'cp_blog_page_template', 'blog.php' );
  2050. // Insert the post into the database
  2051. $blog_id = wp_insert_post( $blog );
  2052. // store the option
  2053. $this->option_set( 'cp_blog_page', $blog_id );
  2054. // set Wordpress internal page reference
  2055. $this->_store_wordpress_option( 'page_for_posts', $blog_id );
  2056. // --<
  2057. return $blog_id;
  2058. }
  2059. /**
  2060. * @description: create "blog archive" page
  2061. * @todo:
  2062. *
  2063. */
  2064. function _create_blog_archive_page() {
  2065. // define blog page
  2066. $blog = array(
  2067. 'post_status' => 'publish',
  2068. 'post_type' => 'page',
  2069. 'post_parent' => 0,
  2070. 'comment_status' => 'closed',
  2071. 'ping_status' => 'closed',
  2072. 'to_ping' => '', // quick fix for Windows
  2073. 'pinged' => '', // quick fix for Windows
  2074. 'post_content_filtered' => '', // quick fix for Windows
  2075. 'post_excerpt' => '', // quick fix for Windows
  2076. 'menu_order' => 0
  2077. );
  2078. // add post-specific stuff
  2079. // default page title
  2080. $title = __( 'Blog Archive', 'commentpress-core' );
  2081. // set, but allow overrides
  2082. $blog['post_title'] = apply_filters( 'cp_blog_archive_page_title', $title );
  2083. // default content
  2084. $content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
  2085. // set, but allow overrides
  2086. $blog['post_content'] = apply_filters( 'cp_blog_archive_page_content', $content );
  2087. // set template, but allow overrides
  2088. $blog['page_template'] = apply_filters( 'cp_blog_archive_page_template', 'archives.php' );
  2089. // Insert the post into the database
  2090. $blog_id = wp_insert_post( $blog );
  2091. // store the option
  2092. $this->option_set( 'cp_blog_archive_page', $blog_id );
  2093. // --<
  2094. return $blog_id;
  2095. }
  2096. /**
  2097. * @description: create "table of contents" page
  2098. * @todo: NOT USED
  2099. *
  2100. */
  2101. function _create_toc_page() {
  2102. // define TOC page
  2103. $toc = array(
  2104. 'post_status' => 'publish',
  2105. 'post_type' => 'page',
  2106. 'post_parent' => 0,
  2107. 'comment_status' => 'closed',
  2108. 'ping_status' => 'closed',
  2109. 'to_ping' => '', // quick fix for Windows
  2110. 'pinged' => '', // quick fix for Windows
  2111. 'post_content_filtered' => '', // quick fix for Windows
  2112. 'post_excerpt' => '', // quick fix for Windows
  2113. 'menu_order' => 0
  2114. );
  2115. // default page title
  2116. $title = __( 'Table of Contents', 'commentpress-core' );
  2117. // set, but allow overrides
  2118. $toc['post_title'] = apply_filters( 'cp_toc_page_title', $title );
  2119. // default content
  2120. $content = __( 'Do not delete this page. Page content is generated with a custom template.', 'commentpress-core' );
  2121. // set, but allow overrides
  2122. $toc['post_content'] = apply_filters( 'cp_toc_page_content', $content );
  2123. // set template, but allow overrides
  2124. $toc['page_template'] = apply_filters( 'cp_toc_page_template', 'toc.php' );
  2125. // Insert the post into the database
  2126. $toc_id = wp_insert_post( $toc );
  2127. // store the option
  2128. $this->option_set( 'cp_toc_page', $toc_id );
  2129. // --<
  2130. return $toc_id;
  2131. }
  2132. /**
  2133. * @description: cancels comment paging because CP will not work with comment paging
  2134. * @todo:
  2135. */
  2136. function _cancel_comment_paging() {
  2137. // store option
  2138. $this->_store_wordpress_option( 'page_comments', '' );
  2139. }
  2140. /**
  2141. * @description: resets comment paging option when plugin is deactivated
  2142. * @todo:
  2143. */
  2144. function _reset_comment_paging() {
  2145. // reset option
  2146. $this->_reset_wordpress_option( 'page_comments' );
  2147. }
  2148. /**
  2149. * @description: clears widgets for a fresh start
  2150. * @todo:
  2151. */
  2152. function _clear_widgets() {
  2153. // set backup option
  2154. add_option( 'commentpress_sidebars_widgets', $this->option_wp_get( 'sidebars_widgets' ) );
  2155. // clear them
  2156. update_option( 'sidebars_widgets', null );
  2157. }
  2158. /**
  2159. * @description: resets widgets when plugin is deactivated
  2160. * @todo:
  2161. */
  2162. function _reset_widgets() {
  2163. // reset option
  2164. $this->_reset_wordpress_option( 'sidebars_widgets' );
  2165. }
  2166. /**
  2167. * @description: store Wordpress option
  2168. * @param string $name the name of the option
  2169. * @param mixed $value the value of the option
  2170. * @todo:
  2171. */
  2172. function _store_wordpress_option( $name, $value ) {
  2173. // set backup option
  2174. add_option( 'commentpress_'.$name, $this->option_wp_get( $name ) );
  2175. // set the Wordpress option
  2176. $this->option_wp_set( $name, $value );
  2177. }
  2178. /**
  2179. * @description: reset Wordpress option
  2180. * @param string $name the name of the option
  2181. * @todo:
  2182. */
  2183. function _reset_wordpress_option( $name ) {
  2184. // set the Wordpress option
  2185. $this->option_wp_set( $name, $this->option_wp_get( 'cp_'.$name ) );
  2186. // remove backup option
  2187. delete_option( 'commentpress_'.$name );
  2188. }
  2189. /**
  2190. * @description: create all basic CommentPress Core options
  2191. * @todo:
  2192. *
  2193. */
  2194. function _options_create() {
  2195. // init options array
  2196. $this->commentpress_options = array(
  2197. 'cp_show_posts_or_pages_in_toc' => $this->toc_content,
  2198. 'cp_toc_chapter_is_page' => $this->toc_chapter_is_page,
  2199. 'cp_show_subpages' => $this->show_subpages,
  2200. 'cp_show_extended_toc' => $this->show_extended_toc,
  2201. 'cp_title_visibility' => $this->title_visibility,
  2202. 'cp_page_meta_visibility' => $this->page_meta_visibility,
  2203. 'cp_header_bg_colour' => $this->header_bg_colour,
  2204. 'cp_js_scroll_speed' => $this->js_scroll_speed,
  2205. 'cp_min_page_width' => $this->min_page_width,
  2206. 'cp_comment_editor' => $this->comment_editor,
  2207. 'cp_promote_reading' => $this->promote_reading,
  2208. 'cp_excerpt_length' => $this->excerpt_length,
  2209. 'cp_para_comments_live' => $this->para_comments_live,
  2210. 'cp_blog_type' => $this->blog_type,
  2211. 'cp_blog_workflow' => $this->blog_workflow,
  2212. 'cp_sidebar_default' => $this->sidebar_default
  2213. );
  2214. // Paragraph-level comments enabled by default
  2215. add_option( 'commentpress_options', $this->commentpress_options );
  2216. }
  2217. /**
  2218. * @description: reset CommentPress Core options
  2219. * @todo:
  2220. *
  2221. */
  2222. function _options_reset() {
  2223. // TOC: show posts by default
  2224. $this->option_set( 'cp_show_posts_or_pages_in_toc', $this->toc_content );
  2225. // TOC: are chapters pages
  2226. $this->option_set( 'cp_toc_chapter_is_page', $this->toc_chapter_is_page );
  2227. // TOC: if pages are shown, show subpages by default
  2228. $this->option_set( 'cp_show_subpages', $this->show_subpages );
  2229. // TOC: show extended post list
  2230. $this->option_set( 'cp_show_extended_toc', $this->show_extended_toc );
  2231. // comment editor
  2232. $this->option_set( 'cp_comment_editor', $this->comment_editor );
  2233. // promote reading or commenting
  2234. $this->option_set( 'cp_promote_reading', $this->promote_reading );
  2235. // show or hide titles
  2236. $this->option_set( 'cp_title_visibility', $this->title_visibility );
  2237. // show or hide page meta
  2238. $this->option_set( 'cp_page_meta_visibility', $this->page_meta_visibility );
  2239. // header background colour
  2240. $this->option_set( 'cp_header_bg_colour', $this->header_bg_colour );
  2241. // js scroll speed
  2242. $this->option_set( 'cp_js_scroll_speed', $this->js_scroll_speed );
  2243. // minimum page width
  2244. $this->option_set( 'cp_min_page_width', $this->min_page_width );
  2245. // "live" comment refeshing
  2246. $this->option_set( 'cp_para_comments_live', $this->para_comments_live );
  2247. // Blog: excerpt length
  2248. $this->option_set( 'cp_excerpt_length', $this->excerpt_length );
  2249. // workflow
  2250. $this->option_set( 'cp_blog_workflow', $this->blog_workflow );
  2251. // blog type
  2252. $this->option_set( 'cp_blog_type', $this->blog_type );
  2253. // default sidebar
  2254. $this->option_set( 'cp_sidebar_default', $this->sidebar_default );
  2255. // store it
  2256. $this->options_save();
  2257. }
  2258. /**
  2259. * @description: migrate all CommentPress Core options from old plugin
  2260. * @todo:
  2261. *
  2262. */
  2263. function _options_migrate() {
  2264. // get existing options
  2265. $old = get_option( 'cp_options', array() );
  2266. // ---------------------------------------------------------------------
  2267. // retrieve new ones, if they exist, or use defaults otherwise
  2268. // ---------------------------------------------------------------------
  2269. $this->toc_content = isset( $old[ 'cp_show_posts_or_pages_in_toc' ] ) ?
  2270. $old[ 'cp_show_posts_or_pages_in_toc' ] :
  2271. $this->toc_content;
  2272. $this->toc_chapter_is_page = isset( $old[ 'cp_toc_chapter_is_page' ] ) ?
  2273. $old[ 'cp_toc_chapter_is_page' ] :
  2274. $this->toc_chapter_is_page;
  2275. $this->show_subpages = isset( $old[ 'cp_show_subpages' ] ) ?
  2276. $old[ 'cp_show_subpages' ] :
  2277. $this->show_subpages;
  2278. $this->show_extended_toc = isset( $old[ 'cp_show_extended_toc' ] ) ?
  2279. $old[ 'cp_show_extended_toc' ] :
  2280. $this->show_extended_toc;
  2281. $this->title_visibility = isset( $old[ 'cp_title_visibility' ] ) ?
  2282. $old[ 'cp_title_visibility' ] :
  2283. $this->title_visibility;
  2284. $this->page_meta_visibility = isset( $old[ 'cp_page_meta_visibility' ] ) ?
  2285. $old[ 'cp_page_meta_visibility' ] :
  2286. $this->page_meta_visibility;
  2287. // header background colour
  2288. $header_bg_colour = isset( $old[ 'cp_header_bg_colour' ] ) ?
  2289. $old[ 'cp_header_bg_colour' ] :
  2290. $this->header_bg_colour;
  2291. // if it's the old default, upgrade to new default
  2292. if ( $header_bg_colour == '819565' ) {
  2293. $header_bg_colour = $this->header_bg_colour;
  2294. }
  2295. $this->js_scroll_speed = isset( $old[ 'cp_js_scroll_speed' ] ) ?
  2296. $old[ 'cp_js_scroll_speed' ] :
  2297. $this->js_scroll_speed;
  2298. $this->min_page_width = isset( $old[ 'cp_min_page_width' ] ) ?
  2299. $old[ 'cp_min_page_width' ] :
  2300. $this->min_page_width;
  2301. $this->comment_editor = isset( $old[ 'cp_comment_editor' ] ) ?
  2302. $old[ 'cp_comment_editor' ] :
  2303. $this->comment_editor;
  2304. $this->promote_reading = isset( $old[ 'cp_promote_reading' ] ) ?
  2305. $old[ 'cp_promote_reading' ] :
  2306. $this->promote_reading;
  2307. $this->excerpt_length = isset( $old[ 'cp_excerpt_length' ] ) ?
  2308. $old[ 'cp_excerpt_length' ] :
  2309. $this->excerpt_length;
  2310. $this->para_comments_live = isset( $old[ 'cp_para_comments_live' ] ) ?
  2311. $old[ 'cp_para_comments_live' ] :
  2312. $this->para_comments_live;
  2313. $blog_type = isset( $old[ 'cp_blog_type' ] ) ?
  2314. $old[ 'cp_blog_type' ] :
  2315. $this->blog_type;
  2316. $blog_workflow = isset( $old[ 'cp_blog_workflow' ] ) ?
  2317. $old[ 'cp_blog_workflow' ] :
  2318. $this->blog_workflow;
  2319. $this->sidebar_default = isset( $old[ 'cp_sidebar_default' ] ) ?
  2320. $old[ 'cp_sidebar_default' ] :
  2321. $this->sidebar_default;
  2322. // ---------------------------------------------------------------------
  2323. // special pages
  2324. // ---------------------------------------------------------------------
  2325. $special_pages = isset( $old[ 'cp_special_pages' ] ) ?
  2326. $old[ 'cp_special_pages' ] :
  2327. null;
  2328. $blog_page = isset( $old[ 'cp_blog_page' ] ) ?
  2329. $old[ 'cp_blog_page' ] :
  2330. null;
  2331. $blog_archive_page = isset( $old[ 'cp_blog_archive_page' ] ) ?
  2332. $old[ 'cp_blog_archive_page' ] :
  2333. null;
  2334. $general_comments_page = isset( $old[ 'cp_general_comments_page' ] ) ?
  2335. $old[ 'cp_general_comments_page' ] :
  2336. null;
  2337. $all_comments_page = isset( $old[ 'cp_all_comments_page' ] ) ?
  2338. $old[ 'cp_all_comments_page' ] :
  2339. null;
  2340. $comments_by_page = isset( $old[ 'cp_comments_by_page' ] ) ?
  2341. $old[ 'cp_comments_by_page' ] :
  2342. null;
  2343. $toc_page = isset( $old[ 'cp_toc_page' ] ) ?
  2344. $old[ 'cp_toc_page' ] :
  2345. null;
  2346. // init options array
  2347. $this->commentpress_options = array(
  2348. // basic options
  2349. 'cp_show_posts_or_pages_in_toc' => $this->toc_content,
  2350. 'cp_toc_chapter_is_page' => $this->toc_chapter_is_page,
  2351. 'cp_show_subpages' => $this->show_subpages,
  2352. 'cp_show_extended_toc' => $this->show_extended_toc,
  2353. 'cp_title_visibility' => $this->title_visibility,
  2354. 'cp_page_meta_visibility' => $this->page_meta_visibility,
  2355. 'cp_header_bg_colour' => $header_bg_colour,
  2356. 'cp_js_scroll_speed' => $this->js_scroll_speed,
  2357. 'cp_min_page_width' => $this->min_page_width,
  2358. 'cp_comment_editor' => $this->comment_editor,
  2359. 'cp_promote_reading' => $this->promote_reading,
  2360. 'cp_excerpt_length' => $this->excerpt_length,
  2361. 'cp_para_comments_live' => $this->para_comments_live,
  2362. 'cp_blog_type' => $blog_type,
  2363. 'cp_blog_workflow' => $blog_workflow,
  2364. 'cp_sidebar_default' => $this->sidebar_default
  2365. );
  2366. // if we have special pages
  2367. if ( !is_null( $special_pages ) AND is_array( $special_pages ) ) {
  2368. // let's have them as well
  2369. $pages = array(
  2370. // special pages
  2371. 'cp_special_pages' => $special_pages,
  2372. 'cp_blog_page' => $blog_page,
  2373. 'cp_blog_archive_page' => $blog_archive_page,
  2374. 'cp_general_comments_page' => $general_comments_page,
  2375. 'cp_all_comments_page' => $all_comments_page,
  2376. 'cp_comments_by_page' => $comments_by_page,
  2377. 'cp_toc_page' => $toc_page
  2378. );
  2379. // merge
  2380. $this->commentpress_options = array_merge( $this->commentpress_options, $pages );
  2381. // access old plugin
  2382. global $commentpress_obj;
  2383. // did we get it?
  2384. if ( is_object( $commentpress_obj ) ) {
  2385. // now delete the old Commentpress options
  2386. $commentpress_obj->db->option_delete( 'cp_special_pages' );
  2387. $commentpress_obj->db->option_delete( 'cp_blog_page' );
  2388. $commentpress_obj->db->option_delete( 'cp_blog_archive_page' );
  2389. $commentpress_obj->db->option_delete( 'cp_general_comments_page' );
  2390. $commentpress_obj->db->option_delete( 'cp_all_comments_page' );
  2391. $commentpress_obj->db->option_delete( 'cp_comments_by_page' );
  2392. $commentpress_obj->db->option_delete( 'cp_toc_page' );
  2393. // save changes
  2394. $commentpress_obj->db->options_save();
  2395. // extra page options: get existing backup, delete it, create new backup
  2396. // backup blog page reference
  2397. $page_for_posts = get_option( 'cp_page_for_posts' );
  2398. delete_option( 'cp_page_for_posts' );
  2399. add_option( 'commentpress_page_for_posts', $page_for_posts );
  2400. }
  2401. }
  2402. // ---------------------------------------------------------------------
  2403. // welcome page
  2404. // ---------------------------------------------------------------------
  2405. // welcome page is a bit of an oddity: deal with it here...
  2406. $welcome_page = isset( $old[ 'cp_welcome_page' ] ) ? $old[ 'cp_welcome_page' ] : null;
  2407. // did we get a welcome page?
  2408. if ( !is_null( $welcome_page ) ) {
  2409. // if the custom field already has a value...
  2410. if ( get_post_meta( $welcome_page, '_cp_post_type_override', true ) !== '' ) {
  2411. // leave the selected formatter alone
  2412. } else {
  2413. // make sure it has the default formatter (0 = prose)
  2414. add_post_meta( $welcome_page, '_cp_post_type_override', '0' );
  2415. }
  2416. // add it to our options
  2417. $this->commentpress_options['cp_welcome_page'] = $welcome_page;
  2418. }
  2419. // add the options to WordPress
  2420. add_option( 'commentpress_options', $this->commentpress_options );
  2421. // ---------------------------------------------------------------------
  2422. // backups
  2423. // ---------------------------------------------------------------------
  2424. // backup what to show as homepage
  2425. $show_on_front = get_option( 'cp_show_on_front' );
  2426. delete_option( 'cp_show_on_front' );
  2427. add_option( 'commentpress_show_on_front', $show_on_front );
  2428. // backup homepage id
  2429. $page_on_front = get_option( 'cp_page_on_front' );
  2430. delete_option( 'cp_page_on_front' );
  2431. add_option( 'commentpress_page_on_front', $page_on_front );
  2432. // backup comment paging
  2433. $page_comments = get_option( 'cp_page_comments' );
  2434. delete_option( 'cp_page_comments' );
  2435. add_option( 'commentpress_page_comments', $page_comments );
  2436. // ---------------------------------------------------------------------
  2437. // Theme Customizations
  2438. // ---------------------------------------------------------------------
  2439. // migrate Theme Customizations
  2440. $theme_settings = get_option( 'cp_theme_settings', array() );
  2441. // did we get any?
  2442. if ( !empty( $theme_settings ) ) {
  2443. // migrate them
  2444. add_option( 'commentpress_theme_settings', $theme_settings );
  2445. }
  2446. // migrate Theme Mods
  2447. $theme_mods = get_option( 'theme_mods_commentpress', array() );
  2448. // did we get any?
  2449. if ( is_array( $theme_mods ) AND count( $theme_mods ) > 0 ) {
  2450. // get header background image...
  2451. if ( isset( $theme_mods['header_image'] ) ) {
  2452. //print_r( 'here' ); die();
  2453. // is it a Commentpress one?
  2454. if ( strstr( $theme_mods['header_image'], 'style/images/header/caves.jpg' ) !== false ) {
  2455. // point it at the equivalent new version
  2456. $theme_mods['header_image'] = COMMENTPRESS_PLUGIN_URL.
  2457. 'themes/commentpress-theme'.
  2458. '/assets/images/header/caves-green.jpg';
  2459. }
  2460. }
  2461. /*
  2462. // if we wanted to clear widgets widgets...
  2463. if ( isset( $theme_mods['sidebars_widgets'] ) ) {
  2464. // remove them
  2465. unset( $theme_mods['sidebars_widgets'] );
  2466. }
  2467. // override widgets
  2468. $this->_clear_widgets();
  2469. */
  2470. // update theme mods (will create if it doesn't exist)
  2471. update_option( 'theme_mods_commentpress-theme', $theme_mods );
  2472. }
  2473. // ---------------------------------------------------------------------
  2474. // deactivate old Commentpress and Commentpress Ajaxified
  2475. // ---------------------------------------------------------------------
  2476. // get old Commentpress Ajaxified
  2477. $cpajax_old = commentpress_find_plugin_by_name( 'Commentpress Ajaxified' );
  2478. if ( $cpajax_old AND is_plugin_active( $cpajax_old ) ) { deactivate_plugins( $cpajax_old ); }
  2479. // get old Commentpress
  2480. $cp_old = commentpress_find_plugin_by_name( 'Commentpress' );
  2481. if ( $cp_old AND is_plugin_active( $cp_old ) ) { deactivate_plugins( $cp_old ); }
  2482. }
  2483. /**
  2484. * @description: upgrade Commentpress options to array (only for pre-CP3.2 upgrades)
  2485. * @todo:
  2486. *
  2487. */
  2488. function _options_upgrade() {
  2489. // populate options array with current values
  2490. $this->commentpress_options = array(
  2491. // theme settings we want to keep
  2492. 'cp_show_posts_or_pages_in_toc' => $this->option_wp_get( 'cp_show_posts_or_pages_in_toc' ),
  2493. 'cp_toc_chapter_is_page' => $this->option_wp_get( 'cp_toc_chapter_is_page'),
  2494. 'cp_show_subpages' => $this->option_wp_get( 'cp_show_subpages'),
  2495. 'cp_excerpt_length' => $this->option_wp_get( 'cp_excerpt_length'),
  2496. // migrate special pages
  2497. 'cp_special_pages' => $this->option_wp_get( 'cp_special_pages'),
  2498. 'cp_welcome_page' => $this->option_wp_get( 'cp_welcome_page'),
  2499. 'cp_general_comments_page' => $this->option_wp_get( 'cp_general_comments_page'),
  2500. 'cp_all_comments_page' => $this->option_wp_get( 'cp_all_comments_page'),
  2501. 'cp_comments_by_page' => $this->option_wp_get( 'cp_comments_by_page'),
  2502. 'cp_blog_page' => $this->option_wp_get( 'cp_blog_page'),
  2503. // store setting for what was independently set by the ajax commenting plugin, "off" by default
  2504. 'cp_para_comments_live' => $this->para_comments_live
  2505. );
  2506. // save options array
  2507. $this->options_save();
  2508. // delete all old options
  2509. $this->_options_delete_legacy();
  2510. }
  2511. /**
  2512. * @description: delete all legacy Commentpress options
  2513. * @todo:
  2514. *
  2515. */
  2516. function _options_delete_legacy() {
  2517. // delete paragraph-level commenting option
  2518. delete_option( 'cp_para_comments_enabled' );
  2519. // delete TOC options
  2520. delete_option( 'cp_show_posts_or_pages_in_toc' );
  2521. delete_option( 'cp_show_subpages' );
  2522. delete_option( 'cp_toc_chapter_is_page' );
  2523. // delete comment editor
  2524. delete_option( 'cp_comment_editor' );
  2525. // promote reading or commenting
  2526. delete_option( 'cp_promote_reading' );
  2527. // show or hide titles
  2528. delete_option( 'cp_title_visibility' );
  2529. // header bg colour
  2530. delete_option( 'cp_header_bg_colour' );
  2531. // header bg colour
  2532. delete_option( 'cp_js_scroll_speed' );
  2533. // header bg colour
  2534. delete_option( 'cp_min_page_width' );
  2535. // delete skin
  2536. delete_option( 'cp_default_skin' );
  2537. // window appearance options
  2538. delete_option( 'cp_default_left_position' );
  2539. delete_option( 'cp_default_top_position' );
  2540. delete_option( 'cp_default_width' );
  2541. delete_option( 'cp_default_height' );
  2542. // window behaviour options
  2543. delete_option( 'cp_allow_users_to_iconize' );
  2544. delete_option( 'cp_allow_users_to_minimize' );
  2545. delete_option( 'cp_allow_users_to_resize' );
  2546. delete_option( 'cp_allow_users_to_drag' );
  2547. delete_option( 'cp_allow_users_to_save_position' );
  2548. // blog options
  2549. delete_option( 'cp_excerpt_length' );
  2550. // "live" comment refreshing
  2551. delete_option( 'cp_para_comments_live' );
  2552. // special pages options
  2553. delete_option( 'cp_special_pages' );
  2554. delete_option( 'cp_welcome_page' );
  2555. delete_option( 'cp_general_comments_page' );
  2556. delete_option( 'cp_all_comments_page' );
  2557. delete_option( 'cp_comments_by_page' );
  2558. delete_option( 'cp_blog_page' );
  2559. }
  2560. //##############################################################################
  2561. } // class ends
  2562. ?>